Advertisement
Guest User

i am vomit

a guest
Aug 2nd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 7.18 KB | None | 0 0
  1. def build_lua(pack, mods, is_base = false)
  2.         lua_start = '
  3.    function errorHandler (error)
  4.        print(error)
  5.        return false
  6.    end
  7.  
  8.    function require_this(path)
  9.    --why
  10.        require(path)
  11.        print("got "..path)
  12.        return true
  13.    end
  14.  
  15.    package.path = package.path ..";./?.lua;./?"
  16.  
  17.  
  18.    -- Rewritten after a Dev suggested I use their github repo
  19.    package.path = package.path .. ";vendor/factorio/factorio-data/core/lualib/?.lua"
  20.    package.path = package.path .. ";vendor/factorio/factorio-data/core/?.lua"
  21.    package.path = package.path .. ";vendor/factorio/factorio-data/base/?.lua"
  22.    package.path = package.path .. ";vendor/factorio/factorio-data/?"
  23.  
  24.    package.path = package.path .. ";vendor/factorio/factorio-data/?.lua"
  25.  
  26.    --package.path = package.path .. ";vendor/factorio/factorio-data/base/?.lua"
  27.    local ppath = package.path
  28.  
  29.    settings = {}
  30.  
  31.    defines = {}
  32.    defines.difficulty_settings = {}
  33.    defines.difficulty_settings.recipe_difficulty = {}
  34.    defines.difficulty_settings.recipe_difficulty.normal = 1
  35.    defines.difficulty_settings.recipe_difficulty.expensive = 2
  36.    defines.difficulty_settings.technology_difficulty = {}
  37.    defines.difficulty_settings.technology_difficulty.normal = 1
  38.    defines.difficulty_settings.technology_difficulty.expensive = 2
  39.    defines.direction = {}
  40.    defines.direction.north = 0
  41.    defines.direction.east = 1
  42.    defines.direction.south = 2
  43.    defines.direction.west = 3
  44.    function log(x) end
  45.  
  46.  
  47.    -- require factorio dataloader and libs
  48.    require("dataloader")
  49.  
  50.    mm = require("mm")
  51.    serpent = require("vendor.factorio.serpent")
  52.    json = require("vendor.factorio.json")
  53.    --require("vendor.factorio.clidebugger")
  54.  
  55.    '
  56.     lua_pt2 = ""
  57.     Dir.entries("vendor/factorio/factorio-data/core/lualib").each do |d|
  58.         next if d == '.' || d == ".."
  59.  
  60.         lua_pt2 += "xpcall(require_this, errorHandler, \"vendor.factorio.factorio-data.core.lualib.#{d[0..-5]}\")\n"
  61.     end
  62.     lua_start += lua_pt2
  63.  
  64.     lua_end =   '
  65.  
  66.  
  67.  
  68.    jsonified = json.encode(data.raw["item"])
  69.    local f = io.open("vendor/factorio/items.json", "w")
  70.    f:write(jsonified, "\n")
  71.    f.close()
  72.  
  73.    jsonified = json.encode(data.raw["fluid"])
  74.    local f = io.open("vendor/factorio/fluids.json", "w")
  75.    f:write(jsonified, "\n")
  76.    f.close()
  77.  
  78.  
  79.    jsonified = json.encode(data.raw["recipe"])
  80.    local f = io.open("vendor/factorio/recipes.json", "w")
  81.    f:write(jsonified, "\n")
  82.    f.close()'
  83.  
  84.         lua_middle = ""
  85.  
  86.         req = /("require\('")/
  87.  
  88.         luas = ["settings.lua", "settings-updates.lua", "settings-final-fixes.lua", "data.lua", "data-updates.lua", "data-final-fixes.lua"]
  89.         luas.each do |l|
  90.             lua_middle = ""
  91.             if l == "data.lua"
  92.                 lua_middle += '
  93.                require("core.data")
  94.                require("base.data")'
  95.             end
  96.             if is_base
  97.                 next
  98.             end
  99.  
  100.             filename = "#{l[0..-5]}".split("-").join()+"_t"
  101.             table_string = "local #{filename} = {"
  102.             mods.each do |m|
  103.                 if File.exist?("vendor/factorio/factorio-data/mod/#{pack}/#{m}/#{l}") == true
  104.                     table_string += "{path = \";vendor/factorio/factorio-data/mod/#{pack}/?.lua;vendor/factorio/factorio-data/mod/#{pack}/#{m}/?.lua\", req = '#{m}.#{l[0..-5]}'},"
  105.                 end
  106.             end
  107.             next if table_string[-1] == "{"
  108.             lua_middle+= "
  109.                #{table_string}}
  110.  
  111.                local a = 0
  112.                while a < 5 do
  113.  
  114.                    local i = 1
  115.                    for i, check in ipairs(#{filename}) do
  116.                        --local check = #{filename}[i]
  117.                        package.path = ppath .. check['path']
  118.                        local pass = xpcall(require_this, errorHandler, check['req'])
  119.                        if pass == true then
  120.                            --table.remove(#{filename}, i)
  121.                        else
  122.                            if i == ##{filename} then
  123.                                --i=1
  124.                            else
  125.                                --i=i+1
  126.                            end
  127.                        end
  128.                    end
  129.                    a=a+1
  130.                end
  131.                if a == 5 then
  132.                    print('skipped some in #{filename}')
  133.                else
  134.                    print('#{filename}')
  135.                end
  136.            "
  137.            
  138.            
  139.             lua_start += lua_middle
  140.         end
  141.        
  142.  
  143.         # build our lua file
  144.         f = File.new("vendor/factorio/get_factorio.lua", 'w')
  145.         f.write("#{lua_start}#{lua_end}")
  146.         f.close
  147.  
  148.         # run our lua script in shell
  149.         `lua vendor/factorio/get_factorio.lua >> errors`
  150.         #exit if pack=="base"
  151. end
  152.  
  153. def extract_mods(pack)
  154.     #Extract all mod data
  155.     zips = Dir.entries("vendor/factorio/modpacks/#{pack}")
  156.     zips.each do |z|
  157.         #binding.pry
  158.         next if z == '.' || z == ".." || z == "base"
  159.         #unzip each zipfile
  160.         zexp = Regexp.new(z[0..-5])
  161.         Zip::File.open("vendor/factorio/modpacks/#{pack}/#{z}") do |f|
  162.             f.each do |e|
  163.                 newname = "#{e.name.gsub(zexp, z[0..z.index("_")-1])}"
  164.                 e.extract("vendor/factorio/factorio-data/mod/#{pack}/#{newname}")
  165.             end
  166.         end
  167.     end
  168.  
  169.     #Load all settings for all mods, in natural sort order
  170.     return Dir.entries("vendor/factorio/factorio-data/mod/#{pack}")
  171. end
  172.  
  173. def get_deps(path)
  174.     f = File.open(path+"/info.json").read
  175.     mods = {}
  176.     json = JSON.parse(f)["dependencies"]
  177.     deps = json.map do |j|
  178.  
  179.         j.match(/[a-z]\S*/)[0]
  180.  
  181.     end
  182. end
  183.  
  184. def sort_mods(mods)
  185.     #first by # dependencies
  186.  
  187.     keys = mods.keys
  188.     keys.sort! do |a,b|
  189.         mods[a].length <=> mods[b].length
  190.     end
  191.     new_keys = [keys[-1]]
  192.     #reverse sort by dependency, most deps go first
  193.     #push E onto stack
  194.     #move all deps of E after
  195.     keys.insert(0, "base")
  196.     i = 0
  197.     while true
  198.         swapped = false
  199.         i=0
  200.         while i < keys.length-1 do
  201.             i+=1
  202.             next if mods[keys[i]].length == 1
  203.             k = 0
  204.             mods[keys[i]].each do |kk|
  205.                 next if kk == "base"
  206.                 a = keys.index {|ak| ak.strip() == kk}
  207.                 if a.nil?
  208.                     next
  209.                 end
  210.                 if a > i
  211.                     keys.insert(i-k, kk)
  212.                     keys.delete_at(keys.rindex(kk))
  213.                     swapped = true
  214.                     k+=1
  215.                 end
  216.             end
  217.             i+=1
  218.         end
  219.         break if swapped == false
  220.     end
  221.  
  222.    # keys.each {|k| puts "#{k} - #{mods[k]}"}
  223.    
  224.     keys
  225.  
  226.  
  227. end
  228.  
  229. def get_mod_order(pack, mods)
  230.     # sort mods by order of dependencies.
  231.     # Base only first, etc etc
  232.     mod_dir = "vendor/factorio/factorio-data/mod/#{pack}/"
  233.     mods_deps = {}
  234.     mods.each do |m|
  235.         next if m == '.' || m == ".."
  236.         mods_deps.merge!({m=>get_deps(mod_dir+m)})
  237.     end
  238.  
  239.     infos = sort_mods(mods_deps)
  240.  
  241.     infos
  242.  
  243. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement