Yanis4224

gz-gui/lua/patch_iso.lua

Oct 17th, 2024 (edited)
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.47 KB | None | 0 0
  1. function usage()
  2. io.stderr:write("usage: make-iso [<gzinject-arg>...] [--no-trim]"
  3.                 .. " [-m <input-rom>] [--mq-rom <input-rom>] [-o <output-iso>] <input-iso>\n")
  4. os.exit(1)
  5. end
  6.  
  7. -- parse arguments
  8. local arg = {...}
  9. local opt_id
  10. local opt_title
  11. local opt_directory = "isoextract"
  12. local opt_raphnet
  13. local opt_disable_controller_remappings
  14. local opt_no_trim
  15. local opt_rom
  16. local opt_mq_rom
  17. local opt_out
  18. local opt_iso
  19. local opt_sub
  20. while arg[1] do
  21. if arg[1] == "-i" or arg[1] == "--gameid" then
  22.     opt_id = arg[2]
  23.     if opt_id == nil then usage() end
  24.     table.remove(arg, 1)
  25.     table.remove(arg, 1)
  26. elseif arg[1] == "-t" or arg[1] == "--gamename" then
  27.     opt_title = arg[2]
  28.     if opt_title == nil then usage() end
  29.     table.remove(arg, 1)
  30.     table.remove(arg, 1)
  31. elseif arg[1] == "-d" or arg[1] == "--directory" then
  32.     opt_directory = arg[2]
  33.     if opt_directory == nil then usage() end
  34.     table.remove(arg, 1)
  35.     table.remove(arg, 1)
  36. elseif arg[1] == "--raphnet" then
  37.     opt_raphnet = true
  38.     table.remove(arg, 1)
  39. elseif arg[1] == "--disable-controller-remappings" then
  40.     opt_disable_controller_remappings = true
  41.     table.remove(arg, 1)
  42. elseif arg[1] == "--no-trim" then
  43.     opt_no_trim = true
  44.     table.remove(arg, 1)
  45. elseif arg[1] == "-m" then
  46.     opt_rom = arg[2]
  47.     if opt_rom == nil then usage() end
  48.     table.remove(arg, 1)
  49.     table.remove(arg, 1)
  50. elseif arg[1] == "--mq-rom" then
  51.     opt_mq_rom = arg[2]
  52.     if opt_mq_rom == nil then usage() end
  53.     table.remove(arg, 1)
  54.     table.remove(arg, 1)
  55. elseif arg[1] == "-o" then
  56.     opt_out = arg[2]
  57.     if opt_out == nil then usage() end
  58.     table.remove(arg, 1)
  59.     table.remove(arg, 1)
  60. elseif arg[1] == "-s" then
  61.     opt_sub = true
  62.     table.remove(arg, 1)
  63. elseif opt_iso ~= nil then usage()
  64. else
  65.     opt_iso = arg[1]
  66.     table.remove(arg, 1)
  67. end
  68. end
  69. if opt_iso == nil then usage() end
  70.  
  71. local gzinject = os.getenv("GZINJECT")
  72. if gzinject == nil or gzinject == "" then gzinject = "gzinject" end
  73.  
  74. wiivc = true
  75. require("lua/rom_table")
  76. local make = loadfile("lua/make.lua")
  77.  
  78. -- extract iso
  79. gru.os_rm(opt_directory)
  80. local gzinject_cmd = gzinject ..
  81.                     " -a extract" ..
  82.                     " -d \"" .. opt_directory .. "\"" ..
  83.                     " -s \"" .. opt_iso .. "\"" ..
  84.                     " --verbose"
  85. if opt_sub then gzinject_cmd = gzinject_cmd .. " 1>&2" end
  86. local _,_,gzinject_result = os.execute(gzinject_cmd)
  87. if gzinject_result ~= 0 then return gzinject_result end
  88.  
  89. -- check gc version
  90. local gc_header = gru.blob_load(opt_directory .. "/header.bin")
  91. local gc_version = gc_table[gc_header:crc32()]
  92. if gc_version == nil then error("unrecognized gc version") end
  93.  
  94. -- check rom id
  95. if opt_rom == nil then opt_rom = opt_directory .. "/" .. gc_version.rom_path end
  96. local rom = gru.n64rom_load(opt_rom)
  97. local rom_info = rom_table[rom:crc32()]
  98. if rom_info == nil then
  99.   io.stderr:write("patch-iso: unrecognized rom: " .. opt_rom .. "\n")
  100.   return 2
  101. end
  102.  
  103. -- patch rom
  104. local patch = gru.ups_load("ups/" .. rom_info.gz_name .. ".ups")
  105. patch:apply(rom)
  106. rom:save_file(opt_directory .. "/" .. gc_version.rom_path)
  107.  
  108. -- patch MQ rom (if available)
  109. if gc_version.mq_rom_path ~= nil then
  110. if opt_mq_rom == nil then opt_mq_rom = opt_directory .. "/" .. gc_version.mq_rom_path end
  111. local mq_rom = gru.n64rom_load(opt_mq_rom)
  112. local mq_rom_info = rom_table[mq_rom:crc32()]
  113. if mq_rom_info == nil then
  114.   io.stderr:write("patch-iso: unrecognized mq rom: " .. opt_mq_rom .. "\n")
  115.   return 2
  116. end
  117. local mq_patch = gru.ups_load("ups/" .. mq_rom_info.gz_name .. ".ups")
  118. mq_patch:apply(mq_rom)
  119. mq_rom:save_file(opt_directory .. "/" .. gc_version.mq_rom_path)
  120. end
  121.  
  122. -- trim unnecessary files to save space
  123. if not opt_no_trim then
  124.   for _, trim_path in ipairs(gc_version.trim_paths) do
  125.     gru.os_rm(opt_directory .. "/" .. trim_path)
  126.   end
  127. end
  128.  
  129. -- build gzinject pack command string
  130. local gzinject_cmd = gzinject ..
  131.                     " -a pack" ..
  132.                     " -d \"" .. opt_directory .. "\"" ..
  133.                     " --verbose"
  134. if opt_id ~= nil then
  135. gzinject_cmd = gzinject_cmd .. " -i \"" .. opt_id .. "\""
  136. else
  137. gzinject_cmd = gzinject_cmd .. " -i " .. rom_info.gc_game_id
  138. end
  139. if opt_title ~= nil then
  140. gzinject_cmd = gzinject_cmd .. " -t \"" .. opt_title .. "\""
  141. else
  142. gzinject_cmd = gzinject_cmd .. " -t " .. rom_info.gz_name
  143. end
  144. gzinject_cmd = gzinject_cmd ..
  145.                 " -p \"gzi/homeboy/hb_" .. gc_version.game_id .. ".gzi\"" ..
  146.                 " --dol-iso-path \"" .. gc_version.dol_path .. "\"" ..
  147.                 " --dol-inject \"homeboy/bin/hb-" .. gc_version.game_id .. "/homeboy.bin\"" ..
  148.                 " --dol-loading 80300000"
  149. if not opt_disable_controller_remappings then
  150. if opt_raphnet then
  151.     gzinject_cmd = gzinject_cmd .. " -p \"gzi/controller/gz_remap_raphnet_" .. gc_version.game_id .. ".gzi\""
  152. else
  153.     gzinject_cmd = gzinject_cmd .. " -p \"gzi/controller/gz_remap_default_" .. gc_version.game_id .. ".gzi\""
  154. end
  155. end
  156. if opt_out ~= nil then
  157. gzinject_cmd = gzinject_cmd .. " -s \"" .. opt_out .. "\""
  158. elseif opt_title ~= nil then
  159. gzinject_cmd = gzinject_cmd .. " -s \"" .. opt_title .. ".iso\""
  160. else
  161. gzinject_cmd = gzinject_cmd .. " -s \"" .. rom_info.gz_name .. ".iso\""
  162. end
  163. if opt_sub then gzinject_cmd = gzinject_cmd .. " 1>&2" end
  164. -- execute
  165. local _,_,gzinject_result = os.execute(gzinject_cmd)
  166. if gzinject_result ~= 0 then return gzinject_result end
  167.  
  168. if opt_sub then print(rom_info.gz_name .. ".iso") end
  169.  
  170. return 0
  171.  
Advertisement
Add Comment
Please, Sign In to add comment