Skip to content

Commit 84537ef

Browse files
committed
Properly implement obj_buffer/table on subclasses, remove ctype_buffer bandaid. Addresses #1066
1 parent 1b7a6d8 commit 84537ef

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

lovely/center.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ pattern = "(?<indent>[\t ]*)for _, v in pairs\\(G.DISCOVER_TALLIES\\) do"
305305
line_prepend = '$indent'
306306
position = 'before'
307307
payload = '''
308-
for _, v in ipairs(SMODS.ConsumableType.ctype_buffer) do
308+
for _, v in ipairs(SMODS.ConsumableType.obj_buffer) do
309309
G.DISCOVER_TALLIES[v:lower()..'s'] = {tally = 0, of = 0}
310310
end'''
311311

@@ -359,7 +359,7 @@ match_indent = true
359359
position = 'at'
360360
payload = '''
361361
local total_rate = G.GAME.joker_rate + G.GAME.playing_card_rate
362-
for _,v in ipairs(SMODS.ConsumableType.ctype_buffer) do
362+
for _,v in ipairs(SMODS.ConsumableType.obj_buffer) do
363363
total_rate = total_rate + G.GAME[v:lower()..'_rate']
364364
end'''
365365

@@ -372,7 +372,7 @@ position = 'at'
372372
payload = '''
373373
-- need to preserve order to leave RNG unchanged
374374
local rates = $li
375-
for _, v in ipairs(SMODS.ConsumableType.ctype_buffer) do
375+
for _, v in ipairs(SMODS.ConsumableType.obj_buffer) do
376376
if not (v == 'Tarot' or v == 'Planet' or v == 'Spectral') then
377377
table.insert(rates, { type = v, val = G.GAME[v:lower()..'_rate'] })
378378
end

lovely/playing_card.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ payload = '''
114114
for _, v in ipairs(SMODS.Gradient.obj_buffer) do
115115
G.ARGS.LOC_COLOURS[v:lower()] = SMODS.Gradients[v]
116116
end
117-
for _, v in ipairs(SMODS.ConsumableType.ctype_buffer) do
117+
for _, v in ipairs(SMODS.ConsumableType.obj_buffer) do
118118
G.ARGS.LOC_COLOURS[v:lower()] = G.C.SECONDARY_SET[v]
119119
end
120120
for _, v in ipairs(SMODS.Suit.obj_buffer) do

src/game_object.lua

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,27 @@ Set `prefix_config.key = false` on your object instead.]]):format(obj.key), obj.
121121
return false
122122
end
123123

124+
function SMODS.GameObject:__internal_register(obj, class_to_compare)
125+
-- only add the object to each unique table/buffer once
126+
if self.obj_table ~= class_to_compare.obj_table then
127+
self.obj_table[obj.key] = obj
128+
end
129+
if self.obj_buffer ~= class_to_compare.obj_buffer then
130+
self.obj_buffer[#self.obj_buffer+1] = obj.key
131+
end
132+
end
133+
124134
function SMODS.GameObject:register()
125135
if self:check_dependencies() then
126-
self.obj_table[self.key] = self
127-
self.obj_buffer[#self.obj_buffer + 1] = self.key
136+
-- start with this class and propagate up to all parent classes that can have objects
137+
self:__internal_register(self, {})
138+
local parent = self.super or {}
139+
local child = self
140+
while parent.obj_buffer and parent.obj_table do
141+
parent:__internal_register(self, child)
142+
parent = parent.super or {}
143+
child = child.super
144+
end
128145
self.registered = true
129146
end
130147
end
@@ -1033,10 +1050,13 @@ Set `prefix_config.key = false` on your object instead.]]):format(obj.key), obj.
10331050
-------------------------------------------------------------------------------------------------
10341051
------- API CODE GameObject.ConsumableType
10351052
-------------------------------------------------------------------------------------------------
1036-
1053+
local ctype_buffer = {}
10371054
SMODS.ConsumableTypes = {}
10381055
SMODS.ConsumableType = SMODS.ObjectType:extend {
1039-
ctype_buffer = {},
1056+
obj_table = SMODS.ConsumableTypes,
1057+
obj_buffer = ctype_buffer,
1058+
--DEPRECATED
1059+
ctype_buffer = ctype_buffer,
10401060
visible_buffer = {},
10411061
set = 'ConsumableType',
10421062
required_params = {
@@ -1056,13 +1076,12 @@ Set `prefix_config.key = false` on your object instead.]]):format(obj.key), obj.
10561076
register = function(self)
10571077
SMODS.ConsumableType.super.register(self)
10581078
if self:check_dependencies() then
1059-
SMODS.ConsumableType.ctype_buffer[#SMODS.ConsumableType.ctype_buffer+1] = self.key
1079+
-- this is duplicate information but it's more convenient to keep
10601080
if not self.no_collection then SMODS.ConsumableType.visible_buffer[#SMODS.ConsumableType.visible_buffer + 1] = self.key end
10611081
end
10621082
end,
10631083
inject = function(self)
10641084
SMODS.ObjectType.inject(self)
1065-
SMODS.ConsumableTypes[self.key] = self
10661085
G.localization.descriptions[self.key] = G.localization.descriptions[self.key] or {}
10671086
G.C.SET[self.key] = self.primary_colour
10681087
G.C.SECONDARY_SET[self.key] = self.secondary_colour

0 commit comments

Comments
 (0)