diff --git a/src/component/gsc.cpp b/src/component/gsc.cpp index 0b00b2f..c42bd63 100644 --- a/src/component/gsc.cpp +++ b/src/component/gsc.cpp @@ -20,8 +20,8 @@ namespace gsc utils::hook::detour get_function_hook; utils::hook::detour get_method_hook; - std::unordered_map> functions; - std::unordered_map> methods; + std::unordered_map functions; + std::unordered_map methods; std::unordered_map function_wraps; std::unordered_map method_wraps; @@ -175,11 +175,9 @@ namespace gsc namespace function { - template - void add(const std::string& name, F f) + void add_internal(const std::string& name, const function_t& function) { - const auto wrap = wrap_function(f); - functions[name] = wrap; + functions[name] = function; const auto call_wrap = wrap_function_call(name); function_wraps[name] = call_wrap; } @@ -188,10 +186,9 @@ namespace gsc namespace method { template - void add(const std::string& name, F f) + void add_internal(const std::string& name, const function_t& method) { - const auto wrap = wrap_function(f); - methods[name] = wrap; + methods[name] = method; const auto call_wrap = wrap_method_call(name); method_wraps[name] = call_wrap; } @@ -213,43 +210,6 @@ namespace gsc // \n******* script runtime error *******\n%s\n utils::hook::set(0xAABA68 + 40, '\n'); utils::hook::set(0xAABA68 + 41, '\0'); - - function::add("print_", [](const scripting::variadic_args& va) - { - for (const auto& arg : va) - { - printf("%s\t", arg.to_string().data()); - } - printf("\n"); - }); - - function::add("writefile", [](const std::string& file, const std::string& data, - const scripting::variadic_args& va) - { - auto append = false; - - if (va.size() > 0) - { - append = va[0]; - } - - return utils::io::write_file(file, data, append); - }); - - function::add("appendfile", [](const std::string& file, const std::string& data) - { - return utils::io::write_file(file, data, true); - }); - - function::add("fileexists", utils::io::file_exists); - function::add("movefile", utils::io::move_file); - function::add("filesize", utils::io::file_size); - function::add("createdirectory", utils::io::create_directory); - function::add("directoryexists", utils::io::directory_exists); - function::add("directoryisempty", utils::io::directory_is_empty); - function::add("listfiles", utils::io::list_files); - function::add("removefile", utils::io::remove_file); - function::add("readfile", static_cast(utils::io::read_file)); } }; } diff --git a/src/component/gsc.hpp b/src/component/gsc.hpp index 551413f..67cfc24 100644 --- a/src/component/gsc.hpp +++ b/src/component/gsc.hpp @@ -4,6 +4,7 @@ namespace gsc { + using function_t = std::function; using script_function = void(*)(game::scr_entref_t); template @@ -48,13 +49,23 @@ namespace gsc namespace function { + void add_internal(const std::string& name, const function_t& function); + template - void add(const std::string& name, F f); + void add(const std::string& name, F f) + { + add_internal(name, wrap_function(f)); + } } namespace method { + void add_internal(const std::string& name, const function_t& function); + template - void add(const std::string& name, F f); + void add(const std::string& name, F f) + { + add_internal(name, wrap_function(f)); + } } } diff --git a/src/component/io.cpp b/src/component/io.cpp new file mode 100644 index 0000000..346fd25 --- /dev/null +++ b/src/component/io.cpp @@ -0,0 +1,58 @@ +#include +#include "loader/component_loader.hpp" + +#include "game/structs.hpp" +#include "game/game.hpp" + +#include "gsc.hpp" + +#include + +namespace io +{ + class component final : public component_interface + { + public: + void post_unpack() override + { + gsc::function::add("print_", [](const scripting::variadic_args& va) + { + for (const auto& arg : va) + { + printf("%s\t", arg.to_string().data()); + } + printf("\n"); + }); + + gsc::function::add("writefile", [](const std::string& file, const std::string& data, + const scripting::variadic_args& va) + { + auto append = false; + + if (va.size() > 0) + { + append = va[0]; + } + + return utils::io::write_file(file, data, append); + }); + + gsc::function::add("appendfile", [](const std::string& file, const std::string& data) + { + return utils::io::write_file(file, data, true); + }); + + gsc::function::add("fileexists", utils::io::file_exists); + gsc::function::add("movefile", utils::io::move_file); + gsc::function::add("filesize", utils::io::file_size); + gsc::function::add("createdirectory", utils::io::create_directory); + gsc::function::add("directoryexists", utils::io::directory_exists); + gsc::function::add("directoryisempty", utils::io::directory_is_empty); + gsc::function::add("listfiles", utils::io::list_files); + gsc::function::add("removefile", utils::io::remove_file); + gsc::function::add("readfile", static_cast(utils::io::read_file)); + } + }; +} + +REGISTER_COMPONENT(io::component)