Print errors

This commit is contained in:
Federico Cecchetto 2021-11-08 01:36:48 +01:00
parent eee68760e4
commit 640fb032ea
9 changed files with 191 additions and 50 deletions

View File

@ -14,32 +14,6 @@
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;
@ -105,7 +79,7 @@ namespace gsc
auto token_map_start = 0x8000;
auto field_offset_start = 0xA000;
struct field
struct entity_field
{
std::string name;
std::function<scripting::script_value(unsigned int entnum)> getter;
@ -113,7 +87,7 @@ namespace gsc
};
std::vector<std::function<void()>> post_load_callbacks;
std::unordered_map<unsigned int, std::unordered_map<unsigned int, field>> custom_fields;
std::unordered_map<unsigned int, std::unordered_map<unsigned int, entity_field>> custom_fields;
void call_function(unsigned int id)
{
@ -135,8 +109,8 @@ namespace gsc
catch (const std::exception& e)
{
printf("************** Script execution error **************\n");
printf("Error executing function %s\n", function_name(id).data());
printf("%s\n", e.what());
printf("Error executing function %s:\n", function_name(id).data());
printf(" %s\n", e.what());
printf("****************************************************\n");
}
}
@ -161,8 +135,8 @@ namespace gsc
catch (const std::exception& e)
{
printf("************** Script execution error **************\n");
printf("Error executing method %s\n", method_name(id).data());
printf("%s\n", e.what());
printf("Error executing method %s:\n", method_name(id).data());
printf(" %s\n", e.what());
printf("****************************************************\n");
}
}
@ -230,8 +204,8 @@ namespace gsc
catch (const std::exception& e)
{
printf("************** Script execution error **************\n");
printf("Error getting field %s\n", field.name.data());
printf("%s\n", e.what());
printf("Error getting field %s:\n", field.name.data());
printf(" %s\n", e.what());
printf("****************************************************\n");
}
}
@ -254,8 +228,8 @@ namespace gsc
catch (const std::exception& e)
{
printf("************** Script execution error **************\n");
printf("Error setting field %s\n", field.name.data());
printf("%s\n", e.what());
printf("Error setting field %s:\n", field.name.data());
printf(" %s\n", e.what());
printf("****************************************************\n");
}
}
@ -314,6 +288,31 @@ 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::value_wrap function_args::get(const int index) const
{
if (index >= this->values_.size())
{
throw std::runtime_error(utils::string::va("parameter %d does not exist", index));
}
return {this->values_[index], index};
}
class component final : public component_interface
{
public:
@ -323,7 +322,7 @@ namespace gsc
scr_set_object_field_hook.create(0x52BCC0, scr_set_object_field_stub);
scr_post_load_scripts_hook.create(0x628B50, scr_post_load_scripts_stub);
gsc::field::add(classid::entity, "flags",
field::add(classid::entity, "flags",
[](unsigned int entnum) -> scripting::script_value
{
const auto entity = &game::g_entities[entnum];
@ -336,7 +335,7 @@ namespace gsc
}
);
gsc::field::add(classid::entity, "clientflags",
field::add(classid::entity, "clientflags",
[](unsigned int entnum) -> scripting::script_value
{
const auto entity = &game::g_entities[entnum];

View File

@ -18,9 +18,9 @@ namespace gsc
unsigned int size() const;
std::vector<scripting::script_value> get_raw() const;
scripting::script_value get(const int index) const;
scripting::value_wrap get(const int index) const;
scripting::script_value operator[](const int index) const
scripting::value_wrap operator[](const int index) const
{
return this->get(index);
}

View File

@ -1,5 +1,6 @@
#include <stdinc.hpp>
#include "array.hpp"
#include "script_value.hpp"
#include "execution.hpp"
namespace scripting

View File

@ -1,4 +1,5 @@
#pragma once
#include "game/game.hpp"
#include "script_value.hpp"
namespace scripting

View File

@ -1,6 +1,8 @@
#pragma once
#include "game/game.hpp"
#include "entity.hpp"
#include "array.hpp"
#include "function.hpp"
#include "script_value.hpp"
namespace scripting

View File

@ -23,7 +23,7 @@ namespace scripting
return this->pos_;
}
script_value function::call(entity self, std::vector<script_value> arguments) const
script_value function::call(const entity& self, std::vector<script_value> arguments) const
{
return exec_ent_thread(self, this->pos_, arguments);
}

View File

@ -12,9 +12,9 @@ namespace scripting
script_value get_raw() const;
const char* get_pos() const;
script_value call(entity self, std::vector<script_value> arguments) const;
script_value call(const entity& self, std::vector<script_value> arguments) const;
script_value operator()(entity self, std::vector<script_value> arguments) const
script_value operator()(const entity& self, std::vector<script_value> arguments) const
{
return this->call(self, arguments);
}

View File

@ -15,6 +15,11 @@ namespace scripting
{
}
script_value::script_value(const value_wrap& value)
: value_(value.get_raw())
{
}
script_value::script_value(void* value)
{
game::VariableValue variable{};
@ -315,4 +320,10 @@ namespace scripting
{
return this->value_.get();
}
value_wrap::value_wrap(const scripting::script_value& value, int argument_index)
: value_(value)
, argument_index_(argument_index)
{
}
}

View File

@ -8,12 +8,95 @@ namespace scripting
class entity;
class array;
class function;
class value_wrap;
namespace
{
std::unordered_map<int, std::string> typenames =
{
{0, "undefined"},
{1, "object"},
{2, "string"},
{3, "localized string"},
{4, "vector"},
{5, "float"},
{6, "integer"},
{7, "codepos"},
{8, "precodepos"},
{9, "function"},
{10, "builtin function"},
{11, "builtin method"},
{12, "stack"},
{13, "animation"},
{14, "pre animation"},
{15, "thread"},
{16, "thread"},
{17, "thread"},
{18, "thread"},
{19, "struct"},
{20, "removed entity"},
{21, "entity"},
{22, "array"},
{23, "removed thread"},
{24, "count"},
{25, "<free>"},
{26, "thread list"},
{27, "endon list"},
};
std::string get_typename(const game::VariableValue& value)
{
if (value.type == game::SCRIPT_OBJECT)
{
const auto type = game::scr_VarGlob->objectVariableValue[value.u.uintValue].w.type;
return typenames[type];
}
else
{
return typenames[value.type];
}
}
template <typename T, typename A = array>
std::string get_c_typename()
{
auto& info = typeid(T);
if (info == typeid(std::string))
{
return "string";
}
if (info == typeid(const char*))
{
return "string";
}
if (info == typeid(entity))
{
return "entity";
}
if (info == typeid(array))
{
return "array";
}
if (info == typeid(vector))
{
return "vector";
}
return info.name();
}
}
class script_value
{
public:
script_value() = default;
script_value(const game::VariableValue& value);
script_value(const value_wrap& value);
script_value(void* value);
@ -43,7 +126,9 @@ namespace scripting
{
if (!this->is<T>())
{
throw std::runtime_error("Invalid type");
const auto type = get_typename(this->get_raw());
const auto c_type = get_c_typename<T>();
throw std::runtime_error(std::string("has type '" + type + "' but should be '" + c_type + "'"));
}
return get<T>();
@ -52,16 +137,12 @@ namespace scripting
template <typename T, typename I = int>
T* as_ptr()
{
if (!this->is<I>())
{
throw std::runtime_error("Invalid type");
}
const auto value = this->get<I>();
const auto value = this->as<I>();
if (!value)
{
throw std::runtime_error("Null pointer");
throw std::runtime_error("is null");
}
return reinterpret_cast<T*>(value);
@ -74,4 +155,50 @@ namespace scripting
template <typename T>
T get() const;
};
class value_wrap
{
public:
value_wrap(const scripting::script_value& value, int argument_index);
template <typename T>
T as() const
{
try
{
return this->value_.as<T>();
}
catch (const std::exception& e)
{
throw std::runtime_error(utils::string::va("parameter %d %s", this->argument_index_, e.what()));
}
}
template <typename T, typename I = int>
T* as_ptr()
{
try
{
return this->value_.as_ptr<T>();
}
catch (const std::exception& e)
{
throw std::runtime_error(utils::string::va("parameter %d %s", this->argument_index_, e.what()));
}
}
template <typename T>
T is() const
{
return this->value_.is<T>();
}
const game::VariableValue& get_raw() const
{
return this->value_.get_raw();
}
int argument_index_{};
scripting::script_value value_;
};
}