mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2025-04-22 05:35:43 +00:00
Add json functions
This commit is contained in:
parent
4697f6136b
commit
d5e6a15175
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -10,3 +10,6 @@
|
|||||||
[submodule "deps/lua"]
|
[submodule "deps/lua"]
|
||||||
path = deps/lua
|
path = deps/lua
|
||||||
url = https://github.com/lua/lua.git
|
url = https://github.com/lua/lua.git
|
||||||
|
[submodule "deps/json"]
|
||||||
|
path = deps/json
|
||||||
|
url = https://github.com/nlohmann/json.git
|
||||||
|
1
deps/json
vendored
Submodule
1
deps/json
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit e10a3fac8a255433146e3f06a703dc110fc3c3da
|
19
deps/premake/json.lua
vendored
Normal file
19
deps/premake/json.lua
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
json = {
|
||||||
|
source = path.join(dependencies.basePath, "json"),
|
||||||
|
}
|
||||||
|
|
||||||
|
function json.import()
|
||||||
|
json.includes()
|
||||||
|
end
|
||||||
|
|
||||||
|
function json.includes()
|
||||||
|
includedirs {
|
||||||
|
path.join(json.source, "single_include/*")
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function json.project()
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(dependencies, json)
|
@ -11,29 +11,12 @@
|
|||||||
|
|
||||||
namespace io
|
namespace io
|
||||||
{
|
{
|
||||||
namespace
|
|
||||||
{
|
|
||||||
std::string load_path()
|
|
||||||
{
|
|
||||||
const auto fs_basegame = game::Dvar_FindVar("fs_basegame");
|
|
||||||
|
|
||||||
return fs_basegame->current.string;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_path()
|
|
||||||
{
|
|
||||||
static const auto path = load_path();
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class component final : public component_interface
|
class component final : public component_interface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void post_unpack() override
|
void post_unpack() override
|
||||||
{
|
{
|
||||||
const auto path = get_path();
|
const auto path = game::Dvar_FindVar("fs_basegame")->current.string;
|
||||||
std::filesystem::current_path(path);
|
std::filesystem::current_path(path);
|
||||||
|
|
||||||
gsc::function::add("fremove", [](gsc::function_args args)
|
gsc::function::add("fremove", [](gsc::function_args args)
|
||||||
|
212
src/component/json.cpp
Normal file
212
src/component/json.cpp
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
#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 "gsc.hpp"
|
||||||
|
|
||||||
|
#include <json.hpp>
|
||||||
|
|
||||||
|
namespace json
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json gsc_to_json(scripting::script_value value);
|
||||||
|
|
||||||
|
nlohmann::json entity_to_array(unsigned int 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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
game::VariableValue variable{};
|
||||||
|
variable.type = (game::scriptType_e)var.type;
|
||||||
|
variable.u = var.u.u;
|
||||||
|
|
||||||
|
if (!string_indexed)
|
||||||
|
{
|
||||||
|
obj.emplace_back(gsc_to_json(variable));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
obj.emplace(str, gsc_to_json(variable));
|
||||||
|
}
|
||||||
|
|
||||||
|
current = var.nextSibling;
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json vector_to_array(const float* value)
|
||||||
|
{
|
||||||
|
nlohmann::json obj;
|
||||||
|
obj.push_back(value[0]);
|
||||||
|
obj.push_back(value[1]);
|
||||||
|
obj.push_back(value[2]);
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json gsc_to_json(scripting::script_value _value)
|
||||||
|
{
|
||||||
|
const auto variable = _value.get_raw();
|
||||||
|
const auto value = variable.u;
|
||||||
|
const auto type = variable.type;
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case (game::SCRIPT_NONE):
|
||||||
|
return {};
|
||||||
|
case (game::SCRIPT_INTEGER):
|
||||||
|
return value.intValue;
|
||||||
|
case (game::SCRIPT_FLOAT):
|
||||||
|
return value.floatValue;
|
||||||
|
case (game::SCRIPT_STRING):
|
||||||
|
case (game::SCRIPT_ISTRING):
|
||||||
|
return game::SL_ConvertToString(static_cast<unsigned int>(value.stringValue));
|
||||||
|
case (game::SCRIPT_VECTOR):
|
||||||
|
return vector_to_array(value.vectorValue);
|
||||||
|
case (game::SCRIPT_OBJECT):
|
||||||
|
{
|
||||||
|
const auto object_type = game::scr_VarGlob->objectVariableValue[value.uintValue].w.type;
|
||||||
|
|
||||||
|
switch (object_type)
|
||||||
|
{
|
||||||
|
case (game::SCRIPT_STRUCT):
|
||||||
|
return "[struct]";
|
||||||
|
case (game::SCRIPT_ARRAY):
|
||||||
|
return entity_to_array(value.uintValue);
|
||||||
|
default:
|
||||||
|
return "[entity]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case (game::SCRIPT_FUNCTION):
|
||||||
|
return "[function]";
|
||||||
|
default:
|
||||||
|
return "[unknown type]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scripting::script_value json_to_gsc(nlohmann::json obj)
|
||||||
|
{
|
||||||
|
const auto type = obj.type();
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case (nlohmann::detail::value_t::number_integer):
|
||||||
|
case (nlohmann::detail::value_t::number_unsigned):
|
||||||
|
return obj.get<int>();
|
||||||
|
case (nlohmann::detail::value_t::number_float):
|
||||||
|
return obj.get<float>();
|
||||||
|
case (nlohmann::detail::value_t::string):
|
||||||
|
return obj.get<std::string>();
|
||||||
|
case (nlohmann::detail::value_t::array):
|
||||||
|
{
|
||||||
|
const auto arr = make_array();
|
||||||
|
|
||||||
|
for (const auto& [key, value] : obj.items())
|
||||||
|
{
|
||||||
|
add_array_value(arr, json_to_gsc(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return scripting::entity(arr);
|
||||||
|
}
|
||||||
|
case (nlohmann::detail::value_t::object):
|
||||||
|
{
|
||||||
|
const auto arr = make_array();
|
||||||
|
|
||||||
|
for (const auto& [key, value] : obj.items())
|
||||||
|
{
|
||||||
|
add_array_key_value(arr, key, json_to_gsc(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return scripting::entity(arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class component final : public component_interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void post_unpack() override
|
||||||
|
{
|
||||||
|
gsc::function::add("jsonparse", [](gsc::function_args args)
|
||||||
|
{
|
||||||
|
const auto json = args[0].as<std::string>();
|
||||||
|
const auto obj = nlohmann::json::parse(json);
|
||||||
|
|
||||||
|
return json_to_gsc(obj);
|
||||||
|
});
|
||||||
|
|
||||||
|
gsc::function::add("jsonserialize", [](gsc::function_args args)
|
||||||
|
{
|
||||||
|
const auto value = args[0];
|
||||||
|
auto indent = -1;
|
||||||
|
|
||||||
|
if (args.size() > 1)
|
||||||
|
{
|
||||||
|
indent = args[1].as<int>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return gsc_to_json(value).dump(indent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
REGISTER_COMPONENT(json::component)
|
@ -58,7 +58,7 @@ namespace scripting
|
|||||||
throw std::runtime_error("Null pointer");
|
throw std::runtime_error("Null pointer");
|
||||||
}
|
}
|
||||||
|
|
||||||
return reinterpret_cast<T*>(this->get<int>());
|
return reinterpret_cast<T*>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const game::VariableValue& get_raw() const;
|
const game::VariableValue& get_raw() const;
|
||||||
|
@ -9,6 +9,7 @@ namespace game
|
|||||||
WEAK symbol<void(int type, VariableUnion u)> AddRefToValue{0x5656E0};
|
WEAK symbol<void(int type, VariableUnion u)> AddRefToValue{0x5656E0};
|
||||||
WEAK symbol<void(unsigned int id)> AddRefToObject{0x5655F0};
|
WEAK symbol<void(unsigned int id)> AddRefToObject{0x5655F0};
|
||||||
WEAK symbol<unsigned int(unsigned int id)> AllocThread{0x565580};
|
WEAK symbol<unsigned int(unsigned int id)> AllocThread{0x565580};
|
||||||
|
WEAK symbol<ObjectVariableValue*(unsigned int* id)> AllocVariable{0x565430};
|
||||||
WEAK symbol<unsigned int()> AllocObject{0x565530};
|
WEAK symbol<unsigned int()> AllocObject{0x565530};
|
||||||
WEAK symbol<void(int type, VariableUnion u)> RemoveRefToValue{0x565730};
|
WEAK symbol<void(int type, VariableUnion u)> RemoveRefToValue{0x565730};
|
||||||
WEAK symbol<void(unsigned int id)> RemoveRefToObject{0x5681E0};
|
WEAK symbol<void(unsigned int id)> RemoveRefToObject{0x5681E0};
|
||||||
@ -28,6 +29,8 @@ namespace game
|
|||||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindVariable{0x5651F0};
|
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindVariable{0x5651F0};
|
||||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindObject{0x565BD0};
|
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)> GetVariable{0x5663E0};
|
||||||
|
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> GetNewVariable{0x566390};
|
||||||
|
WEAK symbol<void(unsigned int parentId, unsigned int id, VariableValue* value)> SetNewVariableValue{0x5658D0};
|
||||||
|
|
||||||
WEAK symbol<const float* (const float* v)> Scr_AllocVector{0x565680};
|
WEAK symbol<const float* (const float* v)> Scr_AllocVector{0x565680};
|
||||||
WEAK symbol<void()> Scr_ClearOutParams{0x569010};
|
WEAK symbol<void()> Scr_ClearOutParams{0x569010};
|
||||||
@ -36,6 +39,12 @@ namespace game
|
|||||||
WEAK symbol<void(int id, unsigned int stringValue, unsigned int paramcount)> Scr_NotifyId{0x56B5E0};
|
WEAK symbol<void(int id, unsigned int stringValue, unsigned int paramcount)> Scr_NotifyId{0x56B5E0};
|
||||||
WEAK symbol<int(const char* filename, unsigned int str)> Scr_GetFunctionHandle{0x5618A0};
|
WEAK symbol<int(const char* filename, unsigned int str)> Scr_GetFunctionHandle{0x5618A0};
|
||||||
WEAK symbol<unsigned int(int handle, unsigned int objId, unsigned int paramcount)> Scr_ExecThreadInternal{0x56E1C0};
|
WEAK symbol<unsigned int(int handle, unsigned int objId, unsigned int paramcount)> Scr_ExecThreadInternal{0x56E1C0};
|
||||||
|
WEAK symbol<unsigned int(int entnum, unsigned int classnum)> Scr_GetEntityId{0x567D80};
|
||||||
|
WEAK symbol<unsigned int(int entnum, unsigned int classnum)> Scr_AddEntityNum{0x56ABC0};
|
||||||
|
WEAK symbol<void()> Scr_AddArray{0x56AE30};
|
||||||
|
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<unsigned int(const char* str, unsigned int user)> SL_GetString{0x5649E0};
|
WEAK symbol<unsigned int(const char* str, unsigned int user)> SL_GetString{0x5649E0};
|
||||||
WEAK symbol<const char*(unsigned int stringValue)> SL_ConvertToString{0x564270};
|
WEAK symbol<const char*(unsigned int stringValue)> SL_ConvertToString{0x564270};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user