Skip to content

Commit

Permalink
Merge pull request #912 from Supremist/master
Browse files Browse the repository at this point in the history
Expose mod input handlers for better integration between mods
  • Loading branch information
WSSDude authored Jan 15, 2024
2 parents ca95e7c + 96cff39 commit 103c8db
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/VKBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct VKBind
std::string DisplayName{};
std::variant<std::string, std::function<void()>> Description{};
std::variant<std::function<TVKBindHotkeyCallback>, std::function<TVKBindInputCallback>> Handler{};
sol::function VmHandler{};

[[nodiscard]] std::function<void()> DelayedCall(const bool acIsDown) const;
void Call(const bool acIsDown) const;
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/LuaSandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static constexpr const char* s_cPostInitializeTweakDBProtectedList[] = {

static constexpr const char* s_cPostInitializeModsProtectedList[] = {
// initialized by Scripting
"NewObject", "GetSingleton", "GetMod", "GameDump", "Dump", "DumpType", "DumpAllTypeNames", "DumpVtables", "DumpReflection", "Game",
"NewObject", "GetSingleton", "GetMods", "GetMod", "GameDump", "Dump", "DumpType", "DumpAllTypeNames", "DumpVtables", "DumpReflection", "Game",

// initialized by RTTIMapper
"Vector3", "ToVector3", "Vector4", "ToVector4", "EulerAngles", "ToEulerAngles", "Quaternion", "ToQuaternion", "ItemID", "ToItemID"};
Expand Down
34 changes: 33 additions & 1 deletion src/scripting/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ScriptContext::ScriptContext(LuaSandbox& aLuaSandbox, const std::filesystem::pat
return;
}

m_vkBinds.emplace_back(acID, acDisplayName, wrapDescription(acDescription), wrapHandler(aCallback, acIsHotkey));
m_vkBinds.emplace_back(acID, acDisplayName, wrapDescription(acDescription), wrapHandler(aCallback, acIsHotkey), aCallback);
};

env["registerHotkey"] = sol::overload(
Expand Down Expand Up @@ -294,6 +294,38 @@ sol::object ScriptContext::GetRootObject() const
return m_object;
}

sol::object ScriptContext::GetInputHandlers() const
{
auto lockedState = m_sandbox.GetLockedState();
const auto& bindings = CET::Get().GetBindings();
sol::table res(lockedState.Get(), sol::create);
for (const auto& bind : m_vkBinds)
{
sol::table info(lockedState.Get(), sol::create);
info["id"] = bind.ID;
info["displayName"] = bind.DisplayName;
info["callback"] = bind.VmHandler;
info["isHotkey"] = bind.IsHotkey();
info["isBound"] = bindings.IsBound({m_name, bind.ID});
if (std::holds_alternative<std::string>(bind.Description))
{
info["description"] = std::get<std::string>(bind.Description);
}
res[bind.ID] = info;
}
return res;
}

sol::object ScriptContext::GetMod() const
{
auto lockedState = m_sandbox.GetLockedState();
sol::table modInfo(lockedState.Get(), sol::create);
modInfo["name"] = m_name;
modInfo["rootObject"] = GetRootObject();
modInfo["inputHandlers"] = GetInputHandlers();
return modInfo;
}

void ScriptContext::TriggerOnShutdown() const
{
auto lockedState = m_sandbox.GetLockedState();
Expand Down
2 changes: 2 additions & 0 deletions src/scripting/ScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct ScriptContext
void TriggerOnOverlayClose() const;

[[nodiscard]] sol::object GetRootObject() const;
[[nodiscard]] sol::object GetInputHandlers() const;
[[nodiscard]] sol::object GetMod() const;

private:
void TriggerOnShutdown() const;
Expand Down
13 changes: 12 additions & 1 deletion src/scripting/ScriptStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,22 @@ void ScriptStore::TriggerOnOverlayClose() const
mod.TriggerOnOverlayClose();
}

sol::object ScriptStore::GetMods() const
{
auto lockedState = m_sandbox.GetLockedState();
sol::table res(lockedState.Get(), sol::create);
for (const auto& [name, context] : m_contexts)
{
res[name] = context.GetMod();
}
return res;
}

sol::object ScriptStore::GetMod(const std::string& acName) const
{
const auto it = m_contexts.find(acName);
if (it != m_contexts.cend())
return it->second.GetRootObject();
return it->second.GetMod();

return sol::nil;
}
1 change: 1 addition & 0 deletions src/scripting/ScriptStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct ScriptStore
void TriggerOnOverlayOpen() const;
void TriggerOnOverlayClose() const;

[[nodiscard]] sol::object GetMods() const;
[[nodiscard]] sol::object GetMod(const std::string& acName) const;

private:
Expand Down
10 changes: 10 additions & 0 deletions src/scripting/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ void Scripting::PostInitializeScripting()
return this->GetSingletonHandle(acName, aThisEnv);
};

globals["GetMods"] = [this]() -> sol::object
{
return GetMods();
};

globals["GetMod"] = [this](const std::string& acName) -> sol::object
{
return GetMod(acName);
Expand Down Expand Up @@ -599,6 +604,11 @@ void Scripting::TriggerOnOverlayClose() const
m_store.TriggerOnOverlayClose();
}

sol::object Scripting::GetMods() const
{
return m_store.GetMods();
}

sol::object Scripting::GetMod(const std::string& acName) const
{
return m_store.GetMod(acName);
Expand Down
1 change: 1 addition & 0 deletions src/scripting/Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Scripting
void TriggerOnOverlayOpen() const;
void TriggerOnOverlayClose() const;

sol::object GetMods() const;
sol::object GetMod(const std::string& acName) const;
void UnloadAllMods();
void ReloadAllMods();
Expand Down

0 comments on commit 103c8db

Please sign in to comment.