h1-mod/src/client/component/io.cpp

133 lines
3.4 KiB
C++
Raw Normal View History

2022-10-04 22:09:22 -04:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "console.hpp"
#include "gsc.hpp"
#include "logfile.hpp"
#include "scheduler.hpp"
#include "game/dvars.hpp"
#include "game/game.hpp"
#include "game/scripting/execution.hpp"
#include <utils/hook.hpp>
#include <utils/http.hpp>
#include <utils/io.hpp>
namespace io
{
namespace
{
void check_path(const std::filesystem::path& path)
{
if (path.generic_string().find("..") != std::string::npos)
{
2022-10-14 12:39:27 -04:00
throw std::runtime_error("directory traversal is not allowed");
2022-10-04 22:09:22 -04:00
}
}
std::string convert_path(const std::filesystem::path& path)
{
check_path(path);
static const auto fs_game = game::Dvar_FindVar("fs_game");
if (fs_game->current.string && fs_game->current.string != ""s)
{
const std::filesystem::path fs_game_path(fs_game->current.string);
return (fs_game_path / path).generic_string();
}
2022-10-14 12:39:27 -04:00
throw std::runtime_error("fs_game is not properly defined");
2022-10-04 22:09:22 -04:00
}
}
class component final : public component_interface
{
public:
void post_unpack() override
{
2022-10-14 12:39:27 -04:00
gsc::function::add("fileexists", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return utils::io::file_exists(path);
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("writefile", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
const auto data = args[1].as<std::string>();
2022-10-04 22:09:22 -04:00
auto append = false;
2022-10-14 12:39:27 -04:00
if (args.size() > 2u)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
append = args[2].as<bool>();
2022-10-04 22:09:22 -04:00
}
2022-10-14 12:39:27 -04:00
return utils::io::write_file(path, data, append);
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("readfile", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return utils::io::read_file(path);
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("filesize", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return static_cast<uint32_t>(utils::io::file_size(path));
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("createdirectory", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return utils::io::create_directory(path);
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("directoryexists", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return utils::io::directory_exists(path);
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("directoryisempty", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return utils::io::directory_is_empty(path);
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("listfiles", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
2022-10-04 22:09:22 -04:00
const auto files = utils::io::list_files(path);
scripting::array array{};
for (const auto& file : files)
{
array.push(file);
}
2022-10-14 12:39:27 -04:00
return array;
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("copyfolder", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto source = convert_path(args[0].as<std::string>());
const auto target = convert_path(args[1].as<std::string>());
2022-10-04 22:09:22 -04:00
utils::io::copy_folder(source, target);
2022-10-14 12:39:27 -04:00
return scripting::script_value{};
2022-10-04 22:09:22 -04:00
});
2022-10-14 12:39:27 -04:00
gsc::function::add("removefile", [](const gsc::function_args& args)
2022-10-04 22:09:22 -04:00
{
2022-10-14 12:39:27 -04:00
const auto path = convert_path(args[0].as<std::string>());
return utils::io::remove_file(path);
2022-10-04 22:09:22 -04:00
});
}
};
}
REGISTER_COMPONENT(io::component)