mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2025-07-03 09:41:51 +00:00
Compare commits
67 Commits
Author | SHA1 | Date | |
---|---|---|---|
3a492f51d9 | |||
d4ba37cde5 | |||
2ded4c6a2a | |||
24b3a9369e | |||
1d94fdd88d | |||
0af6b1cb13 | |||
d72c6f7057 | |||
39b9e7e352 | |||
36fefa6bb5 | |||
4ffdb331b4 | |||
7b995b3348 | |||
14d0f401fb | |||
df23604b1e | |||
e2960bc895 | |||
db90d361a9 | |||
6ec0c5e23c | |||
c84d681a3a | |||
62d1c8671b | |||
877adfa4b6 | |||
12fd44d0da | |||
12562eb8fd | |||
286481cbcd | |||
e9a877bd74 | |||
3060df1f05 | |||
aa1ae8c574 | |||
ccd8176c25
|
|||
c230a1d916
|
|||
2785a60dc0
|
|||
1b65bda323 | |||
9ca07aca55 | |||
10b45027d0 | |||
d5c0063fee
|
|||
31920b0692 | |||
89e4edc00b | |||
379a21ff7e | |||
0ac5225114 | |||
e918bdf28f | |||
50b83787e7 | |||
e6ca925562 | |||
0f92a7ce25 | |||
d4d91de80f | |||
a508a7e3a4 | |||
640fb032ea | |||
eee68760e4 | |||
7526de9b76 | |||
705ad50d65 | |||
e8cad54293 | |||
a548e9d04b | |||
d4c8a7ce2c | |||
ba290e0f7c | |||
234f510e98 | |||
8224ca3b57 | |||
a19c2761c8 | |||
739ea2a7f0 | |||
6c99991429 | |||
c9543dfbe4 | |||
76842b4bce | |||
7139e8d3c4 | |||
6f53ed5d87 | |||
063caa51f9 | |||
9ed8626067 | |||
cb0725bad1 | |||
44886c8f9f | |||
bdfd37de35 | |||
c49ca5a8cd | |||
b1a29ded7d | |||
e5083cd228 |
7
.gitmodules
vendored
7
.gitmodules
vendored
@ -4,12 +4,7 @@
|
||||
[submodule "deps/GSL"]
|
||||
path = deps/GSL
|
||||
url = https://github.com/microsoft/GSL.git
|
||||
[submodule "deps/sol2"]
|
||||
path = deps/sol2
|
||||
url = https://github.com/ThePhD/sol2.git
|
||||
[submodule "deps/lua"]
|
||||
path = deps/lua
|
||||
url = https://github.com/lua/lua.git
|
||||
[submodule "deps/json"]
|
||||
path = deps/json
|
||||
url = https://github.com/nlohmann/json.git
|
||||
branch = develop
|
||||
|
11
README.md
11
README.md
@ -43,6 +43,15 @@ This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
}
|
||||
}
|
||||
```
|
||||
# Player
|
||||
* `say(message)`: Prints a message to all players' chat.
|
||||
|
||||
* `self tell(message)`: Prints a message to the player's chat.
|
||||
* `self setName(name)`: Sets a player's name.
|
||||
* `self resetName(name)`: Resets a player's name to its original.
|
||||
* `self setClantag(name)`: Sets a player's clantag.
|
||||
* `self resetClantag(name)`: Resets a player's clantag to its original.
|
||||
* `self removeClantag(name)`: Removes a player's clantag.
|
||||
# IO
|
||||
|
||||
The basepath for all IO functions is `Plutonium/storage/iw5`
|
||||
@ -217,6 +226,6 @@ The basepath for all IO functions is `Plutonium/storage/iw5`
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
* `jsonPrint(...)`: Prints values as json.
|
||||
# Credits
|
||||
* [xensik](https://github.com/xensik)
|
||||
|
2
deps/GSL
vendored
2
deps/GSL
vendored
Submodule deps/GSL updated: c1cbb41b42...c31a9ad5e8
2
deps/json
vendored
2
deps/json
vendored
Submodule deps/json updated: e10a3fac8a...e4643d1f1b
@ -1,2 +1,3 @@
|
||||
@echo off
|
||||
tools\windows\premake5.exe vs2019
|
||||
call git submodule update --init --recursive
|
||||
tools\windows\premake5.exe vs2022
|
21
premake5.lua
21
premake5.lua
@ -36,12 +36,19 @@ workspace "iw5-gsc-utils"
|
||||
targetdir "%{wks.location}/bin/%{cfg.buildcfg}"
|
||||
targetname "%{prj.name}"
|
||||
|
||||
configurations { "Debug", "Release", }
|
||||
|
||||
language "C++"
|
||||
cppdialect "C++20"
|
||||
|
||||
architecture "x86"
|
||||
|
||||
buildoptions "/std:c++latest"
|
||||
systemversion "latest"
|
||||
symbols "On"
|
||||
staticruntime "On"
|
||||
editandcontinue "Off"
|
||||
warnings "Extra"
|
||||
characterset "ASCII"
|
||||
|
||||
flags
|
||||
{
|
||||
@ -49,19 +56,15 @@ workspace "iw5-gsc-utils"
|
||||
"MultiProcessorCompile",
|
||||
}
|
||||
|
||||
configurations { "Debug", "Release", }
|
||||
|
||||
symbols "On"
|
||||
|
||||
configuration "Release"
|
||||
filter "configurations:Release"
|
||||
optimize "Full"
|
||||
defines { "NDEBUG" }
|
||||
configuration{}
|
||||
filter {}
|
||||
|
||||
configuration "Debug"
|
||||
filter "configurations:Debug"
|
||||
optimize "Debug"
|
||||
defines { "DEBUG", "_DEBUG" }
|
||||
configuration {}
|
||||
filter {}
|
||||
|
||||
startproject "iw5-gsc-utils"
|
||||
|
||||
|
@ -49,6 +49,7 @@ namespace command
|
||||
if (i > index) result.append(" ");
|
||||
result.append(this->get(i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -96,7 +97,6 @@ namespace command
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -14,34 +14,6 @@
|
||||
|
||||
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_method> methods;
|
||||
|
||||
@ -49,13 +21,13 @@ namespace gsc
|
||||
{
|
||||
std::string method_name(unsigned int id)
|
||||
{
|
||||
const auto map = *game::plutonium::method_map_rev;
|
||||
const auto& map = *game::plutonium::method_map_rev;
|
||||
|
||||
for (const auto& function : map)
|
||||
{
|
||||
if (function.second == id)
|
||||
{
|
||||
return function.first;
|
||||
return function.first.data();
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,13 +36,13 @@ namespace gsc
|
||||
|
||||
std::string function_name(unsigned int id)
|
||||
{
|
||||
const auto map = *game::plutonium::function_map_rev;
|
||||
const auto& map = *game::plutonium::function_map_rev;
|
||||
|
||||
for (const auto& function : map)
|
||||
{
|
||||
if (function.second == id)
|
||||
{
|
||||
return function.first;
|
||||
return function.first.data();
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,8 +53,6 @@ namespace gsc
|
||||
{
|
||||
std::vector<scripting::script_value> args;
|
||||
|
||||
const auto top = game::scr_VmPub->top;
|
||||
|
||||
for (auto i = 0; i < game::scr_VmPub->outparamcount; i++)
|
||||
{
|
||||
const auto value = game::scr_VmPub->top[-i];
|
||||
@ -104,12 +74,24 @@ namespace gsc
|
||||
|
||||
auto function_map_start = 0x200;
|
||||
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)
|
||||
{
|
||||
if (id < 0x200)
|
||||
{
|
||||
return reinterpret_cast<builtin_function*>(0x1D6EB34)[id]();
|
||||
return reinterpret_cast<builtin_function*>(game::plutonium::function_table.get())[id]();
|
||||
}
|
||||
|
||||
try
|
||||
@ -125,8 +107,8 @@ namespace gsc
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing function %s\n", function_name(id).data());
|
||||
printf("%s\n", e.what());
|
||||
printf("Error executing function %s:\n", function_name(id).data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
}
|
||||
}
|
||||
@ -135,7 +117,7 @@ namespace gsc
|
||||
{
|
||||
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
|
||||
@ -151,8 +133,8 @@ namespace gsc
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing method %s\n", method_name(id).data());
|
||||
printf("%s\n", e.what());
|
||||
printf("Error executing method %s:\n", method_name(id).data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
}
|
||||
}
|
||||
@ -202,60 +184,63 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
|
||||
const char* replaced_pos = 0;
|
||||
|
||||
void get_replaced_pos(const char* pos)
|
||||
utils::hook::detour scr_get_object_field_hook;
|
||||
void scr_get_object_field_stub(unsigned int classnum, int entnum, unsigned int offset)
|
||||
{
|
||||
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
|
||||
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
|
||||
return scr_set_object_field_hook.invoke<void>(classnum, entnum, offset);
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -266,7 +251,8 @@ namespace gsc
|
||||
const auto index = function_map_start++;
|
||||
|
||||
functions[index] = func;
|
||||
(*game::plutonium::function_map_rev)[name] = index;
|
||||
const auto name_view = utils::memory::get_allocator()->duplicate_string(name);
|
||||
(*game::plutonium::function_map_rev)[name_view] = index;
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,43 +263,111 @@ namespace gsc
|
||||
const auto index = method_map_start++;
|
||||
|
||||
methods[index] = func;
|
||||
(*game::plutonium::method_map_rev)[name] = index;
|
||||
const auto name_view = utils::memory::get_allocator()->duplicate_string(name);
|
||||
(*game::plutonium::method_map_rev)[name_view] = index;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
uint16_t token_id{};
|
||||
auto& token_map = *game::plutonium::token_map_rev;
|
||||
if (token_map.find(name) != token_map.end())
|
||||
{
|
||||
token_id = token_map.at(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
token_id = token_map_start++;
|
||||
token_map.insert(std::make_pair(name, token_id));
|
||||
}
|
||||
|
||||
const auto offset = field_offset_start++;
|
||||
custom_fields[classnum][offset] = {name, getter, setter};
|
||||
|
||||
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
|
||||
{
|
||||
public:
|
||||
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, "entityflags",
|
||||
[](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
|
||||
{
|
||||
game::Cbuf_AddText(0, args[0].as<const char*>());
|
||||
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
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
const auto function = args[1].get_raw();
|
||||
|
||||
if (function.type != game::SCRIPT_FUNCTION)
|
||||
{
|
||||
throw std::runtime_error("Invalid type");
|
||||
}
|
||||
|
||||
const auto pos = function.u.codePosValue;
|
||||
command::add_script_command(name, [pos](const command::params& params)
|
||||
const auto function = args[1].as<scripting::function>();
|
||||
command::add_script_command(name, [function](const command::params& params)
|
||||
{
|
||||
scripting::array array;
|
||||
for (auto i = 0; i < params.size(); i++)
|
||||
@ -321,7 +375,7 @@ namespace gsc
|
||||
array.push(params[i]);
|
||||
}
|
||||
|
||||
scripting::exec_ent_thread(*game::levelEntityId, pos, {array.get_raw()});
|
||||
function({array.get_raw()});
|
||||
});
|
||||
|
||||
return {};
|
||||
@ -335,6 +389,20 @@ namespace gsc
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("dropallbots", [](const function_args&) -> scripting::script_value
|
||||
{
|
||||
for (auto i = 0; i < *game::svs_clientCount; i++)
|
||||
{
|
||||
if (game::svs_clients[i].header.state != game::CS_FREE
|
||||
&& game::svs_clients[i].header.netchan.remoteAddress.type == game::NA_BOT)
|
||||
{
|
||||
game::SV_GameDropClient(i, "GAME_GET_TO_COVER");
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("tell", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
@ -343,16 +411,81 @@ namespace gsc
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
const auto message = args[0].as<std::string>();
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
const auto message = args[0].as<std::string>();
|
||||
game::SV_GameSendServerCommand(client, 0, utils::string::va("%c \"%s\"", 84, message.data()));
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
utils::hook::jump(0x56C8EB, call_builtin_stub);
|
||||
utils::hook::jump(0x56CBDC, call_builtin_method_stub);
|
||||
utils::hook::jump(0x56B726, vm_execute_stub);
|
||||
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 client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
const auto toggle = args[0].as<int>();
|
||||
auto flags = game::g_entities[client].client->ps.perks[0];
|
||||
|
||||
game::g_entities[client].client->ps.perks[0] = toggle
|
||||
? flags | 0x4000u : flags & ~0x4000u;
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("isbot", [](const game::scr_entref_t ent, const function_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("Not a player entity");
|
||||
}
|
||||
|
||||
return game::svs_clients[client].bIsTestClient;
|
||||
});
|
||||
|
||||
method::add("arecontrolsfrozen", [](const game::scr_entref_t ent, const function_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("Not a player entity");
|
||||
}
|
||||
|
||||
return {(game::g_entities[client].client->flags & 4) != 0};
|
||||
});
|
||||
|
||||
// let other plugins read the pointers
|
||||
post_load_callbacks.push_back([]()
|
||||
{
|
||||
utils::hook::jump(0x56C8EB, call_builtin_stub);
|
||||
utils::hook::jump(0x56CBDC, call_builtin_method_stub);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,19 +1,28 @@
|
||||
#pragma once
|
||||
#include "game/scripting/array.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
|
||||
namespace gsc
|
||||
{
|
||||
extern std::unordered_map<const char*, const char*> replaced_functions;
|
||||
enum classid
|
||||
{
|
||||
entity,
|
||||
hudelem,
|
||||
pathnode,
|
||||
node,
|
||||
count
|
||||
};
|
||||
|
||||
class function_args
|
||||
{
|
||||
public:
|
||||
function_args(std::vector<scripting::script_value>);
|
||||
|
||||
unsigned int function_args::size() const;
|
||||
std::vector<scripting::script_value> function_args::get_raw() const;
|
||||
scripting::script_value get(const int index) const;
|
||||
unsigned int size() const;
|
||||
std::vector<scripting::script_value> get_raw() 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);
|
||||
}
|
||||
@ -36,4 +45,11 @@ namespace gsc
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
@ -2,12 +2,6 @@
|
||||
#include "loader/component_loader.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 "json.hpp"
|
||||
|
||||
@ -150,7 +144,7 @@ namespace io
|
||||
array.push(file);
|
||||
}
|
||||
|
||||
return array.get_raw();
|
||||
return array;
|
||||
});
|
||||
|
||||
gsc::function::add("copyfolder", [](const gsc::function_args& args)
|
||||
@ -161,6 +155,26 @@ namespace io
|
||||
|
||||
return scripting::script_value{};
|
||||
});
|
||||
|
||||
gsc::function::add("httpget", [](const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto url = args[0].as<std::string>();
|
||||
const auto object = scripting::entity(scripting::make_object());
|
||||
|
||||
scheduler::once([object, url]()
|
||||
{
|
||||
const auto result = utils::http::get_data(url.data());
|
||||
scheduler::once([object, result]()
|
||||
{
|
||||
const auto value = result.has_value()
|
||||
? result.value().substr(0, 0x5000)
|
||||
: "";
|
||||
scripting::notify(object, "done", {value});
|
||||
});
|
||||
}, scheduler::pipeline::async);
|
||||
|
||||
return object;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,12 +2,6 @@
|
||||
#include "loader/component_loader.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 "json.hpp"
|
||||
|
||||
@ -28,18 +22,23 @@ namespace json
|
||||
const auto keys = array.get_keys();
|
||||
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)
|
||||
{
|
||||
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]));
|
||||
}
|
||||
}
|
||||
|
||||
@ -168,7 +167,7 @@ namespace json
|
||||
array[key] = args[i + 1];
|
||||
}
|
||||
|
||||
return array.get_raw();
|
||||
return array;
|
||||
});
|
||||
|
||||
gsc::function::add("jsonparse", [](const gsc::function_args& args)
|
||||
|
@ -1,35 +1,51 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
#include "scheduler.hpp"
|
||||
|
||||
#include "game/scripting/entity.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "scheduler.hpp"
|
||||
#include "gsc.hpp"
|
||||
#include "scripting.hpp"
|
||||
|
||||
namespace notifies
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::vector<scripting::function> say_callbacks;
|
||||
utils::hook::detour client_command_hook;
|
||||
|
||||
void client_command_stub(int clientNum)
|
||||
{
|
||||
char cmd[1024] = { 0 };
|
||||
char cmd[1024] = {0};
|
||||
const auto* entity = &game::g_entities[clientNum];
|
||||
|
||||
if (entity->client == nullptr)
|
||||
{
|
||||
return; // Client is not fully in game yet
|
||||
}
|
||||
|
||||
game::SV_Cmd_ArgvBuffer(0, cmd, 1024);
|
||||
|
||||
auto hidden = false;
|
||||
if (cmd == "say"s || cmd == "say_team"s)
|
||||
{
|
||||
std::string message = game::ConcatArgs(1);
|
||||
message.erase(0, 1);
|
||||
|
||||
const scripting::entity level{*game::levelEntityId};
|
||||
const auto player = scripting::call("getEntByNum", {clientNum}).as<scripting::entity>();
|
||||
for (const auto& callback : say_callbacks)
|
||||
{
|
||||
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});
|
||||
scripting::notify(player, cmd, {message});
|
||||
if (result.is<int>() && !hidden)
|
||||
{
|
||||
hidden = result.as<int>() == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return client_command_hook.invoke<void>(clientNum);
|
||||
if (!hidden)
|
||||
{
|
||||
client_command_hook.invoke<void>(clientNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,6 +55,18 @@ namespace notifies
|
||||
void post_unpack() override
|
||||
{
|
||||
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 {};
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,73 +1,128 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
#include "scheduler.hpp"
|
||||
#include "game/game.hpp"
|
||||
|
||||
namespace scheduler
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::queue<std::function<void()>> tasks;
|
||||
|
||||
struct task
|
||||
{
|
||||
std::function<bool()> handler;
|
||||
std::function<bool()> handler{};
|
||||
std::chrono::milliseconds interval{};
|
||||
std::chrono::high_resolution_clock::time_point last_call{};
|
||||
};
|
||||
|
||||
utils::concurrent_list<task> callbacks;
|
||||
using task_list = std::vector<task>;
|
||||
|
||||
void execute()
|
||||
class task_pipeline
|
||||
{
|
||||
for (auto callback : callbacks)
|
||||
public:
|
||||
void add(task&& task)
|
||||
{
|
||||
const auto now = std::chrono::high_resolution_clock::now();
|
||||
const auto diff = now - callback->last_call;
|
||||
|
||||
if (diff < callback->interval) continue;
|
||||
|
||||
callback->last_call = now;
|
||||
|
||||
const auto res = callback->handler();
|
||||
if (res)
|
||||
new_callbacks_.access([&task](task_list& tasks)
|
||||
{
|
||||
callbacks.remove(callback);
|
||||
}
|
||||
tasks.emplace_back(std::move(task));
|
||||
});
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
callbacks_.access([&](task_list& tasks)
|
||||
{
|
||||
this->merge_callbacks();
|
||||
|
||||
for (auto i = tasks.begin(); i != tasks.end();)
|
||||
{
|
||||
const auto now = std::chrono::high_resolution_clock::now();
|
||||
const auto diff = now - i->last_call;
|
||||
|
||||
if (diff < i->interval)
|
||||
{
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
i->last_call = now;
|
||||
|
||||
const auto res = i->handler();
|
||||
if (res == cond_end)
|
||||
{
|
||||
i = tasks.erase(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
++i;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
utils::concurrency::container<task_list> new_callbacks_;
|
||||
utils::concurrency::container<task_list, std::recursive_mutex> callbacks_;
|
||||
|
||||
void merge_callbacks()
|
||||
{
|
||||
callbacks_.access([&](task_list& tasks)
|
||||
{
|
||||
new_callbacks_.access([&](task_list& new_tasks)
|
||||
{
|
||||
tasks.insert(tasks.end(), std::move_iterator<task_list::iterator>(new_tasks.begin()), std::move_iterator<task_list::iterator>(new_tasks.end()));
|
||||
new_tasks = {};
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
std::thread thread;
|
||||
task_pipeline pipelines[pipeline::count];
|
||||
|
||||
void execute(const pipeline type)
|
||||
{
|
||||
assert(type >= 0 && type < pipeline::count);
|
||||
pipelines[type].execute();
|
||||
}
|
||||
|
||||
void server_frame()
|
||||
void server_frame_stub()
|
||||
{
|
||||
execute();
|
||||
reinterpret_cast<void (*)()>(0x50C1E0)();
|
||||
execute(pipeline::server);
|
||||
}
|
||||
}
|
||||
|
||||
void schedule(const std::function<bool()>& callback, const std::chrono::milliseconds delay)
|
||||
void schedule(const std::function<bool()>& callback, const pipeline type,
|
||||
const std::chrono::milliseconds delay)
|
||||
{
|
||||
assert(type >= 0 && type < pipeline::count);
|
||||
|
||||
task task;
|
||||
task.handler = callback;
|
||||
task.interval = delay;
|
||||
task.last_call = std::chrono::high_resolution_clock::now();
|
||||
|
||||
callbacks.add(task);
|
||||
pipelines[type].add(std::move(task));
|
||||
}
|
||||
|
||||
void loop(const std::function<void()>& callback, const std::chrono::milliseconds delay)
|
||||
void loop(const std::function<void()>& callback, const pipeline type,
|
||||
const std::chrono::milliseconds delay)
|
||||
{
|
||||
schedule([callback]()
|
||||
{
|
||||
callback();
|
||||
return false;
|
||||
}, delay);
|
||||
return cond_continue;
|
||||
}, type, delay);
|
||||
}
|
||||
|
||||
void once(const std::function<void()>& callback, const std::chrono::milliseconds delay)
|
||||
void once(const std::function<void()>& callback, const pipeline type,
|
||||
const std::chrono::milliseconds delay)
|
||||
{
|
||||
schedule([callback]()
|
||||
{
|
||||
callback();
|
||||
return true;
|
||||
}, delay);
|
||||
return cond_end;
|
||||
}, type, delay);
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
@ -75,7 +130,16 @@ namespace scheduler
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
utils::hook::call(0x50CEDC, server_frame);
|
||||
thread = std::thread([]()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
execute(pipeline::async);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
}
|
||||
});
|
||||
|
||||
utils::hook::call(0x50CEDC, server_frame_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,7 +2,20 @@
|
||||
|
||||
namespace scheduler
|
||||
{
|
||||
void schedule(const std::function<bool()>& callback, std::chrono::milliseconds delay = 0ms);
|
||||
void loop(const std::function<void()>& callback, std::chrono::milliseconds delay = 0ms);
|
||||
void once(const std::function<void()>& callback, std::chrono::milliseconds delay = 0ms);
|
||||
enum pipeline
|
||||
{
|
||||
server,
|
||||
async,
|
||||
count,
|
||||
};
|
||||
|
||||
static const bool cond_continue = false;
|
||||
static const bool cond_end = true;
|
||||
|
||||
void schedule(const std::function<bool()>& callback, pipeline type = pipeline::server,
|
||||
std::chrono::milliseconds delay = 0ms);
|
||||
void loop(const std::function<void()>& callback, pipeline type = pipeline::server,
|
||||
std::chrono::milliseconds delay = 0ms);
|
||||
void once(const std::function<void()>& callback, pipeline type = pipeline::server,
|
||||
std::chrono::milliseconds delay = 0ms);
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ namespace scripting
|
||||
|
||||
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,
|
||||
game::VariableValue* top)
|
||||
{
|
||||
@ -77,7 +79,12 @@ namespace scripting
|
||||
{
|
||||
userinfo::clear_overrides();
|
||||
command::clear_script_commands();
|
||||
gsc::replaced_functions.clear();
|
||||
|
||||
for (const auto& callback : shutdown_callbacks)
|
||||
{
|
||||
callback();
|
||||
}
|
||||
|
||||
g_shutdown_game_hook.invoke<void>(free_scripts);
|
||||
}
|
||||
|
||||
@ -88,7 +95,7 @@ namespace scripting
|
||||
const auto file_id = atoi(filename);
|
||||
if (file_id)
|
||||
{
|
||||
current_file = scripting::file_list[file_id];
|
||||
current_file = scripting::find_file(file_id);
|
||||
}
|
||||
|
||||
process_script_hook.invoke<void>(filename);
|
||||
@ -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
|
||||
{
|
||||
public:
|
||||
|
@ -4,4 +4,6 @@ namespace scripting
|
||||
{
|
||||
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;
|
||||
|
||||
void on_shutdown(const std::function<void()>& callback);
|
||||
}
|
98
src/component/signatures.cpp
Normal file
98
src/component/signatures.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "signatures.hpp"
|
||||
#include <utils/hook.hpp>
|
||||
|
||||
namespace signatures
|
||||
{
|
||||
size_t load_image_size()
|
||||
{
|
||||
MODULEINFO info{};
|
||||
GetModuleInformation(GetCurrentProcess(),
|
||||
GetModuleHandle("plutonium-bootstrapper-win32.exe"), &info, sizeof(MODULEINFO));
|
||||
return info.SizeOfImage;
|
||||
}
|
||||
|
||||
size_t get_image_size()
|
||||
{
|
||||
static const auto image_size = load_image_size();
|
||||
return image_size;
|
||||
}
|
||||
|
||||
void load_function_tables()
|
||||
{
|
||||
static const auto ptr = *reinterpret_cast<size_t*>(0x56CBDC + 0x1) + 0x56CBDC + 0x5;
|
||||
static const auto function_table = *reinterpret_cast<size_t*>(0x56C8EB + 0x3);
|
||||
static const auto method_table = *reinterpret_cast<size_t*>(ptr + 0xA);
|
||||
|
||||
game::plutonium::function_table.set(function_table);
|
||||
game::plutonium::method_table.set(method_table);
|
||||
}
|
||||
|
||||
size_t find_string_ptr(const std::string& string)
|
||||
{
|
||||
const char* string_ptr = nullptr;
|
||||
std::string mask(string.size(), 'x');
|
||||
const auto base = reinterpret_cast<size_t>(GetModuleHandle("plutonium-bootstrapper-win32.exe"));
|
||||
utils::hook::signature signature(base, get_image_size() - base);
|
||||
|
||||
signature.add({
|
||||
string,
|
||||
mask,
|
||||
[&](char* address)
|
||||
{
|
||||
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};
|
||||
const auto string_ptr = find_string_ptr(string);
|
||||
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'!");
|
||||
if (!string_ref)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
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::token_map_rev.set(map_ptr + 0xBC);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool process_printf()
|
||||
{
|
||||
const auto string_ref = find_string_ref("A critical exception occured!\n");
|
||||
if (!string_ref)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto offset = *reinterpret_cast<size_t*>(string_ref + 5);
|
||||
game::plutonium::printf.set(string_ref + 4 + 5 + offset);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool process()
|
||||
{
|
||||
load_function_tables();
|
||||
return process_printf() && process_maps();
|
||||
}
|
||||
}
|
6
src/component/signatures.hpp
Normal file
6
src/component/signatures.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace signatures
|
||||
{
|
||||
bool process();
|
||||
}
|
46
src/component/string.cpp
Normal file
46
src/component/string.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace string
|
||||
{
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
gsc::function::add("toupper", [](const gsc::function_args& args)
|
||||
{
|
||||
return utils::string::to_upper(args[0].as<std::string>());
|
||||
});
|
||||
|
||||
gsc::function::add("getchar", [](const gsc::function_args& args)
|
||||
{
|
||||
auto index = 0;
|
||||
if (args.size() > 1)
|
||||
{
|
||||
index = args[1].as<int>();
|
||||
}
|
||||
|
||||
const auto string = args[0].as<std::string>();
|
||||
if (index >= static_cast<int>(string.size()))
|
||||
{
|
||||
throw std::runtime_error("Char index out of bounds");
|
||||
}
|
||||
|
||||
return static_cast<int>(string[index]);
|
||||
});
|
||||
|
||||
gsc::function::add("chartostring", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto char_ = static_cast<char>(args[0].as<int>());
|
||||
return std::string(1, char_);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(string::component)
|
@ -2,12 +2,6 @@
|
||||
#include "loader/component_loader.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"
|
||||
|
||||
namespace userinfo
|
||||
@ -19,12 +13,17 @@ namespace userinfo
|
||||
{
|
||||
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;
|
||||
const auto args = utils::string::split(userinfo, '\\');
|
||||
userinfo_map map{};
|
||||
|
||||
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];
|
||||
}
|
||||
@ -34,7 +33,7 @@ namespace userinfo
|
||||
|
||||
std::string map_to_userinfo(const userinfo_map& map)
|
||||
{
|
||||
std::string buffer;
|
||||
std::string buffer{};
|
||||
|
||||
for (const auto& value : map)
|
||||
{
|
||||
@ -49,9 +48,8 @@ namespace userinfo
|
||||
|
||||
void sv_getuserinfo_stub(int index, char* buffer, int bufferSize)
|
||||
{
|
||||
char _buffer[1024];
|
||||
sv_getuserinfo_hook.invoke<void>(index, _buffer, 1024);
|
||||
auto map = userinfo_to_map(_buffer);
|
||||
sv_getuserinfo_hook.invoke<void>(index, buffer, bufferSize);
|
||||
auto map = userinfo_to_map(buffer);
|
||||
|
||||
if (userinfo_overrides.find(index) == userinfo_overrides.end())
|
||||
{
|
||||
@ -94,28 +92,36 @@ namespace userinfo
|
||||
|
||||
gsc::method::add("setname", [](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)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
userinfo_overrides[ent.entnum]["name"] = name;
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
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&) -> scripting::script_value
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("name");
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
@ -124,13 +130,18 @@ namespace userinfo
|
||||
|
||||
gsc::method::add("setclantag", [](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)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = name;
|
||||
userinfo_overrides[ent.entnum]["ec_TagText"] = name;
|
||||
userinfo_overrides[ent.entnum]["ec_usingTag"] = "1";
|
||||
@ -139,15 +150,18 @@ namespace userinfo
|
||||
return {};
|
||||
});
|
||||
|
||||
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&) -> scripting::script_value
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("clantag");
|
||||
userinfo_overrides[ent.entnum].erase("ec_TagText");
|
||||
userinfo_overrides[ent.entnum].erase("ec_usingTag");
|
||||
@ -156,13 +170,18 @@ namespace userinfo
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args&) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = "";
|
||||
userinfo_overrides[ent.entnum]["ec_TagText"] = "";
|
||||
userinfo_overrides[ent.entnum]["ec_usingTag"] = "0";
|
||||
|
@ -1,10 +1,27 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
||||
#include "component/signatures.hpp"
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
|
||||
{
|
||||
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
if (!signatures::process())
|
||||
{
|
||||
MessageBoxA(NULL,
|
||||
"This version of iw5-gsc-utils is outdated.\n" \
|
||||
"Download the latest dll from here: https://github.com/fedddddd/iw5-gsc-utils/releases",
|
||||
"ERROR", MB_ICONERROR);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (game::plutonium::printf.get() != nullptr)
|
||||
{
|
||||
utils::hook::jump(reinterpret_cast<uintptr_t>(&printf), game::plutonium::printf);
|
||||
}
|
||||
|
||||
component_loader::post_unpack();
|
||||
}
|
||||
|
||||
|
@ -2,5 +2,11 @@
|
||||
|
||||
namespace game
|
||||
{
|
||||
|
||||
void SV_GameDropClient(int clientNum, const char* reason)
|
||||
{
|
||||
if (clientNum >= 0 && clientNum < sv_maxclients->current.integer)
|
||||
{
|
||||
SV_DropClient(&svs_clients[clientNum], reason, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,11 @@ namespace game
|
||||
return dedi_;
|
||||
}
|
||||
|
||||
void set(const size_t dedi)
|
||||
{
|
||||
this->dedi_ = reinterpret_cast<T*>(dedi);
|
||||
}
|
||||
|
||||
operator T* () const
|
||||
{
|
||||
return this->get();
|
||||
@ -29,6 +34,8 @@ namespace game
|
||||
private:
|
||||
T* dedi_;
|
||||
};
|
||||
|
||||
void SV_GameDropClient(int clientNum, const char* reason);
|
||||
}
|
||||
|
||||
#include "symbols.hpp"
|
@ -1,5 +1,6 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "array.hpp"
|
||||
#include "script_value.hpp"
|
||||
#include "execution.hpp"
|
||||
|
||||
namespace scripting
|
||||
@ -40,9 +41,20 @@ namespace scripting
|
||||
this->value_ = value;
|
||||
}
|
||||
|
||||
array::array(unsigned int id)
|
||||
array::array(const unsigned int id)
|
||||
: id_(id)
|
||||
{
|
||||
this->add();
|
||||
}
|
||||
|
||||
array::array(const array& other) : array(other.id_)
|
||||
{
|
||||
}
|
||||
|
||||
array::array(array&& other) noexcept
|
||||
{
|
||||
this->id_ = other.id_;
|
||||
other.id_ = 0;
|
||||
}
|
||||
|
||||
array::array()
|
||||
@ -70,9 +82,54 @@ namespace scripting
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<array_key> array::get_keys() const
|
||||
array::~array()
|
||||
{
|
||||
std::vector<array_key> result;
|
||||
this->release();
|
||||
}
|
||||
|
||||
array& array::operator=(const array& other)
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
this->release();
|
||||
this->id_ = other.id_;
|
||||
this->add();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
array& array::operator=(array&& other) noexcept
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
this->release();
|
||||
this->id_ = other.id_;
|
||||
other.id_ = 0;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void array::add() const
|
||||
{
|
||||
if (this->id_)
|
||||
{
|
||||
game::AddRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->id_)});
|
||||
}
|
||||
}
|
||||
|
||||
void array::release() const
|
||||
{
|
||||
if (this->id_)
|
||||
{
|
||||
game::RemoveRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->id_)});
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<script_value> array::get_keys() const
|
||||
{
|
||||
std::vector<script_value> result;
|
||||
|
||||
const auto offset = 0xC800 * (this->id_ & 1);
|
||||
auto current = game::scr_VarGlob->objectVariableChildren[this->id_].firstChild;
|
||||
@ -90,16 +147,14 @@ namespace scripting
|
||||
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);
|
||||
|
||||
array_key key;
|
||||
script_value key;
|
||||
if (string_value < 0x40000 && str)
|
||||
{
|
||||
key.is_string = true;
|
||||
key.key = str;
|
||||
key = str;
|
||||
}
|
||||
else
|
||||
{
|
||||
key.is_integer = true;
|
||||
key.index = (string_value - 0x800000) & 0xFFFFFF;
|
||||
key = (string_value - 0x800000) & 0xFFFFFF;
|
||||
}
|
||||
|
||||
result.push_back(key);
|
||||
@ -147,10 +202,24 @@ namespace scripting
|
||||
return value;
|
||||
}
|
||||
|
||||
script_value array::get(const script_value& key) const
|
||||
{
|
||||
if (key.is<int>())
|
||||
{
|
||||
return this->get(key.as<int>());
|
||||
}
|
||||
else
|
||||
{
|
||||
return this->get(key.as<std::string>());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
script_value array::get(const std::string& key) const
|
||||
{
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto variable_id = game::GetVariable(this->id_, string_value);
|
||||
const auto variable_id = game::FindVariable(this->id_, string_value);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
@ -167,7 +236,7 @@ namespace scripting
|
||||
|
||||
script_value array::get(const unsigned int index) const
|
||||
{
|
||||
const auto variable_id = game::GetVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
@ -182,11 +251,21 @@ namespace scripting
|
||||
return variable;
|
||||
}
|
||||
|
||||
void array::set(const std::string& key, const script_value& _value) const
|
||||
void array::set(const script_value& key, const script_value& value) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
if (key.is<int>())
|
||||
{
|
||||
this->set(key.as<int>(), value);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->set(key.as<std::string>(), value);
|
||||
}
|
||||
}
|
||||
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
void array::set(const std::string& key, const script_value& value_) const
|
||||
{
|
||||
const auto value = value_.get_raw();
|
||||
const auto variable_id = this->get_value_id(key);
|
||||
|
||||
if (!variable_id)
|
||||
@ -203,9 +282,9 @@ namespace scripting
|
||||
variable->u.u = value.u;
|
||||
}
|
||||
|
||||
void array::set(const unsigned int index, const script_value& _value) const
|
||||
void array::set(const unsigned int index, const script_value& value_) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
const auto value = value_.get_raw();
|
||||
const auto variable_id = this->get_value_id(index);
|
||||
|
||||
if (!variable_id)
|
||||
|
@ -1,21 +1,14 @@
|
||||
#pragma once
|
||||
#include "game/game.hpp"
|
||||
#include "script_value.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
struct array_key
|
||||
{
|
||||
bool is_string = false;
|
||||
bool is_integer = false;
|
||||
int index{};
|
||||
std::string key{};
|
||||
};
|
||||
|
||||
class array_value : public script_value
|
||||
{
|
||||
public:
|
||||
array_value(unsigned int parent_id, unsigned int id);
|
||||
void array_value::operator=(const script_value& value);
|
||||
array_value(unsigned int, unsigned int);
|
||||
void operator=(const script_value&);
|
||||
private:
|
||||
unsigned int id_;
|
||||
unsigned int parent_id_;
|
||||
@ -25,21 +18,32 @@ namespace scripting
|
||||
{
|
||||
public:
|
||||
array();
|
||||
array(unsigned int);
|
||||
array(const unsigned int);
|
||||
|
||||
array(std::vector<script_value>);
|
||||
array(std::unordered_map<std::string, script_value>);
|
||||
|
||||
std::vector<array_key> get_keys() const;
|
||||
array(const array& other);
|
||||
array(array&& other) noexcept;
|
||||
|
||||
~array();
|
||||
|
||||
array& operator=(const array& other);
|
||||
array& operator=(array&& other) noexcept;
|
||||
|
||||
std::vector<script_value> get_keys() const;
|
||||
unsigned int size() const;
|
||||
|
||||
unsigned int push(script_value) const;
|
||||
void erase(const unsigned int index) const;
|
||||
void erase(const std::string& key) const;
|
||||
void erase(const unsigned int) const;
|
||||
void erase(const std::string&) const;
|
||||
script_value pop() const;
|
||||
|
||||
script_value get(const script_value&) const;
|
||||
script_value get(const std::string&) const;
|
||||
script_value get(const unsigned int) const;
|
||||
|
||||
void set(const script_value&, const script_value&) const;
|
||||
void set(const std::string&, const script_value&) const;
|
||||
void set(const unsigned int, const script_value&) const;
|
||||
|
||||
@ -50,16 +54,34 @@ namespace scripting
|
||||
|
||||
entity get_raw() const;
|
||||
|
||||
array_value array::operator[](const int index) const
|
||||
array_value operator[](const int index) const
|
||||
{
|
||||
return {this->id_, this->get_value_id(index)};
|
||||
}
|
||||
|
||||
array_value array::operator[](const std::string& key) const
|
||||
array_value operator[](const std::string& key) const
|
||||
{
|
||||
return {this->id_, this->get_value_id(key)};
|
||||
}
|
||||
|
||||
template <typename I = int, typename S = std::string>
|
||||
array_value operator[](const script_value& key) const
|
||||
{
|
||||
if (key.is<I>())
|
||||
{
|
||||
return { this->id_, this->get_value_id(key.as<I>()) };
|
||||
}
|
||||
|
||||
if (key.is<S>())
|
||||
{
|
||||
return { this->id_, this->get_value_id(key.as<S>()) };
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void add() const;
|
||||
void release() const;
|
||||
|
||||
unsigned int id_;
|
||||
};
|
||||
}
|
||||
|
@ -269,8 +269,6 @@ namespace scripting
|
||||
{
|
||||
return get_custom_field(entity, field);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
unsigned int make_array()
|
||||
@ -283,4 +281,14 @@ namespace scripting
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
unsigned int make_object()
|
||||
{
|
||||
unsigned int index = 0;
|
||||
const auto variable = game::AllocVariable(&index);
|
||||
variable->w.type = game::SCRIPT_STRUCT;
|
||||
variable->u.f.prev = 0;
|
||||
|
||||
return index;
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
#include "game/game.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "array.hpp"
|
||||
#include "function.hpp"
|
||||
#include "script_value.hpp"
|
||||
|
||||
namespace scripting
|
||||
@ -36,4 +38,5 @@ namespace scripting
|
||||
void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);
|
||||
|
||||
unsigned int make_array();
|
||||
unsigned int make_object();
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace scripting
|
||||
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);
|
||||
}
|
||||
|
@ -12,9 +12,9 @@ namespace scripting
|
||||
script_value get_raw() 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);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -7,27 +7,27 @@ namespace scripting
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::unordered_map<std::string, unsigned> lowercase_map(
|
||||
const std::unordered_map<std::string, unsigned>& old_map)
|
||||
std::unordered_map<std::string, uint16_t> lowercase_map(
|
||||
const std::unordered_map<std::string_view, uint16_t>& old_map)
|
||||
{
|
||||
std::unordered_map<std::string, unsigned> new_map{};
|
||||
std::unordered_map<std::string, uint16_t> new_map{};
|
||||
for (auto& entry : old_map)
|
||||
{
|
||||
new_map[utils::string::to_lower(entry.first)] = entry.second;
|
||||
new_map[utils::string::to_lower(entry.first.data())] = entry.second;
|
||||
}
|
||||
|
||||
return new_map;
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, unsigned>& get_methods()
|
||||
const std::unordered_map<std::string, uint16_t>& get_methods()
|
||||
{
|
||||
static auto methods = lowercase_map(method_map);
|
||||
static auto methods = lowercase_map(*game::plutonium::method_map_rev);
|
||||
return methods;
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, unsigned>& get_functions()
|
||||
const std::unordered_map<std::string, uint16_t>& get_functions()
|
||||
{
|
||||
static auto function = lowercase_map(function_map);
|
||||
static auto function = lowercase_map(*game::plutonium::function_map_rev);
|
||||
return function;
|
||||
}
|
||||
|
||||
@ -59,20 +59,21 @@ namespace scripting
|
||||
|
||||
script_function get_function_by_index(const unsigned index)
|
||||
{
|
||||
static const auto function_table = 0x1D6EB34;
|
||||
static const auto method_table = 0x1D4F258;
|
||||
static const auto function_table = game::plutonium::function_table.get();
|
||||
static const auto method_table = game::plutonium::method_table.get();
|
||||
|
||||
if (index < 0x1C7)
|
||||
{
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
std::string find_token(unsigned int id)
|
||||
{
|
||||
const auto& token_map = *game::plutonium::token_map_rev;
|
||||
for (const auto& token : token_map)
|
||||
{
|
||||
if (token.second == id)
|
||||
@ -84,8 +85,14 @@ namespace scripting
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string find_file(unsigned int id)
|
||||
{
|
||||
return find_token(id);
|
||||
}
|
||||
|
||||
int find_token_id(const std::string& name)
|
||||
{
|
||||
const auto& token_map = *game::plutonium::token_map_rev;
|
||||
const auto result = token_map.find(name);
|
||||
|
||||
if (result != token_map.end())
|
||||
|
@ -3,14 +3,10 @@
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
extern std::unordered_map<std::string, unsigned> method_map;
|
||||
extern std::unordered_map<std::string, unsigned> function_map;
|
||||
extern std::unordered_map<std::string, unsigned> token_map;
|
||||
extern std::unordered_map<unsigned, std::string> file_list;
|
||||
|
||||
using script_function = void(*)(game::scr_entref_t);
|
||||
|
||||
script_function find_function(const std::string& name, const bool prefer_global);
|
||||
int find_token_id(const std::string& name);
|
||||
std::string find_token(unsigned int id);
|
||||
std::string find_file(unsigned int id);
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ namespace scripting::safe_execution
|
||||
bool call(const script_function function, const game::scr_entref_t& entref)
|
||||
{
|
||||
*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;
|
||||
return false;
|
||||
@ -39,7 +39,7 @@ namespace scripting::safe_execution
|
||||
bool set_entity_field(const game::scr_entref_t& entref, const int offset)
|
||||
{
|
||||
*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;
|
||||
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)
|
||||
{
|
||||
*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->u.intValue = 0;
|
||||
|
@ -15,6 +15,11 @@ namespace scripting
|
||||
{
|
||||
}
|
||||
|
||||
script_value::script_value(const value_wrap& value)
|
||||
: value_(value.get_raw())
|
||||
{
|
||||
}
|
||||
|
||||
script_value::script_value(void* value)
|
||||
{
|
||||
game::VariableValue variable{};
|
||||
@ -103,6 +108,24 @@ namespace scripting
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
script_value::script_value(const array& value)
|
||||
{
|
||||
game::VariableValue variable{};
|
||||
variable.type = game::SCRIPT_OBJECT;
|
||||
variable.u.pointerValue = value.get_entity_id();
|
||||
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
script_value::script_value(const function& value)
|
||||
{
|
||||
game::VariableValue variable{};
|
||||
variable.type = game::SCRIPT_OBJECT;
|
||||
variable.u.codePosValue = value.get_pos();
|
||||
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
* Integer
|
||||
**************************************************************/
|
||||
@ -297,4 +320,10 @@ namespace scripting
|
||||
{
|
||||
return this->value_.get();
|
||||
}
|
||||
|
||||
value_wrap::value_wrap(const scripting::script_value& value, int argument_index)
|
||||
: value_(value)
|
||||
, argument_index_(argument_index)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,102 @@
|
||||
namespace scripting
|
||||
{
|
||||
class entity;
|
||||
class array;
|
||||
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
|
||||
{
|
||||
public:
|
||||
script_value() = default;
|
||||
script_value(const game::VariableValue& value);
|
||||
script_value(const value_wrap& value);
|
||||
|
||||
script_value(void* value);
|
||||
|
||||
@ -29,6 +119,10 @@ namespace scripting
|
||||
|
||||
script_value(const vector& value);
|
||||
|
||||
script_value(const array& value);
|
||||
|
||||
script_value(const function& value);
|
||||
|
||||
template <typename T>
|
||||
bool is() const;
|
||||
|
||||
@ -37,25 +131,23 @@ namespace scripting
|
||||
{
|
||||
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>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename T, typename I = int>
|
||||
T* as_ptr()
|
||||
{
|
||||
if (!this->is<int>())
|
||||
{
|
||||
throw std::runtime_error("Invalid type");
|
||||
}
|
||||
|
||||
const auto value = this->get<int>();
|
||||
const auto value = this->as<I>();
|
||||
|
||||
if (!value)
|
||||
{
|
||||
throw std::runtime_error("Null pointer");
|
||||
throw std::runtime_error("is null");
|
||||
}
|
||||
|
||||
return reinterpret_cast<T*>(value);
|
||||
@ -68,4 +160,50 @@ namespace scripting
|
||||
template <typename T>
|
||||
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_;
|
||||
};
|
||||
}
|
||||
|
@ -26,20 +26,35 @@ namespace game
|
||||
const char** argv[8];
|
||||
};
|
||||
|
||||
enum netsrc_t
|
||||
{
|
||||
NS_CLIENT1 = 0x0,
|
||||
NS_CLIENT2 = 0x1,
|
||||
NS_CLIENT3 = 0x2,
|
||||
NS_CLIENT4 = 0x3,
|
||||
NS_MAXCLIENTS = 0x4,
|
||||
NS_SERVER = 0x4,
|
||||
NS_PACKET = 0x5,
|
||||
NS_INVALID_NETSRC = 0x6
|
||||
};
|
||||
|
||||
struct msg_t
|
||||
{
|
||||
int overflowed;
|
||||
int readOnly;
|
||||
char* data;
|
||||
char* splitData;
|
||||
unsigned char* data;
|
||||
unsigned char* splitData;
|
||||
int maxsize;
|
||||
int cursize;
|
||||
int splitSize;
|
||||
int readcount;
|
||||
int bit;
|
||||
int lastEntityRef;
|
||||
netsrc_t targetLocalNetID;
|
||||
};
|
||||
|
||||
static_assert(sizeof(msg_t) == 0x2C);
|
||||
|
||||
struct XZoneInfo
|
||||
{
|
||||
const char* name;
|
||||
@ -303,8 +318,112 @@ namespace game
|
||||
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
|
||||
{
|
||||
int entnum;
|
||||
char __pad0[0x154];
|
||||
gclient_s* client;
|
||||
char __pad1[0x28];
|
||||
int flags;
|
||||
char __pad2[0xEC];
|
||||
};
|
||||
|
||||
enum netadrtype_t
|
||||
{
|
||||
NA_BOT = 0x0,
|
||||
NA_BAD = 0x1,
|
||||
NA_LOOPBACK = 0x2,
|
||||
NA_BROADCAST = 0x3,
|
||||
NA_IP = 0x4
|
||||
};
|
||||
|
||||
struct netadr_s
|
||||
{
|
||||
netadrtype_t type;
|
||||
unsigned char ip[4];
|
||||
unsigned __int16 port;
|
||||
unsigned char ipx[10];
|
||||
unsigned int addrHandleIndex;
|
||||
};
|
||||
|
||||
static_assert(sizeof(netadr_s) == 24);
|
||||
|
||||
struct netchan_t
|
||||
{
|
||||
int outgoingSequence;
|
||||
netsrc_t sock;
|
||||
int dropped;
|
||||
int incomingSequence;
|
||||
netadr_s remoteAddress;
|
||||
int qport;
|
||||
int fragmentSequence;
|
||||
int fragmentLength;
|
||||
unsigned char* fragmentBuffer;
|
||||
int fragmentBufferSize;
|
||||
int unsentFragments;
|
||||
int unsentFragmentStart;
|
||||
int unsentLength;
|
||||
unsigned char* unsentBuffer;
|
||||
int unsentBufferSize;
|
||||
unsigned char __pad0[0x5E0];
|
||||
};
|
||||
|
||||
static_assert(sizeof(netchan_t) == 0x630);
|
||||
|
||||
enum clientState_t
|
||||
{
|
||||
CS_FREE = 0,
|
||||
CS_ZOMBIE = 1,
|
||||
CS_RECONNECTING = 2,
|
||||
CS_CONNECTED = 3,
|
||||
CS_CLIENTLOADING = 4,
|
||||
CS_ACTIVE = 5
|
||||
};
|
||||
|
||||
struct clientHeader_t
|
||||
{
|
||||
clientState_t state; // 0
|
||||
int sendAsActive; // 4
|
||||
int deltaMessage; // 8
|
||||
int rateDealyed; // 12
|
||||
int hasAckedBaselineData; // 16
|
||||
int hugeSnapshotSent; // 20
|
||||
netchan_t netchan; // 24
|
||||
vec3_t predictedOrigin;
|
||||
int predictedOriginServerTime;
|
||||
int migrationState;
|
||||
vec3_t predictedVehicleOrigin;
|
||||
int predictedVehicleServerTime;
|
||||
};
|
||||
|
||||
static_assert(sizeof(clientHeader_t) == 0x66C);
|
||||
|
||||
struct client_s
|
||||
{
|
||||
clientHeader_t header;
|
||||
const char* dropReason;
|
||||
char userinfo[1024];
|
||||
char __pad0[0x41242];
|
||||
unsigned __int16 scriptId; // 269490
|
||||
int bIsTestClient; // 269492
|
||||
int serverId; // 269496
|
||||
char __pad1[0x369DC];
|
||||
};
|
||||
|
||||
static_assert(sizeof(client_s) == 0x78698);
|
||||
}
|
@ -49,18 +49,21 @@ namespace game
|
||||
WEAK symbol<unsigned int(unsigned int threadId)> Scr_GetSelf{0x5655E0};
|
||||
WEAK symbol<void()> Scr_MakeArray{0x56ADE0};
|
||||
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)> SL_GetCanonicalString{0x5619A0};
|
||||
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 arg, char* buffer, int bufferLength)> SV_Cmd_ArgvBuffer{0x5459F0};
|
||||
WEAK symbol<void(client_s* drop, const char* reason, bool tellThem)> SV_DropClient{0x570980};
|
||||
|
||||
WEAK symbol<void(unsigned int notifyListOwnerId, unsigned int stringValue, VariableValue* top)> VM_Notify{0x569720};
|
||||
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<int(jmp_buf* Buf)> _setjmp{0x734CF8};
|
||||
WEAK symbol<int(jmp_buf* Buf, int a2, int a3, int a4)> _setjmp{0x734CF8};
|
||||
|
||||
// Variables
|
||||
|
||||
@ -74,13 +77,20 @@ namespace game
|
||||
|
||||
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<dvar_t> sv_maxclients{0x1BA0E4C};
|
||||
WEAK symbol<int> svs_clientCount{0x4B5CF8C};
|
||||
WEAK symbol<client_s> svs_clients{0x4B5CF90};
|
||||
|
||||
namespace plutonium
|
||||
{
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> function_map_rev{0x20589F68};
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0x20589F88};
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> token_map_rev{0x20589FC8};
|
||||
WEAK symbol<std::unordered_map<std::string_view, std::uint16_t>> function_map_rev{0};
|
||||
WEAK symbol<std::unordered_map<std::string_view, std::uint16_t>> method_map_rev{0};
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> token_map_rev{0};
|
||||
WEAK symbol<int(const char* fmt, ...)> printf{0};
|
||||
WEAK symbol<void*> function_table{0};
|
||||
WEAK symbol<void*> method_table{0};
|
||||
}
|
||||
}
|
@ -2,8 +2,10 @@
|
||||
|
||||
#pragma warning(disable: 4018)
|
||||
#pragma warning(disable: 4146)
|
||||
#pragma warning(disable: 4129)
|
||||
#pragma warning(disable: 4244)
|
||||
#pragma warning(disable: 4267)
|
||||
#pragma warning(disable: 4996)
|
||||
#pragma warning(disable: 26812)
|
||||
|
||||
#define DLL_EXPORT extern "C" __declspec(dllexport)
|
||||
@ -25,6 +27,10 @@
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <csetjmp>
|
||||
#include <atlcomcli.h>
|
||||
#include <Psapi.h>
|
||||
|
||||
#pragma comment(lib, "urlmon.lib")
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
@ -36,6 +42,8 @@ using namespace std::literals;
|
||||
#include "utils/hook.hpp"
|
||||
#include "utils/concurrent_list.hpp"
|
||||
#include "utils/io.hpp"
|
||||
#include "utils/concurrency.hpp"
|
||||
#include "utils/http.hpp"
|
||||
|
||||
#include "game/structs.hpp"
|
||||
#include "game/game.hpp"
|
46
src/utils/concurrency.hpp
Normal file
46
src/utils/concurrency.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace utils::concurrency
|
||||
{
|
||||
template <typename T, typename MutexType = std::mutex>
|
||||
class container
|
||||
{
|
||||
public:
|
||||
template <typename R = void, typename F>
|
||||
R access(F&& accessor) const
|
||||
{
|
||||
std::lock_guard<MutexType> _{mutex_};
|
||||
return accessor(object_);
|
||||
}
|
||||
|
||||
template <typename R = void, typename F>
|
||||
R access(F&& accessor)
|
||||
{
|
||||
std::lock_guard<MutexType> _{mutex_};
|
||||
return accessor(object_);
|
||||
}
|
||||
|
||||
template <typename R = void, typename F>
|
||||
R access_with_lock(F&& accessor) const
|
||||
{
|
||||
std::unique_lock<MutexType> lock{mutex_};
|
||||
return accessor(object_, lock);
|
||||
}
|
||||
|
||||
template <typename R = void, typename F>
|
||||
R access_with_lock(F&& accessor)
|
||||
{
|
||||
std::unique_lock<MutexType> lock{mutex_};
|
||||
return accessor(object_, lock);
|
||||
}
|
||||
|
||||
T& get_raw() { return object_; }
|
||||
const T& get_raw() const { return object_; }
|
||||
|
||||
private:
|
||||
mutable MutexType mutex_{};
|
||||
T object_{};
|
||||
};
|
||||
}
|
@ -4,6 +4,48 @@
|
||||
|
||||
namespace utils::hook
|
||||
{
|
||||
// open-iw5
|
||||
|
||||
void signature::process()
|
||||
{
|
||||
if (this->signatures_.empty()) return;
|
||||
|
||||
const auto start = static_cast<char*>(this->start_);
|
||||
|
||||
const unsigned int sig_count = this->signatures_.size();
|
||||
const auto containers = this->signatures_.data();
|
||||
|
||||
for (size_t i = 0; i < this->length_; ++i)
|
||||
{
|
||||
const auto address = start + i;
|
||||
|
||||
for (unsigned int k = 0; k < sig_count; ++k)
|
||||
{
|
||||
const auto container = &containers[k];
|
||||
|
||||
unsigned int j;
|
||||
for (j = 0; j < static_cast<unsigned int>(container->mask.size()); ++j)
|
||||
{
|
||||
if (container->mask[j] != '?' && container->signature[j] != address[j])
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (j == container->mask.size())
|
||||
{
|
||||
container->callback(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void signature::add(const container& container)
|
||||
{
|
||||
signatures_.push_back(container);
|
||||
}
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
[[maybe_unused]] class _
|
||||
|
@ -4,6 +4,37 @@
|
||||
|
||||
namespace utils::hook
|
||||
{
|
||||
class signature final
|
||||
{
|
||||
public:
|
||||
struct container final
|
||||
{
|
||||
std::string signature;
|
||||
std::string mask;
|
||||
std::function<void(char*)> callback;
|
||||
};
|
||||
|
||||
signature(void* start, const size_t length) : start_(start), length_(length)
|
||||
{
|
||||
}
|
||||
|
||||
signature(const DWORD start, const size_t length) : signature(reinterpret_cast<void*>(start), length)
|
||||
{
|
||||
}
|
||||
|
||||
signature() : signature(0x400000, 0x800000)
|
||||
{
|
||||
}
|
||||
|
||||
void process();
|
||||
void add(const container& container);
|
||||
|
||||
private:
|
||||
void* start_;
|
||||
size_t length_;
|
||||
std::vector<container> signatures_;
|
||||
};
|
||||
|
||||
class detour
|
||||
{
|
||||
public:
|
||||
|
47
src/utils/http.cpp
Normal file
47
src/utils/http.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "http.hpp"
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
std::optional<std::string> get_data(const std::string& url)
|
||||
{
|
||||
CComPtr<IStream> stream;
|
||||
|
||||
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
char buffer[0x1000];
|
||||
std::string result;
|
||||
|
||||
HRESULT status{};
|
||||
|
||||
do
|
||||
{
|
||||
DWORD bytes_read = 0;
|
||||
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
|
||||
|
||||
if (bytes_read > 0)
|
||||
{
|
||||
result.append(buffer, bytes_read);
|
||||
}
|
||||
}
|
||||
while (SUCCEEDED(status) && status != S_FALSE);
|
||||
|
||||
if (FAILED(status))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return {result};
|
||||
}
|
||||
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url)
|
||||
{
|
||||
return std::async(std::launch::async, [url]()
|
||||
{
|
||||
return get_data(url);
|
||||
});
|
||||
}
|
||||
}
|
11
src/utils/http.hpp
Normal file
11
src/utils/http.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <future>
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
std::optional<std::string> get_data(const std::string& url);
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url);
|
||||
}
|
@ -1,8 +1,14 @@
|
||||
#include <stdinc.hpp>
|
||||
#include <fstream>
|
||||
#include "io.hpp"
|
||||
|
||||
namespace utils::io
|
||||
{
|
||||
bool remove_file(const std::string& file)
|
||||
{
|
||||
return DeleteFileA(file.data()) == TRUE;
|
||||
}
|
||||
|
||||
bool file_exists(const std::string& file)
|
||||
{
|
||||
return std::ifstream(file).good();
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
namespace utils::io
|
||||
{
|
||||
bool remove_file(const std::string& file);
|
||||
bool file_exists(const std::string& file);
|
||||
bool write_file(const std::string& file, const std::string& data, bool append = false);
|
||||
bool read_file(const std::string& file, std::string* data);
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user