Skip to content

Commit 2289e9f

Browse files
Add ConvertMod to ModStore/ModList/ModDB
Like ReplaceMod but matches by oldName instead of the new mod's name, so it can change a mod's identity (e.g. FireMin -> ColdMin) rather than just updating its value. ModDB moves the mod between name buckets.
1 parent d8e2290 commit 2289e9f

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/Classes/ModDB.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local ipairs = ipairs
77
local pairs = pairs
88
local select = select
99
local t_insert = table.insert
10+
local t_remove = table.remove
1011
local m_floor = math.floor
1112
local m_min = math.min
1213
local m_max = math.max
@@ -65,6 +66,45 @@ function ModDBClass:ReplaceModInternal(mod)
6566
return false
6667
end
6768

69+
---ConvertModInternal
70+
--- Converts an existing mod with oldName to a new mod with a different name.
71+
--- Moves the mod from the old name's bucket to the new name's bucket.
72+
--- If no matching mod exists, then the function returns false
73+
---@param oldName string @The name of the existing mod to find
74+
---@param mod table @The new mod to replace it with
75+
---@return boolean @Whether any mod was converted
76+
function ModDBClass:ConvertModInternal(oldName, mod)
77+
if not self.mods[oldName] then
78+
if self.parent then
79+
return self.parent:ConvertModInternal(oldName, mod)
80+
end
81+
return false
82+
end
83+
84+
local oldList = self.mods[oldName]
85+
for i = 1, #oldList do
86+
local curMod = oldList[i]
87+
if oldName == curMod.name and mod.type == curMod.type and mod.flags == curMod.flags and mod.keywordFlags == curMod.keywordFlags and mod.source == curMod.source and not curMod.converted then
88+
-- Remove from old name's bucket
89+
t_remove(oldList, i)
90+
-- Add to new name's bucket
91+
local newName = mod.name
92+
if not self.mods[newName] then
93+
self.mods[newName] = { }
94+
end
95+
mod.converted = true
96+
t_insert(self.mods[newName], mod)
97+
return true
98+
end
99+
end
100+
101+
if self.parent then
102+
return self.parent:ConvertModInternal(oldName, mod)
103+
end
104+
105+
return false
106+
end
107+
68108
function ModDBClass:AddList(modList)
69109
local mods = self.mods
70110
for i, mod in ipairs(modList) do

src/Classes/ModList.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ function ModListClass:ReplaceModInternal(mod)
4545
return false
4646
end
4747

48+
---ConvertModInternal
49+
--- Converts an existing mod with oldName to a new mod with a different name.
50+
--- If no matching mod exists, then the function returns false
51+
---@param oldName string @The name of the existing mod to find
52+
---@param mod table @The new mod to replace it with
53+
---@return boolean @Whether any mod was converted
54+
function ModListClass:ConvertModInternal(oldName, mod)
55+
for i, curMod in ipairs(self) do
56+
if oldName == curMod.name and mod.type == curMod.type and mod.flags == curMod.flags and mod.keywordFlags == curMod.keywordFlags and mod.source == curMod.source then
57+
self[i] = mod
58+
return true
59+
end
60+
end
61+
62+
if self.parent then
63+
return self.parent:ConvertModInternal(oldName, mod)
64+
end
65+
66+
return false
67+
end
68+
4869
function ModListClass:MergeMod(mod, skipNonAdditive)
4970
if mod.type == "BASE" or mod.type == "INC" or mod.type == "MORE" then
5071
for i = 1, #self do

src/Classes/ModStore.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ function ModStoreClass:ReplaceMod(...)
110110
end
111111
end
112112

113+
---ConvertMod
114+
--- Converts an existing mod to a new name, replacing it in the store.
115+
--- Finds a mod matching oldName with the same type, flags, keywordFlags, and source as the new mod.
116+
--- If no matching mod exists, the new mod is added instead.
117+
---@param oldName string @The name of the existing mod to convert
118+
---@param ... any @Parameters to be passed along to the modLib.createMod function (new name, type, value, source, ...)
119+
function ModStoreClass:ConvertMod(oldName, ...)
120+
local mod = mod_createMod(...)
121+
if not self:ConvertModInternal(oldName, mod) then
122+
self:AddMod(mod)
123+
end
124+
end
125+
113126
function ModStoreClass:Combine(modType, cfg, ...)
114127
if modType == "MORE" then
115128
return self:More(cfg, ...)

0 commit comments

Comments
 (0)