Skip to content

Commit

Permalink
Update game_reload.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirkaan authored Feb 21, 2024
1 parent 530595a commit 25430aa
Showing 1 changed file with 118 additions and 84 deletions.
202 changes: 118 additions & 84 deletions src/game/functions/game_reload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,51 @@ GameReload::GameReload() = default;
GameReload::~GameReload() = default;

bool GameReload::init(Reload_t reloadTypes) const {
switch (reloadTypes) {
case Reload_t::RELOAD_TYPE_ALL:
return reloadAll();
case Reload_t::RELOAD_TYPE_CHAT:
return reloadChat();
case Reload_t::RELOAD_TYPE_CONFIG:
return reloadConfig();
case Reload_t::RELOAD_TYPE_EVENTS:
return reloadEvents();
case Reload_t::RELOAD_TYPE_CORE:
return reloadCore();
case Reload_t::RELOAD_TYPE_IMBUEMENTS:
return reloadImbuements();
case Reload_t::RELOAD_TYPE_ITEMS:
return reloadItems();
case Reload_t::RELOAD_TYPE_MODULES:
return reloadModules();
case Reload_t::RELOAD_TYPE_MONSTERS:
return reloadMonsters();
case Reload_t::RELOAD_TYPE_MOUNTS:
return reloadMounts();
case Reload_t::RELOAD_TYPE_NPCS:
return reloadNpcs();
case Reload_t::RELOAD_TYPE_RAIDS:
return reloadRaids();
case Reload_t::RELOAD_TYPE_SCRIPTS:
return reloadScripts();
case Reload_t::RELOAD_TYPE_GROUPS:
return reloadGroups();
default:
return false;
}
switch (reloadTypes) {
case Reload_t::RELOAD_TYPE_ALL:
return reloadAll();
case Reload_t::RELOAD_TYPE_CHAT:
return reloadChat();
case Reload_t::RELOAD_TYPE_CONFIG:
return reloadConfig();
case Reload_t::RELOAD_TYPE_EVENTS:
return reloadEvents();
case Reload_t::RELOAD_TYPE_CORE:
return reloadCore();
case Reload_t::RELOAD_TYPE_IMBUEMENTS:
return reloadImbuements();
case Reload_t::RELOAD_TYPE_ITEMS:
return reloadItems();
case Reload_t::RELOAD_TYPE_MODULES:
return reloadModules();
case Reload_t::RELOAD_TYPE_MONSTERS:
return reloadMonsters();
case Reload_t::RELOAD_TYPE_MOUNTS:
return reloadMounts();
case Reload_t::RELOAD_TYPE_NPCS:
return reloadNpcs();
case Reload_t::RELOAD_TYPE_RAIDS:
return reloadRaids();
case Reload_t::RELOAD_TYPE_SCRIPTS:
return reloadScripts();
case Reload_t::RELOAD_TYPE_GROUPS:
return reloadGroups();
default:
return false;
}
}

uint8_t GameReload::getReloadNumber(Reload_t reloadTypes) const {
return magic_enum::enum_integer(reloadTypes);
return magic_enum::enum_integer(reloadTypes);
}

// Helper function for logging reload status
void logReloadStatus(const std::string& name, bool result) {
if (result) {
g_logger().info("Reloaded: {}", name);
} else {
g_logger().error("Failed to reload: {}", name);
}
}

/*
Expand All @@ -68,102 +77,127 @@ uint8_t GameReload::getReloadNumber(Reload_t reloadTypes) const {
* Changing this to public may cause some unexpected behavior or bug
*/
bool GameReload::reloadAll() const {
std::vector<bool> reloadResults;
reloadResults.reserve(magic_enum::enum_count<Reload_t>());
std::vector<bool> reloadResults;
reloadResults.reserve(magic_enum::enum_count<Reload_t>());

for (auto value : magic_enum::enum_values<Reload_t>()) {
g_logger().info("Reloading: {}", magic_enum::enum_name(value));
if (value == Reload_t::RELOAD_TYPE_ALL) {
continue;
}
for (auto value : magic_enum::enum_values<Reload_t>()) {
const auto name = magic_enum::enum_name(value);
g_logger().info("Reloading: {}", name);
if (value != Reload_t::RELOAD_TYPE_ALL) {
reloadResults.push_back(init(value));
}
}

reloadResults.push_back(init(value));
}

return std::ranges::any_of(reloadResults, [](bool result) { return result; });
return std::ranges::any_of(reloadResults, [](bool result) { return result; });
}

bool GameReload::reloadChat() const {
return g_chat().load();
const bool result = g_chat().load();
logReloadStatus("Chat", result);
return result;
}

bool GameReload::reloadConfig() const {
return g_configManager().reload();
const bool result = g_configManager().reload();
logReloadStatus("Config", result);
return result;
}

bool GameReload::reloadEvents() const {
return g_events().loadFromXml();
const bool result = g_events().loadFromXml();
logReloadStatus("Events", result);
return result;
}

bool GameReload::reloadCore() const {
if (auto coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);
g_luaEnvironment().loadFile(coreFolder + "/core.lua", "core.lua") == 0) {
// Reload scripts lib
auto datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
if (!g_scripts().loadScripts(datapackFolder + "/scripts/lib", true, false)) {
return false;
}
const auto& coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);
const bool coreLoaded = g_luaEnvironment().loadFile(coreFolder + "/core.lua", "core.lua") == 0;

return true;
}
return false;
if (coreLoaded) {
const auto& datapackFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);
const bool scriptsLoaded = g_scripts().loadScripts(coreFolder + "/scripts/lib", true, false);
if (scriptsLoaded) {
return true;
}
}

logReloadStatus("Core", false);
return false;
}

bool GameReload::reloadImbuements() const {
return g_imbuements().reload();
const bool result = g_imbuements().reload();
logReloadStatus("Imbuements", result);
return result;
}

bool GameReload::reloadItems() const {
return Item::items.reload();
const bool result = Item::items.reload();
logReloadStatus("Items", result);
return result;
}

bool GameReload::reloadModules() const {
return g_modules().reload();
const bool result = g_modules().reload();
logReloadStatus("Modules", result);
return result;
}

bool GameReload::reloadMonsters() const {
// Clear registered MonsterType vector
g_monsters().clear();
// Resets monster spells to prevent the spell from being incorrectly cleared from memory
auto datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
if (!g_scripts().loadScripts(datapackFolder + "/scripts/lib", true, false)) {
return false;
}
const auto& datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
const auto& coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);

const bool scriptsLoaded = g_scripts().loadScripts(coreFolder + "/scripts/lib", true, false);
const bool monsterScriptsLoaded = g_scripts().loadScripts(datapackFolder + "/monster", false, true);

if (g_scripts().loadScripts(datapackFolder + "/monster", false, true) && g_scripts().loadScripts(datapackFolder + "/scripts/lib", true, true)) {
if (scriptsLoaded && monsterScriptsLoaded) {
logReloadStatus("Monsters", true);
return true;
} else {
logReloadStatus("Monsters", false);
return false;
}
return false;
}

bool GameReload::reloadMounts() const {
return g_game().mounts.reload();
const bool result = g_game().mounts.reload();
logReloadStatus("Mounts", result);
return result;
}

bool GameReload::reloadNpcs() const {
return g_npcs().reload();
const bool result = g_npcs().reload();
logReloadStatus("NPCs", result);
return result;
}

bool GameReload::reloadRaids() const {
return g_game().raids.reload() && g_game().raids.startup();
const bool result = g_game().raids.reload() && g_game().raids.startup();
logReloadStatus("Raids", result);
return result;
}

bool GameReload::reloadScripts() const {
g_scripts().clearAllScripts();
Zone::clearZones();
// Reset scripts lib to prevent the objects from being incorrectly cleared from memory
auto datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
g_scripts().loadScripts(datapackFolder + "/scripts/lib", true, false);
auto coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);
g_scripts().loadScripts(datapackFolder + "/scripts", false, true);
g_scripts().loadScripts(coreFolder + "/scripts", false, true);
g_scripts().clearAllScripts();
Zone::clearZones();

const auto& datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
const auto& coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);

g_scripts().loadScripts(coreFolder + "/scripts/lib", true, false);
g_scripts().loadScripts(datapackFolder + "/scripts", false, true);
g_scripts().loadScripts(coreFolder + "/scripts", false, true);

// It should come last, after everything else has been cleaned up.
reloadMonsters();
reloadNpcs();
return true;
// It should come last, after everything else has been cleaned up.
reloadMonsters();
reloadNpcs();
logReloadStatus("Scripts", true);
return true;
}

bool GameReload::reloadGroups() const {
return g_game().groups.reload();
const bool result = g_game().groups.reload();
logReloadStatus("Groups", result);
return result;
}

0 comments on commit 25430aa

Please sign in to comment.