Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- customAssetHelper.lua
- v0.1.0
- system for defining custom SMBX2 assets types similar with similar folder structures to costumes.
- written by rixithechao, partially derived from the basegame costume framework
- --]]
- local lunajson = require("ext/lunajson")
- local assetHelper = {}
- -- ASSET SET METHODS
- local function loadLunaFile(path, prefix)
- local luafile = nil;
- local func, err = loadfile(path)
- if(func)then
- luafile = func()
- if(type(luafile) ~= "table")then
- error(prefix.." Lua file \""..path.."\" did not return the table (got "..type(luafile)..")", 2)
- end
- else
- if(not err:find("such file"))then
- error(err,2)
- end
- end
- if(not luafile) then error(prefix.." Lua file failed to load correctly: \""..path.."\"",2) end
- return luafile;
- end
- local function tryLunaEvent(self, evName, ...)
- local currentLuna = self.currentLuna
- if currentLuna ~= nil and currentLuna[evName] ~= nil and type(currentLuna[evName]) == "function" then
- if type(arg) == "table" then
- currentLuna[evName](...)
- else
- currentLuna[evName]()
- end
- end
- end
- local function cleanup(self, ...)
- -- Clean up previous lua
- self:tryLunaEvent("onCleanup", ...)
- if(self.currentLuna ~= nil) then
- clearEvents(self.currentLuna)
- end
- self.currentLuna = nil
- end
- -- METHODS
- --[[ registerAssetType args
- folder string the folder the assets are stored in
- prefix string the prefix for the global constants
- jsonfunc function (jsonData,k,v,path) a function for processing and adding to the imported json data
- images list of strings image filenames to check for
- singleluna bool if true, functionality for managing a current luna.lua file will be included
- ]]
- function assetHelper.importAssets(args)
- local assets = {}
- local lookup = {}
- -- Loop through alll subfolders
- for k,v in ipairs(Misc.listDirectories(Misc.episodePath().."/"..args.folder)) do
- -- Ignore default (why do I have this check again)
- if v ~= "default" then
- -- CREATE THE PREFIX_NAME CONSTANT
- _G[args.prefix:upper().."_"..v:upper()] = k
- -- GET THE PATHS
- local absPath = Misc.episodePath()..args.folder.."\\"..v
- local path = args.folder.."/"..v
- -- IMPORT LUA
- local lunaTbl = nil
- local lunaFile = Misc.resolveFile(path.."\\luna.lua")
- if lunaFile ~= nil then
- lunaTbl = loadLunaFile(lunaFile, args.prefix)
- end
- -- IMPORT CONFIG JSON
- local jsonData = {}
- if Misc.resolveFile(path.."\\config.json") ~= nil then
- local f = io.open(absPath.."\\config.json", "r")
- local content = f:read("*all")
- f:close()
- jsonData = lunajson.decode(content)
- end
- -- IMPORT SPECIFIC IMAGES
- if args.images ~= nil then
- jsonData.img = {}
- for k,v in ipairs(args.images) do
- local resolvePath = path.."/"..v..".png"
- if Misc.resolveFile(resolvePath) then
- jsonData.img[v] = Graphics.loadImageResolved(resolvePath)
- end
- end
- end
- -- PROCESS JSON DATA, LOAD EXTRA FILES, ETC
- if args.jsonfunc ~= nil then
- args.jsonfunc(jsonData, k,v,path)
- end
- -- FINAL ASSET TABLE
- local thisAsset = table.join({
- key = v,
- path = path,
- absPath = absPath,
- luna = lunaTbl
- }, jsonData)
- -- INDEX THE ASSET
- assets[k] = thisAsset
- lookup[v] = k
- end
- end
- assets._lookup = lookup
- -- Luna.lua management functionality
- if args.singleluna then
- assets.tryLunaEvent = tryLunaEvent
- assets.cleanup = cleanup
- end
- -- Return
- return assets
- end
- return assetHelper
Advertisement
Add Comment
Please, Sign In to add comment