Skip to content

Commit

Permalink
Merge pull request #45 from Aurelius7309/prio
Browse files Browse the repository at this point in the history
Add priority system to loader
  • Loading branch information
Steamopollys authored Mar 24, 2024
2 parents 4961ff6 + 40c598a commit a5b9509
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions loader/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
------------MOD LOADER------------------------

SMODS.INIT = {}
SMODS._MOD_PRIO_MAP = {}
SMODS._INIT_PRIO_MAP = {}
SMODS._INIT_KEYS = {}

function loadMods(modsDirectory)
local mods = {}
Expand Down Expand Up @@ -33,6 +36,8 @@ function loadMods(modsDirectory)
elseif headerLine == "--- STEAMODDED HEADER" then
-- Extract individual components from the header
local modName, modID, modAuthorString, modDescription = fileContent:match("%-%-%- MOD_NAME: ([^\n]+)\n%-%-%- MOD_ID: ([^\n]+)\n%-%-%- MOD_AUTHOR: %[(.-)%]\n%-%-%- MOD_DESCRIPTION: ([^\n]+)")
local priority = fileContent:match("%-%-%- PRIORITY: (%-?%d+)")
priority = priority and priority + 0 or 0

-- Validate MOD_ID to ensure it doesn't contain spaces
if modID and string.find(modID, " ") then
Expand All @@ -53,12 +58,13 @@ function loadMods(modsDirectory)
id = modID,
author = modAuthorArray,
description = modDescription,
path = directory .. "/" -- Store the directory path
path = directory .. "/", -- Store the directory path
priority = priority,
})
modIDs[modID] = true -- Mark this ID as used

-- Load the mod file
assert(load(fileContent))()
SMODS._MOD_PRIO_MAP[priority] = SMODS._MOD_PRIO_MAP[priority] or {}
table.insert(SMODS._MOD_PRIO_MAP[priority], fileContent)
end
end
else
Expand All @@ -70,15 +76,42 @@ function loadMods(modsDirectory)

-- Start processing with the initial directory at depth 1
processDirectory(modsDirectory, 1)

-- sort by priority
local keyset = {}
for k, _ in pairs(SMODS._MOD_PRIO_MAP) do
keyset[#keyset + 1] = k
end
table.sort(keyset)

-- load the mod files
for _,priority in ipairs(keyset) do
for __,v in ipairs(SMODS._MOD_PRIO_MAP[priority]) do
assert(load(v))()
-- set priority of added init functions
for modName, initFunc in pairs(SMODS.INIT) do
if type(initFunc) == 'function' and SMODS._INIT_KEYS[modName] == nil then
SMODS._INIT_PRIO_MAP[priority] = SMODS._INIT_PRIO_MAP[priority] or {}
table.insert(SMODS._INIT_PRIO_MAP[priority], modName)
SMODS._INIT_KEYS[modName] = true
end
end
end
end

return mods
end

function initMods()
for modName, initFunc in pairs(SMODS.INIT) do
if type(initFunc) == "function" then
sendDebugMessage("Launch Init Function for: " .. modName .. ".")
initFunc()
local keyset = {}
for k, _ in pairs(SMODS._INIT_PRIO_MAP) do
keyset[#keyset + 1] = k
end
table.sort(keyset)
for _,k in ipairs(keyset) do
for _, modName in ipairs(SMODS._INIT_PRIO_MAP[k]) do
sendDebugMessage("Launch Init Function for: " .. modName .. ".")
SMODS.INIT[modName]()
end
end
end
Expand Down

0 comments on commit a5b9509

Please sign in to comment.