Add function_ptr class for scripting
This commit is contained in:
parent
d320db4ecd
commit
98258080af
@ -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");
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "entity.hpp"
|
||||
#include "array.hpp"
|
||||
#include "animation.hpp"
|
||||
#include "function.hpp"
|
||||
#include "script_value.hpp"
|
||||
|
||||
namespace scripting
|
||||
|
17
src/client/game/scripting/function.cpp
Normal file
17
src/client/game/scripting/function.cpp
Normal 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_;
|
||||
}
|
||||
}
|
16
src/client/game/scripting/function.hpp
Normal file
16
src/client/game/scripting/function.hpp
Normal 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_;
|
||||
};
|
||||
}
|
@ -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());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
**************************************************************/
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user