DarkflameUniverse / DarkflameServer

The main repository for the Darkflame Universe Server Emulator project.
https://www.darkflameuniverse.org/
GNU Affero General Public License v3.0
651 stars 173 forks source link

ENH: Cleanup script loading #1511

Closed EmosewaMC closed 5 months ago

EmosewaMC commented 7 months ago

Is your feature request related to a problem?

Script loading is quite clunky as it stands right now, given its form of a giant if else block, we would like to see it be cleaned up a bit.

Describe the solution you'd like

Instead of the if else chain in the script loader, something like the following is wanted:

map<string, std::function<CppScripts::Script*()>> ScriptLoader = {
        {"scripts/test.lua", [](){ return new Test(); } },
        {"scripts/foo.lua", [](){ return new Foo(); } },
        // and so it goes
 };

When creating the initial copy of a script, the following would be done:

map<string, std::function<CppScripts::Script*()>> ScriptLoader = {
        {"scripts/test.lua", [](){ return new Test(); } },
        {"scripts/foo.lua", [](){ return new Foo(); } },
        // and so it goes
 };
std::map<string, CppScripts::Script*> cache;

CppScripts::Script* GetScript(string script_name) {
auto itr = cache.find(script_name);
if (itr != cache.end()) return itr->second;
auto itrLoad = ScriptLoader[script_name];
if (itrLoad == ScriptLoader.end()) {
    cache[script_name] = InvalidScript;
    return InvalidScript;    
}
auto toReturn = itrLoad->second();
cache[script_name] = toReturn;
return toReturn;
}

Repository breaking implications

During the migration, its possible an error could cause a script to be missing accidentally due to a typo or another issue

Describe alternatives you've considered

modifying windows build to support if else chains longer than 128 blocks

Additional context

Shoutouts windows for enforcing a limit that doesnt need to exist

EmosewaMC commented 7 months ago

Note: This issue is for someone else for right now.