26 Commits

Author SHA1 Message Date
9ca07aca55 Update pointers 2022-01-23 22:09:40 +01:00
fed
10b45027d0 Merge pull request #10 from diamante0018/main
Add isBot function
2022-01-06 22:08:45 +01:00
d5c0063fee Add isBot function 2022-01-06 22:04:19 +01:00
31920b0692 Some fixes 2021-12-27 21:03:29 +01:00
fed
89e4edc00b Fix 2021-12-14 10:03:59 +01:00
fed
379a21ff7e Small fix 2021-12-13 08:06:24 +01:00
0ac5225114 #5 2021-12-13 03:05:22 +01:00
e918bdf28f Update dllmain.cpp 2021-12-02 00:42:58 +01:00
fed
50b83787e7 Update pointers 2021-12-02 00:38:28 +01:00
e6ca925562 Fix setjmp 2021-11-15 22:06:44 +01:00
0f92a7ce25 Fixes for pluto update 2021-11-09 23:55:25 +01:00
d4d91de80f Small change 2021-11-09 22:48:15 +01:00
a508a7e3a4 Also function 2021-11-08 01:42:55 +01:00
640fb032ea Print errors 2021-11-08 01:36:48 +01:00
eee68760e4 Add custom entity field support 2021-11-07 23:42:56 +01:00
7526de9b76 Fix struct 2021-11-07 18:57:55 +01:00
705ad50d65 Fixes for pluto update 2021-11-07 02:33:16 +01:00
e8cad54293 Merge branch 'main' of https://github.com/fedddddd/iw5-gsc-utils 2021-11-07 02:06:05 +01:00
a548e9d04b Update dllmain.cpp 2021-11-07 02:06:03 +01:00
fed
d4c8a7ce2c Merge pull request #3 from diamante0018/main
Add unlimited sprint toggle via gsc
2021-11-07 01:22:05 +01:00
Edo
ba290e0f7c [GSC] VS Delete this space 2021-11-07 00:19:03 +00:00
234f510e98 [GSC] Add infinite sprint 2021-11-07 00:04:31 +00:00
8224ca3b57 Update pointers 2021-11-06 02:16:11 +01:00
a19c2761c8 Update pointers 2021-10-21 18:55:01 +02:00
739ea2a7f0 Update pointers 2021-10-01 03:04:08 +02:00
6c99991429 Typo 2021-08-31 21:06:18 +02:00
21 changed files with 506 additions and 200 deletions

2
deps/GSL vendored

Submodule deps/GSL updated: c1cbb41b42...c31a9ad5e8

View File

@ -14,34 +14,6 @@
namespace gsc namespace gsc
{ {
std::unordered_map<const char*, const char*> replaced_functions;
function_args::function_args(std::vector<scripting::script_value> values)
: values_(values)
{
}
unsigned int function_args::size() const
{
return this->values_.size();
}
std::vector<scripting::script_value> function_args::get_raw() const
{
return this->values_;
}
scripting::script_value function_args::get(const int index) const
{
if (index >= this->values_.size())
{
throw std::runtime_error(utils::string::va("Insufficient arguments, got %i expected %i",
this->values_.size(), index + 1));
}
return this->values_[index];
}
std::unordered_map<unsigned, script_function> functions; std::unordered_map<unsigned, script_function> functions;
std::unordered_map<unsigned, script_method> methods; std::unordered_map<unsigned, script_method> methods;
@ -104,12 +76,24 @@ namespace gsc
auto function_map_start = 0x200; auto function_map_start = 0x200;
auto method_map_start = 0x8400; auto method_map_start = 0x8400;
auto token_map_start = 0x8000;
auto field_offset_start = 0xA000;
struct entity_field
{
std::string name;
std::function<scripting::script_value(unsigned int entnum)> getter;
std::function<void(unsigned int entnum, scripting::script_value)> setter;
};
std::vector<std::function<void()>> post_load_callbacks;
std::unordered_map<unsigned int, std::unordered_map<unsigned int, entity_field>> custom_fields;
void call_function(unsigned int id) void call_function(unsigned int id)
{ {
if (id < 0x200) if (id < 0x200)
{ {
return reinterpret_cast<builtin_function*>(0x1D6EB34)[id](); return reinterpret_cast<builtin_function*>(game::plutonium::function_table.get())[id]();
} }
try try
@ -125,8 +109,8 @@ namespace gsc
catch (const std::exception& e) catch (const std::exception& e)
{ {
printf("************** Script execution error **************\n"); printf("************** Script execution error **************\n");
printf("Error executing function %s\n", function_name(id).data()); printf("Error executing function %s:\n", function_name(id).data());
printf("%s\n", e.what()); printf(" %s\n", e.what());
printf("****************************************************\n"); printf("****************************************************\n");
} }
} }
@ -135,7 +119,7 @@ namespace gsc
{ {
if (id < 0x8400) if (id < 0x8400)
{ {
return reinterpret_cast<builtin_method*>(0x1D4F258)[id](ent); return reinterpret_cast<builtin_method*>(game::plutonium::method_table.get())[id - 0x8000](ent);
} }
try try
@ -151,8 +135,8 @@ namespace gsc
catch (const std::exception& e) catch (const std::exception& e)
{ {
printf("************** Script execution error **************\n"); printf("************** Script execution error **************\n");
printf("Error executing method %s\n", method_name(id).data()); printf("Error executing method %s:\n", method_name(id).data());
printf("%s\n", e.what()); printf(" %s\n", e.what());
printf("****************************************************\n"); printf("****************************************************\n");
} }
} }
@ -202,60 +186,63 @@ namespace gsc
} }
} }
const char* replaced_pos = 0; utils::hook::detour scr_get_object_field_hook;
void scr_get_object_field_stub(unsigned int classnum, int entnum, unsigned int offset)
void get_replaced_pos(const char* pos)
{ {
if (replaced_functions.find(pos) != replaced_functions.end()) if (custom_fields[classnum].find(offset) == custom_fields[classnum].end())
{ {
replaced_pos = replaced_functions[pos]; return scr_get_object_field_hook.invoke<void>(classnum, entnum, offset);
}
const auto field = custom_fields[classnum][offset];
try
{
const auto result = field.getter(entnum);
return_value(result);
}
catch (const std::exception& e)
{
printf("************** Script execution error **************\n");
printf("Error getting field %s:\n", field.name.data());
printf(" %s\n", e.what());
printf("****************************************************\n");
} }
} }
__declspec(naked) void vm_execute_stub() utils::hook::detour scr_set_object_field_hook;
void scr_set_object_field_stub(unsigned int classnum, int entnum, unsigned int offset)
{ {
__asm if (custom_fields[classnum].find(offset) == custom_fields[classnum].end())
{ {
pushad return scr_set_object_field_hook.invoke<void>(classnum, entnum, offset);
push esi
call get_replaced_pos
pop esi
popad
cmp replaced_pos, 0
jne set_pos
movzx eax, byte ptr[esi]
inc esi
jmp loc_1
loc_1:
mov [ebp - 0x18], eax
mov [ebp - 0x8], esi
push ecx
mov ecx, 0x20B8E28
mov [ecx], eax
mov ecx, 0x20B4A5C
mov[ecx], esi
pop ecx
cmp eax, 0x98
push 0x56B740
retn
set_pos:
mov esi, replaced_pos
mov replaced_pos, 0
movzx eax, byte ptr[esi]
inc esi
jmp loc_1
} }
const auto args = get_arguments();
const auto field = custom_fields[classnum][offset];
try
{
field.setter(entnum, args[0]);
}
catch (const std::exception& e)
{
printf("************** Script execution error **************\n");
printf("Error setting field %s:\n", field.name.data());
printf(" %s\n", e.what());
printf("****************************************************\n");
}
}
utils::hook::detour scr_post_load_scripts_hook;
void scr_post_load_scripts_stub()
{
for (const auto& callback : post_load_callbacks)
{
callback();
}
return scr_post_load_scripts_hook.invoke<void>();
} }
} }
@ -281,27 +268,92 @@ namespace gsc
} }
} }
namespace field
{
void add(const classid classnum, const std::string& name,
const std::function<scripting::script_value(unsigned int entnum)>& getter,
const std::function<void(unsigned int entnum, const scripting::script_value&)>& setter)
{
const auto token_id = token_map_start++;
const auto offset = field_offset_start++;
custom_fields[classnum][offset] = {name, getter, setter};
(*game::plutonium::token_map_rev)[name] = token_id;
post_load_callbacks.push_back([classnum, name, token_id, offset]()
{
const auto name_str = game::SL_GetString(name.data(), 0);
game::Scr_AddClassField(classnum, name_str, token_id, offset);
});
}
}
function_args::function_args(std::vector<scripting::script_value> values)
: values_(values)
{
}
unsigned int function_args::size() const
{
return this->values_.size();
}
std::vector<scripting::script_value> function_args::get_raw() const
{
return this->values_;
}
scripting::value_wrap function_args::get(const int index) const
{
if (index >= this->values_.size())
{
throw std::runtime_error(utils::string::va("parameter %d does not exist", index));
}
return {this->values_[index], index};
}
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
scr_get_object_field_hook.create(0x52BDB0, scr_get_object_field_stub);
scr_set_object_field_hook.create(0x52BCC0, scr_set_object_field_stub);
scr_post_load_scripts_hook.create(0x628B50, scr_post_load_scripts_stub);
field::add(classid::entity, "flags",
[](unsigned int entnum) -> scripting::script_value
{
const auto entity = &game::g_entities[entnum];
return entity->flags;
},
[](unsigned int entnum, const scripting::script_value& value)
{
const auto entity = &game::g_entities[entnum];
entity->flags = value.as<int>();
}
);
field::add(classid::entity, "clientflags",
[](unsigned int entnum) -> scripting::script_value
{
const auto entity = &game::g_entities[entnum];
return entity->client->flags;
},
[](unsigned int entnum, const scripting::script_value& value)
{
const auto entity = &game::g_entities[entnum];
entity->client->flags = value.as<int>();
}
);
function::add("executecommand", [](const function_args& args) -> scripting::script_value function::add("executecommand", [](const function_args& args) -> scripting::script_value
{ {
game::Cbuf_AddText(0, args[0].as<const char*>()); game::Cbuf_AddText(0, args[0].as<const char*>());
return {}; return {};
}); });
function::add("replacefunc", [](const function_args& args) -> scripting::script_value
{
const auto what = args[0].as<scripting::function>();
const auto with = args[1].as<scripting::function>();
replaced_functions[what.get_pos()] = with.get_pos();
return {};
});
function::add("addcommand", [](const function_args& args) -> scripting::script_value function::add("addcommand", [](const function_args& args) -> scripting::script_value
{ {
const auto name = args[0].as<std::string>(); const auto name = args[0].as<std::string>();
@ -343,11 +395,47 @@ namespace gsc
return {}; return {};
}); });
method::add("specialtymarathon", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
{
if (ent.classnum != 0)
{
throw std::runtime_error("Invalid entity");
}
const auto num = ent.entnum;
const auto toggle = args[0].as<int>();
auto g_client = game::g_entities[num].client;
auto playerState = &g_client->ps;
auto flags = playerState->perks[0];
playerState->perks[0] = toggle
? flags | 0x4000u
: flags & ~0x4000u;
return {};
});
method::add("isbot", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
{
if (ent.classnum != 0)
{
throw std::runtime_error("Invalid entity");
}
const auto client = ent.entnum;
if (game::g_entities[client].client == nullptr)
{
throw std::runtime_error("entity is not a player");
}
return game::svs_clients[client].bIsTestClient;
});
utils::hook::jump(0x56C8EB, call_builtin_stub); utils::hook::jump(0x56C8EB, call_builtin_stub);
utils::hook::jump(0x56CBDC, call_builtin_method_stub); utils::hook::jump(0x56CBDC, call_builtin_method_stub);
utils::hook::jump(0x56B726, vm_execute_stub);
} }
}; };
} }
REGISTER_COMPONENT(gsc::component) REGISTER_COMPONENT(gsc::component)

View File

@ -1,8 +1,17 @@
#pragma once #pragma once
#include "game/scripting/array.hpp"
#include "game/scripting/execution.hpp"
namespace gsc namespace gsc
{ {
extern std::unordered_map<const char*, const char*> replaced_functions; enum classid
{
entity,
hudelem,
pathnode,
node,
count
};
class function_args class function_args
{ {
@ -11,9 +20,9 @@ namespace gsc
unsigned int size() const; unsigned int size() const;
std::vector<scripting::script_value> get_raw() const; std::vector<scripting::script_value> get_raw() const;
scripting::script_value get(const int index) const; scripting::value_wrap get(const int index) const;
scripting::script_value operator[](const int index) const scripting::value_wrap operator[](const int index) const
{ {
return this->get(index); return this->get(index);
} }
@ -36,4 +45,11 @@ namespace gsc
{ {
void add(const std::string& name, const script_method& func); void add(const std::string& name, const script_method& func);
} }
namespace field
{
void add(const classid classnum, const std::string& name,
const std::function<scripting::script_value(unsigned int entnum)>& getter,
const std::function<void(unsigned int entnum, const scripting::script_value&)>& setter);
}
} }

View File

@ -2,12 +2,6 @@
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "game/scripting/event.hpp"
#include "game/scripting/execution.hpp"
#include "game/scripting/functions.hpp"
#include "game/scripting/array.hpp"
#include "gsc.hpp" #include "gsc.hpp"
#include "json.hpp" #include "json.hpp"

View File

@ -2,12 +2,6 @@
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "game/scripting/event.hpp"
#include "game/scripting/execution.hpp"
#include "game/scripting/functions.hpp"
#include "game/scripting/array.hpp"
#include "gsc.hpp" #include "gsc.hpp"
#include "json.hpp" #include "json.hpp"
@ -28,18 +22,23 @@ namespace json
const auto keys = array.get_keys(); const auto keys = array.get_keys();
for (auto i = 0; i < keys.size(); i++) for (auto i = 0; i < keys.size(); i++)
{ {
const auto is_int = keys[i].is<int>();
const auto is_string = keys[i].is<std::string>();
if (string_indexed == -1) if (string_indexed == -1)
{ {
string_indexed = keys[i].is_string; string_indexed = is_string;
} }
if (!string_indexed && keys[i].is_integer) if (!string_indexed && is_int)
{ {
obj[keys[i].index] = gsc_to_json(array[keys[i].index]); const auto index = keys[i].as<int>();
obj[index] = gsc_to_json(array[index]);
} }
else if (string_indexed && keys[i].is_string) else if (string_indexed && is_string)
{ {
obj.emplace(keys[i].key, gsc_to_json(array[keys[i].key])); const auto key = keys[i].as<std::string>();
obj.emplace(key, gsc_to_json(array[key]));
} }
} }

View File

@ -1,14 +1,15 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "scheduler.hpp"
#include "game/scripting/entity.hpp" #include "scheduler.hpp"
#include "game/scripting/execution.hpp" #include "gsc.hpp"
#include "scripting.hpp"
namespace notifies namespace notifies
{ {
namespace namespace
{ {
std::vector<scripting::function> say_callbacks;
utils::hook::detour client_command_hook; utils::hook::detour client_command_hook;
void client_command_stub(int clientNum) void client_command_stub(int clientNum)
@ -17,19 +18,28 @@ namespace notifies
game::SV_Cmd_ArgvBuffer(0, cmd, 1024); game::SV_Cmd_ArgvBuffer(0, cmd, 1024);
auto hidden = false;
if (cmd == "say"s || cmd == "say_team"s) if (cmd == "say"s || cmd == "say_team"s)
{ {
std::string message = game::ConcatArgs(1); std::string message = game::ConcatArgs(1);
message.erase(0, 1); message.erase(0, 1);
const scripting::entity level{*game::levelEntityId}; for (const auto& callback : say_callbacks)
const auto player = scripting::call("getEntByNum", {clientNum}).as<scripting::entity>(); {
const auto entity_id = game::Scr_GetEntityId(clientNum, 0);
const auto result = callback(entity_id, {message, cmd == "say_team"s});
scripting::notify(level, cmd, {player, message}); if (result.is<int>() && !hidden)
scripting::notify(player, cmd, {message}); {
hidden = result.as<int>() == 0;
}
}
} }
return client_command_hook.invoke<void>(clientNum); if (!hidden)
{
client_command_hook.invoke<void>(clientNum);
}
} }
} }
@ -39,6 +49,18 @@ namespace notifies
void post_unpack() override void post_unpack() override
{ {
client_command_hook.create(0x502CB0, client_command_stub); client_command_hook.create(0x502CB0, client_command_stub);
scripting::on_shutdown([]()
{
say_callbacks.clear();
});
gsc::function::add("onplayersay", [](const gsc::function_args& args) -> scripting::script_value
{
const auto function = args[0].as<scripting::function>();
say_callbacks.push_back(function);
return {};
});
} }
}; };
} }

View File

@ -29,6 +29,8 @@ namespace scripting
std::string current_file; std::string current_file;
std::vector<std::function<void()>> shutdown_callbacks;
void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value, void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value,
game::VariableValue* top) game::VariableValue* top)
{ {
@ -77,7 +79,12 @@ namespace scripting
{ {
userinfo::clear_overrides(); userinfo::clear_overrides();
command::clear_script_commands(); command::clear_script_commands();
gsc::replaced_functions.clear();
for (const auto& callback : shutdown_callbacks)
{
callback();
}
g_shutdown_game_hook.invoke<void>(free_scripts); g_shutdown_game_hook.invoke<void>(free_scripts);
} }
@ -107,6 +114,11 @@ namespace scripting
} }
} }
void on_shutdown(const std::function<void()>& callback)
{
shutdown_callbacks.push_back(callback);
}
class component final : public component_interface class component final : public component_interface
{ {
public: public:

View File

@ -4,4 +4,6 @@ namespace scripting
{ {
extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table; extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table; extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
void on_shutdown(const std::function<void()>& callback);
} }

View File

@ -2,12 +2,6 @@
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "game/scripting/event.hpp"
#include "game/scripting/execution.hpp"
#include "game/scripting/functions.hpp"
#include "game/scripting/array.hpp"
#include "gsc.hpp" #include "gsc.hpp"
namespace userinfo namespace userinfo
@ -19,12 +13,17 @@ namespace userinfo
{ {
utils::hook::detour sv_getuserinfo_hook; utils::hook::detour sv_getuserinfo_hook;
userinfo_map userinfo_to_map(const std::string& userinfo) userinfo_map userinfo_to_map(std::string userinfo)
{ {
userinfo_map map; userinfo_map map{};
const auto args = utils::string::split(userinfo, '\\');
for (auto i = 1; i < args.size() - 1; i += 2) if (userinfo[0] == '\\')
{
userinfo = userinfo.substr(1);
}
const auto args = utils::string::split(userinfo, '\\');
for (size_t i = 0; !args.empty() && i < (args.size() - 1); i += 2)
{ {
map[args[i]] = args[i + 1]; map[args[i]] = args[i + 1];
} }
@ -34,7 +33,7 @@ namespace userinfo
std::string map_to_userinfo(const userinfo_map& map) std::string map_to_userinfo(const userinfo_map& map)
{ {
std::string buffer; std::string buffer{};
for (const auto& value : map) for (const auto& value : map)
{ {
@ -49,9 +48,8 @@ namespace userinfo
void sv_getuserinfo_stub(int index, char* buffer, int bufferSize) void sv_getuserinfo_stub(int index, char* buffer, int bufferSize)
{ {
char _buffer[1024]; sv_getuserinfo_hook.invoke<void>(index, buffer, bufferSize);
sv_getuserinfo_hook.invoke<void>(index, _buffer, 1024); auto map = userinfo_to_map(buffer);
auto map = userinfo_to_map(_buffer);
if (userinfo_overrides.find(index) == userinfo_overrides.end()) if (userinfo_overrides.find(index) == userinfo_overrides.end())
{ {
@ -109,8 +107,6 @@ namespace userinfo
gsc::method::add("resetname", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value gsc::method::add("resetname", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
{ {
const auto name = args[0].as<std::string>();
if (ent.classnum != 0 || ent.entnum > 17) if (ent.classnum != 0 || ent.entnum > 17)
{ {
throw std::runtime_error("Invalid entity"); throw std::runtime_error("Invalid entity");
@ -141,8 +137,6 @@ namespace userinfo
gsc::method::add("resetclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value gsc::method::add("resetclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
{ {
const auto name = args[0].as<std::string>();
if (ent.classnum != 0 || ent.entnum > 17) if (ent.classnum != 0 || ent.entnum > 17)
{ {
throw std::runtime_error("Invalid entity"); throw std::runtime_error("Invalid entity");

View File

@ -5,16 +5,18 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
{ {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{ {
const auto value = *reinterpret_cast<DWORD*>(0x20600000); const auto value = *reinterpret_cast<DWORD*>(0x21000000);
if (value != 0x77E0B164) if (value != 0x7AC6)
{ {
printf("\x1b[31m\n**************************************************************************************\n\n"); MessageBoxA(NULL,
printf("This version of \x1b[33miw5-gsc-utils\x1b[31m is outdated.\n"); "This version of iw5-gsc-utils is outdated.\n" \
printf("Download the latest dll from here:\x1b[34m https://github.com/fedddddd/iw5-gsc-utils/releases\ \x1b[31m\n"); "Download the latest dll from here: https://github.com/fedddddd/iw5-gsc-utils/releases" \
printf("\n**************************************************************************************\n\n\x1b[37m"); , "ERROR", MB_ICONERROR);
return FALSE; return FALSE;
} }
utils::hook::jump(reinterpret_cast<uintptr_t>(&printf), game::plutonium::printf);
component_loader::post_unpack(); component_loader::post_unpack();
} }

View File

@ -1,5 +1,6 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "array.hpp" #include "array.hpp"
#include "script_value.hpp"
#include "execution.hpp" #include "execution.hpp"
namespace scripting namespace scripting
@ -59,13 +60,11 @@ namespace scripting
array::array() array::array()
{ {
this->id_ = make_array(); this->id_ = make_array();
this->add();
} }
array::array(std::vector<script_value> values) array::array(std::vector<script_value> values)
{ {
this->id_ = make_array(); this->id_ = make_array();
this->add();
for (const auto& value : values) for (const auto& value : values)
{ {
@ -76,7 +75,6 @@ namespace scripting
array::array(std::unordered_map<std::string, script_value> values) array::array(std::unordered_map<std::string, script_value> values)
{ {
this->id_ = make_array(); this->id_ = make_array();
this->add();
for (const auto& value : values) for (const auto& value : values)
{ {
@ -129,9 +127,9 @@ namespace scripting
} }
} }
std::vector<array_key> array::get_keys() const std::vector<script_value> array::get_keys() const
{ {
std::vector<array_key> result; std::vector<script_value> result;
const auto offset = 0xC800 * (this->id_ & 1); const auto offset = 0xC800 * (this->id_ & 1);
auto current = game::scr_VarGlob->objectVariableChildren[this->id_].firstChild; auto current = game::scr_VarGlob->objectVariableChildren[this->id_].firstChild;
@ -149,16 +147,14 @@ namespace scripting
const auto string_value = (unsigned int)((unsigned __int8)var.name_lo + (var.k.keys.name_hi << 8)); const auto string_value = (unsigned int)((unsigned __int8)var.name_lo + (var.k.keys.name_hi << 8));
const auto* str = game::SL_ConvertToString(string_value); const auto* str = game::SL_ConvertToString(string_value);
array_key key; script_value key;
if (string_value < 0x40000 && str) if (string_value < 0x40000 && str)
{ {
key.is_string = true; key = str;
key.key = str;
} }
else else
{ {
key.is_integer = true; key = (string_value - 0x800000) & 0xFFFFFF;
key.index = (string_value - 0x800000) & 0xFFFFFF;
} }
result.push_back(key); result.push_back(key);
@ -206,16 +202,18 @@ namespace scripting
return value; return value;
} }
script_value array::get(const array_key& key) const script_value array::get(const script_value& key) const
{ {
if (key.is_integer) if (key.is<int>())
{ {
return this->get(key.index); return this->get(key.as<int>());
} }
else else
{ {
return this->get(key.key); return this->get(key.as<std::string>());
} }
return {};
} }
script_value array::get(const std::string& key) const script_value array::get(const std::string& key) const
@ -253,15 +251,15 @@ namespace scripting
return variable; return variable;
} }
void array::set(const array_key& key, const script_value& value) const void array::set(const script_value& key, const script_value& value) const
{ {
if (key.is_integer) if (key.is<int>())
{ {
this->set(key.index, value); this->set(key.as<int>(), value);
} }
else else
{ {
this->set(key.key, value); this->set(key.as<std::string>(), value);
} }
} }

View File

@ -1,16 +1,9 @@
#pragma once #pragma once
#include "game/game.hpp"
#include "script_value.hpp" #include "script_value.hpp"
namespace scripting namespace scripting
{ {
struct array_key
{
bool is_string = false;
bool is_integer = false;
unsigned int index{};
std::string key{};
};
class array_value : public script_value class array_value : public script_value
{ {
public: public:
@ -38,7 +31,7 @@ namespace scripting
array& operator=(const array& other); array& operator=(const array& other);
array& operator=(array&& other) noexcept; array& operator=(array&& other) noexcept;
std::vector<array_key> get_keys() const; std::vector<script_value> get_keys() const;
unsigned int size() const; unsigned int size() const;
unsigned int push(script_value) const; unsigned int push(script_value) const;
@ -46,11 +39,11 @@ namespace scripting
void erase(const std::string&) const; void erase(const std::string&) const;
script_value pop() const; script_value pop() const;
script_value get(const array_key&) const; script_value get(const script_value&) const;
script_value get(const std::string&) const; script_value get(const std::string&) const;
script_value get(const unsigned int) const; script_value get(const unsigned int) const;
void set(const array_key&, const script_value&) const; void set(const script_value&, const script_value&) const;
void set(const std::string&, const script_value&) const; void set(const std::string&, const script_value&) const;
void set(const unsigned int, const script_value&) const; void set(const unsigned int, const script_value&) const;
@ -71,15 +64,17 @@ namespace scripting
return {this->id_, this->get_value_id(key)}; return {this->id_, this->get_value_id(key)};
} }
array_value operator[](const array_key& key) const template <typename I = int, typename S = std::string>
array_value operator[](const script_value& key) const
{ {
if (key.is_integer) if (key.is<I>())
{ {
return {this->id_, this->get_value_id(key.index)}; return { this->id_, this->get_value_id(key.as<I>()) };
} }
else
if (key.is<S>())
{ {
return {this->id_, this->get_value_id(key.key)}; return { this->id_, this->get_value_id(key.as<S>()) };
} }
} }

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "game/game.hpp" #include "game/game.hpp"
#include "entity.hpp" #include "entity.hpp"
#include "array.hpp"
#include "function.hpp"
#include "script_value.hpp" #include "script_value.hpp"
namespace scripting namespace scripting

View File

@ -23,7 +23,7 @@ namespace scripting
return this->pos_; return this->pos_;
} }
script_value function::call(entity self, std::vector<script_value> arguments) const script_value function::call(const entity& self, std::vector<script_value> arguments) const
{ {
return exec_ent_thread(self, this->pos_, arguments); return exec_ent_thread(self, this->pos_, arguments);
} }

View File

@ -12,9 +12,9 @@ namespace scripting
script_value get_raw() const; script_value get_raw() const;
const char* get_pos() const; const char* get_pos() const;
script_value call(entity self, std::vector<script_value> arguments) const; script_value call(const entity& self, std::vector<script_value> arguments) const;
script_value operator()(entity self, std::vector<script_value> arguments) const script_value operator()(const entity& self, std::vector<script_value> arguments) const
{ {
return this->call(self, arguments); return this->call(self, arguments);
} }

View File

@ -59,15 +59,15 @@ namespace scripting
script_function get_function_by_index(const unsigned index) script_function get_function_by_index(const unsigned index)
{ {
static const auto function_table = 0x1D6EB34; static const auto function_table = game::plutonium::function_table.get();
static const auto method_table = 0x1D4F258; static const auto method_table = game::plutonium::method_table.get();
if (index < 0x1C7) if (index < 0x1C7)
{ {
return reinterpret_cast<script_function*>(function_table)[index]; return reinterpret_cast<script_function*>(function_table)[index];
} }
return reinterpret_cast<script_function*>(method_table)[index]; return reinterpret_cast<script_function*>(method_table)[index - 0x8000];
} }
} }

View File

@ -25,7 +25,7 @@ namespace scripting::safe_execution
bool call(const script_function function, const game::scr_entref_t& entref) bool call(const script_function function, const game::scr_entref_t& entref)
{ {
*game::g_script_error_level += 1; *game::g_script_error_level += 1;
if (game::_setjmp(&game::g_script_error[*game::g_script_error_level])) if (game::_setjmp(&game::g_script_error[*game::g_script_error_level], 0, 0, 0))
{ {
*game::g_script_error_level -= 1; *game::g_script_error_level -= 1;
return false; return false;
@ -39,7 +39,7 @@ namespace scripting::safe_execution
bool set_entity_field(const game::scr_entref_t& entref, const int offset) bool set_entity_field(const game::scr_entref_t& entref, const int offset)
{ {
*game::g_script_error_level += 1; *game::g_script_error_level += 1;
if (game::_setjmp(&game::g_script_error[*game::g_script_error_level])) if (game::_setjmp(&game::g_script_error[*game::g_script_error_level], 0, 0, 0))
{ {
*game::g_script_error_level -= 1; *game::g_script_error_level -= 1;
return false; return false;
@ -54,7 +54,7 @@ namespace scripting::safe_execution
bool get_entity_field(const game::scr_entref_t& entref, const int offset, game::VariableValue* value) bool get_entity_field(const game::scr_entref_t& entref, const int offset, game::VariableValue* value)
{ {
*game::g_script_error_level += 1; *game::g_script_error_level += 1;
if (game::_setjmp(&game::g_script_error[*game::g_script_error_level])) if (game::_setjmp(&game::g_script_error[*game::g_script_error_level], 0, 0, 0))
{ {
value->type = game::SCRIPT_NONE; value->type = game::SCRIPT_NONE;
value->u.intValue = 0; value->u.intValue = 0;

View File

@ -15,6 +15,11 @@ namespace scripting
{ {
} }
script_value::script_value(const value_wrap& value)
: value_(value.get_raw())
{
}
script_value::script_value(void* value) script_value::script_value(void* value)
{ {
game::VariableValue variable{}; game::VariableValue variable{};
@ -315,4 +320,10 @@ namespace scripting
{ {
return this->value_.get(); return this->value_.get();
} }
value_wrap::value_wrap(const scripting::script_value& value, int argument_index)
: value_(value)
, argument_index_(argument_index)
{
}
} }

View File

@ -8,12 +8,100 @@ namespace scripting
class entity; class entity;
class array; class array;
class function; class function;
class value_wrap;
namespace
{
std::unordered_map<int, std::string> typenames =
{
{0, "undefined"},
{1, "object"},
{2, "string"},
{3, "localized string"},
{4, "vector"},
{5, "float"},
{6, "integer"},
{7, "codepos"},
{8, "precodepos"},
{9, "function"},
{10, "builtin function"},
{11, "builtin method"},
{12, "stack"},
{13, "animation"},
{14, "pre animation"},
{15, "thread"},
{16, "thread"},
{17, "thread"},
{18, "thread"},
{19, "struct"},
{20, "removed entity"},
{21, "entity"},
{22, "array"},
{23, "removed thread"},
{24, "count"},
{25, "<free>"},
{26, "thread list"},
{27, "endon list"},
};
std::string get_typename(const game::VariableValue& value)
{
if (value.type == game::SCRIPT_OBJECT)
{
const auto type = game::scr_VarGlob->objectVariableValue[value.u.uintValue].w.type;
return typenames[type];
}
else
{
return typenames[value.type];
}
}
template <typename T, typename A = array>
std::string get_c_typename()
{
auto& info = typeid(T);
if (info == typeid(std::string))
{
return "string";
}
if (info == typeid(const char*))
{
return "string";
}
if (info == typeid(entity))
{
return "entity";
}
if (info == typeid(array))
{
return "array";
}
if (info == typeid(function))
{
return "function";
}
if (info == typeid(vector))
{
return "vector";
}
return info.name();
}
}
class script_value class script_value
{ {
public: public:
script_value() = default; script_value() = default;
script_value(const game::VariableValue& value); script_value(const game::VariableValue& value);
script_value(const value_wrap& value);
script_value(void* value); script_value(void* value);
@ -43,7 +131,9 @@ namespace scripting
{ {
if (!this->is<T>()) if (!this->is<T>())
{ {
throw std::runtime_error("Invalid type"); const auto type = get_typename(this->get_raw());
const auto c_type = get_c_typename<T>();
throw std::runtime_error(std::string("has type '" + type + "' but should be '" + c_type + "'"));
} }
return get<T>(); return get<T>();
@ -52,16 +142,12 @@ namespace scripting
template <typename T, typename I = int> template <typename T, typename I = int>
T* as_ptr() T* as_ptr()
{ {
if (!this->is<I>())
{
throw std::runtime_error("Invalid type");
}
const auto value = this->get<I>(); const auto value = this->as<I>();
if (!value) if (!value)
{ {
throw std::runtime_error("Null pointer"); throw std::runtime_error("is null");
} }
return reinterpret_cast<T*>(value); return reinterpret_cast<T*>(value);
@ -74,4 +160,50 @@ namespace scripting
template <typename T> template <typename T>
T get() const; T get() const;
}; };
class value_wrap
{
public:
value_wrap(const scripting::script_value& value, int argument_index);
template <typename T>
T as() const
{
try
{
return this->value_.as<T>();
}
catch (const std::exception& e)
{
throw std::runtime_error(utils::string::va("parameter %d %s", this->argument_index_, e.what()));
}
}
template <typename T, typename I = int>
T* as_ptr()
{
try
{
return this->value_.as_ptr<T>();
}
catch (const std::exception& e)
{
throw std::runtime_error(utils::string::va("parameter %d %s", this->argument_index_, e.what()));
}
}
template <typename T>
T is() const
{
return this->value_.is<T>();
}
const game::VariableValue& get_raw() const
{
return this->value_.get_raw();
}
int argument_index_{};
scripting::script_value value_;
};
} }

View File

@ -303,8 +303,39 @@ namespace game
dvar_t* hashNext; dvar_t* hashNext;
}; };
struct playerState_s
{
char __pad0[0x4EC];
unsigned int perks[0x2];
unsigned int perkSlots[0x9];
char __pad1[0x2DE8];
};
struct gclient_s
{
playerState_s ps;
char __pad0[0x2CC];
int flags;
};
struct gentity_s struct gentity_s
{ {
int entnum; int entnum;
char __pad0[0x154];
gclient_s* client;
char __pad1[0x28];
int flags;
char __pad2[0xEC];
}; };
struct client_s
{
char __pad0[0x41CB2];
unsigned __int16 scriptId; // 269490
int bIsTestClient; // 269492
int serverId; // 269496
char __pad1[0x369DC];
};
static_assert(sizeof(client_s) == 0x78698);
} }

View File

@ -49,8 +49,10 @@ namespace game
WEAK symbol<unsigned int(unsigned int threadId)> Scr_GetSelf{0x5655E0}; WEAK symbol<unsigned int(unsigned int threadId)> Scr_GetSelf{0x5655E0};
WEAK symbol<void()> Scr_MakeArray{0x56ADE0}; WEAK symbol<void()> Scr_MakeArray{0x56ADE0};
WEAK symbol<void(unsigned int stringValue)> Scr_AddArrayStringIndexed{0x56AE70}; WEAK symbol<void(unsigned int stringValue)> Scr_AddArrayStringIndexed{0x56AE70};
WEAK symbol<void(unsigned int classnum, unsigned int name, unsigned int canonicalString, unsigned int offset)> Scr_AddClassField{0x567CD0};
WEAK symbol<unsigned int(const char* str, unsigned int user)> SL_GetString{0x5649E0}; WEAK symbol<unsigned int(const char* str, unsigned int user)> SL_GetString{0x5649E0};
WEAK symbol<unsigned int(const char* str)> SL_GetCanonicalString{0x5619A0};
WEAK symbol<const char*(unsigned int stringValue)> SL_ConvertToString{0x564270}; WEAK symbol<const char*(unsigned int stringValue)> SL_ConvertToString{0x564270};
WEAK symbol<void(int clientNum, int type, const char* command)> SV_GameSendServerCommand{0x573220}; WEAK symbol<void(int clientNum, int type, const char* command)> SV_GameSendServerCommand{0x573220};
@ -60,7 +62,7 @@ namespace game
WEAK symbol<unsigned int(unsigned int localId, const char* pos, unsigned int paramcount)> VM_Execute{0x56DFE0}; WEAK symbol<unsigned int(unsigned int localId, const char* pos, unsigned int paramcount)> VM_Execute{0x56DFE0};
WEAK symbol<void* (jmp_buf* Buf, int Value)> longjmp{0x7363BC}; WEAK symbol<void* (jmp_buf* Buf, int Value)> longjmp{0x7363BC};
WEAK symbol<int(jmp_buf* Buf)> _setjmp{0x734CF8}; WEAK symbol<int(jmp_buf* Buf, int a2, int a3, int a4)> _setjmp{0x734CF8};
// Variables // Variables
@ -74,12 +76,18 @@ namespace game
WEAK symbol<scr_classStruct_t*> g_classMap{0x8B4300}; WEAK symbol<scr_classStruct_t*> g_classMap{0x8B4300};
WEAK symbol<gentity_s> g_entities{0x0}; WEAK symbol<gentity_s> g_entities{0x1A66E28};
WEAK symbol<unsigned int> levelEntityId{0x208E1A4}; WEAK symbol<unsigned int> levelEntityId{0x208E1A4};
WEAK symbol<client_s> svs_clients{0x4B5CF90};
namespace plutonium namespace plutonium
{ {
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> function_map_rev{0x2059A610}; WEAK symbol<std::unordered_map<std::string, std::uint16_t>> function_map_rev{0x206964D0};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0x2059A630}; WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0x206964F0};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> token_map_rev{0x20696530};
WEAK symbol<int(const char* fmt, ...)> printf{0x20887840};
WEAK symbol<void*> function_table{0x2068F210};
WEAK symbol<void*> method_table{0x2068F9E0};
} }
} }