]> git.tdb.fi Git - libs/game.git/blobdiff - tools/setupgen/setupgen.cpp
Support API decorations for setups
[libs/game.git] / tools / setupgen / setupgen.cpp
index 083fc8a96be3e6ec158d18e10de7ca993b2f0bb0..204045fbd364a90c5da1199cdb10176e07de633e 100644 (file)
@@ -1,6 +1,8 @@
 #include "setupgen.h"
+#include <algorithm>
 #include <msp/core/getopt.h>
 #include <msp/core/maputils.h>
+#include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
 #include <msp/io/console.h>
 #include <msp/io/print.h>
@@ -11,10 +13,15 @@ using namespace Msp;
 
 SetupGen::SetupGen(int argc, char **argv)
 {
+       vector<string> import_dirs;
+
        GetOpt getopt;
+       getopt.add_option('I', "importdir", import_dirs, GetOpt::REQUIRED_ARG);
        getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
        getopt.add_argument("input_file", in_fn, GetOpt::REQUIRED_ARG);
        getopt(argc, argv);
+
+       import_path.insert(import_path.end(), import_dirs.begin(), import_dirs.end());
 }
 
 int SetupGen::main()
@@ -99,6 +106,8 @@ void SetupGen::collect_headers(const Module &mod)
 {
        headers.insert("msp/datafile/objectloader.h");
        headers.insert("msp/strings/lexicalcast.h");
+       if(!mod.api_header.empty())
+               headers.insert(mod.api_header);
        for(const unique_ptr<Struct> &s: mod.structs)
                for(const Struct::Field &f: s->get_fields())
                        if(const string &h = f.type->get_header(); !h.empty())
@@ -124,6 +133,8 @@ void SetupGen::generate_header(const Module &mod, IO::Base &out) const
                        IO::print(out, "#include <%s>\n", h);
        }
 
+       IO::print(out, "\n#define API %s\n", mod.api);
+
        if(!mod.name_space.empty())
                IO::print(out, "\nnamespace %s {\n", mod.name_space);
 
@@ -189,20 +200,49 @@ SetupGen::Loader::Loader(SetupGen &s, Module &m):
 
 void SetupGen::Loader::init_actions()
 {
+       add("api", &Loader::api);
+       add("api", &Loader::api_with_header);
        add("component", &Loader::struct_def, Struct::COMPONENT);
        add("entity", &Loader::struct_def, Struct::ENTITY);
        add("enum", &Loader::enum_def);
+       add("import", &Loader::import);
        add("namespace", &Loader::name_space);
 }
 
+void SetupGen::Loader::api(const string &a)
+{
+       api_with_header(a, string());
+}
+
+void SetupGen::Loader::api_with_header(const string &a, const string &h)
+{
+       mod.api = a;
+       mod.api_header = h;
+}
+
 void SetupGen::Loader::enum_def(const DataFile::Symbol &n)
 {
        Enum en(n.name);
        load_sub(en);
        Type &type = obj.add_type(n.name, Type::ENUM);
+       type.set_cpp_type(join(mod.name_space, "::", type.get_cpp_type()));
        type.set_enum(*mod.enums.emplace_back(make_unique<Enum>(move(en))));
 }
 
+void SetupGen::Loader::import(const string &n)
+{
+       string fn = n+".mgs";
+       FS::Path full_path;
+       if(!ranges::any_of(obj.import_path, [&full_path, &fn](const FS::Path &p){
+                       full_path = p/fn;
+                       return FS::exists(full_path);
+               }))
+               throw IO::file_not_found(fn);
+
+       obj.load(full_path);
+       obj.headers.insert(n+".h");
+}
+
 void SetupGen::Loader::name_space(const string &ns)
 {
        mod.name_space = ns;
@@ -213,5 +253,6 @@ void SetupGen::Loader::struct_def(Struct::Kind kind, const DataFile::Symbol &n)
        Struct sct(n.name+"Setup", kind);
        load_sub(sct, obj);
        Type &type = obj.add_type(n.name, Type::STRUCT);
+       type.set_cpp_type(join(mod.name_space, "::", type.get_cpp_type()));
        type.set_struct(*mod.structs.emplace_back(make_unique<Struct>(move(sct))));
 }