Skip to content

Commit

Permalink
[ServerModLoader] Now checking cyclic dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 4, 2020
1 parent a3d5180 commit e0d8423
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
10 changes: 8 additions & 2 deletions source/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ extern "C" {

int main(int argc, char **argv) {
ClientApplication app(argc, argv);
app.run();

return 0;
int exitCode = app.run();

#ifdef _WIN32
if (exitCode != 0)
system("pause");
#endif

return exitCode;
}

73 changes: 54 additions & 19 deletions source/server/lua/ServerModLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,70 @@ void ServerModLoader::loadMods() {
mod.path = entry.path().string();
mod.id = config["id"];

if (mod.id != entry.path().filename().string())
gkWarning() << ("For mod '" + mod.id + "': Folder name '" + entry.path().filename().string() + "' is different from mod id").c_str();

auto it = mods.find(mod.id);
if (it == mods.end()) {
sol::table dependencies = config["dependencies"];
for (auto &it : dependencies) {
mod.dependencies.emplace_back(it.second.as<std::string>());
if (!mod.id.empty()) {
if (mod.id != entry.path().filename().string())
gkWarning() << ("Mod ID '" + mod.id + "' is different from folder name '" + entry.path().filename().string() + "'").c_str();

auto it = mods.find(mod.id);
if (it == mods.end()) {
sol::object dependencies = config["dependencies"];
if (dependencies.get_type() == sol::type::table) {
for (auto &it : dependencies.as<sol::table>()) {
mod.dependencies.emplace_back(it.second.as<std::string>());
}
}
else
gkWarning() << ("Failed to load dependencies for mod '" + mod.id + "'").c_str();

mods.emplace(mod.id, mod);
}
else {
gkError() << ("Trying to load mod '" + mod.id + "' twice").c_str();
gkError() << "Loaded path: " << it->second.path.string();
gkError() << "Current path:" << mod.path.string();
}

mods.emplace(mod.id, mod);
}
else {
gkWarning() << ("Trying to load mod '" + mod.id + "' twice").c_str();
gkWarning() << "Loaded path: " << it->second.path.string();
gkWarning() << "Current path:" << mod.path.string();
}
else
gkError() << ("Failed to load mod '" + mod.path.filename().string() + "': Mod ID required in 'config.lua'").c_str();
}
catch (sol::error &e) {
gkError() << e.what();
}

}
else
gkWarning() << ("The mod at '" + entry.path().string() + "' doesn't contain a 'config.lua' file.").c_str();
gkError() << ("The mod at '" + entry.path().string() + "' doesn't contain a 'config.lua' file.").c_str();
}

{
// Small BFS to check cyclic dependencies
for (auto &modit : mods) {
std::queue<std::string> queue;
queue.emplace(modit.first);
while (!queue.empty()) {
std::string modID = queue.front();
queue.pop();

auto it = mods.find(modID);
if (it == mods.end())
break;

for (auto &dep : it->second.dependencies) {
if (dep == modit.first)
throw EXCEPTION("Cyclic dependency detected for mod '"
+ modit.second.id + "' in mod '" + it->second.id + "'");

queue.emplace(dep);
}
}
}
}

// TODO: Build a dependency graph
// TODO: Make sure to handle cyclic dependencies
// Two types of dependencies should be handled
// - Required (cyclic depedencies not handled)
// - Optional (cyclic dependencies handled)

// TODO: Load 'mods/config.lua' to let user define a preferred mod path (see #82)

m_scriptEngine.init();
Expand All @@ -100,7 +135,7 @@ void ServerModLoader::loadMods() {
m_scriptEngine.lua().safe_script_file("init.lua");
}
catch (const sol::error &e) {
gkError() << "Error: Failed eo load mod at" << it.second.path.string();
gkError() << "Error: Failed to load mod at" << it.second.path.string();
gkError() << e.what();
}

Expand All @@ -109,7 +144,7 @@ void ServerModLoader::loadMods() {
gkInfo() << "Mod" << it.first << "loaded";
}
else
gkWarning() << ("The mod at '" + it.second.path.string() + "' doesn't contain an 'init.lua' file.").c_str();
gkError() << ("The mod at '" + it.second.path.string() + "' doesn't contain an 'init.lua' file.").c_str();
}

for (auto &it : m_mods) {
Expand Down
10 changes: 8 additions & 2 deletions source/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@

int main(int argc, char **argv) {
ServerApplication app(argc, argv);
app.run();

return 0;
int exitCode = app.run();

#ifdef _WIN32
if (exitCode != 0)
system("pause");
#endif

return exitCode;
}

0 comments on commit e0d8423

Please sign in to comment.