diff options
author | Vittorio Romeo <vittorio.romeo@outlook.com> | 2020-07-29 00:31:10 +0100 |
---|---|---|
committer | Vittorio Romeo <vittorio.romeo@outlook.com> | 2020-07-29 00:31:10 +0100 |
commit | 506938a02ecbb0b878a6610608e2fe86fda2e948 (patch) | |
tree | 770d11727558f5fb007223c222fac45ace1d9ece | |
parent | 18a0c57398b4413bac34556c99bd9e6de709d867 (diff) |
Optimize luawip_arcadia
-rw-r--r-- | _RELEASE/Packs/experimental/Scripts/Levels/cw.lua | 1 | ||||
-rw-r--r-- | _RELEASE/config.json | 222 | ||||
-rw-r--r-- | include/SSVOpenHexagon/Utils/LuaMetadataProxy.hpp | 29 | ||||
-rw-r--r-- | include/SSVOpenHexagon/Utils/LuaWrapper.hpp | 231 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/HGScripting.cpp | 36 |
5 files changed, 288 insertions, 231 deletions
diff --git a/_RELEASE/Packs/experimental/Scripts/Levels/cw.lua b/_RELEASE/Packs/experimental/Scripts/Levels/cw.lua index 0b529ba2..9e6a0b49 100644 --- a/_RELEASE/Packs/experimental/Scripts/Levels/cw.lua +++ b/_RELEASE/Packs/experimental/Scripts/Levels/cw.lua @@ -101,7 +101,6 @@ function onStep() end cwHandle = cw_create() - -- u_log("Created handle " .. cwHandle) if math.random() > 0.5 then x = math.random(-600, 600) diff --git a/_RELEASE/config.json b/_RELEASE/config.json index f05a2470..b4205304 100644 --- a/_RELEASE/config.json +++ b/_RELEASE/config.json @@ -1,111 +1,111 @@ - -{ - "3D_enabled" : true, - "3D_max_depth" : 100, - "3D_multiplier" : 1.0, - "antialiasing_level" : 3, - "auto_restart" : false, - "auto_zoom_factor" : true, - "beatpulse_enabled" : true, - "black_and_white" : false, - "darken_uneven_background_chunk" : true, - "debug" : true, - "draw_text_outlines" : true, - "first_time_playing" : false, - "flash_enabled" : true, - "fullscreen" : false, - "fullscreen_auto_resolution" : true, - "fullscreen_height" : 1080, - "fullscreen_width" : 3840, - "invincible" : true, - "joystick_deadzone" : 5.0, - "key_icons_scale" : 0.7500000596046448, - "limit_fps" : true, - "max_fps" : 200, - "mouse_visible" : false, - "music_speed_dm_sync" : true, - "music_speed_mult" : 1.0, - "music_volume" : 30.0, - "no_background" : false, - "no_music" : false, - "no_rotation" : false, - "no_sound" : false, - "official" : false, - "online" : true, - "pixel_multiplier" : 1, - "player_focus_speed" : 4.6250, - "player_size" : 7.300000190734863, - "player_speed" : 9.449999809265137, - "pulse_enabled" : true, - "rotate_to_start" : true, - "server_local" : true, - "server_verbose" : true, - "show_fps" : true, - "show_key_icons" : true, - "show_messages" : true, - "show_tracked_variables" : true, - "sound_volume" : 75.0, - "t_down" : - [ - [ "kDown" ], - [ "kS" ] - ], - "t_exit" : - [ - [ "kEscape" ] - ], - "t_focus" : - [ - [ "kLShift" ], - [ "bXButton1" ] - ], - "t_force_restart" : - [ - [ "kR" ], - [ "bXButton2" ] - ], - "t_replay" : - [ - [ "kY" ] - ], - "t_restart" : - [ - [ "kReturn" ], - [ "bMiddle" ] - ], - "t_rotate_ccw" : - [ - [ "kLeft" ], - [ "kA" ], - [ "bLeft" ] - ], - "t_rotate_cw" : - [ - [ "kRight" ], - [ "kD" ], - [ "bRight" ] - ], - "t_screenshot" : - [ - [ "kF12" ] - ], - "t_swap" : - [ - [ "kSpace" ], - [ "bMiddle" ] - ], - "t_up" : - [ - [ "kUp" ], - [ "kW" ] - ], - "text_padding" : 8.0, - "text_scaling" : 0.9999999403953552, - "timer_static" : true, - "timescale" : 1.000000119209290, - "vsync" : false, - "windowed_auto_resolution" : false, - "windowed_height" : 900, - "windowed_width" : 1600, - "zoom_factor" : 0.8533333539962769 -} +
+{
+ "3D_enabled" : true,
+ "3D_max_depth" : 100,
+ "3D_multiplier" : 1.0,
+ "antialiasing_level" : 3,
+ "auto_restart" : false,
+ "auto_zoom_factor" : true,
+ "beatpulse_enabled" : true,
+ "black_and_white" : false,
+ "darken_uneven_background_chunk" : true,
+ "debug" : true,
+ "draw_text_outlines" : true,
+ "first_time_playing" : false,
+ "flash_enabled" : true,
+ "fullscreen" : false,
+ "fullscreen_auto_resolution" : true,
+ "fullscreen_height" : 1080,
+ "fullscreen_width" : 1920,
+ "invincible" : true,
+ "joystick_deadzone" : 5.0,
+ "key_icons_scale" : 0.7500000596046448,
+ "limit_fps" : true,
+ "max_fps" : 200,
+ "mouse_visible" : false,
+ "music_speed_dm_sync" : true,
+ "music_speed_mult" : 1.0,
+ "music_volume" : 30.0,
+ "no_background" : false,
+ "no_music" : false,
+ "no_rotation" : false,
+ "no_sound" : false,
+ "official" : true,
+ "online" : true,
+ "pixel_multiplier" : 1,
+ "player_focus_speed" : 4.6250,
+ "player_size" : 7.300000190734863,
+ "player_speed" : 9.449999809265137,
+ "pulse_enabled" : true,
+ "rotate_to_start" : true,
+ "server_local" : true,
+ "server_verbose" : true,
+ "show_fps" : true,
+ "show_key_icons" : true,
+ "show_messages" : true,
+ "show_tracked_variables" : true,
+ "sound_volume" : 75.0,
+ "t_down" :
+ [
+ [ "kDown" ],
+ [ "kS" ]
+ ],
+ "t_exit" :
+ [
+ [ "kEscape" ]
+ ],
+ "t_focus" :
+ [
+ [ "kLShift" ],
+ [ "bXButton1" ]
+ ],
+ "t_force_restart" :
+ [
+ [ "kR" ],
+ [ "bXButton2" ]
+ ],
+ "t_replay" :
+ [
+ [ "kY" ]
+ ],
+ "t_restart" :
+ [
+ [ "kReturn" ],
+ [ "bMiddle" ]
+ ],
+ "t_rotate_ccw" :
+ [
+ [ "kLeft" ],
+ [ "kA" ],
+ [ "bLeft" ]
+ ],
+ "t_rotate_cw" :
+ [
+ [ "kRight" ],
+ [ "kD" ],
+ [ "bRight" ]
+ ],
+ "t_screenshot" :
+ [
+ [ "kF12" ]
+ ],
+ "t_swap" :
+ [
+ [ "kSpace" ],
+ [ "bMiddle" ]
+ ],
+ "t_up" :
+ [
+ [ "kUp" ],
+ [ "kW" ]
+ ],
+ "text_padding" : 8.0,
+ "text_scaling" : 0.9999999403953552,
+ "timer_static" : true,
+ "timescale" : 0.6000000238418579,
+ "vsync" : false,
+ "windowed_auto_resolution" : false,
+ "windowed_height" : 900,
+ "windowed_width" : 1600,
+ "zoom_factor" : 0.8533333539962769
+}
diff --git a/include/SSVOpenHexagon/Utils/LuaMetadataProxy.hpp b/include/SSVOpenHexagon/Utils/LuaMetadataProxy.hpp index 243f28d0..062b32b7 100644 --- a/include/SSVOpenHexagon/Utils/LuaMetadataProxy.hpp +++ b/include/SSVOpenHexagon/Utils/LuaMetadataProxy.hpp @@ -12,6 +12,7 @@ #include <string> #include <vector> #include <utility> +#include <type_traits> namespace hg::Utils { @@ -29,55 +30,57 @@ private: template <typename T> [[nodiscard]] constexpr static const char* typeToStr() noexcept { - if constexpr(std::is_same_v<T, void>) + using Type = std::decay_t<T>; + + if constexpr(std::is_same_v<Type, void>) { return "void"; } - else if constexpr(std::is_same_v<T, bool>) + else if constexpr(std::is_same_v<Type, bool>) { return "bool"; } - else if constexpr(std::is_same_v<T, int>) + else if constexpr(std::is_same_v<Type, int>) { return "int"; } - else if constexpr(std::is_same_v<T, float>) + else if constexpr(std::is_same_v<Type, float>) { return "float"; } - else if constexpr(std::is_same_v<T, double>) + else if constexpr(std::is_same_v<Type, double>) { return "double"; } - else if constexpr(std::is_same_v<T, std::string>) + else if constexpr(std::is_same_v<Type, std::string>) { return "string"; } - else if constexpr(std::is_same_v<T, unsigned int>) + else if constexpr(std::is_same_v<Type, unsigned int>) { return "unsigned int"; } - else if constexpr(std::is_same_v<T, long>) + else if constexpr(std::is_same_v<Type, long>) { return "long"; } - else if constexpr(std::is_same_v<T, unsigned long>) + else if constexpr(std::is_same_v<Type, unsigned long>) { return "unsigned long"; } - else if constexpr(std::is_same_v<T, long long>) + else if constexpr(std::is_same_v<Type, long long>) { return "long long"; } - else if constexpr(std::is_same_v<T, unsigned long long>) + else if constexpr(std::is_same_v<Type, unsigned long long>) { return "unsigned long long"; } - else if constexpr(std::is_same_v<T, std::size_t>) + else if constexpr(std::is_same_v<Type, std::size_t>) { return "size_t"; } - else if constexpr(std::is_same_v<T, std::tuple<float, float>>) + else if constexpr(std::is_same_v<Type, std::tuple<float, float>>) { return "tuple<float, float>"; } diff --git a/include/SSVOpenHexagon/Utils/LuaWrapper.hpp b/include/SSVOpenHexagon/Utils/LuaWrapper.hpp index a2c586a5..3f6179f2 100644 --- a/include/SSVOpenHexagon/Utils/LuaWrapper.hpp +++ b/include/SSVOpenHexagon/Utils/LuaWrapper.hpp @@ -37,9 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <algorithm> #include <cassert> #include <cstring> -#include <functional> #include <limits> -#include <list> #include <map> #include <memory> #include <stdexcept> @@ -57,41 +55,46 @@ extern "C" namespace Lua { + template <typename> struct RemoveMemberPtr; + template <typename TReturn, typename TThis, typename... TArgs> struct RemoveMemberPtr<TReturn (TThis::*)(TArgs...) const> { using Type = TReturn(TArgs...); }; -template <typename> -struct FnTupleWrapper; + template <typename FnType> struct FnTupleWrapper { }; + template <typename... TArgs> struct FnTupleWrapper<void(TArgs...)> { static constexpr int count{sizeof...(TArgs)}; using ParamsType = std::tuple<TArgs...>; + template <typename T> - static std::tuple<> call(const T& fn, ParamsType mTpl) + constexpr static std::tuple<> call(T&& fn, ParamsType&& mTpl) { - std::apply(fn, mTpl); + std::apply(std::forward<T>(fn), std::move(mTpl)); return {}; } }; + template <typename R, typename... TArgs> struct FnTupleWrapper<R(TArgs...)> { static constexpr int count{sizeof...(TArgs)}; using ParamsType = std::tuple<TArgs...>; + template <typename T> - static std::tuple<R> call(const T& fn, ParamsType mTpl) + constexpr static std::tuple<R> call(T&& fn, ParamsType&& mTpl) { - return std::tuple<R>{std::apply(fn, mTpl)}; + return std::tuple<R>{std::apply(std::forward<T>(fn), std::move(mTpl))}; } }; @@ -128,28 +131,39 @@ public: free(ptr); return nullptr; } + return realloc(ptr, nsize); } }; // lua_newstate can return null if allocation failed _state = lua_newstate(&Allocator::allocator, nullptr); - if(_state == nullptr) throw std::bad_alloc(); + if(_state == nullptr) + { + throw std::bad_alloc(); + } // opening default library if required to do so - if(openDefaultLibs) luaL_openlibs(_state); + if(openDefaultLibs) + { + luaL_openlibs(_state); + } } + LuaContext(const LuaContext&) = delete; LuaContext& operator=(const LuaContext&) = delete; + LuaContext(LuaContext&& s) : _state(s._state) { s._state = nullptr; } + LuaContext& operator=(LuaContext&& s) { std::swap(_state, s._state); return *this; } + ~LuaContext() { if(_state != nullptr) lua_close(_state); @@ -170,6 +184,7 @@ public: { } }; + /// \brief Generated by readVariable or isVariableArray when the asked /// variable doesn't exist/is nil class VariableDoesntExistException : public std::runtime_error @@ -183,6 +198,7 @@ public: { } }; + /// \brief Thrown when a syntax error happens in a lua script class SyntaxErrorException : public std::runtime_error { @@ -192,6 +208,7 @@ public: { } }; + /// \brief Thrown when trying to cast a lua variable to an invalid type class WrongTypeException : public std::runtime_error { @@ -203,7 +220,6 @@ public: } }; - /// \brief Executes lua code from the stream \param code A stream that /// lua will read its code from void executeCode(std::istream& code) @@ -244,31 +260,38 @@ public: template <typename T, typename R, typename... Args> void registerFunction(const std::string& name, R (T::*f)(Args...)) { - _registerFunction(name, [f](std::shared_ptr<T> ptr, Args... args) { - return ((*ptr).*f)(args...); - }); + _registerFunction( + name, [f](const std::shared_ptr<T>& ptr, Args... args) { + return ((*ptr).*f)(args...); + }); } + template <typename T, typename R, typename... Args> void registerFunction(const std::string& name, R (T::*f)(Args...) const) { - _registerFunction(name, [f](std::shared_ptr<T> ptr, Args... args) { - return ((*ptr).*f)(args...); - }); + _registerFunction( + name, [f](const std::shared_ptr<T>& ptr, Args... args) { + return ((*ptr).*f)(args...); + }); } + template <typename T, typename R, typename... Args> void registerFunction(const std::string& name, R (T::*f)(Args...) volatile) { - _registerFunction(name, [f](std::shared_ptr<T> ptr, Args... args) { - return ((*ptr).*f)(args...); - }); + _registerFunction( + name, [f](const std::shared_ptr<T>& ptr, Args... args) { + return ((*ptr).*f)(args...); + }); } + template <typename T, typename R, typename... Args> void registerFunction( const std::string& name, R (T::*f)(Args...) const volatile) { - _registerFunction(name, [f](std::shared_ptr<T> ptr, Args... args) { - return ((*ptr).*f)(args...); - }); + _registerFunction( + name, [f](const std::shared_ptr<T>& ptr, Args... args) { + return ((*ptr).*f)(args...); + }); } /// \brief Adds a custom function to a type determined using the @@ -277,9 +300,9 @@ public: /// \param fn Function which takes as first parameter a std::shared_ptr template <typename T> void registerFunction( - const std::string& name, T fn, decltype(&T::operator())* = nullptr) + const std::string& name, T&& fn, decltype(&T::operator())* = nullptr) { - _registerFunction(name, fn); + _registerFunction(name, std::forward<T>(fn)); } /// \brief Inverse operation of registerFunction @@ -288,6 +311,7 @@ public: { _unregisterFunction<T>(name); } + /// \brief Calls a function stored in a lua variable /// \details Template parameter of the function should be the expected /// return type (tuples and void are supported) @@ -305,7 +329,8 @@ public: bool isVariableArray(const std::string& mVarName) const { _getGlobal(mVarName); - bool answer = lua_istable(_state, -1); + + const bool answer = lua_istable(_state, -1); lua_pop(_state, 1); return answer; } @@ -324,7 +349,8 @@ public: bool doesVariableExist(const std::string& mVarName) const { _getGlobal(mVarName); - bool answer = lua_isnil(_state, -1); + + const bool answer = lua_isnil(_state, -1); lua_pop(_state, 1); return !answer; } @@ -346,11 +372,16 @@ public: _getGlobal(mVarName); return _readTopAndPop(1, (T*)nullptr); } + /// \brief template <typename T> bool readVariableIfExists(const std::string& mVarName, T& out) { - if(!doesVariableExist(mVarName)) return false; + if(!doesVariableExist(mVarName)) + { + return false; + } + out = readVariable<T>(mVarName); return true; } @@ -364,7 +395,9 @@ public: { static_assert(!std::is_same<typename std::tuple<T>, T>::value, "Error: you can't use LuaContext::writeVariable with a tuple"); + const int pushedElems = _push(std::forward<T>(data)); + try { _setGlobal(mVarName); @@ -374,6 +407,7 @@ public: lua_pop(_state, pushedElems - 1); throw; } + lua_pop(_state, pushedElems - 1); } @@ -555,10 +589,11 @@ private: // function names and values are functions // (where type is the first parameter of the functor) template <typename T> - void _registerFunction(const std::string& name, T function) + void _registerFunction(const std::string& name, T&& function) { typedef typename RemoveMemberPtr<decltype(&T::operator())>::Type FunctionType; + typedef typename std::tuple_element<0, typename FnTupleWrapper<FunctionType>::ParamsType>::type:: element_type ObjectType; @@ -566,6 +601,7 @@ private: // trying to get the existing functions list lua_pushlightuserdata( _state, const_cast<std::type_info*>(&typeid(ObjectType))); + lua_gettable(_state, LUA_REGISTRYINDEX); // if it doesn't exist, we create one, then write it in registry but @@ -584,7 +620,7 @@ private: // now we have our functions list on top of the stack, we write the // function here lua_pushstring(_state, name.c_str()); - _push(std::move(function)); + _push(std::forward<T>(function)); lua_settable(_state, -3); lua_pop(_state, 1); } @@ -737,7 +773,9 @@ private: static_assert(std::tuple_size<In>::value >= 0, "Error: template parameter 'In' should be a tuple"); - int outArguments{0}, inArguments{0}; + int outArguments{0}; + int inArguments{0}; + try { // we push the parameters on the stack @@ -788,10 +826,12 @@ public: { public: Table() = default; + Table(Table&& t) { swap(t, *this); } + Table& operator=(Table&& t) { swap(t, *this); @@ -799,9 +839,9 @@ public: } template <typename... Args> - explicit Table(Args... args) + explicit Table(Args&&... args) { - insert(args...); + insert(std::forward<Args>(args)...); } friend void swap(Table& a, Table& b) @@ -810,13 +850,15 @@ public: } template <typename Key, typename Value, typename... Args> - void insert(Key k, Value v, Args... args) + void insert(Key&& k, Value&& v, Args&&... args) { - typedef typename ToPushableType<Key>::type RKey; - typedef typename ToPushableType<Value>::type RValue; - _elements.emplace_back( - new Element<RKey, RValue>(std::move(k), std::move(v))); - insert(args...); + typedef typename ToPushableType<std::decay_t<Key>>::type RKey; + typedef typename ToPushableType<std::decay_t<Value>>::type RValue; + + _elements.emplace_back(new Element<RKey, RValue>( + std::forward<Key>(k), std::forward<Value>(v))); + + insert(std::forward<Args>(args)...); } void insert() @@ -834,7 +876,8 @@ public: if(element != nullptr && element->key == key) return element->value; } - throw(VariableDoesntExistException("<key in table>")); + + throw VariableDoesntExistException("<key in table>"); } private: @@ -849,6 +892,7 @@ public: virtual ~ElementBase() { } + virtual void push(LuaContext&) const = 0; }; @@ -858,7 +902,9 @@ public: { Key key; Value value; - Element(Key k, Value v) : key(std::move(k)), value(std::move(v)) + + Element(Key&& k, Value&& v) + : key(std::forward<Key>(k)), value(std::forward<Value>(v)) { } @@ -876,20 +922,25 @@ public: int _push(LuaContext& ctxt) const { lua_newtable(ctxt._state); + try { - for(auto& i : _elements) i->push(ctxt); + for(auto& i : _elements) + { + i->push(ctxt); + } } catch(...) { lua_pop(ctxt._state, 1); throw; } + return 1; } // elements storage - std::list<std::unique_ptr<ElementBase>> _elements; + std::vector<std::unique_ptr<ElementBase>> _elements; }; private: @@ -905,16 +956,19 @@ private: { return 0; } + int _push(bool v) { lua_pushboolean(_state, v); return 1; } + int _push(const std::string& s) { lua_pushstring(_state, s.c_str()); return 1; } + int _push(const char* s) { lua_pushstring(_state, s); @@ -995,53 +1049,53 @@ private: // we will create a userdata which contains a copy of a lambda // function [](lua_State*) -> int // but first we have to create it - std::function<int(lua_State*)> functionToPush( - [this, fn](lua_State* state) { - // note that I'm using "this->" because of g++, - // I don't know if it is required by standards or if it is a - // bug - assert(this->_state == state); - - // FnTupleWrapper<FnType> is a specialized template - // structure which defines - // "ParamsType", "ReturnType" and "call" - // the first two correspond to the params list and return - // type as tuples - // and "call" is a static function which will call a - // function - // of this type using parameters passed as a tuple - using TupledFunction = FnTupleWrapper<FnType>; - - // checking if number of parameters is correct - const int paramsCount = TupledFunction::count; - if(lua_gettop(state) < paramsCount) - { - // if not, using lua_error to return an error - luaL_where(state, 1); - lua_pushstring(state, "this function requires at least "); - lua_pushnumber(state, paramsCount); - lua_pushstring(state, " parameter(s)"); - lua_concat(state, 4); - return lua_error(state); // lua_error throws an - // exception when compiling as - // C++ - } + auto functionToPush([this, fn](lua_State* state) { + // note that I'm using "this->" because of g++, + // I don't know if it is required by standards or if it is a + // bug + assert(this->_state == state); + + // FnTupleWrapper<FnType> is a specialized template + // structure which defines + // "ParamsType", "ReturnType" and "call" + // the first two correspond to the params list and return + // type as tuples + // and "call" is a static function which will call a + // function + // of this type using parameters passed as a tuple + using TupledFunction = FnTupleWrapper<FnType>; + + // checking if number of parameters is correct + constexpr int paramsCount = TupledFunction::count; + if(lua_gettop(state) < paramsCount) + { + // if not, using lua_error to return an error + luaL_where(state, 1); + lua_pushstring(state, "this function requires at least "); + lua_pushnumber(state, paramsCount); + lua_pushstring(state, " parameter(s)"); + lua_concat(state, 4); + return lua_error(state); // lua_error throws an + // exception when compiling as + // C++ + } // reading parameters from the stack #ifdef _MSC_VER - auto parameters = this->_read(-paramsCount, - static_cast<TupledFunction::ParamsType*>(nullptr)); + auto parameters = this->_read(-paramsCount, + static_cast<TupledFunction::ParamsType*>(nullptr)); #else - auto parameters = this->_read(-paramsCount, - static_cast<typename TupledFunction::ParamsType*>(nullptr)); + auto parameters = this->_read(-paramsCount, + static_cast<typename TupledFunction::ParamsType*>(nullptr)); #endif - // calling the function, note that "result" should be a - // tuple - auto result = TupledFunction::call(fn, std::move(parameters)); - // pushing the result on the stack and returning number of - // pushed elements - return this->_push(std::move(result)); - }); + // calling the function, note that "result" should be a + // tuple + auto result = TupledFunction::call(fn, std::move(parameters)); + + // pushing the result on the stack and returning number of + // pushed elements + return this->_push(std::move(result)); + }); // typedefing the type of data we will push typedef decltype(functionToPush) FunctionPushType; @@ -1110,7 +1164,7 @@ private: // conversion operator // with C++0x, this bool operator will certainly be declared explicit template <typename T> - int _push(std::unique_ptr<T> mObj) + int _push(std::unique_ptr<T>&& mObj) { return _push(std::shared_ptr<T>(std::move(mObj))); } @@ -1121,7 +1175,7 @@ private: // to the corresponding // table in the registry (see _registerFunction) template <typename T> - int _push(std::shared_ptr<T> mObj) + int _push(std::shared_ptr<T>&& mObj) { // this is a structure providing static C-like functions that we can // feed to lua @@ -1223,6 +1277,7 @@ private: static_cast<std::integral_constant<int, N - 1>*>(nullptr)) + _push(std::get<N - 1>(t)); } + template <typename... Args, int N = sizeof...(Args)> int _push(const std::tuple<Args...>&, std::integral_constant<int, N>* = nullptr, @@ -1375,7 +1430,7 @@ return table;*/ return [&]<int... Is>(std::integer_sequence<int, Is...>) { return std::make_tuple( - _read(index + Is, static_cast<Ts*>(nullptr))...); + _read(index + Is, static_cast<std::decay_t<Ts>*>(nullptr))...); } (std::make_integer_sequence<int, sizeof...(Ts)>{}); } diff --git a/src/SSVOpenHexagon/Core/HGScripting.cpp b/src/SSVOpenHexagon/Core/HGScripting.cpp index 14ecee2e..db41b47a 100644 --- a/src/SSVOpenHexagon/Core/HGScripting.cpp +++ b/src/SSVOpenHexagon/Core/HGScripting.cpp @@ -57,26 +57,26 @@ void HexagonGame::initLua_Utils() .doc("Flash the screen with `$0` intensity (from 0 to 255)."); addLuaFn("u_log", // - [this](std::string mLog) { ssvu::lo("lua") << mLog << "\n"; }) + [this](const std::string& mLog) { ssvu::lo("lua") << mLog << "\n"; }) .arg("message") .doc("Print out `$0` to the console."); addLuaFn("u_execScript", // - [this](std::string mName) { + [this](const std::string& mName) { runLuaFile(levelData->packPath + "Scripts/" + mName); }) .arg("scriptFilename") .doc("Execute the script located at `<pack>/Scripts/$0`."); addLuaFn("u_playSound", // - [this](std::string mId) { assets.playSound(mId); }) + [this](const std::string& mId) { assets.playSound(mId); }) .arg("soundId") .doc( "Play the sound with id `$0`. The id must be registered in " "`assets.json`, under `\"soundBuffers\"`."); addLuaFn("u_setMusic", // - [this](std::string mId) { + [this](const std::string& mId) { musicData = assets.getMusicData(levelData->packId, mId); musicData.firstPlay = true; stopLevelMusic(); @@ -88,7 +88,7 @@ void HexagonGame::initLua_Utils() "defined in the music `.json` file, under `\"id\"`."); addLuaFn("u_setMusicSegment", // - [this](std::string mId, int segment) { + [this](const std::string& mId, int segment) { musicData = assets.getMusicData(levelData->packId, mId); stopLevelMusic(); playLevelMusicAtTime(musicData.getSegment(segment).time); @@ -101,7 +101,7 @@ void HexagonGame::initLua_Utils() "under `\"segments\"`."); addLuaFn("u_setMusicSeconds", // - [this](std::string mId, float mTime) { + [this](const std::string& mId, float mTime) { musicData = assets.getMusicData(levelData->packId, mId); stopLevelMusic(); playLevelMusicAtTime(mTime); @@ -203,7 +203,7 @@ void HexagonGame::initLua_Utils() void HexagonGame::initLua_Messages() { addLuaFn("m_messageAdd", // - [this](std::string mMsg, double mDuration) { + [this](const std::string& mMsg, double mDuration) { eventTimeline.append_do([=, this] { if(firstPlay && Config::getShowMessages()) { @@ -219,7 +219,7 @@ void HexagonGame::initLua_Messages() "run of the level."); addLuaFn("m_messageAddImportant", // - [this](std::string mMsg, double mDuration) { + [this](const std::string& mMsg, double mDuration) { eventTimeline.append_do([=, this] { if(Config::getShowMessages()) { @@ -236,7 +236,7 @@ void HexagonGame::initLua_Messages() addLuaFn("m_messageAddImportantSilent", - [this](std::string mMsg, double mDuration) { + [this](const std::string& mMsg, double mDuration) { eventTimeline.append_do([=, this] { if(Config::getShowMessages()) { @@ -260,7 +260,7 @@ void HexagonGame::initLua_Messages() void HexagonGame::initLua_MainTimeline() { addLuaFn("t_eval", - [this](std::string mCode) { + [this](const std::string& mCode) { timeline.append_do([=, this] { lua.executeCode(mCode); }); }) .arg("code") @@ -359,8 +359,8 @@ void HexagonGame::initLua_LevelControl() const std::string& setterDesc) { using Type = std::decay_t<decltype(levelStatus.*pmd)>; - std::string getterString = std::string{"l_get"} + name; - std::string setterString = std::string{"l_set"} + name; + const std::string getterString = std::string{"l_get"} + name; + const std::string setterString = std::string{"l_set"} + name; addLuaFn(getterString, // [this, pmd]() -> Type { return levelStatus.*pmd; }) @@ -728,7 +728,7 @@ void HexagonGame::initLua_LevelControl() "increment."); addLuaFn("l_addTracked", // - [this](std::string mVar, std::string mName) { + [this](const std::string& mVar, std::string mName) { levelStatus.trackedVariables.emplace_back(mVar, mName); }) .arg("variable") @@ -760,7 +760,7 @@ void HexagonGame::initLua_LevelControl() // TODO: test and consider re-enabling /* addLuaFn("l_setLevel", - [this](std::string mId) + [this](const std::string& mId) { setLevelData(assets.getLevelData(mId), true); stopLevelMusic(); @@ -778,8 +778,8 @@ void HexagonGame::initLua_StyleControl() const std::string& setterDesc) { using Type = std::decay_t<decltype(styleData.*pmd)>; - std::string getterString = std::string{"s_get"} + name; - std::string setterString = std::string{"s_set"} + name; + const std::string getterString = std::string{"s_get"} + name; + const std::string setterString = std::string{"s_set"} + name; addLuaFn(getterString, // [this, pmd]() -> Type { return styleData.*pmd; }) @@ -967,7 +967,7 @@ void HexagonGame::initLua_StyleControl() "Sets the rotation offset of the background panels to `$0` degrees."); addLuaFn("s_setStyle", // - [this](std::string mId) { + [this](const std::string& mId) { styleData = assets.getStyleData(levelData->packId, mId); }) .arg("styleId") @@ -1121,7 +1121,7 @@ void HexagonGame::initLua_WallCreation() void HexagonGame::initLua_Steam() { addLuaFn("steam_unlockAchievement", // - [this](std::string mId) { + [this](const std::string& mId) { if(Config::getOfficial()) { steamManager.unlock_achievement(mId); |