forked from alterware/s1-mod
December 2069 Update
This commit is contained in:
@ -186,100 +186,6 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scr_get_vector(unsigned int index, float* vector_value)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
auto* value = game::scr_VmPub->top - index;
|
||||
if (value->type == game::VAR_VECTOR)
|
||||
{
|
||||
std::memcpy(vector_value, value->u.vectorValue, sizeof(std::float_t[3]));
|
||||
return;
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not a vector", var_typename[value->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
}
|
||||
|
||||
int scr_get_int(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
auto* value = game::scr_VmPub->top - index;
|
||||
if (value->type == game::VAR_INTEGER)
|
||||
{
|
||||
return value->u.intValue;
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not an int", var_typename[value->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
float scr_get_float(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
auto* value = game::scr_VmPub->top - index;
|
||||
if (value->type == game::VAR_FLOAT)
|
||||
{
|
||||
return value->u.floatValue;
|
||||
}
|
||||
|
||||
if (value->type == game::VAR_INTEGER)
|
||||
{
|
||||
return static_cast<float>(value->u.intValue);
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not a float", var_typename[value->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
int scr_get_pointer_type(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
if ((game::scr_VmPub->top - index)->type == game::VAR_POINTER)
|
||||
{
|
||||
return static_cast<int>(game::GetObjectType((game::scr_VmPub->top - index)->u.uintValue));
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not an object", var_typename[(game::scr_VmPub->top - index)->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int scr_get_type(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
return (game::scr_VmPub->top - index)->type;
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* scr_get_type_name(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
return var_typename[(game::scr_VmPub->top - index)->type];
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::pair<std::string, std::string>> find_function(const char* pos)
|
||||
@ -299,34 +205,130 @@ namespace gsc
|
||||
return {};
|
||||
}
|
||||
|
||||
void scr_get_vector(unsigned int index, float* vector_value)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
auto* value = game::scr_VmPub->top - index;
|
||||
if (value->type == game::VAR_VECTOR)
|
||||
{
|
||||
std::memcpy(vector_value, value->u.vectorValue, sizeof(std::float_t[3]));
|
||||
return;
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not a vector", var_typename[value->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
}
|
||||
|
||||
int scr_get_int(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
auto* value = game::scr_VmPub->top - index;
|
||||
if (value->type == game::VAR_INTEGER)
|
||||
{
|
||||
return value->u.intValue;
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not an int", var_typename[value->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
float scr_get_float(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
auto* value = game::scr_VmPub->top - index;
|
||||
if (value->type == game::VAR_FLOAT)
|
||||
{
|
||||
return value->u.floatValue;
|
||||
}
|
||||
|
||||
if (value->type == game::VAR_INTEGER)
|
||||
{
|
||||
return static_cast<float>(value->u.intValue);
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not a float", var_typename[value->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
int scr_get_pointer_type(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
if ((game::scr_VmPub->top - index)->type == game::VAR_POINTER)
|
||||
{
|
||||
return static_cast<int>(game::GetObjectType((game::scr_VmPub->top - index)->u.uintValue));
|
||||
}
|
||||
|
||||
scr_error(va("Type %s is not an object", var_typename[(game::scr_VmPub->top - index)->type]));
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int scr_get_type(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
return (game::scr_VmPub->top - index)->type;
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* scr_get_type_name(unsigned int index)
|
||||
{
|
||||
if (index < game::scr_VmPub->outparamcount)
|
||||
{
|
||||
return var_typename[(game::scr_VmPub->top - index)->type];
|
||||
}
|
||||
|
||||
scr_error(va("Parameter %u does not exist", index + 1));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
class error final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
scr_emit_function_hook.create(SELECT_VALUE(0x1403113D0, 0x1403ED900), &scr_emit_function_stub);
|
||||
|
||||
utils::hook::call(SELECT_VALUE(0x140311364, 0x1403ED894), compile_error_stub); // LinkFile
|
||||
utils::hook::call(SELECT_VALUE(0x1403113B8, 0x1403ED8E8), compile_error_stub); // LinkFile
|
||||
utils::hook::call(SELECT_VALUE(0x1403114AA, 0x1403ED9DB), find_variable_stub); // Scr_EmitFunction
|
||||
|
||||
// Restore basic error messages for commonly used scr functions
|
||||
utils::hook::jump(SELECT_VALUE(0x14031BD80, 0x1403F8510), scr_get_const_string);
|
||||
utils::hook::jump(game::Scr_GetInt, scr_get_int);
|
||||
utils::hook::jump(game::Scr_GetFloat, scr_get_float);
|
||||
utils::hook::jump(game::Scr_GetVector, scr_get_vector);
|
||||
|
||||
utils::hook::jump(SELECT_VALUE(0x14031C690, 0x1403F8D70), scr_get_type);
|
||||
utils::hook::jump(SELECT_VALUE(0x14031C700, 0x1403F8DE0), scr_get_type_name);
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
scr_emit_function_hook.create(0x1403ED900, &scr_emit_function_stub);
|
||||
|
||||
utils::hook::call(0x1403ED894, compile_error_stub); // LinkFile
|
||||
utils::hook::call(0x1403ED8E8, compile_error_stub); // LinkFile
|
||||
utils::hook::call(0x1403ED9DB, find_variable_stub); // Scr_EmitFunction
|
||||
|
||||
// Restore basic error messages for commonly used scr functions
|
||||
utils::hook::jump(0x1403F8990, scr_get_object);
|
||||
utils::hook::jump(0x1403F8510, scr_get_const_string);
|
||||
utils::hook::jump(0x1403F82F0, scr_get_const_istring);
|
||||
utils::hook::jump(0x140327870, scr_validate_localized_string_ref);
|
||||
utils::hook::jump(0x1403F8EC0, scr_get_vector);
|
||||
utils::hook::jump(0x1403F88D0, scr_get_int);
|
||||
utils::hook::jump(0x1403F8820, scr_get_float);
|
||||
|
||||
utils::hook::jump(0x1403F8BA0, scr_get_pointer_type);
|
||||
utils::hook::jump(0x1403F8D70, scr_get_type);
|
||||
utils::hook::jump(0x1403F8DE0, scr_get_type_name);
|
||||
}
|
||||
|
||||
void pre_destroy() override
|
||||
|
@ -3,4 +3,11 @@
|
||||
namespace gsc
|
||||
{
|
||||
std::optional<std::pair<std::string, std::string>> find_function(const char* pos);
|
||||
|
||||
void scr_get_vector(unsigned int index, float* vector_value);
|
||||
int scr_get_int(unsigned int index);
|
||||
float scr_get_float(unsigned int index);
|
||||
int scr_get_pointer_type(unsigned int index);
|
||||
int scr_get_type(unsigned int index);
|
||||
const char* scr_get_type_name(unsigned int index);
|
||||
}
|
||||
|
@ -5,15 +5,15 @@
|
||||
|
||||
#include "game/scripting/functions.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
|
||||
#include "component/console.hpp"
|
||||
#include "component/command.hpp"
|
||||
#include "component/console.hpp"
|
||||
#include "component/notifies.hpp"
|
||||
|
||||
#include "script_error.hpp"
|
||||
#include "script_extension.hpp"
|
||||
#include "script_loading.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace gsc
|
||||
@ -39,9 +39,60 @@ namespace gsc
|
||||
|
||||
utils::hook::detour scr_register_function_hook;
|
||||
|
||||
std::vector<devmap_entry> devmap_entries{};
|
||||
|
||||
std::optional<devmap_entry> get_devmap_entry(const std::uint8_t* codepos)
|
||||
{
|
||||
const auto itr = std::ranges::find_if(devmap_entries, [codepos](const devmap_entry& entry) -> bool
|
||||
{
|
||||
return codepos >= entry.bytecode && codepos < entry.bytecode + entry.size;
|
||||
});
|
||||
|
||||
if (itr != devmap_entries.end())
|
||||
{
|
||||
return *itr;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::pair<std::uint16_t, std::uint16_t>> get_line_and_col_for_codepos(const std::uint8_t* codepos)
|
||||
{
|
||||
const auto entry = get_devmap_entry(codepos);
|
||||
|
||||
if (!entry.has_value())
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::pair<std::uint16_t, std::uint16_t>> best_line_info{};
|
||||
std::uint32_t best_codepos = 0;
|
||||
|
||||
assert(codepos >= entry->bytecode);
|
||||
const std::uint32_t codepos_offset = static_cast<std::uint32_t>(codepos - entry->bytecode);
|
||||
|
||||
for (const auto& instruction : entry->devmap)
|
||||
{
|
||||
if (instruction.codepos > codepos_offset)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (best_line_info.has_value() && codepos_offset - instruction.codepos > codepos_offset - best_codepos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
best_line_info = { { instruction.line, instruction.col } };
|
||||
best_codepos = instruction.codepos;
|
||||
}
|
||||
|
||||
return best_line_info;
|
||||
}
|
||||
|
||||
unsigned int scr_get_function_stub(const char** p_name, int* type)
|
||||
{
|
||||
const auto result = utils::hook::invoke<unsigned int>(0x1403318B0, p_name, type);
|
||||
const auto result = game::Scr_GetFunction(p_name, type);
|
||||
|
||||
for (const auto& [name, func] : functions)
|
||||
{
|
||||
@ -122,27 +173,36 @@ namespace gsc
|
||||
const auto pos = frame == game::scr_VmPub->function_frame ? game::scr_function_stack->pos : frame->fs.pos;
|
||||
const auto function = find_function(frame->fs.pos);
|
||||
|
||||
const char* location;
|
||||
if (function.has_value())
|
||||
{
|
||||
console::warn("\tat function \"%s\" in file \"%s.gsc\"\n", function.value().first.data(), function.value().second.data());
|
||||
location = utils::string::va("function \"%s\" in file \"%s\"", function.value().first.data(), function.value().second.data());
|
||||
}
|
||||
else
|
||||
{
|
||||
console::warn("\tat unknown location %p\n", pos);
|
||||
location = utils::string::va("unknown location %p", pos);
|
||||
}
|
||||
|
||||
const auto line_info = get_line_and_col_for_codepos(reinterpret_cast<const std::uint8_t*>(pos));
|
||||
if (line_info.has_value())
|
||||
{
|
||||
location = utils::string::va("%s line \"%d\" column \"%d\"", location, line_info->first, line_info->second);
|
||||
}
|
||||
|
||||
console::warn("\tat %s\n", location);
|
||||
}
|
||||
}
|
||||
|
||||
void vm_error_stub(int mark_pos)
|
||||
void vm_error_stub(unsigned __int64 mark_pos)
|
||||
{
|
||||
if (!dvars::com_developer_script->current.enabled && !force_error_print)
|
||||
{
|
||||
utils::hook::invoke<void>(0x1404B6790, mark_pos);
|
||||
game::LargeLocalResetToMark(mark_pos);
|
||||
return;
|
||||
}
|
||||
|
||||
console::warn("******* script runtime error ********\n");
|
||||
const auto opcode_id = *reinterpret_cast<std::uint8_t*>(0x148806768);
|
||||
const auto opcode_id = *reinterpret_cast<std::uint8_t*>(SELECT_VALUE(0x14A19DE68, 0x148806768));
|
||||
|
||||
const std::string error = gsc_error_msg.has_value() ? std::format(": {}", gsc_error_msg.value()) : std::string();
|
||||
|
||||
@ -168,7 +228,7 @@ namespace gsc
|
||||
|
||||
print_callstack();
|
||||
console::warn("************************************\n");
|
||||
utils::hook::invoke<void>(0x1404B6790, mark_pos);
|
||||
game::LargeLocalResetToMark(mark_pos);
|
||||
}
|
||||
|
||||
void scr_register_function_stub(void* func, int type, unsigned int name)
|
||||
@ -219,6 +279,46 @@ namespace gsc
|
||||
{
|
||||
scr_error(utils::string::va("Assert fail: %s", game::Scr_GetString(0)));
|
||||
}
|
||||
|
||||
void scr_cmd_is_dedicated_server()
|
||||
{
|
||||
game::Scr_AddInt(game::environment::is_dedi());
|
||||
}
|
||||
|
||||
const char* get_code_pos(const int index)
|
||||
{
|
||||
if (static_cast<unsigned int>(index) >= game::scr_VmPub->outparamcount)
|
||||
{
|
||||
scr_error("Scr_GetCodePos: index is out of range");
|
||||
return "";
|
||||
}
|
||||
|
||||
const auto* value = &game::scr_VmPub->top[-index];
|
||||
|
||||
if (value->type != game::VAR_FUNCTION)
|
||||
{
|
||||
scr_error("Scr_GetCodePos requires a function as parameter");
|
||||
return "";
|
||||
}
|
||||
|
||||
return value->u.codePosValue;
|
||||
}
|
||||
}
|
||||
|
||||
void add_devmap_entry(std::uint8_t* codepos, std::size_t size, const std::string& name, xsk::gsc::buffer devmap_buf)
|
||||
{
|
||||
std::vector<dev_map_instruction> devmap{};
|
||||
const auto* devmap_ptr = reinterpret_cast<const dev_map*>(devmap_buf.data);
|
||||
|
||||
devmap.resize(devmap_ptr->num_instructions);
|
||||
std::memcpy(devmap.data(), devmap_ptr->instructions, sizeof(dev_map_instruction) * devmap_ptr->num_instructions);
|
||||
|
||||
devmap_entries.emplace_back(codepos, size, name, std::move(devmap));
|
||||
}
|
||||
|
||||
void clear_devmap()
|
||||
{
|
||||
devmap_entries.clear();
|
||||
}
|
||||
|
||||
void scr_error(const char* error)
|
||||
@ -252,33 +352,40 @@ namespace gsc
|
||||
override_function("print", &scr_print);
|
||||
override_function("println", &scr_print_ln);
|
||||
|
||||
utils::hook::set<std::uint32_t>(SELECT_VALUE(0x1403115BC, 0x1403EDAEC), 0x1000); // Scr_RegisterFunction
|
||||
|
||||
utils::hook::set<std::uint32_t>(SELECT_VALUE(0x1403115C2 + 4, 0x1403EDAF2 + 4), RVA(&func_table)); // Scr_RegisterFunction
|
||||
utils::hook::set<std::uint32_t>(SELECT_VALUE(0x14031F097 + 3, 0x1403FB7F7 + 3), RVA(&func_table)); // VM_Execute_0
|
||||
utils::hook::inject(SELECT_VALUE(0x140311964 + 3, 0x1403EDEE4 + 3), &func_table); // Scr_BeginLoadScripts
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
utils::hook::nop(0x1403FB7F7 + 5, 2);
|
||||
utils::hook::call(0x1403FB7F7, vm_call_builtin_function);
|
||||
|
||||
utils::hook::call(0x1403FCAB0, vm_error_stub); // LargeLocalResetToMark
|
||||
|
||||
utils::hook::call(0x1403EDF1F, scr_get_function_stub);
|
||||
|
||||
override_function("assert", &assert_cmd);
|
||||
override_function("assertex", &assert_ex_cmd);
|
||||
override_function("assertmsg", &assert_msg_cmd);
|
||||
|
||||
add_function("executecommand", []
|
||||
override_function("isdedicatedserver", &scr_cmd_is_dedicated_server);
|
||||
|
||||
add_function("replacefunc", []
|
||||
{
|
||||
if (scr_get_type(0) != game::VAR_FUNCTION || scr_get_type(1) != game::VAR_FUNCTION)
|
||||
{
|
||||
throw gsc_error("Parameter must be a function");
|
||||
}
|
||||
|
||||
notifies::set_gsc_hook(get_code_pos(0), get_code_pos(1));
|
||||
});
|
||||
|
||||
add_function("executecommand", []() -> void
|
||||
{
|
||||
const auto* cmd = game::Scr_GetString(0);
|
||||
command::execute(cmd);
|
||||
});
|
||||
|
||||
utils::hook::set<std::uint32_t>(SELECT_VALUE(0x1403115BC, 0x1403EDAEC), 0x1000); // Scr_RegisterFunction
|
||||
|
||||
utils::hook::set<std::uint32_t>(SELECT_VALUE(0x1403115C2 + 4, 0x1403EDAF2 + 4), RVA(&func_table)); // Scr_RegisterFunction
|
||||
// utils::hook::set<std::uint32_t>(SELECT_VALUE(0x14031F097 + 3, 0x1403FB7F7 + 3), RVA(&func_table)); // VM_Execute_0
|
||||
utils::hook::inject(SELECT_VALUE(0x140311964 + 3, 0x1403EDEE4 + 3), &func_table); // Scr_BeginLoadScripts
|
||||
|
||||
utils::hook::nop(SELECT_VALUE(0x14031F097 + 5, 0x1403FB7F7 + 5), 2);
|
||||
utils::hook::call(SELECT_VALUE(0x14031F097, 0x1403FB7F7), vm_call_builtin_function);
|
||||
|
||||
utils::hook::call(SELECT_VALUE(0x14032034F, 0x1403FCAB0), vm_error_stub); // LargeLocalResetToMark
|
||||
|
||||
utils::hook::call(SELECT_VALUE(0x14031199F, 0x1403EDF1F), scr_get_function_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,9 +1,36 @@
|
||||
#pragma once
|
||||
#include <xsk/gsc/engine/s1_pc.hpp>
|
||||
|
||||
namespace gsc
|
||||
{
|
||||
extern void* func_table[0x1000];
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct dev_map_instruction
|
||||
{
|
||||
std::uint32_t codepos;
|
||||
std::uint16_t line;
|
||||
std::uint16_t col;
|
||||
};
|
||||
|
||||
struct dev_map
|
||||
{
|
||||
std::uint32_t num_instructions;
|
||||
dev_map_instruction instructions[1];
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
struct devmap_entry
|
||||
{
|
||||
const std::uint8_t* bytecode;
|
||||
std::size_t size;
|
||||
std::string script_name;
|
||||
std::vector<dev_map_instruction> devmap;
|
||||
};
|
||||
|
||||
void add_devmap_entry(std::uint8_t*, std::size_t, const std::string&, xsk::gsc::buffer);
|
||||
void clear_devmap();
|
||||
|
||||
void scr_error(const char* error);
|
||||
void override_function(const std::string& name, game::BuiltinFunction func);
|
||||
void add_function(const std::string& name, game::BuiltinFunction function);
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "component/console.hpp"
|
||||
#include "component/scripting.hpp"
|
||||
|
||||
#include "script_extension.hpp"
|
||||
#include "script_loading.hpp"
|
||||
|
||||
namespace gsc
|
||||
@ -32,6 +33,7 @@ namespace gsc
|
||||
init_handles.clear();
|
||||
loaded_scripts.clear();
|
||||
script_allocator.clear();
|
||||
clear_devmap();
|
||||
}
|
||||
|
||||
bool read_raw_script_file(const std::string& name, std::string* data)
|
||||
@ -115,6 +117,12 @@ namespace gsc
|
||||
|
||||
loaded_scripts[real_name] = script_file_ptr;
|
||||
|
||||
const auto devmap = std::get<2>(output_script);
|
||||
if (devmap.size > 0 && (gsc_ctx->build() & xsk::gsc::build::dev_maps) != xsk::gsc::build::prod)
|
||||
{
|
||||
add_devmap_entry(script_file_ptr->bytecode, byte_code_size, real_name, devmap);
|
||||
}
|
||||
|
||||
return script_file_ptr;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
@ -181,13 +189,10 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
|
||||
void load_scripts(const std::filesystem::path& root_dir)
|
||||
|
||||
void load_scripts_from_folder(const std::filesystem::path& root_dir, const std::filesystem::path& script_dir)
|
||||
{
|
||||
const std::filesystem::path script_dir = root_dir / "scripts";
|
||||
if (!utils::io::directory_exists(script_dir.generic_string()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
console::info("Scanning directory '%s' for custom GSC scripts...\n", script_dir.generic_string().data());
|
||||
|
||||
const auto scripts = utils::io::list_files(script_dir.generic_string());
|
||||
for (const auto& script : scripts)
|
||||
@ -205,6 +210,44 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
|
||||
void load_scripts(const std::filesystem::path& root_dir)
|
||||
{
|
||||
const auto load = [&root_dir](const std::filesystem::path& folder) -> void
|
||||
{
|
||||
const std::filesystem::path script_dir = root_dir / folder;
|
||||
if (utils::io::directory_exists(script_dir.generic_string()))
|
||||
{
|
||||
load_scripts_from_folder(root_dir, script_dir);
|
||||
}
|
||||
};
|
||||
|
||||
const std::filesystem::path base_dir = "scripts";
|
||||
|
||||
load(base_dir);
|
||||
|
||||
const auto* map_name = game::Dvar_FindVar("mapname");
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
const std::filesystem::path game_folder = "sp";
|
||||
|
||||
load(base_dir / game_folder);
|
||||
|
||||
load(base_dir / game_folder / map_name->current.string);
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::filesystem::path game_folder = "mp";
|
||||
|
||||
load(base_dir / game_folder);
|
||||
|
||||
load(base_dir / game_folder / map_name->current.string);
|
||||
|
||||
const auto* game_type = game::Dvar_FindVar("g_gametype");
|
||||
load(base_dir / game_folder / game_type->current.string);
|
||||
}
|
||||
}
|
||||
|
||||
int db_is_x_asset_default(game::XAssetType type, const char* name)
|
||||
{
|
||||
if (loaded_scripts.contains(name))
|
||||
@ -224,8 +267,6 @@ namespace gsc
|
||||
return;
|
||||
}
|
||||
|
||||
clear();
|
||||
|
||||
for (const auto& path : filesystem::get_search_paths())
|
||||
{
|
||||
load_scripts(path);
|
||||
@ -259,7 +300,38 @@ namespace gsc
|
||||
utils::hook::invoke<void>(0x1403380D0);
|
||||
}
|
||||
|
||||
void scr_load_level_stub()
|
||||
int g_scr_set_level_script_stub(game::ScriptFunctions* functions)
|
||||
{
|
||||
const auto result = utils::hook::invoke<int>(0x140262F60, functions);
|
||||
|
||||
for (const auto& path : filesystem::get_search_paths())
|
||||
{
|
||||
load_scripts(path);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void scr_load_level_singleplayer_stub()
|
||||
{
|
||||
for (auto& function_handle : main_handles)
|
||||
{
|
||||
console::info("Executing '%s::main'\n", function_handle.first.data());
|
||||
const auto thread = game::Scr_ExecThread(static_cast<int>(function_handle.second), 0);
|
||||
game::RemoveRefToObject(thread);
|
||||
}
|
||||
|
||||
utils::hook::invoke<void>(0x140257720);
|
||||
|
||||
for (auto& function_handle : init_handles)
|
||||
{
|
||||
console::info("Executing '%s::init'\n", function_handle.first.data());
|
||||
const auto thread = game::Scr_ExecThread(static_cast<int>(function_handle.second), 0);
|
||||
game::RemoveRefToObject(thread);
|
||||
}
|
||||
}
|
||||
|
||||
void scr_load_level_multiplayer_stub()
|
||||
{
|
||||
utils::hook::invoke<void>(0x140325B90);
|
||||
|
||||
@ -358,28 +430,32 @@ namespace gsc
|
||||
utils::hook::call(SELECT_VALUE(0x1403309E9, 0x1403309E9), scr_begin_load_scripts_stub); // GScr_LoadScripts
|
||||
utils::hook::call(SELECT_VALUE(0x14023DA84, 0x140330B9C), scr_end_load_scripts_stub); // GScr_LoadScripts
|
||||
|
||||
dvars::com_developer = game::Dvar_RegisterInt("developer", 0, 0, 2, game::DVAR_FLAG_NONE, "Enable development options");
|
||||
dvars::com_developer_script = game::Dvar_RegisterBool("developer_script", false, game::DVAR_FLAG_NONE, "Enable developer script comments");
|
||||
// ProcessScript
|
||||
utils::hook::call(SELECT_VALUE(0x14031AB47, 0x1403F7317), find_script);
|
||||
utils::hook::call(SELECT_VALUE(0x14031AB57, 0x1403F7327), db_is_x_asset_default);
|
||||
|
||||
dvars::com_developer = game::Dvar_RegisterInt("developer", 0, 0, 2, game::DVAR_FLAG_NONE);
|
||||
dvars::com_developer_script = game::Dvar_RegisterBool("developer_script", false, game::DVAR_FLAG_NONE);
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
utils::hook::call(0x1402632A5, g_scr_set_level_script_stub);
|
||||
|
||||
utils::hook::call(0x140226931, scr_load_level_singleplayer_stub);
|
||||
}
|
||||
else
|
||||
{
|
||||
// GScr_LoadScripts
|
||||
utils::hook::call(0x140330B97, gscr_post_load_scripts_stub);
|
||||
|
||||
// Exec script handles
|
||||
utils::hook::call(0x1402F71AE, g_load_structs_stub);
|
||||
utils::hook::call(0x1402F71C7, scr_load_level_multiplayer_stub);
|
||||
}
|
||||
|
||||
// ProcessScript
|
||||
utils::hook::call(0x1403F7317, find_script);
|
||||
utils::hook::call(0x1403F7327, db_is_x_asset_default);
|
||||
|
||||
// GScr_LoadScripts
|
||||
utils::hook::call(0x140330B97, gscr_post_load_scripts_stub);
|
||||
|
||||
// Exec script handles
|
||||
utils::hook::call(0x1402F71AE, g_load_structs_stub);
|
||||
utils::hook::call(0x1402F71C7, scr_load_level_stub);
|
||||
|
||||
scripting::on_shutdown([](int free_scripts)
|
||||
scripting::on_shutdown([](const int clear_scripts) -> void
|
||||
{
|
||||
if (free_scripts)
|
||||
if (clear_scripts)
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
Reference in New Issue
Block a user