From 10bf954c2b018d5682b9b0e801f2170f52d6a3e1 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Fri, 28 Jan 2022 00:51:44 +0100 Subject: [PATCH] Move mod commands to new component + small fix --- src/client/component/command.cpp | 56 -------------------- src/client/component/mods.cpp | 84 ++++++++++++++++++++++++++++++ src/client/resources/mods_menu.lua | 34 ++++++------ 3 files changed, 102 insertions(+), 72 deletions(-) create mode 100644 src/client/component/mods.cpp diff --git a/src/client/component/command.cpp b/src/client/component/command.cpp index a765be2a..b5350491 100644 --- a/src/client/component/command.cpp +++ b/src/client/component/command.cpp @@ -448,62 +448,6 @@ namespace command } }, scheduler::pipeline::server); }); - - add("loadmod", [](const params& params) - { - if (params.size() < 2) - { - game_console::print(game_console::con_type_info, "Usage: loadmod mods/"); - return; - } - - if (::game::SV_Loaded()) - { - game_console::print(game_console::con_type_error, "Cannot load mod while in-game!\n"); - game::CG_GameMessage(0, "^1Cannot unload mod while in-game!"); - return; - } - - const auto path = params.get(1); - game_console::print(game_console::con_type_info, "Loading mod %s\n", path); - - if (!utils::io::directory_exists(path)) - { - game_console::print(game_console::con_type_error, "Mod %s not found!\n", path); - return; - } - - game::mod_folder = path; - - ::scheduler::once([]() - { - command::execute("lui_restart", true); - }, ::scheduler::pipeline::renderer); - }); - - add("unloadmod", [](const params& params) - { - if (game::mod_folder.empty()) - { - game_console::print(game_console::con_type_info, "No mod loaded\n"); - return; - } - - if (::game::SV_Loaded()) - { - game_console::print(game_console::con_type_error, "Cannot unload mod while in-game!\n"); - game::CG_GameMessage(0, "^1Cannot unload mod while in-game!"); - return; - } - - game_console::print(game_console::con_type_info, "Unloading mod %s\n", game::mod_folder.data()); - game::mod_folder.clear(); - - ::scheduler::once([]() - { - command::execute("lui_restart", true); - }, ::scheduler::pipeline::renderer); - }); } }; } diff --git a/src/client/component/mods.cpp b/src/client/component/mods.cpp new file mode 100644 index 00000000..32b58052 --- /dev/null +++ b/src/client/component/mods.cpp @@ -0,0 +1,84 @@ +#include +#include "loader/component_loader.hpp" + +#include "game/game.hpp" + +#include "command.hpp" +#include "game_console.hpp" +#include "scheduler.hpp" + +#include +#include + +namespace mods +{ + class component final : public component_interface + { + public: + void post_unpack() override + { + if (!utils::io::directory_exists("mods")) + { + utils::io::create_directory("mods"); + } + + command::add("loadmod", [](const command::params& params) + { + if (params.size() < 2) + { + game_console::print(game_console::con_type_info, "Usage: loadmod mods/"); + return; + } + + if (::game::SV_Loaded()) + { + game_console::print(game_console::con_type_error, "Cannot load mod while in-game!\n"); + game::CG_GameMessage(0, "^1Cannot unload mod while in-game!"); + return; + } + + const auto path = params.get(1); + game_console::print(game_console::con_type_info, "Loading mod %s\n", path); + + if (!utils::io::directory_exists(path)) + { + game_console::print(game_console::con_type_error, "Mod %s not found!\n", path); + return; + } + + game::mod_folder = path; + + scheduler::once([]() + { + command::execute("lui_restart", true); + }, scheduler::pipeline::renderer); + }); + + command::add("unloadmod", [](const command::params& params) + { + if (game::mod_folder.empty()) + { + game_console::print(game_console::con_type_info, "No mod loaded\n"); + return; + } + + if (::game::SV_Loaded()) + { + game_console::print(game_console::con_type_error, "Cannot unload mod while in-game!\n"); + game::CG_GameMessage(0, "^1Cannot unload mod while in-game!"); + return; + } + + game_console::print(game_console::con_type_info, "Unloading mod %s\n", game::mod_folder.data()); + game::mod_folder.clear(); + + scheduler::once([]() + { + command::execute("lui_restart", true); + }, scheduler::pipeline::renderer); + }); + } + }; +} + +REGISTER_COMPONENT(mods::component) diff --git a/src/client/resources/mods_menu.lua b/src/client/resources/mods_menu.lua index 50edbc0f..b358cac6 100644 --- a/src/client/resources/mods_menu.lua +++ b/src/client/resources/mods_menu.lua @@ -74,22 +74,24 @@ function modsmenu(a1) createdivider(menu, "$_Available mods") - local mods = io.listfiles("mods/") - for i = 1, #mods do - local desc = "Load " .. mods[i] - local infofile = mods[i] .. "/mod.txt" - local exists = io.fileexists(infofile) - - if (exists) then - desc = io.readfile(infofile) - end - - if (mods[i] ~= modfolder) then - menu:AddButton("$_" .. mods[i], function() - game:executecommand("loadmod " .. mods[i]) - end, nil, true, nil, { - desc_text = desc - }) + if (io.directoryexists("mods")) then + local mods = io.listfiles("mods/") + for i = 1, #mods do + local desc = "Load " .. mods[i] + local infofile = mods[i] .. "/mod.txt" + local exists = io.fileexists(infofile) + + if (exists) then + desc = io.readfile(infofile) + end + + if (mods[i] ~= modfolder) then + menu:AddButton("$_" .. mods[i], function() + game:executecommand("loadmod " .. mods[i]) + end, nil, true, nil, { + desc_text = desc + }) + end end end