mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2025-07-03 01:31:49 +00:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
c9543dfbe4 | |||
76842b4bce | |||
7139e8d3c4 | |||
6f53ed5d87 | |||
063caa51f9 | |||
9ed8626067 | |||
cb0725bad1 | |||
44886c8f9f | |||
bdfd37de35 | |||
c49ca5a8cd | |||
b1a29ded7d | |||
e5083cd228 | |||
288fa38dac | |||
dbc0a928b9 | |||
9ead1676b0 | |||
97371d8f44 | |||
7ac6443b2c | |||
50eb58dfd9 | |||
833271b7a1 | |||
517cad33f4 | |||
d96e28cdcb | |||
3d59fd7aa7 | |||
8e0c68ddf9 | |||
2a0789692f | |||
376e7f11ec | |||
c8063eb0b0 | |||
7170f863f3 | |||
ec9a2ad7fa | |||
e84cfe54a6 |
25
README.md
25
README.md
@ -10,7 +10,7 @@ This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
```c
|
||||
init()
|
||||
{
|
||||
replaceFunc(maps\mp\gametypes\_damage::Callback_PlayerDamage, ::callbackPlayerDamage);
|
||||
replaceFunc(maps\mp\gametypes\_damage::Callback_PlayerDamage, ::callbackPlayerDamage);
|
||||
}
|
||||
|
||||
callbackPlayerDamage(eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, timeOffset)
|
||||
@ -43,7 +43,19 @@ 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`
|
||||
|
||||
* `fopen(path, mode)`: Opens a file of given name with given mode, returns a file stream.
|
||||
* `fwrite(stream, text)`: Writes a string to a stream.
|
||||
* `fread(stream)`: Reads entire file.
|
||||
@ -64,6 +76,15 @@ This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
fclose(file);
|
||||
}
|
||||
```
|
||||
* `fileExists(path)`: Returns true if the file exists.
|
||||
* `writeFile(path, data[, append])`: Creates a file if it doesn't exist and writes/appends text to it.
|
||||
* `readFile(path)`: Reads a file.
|
||||
* `fileSize(path)`: Returns file size in bytes.
|
||||
* `createDirectory(path)`: Creates a directory.
|
||||
* `directoryExists(path)`: Returns true if the directory exists.
|
||||
* `directoryIsEmpty(path)`: Returns true if the directory is empty.
|
||||
* `listFiles(path)`: Returns the list of files in the directory as an array.
|
||||
* `copyFolder(source, target)`: Copies a folder.
|
||||
|
||||
# JSON
|
||||
|
||||
@ -205,6 +226,6 @@ This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
* `jsonPrint(...)`: Prints values as json.
|
||||
# Credits
|
||||
* [xensik](https://github.com/xensik)
|
||||
|
@ -7,11 +7,41 @@
|
||||
#include "game/scripting/event.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "game/scripting/functions.hpp"
|
||||
#include "game/scripting/array.hpp"
|
||||
#include "game/scripting/function.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
|
||||
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,7 +79,7 @@ namespace gsc
|
||||
|
||||
function_args get_arguments()
|
||||
{
|
||||
function_args args;
|
||||
std::vector<scripting::script_value> args;
|
||||
|
||||
const auto top = game::scr_VmPub->top;
|
||||
|
||||
@ -92,7 +122,7 @@ namespace gsc
|
||||
return_value(result);
|
||||
}
|
||||
}
|
||||
catch (std::exception e)
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing function %s\n", function_name(id).data());
|
||||
@ -118,7 +148,7 @@ namespace gsc
|
||||
return_value(result);
|
||||
}
|
||||
}
|
||||
catch (std::exception e)
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing method %s\n", method_name(id).data());
|
||||
@ -172,13 +202,13 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int replaced_pos = 0;
|
||||
const char* replaced_pos = 0;
|
||||
|
||||
void get_replaced_pos(unsigned int pos)
|
||||
void get_replaced_pos(const char* pos)
|
||||
{
|
||||
if (scripting::replaced_functions.find(pos) != scripting::replaced_functions.end())
|
||||
if (replaced_functions.find(pos) != replaced_functions.end())
|
||||
{
|
||||
replaced_pos = scripting::replaced_functions[pos];
|
||||
replaced_pos = replaced_functions[pos];
|
||||
}
|
||||
}
|
||||
|
||||
@ -251,86 +281,46 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int make_array()
|
||||
{
|
||||
unsigned int index = 0;
|
||||
const auto variable = game::AllocVariable(&index);
|
||||
variable->w.type = game::SCRIPT_ARRAY;
|
||||
variable->u.f.prev = 0;
|
||||
variable->u.f.next = 0;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void add_array_key_value(unsigned int parent_id, const std::string& _key, const scripting::script_value& value)
|
||||
{
|
||||
const auto key = game::SL_GetString(_key.data(), 0);
|
||||
|
||||
scripting::push_value(scripting::entity(parent_id));
|
||||
scripting::push_value(value);
|
||||
game::Scr_AddArrayStringIndexed(key);
|
||||
}
|
||||
|
||||
void add_array_value(unsigned int parent_id, const scripting::script_value& value)
|
||||
{
|
||||
scripting::push_value(scripting::entity(parent_id));
|
||||
scripting::push_value(value);
|
||||
game::Scr_AddArray();
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
function::add("executecommand", [](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*>());
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("replacefunc", [](function_args args) -> scripting::script_value
|
||||
function::add("replacefunc", [](const function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto what = args[0].get_raw();
|
||||
const auto with = args[1].get_raw();
|
||||
const auto what = args[0].as<scripting::function>();
|
||||
const auto with = args[1].as<scripting::function>();
|
||||
|
||||
if (what.type != game::SCRIPT_FUNCTION || with.type != game::SCRIPT_FUNCTION)
|
||||
{
|
||||
throw std::runtime_error("Invalid type");
|
||||
}
|
||||
|
||||
scripting::replaced_functions[what.u.uintValue] = with.u.uintValue;
|
||||
replaced_functions[what.get_pos()] = with.get_pos();
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("addcommand", [](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 function = args[1].get_raw();
|
||||
|
||||
if (function.type != game::SCRIPT_FUNCTION)
|
||||
const auto function = args[1].as<scripting::function>();
|
||||
command::add_script_command(name, [function](const command::params& params)
|
||||
{
|
||||
throw std::runtime_error("Invalid type");
|
||||
}
|
||||
|
||||
const auto pos = function.u.codePosValue;
|
||||
command::add_script_command(name, [pos](const command::params& params)
|
||||
{
|
||||
const auto array = make_array();
|
||||
scripting::array array;
|
||||
for (auto i = 0; i < params.size(); i++)
|
||||
{
|
||||
add_array_value(array, params[i]);
|
||||
array.push(params[i]);
|
||||
}
|
||||
|
||||
const auto entity = scripting::entity(array);
|
||||
scripting::exec_ent_thread(*game::levelEntityId, pos, {entity});
|
||||
function({array.get_raw()});
|
||||
});
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("say", [](function_args args) -> scripting::script_value
|
||||
function::add("say", [](const function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto message = args[0].as<std::string>();
|
||||
game::SV_GameSendServerCommand(-1, 0, utils::string::va("%c \"%s\"", 84, message.data()));
|
||||
@ -338,11 +328,11 @@ namespace gsc
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("tell", [](game::scr_entref_t ent, function_args args) -> scripting::script_value
|
||||
method::add("tell", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid type");
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
@ -2,13 +2,30 @@
|
||||
|
||||
namespace gsc
|
||||
{
|
||||
using function_args = std::vector<scripting::script_value>;
|
||||
extern std::unordered_map<const char*, const char*> replaced_functions;
|
||||
|
||||
class function_args
|
||||
{
|
||||
public:
|
||||
function_args(std::vector<scripting::script_value>);
|
||||
|
||||
unsigned int size() const;
|
||||
std::vector<scripting::script_value> get_raw() const;
|
||||
scripting::script_value get(const int index) const;
|
||||
|
||||
scripting::script_value operator[](const int index) const
|
||||
{
|
||||
return this->get(index);
|
||||
}
|
||||
private:
|
||||
std::vector<scripting::script_value> values_;
|
||||
};
|
||||
|
||||
using builtin_function = void(*)();
|
||||
using builtin_method = void(*)(game::scr_entref_t);
|
||||
|
||||
using script_function = std::function<scripting::script_value(function_args)>;
|
||||
using script_method = std::function<scripting::script_value(game::scr_entref_t, function_args)>;
|
||||
using script_function = std::function<scripting::script_value(const function_args&)>;
|
||||
using script_method = std::function<scripting::script_value(const game::scr_entref_t, const function_args&)>;
|
||||
|
||||
namespace function
|
||||
{
|
||||
@ -19,8 +36,4 @@ namespace gsc
|
||||
{
|
||||
void add(const std::string& name, const script_method& func);
|
||||
}
|
||||
|
||||
unsigned int make_array();
|
||||
void add_array_key_value(unsigned int parent_id, const std::string& _key, const scripting::script_value& value);
|
||||
void add_array_value(unsigned int parent_id, const scripting::script_value& value);
|
||||
}
|
@ -6,8 +6,10 @@
|
||||
#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"
|
||||
|
||||
namespace io
|
||||
{
|
||||
@ -19,13 +21,27 @@ namespace io
|
||||
const auto path = game::Dvar_FindVar("fs_basegame")->current.string;
|
||||
std::filesystem::current_path(path);
|
||||
|
||||
gsc::function::add("fremove", [](gsc::function_args args)
|
||||
gsc::function::add("jsonprint", [](const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
std::string buffer;
|
||||
|
||||
for (const auto arg : args.get_raw())
|
||||
{
|
||||
buffer.append(json::gsc_to_string(arg));
|
||||
buffer.append("\t");
|
||||
}
|
||||
|
||||
printf("%s\n", buffer.data());
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::function::add("fremove", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<const char*>();
|
||||
return std::remove(path);
|
||||
});
|
||||
|
||||
gsc::function::add("fopen", [](gsc::function_args args)
|
||||
gsc::function::add("fopen", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto* path = args[0].as<const char*>();
|
||||
const auto* mode = args[1].as<const char*>();
|
||||
@ -40,13 +56,13 @@ namespace io
|
||||
return handle;
|
||||
});
|
||||
|
||||
gsc::function::add("fclose", [](gsc::function_args args)
|
||||
gsc::function::add("fclose", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
return fclose(handle);
|
||||
});
|
||||
|
||||
gsc::function::add("fwrite", [](gsc::function_args args)
|
||||
gsc::function::add("fwrite", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
const auto text = args[1].as<const char*>();
|
||||
@ -54,7 +70,7 @@ namespace io
|
||||
return fprintf(handle, text);
|
||||
});
|
||||
|
||||
gsc::function::add("fread", [](gsc::function_args args)
|
||||
gsc::function::add("fread", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
|
||||
@ -72,6 +88,99 @@ namespace io
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
gsc::function::add("fileexists", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
return utils::io::file_exists(path);
|
||||
});
|
||||
|
||||
gsc::function::add("writefile", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
const auto data = args[1].as<std::string>();
|
||||
|
||||
auto append = false;
|
||||
if (args.size() > 2)
|
||||
{
|
||||
append = args[2].as<bool>();
|
||||
}
|
||||
|
||||
return utils::io::write_file(path, data, append);
|
||||
});
|
||||
|
||||
gsc::function::add("readfile", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
return utils::io::read_file(path);
|
||||
});
|
||||
|
||||
gsc::function::add("filesize", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
return utils::io::file_size(path);
|
||||
});
|
||||
|
||||
gsc::function::add("createdirectory", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
return utils::io::create_directory(path);
|
||||
});
|
||||
|
||||
gsc::function::add("directoryexists", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
return utils::io::directory_exists(path);
|
||||
});
|
||||
|
||||
gsc::function::add("directoryisempty", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
return utils::io::directory_is_empty(path);
|
||||
});
|
||||
|
||||
gsc::function::add("listfiles", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
const auto files = utils::io::list_files(path);
|
||||
|
||||
scripting::array array;
|
||||
for (const auto& file : files)
|
||||
{
|
||||
array.push(file);
|
||||
}
|
||||
|
||||
return array;
|
||||
});
|
||||
|
||||
gsc::function::add("copyfolder", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto source = args[0].as<std::string>();
|
||||
const auto target = args[1].as<std::string>();
|
||||
utils::io::copy_folder(source, target);
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -6,8 +6,10 @@
|
||||
#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"
|
||||
|
||||
#include <json.hpp>
|
||||
|
||||
@ -19,44 +21,26 @@ namespace json
|
||||
|
||||
nlohmann::json entity_to_array(unsigned int id)
|
||||
{
|
||||
scripting::array array(id);
|
||||
nlohmann::json obj;
|
||||
|
||||
auto string_indexed = -1;
|
||||
|
||||
const auto offset = 0xC800 * (id & 1);
|
||||
auto current = game::scr_VarGlob->objectVariableChildren[id].firstChild;
|
||||
|
||||
for (auto i = offset + current; current; i = offset + current)
|
||||
const auto keys = array.get_keys();
|
||||
for (auto i = 0; i < keys.size(); i++)
|
||||
{
|
||||
const auto var = game::scr_VarGlob->childVariableValue[i];
|
||||
|
||||
if (var.type == game::SCRIPT_NONE)
|
||||
{
|
||||
current = var.nextSibling;
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (string_indexed == -1)
|
||||
{
|
||||
string_indexed = string_value < 0x40000 && str;
|
||||
string_indexed = keys[i].is_string;
|
||||
}
|
||||
|
||||
game::VariableValue variable{};
|
||||
variable.type = (game::scriptType_e)var.type;
|
||||
variable.u = var.u.u;
|
||||
|
||||
if (!string_indexed)
|
||||
if (!string_indexed && keys[i].is_integer)
|
||||
{
|
||||
obj.emplace_back(gsc_to_json(variable));
|
||||
obj[keys[i].index] = gsc_to_json(array[keys[i].index]);
|
||||
}
|
||||
else
|
||||
else if (string_indexed && keys[i].is_string)
|
||||
{
|
||||
obj.emplace(str, gsc_to_json(variable));
|
||||
obj.emplace(keys[i].key, gsc_to_json(array[keys[i].key]));
|
||||
}
|
||||
|
||||
current = var.nextSibling;
|
||||
}
|
||||
|
||||
return obj;
|
||||
@ -127,25 +111,25 @@ namespace json
|
||||
return obj.get<std::string>();
|
||||
case (nlohmann::detail::value_t::array):
|
||||
{
|
||||
const auto arr = gsc::make_array();
|
||||
scripting::array array;
|
||||
|
||||
for (const auto& [key, value] : obj.items())
|
||||
{
|
||||
gsc::add_array_value(arr, json_to_gsc(value));
|
||||
array.push(json_to_gsc(value));
|
||||
}
|
||||
|
||||
return scripting::entity(arr);
|
||||
return array.get_raw();
|
||||
}
|
||||
case (nlohmann::detail::value_t::object):
|
||||
{
|
||||
const auto arr = gsc::make_array();
|
||||
scripting::array array;
|
||||
|
||||
for (const auto& [key, value] : obj.items())
|
||||
{
|
||||
gsc::add_array_key_value(arr, key, json_to_gsc(value));
|
||||
array[key] = json_to_gsc(value);
|
||||
}
|
||||
|
||||
return scripting::entity(arr);
|
||||
return array.get_raw();
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,26 +137,25 @@ namespace json
|
||||
}
|
||||
}
|
||||
|
||||
std::string gsc_to_string(const scripting::script_value& value)
|
||||
{
|
||||
return gsc_to_json(value).dump();
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
gsc::function::add("array", [](gsc::function_args args)
|
||||
gsc::function::add("array", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto array = gsc::make_array();
|
||||
|
||||
for (auto i = 0; i < args.size(); i++)
|
||||
{
|
||||
gsc::add_array_value(array, args[i]);
|
||||
}
|
||||
|
||||
return scripting::entity(array);
|
||||
scripting::array array(args.get_raw());
|
||||
return array.get_raw();
|
||||
});
|
||||
|
||||
gsc::function::add("map", [](gsc::function_args args)
|
||||
gsc::function::add("map", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto array = gsc::make_array();
|
||||
scripting::array array;
|
||||
|
||||
for (auto i = 0; i < args.size(); i += 2)
|
||||
{
|
||||
@ -182,13 +165,13 @@ namespace json
|
||||
}
|
||||
|
||||
const auto key = args[i].as<std::string>();
|
||||
gsc::add_array_key_value(array, key, args[i + 1]);
|
||||
array[key] = args[i + 1];
|
||||
}
|
||||
|
||||
return scripting::entity(array);
|
||||
return array;
|
||||
});
|
||||
|
||||
gsc::function::add("jsonparse", [](gsc::function_args args)
|
||||
gsc::function::add("jsonparse", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto json = args[0].as<std::string>();
|
||||
const auto obj = nlohmann::json::parse(json);
|
||||
@ -196,7 +179,7 @@ namespace json
|
||||
return json_to_gsc(obj);
|
||||
});
|
||||
|
||||
gsc::function::add("jsonserialize", [](gsc::function_args args)
|
||||
gsc::function::add("jsonserialize", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto value = args[0];
|
||||
auto indent = -1;
|
||||
|
6
src/component/json.hpp
Normal file
6
src/component/json.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace json
|
||||
{
|
||||
std::string gsc_to_string(const scripting::script_value& _value);
|
||||
}
|
@ -4,7 +4,6 @@
|
||||
|
||||
#include "game/scripting/entity.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "notifies.hpp"
|
||||
|
||||
namespace notifies
|
||||
{
|
||||
@ -12,16 +11,13 @@ namespace notifies
|
||||
{
|
||||
utils::hook::detour client_command_hook;
|
||||
|
||||
utils::hook::detour scr_player_killed_hook;
|
||||
utils::hook::detour scr_player_damage_hook;
|
||||
|
||||
void client_command_stub(int clientNum)
|
||||
{
|
||||
char cmd[1024] = { 0 };
|
||||
|
||||
game::SV_Cmd_ArgvBuffer(0, cmd, 1024);
|
||||
|
||||
if (cmd == "say"s)
|
||||
if (cmd == "say"s || cmd == "say_team"s)
|
||||
{
|
||||
std::string message = game::ConcatArgs(1);
|
||||
message.erase(0, 1);
|
||||
@ -29,8 +25,8 @@ namespace notifies
|
||||
const scripting::entity level{*game::levelEntityId};
|
||||
const auto player = scripting::call("getEntByNum", {clientNum}).as<scripting::entity>();
|
||||
|
||||
scripting::notify(level, "say", {player, message});
|
||||
scripting::notify(player, "say", {message});
|
||||
scripting::notify(level, cmd, {player, message});
|
||||
scripting::notify(player, cmd, {message});
|
||||
}
|
||||
|
||||
return client_command_hook.invoke<void>(clientNum);
|
||||
|
@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace notifies
|
||||
{
|
||||
}
|
@ -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,9 +130,18 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(scheduler::component)
|
||||
REGISTER_COMPONENT(scheduler::component)
|
||||
|
@ -2,9 +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,
|
||||
};
|
||||
|
||||
void init();
|
||||
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);
|
||||
}
|
||||
|
@ -3,17 +3,18 @@
|
||||
|
||||
#include "scheduler.hpp"
|
||||
#include "command.hpp"
|
||||
#include "userinfo.hpp"
|
||||
|
||||
#include "game/scripting/event.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "game/scripting/functions.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
||||
std::unordered_map<std::string, std::unordered_map<std::string, char*>> script_function_table;
|
||||
|
||||
std::unordered_map<unsigned, unsigned> replaced_functions;
|
||||
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
||||
|
||||
namespace
|
||||
{
|
||||
@ -23,8 +24,10 @@ namespace scripting
|
||||
utils::hook::detour scr_load_level_hook;
|
||||
utils::hook::detour g_shutdown_game_hook;
|
||||
|
||||
utils::hook::detour scr_emit_function_hook;
|
||||
utils::hook::detour scr_end_load_scripts_hook;
|
||||
utils::hook::detour scr_set_thread_position_hook;
|
||||
utils::hook::detour process_script_hook;
|
||||
|
||||
std::string current_file;
|
||||
|
||||
void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value,
|
||||
game::VariableValue* top)
|
||||
@ -44,7 +47,9 @@ namespace scripting
|
||||
|
||||
if (e.name == "connected")
|
||||
{
|
||||
scripting::clear_entity_fields(e.entity);
|
||||
const auto player = e.arguments[0].as<scripting::entity>();
|
||||
const auto client = player.call("getentitynumber").as<int>();
|
||||
userinfo::clear_client_overrides(client);
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,58 +75,35 @@ namespace scripting
|
||||
|
||||
void g_shutdown_game_stub(const int free_scripts)
|
||||
{
|
||||
userinfo::clear_overrides();
|
||||
command::clear_script_commands();
|
||||
replaced_functions.clear();
|
||||
gsc::replaced_functions.clear();
|
||||
g_shutdown_game_hook.invoke<void>(free_scripts);
|
||||
}
|
||||
|
||||
char* function_pos(unsigned int filename, unsigned int name)
|
||||
void process_script_stub(const char* filename)
|
||||
{
|
||||
const auto scripts_pos = *reinterpret_cast<int*>(0x1D6EB14);
|
||||
current_file = filename;
|
||||
|
||||
const auto v2 = game::FindVariable(scripts_pos, filename);
|
||||
|
||||
const auto v3 = game::FindObject(scripts_pos, v2);
|
||||
const auto v4 = game::FindVariable(v3, name);
|
||||
|
||||
if (!v2 || !v3 || !v4)
|
||||
const auto file_id = atoi(filename);
|
||||
if (file_id)
|
||||
{
|
||||
return 0;
|
||||
current_file = scripting::file_list[file_id];
|
||||
}
|
||||
|
||||
return utils::hook::invoke<char*>(0x5659C0, v3, v4);
|
||||
process_script_hook.invoke<void>(filename);
|
||||
}
|
||||
|
||||
void scr_emit_function_stub(unsigned int filename, unsigned int threadName, char* codePos)
|
||||
void scr_set_thread_position_stub(unsigned int threadName, const char* codePos)
|
||||
{
|
||||
const auto* name = game::SL_ConvertToString(filename);
|
||||
const auto filename_id = atoi(name);
|
||||
const auto function_name = scripting::find_token(threadName);
|
||||
|
||||
for (const auto& entry : scripting::file_list)
|
||||
if (!function_name.empty())
|
||||
{
|
||||
if (entry.first == filename_id)
|
||||
{
|
||||
if (script_function_table.find(entry.second) == script_function_table.end())
|
||||
{
|
||||
script_function_table[entry.second] = {};
|
||||
}
|
||||
|
||||
for (const auto& token : scripting::token_map)
|
||||
{
|
||||
if (token.second == threadName)
|
||||
{
|
||||
const auto pos = function_pos(filename, threadName);
|
||||
|
||||
if (pos)
|
||||
{
|
||||
script_function_table[entry.second][token.first] = pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
script_function_table[current_file][function_name] = codePos;
|
||||
}
|
||||
|
||||
scr_emit_function_hook.invoke<void>(filename, threadName, codePos);
|
||||
scr_set_thread_position_hook.invoke<void>(threadName, codePos);
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,6 +116,10 @@ namespace scripting
|
||||
g_shutdown_game_hook.create(0x50C100, g_shutdown_game_stub);
|
||||
|
||||
scr_add_class_field_hook.create(0x567CD0, scr_add_class_field_stub);
|
||||
vm_notify_hook.create(0x569720, vm_notify_stub);
|
||||
|
||||
scr_set_thread_position_hook.create(0x5616D0, scr_set_thread_position_stub);
|
||||
process_script_hook.create(0x56B130, process_script_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,8 +2,6 @@
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
extern std::unordered_map<unsigned, unsigned> replaced_functions;
|
||||
|
||||
extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
||||
extern std::unordered_map<std::string, std::unordered_map<std::string, char*>> script_function_table;
|
||||
extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
||||
}
|
177
src/component/userinfo.cpp
Normal file
177
src/component/userinfo.cpp
Normal file
@ -0,0 +1,177 @@
|
||||
#include <stdinc.hpp>
|
||||
#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
|
||||
{
|
||||
using userinfo_map = std::unordered_map<std::string, std::string>;
|
||||
std::unordered_map<int, userinfo_map> userinfo_overrides;
|
||||
|
||||
namespace
|
||||
{
|
||||
utils::hook::detour sv_getuserinfo_hook;
|
||||
|
||||
userinfo_map userinfo_to_map(const std::string& userinfo)
|
||||
{
|
||||
userinfo_map map;
|
||||
const auto args = utils::string::split(userinfo, '\\');
|
||||
|
||||
for (auto i = 1; i < args.size() - 1; i += 2)
|
||||
{
|
||||
map[args[i]] = args[i + 1];
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
std::string map_to_userinfo(const userinfo_map& map)
|
||||
{
|
||||
std::string buffer;
|
||||
|
||||
for (const auto& value : map)
|
||||
{
|
||||
buffer.append("\\");
|
||||
buffer.append(value.first);
|
||||
buffer.append("\\");
|
||||
buffer.append(value.second);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (userinfo_overrides.find(index) == userinfo_overrides.end())
|
||||
{
|
||||
userinfo_overrides[index] = {};
|
||||
}
|
||||
|
||||
for (const auto& values : userinfo_overrides[index])
|
||||
{
|
||||
if (values.second.empty())
|
||||
{
|
||||
map.erase(values.first);
|
||||
}
|
||||
else
|
||||
{
|
||||
map[values.first] = values.second;
|
||||
}
|
||||
}
|
||||
|
||||
const auto userinfo = map_to_userinfo(map);
|
||||
strcpy_s(buffer, 1024, userinfo.data());
|
||||
}
|
||||
}
|
||||
|
||||
void clear_client_overrides(int client)
|
||||
{
|
||||
userinfo_overrides[client].clear();
|
||||
}
|
||||
|
||||
void clear_overrides()
|
||||
{
|
||||
userinfo_overrides.clear();
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
sv_getuserinfo_hook.create(0x573E00, sv_getuserinfo_stub);
|
||||
|
||||
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)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("name");
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
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)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = name;
|
||||
userinfo_overrides[ent.entnum]["ec_TagText"] = name;
|
||||
userinfo_overrides[ent.entnum]["ec_usingTag"] = "1";
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
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)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("clantag");
|
||||
userinfo_overrides[ent.entnum].erase("ec_TagText");
|
||||
userinfo_overrides[ent.entnum].erase("ec_usingTag");
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = "";
|
||||
userinfo_overrides[ent.entnum]["ec_TagText"] = "";
|
||||
userinfo_overrides[ent.entnum]["ec_usingTag"] = "0";
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
return {};
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(userinfo::component)
|
7
src/component/userinfo.hpp
Normal file
7
src/component/userinfo.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace userinfo
|
||||
{
|
||||
void clear_client_overrides(int client);
|
||||
void clear_overrides();
|
||||
}
|
@ -5,6 +5,16 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
|
||||
{
|
||||
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
const auto value = *reinterpret_cast<DWORD*>(0x20600000);
|
||||
if (value != 0x77E0B164)
|
||||
{
|
||||
printf("\x1b[31m\n**************************************************************************************\n\n");
|
||||
printf("This version of \x1b[33miw5-gsc-utils\x1b[31m is outdated.\n");
|
||||
printf("Download the latest dll from here:\x1b[34m https://github.com/fedddddd/iw5-gsc-utils/releases\ \x1b[31m\n");
|
||||
printf("\n**************************************************************************************\n\n\x1b[37m");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
component_loader::post_unpack();
|
||||
}
|
||||
|
||||
|
341
src/game/scripting/array.cpp
Normal file
341
src/game/scripting/array.cpp
Normal file
@ -0,0 +1,341 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "array.hpp"
|
||||
#include "execution.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
array_value::array_value(unsigned int parent_id, unsigned int id)
|
||||
: id_(id)
|
||||
, parent_id_(parent_id)
|
||||
{
|
||||
if (!this->id_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const auto value = game::scr_VarGlob->childVariableValue[this->id_ + 0xC800 * (this->parent_id_ & 1)];
|
||||
game::VariableValue variable;
|
||||
variable.u = value.u.u;
|
||||
variable.type = (game::scriptType_e)value.type;
|
||||
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
void array_value::operator=(const script_value& _value)
|
||||
{
|
||||
if (!this->id_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const auto value = _value.get_raw();
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[this->id_ + 0xC800 * (this->parent_id_ & 1)];
|
||||
game::AddRefToValue(value.type, value.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = value.type;
|
||||
variable->u.u = value.u;
|
||||
|
||||
this->value_ = value;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
this->id_ = make_array();
|
||||
this->add();
|
||||
}
|
||||
|
||||
array::array(std::vector<script_value> values)
|
||||
{
|
||||
this->id_ = make_array();
|
||||
this->add();
|
||||
|
||||
for (const auto& value : values)
|
||||
{
|
||||
this->push(value);
|
||||
}
|
||||
}
|
||||
|
||||
array::array(std::unordered_map<std::string, script_value> values)
|
||||
{
|
||||
this->id_ = make_array();
|
||||
this->add();
|
||||
|
||||
for (const auto& value : values)
|
||||
{
|
||||
this->set(value.first, value.second);
|
||||
}
|
||||
}
|
||||
|
||||
array::~array()
|
||||
{
|
||||
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<array_key> array::get_keys() const
|
||||
{
|
||||
std::vector<array_key> result;
|
||||
|
||||
const auto offset = 0xC800 * (this->id_ & 1);
|
||||
auto current = game::scr_VarGlob->objectVariableChildren[this->id_].firstChild;
|
||||
|
||||
for (auto i = offset + current; current; i = offset + current)
|
||||
{
|
||||
const auto var = game::scr_VarGlob->childVariableValue[i];
|
||||
|
||||
if (var.type == game::SCRIPT_NONE)
|
||||
{
|
||||
current = var.nextSibling;
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
if (string_value < 0x40000 && str)
|
||||
{
|
||||
key.is_string = true;
|
||||
key.key = str;
|
||||
}
|
||||
else
|
||||
{
|
||||
key.is_integer = true;
|
||||
key.index = (string_value - 0x800000) & 0xFFFFFF;
|
||||
}
|
||||
|
||||
result.push_back(key);
|
||||
|
||||
current = var.nextSibling;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned int array::size() const
|
||||
{
|
||||
return game::Scr_GetSelf(this->id_);
|
||||
}
|
||||
|
||||
unsigned int array::push(script_value value) const
|
||||
{
|
||||
this->set(this->size(), value);
|
||||
return this->size();
|
||||
}
|
||||
|
||||
void array::erase(const unsigned int index) const
|
||||
{
|
||||
const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
if (variable_id)
|
||||
{
|
||||
game::RemoveVariableValue(this->id_, variable_id);
|
||||
}
|
||||
}
|
||||
|
||||
void array::erase(const std::string& key) const
|
||||
{
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto variable_id = game::FindVariable(this->id_, string_value);
|
||||
if (variable_id)
|
||||
{
|
||||
game::RemoveVariableValue(this->id_, variable_id);
|
||||
}
|
||||
}
|
||||
|
||||
script_value array::pop() const
|
||||
{
|
||||
const auto value = this->get(this->size() - 1);
|
||||
this->erase(this->size() - 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
script_value array::get(const array_key& key) const
|
||||
{
|
||||
if (key.is_integer)
|
||||
{
|
||||
return this->get(key.index);
|
||||
}
|
||||
else
|
||||
{
|
||||
return this->get(key.key);
|
||||
}
|
||||
}
|
||||
|
||||
script_value array::get(const std::string& key) const
|
||||
{
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto variable_id = game::FindVariable(this->id_, string_value);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto value = game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
game::VariableValue variable;
|
||||
variable.u = value.u.u;
|
||||
variable.type = (game::scriptType_e)value.type;
|
||||
|
||||
return variable;
|
||||
}
|
||||
|
||||
script_value array::get(const unsigned int index) const
|
||||
{
|
||||
const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto value = game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
game::VariableValue variable;
|
||||
variable.u = value.u.u;
|
||||
variable.type = (game::scriptType_e)value.type;
|
||||
|
||||
return variable;
|
||||
}
|
||||
|
||||
void array::set(const array_key& key, const script_value& value) const
|
||||
{
|
||||
if (key.is_integer)
|
||||
{
|
||||
this->set(key.index, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->set(key.key, value);
|
||||
}
|
||||
}
|
||||
|
||||
void array::set(const std::string& key, const script_value& _value) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto variable_id = this->get_value_id(key);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
|
||||
game::AddRefToValue(value.type, value.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = value.type;
|
||||
variable->u.u = value.u;
|
||||
}
|
||||
|
||||
void array::set(const unsigned int index, const script_value& _value) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
const auto variable_id = this->get_value_id(index);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
|
||||
game::AddRefToValue(value.type, value.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = value.type;
|
||||
variable->u.u = value.u;
|
||||
}
|
||||
|
||||
unsigned int array::get_entity_id() const
|
||||
{
|
||||
return this->id_;
|
||||
}
|
||||
|
||||
unsigned int array::get_value_id(const std::string& key) const
|
||||
{
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto variable_id = game::FindVariable(this->id_, string_value);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
return game::GetNewVariable(this->id_, string_value);
|
||||
}
|
||||
|
||||
return variable_id;
|
||||
}
|
||||
|
||||
unsigned int array::get_value_id(const unsigned int index) const
|
||||
{
|
||||
const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
if (!variable_id)
|
||||
{
|
||||
return game::GetNewArrayVariable(this->id_, index);
|
||||
}
|
||||
|
||||
return variable_id;
|
||||
}
|
||||
|
||||
entity array::get_raw() const
|
||||
{
|
||||
return entity(this->id_);
|
||||
}
|
||||
}
|
92
src/game/scripting/array.hpp
Normal file
92
src/game/scripting/array.hpp
Normal file
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
#include "script_value.hpp"
|
||||
|
||||
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
|
||||
{
|
||||
public:
|
||||
array_value(unsigned int, unsigned int);
|
||||
void operator=(const script_value&);
|
||||
private:
|
||||
unsigned int id_;
|
||||
unsigned int parent_id_;
|
||||
};
|
||||
|
||||
class array final
|
||||
{
|
||||
public:
|
||||
array();
|
||||
array(const unsigned int);
|
||||
|
||||
array(std::vector<script_value>);
|
||||
array(std::unordered_map<std::string, script_value>);
|
||||
|
||||
array(const array& other);
|
||||
array(array&& other) noexcept;
|
||||
|
||||
~array();
|
||||
|
||||
array& operator=(const array& other);
|
||||
array& operator=(array&& other) noexcept;
|
||||
|
||||
std::vector<array_key> get_keys() const;
|
||||
unsigned int size() const;
|
||||
|
||||
unsigned int push(script_value) const;
|
||||
void erase(const unsigned int) const;
|
||||
void erase(const std::string&) const;
|
||||
script_value pop() const;
|
||||
|
||||
script_value get(const array_key&) const;
|
||||
script_value get(const std::string&) const;
|
||||
script_value get(const unsigned int) const;
|
||||
|
||||
void set(const array_key&, const script_value&) const;
|
||||
void set(const std::string&, const script_value&) const;
|
||||
void set(const unsigned int, const script_value&) const;
|
||||
|
||||
unsigned int get_entity_id() const;
|
||||
|
||||
unsigned int get_value_id(const std::string&) const;
|
||||
unsigned int get_value_id(const unsigned int) const;
|
||||
|
||||
entity get_raw() const;
|
||||
|
||||
array_value operator[](const int index) const
|
||||
{
|
||||
return {this->id_, this->get_value_id(index)};
|
||||
}
|
||||
|
||||
array_value operator[](const std::string& key) const
|
||||
{
|
||||
return {this->id_, this->get_value_id(key)};
|
||||
}
|
||||
|
||||
array_value operator[](const array_key& key) const
|
||||
{
|
||||
if (key.is_integer)
|
||||
{
|
||||
return {this->id_, this->get_value_id(key.index)};
|
||||
}
|
||||
else
|
||||
{
|
||||
return {this->id_, this->get_value_id(key.key)};
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void add() const;
|
||||
void release() const;
|
||||
|
||||
unsigned int id_;
|
||||
};
|
||||
}
|
@ -155,8 +155,7 @@ namespace scripting
|
||||
return get_return_value();
|
||||
}
|
||||
|
||||
script_value call_script_function(const entity& entity, const std::string& filename,
|
||||
const std::string& function, const std::vector<script_value>& arguments)
|
||||
const char* get_function_pos(const std::string& filename, const std::string& function)
|
||||
{
|
||||
if (scripting::script_function_table.find(filename) == scripting::script_function_table.end())
|
||||
{
|
||||
@ -164,14 +163,18 @@ namespace scripting
|
||||
};
|
||||
|
||||
const auto functions = scripting::script_function_table[filename];
|
||||
|
||||
if (functions.find(function) == functions.end())
|
||||
{
|
||||
throw std::runtime_error("Function '" + function + "' in file '" + filename + "' not found");
|
||||
}
|
||||
|
||||
const auto pos = functions.at(function);
|
||||
return functions.at(function);
|
||||
}
|
||||
|
||||
script_value call_script_function(const entity& entity, const std::string& filename,
|
||||
const std::string& function, const std::vector<script_value>& arguments)
|
||||
{
|
||||
const auto pos = get_function_pos(filename, function);
|
||||
return exec_ent_thread(entity, pos, arguments);
|
||||
}
|
||||
|
||||
@ -269,4 +272,25 @@ namespace scripting
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
unsigned int make_array()
|
||||
{
|
||||
unsigned int index = 0;
|
||||
const auto variable = game::AllocVariable(&index);
|
||||
variable->w.type = game::SCRIPT_ARRAY;
|
||||
variable->u.f.prev = 0;
|
||||
variable->u.f.next = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -34,4 +34,7 @@ namespace scripting
|
||||
script_value get_entity_field(const entity& entity, const std::string& field);
|
||||
|
||||
void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);
|
||||
|
||||
unsigned int make_array();
|
||||
unsigned int make_object();
|
||||
}
|
||||
|
30
src/game/scripting/function.cpp
Normal file
30
src/game/scripting/function.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "function.hpp"
|
||||
#include "execution.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
function::function(const char* pos)
|
||||
: pos_(pos)
|
||||
{
|
||||
}
|
||||
|
||||
script_value function::get_raw() const
|
||||
{
|
||||
game::VariableValue value;
|
||||
value.type = game::SCRIPT_FUNCTION;
|
||||
value.u.codePosValue = this->pos_;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
const char* function::get_pos() const
|
||||
{
|
||||
return this->pos_;
|
||||
}
|
||||
|
||||
script_value function::call(entity self, std::vector<script_value> arguments) const
|
||||
{
|
||||
return exec_ent_thread(self, this->pos_, arguments);
|
||||
}
|
||||
}
|
34
src/game/scripting/function.hpp
Normal file
34
src/game/scripting/function.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "entity.hpp"
|
||||
#include "script_value.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
class function
|
||||
{
|
||||
public:
|
||||
function(const char*);
|
||||
|
||||
script_value get_raw() const;
|
||||
const char* get_pos() const;
|
||||
|
||||
script_value call(entity self, std::vector<script_value> arguments) const;
|
||||
|
||||
script_value operator()(entity self, std::vector<script_value> arguments) const
|
||||
{
|
||||
return this->call(self, arguments);
|
||||
}
|
||||
|
||||
script_value operator()(std::vector<script_value> arguments) const
|
||||
{
|
||||
return this->call(*game::levelEntityId, arguments);
|
||||
}
|
||||
|
||||
script_value operator()() const
|
||||
{
|
||||
return this->call(*game::levelEntityId, {});
|
||||
}
|
||||
private:
|
||||
const char* pos_;
|
||||
};
|
||||
}
|
@ -71,6 +71,19 @@ namespace scripting
|
||||
}
|
||||
}
|
||||
|
||||
std::string find_token(unsigned int id)
|
||||
{
|
||||
for (const auto& token : token_map)
|
||||
{
|
||||
if (token.second == id)
|
||||
{
|
||||
return token.first;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
int find_token_id(const std::string& name)
|
||||
{
|
||||
const auto result = token_map.find(name);
|
||||
|
@ -12,4 +12,5 @@ namespace scripting
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "script_value.hpp"
|
||||
#include "entity.hpp"
|
||||
|
||||
#include "array.hpp"
|
||||
#include "function.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
@ -102,6 +103,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
|
||||
**************************************************************/
|
||||
@ -219,7 +238,7 @@ namespace scripting
|
||||
**************************************************************/
|
||||
|
||||
template <>
|
||||
bool script_value::is<std::vector<script_value>>() const
|
||||
bool script_value::is<array>() const
|
||||
{
|
||||
if (this->get_raw().type != game::SCRIPT_OBJECT)
|
||||
{
|
||||
@ -232,6 +251,12 @@ namespace scripting
|
||||
return type == game::SCRIPT_ARRAY;
|
||||
}
|
||||
|
||||
template <>
|
||||
array script_value::get() const
|
||||
{
|
||||
return array(this->get_raw().u.uintValue);
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
* Struct
|
||||
**************************************************************/
|
||||
@ -255,11 +280,17 @@ namespace scripting
|
||||
**************************************************************/
|
||||
|
||||
template <>
|
||||
bool script_value::is<std::function<void()>>() const
|
||||
bool script_value::is<function>() const
|
||||
{
|
||||
return this->get_raw().type == game::SCRIPT_FUNCTION;
|
||||
}
|
||||
|
||||
template <>
|
||||
function script_value::get() const
|
||||
{
|
||||
return function(this->get_raw().u.codePosValue);
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
* Vector
|
||||
**************************************************************/
|
||||
|
@ -6,6 +6,8 @@
|
||||
namespace scripting
|
||||
{
|
||||
class entity;
|
||||
class array;
|
||||
class function;
|
||||
|
||||
class script_value
|
||||
{
|
||||
@ -29,6 +31,10 @@ namespace scripting
|
||||
|
||||
script_value(const vector& value);
|
||||
|
||||
script_value(const array& value);
|
||||
|
||||
script_value(const function& value);
|
||||
|
||||
template <typename T>
|
||||
bool is() const;
|
||||
|
||||
@ -43,15 +49,15 @@ namespace scripting
|
||||
return get<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename T, typename I = int>
|
||||
T* as_ptr()
|
||||
{
|
||||
if (!this->is<int>())
|
||||
if (!this->is<I>())
|
||||
{
|
||||
throw std::runtime_error("Invalid type");
|
||||
}
|
||||
|
||||
const auto value = this->get<int>();
|
||||
const auto value = this->get<I>();
|
||||
|
||||
if (!value)
|
||||
{
|
||||
@ -63,10 +69,9 @@ namespace scripting
|
||||
|
||||
const game::VariableValue& get_raw() const;
|
||||
|
||||
variable_value value_{};
|
||||
private:
|
||||
template <typename T>
|
||||
T get() const;
|
||||
|
||||
variable_value value_{};
|
||||
};
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ namespace game
|
||||
|
||||
WEAK symbol<void(unsigned int weapon, bool isAlternate, char* output, unsigned int maxStringLen)> BG_GetWeaponNameComplete{0x42F760};
|
||||
|
||||
WEAK symbol<void(int client)> ClientUserinfoChanged{0x4FADB0};
|
||||
WEAK symbol<const char*(int index)> ConcatArgs{0x502150};
|
||||
WEAK symbol<void(int localClientNum, const char* text)> Cbuf_AddText{0x545680};
|
||||
WEAK symbol<void(const char* cmdName, void(), cmd_function_t* allocedCmd)> Cmd_AddCommandInternal{0x545DF0};
|
||||
@ -31,7 +32,9 @@ namespace game
|
||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindObject{0x565BD0};
|
||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> GetVariable{0x5663E0};
|
||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> GetNewVariable{0x566390};
|
||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int unsignedValue)> GetNewArrayVariable{0x5668C0};
|
||||
WEAK symbol<void(unsigned int parentId, unsigned int id, VariableValue* value)> SetNewVariableValue{0x5658D0};
|
||||
WEAK symbol<void(unsigned int parentId, unsigned int index)> RemoveVariableValue{0x566500};
|
||||
|
||||
WEAK symbol<const float* (const float* v)> Scr_AllocVector{0x565680};
|
||||
WEAK symbol<void()> Scr_ClearOutParams{0x569010};
|
||||
@ -76,8 +79,7 @@ namespace game
|
||||
|
||||
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, std::uint16_t>> function_map_rev{0x2059A610};
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0x2059A630};
|
||||
}
|
||||
}
|
@ -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,9 @@
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <csetjmp>
|
||||
#include <atlcomcli.h>
|
||||
|
||||
#pragma comment(lib, "urlmon.lib")
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
@ -36,6 +41,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_{};
|
||||
};
|
||||
}
|
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);
|
||||
}
|
Reference in New Issue
Block a user