Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add priority system to loader #45

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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