Skip to content

Commit

Permalink
update to match expectations from newest lua api changes
Browse files Browse the repository at this point in the history
fix some crashes when shutting down the game
  • Loading branch information
Zarklord committed Jul 30, 2024
1 parent fec0fa6 commit 15fbe84
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 33 deletions.
8 changes: 4 additions & 4 deletions Projects/UniversalPropertyEnhancer/mod/main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AddOnInitializePropManagerFunction(function()
require("upe/palette_icons")
RequireOnAllThreads("upe/property_list_duplicator")
RequireOnAllThreads("upe/property_list_postinit")
RequireOnAllThreads("upe/property_replacer")
modimport("upe/palette_icons")
ModImportOnAllThreads("upe/property_list_duplicator")
ModImportOnAllThreads("upe/property_list_postinit")
ModImportOnAllThreads("upe/property_replacer")
end)
2 changes: 1 addition & 1 deletion Projects/UniversalPropertyEnhancer/upe/palette_icons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ end)
--------------------------------------------API--------------------------------------------
-------------------------------------------------------------------------------------------

function AddPaletteIcon(palette_icon)
function _G.AddPaletteIcon(palette_icon)
table.insert(palette_icons, palette_icon)
end
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ end)
--------------------------------------------API--------------------------------------------
-------------------------------------------------------------------------------------------

function AddDuplicatedPropertyList(target_prop_key, source_prop_key)
function _G.AddDuplicatedPropertyList(target_prop_key, source_prop_key)
duplication_map[target_prop_key] = source_prop_key
end

function ClearDuplicatedPropertyList(target_prop_key)
function _G.ClearDuplicatedPropertyList(target_prop_key)
duplication_map[target_prop_key] = nil
end
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ end)
--------------------------------------------API--------------------------------------------
-------------------------------------------------------------------------------------------

AddPropPostInit_Pre, RemovePropPostInit_Pre, ExecutePropPostInit_Pre = GenerateCallbackExecuter()
AddPropPostInit_Post, RemovePropPostInit_Post, ExecutePropPostInit_Post = GenerateCallbackExecuter()
_G.AddPropPostInit_Pre, _G.RemovePropPostInit_Pre, ExecutePropPostInit_Pre = GenerateCallbackExecuter()
_G.AddPropPostInit_Post, _G.RemovePropPostInit_Post, ExecutePropPostInit_Post = GenerateCallbackExecuter()

function AddPropSpecificPostInit(group_id, instance_id, postinit)
function _G.AddPropSpecificPostInit(group_id, instance_id, postinit)
local group_instances = postinit_groups[group_id]
if not group_instances then
group_instances = {}
Expand All @@ -127,7 +127,7 @@ function AddPropSpecificPostInit(group_id, instance_id, postinit)
group_instances[instance_id] = postinit
end

function RemovePropSpecificPostInit(group_id, instance_id)
function _G.RemovePropSpecificPostInit(group_id, instance_id)
local group_instances = postinit_groups[group_id]
if not group_instances then
return
Expand Down
10 changes: 5 additions & 5 deletions Projects/UniversalPropertyEnhancer/upe/property_replacer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,22 @@ end)
--------------------------------------------API--------------------------------------------
-------------------------------------------------------------------------------------------

AddPropReplacerFunction, RemovePropReplacerFunction, ExecutePropReplacers = GenerateCallbackExecuter()
_G.AddPropReplacerFunction, _G.RemovePropReplacerFunction, ExecutePropReplacers = GenerateCallbackExecuter()

function AddUniversalPropertyReplacer(id, replacer)
function _G.AddUniversalPropertyReplacer(id, replacer)
universal_replacements[id] = replacer
end

function RemoveUniversalPropertyReplacer(id)
function _G.RemoveUniversalPropertyReplacer(id)
universal_replacements[id] = nil
end

function AddTypedPropertyReplacer(property_type, id, replacer)
function _G.AddTypedPropertyReplacer(property_type, id, replacer)
type_replacement_map[property_type] = type_replacement_map[property_type] or {}
type_replacement_map[property_type][id] = replacer
end

function RemoveTypedPropertyReplacer(property_type, id)
function _G.RemoveTypedPropertyReplacer(property_type, id)
local id_replacment_map = type_replacement_map[property_type]
if not id_replacment_map then
return
Expand Down
18 changes: 13 additions & 5 deletions source/Features/PaletteIcons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <Spore/Resource/cResourceManager.h>

static sol::function sGetPaletteIcons;
static sol::function* sGetPaletteIcons = nullptr;

struct PaletteIcon {
uint32_t id;
Expand Down Expand Up @@ -78,21 +78,26 @@ OnLuaInit(sol::state_view s, bool is_main_state)
{
icon.name.SetText(text.tableID, text.instanceID);
}
)
),
sol::meta_function::to_string, [](const PaletteIcons& icon)
{
return string().sprintf("PaletteIcons (%p)", &icon);
}
);

if (!is_main_state) return;

s["AddCustomPaletteIcons"] = [](const sol::function& fn)
{
sGetPaletteIcons = fn;
sGetPaletteIcons = new sol::function(fn);
};
}

OnLuaDispose(sol::state_view s, bool is_main_state)
{
if (!is_main_state) return;
sGetPaletteIcons.reset();
delete sGetPaletteIcons;
sGetPaletteIcons = nullptr;
}

member_detour(LoadPaletteIconProps_detour, PaletteIcons, void())
Expand All @@ -103,7 +108,10 @@ member_detour(LoadPaletteIconProps_detour, PaletteIcons, void())

if (LuaSpore::CanExecuteOnMainState() && sGetPaletteIcons)
{
const sol::table palette_icons = sGetPaletteIcons();
const auto result = sGetPaletteIcons->call();
if (!result.valid()) return;

const sol::table palette_icons = result;
const size_t palette_icons_size = palette_icons.size();

mPaletteIcons.reserve(mPaletteIcons.size() + palette_icons_size);
Expand Down
14 changes: 10 additions & 4 deletions source/Features/PropertyListDuplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ virtual_detour(GetRecordKeyList_detour, Resource::cResourceManager, Resource::IR
{
GetLuaSpore().ExecuteOnFreeState([&func_result, &dst, filter](const sol::state_view& s)
{
if (auto fn = sApplyPropertyListDuplicator.get(s))
if (const auto* fn = sApplyPropertyListDuplicator.get(s))
{
const sol::table record_keys = fn.value()(filter);
const auto result = fn->call(filter);
if (!result.valid()) return;

const sol::table record_keys = result;
const size_t record_keys_size = record_keys.size();

func_result += record_keys_size;
Expand All @@ -78,9 +81,12 @@ virtual_detour(GetRecordKeyList2_detour, Resource::cResourceManager, Resource::I
{
GetLuaSpore().ExecuteOnFreeState([&func_result, &dst, filter](const sol::state_view& s)
{
if (auto fn = sApplyPropertyListDuplicator.get(s))
if (const auto* fn = sApplyPropertyListDuplicator.get(s))
{
const sol::table record_keys = fn.value()(filter);
const auto result = fn->call(filter);
if (!result.valid()) return;

const sol::table record_keys = result;
const size_t record_keys_size = record_keys.size();

func_result += record_keys_size;
Expand Down
4 changes: 2 additions & 2 deletions source/Features/PropertyListPostInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ virtual_detour(GetPropertyList_detour, App::cPropManager, App::IPropManager, boo
{
GetLuaSpore().ExecuteOnFreeState([&pDst, instanceID, groupID](const sol::state_view& s)
{
if (auto fn = sApplyPropertyListPostInit.get(s))
if (const auto* fn = sApplyPropertyListPostInit.get(s))
{
fn.value()(pDst, instanceID, groupID);
fn->call(pDst, instanceID, groupID);
}
});
}
Expand Down
12 changes: 6 additions & 6 deletions source/Features/PropertyReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ member_detour(GetPropertyAlt_detour, App::PropertyList, bool(uint32_t propertyID
{
GetLuaSpore().ExecuteOnFreeState([this, propertyID, &result](const sol::state_view& s)
{
if (auto fn = sApplyPropertyReplacerFunction.get(s))
if (const auto* fn = sApplyPropertyReplacerFunction.get(s))
{
fn.value()(propertyID, result, static_cast<App::PropertyList*>(this));
fn->call(propertyID, result, static_cast<App::PropertyList*>(this));
SetPropertyReplaced(result);
}
});
Expand All @@ -81,9 +81,9 @@ member_detour(GetProperty_detour, App::PropertyList, bool(uint32_t propertyID, A
{
GetLuaSpore().ExecuteOnFreeState([this, propertyID, &result](const sol::state_view& s)
{
if (auto fn = sApplyPropertyReplacerFunction.get(s))
if (const auto* fn = sApplyPropertyReplacerFunction.get(s))
{
fn.value()(propertyID, result, static_cast<App::PropertyList*>(this));
fn->call(propertyID, result, static_cast<App::PropertyList*>(this));
SetPropertyReplaced(result);
}
});
Expand All @@ -104,9 +104,9 @@ member_detour(GetPropertyObject_detour, App::PropertyList, App::Property*(uint32
{
GetLuaSpore().ExecuteOnFreeState([this, propertyID, &result](const sol::state_view& s)
{
if (auto fn = sApplyPropertyReplacerFunction.get(s))
if (const auto* fn = sApplyPropertyReplacerFunction.get(s))
{
fn.value()(propertyID, result, static_cast<App::PropertyList*>(this));
fn->call(propertyID, result, static_cast<App::PropertyList*>(this));
}
});
}
Expand Down

0 comments on commit 15fbe84

Please sign in to comment.