Remove m prefix from private logic handler fields

This commit is contained in:
Sezz 2025-03-12 00:10:05 +11:00
parent 6f6a07e696
commit e97c47315e
2 changed files with 205 additions and 202 deletions

View file

@ -194,11 +194,11 @@ sol::object GetVariable(sol::table tab, sol::object key)
return tab.raw_get<sol::object>(key); return tab.raw_get<sol::object>(key);
} }
LogicHandler::LogicHandler(sol::state* lua, sol::table & parent) : m_handler{ lua } LogicHandler::LogicHandler(sol::state* lua, sol::table& parent) : _handler{ lua }
{ {
m_handler.GetState()->set_function("print", &LogicHandler::LogPrint, this); _handler.GetState()->set_function("print", &LogicHandler::LogPrint, this);
sol::table tableLogic{ m_handler.GetState()->lua_state(), sol::create }; auto tableLogic = sol::table(_handler.GetState()->lua_state(), sol::create);
parent.set(ScriptReserved_Logic, tableLogic); parent.set(ScriptReserved_Logic, tableLogic);
@ -208,24 +208,24 @@ LogicHandler::LogicHandler(sol::state* lua, sol::table & parent) : m_handler{ lu
tableLogic.set_function(ScriptReserved_EnableEvent, &LogicHandler::EnableEvent, this); tableLogic.set_function(ScriptReserved_EnableEvent, &LogicHandler::EnableEvent, this);
tableLogic.set_function(ScriptReserved_DisableEvent, &LogicHandler::DisableEvent, this); tableLogic.set_function(ScriptReserved_DisableEvent, &LogicHandler::DisableEvent, this);
m_handler.MakeReadOnlyTable(tableLogic, ScriptReserved_EndReason, LEVEL_END_REASONS); _handler.MakeReadOnlyTable(tableLogic, ScriptReserved_EndReason, LEVEL_END_REASONS);
m_handler.MakeReadOnlyTable(tableLogic, ScriptReserved_CallbackPoint, CALLBACK_POINTS); _handler.MakeReadOnlyTable(tableLogic, ScriptReserved_CallbackPoint, CALLBACK_POINTS);
m_handler.MakeReadOnlyTable(tableLogic, ScriptReserved_EventType, EVENT_TYPES); _handler.MakeReadOnlyTable(tableLogic, ScriptReserved_EventType, EVENT_TYPES);
m_callbacks.insert(std::make_pair(CallbackPoint::PreStart, &m_callbacksPreStart)); _callbacks.insert(std::make_pair(CallbackPoint::PreStart, &_callbacksPreStart));
m_callbacks.insert(std::make_pair(CallbackPoint::PostStart, &m_callbacksPostStart)); _callbacks.insert(std::make_pair(CallbackPoint::PostStart, &_callbacksPostStart));
m_callbacks.insert(std::make_pair(CallbackPoint::PreLoad, &m_callbacksPreLoad)); _callbacks.insert(std::make_pair(CallbackPoint::PreLoad, &_callbacksPreLoad));
m_callbacks.insert(std::make_pair(CallbackPoint::PostLoad, &m_callbacksPostLoad)); _callbacks.insert(std::make_pair(CallbackPoint::PostLoad, &_callbacksPostLoad));
m_callbacks.insert(std::make_pair(CallbackPoint::PreLoop, &m_callbacksPreLoop)); _callbacks.insert(std::make_pair(CallbackPoint::PreLoop, &_callbacksPreLoop));
m_callbacks.insert(std::make_pair(CallbackPoint::PostLoop, &m_callbacksPostLoop)); _callbacks.insert(std::make_pair(CallbackPoint::PostLoop, &_callbacksPostLoop));
m_callbacks.insert(std::make_pair(CallbackPoint::PreSave, &m_callbacksPreSave)); _callbacks.insert(std::make_pair(CallbackPoint::PreSave, &_callbacksPreSave));
m_callbacks.insert(std::make_pair(CallbackPoint::PostSave, &m_callbacksPostSave)); _callbacks.insert(std::make_pair(CallbackPoint::PostSave, &_callbacksPostSave));
m_callbacks.insert(std::make_pair(CallbackPoint::PreEnd, &m_callbacksPreEnd)); _callbacks.insert(std::make_pair(CallbackPoint::PreEnd, &_callbacksPreEnd));
m_callbacks.insert(std::make_pair(CallbackPoint::PostEnd, &m_callbacksPostEnd)); _callbacks.insert(std::make_pair(CallbackPoint::PostEnd, &_callbacksPostEnd));
m_callbacks.insert(std::make_pair(CallbackPoint::PreUseItem, &m_callbacksPreUseItem)); _callbacks.insert(std::make_pair(CallbackPoint::PreUseItem, &_callbacksPreUseItem));
m_callbacks.insert(std::make_pair(CallbackPoint::PostUseItem, &m_callbacksPostUseItem)); _callbacks.insert(std::make_pair(CallbackPoint::PostUseItem, &_callbacksPostUseItem));
m_callbacks.insert(std::make_pair(CallbackPoint::PreFreeze, &m_callbacksPreFreeze)); _callbacks.insert(std::make_pair(CallbackPoint::PreFreeze, &_callbacksPreFreeze));
m_callbacks.insert(std::make_pair(CallbackPoint::PostFreeze, &m_callbacksPostFreeze)); _callbacks.insert(std::make_pair(CallbackPoint::PostFreeze, &_callbacksPostFreeze));
LevelFunc::Register(tableLogic); LevelFunc::Register(tableLogic);
@ -234,10 +234,10 @@ LogicHandler::LogicHandler(sol::state* lua, sol::table & parent) : m_handler{ lu
void LogicHandler::ResetGameTables() void LogicHandler::ResetGameTables()
{ {
auto state = m_handler.GetState(); auto state = _handler.GetState();
MakeSpecialTable(state, ScriptReserved_GameVars, &GetVariable, &SetVariable); MakeSpecialTable(state, ScriptReserved_GameVars, &GetVariable, &SetVariable);
(*state)[ScriptReserved_GameVars][ScriptReserved_Engine] = sol::table{ *state, sol::create }; (*state)[ScriptReserved_GameVars][ScriptReserved_Engine] = sol::table(*state, sol::create);
} }
/*** Register a function as a callback. /*** Register a function as a callback.
@ -286,9 +286,8 @@ Any returned value will be discarded.
*/ */
void LogicHandler::AddCallback(CallbackPoint point, const LevelFunc& levelFunc) void LogicHandler::AddCallback(CallbackPoint point, const LevelFunc& levelFunc)
{ {
auto it = m_callbacks.find(point); auto it = _callbacks.find(point);
if (it == _callbacks.end())
if (it == m_callbacks.end())
{ {
TENLog("Error: callback point not found. Attempted to access missing value.", LogLevel::Error, LogConfig::All, false); TENLog("Error: callback point not found. Attempted to access missing value.", LogLevel::Error, LogConfig::All, false);
return; return;
@ -315,8 +314,8 @@ Will have no effect if the function was not registered as a callback
*/ */
void LogicHandler::RemoveCallback(CallbackPoint point, const LevelFunc& levelFunc) void LogicHandler::RemoveCallback(CallbackPoint point, const LevelFunc& levelFunc)
{ {
auto it = m_callbacks.find(point); auto it = _callbacks.find(point);
if (it == m_callbacks.end()) if (it == _callbacks.end())
{ {
TENLog("Error: callback point not found. Attempted to access missing value.", LogLevel::Error, LogConfig::All, false); TENLog("Error: callback point not found. Attempted to access missing value.", LogLevel::Error, LogConfig::All, false);
return; return;
@ -374,7 +373,7 @@ void LogicHandler::DisableEvent(const std::string& name, EventType type)
void LogicHandler::ResetLevelTables() void LogicHandler::ResetLevelTables()
{ {
auto state = m_handler.GetState(); auto state = _handler.GetState();
MakeSpecialTable(state, ScriptReserved_LevelVars, &GetVariable, &SetVariable); MakeSpecialTable(state, ScriptReserved_LevelVars, &GetVariable, &SetVariable);
(*state)[ScriptReserved_LevelVars][ScriptReserved_Engine] = sol::table{ *state, sol::create }; (*state)[ScriptReserved_LevelVars][ScriptReserved_Engine] = sol::table{ *state, sol::create };
@ -382,15 +381,15 @@ void LogicHandler::ResetLevelTables()
sol::object LogicHandler::GetLevelFuncsMember(sol::table tab, const std::string& name) sol::object LogicHandler::GetLevelFuncsMember(sol::table tab, const std::string& name)
{ {
std::string partName = tab.raw_get<std::string>(strKey); auto partName = tab.raw_get<std::string>(strKey);
auto& map = m_levelFuncs_tablesOfNames[partName]; auto& map = _levelFuncs_tablesOfNames[partName];
auto fullNameIt = map.find(name); auto fullNameIt = map.find(name);
if (fullNameIt != std::cend(map)) if (fullNameIt != std::cend(map))
{ {
std::string_view key = fullNameIt->second; std::string_view key = fullNameIt->second;
if (m_levelFuncs_levelFuncObjects[key].valid()) if (_levelFuncs_levelFuncObjects[key].valid())
return m_levelFuncs_levelFuncObjects[key]; return _levelFuncs_levelFuncObjects[key];
} }
return sol::nil; return sol::nil;
@ -400,7 +399,7 @@ bool LogicHandler::SetLevelFuncsMember(sol::table tab, const std::string& name,
{ {
if (sol::type::lua_nil == value.get_type()) if (sol::type::lua_nil == value.get_type())
{ {
std::string error{ "Tried to set " + std::string{ScriptReserved_LevelFuncs} + " member " }; auto error = std::string("Tried to set " + std::string{ScriptReserved_LevelFuncs} + " member ");
error += name + " to nil; this not permitted at this time."; error += name + " to nil; this not permitted at this time.";
return ScriptAssert(false, error); return ScriptAssert(false, error);
} }
@ -409,27 +408,27 @@ bool LogicHandler::SetLevelFuncsMember(sol::table tab, const std::string& name,
// Add name to table of names. // Add name to table of names.
auto partName = tab.raw_get<std::string>(strKey); auto partName = tab.raw_get<std::string>(strKey);
auto fullName = partName + "." + name; auto fullName = partName + "." + name;
auto& parentNameTab = m_levelFuncs_tablesOfNames[partName]; auto& parentNameTab = _levelFuncs_tablesOfNames[partName];
parentNameTab.insert_or_assign(name, fullName); parentNameTab.insert_or_assign(name, fullName);
// Create LevelFunc userdata and add that too. // Create LevelFunc userdata and add that too.
LevelFunc levelFuncObject; LevelFunc levelFuncObject;
levelFuncObject.m_funcName = fullName; levelFuncObject.m_funcName = fullName;
levelFuncObject.m_handler = this; levelFuncObject.m_handler = this;
m_levelFuncs_levelFuncObjects[fullName] = levelFuncObject; _levelFuncs_levelFuncObjects[fullName] = levelFuncObject;
// Add function itself. // Add function itself.
m_levelFuncs_luaFunctions[fullName] = value; _levelFuncs_luaFunctions[fullName] = value;
} }
else if (sol::type::table == value.get_type()) else if (sol::type::table == value.get_type())
{ {
// Create and add new name map. // Create and add new name map.
std::unordered_map<std::string, std::string> newNameMap; auto newNameMap = std::unordered_map<std::string, std::string>{};
auto fullName = tab.raw_get<std::string>(strKey) + "." + name; auto fullName = tab.raw_get<std::string>(strKey) + "." + name;
m_levelFuncs_tablesOfNames.insert_or_assign(fullName, newNameMap); _levelFuncs_tablesOfNames.insert_or_assign(fullName, newNameMap);
// Create new table to put in the LevelFuncs hierarchy. // Create new table to put in the LevelFuncs hierarchy.
auto newLevelFuncsTab = MakeSpecialTable(m_handler.GetState(), name, &LogicHandler::GetLevelFuncsMember, &LogicHandler::SetLevelFuncsMember, this); auto newLevelFuncsTab = MakeSpecialTable(_handler.GetState(), name, &LogicHandler::GetLevelFuncsMember, &LogicHandler::SetLevelFuncsMember, this);
newLevelFuncsTab.raw_set(strKey, fullName); newLevelFuncsTab.raw_set(strKey, fullName);
tab.raw_set(name, newLevelFuncsTab); tab.raw_set(name, newLevelFuncsTab);
@ -440,7 +439,7 @@ bool LogicHandler::SetLevelFuncsMember(sol::table tab, const std::string& name,
} }
else else
{ {
std::string error{ "Failed to add " }; auto error = std::string("Failed to add ");
error += name + " to " + ScriptReserved_LevelFuncs + " or one of its tables; it must be a function or a table of functions."; error += name + " to " + ScriptReserved_LevelFuncs + " or one of its tables; it must be a function or a table of functions.";
return ScriptAssert(false, error); return ScriptAssert(false, error);
} }
@ -450,10 +449,10 @@ bool LogicHandler::SetLevelFuncsMember(sol::table tab, const std::string& name,
void LogicHandler::LogPrint(sol::variadic_args args) void LogicHandler::LogPrint(sol::variadic_args args)
{ {
std::string str; auto str = std::string();
for (const sol::object& o : args) for (const sol::object& o : args)
{ {
auto strPart = (*m_handler.GetState())["tostring"](o).get<std::string>(); auto strPart = (*_handler.GetState())["tostring"](o).get<std::string>();
str += strPart; str += strPart;
str += "\t"; str += "\t";
} }
@ -465,10 +464,10 @@ void LogicHandler::ResetScripts(bool clearGameVars)
{ {
FreeLevelScripts(); FreeLevelScripts();
for (auto& [first, second] : m_callbacks) for (auto& [first, second] : _callbacks)
second->clear(); second->clear();
auto currentPackage = m_handler.GetState()->get<sol::table>("package"); auto currentPackage = _handler.GetState()->get<sol::table>("package");
auto currentLoaded = currentPackage.get<sol::table>("loaded"); auto currentLoaded = currentPackage.get<sol::table>("loaded");
for (auto& [first, second] : currentLoaded) for (auto& [first, second] : currentLoaded)
@ -477,35 +476,35 @@ void LogicHandler::ResetScripts(bool clearGameVars)
if (clearGameVars) if (clearGameVars)
ResetGameTables(); ResetGameTables();
m_handler.ResetGlobals(); _handler.ResetGlobals();
m_shortenedCalls = false; _shortenedCalls = false;
m_handler.GetState()->collect_garbage(); _handler.GetState()->collect_garbage();
} }
void LogicHandler::FreeLevelScripts() void LogicHandler::FreeLevelScripts()
{ {
m_levelFuncs = MakeSpecialTable(m_handler.GetState(), ScriptReserved_LevelFuncs, &LogicHandler::GetLevelFuncsMember, &LogicHandler::SetLevelFuncsMember, this); _levelFuncs = MakeSpecialTable(_handler.GetState(), ScriptReserved_LevelFuncs, &LogicHandler::GetLevelFuncsMember, &LogicHandler::SetLevelFuncsMember, this);
m_levelFuncs.raw_set(strKey, ScriptReserved_LevelFuncs); _levelFuncs.raw_set(strKey, ScriptReserved_LevelFuncs);
m_levelFuncs[ScriptReserved_Engine] = sol::table{ *m_handler.GetState(), sol::create }; _levelFuncs[ScriptReserved_Engine] = sol::table(*_handler.GetState(), sol::create);
m_levelFuncs_tablesOfNames.clear(); _levelFuncs_tablesOfNames.clear();
m_levelFuncs_luaFunctions.clear(); _levelFuncs_luaFunctions.clear();
m_levelFuncs_levelFuncObjects = sol::table{ *m_handler.GetState(), sol::create }; _levelFuncs_levelFuncObjects = sol::table(*_handler.GetState(), sol::create);
m_levelFuncs_tablesOfNames.emplace(std::make_pair(ScriptReserved_LevelFuncs, std::unordered_map<std::string, std::string>{})); _levelFuncs_tablesOfNames.emplace(std::make_pair(ScriptReserved_LevelFuncs, std::unordered_map<std::string, std::string>{}));
ResetLevelTables(); ResetLevelTables();
m_onStart = sol::nil; _onStart = sol::nil;
m_onLoad = sol::nil; _onLoad = sol::nil;
m_onLoop = sol::nil; _onLoop = sol::nil;
m_onSave = sol::nil; _onSave = sol::nil;
m_onEnd = sol::nil; _onEnd = sol::nil;
m_onUseItem = sol::nil; _onUseItem = sol::nil;
m_onFreeze = sol::nil; _onFreeze = sol::nil;
m_handler.GetState()->collect_garbage(); _handler.GetState()->collect_garbage();
} }
// Used when loading. // Used when loading.
@ -516,13 +515,13 @@ void LogicHandler::SetVariables(const std::vector<SavedVar>& vars, bool onlyLeve
ResetLevelTables(); ResetLevelTables();
std::unordered_map<unsigned int, sol::table> solTables; auto solTables = std::unordered_map<unsigned int, sol::table>{};
for(int i = 0; i < vars.size(); ++i) for(int i = 0; i < vars.size(); ++i)
{ {
if (std::holds_alternative<IndexTable>(vars[i])) if (std::holds_alternative<IndexTable>(vars[i]))
{ {
solTables.try_emplace(i, *m_handler.GetState(), sol::create); solTables.try_emplace(i, *_handler.GetState(), sol::create);
auto indexTab = std::get<IndexTable>(vars[i]); auto indexTab = std::get<IndexTable>(vars[i]);
for (auto& [first, second] : indexTab) for (auto& [first, second] : indexTab)
{ {
@ -530,7 +529,7 @@ void LogicHandler::SetVariables(const std::vector<SavedVar>& vars, bool onlyLeve
// create it if need be // create it if need be
if (std::holds_alternative<IndexTable>(vars[second])) if (std::holds_alternative<IndexTable>(vars[second]))
{ {
solTables.try_emplace(second, *m_handler.GetState(), sol::create); solTables.try_emplace(second, *_handler.GetState(), sol::create);
solTables[i][vars[first]] = solTables[second]; solTables[i][vars[first]] = solTables[second];
} }
else if (std::holds_alternative<double>(vars[second])) else if (std::holds_alternative<double>(vars[second]))
@ -594,14 +593,14 @@ void LogicHandler::SetVariables(const std::vector<SavedVar>& vars, bool onlyLeve
sol::table levelVars = rootTable[ScriptReserved_LevelVars]; sol::table levelVars = rootTable[ScriptReserved_LevelVars];
for (auto& [first, second] : levelVars) for (auto& [first, second] : levelVars)
(*m_handler.GetState())[ScriptReserved_LevelVars][first] = second; (*_handler.GetState())[ScriptReserved_LevelVars][first] = second;
if (onlyLevelVars) if (onlyLevelVars)
return; return;
sol::table gameVars = rootTable[ScriptReserved_GameVars]; sol::table gameVars = rootTable[ScriptReserved_GameVars];
for (auto& [first, second] : gameVars) for (auto& [first, second] : gameVars)
(*m_handler.GetState())[ScriptReserved_GameVars][first] = second; (*_handler.GetState())[ScriptReserved_GameVars][first] = second;
} }
template<SavedVarType TypeEnum, typename TypeTo, typename TypeFrom, typename MapType> template<SavedVarType TypeEnum, typename TypeTo, typename TypeFrom, typename MapType>
@ -623,10 +622,10 @@ int Handle(TypeFrom& var, MapType& varsMap, size_t& numVars, std::vector<SavedVa
std::string LogicHandler::GetRequestedPath() const std::string LogicHandler::GetRequestedPath() const
{ {
std::string path; auto path = std::string();
for (unsigned int i = 0; i < m_savedVarPath.size(); ++i) for (unsigned int i = 0; i < _savedVarPath.size(); ++i)
{ {
auto key = m_savedVarPath[i]; auto key = _savedVarPath[i];
if (std::holds_alternative<unsigned int>(key)) if (std::holds_alternative<unsigned int>(key))
{ {
path += "[" + std::to_string(std::get<unsigned int>(key)) + "]"; path += "[" + std::to_string(std::get<unsigned int>(key)) + "]";
@ -635,9 +634,13 @@ std::string LogicHandler::GetRequestedPath() const
{ {
auto part = std::get<std::string>(key); auto part = std::get<std::string>(key);
if (i > 0) if (i > 0)
{
path += "." + part; path += "." + part;
}
else else
{
path += part; path += part;
}
} }
} }
@ -647,15 +650,15 @@ std::string LogicHandler::GetRequestedPath() const
// Used when saving. // Used when saving.
void LogicHandler::GetVariables(std::vector<SavedVar>& vars) void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
{ {
sol::table tab{ *m_handler.GetState(), sol::create }; auto tab = sol::table(*_handler.GetState(), sol::create);
tab[ScriptReserved_LevelVars] = (*m_handler.GetState())[ScriptReserved_LevelVars]; tab[ScriptReserved_LevelVars] = (*_handler.GetState())[ScriptReserved_LevelVars];
tab[ScriptReserved_GameVars] = (*m_handler.GetState())[ScriptReserved_GameVars]; tab[ScriptReserved_GameVars] = (*_handler.GetState())[ScriptReserved_GameVars];
std::unordered_map<void const*, unsigned int> varsMap; auto varsMap = std::unordered_map<void const*, unsigned int>{};
std::unordered_map<double, unsigned int> numMap; auto numMap = std::unordered_map<double, unsigned int>{};
std::unordered_map<bool, unsigned int> boolMap; auto boolMap = std::unordered_map<bool, unsigned int>{};
size_t numVars = 0; size_t varCount = 0;
// The following functions will all try to put their values in a map. If it succeeds // The following functions will all try to put their values in a map. If it succeeds
// then the value was not already in the map, so we can put it into the var vector. // then the value was not already in the map, so we can put it into the var vector.
@ -667,12 +670,12 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
auto handleNum = [&](auto num, auto map) auto handleNum = [&](auto num, auto map)
{ {
auto [first, second] = map.insert(std::make_pair(num, (int)numVars)); auto [first, second] = map.insert(std::make_pair(num, (int)varCount));
if (second) if (second)
{ {
vars.push_back(num); vars.push_back(num);
++numVars; ++varCount;
} }
return first->second; return first->second;
@ -681,12 +684,12 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
auto handleStr = [&](const sol::object& obj) auto handleStr = [&](const sol::object& obj)
{ {
auto str = obj.as<sol::string_view>(); auto str = obj.as<sol::string_view>();
auto [first, second] = varsMap.insert(std::make_pair(str.data(), (int)numVars)); auto [first, second] = varsMap.insert(std::make_pair(str.data(), (int)varCount));
if (second) if (second)
{ {
vars.push_back(std::string{ str.data() }); vars.push_back(std::string{ str.data() });
++numVars; ++varCount;
} }
return first->second; return first->second;
@ -694,12 +697,12 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
auto handleFuncName = [&](const LevelFunc& fnh) auto handleFuncName = [&](const LevelFunc& fnh)
{ {
auto [first, second] = varsMap.insert(std::make_pair(&fnh, (int)numVars)); auto [first, second] = varsMap.insert(std::make_pair(&fnh, (int)varCount));
if (second) if (second)
{ {
vars.push_back(FuncName{ std::string{ fnh.m_funcName } }); vars.push_back(FuncName{ std::string{ fnh.m_funcName } });
++numVars; ++varCount;
} }
return first->second; return first->second;
@ -707,11 +710,11 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
std::function<unsigned int(const sol::table&)> populate = [&](const sol::table& obj) std::function<unsigned int(const sol::table&)> populate = [&](const sol::table& obj)
{ {
auto [first, second] = varsMap.insert(std::make_pair(obj.pointer(), (int)numVars)); auto [first, second] = varsMap.insert(std::make_pair(obj.pointer(), (int)varCount));
if(second) if(second)
{ {
++numVars; ++varCount;
auto id = first->second; auto id = first->second;
vars.push_back(IndexTable{}); vars.push_back(IndexTable{});
@ -729,7 +732,7 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
{ {
keyIndex = handleStr(first); keyIndex = handleStr(first);
key = std::string{ first.as<sol::string_view>().data() }; key = std::string{ first.as<sol::string_view>().data() };
m_savedVarPath.push_back(key); _savedVarPath.push_back(key);
} }
break; break;
@ -744,7 +747,7 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
{ {
keyIndex = handleNum(data, numMap); keyIndex = handleNum(data, numMap);
key = static_cast<unsigned int>(data); key = static_cast<unsigned int>(data);
m_savedVarPath.push_back(key); _savedVarPath.push_back(key);
} }
} }
break; break;
@ -784,23 +787,23 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
{ {
if (second.is<Vec2>()) if (second.is<Vec2>())
{ {
putInVars(Handle<SavedVarType::Vec2, Vector2>(second.as<Vec2>(), varsMap, numVars, vars)); putInVars(Handle<SavedVarType::Vec2, Vector2>(second.as<Vec2>(), varsMap, varCount, vars));
} }
else if (second.is<Vec3>()) else if (second.is<Vec3>())
{ {
putInVars(Handle<SavedVarType::Vec3, Vector3>(second.as<Vec3>(), varsMap, numVars, vars)); putInVars(Handle<SavedVarType::Vec3, Vector3>(second.as<Vec3>(), varsMap, varCount, vars));
} }
else if (second.is<Rotation>()) else if (second.is<Rotation>())
{ {
putInVars(Handle<SavedVarType::Rotation, Vector3>(second.as<Rotation>(), varsMap, numVars, vars)); putInVars(Handle<SavedVarType::Rotation, Vector3>(second.as<Rotation>(), varsMap, varCount, vars));
} }
else if (second.is<Time>()) else if (second.is<Time>())
{ {
putInVars(Handle<SavedVarType::Time, int>(second.as<Time>(), varsMap, numVars, vars)); putInVars(Handle<SavedVarType::Time, int>(second.as<Time>(), varsMap, varCount, vars));
} }
else if (second.is<ScriptColor>()) else if (second.is<ScriptColor>())
{ {
putInVars(Handle<SavedVarType::Color, D3DCOLOR>(second.as<ScriptColor>(), varsMap, numVars, vars)); putInVars(Handle<SavedVarType::Color, D3DCOLOR>(second.as<ScriptColor>(), varsMap, varCount, vars));
} }
else if (second.is<LevelFunc>()) else if (second.is<LevelFunc>())
{ {
@ -817,7 +820,7 @@ void LogicHandler::GetVariables(std::vector<SavedVar>& vars)
ScriptAssert(false, "Tried saving an unsupported type as a value; variable is " + GetRequestedPath()); ScriptAssert(false, "Tried saving an unsupported type as a value; variable is " + GetRequestedPath());
} }
m_savedVarPath.pop_back(); _savedVarPath.pop_back();
} }
} }
@ -845,30 +848,30 @@ void LogicHandler::GetCallbackStrings(
{ {
auto populateWith = [](std::vector<std::string>& dest, const std::unordered_set<std::string>& src) auto populateWith = [](std::vector<std::string>& dest, const std::unordered_set<std::string>& src)
{ {
for (const auto& s : src) for (const auto& string : src)
dest.push_back(s); dest.push_back(string);
}; };
populateWith(preStart, m_callbacksPreStart); populateWith(preStart, _callbacksPreStart);
populateWith(postStart, m_callbacksPostStart); populateWith(postStart, _callbacksPostStart);
populateWith(preEnd, m_callbacksPreEnd); populateWith(preEnd, _callbacksPreEnd);
populateWith(postEnd, m_callbacksPostEnd); populateWith(postEnd, _callbacksPostEnd);
populateWith(preSave, m_callbacksPreSave); populateWith(preSave, _callbacksPreSave);
populateWith(postSave, m_callbacksPostSave); populateWith(postSave, _callbacksPostSave);
populateWith(preLoad, m_callbacksPreLoad); populateWith(preLoad, _callbacksPreLoad);
populateWith(postLoad, m_callbacksPostLoad); populateWith(postLoad, _callbacksPostLoad);
populateWith(preLoop, m_callbacksPreLoop); populateWith(preLoop, _callbacksPreLoop);
populateWith(postLoop, m_callbacksPostLoop); populateWith(postLoop, _callbacksPostLoop);
populateWith(preUseItem, m_callbacksPreUseItem); populateWith(preUseItem, _callbacksPreUseItem);
populateWith(postUseItem, m_callbacksPostUseItem); populateWith(postUseItem, _callbacksPostUseItem);
populateWith(preBreak, m_callbacksPreFreeze); populateWith(preBreak, _callbacksPreFreeze);
populateWith(postBreak, m_callbacksPostFreeze); populateWith(postBreak, _callbacksPostFreeze);
} }
void LogicHandler::SetCallbackStrings( void LogicHandler::SetCallbackStrings(
@ -893,26 +896,26 @@ void LogicHandler::SetCallbackStrings(
dest.insert(string); dest.insert(string);
}; };
populateWith(m_callbacksPreStart, preStart); populateWith(_callbacksPreStart, preStart);
populateWith(m_callbacksPostStart, postStart); populateWith(_callbacksPostStart, postStart);
populateWith(m_callbacksPreEnd, preEnd); populateWith(_callbacksPreEnd, preEnd);
populateWith(m_callbacksPostEnd, postEnd); populateWith(_callbacksPostEnd, postEnd);
populateWith(m_callbacksPreSave, preSave); populateWith(_callbacksPreSave, preSave);
populateWith(m_callbacksPostSave, postSave); populateWith(_callbacksPostSave, postSave);
populateWith(m_callbacksPreLoad, preLoad); populateWith(_callbacksPreLoad, preLoad);
populateWith(m_callbacksPostLoad, postLoad); populateWith(_callbacksPostLoad, postLoad);
populateWith(m_callbacksPreLoop, preLoop); populateWith(_callbacksPreLoop, preLoop);
populateWith(m_callbacksPostLoop, postLoop); populateWith(_callbacksPostLoop, postLoop);
populateWith(m_callbacksPreUseItem, preUseItem); populateWith(_callbacksPreUseItem, preUseItem);
populateWith(m_callbacksPostUseItem, postUseItem); populateWith(_callbacksPostUseItem, postUseItem);
populateWith(m_callbacksPreFreeze, preBreak); populateWith(_callbacksPreFreeze, preBreak);
populateWith(m_callbacksPostFreeze, postBreak); populateWith(_callbacksPostFreeze, postBreak);
} }
template <typename R, char const * S, typename mapType> template <typename R, char const * S, typename mapType>
@ -931,7 +934,7 @@ std::unique_ptr<R> GetByName(const std::string& type, const std::string& name, c
*/ */
void LogicHandler::ResetVariables() void LogicHandler::ResetVariables()
{ {
(*m_handler.GetState())["Lara"] = nullptr; (*_handler.GetState())["Lara"] = nullptr;
} }
void LogicHandler::ShortenTENCalls() void LogicHandler::ShortenTENCalls()
@ -956,33 +959,33 @@ void LogicHandler::ShortenTENCalls()
ExecuteString(str); ExecuteString(str);
m_shortenedCalls = true; _shortenedCalls = true;
} }
void LogicHandler::ExecuteScriptFile(const std::string& luaFilename) void LogicHandler::ExecuteScriptFile(const std::string& luaFilename)
{ {
if (!m_shortenedCalls) if (!_shortenedCalls)
ShortenTENCalls(); ShortenTENCalls();
m_handler.ExecuteScript(luaFilename); _handler.ExecuteScript(luaFilename);
} }
void LogicHandler::ExecuteString(const std::string& command) void LogicHandler::ExecuteString(const std::string& command)
{ {
m_handler.ExecuteString(command); _handler.ExecuteString(command);
} }
// These wind up calling CallLevelFunc, which is where all error checking is. // These wind up calling CallLevelFunc, which is where all error checking is.
void LogicHandler::ExecuteFunction(const std::string& name, short idOne, short idTwo) void LogicHandler::ExecuteFunction(const std::string& name, short idOne, short idTwo)
{ {
auto func = m_levelFuncs_luaFunctions[name]; auto func = _levelFuncs_luaFunctions[name];
func(std::make_unique<Moveable>(idOne), std::make_unique<Moveable>(idTwo)); func(std::make_unique<Moveable>(idOne), std::make_unique<Moveable>(idTwo));
} }
void LogicHandler::ExecuteFunction(const std::string& name, TEN::Control::Volumes::Activator activator, const std::string& arguments) void LogicHandler::ExecuteFunction(const std::string& name, TEN::Control::Volumes::Activator activator, const std::string& arguments)
{ {
sol::protected_function func = (*m_handler.GetState())[ScriptReserved_LevelFuncs][name.c_str()]; sol::protected_function func = (*_handler.GetState())[ScriptReserved_LevelFuncs][name.c_str()];
if (std::holds_alternative<int>(activator)) if (std::holds_alternative<int>(activator))
{ {
func(std::make_unique<Moveable>(std::get<int>(activator), true), arguments); func(std::make_unique<Moveable>(std::get<int>(activator), true), arguments);
@ -995,25 +998,25 @@ void LogicHandler::ExecuteFunction(const std::string& name, TEN::Control::Volume
void LogicHandler::OnStart() void LogicHandler::OnStart()
{ {
for (const auto& name : m_callbacksPreStart) for (const auto& name : _callbacksPreStart)
CallLevelFuncByName(name); CallLevelFuncByName(name);
if (m_onStart.valid()) if (_onStart.valid())
CallLevelFunc(m_onStart); CallLevelFunc(_onStart);
for (const auto& name : m_callbacksPostStart) for (const auto& name : _callbacksPostStart)
CallLevelFuncByName(name); CallLevelFuncByName(name);
} }
void LogicHandler::OnLoad() void LogicHandler::OnLoad()
{ {
for (const auto& name : m_callbacksPreLoad) for (const auto& name : _callbacksPreLoad)
CallLevelFuncByName(name); CallLevelFuncByName(name);
if (m_onLoad.valid()) if (_onLoad.valid())
CallLevelFunc(m_onLoad); CallLevelFunc(_onLoad);
for (const auto& name : m_callbacksPostLoad) for (const auto& name : _callbacksPostLoad)
CallLevelFuncByName(name); CallLevelFuncByName(name);
} }
@ -1021,29 +1024,29 @@ void LogicHandler::OnLoop(float deltaTime, bool postLoop)
{ {
if (!postLoop) if (!postLoop)
{ {
for (const auto& name : m_callbacksPreLoop) for (const auto& name : _callbacksPreLoop)
CallLevelFuncByName(name, deltaTime); CallLevelFuncByName(name, deltaTime);
lua_gc(m_handler.GetState()->lua_state(), LUA_GCCOLLECT, 0); lua_gc(_handler.GetState()->lua_state(), LUA_GCCOLLECT, 0);
if (m_onLoop.valid()) if (_onLoop.valid())
CallLevelFunc(m_onLoop, deltaTime); CallLevelFunc(_onLoop, deltaTime);
} }
else else
{ {
for (const auto& name : m_callbacksPostLoop) for (const auto& name : _callbacksPostLoop)
CallLevelFuncByName(name, deltaTime); CallLevelFuncByName(name, deltaTime);
} }
} }
void LogicHandler::OnSave() void LogicHandler::OnSave()
{ {
for (const auto& name : m_callbacksPreSave) for (const auto& name : _callbacksPreSave)
CallLevelFuncByName(name); CallLevelFuncByName(name);
if (m_onSave.valid()) if (_onSave.valid())
CallLevelFunc(m_onSave); CallLevelFunc(_onSave);
for (const auto& name : m_callbacksPostSave) for (const auto& name : _callbacksPostSave)
CallLevelFuncByName(name); CallLevelFuncByName(name);
} }
@ -1069,37 +1072,37 @@ void LogicHandler::OnEnd(GameStatus reason)
break; break;
} }
for (const auto& name : m_callbacksPreEnd) for (const auto& name : _callbacksPreEnd)
CallLevelFuncByName(name, endReason); CallLevelFuncByName(name, endReason);
if (m_onEnd.valid()) if (_onEnd.valid())
CallLevelFunc(m_onEnd, endReason); CallLevelFunc(_onEnd, endReason);
for (const auto& name : m_callbacksPostEnd) for (const auto& name : _callbacksPostEnd)
CallLevelFuncByName(name, endReason); CallLevelFuncByName(name, endReason);
} }
void LogicHandler::OnUseItem(GAME_OBJECT_ID objectNumber) void LogicHandler::OnUseItem(GAME_OBJECT_ID objectNumber)
{ {
for (const auto& name : m_callbacksPreUseItem) for (const auto& name : _callbacksPreUseItem)
CallLevelFuncByName(name, objectNumber); CallLevelFuncByName(name, objectNumber);
if (m_onUseItem.valid()) if (_onUseItem.valid())
CallLevelFunc(m_onUseItem, objectNumber); CallLevelFunc(_onUseItem, objectNumber);
for (const auto& name : m_callbacksPostUseItem) for (const auto& name : _callbacksPostUseItem)
CallLevelFuncByName(name, objectNumber); CallLevelFuncByName(name, objectNumber);
} }
void LogicHandler::OnFreeze() void LogicHandler::OnFreeze()
{ {
for (const auto& name : m_callbacksPreFreeze) for (const auto& name : _callbacksPreFreeze)
CallLevelFuncByName(name); CallLevelFuncByName(name);
if (m_onFreeze.valid()) if (_onFreeze.valid())
CallLevelFunc(m_onFreeze); CallLevelFunc(_onFreeze);
for (const auto& name : m_callbacksPostFreeze) for (const auto& name : _callbacksPostFreeze)
CallLevelFuncByName(name); CallLevelFuncByName(name);
} }
@ -1226,8 +1229,8 @@ void LogicHandler::InitCallbacks()
{ {
auto assignCB = [this](sol::protected_function& func, const std::string& luaFunc) auto assignCB = [this](sol::protected_function& func, const std::string& luaFunc)
{ {
auto state = m_handler.GetState(); auto state = _handler.GetState();
std::string fullName = std::string{ ScriptReserved_LevelFuncs } + "." + luaFunc; auto fullName = std::string(ScriptReserved_LevelFuncs) + "." + luaFunc;
sol::object theData = (*state)[ScriptReserved_LevelFuncs][luaFunc]; sol::object theData = (*state)[ScriptReserved_LevelFuncs][luaFunc];
@ -1236,20 +1239,20 @@ void LogicHandler::InitCallbacks()
LevelFunc fnh = (*state)[ScriptReserved_LevelFuncs][luaFunc]; LevelFunc fnh = (*state)[ScriptReserved_LevelFuncs][luaFunc];
func = m_levelFuncs_luaFunctions[fnh.m_funcName]; func = _levelFuncs_luaFunctions[fnh.m_funcName];
if (!func.valid()) if (!func.valid())
TENLog("Level's script does not define callback " + fullName + ". Defaulting to no " + fullName + " behaviour."); TENLog("Level's script does not define callback " + fullName + ". Defaulting to no " + fullName + " behaviour.");
}; };
assignCB(m_onStart, ScriptReserved_OnStart); assignCB(_onStart, ScriptReserved_OnStart);
assignCB(m_onLoad, ScriptReserved_OnLoad); assignCB(_onLoad, ScriptReserved_OnLoad);
assignCB(m_onLoop, ScriptReserved_OnLoop); assignCB(_onLoop, ScriptReserved_OnLoop);
assignCB(m_onSave, ScriptReserved_OnSave); assignCB(_onSave, ScriptReserved_OnSave);
assignCB(m_onEnd, ScriptReserved_OnEnd); assignCB(_onEnd, ScriptReserved_OnEnd);
assignCB(m_onUseItem, ScriptReserved_OnUseItem); assignCB(_onUseItem, ScriptReserved_OnUseItem);
assignCB(m_onFreeze, ScriptReserved_OnFreeze); assignCB(_onFreeze, ScriptReserved_OnFreeze);
// COMPATIBILITY // COMPATIBILITY
assignCB(m_onLoop, "OnControlPhase"); assignCB(_onLoop, "OnControlPhase");
} }

View file

@ -32,14 +32,14 @@ private:
// Each of these has a metatable whose __index metamethod looks in m_levelFuncsTables, using the path // Each of these has a metatable whose __index metamethod looks in m_levelFuncsTables, using the path
// as the key, for the full name of the function. It then gets the FuncNameHolder from m_levelFuncsFakeFuncs, // as the key, for the full name of the function. It then gets the FuncNameHolder from m_levelFuncsFakeFuncs,
// and that FuncNameHolder's __call metamethod looks in m_levelFuncs_luaFunctions for the real function. // and that FuncNameHolder's __call metamethod looks in m_levelFuncs_luaFunctions for the real function.
sol::table m_levelFuncs{}; sol::table _levelFuncs = {};
// Maps full function paths into Lua functions. // Maps full function paths into Lua functions.
std::unordered_map<std::string, sol::protected_function> m_levelFuncs_luaFunctions{}; std::unordered_map<std::string, sol::protected_function> _levelFuncs_luaFunctions = {};
// Maps full function paths to LevelFunc objects. // Maps full function paths to LevelFunc objects.
// This is a table instead of a C++ container to more easily interface with Sol. // This is a table instead of a C++ container to more easily interface with Sol.
sol::table m_levelFuncs_levelFuncObjects{}; sol::table _levelFuncs_levelFuncObjects = {};
// Contains tables; each table refers to a table in the LevelFuncs hierarchy, and contains the full names // Contains tables; each table refers to a table in the LevelFuncs hierarchy, and contains the full names
// of the functions to index in m_levelFuncs_luaFunctions. // of the functions to index in m_levelFuncs_luaFunctions.
@ -49,47 +49,47 @@ private:
// "LevelFuncs.Engine.Util" // "LevelFuncs.Engine.Util"
// "LevelFuncs.MyLevel" // "LevelFuncs.MyLevel"
// "LevelFuncs.MyLevel.CoolFuncs" // "LevelFuncs.MyLevel.CoolFuncs"
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> m_levelFuncs_tablesOfNames{}; std::unordered_map<std::string, std::unordered_map<std::string, std::string>> _levelFuncs_tablesOfNames = {};
std::unordered_set<std::string> m_callbacksPreStart; std::unordered_set<std::string> _callbacksPreStart = {};
std::unordered_set<std::string> m_callbacksPostStart; std::unordered_set<std::string> _callbacksPostStart = {};
std::unordered_set<std::string> m_callbacksPreLoop; std::unordered_set<std::string> _callbacksPreLoop = {};
std::unordered_set<std::string> m_callbacksPostLoop; std::unordered_set<std::string> _callbacksPostLoop = {};
std::unordered_set<std::string> m_callbacksPreLoad; std::unordered_set<std::string> _callbacksPreLoad = {};
std::unordered_set<std::string> m_callbacksPostLoad; std::unordered_set<std::string> _callbacksPostLoad = {};
std::unordered_set<std::string> m_callbacksPreSave; std::unordered_set<std::string> _callbacksPreSave = {};
std::unordered_set<std::string> m_callbacksPostSave; std::unordered_set<std::string> _callbacksPostSave = {};
std::unordered_set<std::string> m_callbacksPreEnd; std::unordered_set<std::string> _callbacksPreEnd = {};
std::unordered_set<std::string> m_callbacksPostEnd; std::unordered_set<std::string> _callbacksPostEnd = {};
std::unordered_set<std::string> m_callbacksPreUseItem; std::unordered_set<std::string> _callbacksPreUseItem = {};
std::unordered_set<std::string> m_callbacksPostUseItem; std::unordered_set<std::string> _callbacksPostUseItem = {};
std::unordered_set<std::string> m_callbacksPreFreeze; std::unordered_set<std::string> _callbacksPreFreeze = {};
std::unordered_set<std::string> m_callbacksPostFreeze; std::unordered_set<std::string> _callbacksPostFreeze = {};
sol::protected_function m_onStart{}; sol::protected_function _onStart = {};
sol::protected_function m_onLoop{}; sol::protected_function _onLoop = {};
sol::protected_function m_onLoad{}; sol::protected_function _onLoad = {};
sol::protected_function m_onSave{}; sol::protected_function _onSave = {};
sol::protected_function m_onEnd{}; sol::protected_function _onEnd = {};
sol::protected_function m_onUseItem{}; sol::protected_function _onUseItem = {};
sol::protected_function m_onFreeze{}; sol::protected_function _onFreeze = {};
std::unordered_map<CallbackPoint, std::unordered_set<std::string> *> m_callbacks; std::unordered_map<CallbackPoint, std::unordered_set<std::string>*> _callbacks;
std::vector<std::variant<std::string, unsigned int>> m_savedVarPath; std::vector<std::variant<std::string, unsigned int>> _savedVarPath;
bool m_shortenedCalls = false; bool _shortenedCalls = false;
std::string GetRequestedPath() const; std::string GetRequestedPath() const;
void ResetLevelTables(); void ResetLevelTables();
void ResetGameTables(); void ResetGameTables();
LuaHandler m_handler; LuaHandler _handler;
public: public:
LogicHandler(sol::state* lua, sol::table& parent); LogicHandler(sol::state* lua, sol::table& parent);
template <typename ... Ts> sol::protected_function_result CallLevelFuncBase(const sol::protected_function & func, Ts ... vs) template <typename ... Ts> sol::protected_function_result CallLevelFuncBase(const sol::protected_function& func, Ts ... vs)
{ {
auto funcResult = func.call(vs...); auto funcResult = func.call(vs...);
return funcResult; return funcResult;
@ -97,7 +97,7 @@ public:
template <typename ... Ts> sol::protected_function_result CallLevelFuncByName(const std::string& name, Ts ... vs) template <typename ... Ts> sol::protected_function_result CallLevelFuncByName(const std::string& name, Ts ... vs)
{ {
auto func = m_levelFuncs_luaFunctions[name]; auto func = _levelFuncs_luaFunctions[name];
auto funcResult = CallLevelFuncBase(func, vs...); auto funcResult = CallLevelFuncBase(func, vs...);
if (!funcResult.valid()) if (!funcResult.valid())
@ -109,7 +109,7 @@ public:
return funcResult; return funcResult;
} }
template <typename ... Ts> sol::protected_function_result CallLevelFunc(const sol::protected_function & func, Ts ... vs) template <typename ... Ts> sol::protected_function_result CallLevelFunc(const sol::protected_function& func, Ts ... vs)
{ {
auto funcResult = CallLevelFuncBase(func, vs...); auto funcResult = CallLevelFuncBase(func, vs...);