Guest User

customAssetHelper.lua

a guest
Jul 19th, 2024
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. --[[
  2. customAssetHelper.lua
  3. v0.1.0
  4. system for defining custom SMBX2 assets types similar with similar folder structures to costumes.
  5. written by rixithechao, partially derived from the basegame costume framework
  6. --]]
  7.  
  8. local lunajson = require("ext/lunajson")
  9.  
  10. local assetHelper = {}
  11.  
  12.  
  13.  
  14. -- ASSET SET METHODS
  15. local function loadLunaFile(path, prefix)
  16.     local luafile = nil;
  17.     local func, err = loadfile(path)
  18.     if(func)then
  19.         luafile = func()
  20.         if(type(luafile) ~= "table")then
  21.             error(prefix.." Lua file \""..path.."\" did not return the table (got "..type(luafile)..")", 2)
  22.         end
  23.     else
  24.         if(not err:find("such file"))then
  25.             error(err,2)
  26.         end
  27.     end
  28.            
  29.     if(not luafile) then error(prefix.." Lua file failed to load correctly: \""..path.."\"",2) end
  30.  
  31.     return luafile;
  32. end
  33.  
  34. local function tryLunaEvent(self, evName, ...)
  35.     local currentLuna = self.currentLuna
  36.  
  37.     if  currentLuna ~= nil  and  currentLuna[evName] ~= nil  and  type(currentLuna[evName]) == "function"  then
  38.         if  type(arg) == "table"  then
  39.             currentLuna[evName](...)
  40.         else
  41.             currentLuna[evName]()
  42.         end
  43.     end
  44. end
  45.  
  46. local function cleanup(self, ...)
  47.  
  48.     -- Clean up previous lua
  49.     self:tryLunaEvent("onCleanup", ...)
  50.     if(self.currentLuna ~= nil) then
  51.         clearEvents(self.currentLuna)
  52.     end
  53.  
  54.     self.currentLuna = nil
  55. end
  56.  
  57.  
  58.  
  59.  
  60. -- METHODS
  61.  
  62. --[[ registerAssetType args
  63.     folder      string                         the folder the assets are stored in
  64.     prefix      string                         the prefix for the global constants
  65.     jsonfunc    function (jsonData,k,v,path)   a function for processing and adding to the imported json data
  66.     images      list of strings                image filenames to check for
  67.     singleluna  bool                           if true, functionality for managing a current luna.lua file will be included
  68. ]]
  69. function assetHelper.importAssets(args)
  70.     local assets = {}
  71.     local lookup = {}
  72.  
  73.     -- Loop through alll subfolders
  74.     for  k,v in ipairs(Misc.listDirectories(Misc.episodePath().."/"..args.folder))  do
  75.  
  76.         -- Ignore default (why do I have this check again)
  77.         if  v ~= "default"  then
  78.  
  79.            
  80.             -- CREATE THE PREFIX_NAME CONSTANT
  81.             _G[args.prefix:upper().."_"..v:upper()] = k
  82.  
  83.  
  84.             -- GET THE PATHS
  85.             local absPath = Misc.episodePath()..args.folder.."\\"..v
  86.             local path = args.folder.."/"..v
  87.    
  88.    
  89.             -- IMPORT LUA
  90.             local lunaTbl = nil
  91.             local lunaFile = Misc.resolveFile(path.."\\luna.lua")
  92.             if  lunaFile ~= nil  then
  93.                 lunaTbl = loadLunaFile(lunaFile, args.prefix)
  94.             end
  95.    
  96.  
  97.             -- IMPORT CONFIG JSON
  98.             local jsonData = {}
  99.             if  Misc.resolveFile(path.."\\config.json") ~= nil  then
  100.                 local f = io.open(absPath.."\\config.json", "r")
  101.                 local content = f:read("*all")
  102.                 f:close()
  103.                 jsonData = lunajson.decode(content)
  104.             end
  105.  
  106.  
  107.             -- IMPORT SPECIFIC IMAGES
  108.             if  args.images ~= nil  then
  109.                 jsonData.img = {}
  110.  
  111.                 for  k,v in ipairs(args.images)  do
  112.                     local resolvePath = path.."/"..v..".png"
  113.                     if  Misc.resolveFile(resolvePath)  then
  114.                         jsonData.img[v] = Graphics.loadImageResolved(resolvePath)
  115.                     end
  116.                 end
  117.             end
  118.  
  119.  
  120.             -- PROCESS JSON DATA, LOAD EXTRA FILES, ETC
  121.             if  args.jsonfunc ~= nil  then
  122.                 args.jsonfunc(jsonData, k,v,path)
  123.             end
  124.  
  125.    
  126.             -- FINAL ASSET TABLE
  127.             local thisAsset = table.join({
  128.                 key = v,
  129.                 path = path,
  130.                 absPath = absPath,
  131.                 luna = lunaTbl
  132.             }, jsonData)
  133.    
  134.    
  135.             -- INDEX THE ASSET
  136.             assets[k] = thisAsset
  137.             lookup[v] = k
  138.         end
  139.     end
  140.  
  141.     assets._lookup = lookup
  142.  
  143.     -- Luna.lua management functionality
  144.     if  args.singleluna  then
  145.         assets.tryLunaEvent = tryLunaEvent
  146.         assets.cleanup = cleanup
  147.     end
  148.  
  149.  
  150.     -- Return
  151.     return assets
  152. end
  153.  
  154.  
  155.  
  156.  
  157. return assetHelper
Advertisement
Add Comment
Please, Sign In to add comment