Xilmi / OpenXcom

Open-source clone of the original X-Com
http://openxcom.org
GNU General Public License v3.0
19 stars 10 forks source link

Basic LuaState with mods now operational #40

Closed ken-noland closed 5 months ago

ken-noland commented 5 months ago

Right, so the first pull request failed because I accidentally included some Lua stuff that shouldn't have been there. Rather than trying to fix up that branch by removing the Lua stuff, I'm including the Lua stuff with this pull request. Right now, the Lua stuff doesn't do much. It loads, and you can use Lua print to output to the log file.

Lua is only used if the lua file is specified in the metadata.yml for each mod.

if (doc["lua"].IsDefined()) { _luaScript = doc["lua"].as<std::string>(_id); }

If it sees that there is a tag, then it will load in that Lua file after the rules have been loaded.

    Log(LOG_INFO) << "Loading Lua...";
    for (const ModData& modData : _modData)
    {
        //okay, so this gets a bit tricky. The whole "mod" part was originally developed just to allow cascading rulesets, so
        // there is no central "mod" object that I can utilize for the LuaState object. What this means is that I have to
        // manage the life-cycle of the Lua stuff separately from everything else.
        if (modData.info->hasLua())
        {
            std::filesystem::path luaPath = modData.info->getPath() / modData.info->getLuaScript();
            _luaMods.push_back(LuaState(luaPath, &modData));
        }
    }
    Log(LOG_INFO) << "Loading Lua done.";

That's it for now with the Lua stuff. The next steps are to create the callback registers using multicast delegates. Once the callbacks are created, then that will allow Lua to intercept game events and actually DO something. What that something is, well, at this point it's too early to tell.

It's worth noting, yet again(same as the parent commit), that I've changed the YAML library from a binary dependency to a FetchContent in the CMake file. Similarly, I use FetchContent to get the lua library as well. I've updated the README.md to reflect that the library is no longer required.