Advertisement
antonsavov

WIP loader

Jan 8th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.98 KB | None | 0 0
  1. -------------------------------
  2. -- /loader --------------------
  3. -------------------------------
  4. --- This package takes care for loading and unloading the other packages to the global environment
  5.  
  6. -------------------------------
  7. --- PRIVATE FUNCTIONS ---------
  8. -------------------------------
  9. --os.loadAPI('lib/utils')
  10.  
  11. local added_names = {} --to keep track what has been added to the global environment so we can remove it on exit
  12. local package_folder = '/lib' -- where are the other packages to load
  13.  
  14. --- Adds the contents of table t to the global table _G
  15. -- protects the existing contents of _G
  16. local function addTableToGlobal(t)
  17.     assert(type(t)=='table', "addTableToGlobal():\nExpected table got "..type(t))
  18.     for name,value in pairs(t) do
  19.         assert(_G[name] == nil, "addTableToGlobal():\nGlobal variable already exists: "..name.."\nTry rebooting computer with 'reboot'")
  20.         _G[name] = value
  21.         table.insert(added_names, name)
  22.     end
  23. end
  24.  
  25. --- we have this in here too to avoid the dependency on "utils"
  26. --TODO maybe move all file operations to this package???
  27. local function file_exists(filename)
  28.     local f = fs.open(filename, "rb")
  29.     if f then f:close() end
  30.     return f ~= nil
  31. end
  32.  
  33. --- getTableFromAPI(_filename, _folder )
  34. local function getTableFromAPI(_filename, _folder )
  35.     local fullname = fs.combine( _folder or "", _filename )
  36.     local package_table = nil
  37.    
  38.     --
  39.     if file_exists(fullname) then
  40.         os.loadAPI(fullname) --load the API
  41.         package_table = _G[_filename] --get the API from the global environment
  42.         _G[_filename] = nil --clean up the global environment
  43.     end
  44.    
  45.     return package_table --returns the API as a table
  46. end
  47.  
  48. --- Loads all packages found in @path to the global table _G
  49. -- TODO add a function unload() to the loader API that must be run when user presses the key to quite the game. unload() cleans the _G table
  50. local function loadPackagesFromFolder(path)
  51.     local packages = fs.list(path)
  52.     for i,package_name in ipairs(packages) do
  53.         if not fs.isDir(package_name) then
  54.             print("Found file "..package_name)
  55.             if package_name ~= '.DS_Store' then --filters out a weird macOS file
  56.                 print("Loading API: "..package_name)
  57.                 addTableToGlobal(getTableFromAPI(package_name, path )) -- avoid poluting the global environment with the packages in two locations
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. -------------------------------
  64. --- PUBLIC API ----------------
  65. -------------------------------
  66.  
  67. --[[
  68. --- loadPackage( _filename, _folder )
  69. function loadPackage( _filename, _folder )
  70.     local fullname = fs.combine( _folder or "", _filename )
  71.     print("Loading API: "..fullname)
  72.     os.loadAPI(fullname)
  73.     local package_table = _G[package_name]
  74.     addTableToGlobal(package_table)
  75. end
  76. --]]
  77.  
  78.  
  79.  
  80. --- loadPackages()
  81. function loadPackages()
  82.     loadPackagesFromFolder(package_folder)
  83. end
  84.  
  85. --- unloadPackages()
  86. function unloadPackages()
  87.     for i,name in ipairs(added_names) do
  88.         _G[name] = nil
  89.     end
  90. end
  91.  
  92. --- createFileFromDefaultFile(_filename, _defaults_filename, settings_folder, forced)
  93. local function createFileFromDefaultFile(_filename, _defaults_filename, settings_folder, forced)
  94.     local fullname = fs.combine(settings_folder or "", _filename)
  95.     local fullname_default = fs.combine(settings_folder or "", _defaults_filename) 
  96.     if (not file_exists( fullname)) or forced then
  97.         --copies from default file
  98.         fs.copy( fullname_default, fullname )
  99.     end
  100. end
  101.  
  102. --- Reads the game settings and last_settings from files
  103. -- Checks if settings file exist and if not creates it using the default settings
  104. -- at the end overwrites the last_settings file with the current settings
  105. function getGameSettings()
  106.     -- if settings file in root folder doesn't exist then
  107.     local current_settings_filename = "settings"
  108.     local default_settings_filename = "default_settings"
  109.     local last_settings_filename = "last_settings"
  110.     --local settings = {}
  111.     createFileFromDefaultFile(current_settings_filename, default_settings_filename)
  112.     local current_settings = getTableFromAPI(current_settings_filename)
  113.     local last_settings = getTableFromAPI(last_settings_filename)
  114.     createFileFromDefaultFile(last_settings_filename, current_settings_filename, true) --force the creation of last_settings file from the current settings
  115.     return current_settings, last_settings
  116. end
  117.  
  118. ---
  119. function getStartingItems(settings)
  120.     local STARTING_ITEMS = {}
  121.     local filename = "starting_items"
  122.     local items
  123.     if not file_exists(filename) then  
  124.         local file = fs.open(filename,"w")
  125.         file.write(json.encodePretty(settings.default_starting_items))
  126.         file.close()
  127.         items = settings.default_starting_items
  128.     else
  129.         items = json.decodeFromFile(filename)
  130.     end
  131.     local startingItemsTable = {}
  132.     local CanPlaceOnBlocks = {}
  133.     --check the read items and also add their block type to the table with blocks on which any item in the game can be placed
  134.     for i, item in ipairs(items) do
  135.         if item.block and item.variant and item.amount then
  136.             CanPlaceOnBlocks[item.block] = true
  137.             table.insert(startingItemsTable,item)
  138.         end
  139.     end
  140.     -- convert the starting items table to minecraft strings with the correct PlaceOn tags
  141.     -- all items can be placed on the white grid, the plug, the detector and on themselves
  142.     local blocksList = ''  
  143.     for block in pairs(CanPlaceOnBlocks) do
  144.         blocksList = blocksList..',"'..block..'"'
  145.     end
  146.     for i,item in ipairs(startingItemsTable) do
  147.         --print(item.block)
  148.         --print(item.amount)
  149.         --print(item.variant)
  150.         --print(BLOCKS.WHITE_GRID.block)
  151.         --print(BLOCKS.PLUG.block)
  152.         --print(BLOCKS.DETECT.block)
  153.         --print(blocksList)
  154.         local item_as_string = item.block..' '..item.amount..' '..item.variant..' {CanPlaceOn:["'..BLOCKS.WHITE_GRID.block..'","'..BLOCKS.PLUG.block..'","'..BLOCKS.DETECT.block..'"'..blocksList..']}'
  155.         table.insert(STARTING_ITEMS,item_as_string)
  156.     end
  157.     local pickaxe = 'stone_pickaxe 1 '..131-PICKAXE_USES..' {CanDestroy:['..blocksList:sub(2)..'],display:{Name:"Resource Remover",Lore:[Use the pickaxe to remove up to '..PICKAXE_USES..' resource blocks]}}'
  158.     table.insert(STARTING_ITEMS,pickaxe)
  159.     return STARTING_ITEMS
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement