Skip to content

Commit

Permalink
Core: Add a lookup caching object layer to luautils::
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Jan 22, 2025
1 parent 0ecb9e0 commit 8793c87
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/common/filewatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void Filewatcher::handleFileAction(efsw::WatchID watchid, std::string const& dir
}
}

auto Filewatcher::getChangedLuaFiles() -> std::vector<std::pair<std::filesystem::path, Action>>
auto Filewatcher::popChangedLuaFilesList() -> std::vector<std::pair<std::filesystem::path, Action>>
{
std::set<std::pair<std::filesystem::path, Action>> actions; // For de-duping

Expand Down
2 changes: 1 addition & 1 deletion src/common/filewatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Filewatcher : public efsw::FileWatchListener
Moved = 4,
};

auto getChangedLuaFiles() -> std::vector<std::pair<std::filesystem::path, Action>>;
auto popChangedLuaFilesList() -> std::vector<std::pair<std::filesystem::path, Action>>;

private:
std::unique_ptr<efsw::FileWatcher> fileWatcherImpl;
Expand Down
4 changes: 2 additions & 2 deletions src/map/entities/battleentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ int32 CBattleEntity::takeDamage(int32 amount, CBattleEntity* attacker /* = nullp
DAMAGE_TYPE damageType /* = DAMAGE_NONE*/, bool isSkillchainDamage /* = false */)
{
TracyZoneScoped;
PLastAttacker = attacker;
this->BattleHistory.lastHitTaken_atkType = attackType;
PLastAttacker = attacker;
this->BattleHistory.lastHitTaken_atkType = attackType;

PAI->EventHandler.triggerListener("TAKE_DAMAGE", this, amount, attacker, (uint16)attackType, (uint16)damageType);

Expand Down
82 changes: 74 additions & 8 deletions src/map/lua/luautils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,58 @@ void ReportErrorToPlayer(CBaseEntity* PEntity, std::string const& message = "")

namespace luautils
{
std::unique_ptr<Filewatcher> filewatcher;

std::unique_ptr<Filewatcher> filewatcher;
std::unordered_map<uint32, sol::table> customMenuContext;

namespace detail
{
std::unordered_map<std::string, sol::reference> cachedObjects;

auto findCachedObject(const std::string& objName) -> sol::reference
{
if (auto it = cachedObjects.find(objName); it != cachedObjects.end())
{
return it->second;
}

return sol::lua_nil;
}

void cacheObject(const std::string& objName, sol::reference obj)
{
cachedObjects[objName] = obj;
}

auto findGlobalLuaFunction(const std::string& funcName) -> sol::function
{
if (const auto* cachedFunc = findCachedObject(funcName); cachedFunc->valid())
{
return cachedFunc->as<sol::reference>();
}

const auto parts = split(funcName, ".");

sol::table table = lua["_G"];
for (const auto& part : parts)
{
if (part == parts.back() && table[part].get_type() == sol::type::function)
{
sol::reference func = table[part];
cacheObject(funcName, func);
return func;
}

table = table[part].get_or<sol::table>(sol::lua_nil);
if (table == sol::lua_nil)
{
return sol::lua_nil;
}
}

return sol::lua_nil;
}
} // namespace detail

/**
* @brief Initialization of Lua user classes and global functions.
*/
Expand Down Expand Up @@ -413,9 +461,20 @@ namespace luautils
TracyReportLuaMemory(lua.lua_state());
}

void ReloadFilewatchList()
void TryReloadFilewatchList()
{
for (const auto& [filename, action] : filewatcher->getChangedLuaFiles())
const auto changedFiles = filewatcher->popChangedLuaFilesList();

if (changedFiles.empty())
{
return;
}

// For coherency between looking things up by filename and by Lua global
// name we need to nuke the whole lookup cache on any file changes.
detail::cachedObjects.clear();

for (const auto& [filename, action] : changedFiles)
{
const auto pathStr = filename.generic_string();
if (action == Filewatcher::Action::Add || action == Filewatcher::Action::Modified)
Expand Down Expand Up @@ -761,8 +820,9 @@ namespace luautils

// file_result should be good, cache it!

auto table = lua["xi"].get_or_create<sol::table>();
std::string out_str = "xi";
detail::cachedObjects[filename] = file_result;

auto table = lua["xi"].get_or_create<sol::table>();
for (auto& part : parts)
{
if (part == parts.back())
Expand All @@ -780,7 +840,6 @@ namespace luautils
{
table = table[part].get_or_create<sol::table>(lua.create_table());
}
out_str += "." + part;
}

moduleutils::TryApplyLuaModules();
Expand All @@ -793,7 +852,12 @@ namespace luautils

if (filename.empty())
{
return lua.create_table();
return sol::lua_nil;
}

if (auto cached = detail::findCachedObject(filename); cached.valid())
{
return cached;
}

// Handle filename -> path conversion
Expand Down Expand Up @@ -822,6 +886,8 @@ namespace luautils
table = table[part].get_or_create<sol::table>();
}

detail::cacheObject(filename, table);

return table;
}

Expand Down
37 changes: 8 additions & 29 deletions src/map/lua/luautils.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,9 @@ namespace luautils
{
namespace detail
{
inline auto findLuaFunction(const std::string& funcName) -> sol::function
{
// TODO: Cache the lookups between funcName and the underlying function object so
// : we don't have to do this splitting and table lookup every time

const auto parts = split(funcName, ".");

sol::table table = lua["_G"];
for (const auto& part : parts)
{
if (part == parts.back())
{
return table[part].get_or<sol::function>(sol::lua_nil);
}

table = table[part].get_or<sol::table>(sol::lua_nil);
if (table == sol::lua_nil)
{
return sol::lua_nil;
}
}

return sol::lua_nil;
}
auto findCachedObject(const std::string& objName) -> sol::reference;
void cacheObject(const std::string& objName, sol::reference obj);
auto findGlobalLuaFunction(const std::string& funcName) -> sol::function;
} // namespace detail

void init();
Expand All @@ -159,7 +138,7 @@ namespace luautils
template <typename T, typename... Targs>
auto callGlobal(const std::string& funcName, Targs... args)
{
auto func = detail::findLuaFunction(funcName);
auto func = detail::findGlobalLuaFunction(funcName);
if (!func.valid())
{
ShowError("luautils::callGlobalFunction: %s: Function not found", funcName);
Expand Down Expand Up @@ -194,10 +173,10 @@ namespace luautils
}
else
{
auto returnObject = result.get<sol::object>();
if (returnObject.is<T>())
auto returnObject = result.template get<sol::object>();
if (returnObject.template is<T>())
{
return returnObject.as<T>();
return returnObject.template as<T>();
}
else
{
Expand All @@ -207,7 +186,7 @@ namespace luautils
}
}

void ReloadFilewatchList();
void TryReloadFilewatchList();

auto GetContainerFilenamesList() -> std::vector<std::string>;

Expand Down
2 changes: 1 addition & 1 deletion src/map/time_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int32 time_server(time_point tick, CTaskMgr::CTask* PTask)

luautils::OnTimeServerTick();

luautils::ReloadFilewatchList();
luautils::TryReloadFilewatchList();

moduleutils::OnTimeServerTick();

Expand Down
2 changes: 1 addition & 1 deletion src/map/utils/battleutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3989,7 +3989,7 @@ namespace battleutils
// TODO: × (1 + Day/Weather bonuses)
// TODO: × (1 + Staff Affinity)

auto damage = (int32)floor((double)(abs(lastSkillDamage))*g_SkillChainDamageModifiers[chainLevel][chainCount] / 1000 *
auto damage = (int32)floor((double)(abs(lastSkillDamage)) * g_SkillChainDamageModifiers[chainLevel][chainCount] / 1000 *
(100 + PAttacker->getMod(Mod::SKILLCHAINBONUS)) / 100 * (10000 + PAttacker->getMod(Mod::SKILLCHAINDMG)) / 10000);

auto* PChar = dynamic_cast<CCharEntity*>(PAttacker);
Expand Down

0 comments on commit 8793c87

Please sign in to comment.