| 89 | } |
| 90 | |
| 91 | void writeSource(std::ofstream &out, const StateDefinition &state, |
| 92 | const std::string &outputHeaderFile) |
| 93 | { |
| 94 | out << "// GENERATED SOURCE - generated by LuaGamestateSupportGen - do not modify directly\n\n"; |
| 95 | out << "#include \"framework/luaframework.h\"\n"; |
| 96 | out << "#include \"game/state/luagamestate_support.h\"\n"; |
| 97 | out << "#include \"" << outputHeaderFile << "\"\n\n"; |
| 98 | |
| 99 | out << "namespace OpenApoc {\n\n"; |
| 100 | |
| 101 | for (auto &object : state.objects) |
| 102 | { |
| 103 | out << "void pushToLua(lua_State *L, " << object.name << " &obj);\n" |
| 104 | << "void pushToLua(lua_State *L, const " << object.name << " &obj);\n"; |
| 105 | } |
| 106 | |
| 107 | for (auto &e : state.enums) |
| 108 | { |
| 109 | if (e.external == true) |
| 110 | continue; |
| 111 | out << "inline void pushToLua(lua_State *L, " << e.name << " val);\n"; |
| 112 | } |
| 113 | |
| 114 | out << "namespace\n{\n" |
| 115 | << "template<typename T> int getIndexMetamethod(lua_State *L);\n" |
| 116 | << "template<typename T> int getIndexConstMetamethod(lua_State *L);\n" |
| 117 | << "template<typename T> int newIndexMetamethod(lua_State *L);\n" |
| 118 | << "template<typename T> int toStringMetamethod(lua_State *L);\n"; |
| 119 | |
| 120 | for (auto &object : state.objects) |
| 121 | { |
| 122 | // non-const case |
| 123 | out << "template<> int getIndexMetamethod<" << object.name << ">(lua_State* L)\n" |
| 124 | << "{\n" |
| 125 | << "\t[[maybe_unused]]" << object.name << "** obj = (" << object.name |
| 126 | << "**)lua_touserdata(L, 1);\n" |
| 127 | << "\tstd::string key = lua_tostring(L, 2);\n" |
| 128 | << "\tlua_settop(L, 0);\n" |
| 129 | << "\t"; |
| 130 | for (auto &m : object.members) |
| 131 | { |
| 132 | out << "if (key == \"" << m.first << "\") pushToLua(L, (*obj)->" << m.first << ");\n" |
| 133 | << "\telse "; |
| 134 | } |
| 135 | out << "if (auto method = getLuaObjectMethods<" << object.name |
| 136 | << ">(key)) lua_pushcfunction(L, method);\n" |
| 137 | << "\telse lua_pushnil(L);\n" |
| 138 | << "\treturn 1;\n" |
| 139 | << "}\n"; |
| 140 | |
| 141 | // const case |
| 142 | out << "template<> int getIndexConstMetamethod<" << object.name << ">(lua_State* L)\n" |
| 143 | << "{\n" |
| 144 | << "\t[[maybe_unused]] const " << object.name << "** obj = (const " << object.name |
| 145 | << "**)lua_touserdata(L, 1);\n" |
| 146 | << "\tstd::string key = lua_tostring(L, 2);\n" |
| 147 | << "\tlua_settop(L, 0);\n" |
| 148 | << "\t"; |
no test coverage detected