mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2025-07-05 18:51:52 +00:00
Use array class
This commit is contained in:
236
src/game/scripting/array.cpp
Normal file
236
src/game/scripting/array.cpp
Normal file
@ -0,0 +1,236 @@
|
||||
#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)
|
||||
{
|
||||
add_array_value(this->id_, value);
|
||||
}
|
||||
}
|
||||
|
||||
array::array(std::unordered_map<std::string, script_value> values)
|
||||
{
|
||||
this->id_ = make_array();
|
||||
|
||||
for (const auto& value : values)
|
||||
{
|
||||
add_array_key_value(this->id_, 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
|
||||
{
|
||||
add_array_value(this->id_, 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)
|
||||
{
|
||||
add_array_key_value(this->id_, key, {});
|
||||
return this->get_value_id(key);
|
||||
}
|
||||
|
||||
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 = game::GetVariable(this->id_, string_value);
|
||||
|
||||
if (variable_id)
|
||||
{
|
||||
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;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
add_array_key_value(this->id_, key, _value);
|
||||
}
|
||||
|
||||
void array::set(const unsigned int index, const script_value& _value) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
const auto variable_id = game::GetVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
|
||||
if (variable_id)
|
||||
{
|
||||
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;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
add_array_value(this->id_, _value);
|
||||
}
|
||||
|
||||
entity array::get_raw() const
|
||||
{
|
||||
return entity(this->id_);
|
||||
}
|
||||
}
|
62
src/game/scripting/array.hpp
Normal file
62
src/game/scripting/array.hpp
Normal 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_;
|
||||
};
|
||||
}
|
@ -269,4 +269,31 @@ 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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -34,4 +34,8 @@ 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();
|
||||
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);
|
||||
}
|
||||
|
@ -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
|
||||
**************************************************************/
|
||||
|
@ -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_{};
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user