Skip to content

Commit

Permalink
Many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alicealys committed Feb 27, 2023
1 parent f40d366 commit 22cfa38
Show file tree
Hide file tree
Showing 16 changed files with 451 additions and 149 deletions.
53 changes: 46 additions & 7 deletions src/component/scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ namespace scripting
utils::hook::detour scr_set_thread_position_hook;
utils::hook::detour process_script_hook;

utils::hook::detour scr_run_current_threads_hook;

std::string current_file;

using notify_list = std::vector<event>;
utils::concurrency::container<notify_list> scheduled_notifies;

void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value,
game::VariableValue* top)
Expand All @@ -51,35 +56,50 @@ namespace scripting
const auto entity = e.arguments[0].as<scripting::entity>();

notifies::clear_cmd_notifies(entity);
clear_entity_fields(entity);
}

lua::engine::notify(e);
lua::engine::handle_endon_conditions(e);

scheduled_notifies.access([&](notify_list& list)
{
list.push_back(e);
});
}

vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
}

void scr_add_class_field_stub(unsigned int classnum, unsigned int _name, unsigned int canonicalString, unsigned int offset)
void clear_scheduled_notifies()
{
scheduled_notifies.access([](notify_list& list)
{
list.clear();
});
}

void scr_add_class_field_stub(unsigned int classnum, unsigned int name,
unsigned int canonicalString, unsigned int offset)
{
const auto name = game::SL_ConvertToString(_name);
const auto name_str = game::SL_ConvertToString(name);

if (fields_table[classnum].find(name) == fields_table[classnum].end())
if (fields_table[classnum].find(name_str) == fields_table[classnum].end())
{
fields_table[classnum][name] = offset;
fields_table[classnum][name_str] = offset;
}

scr_add_class_field_hook.invoke<void>(classnum, _name, canonicalString, offset);
scr_add_class_field_hook.invoke<void>(classnum, name, canonicalString, offset);
}

void scr_load_level_stub()
{
scr_load_level_hook.invoke<void>();
clear_scheduled_notifies();
lua::engine::start();
}

void g_shutdown_game_stub(const int free_scripts)
{
clear_scheduled_notifies();
lua::engine::stop();
g_shutdown_game_hook.invoke<void>(free_scripts);
}
Expand Down Expand Up @@ -108,6 +128,23 @@ namespace scripting

scr_set_thread_position_hook.invoke<void>(threadName, codePos);
}

void scr_run_current_threads_stub()
{
notify_list list_copy{};
scheduled_notifies.access([&](notify_list& list)
{
list_copy = list;
list.clear();
});

for (const auto& e : list_copy)
{
lua::engine::notify(e);
}

scr_run_current_threads_hook.invoke<void>();
}
}

class component final : public component_interface
Expand All @@ -124,6 +161,8 @@ namespace scripting
scr_set_thread_position_hook.create(0x5616D0, scr_set_thread_position_stub);
process_script_hook.create(0x56B130, process_script_stub);

scr_run_current_threads_hook.create(0x56E320, scr_run_current_threads_stub);

scheduler::loop([]()
{
lua::engine::run_frame();
Expand Down
25 changes: 15 additions & 10 deletions src/component/signatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace signatures
{
MODULEINFO info{};
GetModuleInformation(GetCurrentProcess(),
GetModuleHandle("plutonium-bootstrapper-win32.exe"), &info, sizeof(MODULEINFO));
GetModuleHandle(L"plutonium-bootstrapper-win32.exe"), &info, sizeof(MODULEINFO));
return info.SizeOfImage;
}

Expand All @@ -32,7 +32,7 @@ namespace signatures
{
const char* string_ptr = nullptr;
std::string mask(string.size(), 'x');
const auto base = reinterpret_cast<size_t>(GetModuleHandle("plutonium-bootstrapper-win32.exe"));
const auto base = reinterpret_cast<size_t>(GetModuleHandle(L"plutonium-bootstrapper-win32.exe"));
utils::hook::signature signature(base, get_image_size() - base);

signature.add({
Expand All @@ -42,33 +42,37 @@ namespace signatures
{
string_ptr = address;
}
});
});

signature.process();
return reinterpret_cast<size_t>(string_ptr);
}

size_t find_string_ref(const std::string& string)
{
char bytes[4] = {0};
char bytes[4] = { 0 };
const auto string_ptr = find_string_ptr(string);
memcpy(bytes, &string_ptr, sizeof(size_t));
return find_string_ptr(bytes);
if (!string_ptr)
{
return 0;
}

std::memcpy(bytes, &string_ptr, sizeof(bytes));
return find_string_ptr({ bytes, 4 });
}

bool process_maps()
{
const auto string_ref = find_string_ref("Couldn't resolve builtin function id for name '%s'!");
const auto string_ref = find_string_ref("couldn't resolve builtin function id for name '%s'!");
if (!string_ref)
{
return false;
}

const auto map_ptr = *reinterpret_cast<size_t*>(string_ref - 0x3A);
const auto map_ptr = *reinterpret_cast<size_t*>(string_ref - 0x2B);
game::plutonium::function_map_rev.set(map_ptr);
game::plutonium::method_map_rev.set(map_ptr + 0x20);
game::plutonium::file_map_rev.set(map_ptr + 0x40);
game::plutonium::token_map_rev.set(map_ptr + 0x60);
game::plutonium::token_map_rev.set(map_ptr + 0xBC);
return true;
}

Expand All @@ -82,6 +86,7 @@ namespace signatures

const auto offset = *reinterpret_cast<size_t*>(string_ref + 5);
game::plutonium::printf.set(string_ref + 4 + 5 + offset);

return true;
}

Expand Down
131 changes: 78 additions & 53 deletions src/game/scripting/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ namespace scripting

game::scr_VmPub->inparamcount = v7;
}

bool is_entity_variable(const game::scr_entref_t& entref, const unsigned int id)
{
const auto type = game::scr_VarGlob->objectVariableValue[id].w.type;
if (entref.classnum == 0)
{
return type == game::SCRIPT_ENTITY;
}
else if (entref.classnum > 0)
{
return true;
}

return false;
}
}

void push_value(const script_value& value)
Expand All @@ -100,6 +115,12 @@ namespace scripting
const std::vector<script_value>& arguments)
{
const auto entref = entity.get_entity_reference();
const auto ent_id = entity.get_entity_id();

if (!is_entity_variable(entref, ent_id))
{
return {};
}

const auto is_method_call = *reinterpret_cast<const int*>(&entref) != -1;
const auto function = find_function(name, !is_method_call);
Expand Down Expand Up @@ -158,18 +179,22 @@ namespace scripting

unsigned int get_function_pos(const std::string& filename, const std::string& function)
{
if (scripting::script_function_table.find(filename) == scripting::script_function_table.end())
const auto filename_lower = utils::string::to_lower(filename);
const auto function_lower = utils::string::to_lower(function);

if (!scripting::script_function_table.contains(filename_lower))
{
throw std::runtime_error("File '" + filename + "' not found");
};

const auto functions = scripting::script_function_table[filename];
if (functions.find(function) == functions.end())
const auto& functions = scripting::script_function_table[filename_lower];
const auto function_iter = functions.find(function_lower);
if (function_iter == functions.end())
{
throw std::runtime_error("Function '" + function + "' in file '" + filename + "' not found");
}

return functions.at(function);
return function_iter->second;
}

script_value call_script_function(const entity& entity, const std::string& filename,
Expand All @@ -179,53 +204,13 @@ namespace scripting
return exec_ent_thread(entity, pos, arguments);
}

static std::unordered_map<unsigned int, std::unordered_map<std::string, script_value>> custom_fields;

script_value get_custom_field(const entity& entity, const std::string& field)
{
auto fields = custom_fields[entity.get_entity_id()];
const auto _field = fields.find(field);
if (_field != fields.end())
{
return _field->second;
}
return {};
}

void set_custom_field(const entity& entity, const std::string& field, const script_value& value)
{
const auto id = entity.get_entity_id();

if (custom_fields[id].find(field) != custom_fields[id].end())
{
custom_fields[id][field] = value;
return;
}

custom_fields[id].insert(std::make_pair(field, value));
}

void clear_entity_fields(const entity& entity)
{
const auto id = entity.get_entity_id();

if (custom_fields.find(id) != custom_fields.end())
{
custom_fields[id].clear();
}
}

void clear_custom_fields()
{
custom_fields.clear();
}

void set_entity_field(const entity& entity, const std::string& field, const script_value& value)
{
const auto entref = entity.get_entity_reference();
const int id = get_field_id(entref.classnum, field);
const auto ent_id = entity.get_entity_id();

if (id != -1)
if (id != -1 && is_entity_variable(entref, ent_id))
{
stack_isolation _;
push_value(value);
Expand All @@ -240,16 +225,17 @@ namespace scripting
}
else
{
set_custom_field(entity, field, value);
set_object_variable(ent_id, field, value);
}
}

script_value get_entity_field(const entity& entity, const std::string& field)
{
const auto entref = entity.get_entity_reference();
const auto id = get_field_id(entref.classnum, field);
const auto ent_id = entity.get_entity_id();

if (id != -1)
if (id != -1 && is_entity_variable(entref, ent_id))
{
stack_isolation _;

Expand All @@ -266,12 +252,8 @@ namespace scripting

return value;
}
else
{
return get_custom_field(entity, field);
}

return {};
return get_object_variable(ent_id, field);
}

unsigned int make_array()
Expand All @@ -284,4 +266,47 @@ namespace scripting

return index;
}

void set_object_variable(const unsigned int parent_id, const unsigned int id, const script_value& value)
{
const auto offset = 0xC800 * (parent_id & 1);
const auto variable_id = game::GetVariable(parent_id, id);
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
const auto& raw_value = value.get_raw();

game::AddRefToValue(raw_value.type, raw_value.u);
game::RemoveRefToValue(variable->type, variable->u.u);

variable->type = static_cast<char>(raw_value.type);
variable->u.u = raw_value.u;
}

void set_object_variable(const unsigned int parent_id, const std::string& name, const script_value& value)
{
const auto id = scripting::find_token_id(name);
set_object_variable(parent_id, id, value);
}

script_value get_object_variable(const unsigned int parent_id, const unsigned int id)
{
const auto offset = 0xC800 * (parent_id & 1);
const auto variable_id = game::FindVariable(parent_id, id);
if (!variable_id)
{
return {};
}

const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
game::VariableValue value{};
value.type = static_cast<game::scriptType_e>(variable->type);
value.u = variable->u.u;

return value;
}

script_value get_object_variable(const unsigned int parent_id, const std::string& name)
{
const auto id = scripting::find_token_id(name);
return get_object_variable(parent_id, id);
}
}
9 changes: 6 additions & 3 deletions src/game/scripting/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ namespace scripting
script_value call_script_function(const entity& entity, const std::string& filename,
const std::string& function, const std::vector<script_value>& arguments);

void clear_entity_fields(const entity& entity);
void clear_custom_fields();

void set_entity_field(const entity& entity, const std::string& field, const script_value& value);
script_value get_entity_field(const entity& entity, const std::string& field);

void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);

unsigned int make_array();

script_value get_object_variable(const unsigned int parent_id, const unsigned int id);
script_value get_object_variable(const unsigned int parent_id, const std::string& name);

void set_object_variable(const unsigned int parent_id, const std::string& name, const script_value& value);
void set_object_variable(const unsigned int parent_id, const unsigned int id, const script_value& value);
}
Loading

0 comments on commit 22cfa38

Please sign in to comment.