8 Commits

Author SHA1 Message Date
3d59fd7aa7 More fixes 2021-06-18 23:29:48 +02:00
8e0c68ddf9 Add more IO functions 2021-06-18 23:18:14 +02:00
2a0789692f Catch insufficient arguments 2021-06-18 23:18:04 +02:00
376e7f11ec Small fix 2021-06-18 23:01:15 +02:00
c8063eb0b0 Use array class 2021-06-18 22:42:12 +02:00
7170f863f3 Cleanup 2021-06-18 22:26:30 +02:00
ec9a2ad7fa Also notify say_team 2021-06-18 22:26:18 +02:00
e84cfe54a6 Correct json array indexes 2021-06-18 22:23:37 +02:00
13 changed files with 461 additions and 97 deletions

View File

@ -7,11 +7,38 @@
#include "game/scripting/event.hpp"
#include "game/scripting/execution.hpp"
#include "game/scripting/functions.hpp"
#include "game/scripting/array.hpp"
#include "gsc.hpp"
namespace gsc
{
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 +76,7 @@ namespace gsc
function_args get_arguments()
{
function_args args;
std::vector<scripting::script_value> args;
const auto top = game::scr_VmPub->top;
@ -251,33 +278,6 @@ 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:
@ -317,14 +317,13 @@ namespace gsc
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});
scripting::exec_ent_thread(*game::levelEntityId, pos, {array.get_raw()});
});
return {};

View File

@ -2,7 +2,22 @@
namespace gsc
{
using function_args = std::vector<scripting::script_value>;
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;
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);
@ -19,8 +34,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);
}

View File

@ -6,6 +6,7 @@
#include "game/scripting/event.hpp"
#include "game/scripting/execution.hpp"
#include "game/scripting/functions.hpp"
#include "game/scripting/array.hpp"
#include "gsc.hpp"
@ -72,6 +73,79 @@ namespace io
return result;
});
gsc::function::add("fileexists", [](gsc::function_args args)
{
const auto path = args[0].as<std::string>();
return utils::io::file_exists(path);
});
gsc::function::add("writefile", [](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", [](gsc::function_args args)
{
const auto path = args[0].as<std::string>();
return utils::io::read_file(path);
});
gsc::function::add("filesize", [](gsc::function_args args)
{
const auto path = args[0].as<std::string>();
return utils::io::file_size(path);
});
gsc::function::add("createdirectory", [](gsc::function_args args)
{
const auto path = args[0].as<std::string>();
return utils::io::create_directory(path);
});
gsc::function::add("directoryexists", [](gsc::function_args args)
{
const auto path = args[0].as<std::string>();
return utils::io::directory_exists(path);
});
gsc::function::add("directoryisempty", [](gsc::function_args args)
{
const auto path = args[0].as<std::string>();
return utils::io::directory_is_empty(path);
});
gsc::function::add("listfiles", [](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.get_raw();
});
gsc::function::add("copyfolder", [](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{};
});
}
};
}

View File

@ -6,6 +6,7 @@
#include "game/scripting/event.hpp"
#include "game/scripting/execution.hpp"
#include "game/scripting/functions.hpp"
#include "game/scripting/array.hpp"
#include "gsc.hpp"
@ -19,44 +20,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 +110,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();
}
}
@ -160,19 +143,13 @@ namespace json
{
gsc::function::add("array", [](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)
{
const auto array = gsc::make_array();
scripting::array array;
for (auto i = 0; i < args.size(); i += 2)
{
@ -182,10 +159,10 @@ 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.get_raw();
});
gsc::function::add("jsonparse", [](gsc::function_args args)

View File

@ -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);

View File

@ -1,5 +0,0 @@
#pragma once
namespace notifies
{
}

View File

@ -0,0 +1,231 @@
#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(unsigned int id)
: id_(id)
{
}
array::array()
{
this->id_ = make_array();
}
array::array(std::vector<script_value> values)
{
this->id_ = make_array();
for (const auto& value : values)
{
this->push(value);
}
}
array::array(std::unordered_map<std::string, script_value> values)
{
this->id_ = make_array();
for (const auto& value : values)
{
this->set(value.first, value.second);
}
}
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;
}
int array::size() const
{
return game::Scr_GetSelf(this->id_);
}
void array::push(script_value value) const
{
this->set(this->size(), value);
}
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);
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::GetVariable(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;
}
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::GetVariable(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::GetVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
if (!variable_id)
{
return game::GetNewArrayVariable(this->id_, index);
}
return variable_id;
}
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;
}
entity array::get_raw() const
{
return entity(this->id_);
}
}

View File

@ -0,0 +1,62 @@
#pragma once
#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);
private:
unsigned int id_;
unsigned int parent_id_;
};
class array final
{
public:
array();
array(unsigned int);
array(std::vector<script_value>);
array(std::unordered_map<std::string, script_value>);
std::vector<array_key> get_keys() const;
int size() const;
void push(script_value) const;
script_value get(const std::string&) const;
script_value get(const unsigned int) 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;
void set(const std::string&, const script_value&) const;
void set(const unsigned int, const script_value&) const;
entity get_raw() const;
array_value array::operator[](const int index) const
{
return {this->id_, this->get_value_id(index)};
}
array_value array::operator[](const std::string& key) const
{
return {this->id_, this->get_value_id(key)};
}
private:
unsigned int id_;
};
}

View File

@ -269,4 +269,15 @@ 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;
}
}

View File

@ -34,4 +34,6 @@ 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();
}

View File

@ -1,7 +1,7 @@
#include <stdinc.hpp>
#include "script_value.hpp"
#include "entity.hpp"
#include "array.hpp"
namespace scripting
{
@ -219,7 +219,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 +232,12 @@ namespace scripting
return type == game::SCRIPT_ARRAY;
}
template <>
array script_value::get() const
{
return array(this->get_raw().u.uintValue);
}
/***************************************************************
* Struct
**************************************************************/

View File

@ -63,10 +63,9 @@ namespace scripting
const game::VariableValue& get_raw() const;
variable_value value_{};
private:
template <typename T>
T get() const;
variable_value value_{};
};
}

View File

@ -31,6 +31,7 @@ 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<const float* (const float* v)> Scr_AllocVector{0x565680};