Files
gen1recomp/src/sync/SyncMods.lua
T
bryanthaboi ca4d3d283c Add Pokemon Crystal as a sixth supported version
Crystal boots from a user-supplied ROM, imports a full cache and is playable:
copyright, the Crystal intro movie, the animated title, gender select, Oak,
and out into Johto. 122 of the cart's 169 script specials are implemented.

Import and data
- tools/make_crystal_manifest.py derives the manifest by importing
  make_gold_manifest as a library, with three additive keyword seams. Gold and
  Silver still regenerate byte-identical, which is the standing requirement for
  touching that generator.
- crystal_symbol_deltas.py and crystal_movie_symbols.py carry the symbol delta:
  Crystal renames the credits mons, splits the trainer card, Pokegear and
  pack-pal blocks by gender, and replaces the intro and title outright.
- Crystal-only manifest keys: engineFlagOrder (162 flags to Gold's 93, so the
  badge block sits one higher) and unownCharmap (the main charmap parser stops
  at the first newcharmap so the two cannot contaminate each other).

Extractor
- RomExtractorGen2 becomes three-edition. Crystal corrections: PAL_MAP_BANK
  0x13, a flat PICS_FIX pic bank, audio bank 0x5e, the mapSongs id-100 hole,
  seven NPC trades, a TradeTexts stride of 8, the five Crystal tileset anim
  steps with per-row degrade, and the column-major trainer card portraits.
- New: animated front sprites (frames, bitmasks, play and idle scripts), the
  Battle Tower roster, Kris assets, Mobile System GB art, and the Crystal
  intro and title via src/import/CrystalMovie.lua.

Engine
- GameVersion gains engine(id) and fixes(id). Gold and Silver keep their
  original bugs where the bug is not hardware dependent; Crystal gets the fixes
  Crystal shipped: Lucky Number boxes 10-14, surfing onto an NPC, and the
  Reflect and Light Screen defence overflow.
- Crystal story: Suicune and Eusine, Celebi behind the GS Ball flag, the Ruins
  of Alph chambers, Buena, the Move Tutor, the Poke Seer, and the Battle Tower
  including the wInBattleTowerBattle badge-boost guard.
- Kris and the gender flag, animated fronts in battle and the summary screen,
  and mon caught data.

Verification
- Every extracted asset is pixel-compared against pret's own source PNGs.
- Gold caches are byte-identical before and after, file for file.
- New Crystal suites plus a T2 Gen 2 tier; the full suite passes.
2026-08-23 12:10:28 -04:00

295 lines
8.7 KiB
Lua

local SyncMods = {}
SyncMods.REV = 2
SyncMods.MAX_OPTION_KEYS = 64
SyncMods.MAX_OPTION_TEXT = 256
local function versions()
local ok, GameVersion = pcall(require, "src.core.GameVersion")
if ok and GameVersion and GameVersion.ORDER then return GameVersion.ORDER end
return { "red", "blue", "yellow", "gold", "silver", "crystal" }
end
local function defaultDeps()
return {
installed = function()
return require("src.mods.LauncherMods").list()
end,
indexes = function()
return require("src.mods.ModIndex").sources()
end,
addIndex = function(url)
return require("src.mods.ModIndex").addSource(url)
end,
findEntry = function(id)
local ModIndex = require("src.mods.ModIndex")
for _, source in ipairs(ModIndex.sources()) do
local cached = ModIndex.readCache(source.feed)
for _, entry in ipairs((cached and cached.mods) or {}) do
if entry.id == id then return entry end
end
end
return nil
end,
install = function(entry)
return require("src.mods.LauncherMods").installFromIndex(entry)
end,
setEnabled = function(id, enabled, version)
return require("src.mods.LauncherMods").setEnabled(id, enabled, version)
end,
modOptions = function()
return require("src.mods.LauncherMods").modOptions()
end,
setOptions = function(id, values)
return require("src.mods.LauncherMods").setModOptions(id, values)
end,
}
end
local function deps(given)
local out = defaultDeps()
if type(given) == "table" then
for k, v in pairs(given) do out[k] = v end
end
return out
end
local function sanitizeOptions(bucket)
if type(bucket) ~= "table" then return nil end
local keys = {}
for k, v in pairs(bucket) do
local t = type(v)
if type(k) == "string" and k ~= ""
and (t == "string" or t == "number" or t == "boolean") then
keys[#keys + 1] = k
end
end
if #keys == 0 then return nil end
table.sort(keys)
local out, n = {}, 0
for _, k in ipairs(keys) do
if n >= SyncMods.MAX_OPTION_KEYS then break end
local v = bucket[k]
if type(v) == "string" then v = v:sub(1, SyncMods.MAX_OPTION_TEXT) end
local finite = type(v) ~= "number"
or (v == v and v ~= math.huge and v ~= -math.huge)
if finite then
out[k] = v
n = n + 1
end
end
if n == 0 then return nil end
return out
end
local function sameOptions(a, b)
for k, v in pairs(a) do
if (b or {})[k] ~= v then return false end
end
return true
end
local function sourceOf(row)
local github = row.github
or (type(row.manifest) == "table" and row.manifest.github)
if type(github) == "string" and github ~= "" then
return "github:" .. github
end
return "local"
end
function SyncMods.build(given, includeOptions)
local d = deps(given)
local manifest = { rev = SyncMods.REV, indexes = {}, mods = {} }
local stored = {}
if includeOptions then
local ok, live = pcall(d.modOptions)
if ok and type(live) == "table" then stored = live end
end
for _, row in ipairs(d.indexes() or {}) do
local url = row.url or row.feed
if type(url) == "string" and url ~= "" then
manifest.indexes[#manifest.indexes + 1] = url
end
end
table.sort(manifest.indexes)
for _, row in ipairs(d.installed() or {}) do
if type(row) == "table" and type(row.id) == "string" then
local enabledFor = {}
local answers = row.enabledByVersion or {}
for _, version in ipairs(versions()) do
if answers[version] then enabledFor[#enabledFor + 1] = version end
end
local options = includeOptions and sanitizeOptions(stored[row.id]) or nil
if options then manifest.hasOptions = true end
manifest.mods[#manifest.mods + 1] = {
id = row.id,
version = row.version,
source = sourceOf(row),
enabledFor = enabledFor,
options = options,
}
end
end
table.sort(manifest.mods, function(a, b) return a.id < b.id end)
return manifest
end
function SyncMods.plan(manifest, given)
local d = deps(given)
local plan = { indexes = {}, toInstall = {}, toEnable = {}, missing = {},
options = {}, applyOptions = nil }
if type(manifest) ~= "table" then return plan end
local liveOptions = {}
do
local ok, live = pcall(d.modOptions)
if ok and type(live) == "table" then liveOptions = live end
end
local haveIndex = {}
for _, row in ipairs(d.indexes() or {}) do
if type(row.url) == "string" then haveIndex[row.url] = true end
if type(row.feed) == "string" then haveIndex[row.feed] = true end
end
for _, url in ipairs(manifest.indexes or {}) do
if type(url) == "string" and url ~= "" and not haveIndex[url] then
plan.indexes[#plan.indexes + 1] = url
haveIndex[url] = true
end
end
local installed = {}
for _, row in ipairs(d.installed() or {}) do
if type(row) == "table" and type(row.id) == "string" then
installed[row.id] = row
end
end
for _, mod in ipairs(manifest.mods or {}) do
if type(mod) == "table" and type(mod.id) == "string" then
local here = installed[mod.id]
local available = here ~= nil
if not here then
local entry = d.findEntry(mod.id)
if entry then
available = true
plan.toInstall[#plan.toInstall + 1] =
{ id = mod.id, version = mod.version, entry = entry }
else
plan.missing[#plan.missing + 1] =
{ id = mod.id, version = mod.version, source = mod.source }
end
end
if available then
local answers = (here and here.enabledByVersion) or {}
for _, version in ipairs(mod.enabledFor or {}) do
if answers[version] ~= true then
plan.toEnable[#plan.toEnable + 1] = { id = mod.id, version = version }
end
end
local wanted = sanitizeOptions(mod.options)
if wanted and not sameOptions(wanted, liveOptions[mod.id]) then
plan.options[#plan.options + 1] = { id = mod.id, values = wanted }
end
end
end
end
return plan
end
function SyncMods.planEmpty(plan)
if type(plan) ~= "table" then return true end
if plan.applyOptions and #(plan.options or {}) > 0 then return false end
return #(plan.indexes or {}) == 0 and #(plan.toInstall or {}) == 0
and #(plan.toEnable or {}) == 0
end
function SyncMods.planHasOptions(plan)
return type(plan) == "table" and #(plan.options or {}) > 0
end
function SyncMods.optionsAnswered(plan)
return not SyncMods.planHasOptions(plan) or plan.applyOptions ~= nil
end
function SyncMods.answerOptions(plan, importThem)
if type(plan) ~= "table" then return false end
plan.applyOptions = importThem and true or false
return plan.applyOptions
end
function SyncMods.optionModIds(plan)
local out = {}
for _, row in ipairs((type(plan) == "table" and plan.options) or {}) do
out[#out + 1] = row.id
end
return out
end
function SyncMods.steps(plan, given)
local d = deps(given)
local out = {}
if type(plan) ~= "table" then return out end
local broken = {}
for _, url in ipairs(plan.indexes or {}) do
out[#out + 1] = { label = url, run = function()
local ok, err = d.addIndex(url)
if not ok then return nil, tostring(err or url) end
return true
end }
end
for _, mod in ipairs(plan.toInstall or {}) do
out[#out + 1] = { label = mod.id, run = function()
local ok, err = d.install(mod.entry)
if not ok then
broken[mod.id] = true
return nil, mod.id .. ": " .. tostring(err or "install failed")
end
return true
end }
end
for _, want in ipairs(plan.toEnable or {}) do
out[#out + 1] = { label = want.id, run = function()
if broken[want.id] then return true end
local ok, err = d.setEnabled(want.id, true, want.version)
if ok == false then
return nil, want.id .. ": " .. tostring(err or "could not enable")
end
return true
end }
end
if plan.applyOptions then
for _, want in ipairs(plan.options or {}) do
out[#out + 1] = { label = want.id, run = function()
if broken[want.id] then return true end
local ok, err = d.setOptions(want.id, want.values)
if ok == false then
return nil, want.id .. ": "
.. tostring(err or "could not set the mod options")
end
return true
end }
end
end
return out
end
function SyncMods.apply(plan, progress, given)
if type(plan) ~= "table" then return false, "nothing to apply" end
local steps = SyncMods.steps(plan, given)
local failures = {}
for i, step in ipairs(steps) do
local ok, err = step.run()
if not ok then failures[#failures + 1] = err end
if progress then progress(i, #steps, step.label) end
end
if #failures > 0 then
return false, table.concat(failures, "; ")
end
return true
end
return SyncMods