Add function_ptr class for scripting

This commit is contained in:
Federico Cecchetto 2022-08-02 04:42:26 +02:00
parent d320db4ecd
commit 98258080af
8 changed files with 59 additions and 1 deletions

View File

@ -144,7 +144,7 @@ namespace scripting
throw std::runtime_error("File '" + filename + "' not found");
};
const auto functions = scripting::script_function_table[filename];
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");

View File

@ -3,6 +3,7 @@
#include "entity.hpp"
#include "array.hpp"
#include "animation.hpp"
#include "function.hpp"
#include "script_value.hpp"
namespace scripting

View File

@ -0,0 +1,17 @@
#include <std_include.hpp>
#include "array.hpp"
#include "script_value.hpp"
#include "execution.hpp"
namespace scripting
{
function_ptr::function_ptr(const std::string& file, const std::string& name)
{
this->pos_ = get_function_pos(file, name);
}
const char* function_ptr::get_pos() const
{
return this->pos_;
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "game/game.hpp"
#include "script_value.hpp"
namespace scripting
{
class function_ptr final
{
public:
function_ptr(const std::string& file, const std::string& name);
const char* get_pos() const;
private:
const char* pos_;
};
}

View File

@ -776,6 +776,14 @@ namespace scripting::lua
{
scripting::get_dvar_int_overrides.erase(dvar);
};
auto function_ptr_type = state.new_usertype<function_ptr>("functionptr",
sol::constructors<function_ptr(const std::string&, const std::string&)>());
function_ptr_type["getpos"] = [](const function_ptr& ptr)
{
return reinterpret_cast<uint64_t>(ptr.get_pos());
};
}
}

View File

@ -272,6 +272,11 @@ namespace scripting::lua
return {value.as<animation>()};
}
if (value.is<function_ptr>())
{
return {value.as<function_ptr>()};
}
if (value.is<sol::protected_function>())
{
return convert_function(value);

View File

@ -113,6 +113,15 @@ namespace scripting
this->value_ = variable;
}
script_value::script_value(const function_ptr& value)
{
game::VariableValue variable{};
variable.type = game::SCRIPT_FUNCTION;
variable.u.codePosValue = value.get_pos();
this->value_ = variable;
}
/***************************************************************
* Integer
**************************************************************/

View File

@ -3,6 +3,7 @@
#include "variable_value.hpp"
#include "vector.hpp"
#include "animation.hpp"
#include "function.hpp"
namespace scripting
{
@ -31,6 +32,7 @@ namespace scripting
script_value(const vector& value);
script_value(const animation& value);
script_value(const function_ptr& value);
template <typename T>
bool is() const;