Skip to content

Commit

Permalink
Move mod commands to new component + small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alicealys committed Jan 27, 2022
1 parent ff77aba commit 10bf954
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 72 deletions.
56 changes: 0 additions & 56 deletions src/client/component/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
};
}
Expand Down
84 changes: 84 additions & 0 deletions src/client/component/mods.cpp
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 18 additions & 16 deletions src/client/resources/mods_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 10bf954

Please sign in to comment.