Move mod commands to new component + small fix

This commit is contained in:
Federico Cecchetto 2022-01-28 00:51:44 +01:00
parent ff77aba0f8
commit 10bf954c2b
3 changed files with 102 additions and 72 deletions

View File

@ -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/<modname>");
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);
});
}
};
}

View File

@ -0,0 +1,84 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include "command.hpp"
#include "game_console.hpp"
#include "scheduler.hpp"
#include <utils/hook.hpp>
#include <utils/io.hpp>
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/<modname>");
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)

View File

@ -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