-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod-loader.lua
More file actions
205 lines (178 loc) · 6.02 KB
/
mod-loader.lua
File metadata and controls
205 lines (178 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
mods = {}
modIcons = { unknown = love.graphics.newImage("Mods/BeatblockPlus/unknown.png") }
local function readJsonFromFile(filePath)
local contents = love.filesystem.read(filePath)
return json.decode(contents)
end
local function registerMod(modData)
local mod = {
id = modData.id,
name = modData.name or "Unknown",
author = modData.author or "Unknown",
description = modData.description or "",
version = modData.version or "1.0.0",
enabled = modData.enabled,
config = modData.config or {}
}
mods[mod.id] = mod
print("[BB+] Registered mod '" .. mod.name .. "' by " .. mod.author .. ".")
end
-- files.walk implementation
local function loopFiles(tbl, dir, callback)
local function recurse(currentTable, currentDir)
for _, item in ipairs(love.filesystem.getDirectoryItems(currentDir)) do
local fullPath = currentDir .. "/" .. item
if love.filesystem.getInfo(fullPath, "directory") then
currentTable[item] = currentTable[item] or {}
recurse(currentTable[item], fullPath)
else
local fileName = item:match("([^/]+)$"):gsub("%..+$", "") -- extract the file name without the directory and extension
callback(currentTable, fullPath, fileName)
end
end
end
recurse(tbl, dir)
end
local function printTable(t, indent, title)
if title then
print(title)
end
indent = indent or 0
local indentStr = string.rep(" ", indent)
for k, v in pairs(t) do
if type(v) == "table" then
print(indentStr .. tostring(k) .. ":")
printTable(v, indent + 1)
else
print(indentStr .. tostring(k) .. ": " .. tostring(v))
end
end
end
local function mergeLangFiles(originalLoc, modLoc)
local selectedLanguage = savedata.options.language
for key, value in pairs(modLoc) do
if not originalLoc[key] then
originalLoc[key] = {}
end
originalLoc[key][selectedLanguage] = value
end
end
function tableContains(t, v)
for i = 1, #t do
if (t[i] == v) then
return true
end
end
return false
end
local function getParent(fullPath)
return fullPath:match("(.*/)") or "."
end
function string:endswith(ending)
return ending == "" or self:sub(- #ending) == ending
end
function loadMods() -- loads mod data, assets, mod icons etc.
local modsPath = "Mods"
local success = love.filesystem.getInfo(modsPath, 'directory')
if not success then
print("[BB+] Failed to find Mods directory. Mods won't be loaded.")
return
end
for _, modId in ipairs(love.filesystem.getDirectoryItems(modsPath)) do
local modPath = modsPath .. "/" .. modId
if love.filesystem.getInfo(modPath, 'directory') then
-- load mod data if it exists
local modData
if love.filesystem.getInfo(modPath .. "/mod.json", 'file') then
modData = readJsonFromFile(modPath .. "/mod.json")
registerMod(modData)
end
-- load mod icon if it exists
if love.filesystem.getInfo(modPath .. "/icon.png", 'file') then
local modIcon = love.graphics.newImage(modPath .. "/icon.png")
local width, height = modIcon:getDimensions()
if width ~= 73 or height ~= 33 then
print("[BB+] Mod " .. modId .. " has invalid icon size. Mod icons must be 73x33.")
else
modIcons[modId] = modIcon
end
end
if modData ~= nil and not modData.enabled then
goto continue
end
-- load assets
local assetsPath = modPath .. "/assets"
if love.filesystem.getInfo(assetsPath, 'directory') then
-- load sprites
loopFiles(sprites, assetsPath .. "/textures", function(tbl, path, fileName)
print("[BB+] injecting sprite " .. path .. "...")
tbl[fileName] = love.graphics.newImage(path)
end)
-- load sounds
loopFiles(sounds, assetsPath .. "/sounds", function(tbl, path, fileName)
print("[BB+] injecting sound " .. path .. "...")
tbl[fileName] = love.sound.newSoundData(path)
end)
-- load shaders
loopFiles(shaders, assetsPath .. "/shaders", function(tbl, path, fileName)
print("[BB+] injecting shader " .. path .. "...")
tbl[fileName] = love.graphics.newShader(path)
end)
-- load animations
loopFiles(animations, assetsPath .. "/animations", function(tbl, path, fileName)
if path:endswith(".png") then
print("[BB+] injecting animation " .. path .. "...")
local data = getParent(path) .. "data.json"
if not love.filesystem.getInfo(data, 'file') then
print("[BB+] Error while injecting animation '" .. path .. "'. The '" .. data .. "' file is missing!")
end
tbl[fileName] = ez.newjson(path, data)
end
end)
loopFiles(loc.json, assetsPath .. "/lang", function(tbl, path, fileName)
table.insert(customLanguages, fileName)
-- make sure we don't load english lang when owo is selected
if fileName == savedata.options.language then
print("[BB+] injecting lang file " .. path .. "...")
local modLoc = dpf.loadJson(path, {})
mergeLangFiles(loc.json, modLoc)
end
end)
end
-- load states
loopFiles({}, modPath .. "/states", function(_, path, fileName)
print("[BB+] injecting state " .. path .. "...")
bs.fromPath(fileName, path)
end)
-- load entities
loopFiles({}, modPath .. "/entities", function(_, path, fileName)
print("[BB+] injecting entity " .. path .. "...")
em.new(path, fileName)
end)
-- load and call main.lua
if love.filesystem.getInfo(modPath .. "/main.lua") then
local chunk, errormsg = love.filesystem.load(modPath .. "/main.lua")
if errormsg then
print("[BB+] Error while loading the main.lua file of '" .. modId .. "': " .. errormsg)
else
local env = setmetatable({ modId = modId, modPath = modPath, modData = modData }, { __index = _G })
setfenv(chunk, env)()
end
end
end
::continue::
end
print("[BB+] Finished loading all mods! :D")
printTable(animations, 3, "Animations:")
printTable(sprites, 3, "Sprites:")
printTable(sounds, 3, "Sounds:")
printTable(shaders, 3, "Shaders:")
printTable(modIcons, 3, "Mod icons:")
end
function getModNames()
local modNames = {}
for _, mod in pairs(mods) do
table.insert(modNames, " - " .. mod.name .. " (" .. mod.version .. ") by " .. mod.author)
end
return table.concat(modNames, "\n")
end