#============================================================================== # LonaCraft - 70_Save.rb # What it does: # * Tier variants get their ids from the load order (id = table length), and # saves record weapons/armour by id: party counts {id => n} # (29_Game_Party.rb:175-183, 351-358) and equip slots Game_BaseItem # @class + @item_id (20_Game_BaseItem.rb:32-53). So every save carries # contents[:lonacraft] = {"registry" => {item_name => id}, # "kinds" => {item_name => "W"/"A"/"I"}, "version" => 3} (core types # only, so the save still loads without the mod). "W"/"A" are the tier # variants and (version 3) the mod's own weapons (LonaCraft::WEAPON_NAMES, # 0_Core.rb: the Hatchet); "I" (version 2) are the plain items: the # materials (LonaCraft::ITEM_NAMES, 0_Core.rb) and the furniture # (Furniture::ITEM_NAMES, 90_Furniture.rb), whose ids also follow the load # order and sit in the party's item counts {id => n} (29_Game_Party.rb:44, # 170, 357). # * The Hatchet was an RPG::Item until 2026-10-07 and is a weapon now. A # version 2 save lists it as "I": its count in the party's items moves # to the party's weapons under this session's weapon id (console line), # and a Hatchet item in a storage box becomes the weapon there (by # item_name, so any save version). A version 1 save has no ids for it: # a Hatchet in the bag is left as it is (as the other plain items). # * On load, before the game's extract_save_contents runs, every saved id # that differs from this session's id is rewritten in the party's weapon, # armour and item counts, in every equip slot of Lona ($game_actors), the # NPC portrait channels and the source NPC actors (Cocona etc.), and in # the party's menu cursor memo (@last_item, 29_Game_Party.rb:34). An item # that no longer exists is dropped (console note). Storage boxes keep # whole item copies as keys (203_Storage Boxes.rb:104-125), which carry # their item_name; a variant, material or furniture key gets this # session's id, or is dropped if the item is gone (works for any save). # A version 1 save has no "I" entries: plain items in the party's counts # are left as they are (one console note). # Not remapped, as nothing reads them by a stale id: $game_boxes # @defaultPlayerItems (a copy of the party counts that every trade # rebuilds first, 203_Storage Boxes.rb:184-207, read only at :1187) and # @storeItemPrice (item objects as keys, matched by identity); ground # drops, traps and shop stock record item_name strings # (10_Game_Temp.rb:74-81, 480_EventCharacterBatch.rb:1088, # 29_Functions_417_Interpreter.rb:96-127); placed furniture records # item_name and box ids from 66000 (90_Furniture.rb), never an item id. # Remapping runs BEFORE the original extract so the portrait prebuild at # the end of it (5_DataManager.rb:978) already sees the right items. # * Hooks: DataManager.make_save_contents and extract_save_contents only. # All writers call make_save_contents: manual save_game_without_rescue # (5_DataManager.rb:790, also used directly by CustomDifficulty), doom # save_doom_without_header (:801) and auto/quick save_custom_without_header # (:812). All readers call extract_save_contents (:827, :838, :849). # * The game deletes the save slot if a save raises (5_DataManager.rb:709-712, # 721-724), so the mod's part of each hook is wrapped and never raises. # * The crafting skill is in $game_system (60_Skill.rb) and needs no hook. #============================================================================== module LonaCraft module Save KEY = :lonacraft VERSION = 3 # 2: plain items (materials, furniture) with kind "I"; 3: the mod's weapons with kind "W" # The mod's plain RPG::Item names: materials, then furniture. def self.plain_item_names names = LonaCraft::ITEM_NAMES.dup names.concat(LonaCraft::Furniture::ITEM_NAMES) if defined?(LonaCraft::Furniture::ITEM_NAMES) names end # {item_name => true} of the plain items registered this session, or # nil when none is (the load failed: then nothing is dropped). def self.plain_items_now set = {} plain_item_names.each{|name| set[name] = true if $data_ItemName[name].is_a?(RPG::Item) } set.empty? ? nil : set end # The mod's own weapon (LonaCraft::WEAPON_NAMES) called +name+ if it is # registered as a weapon this session, else nil def self.mod_weapon_now(name) return nil unless defined?(LonaCraft::WEAPON_NAMES) && LonaCraft::WEAPON_NAMES.include?(name.to_s) obj = $data_ItemName[name.to_s] obj.is_a?(RPG::Weapon) ? obj : nil end def self.payload registry = {} kinds = {} LonaCraft::Tiers::BASE_OF.each_key{|name| obj = $data_ItemName[name] next if obj.nil? registry[name] = obj.id kinds[name] = obj.is_a?(RPG::Weapon) ? "W" : "A" } plain_item_names.each{|name| obj = $data_ItemName[name] next unless obj.is_a?(RPG::Item) registry[name] = obj.id kinds[name] = "I" } LonaCraft::WEAPON_NAMES.each{|name| obj = mod_weapon_now(name) next if obj.nil? registry[name] = obj.id kinds[name] = "W" } if defined?(LonaCraft::WEAPON_NAMES) {"registry" => registry, "kinds" => kinds, "version" => VERSION} end def self.remap(contents) return unless contents.is_a?(Hash) variants_ok = !LonaCraft::Tiers::BASE_OF.empty? p "LonaCraft Save: no tier variants this session, crafted items in the save left as they are" unless variants_ok plain_now = plain_items_now p "LonaCraft Save: no materials or furniture this session, those in the save left as they are" if plain_now.nil? remap_boxes(contents[:imp_boxes], variants_ok, plain_now) saved = contents[KEY] return unless saved.is_a?(Hash) registry = saved["registry"] return unless registry.is_a?(Hash) && !registry.empty? kinds = saved["kinds"].is_a?(Hash) ? saved["kinds"] : {} if saved["version"].to_i < 2 p "LonaCraft Save: save version #{saved["version"].to_i} has no material or furniture ids, those in the bag are left as they are" end wmap = {} amap = {} imap = {} conv = {} # old item id => [weapon id now, name]: items that are weapons now (the Hatchet) registry.each{|name, old_id| next unless old_id.is_a?(Integer) own = mod_weapon_now(name) if kinds[name] == "I" if own conv[old_id] = [own.id, name] next end next if plain_now.nil? cur = plain_now.has_key?(name) ? $data_ItemName[name] : nil new_id = cur ? cur.id : nil imap[old_id] = [new_id, name] if new_id != old_id next end next unless variants_ok || own cur = $data_ItemName[name] cur = nil unless LonaCraft::Tiers::BASE_OF.has_key?(name) || own kind = kinds[name] kind = (cur.is_a?(RPG::Weapon) ? "W" : "A") if kind.nil? && cur next if kind.nil? new_id = cur ? cur.id : nil next if new_id == old_id (kind == "W" ? wmap : amap)[old_id] = [new_id, name] } return if wmap.empty? && amap.empty? && imap.empty? && conv.empty? p "LonaCraft Save: remapping #{wmap.size + amap.size} crafted item and weapon ids" unless wmap.empty? && amap.empty? p "LonaCraft Save: remapping #{imap.size} material and furniture item ids" unless imap.empty? # taken out before the remap (another item may get the old id), added # after it (the weapon id is already this session's) moved = take_converted(contents[:party], conv) remap_party(contents[:party], wmap, amap, imap) give_converted(contents[:party], moved) actors_in(contents).each{|actor| remap_equips(actor, wmap, amap, imap) } gone = imap.dup conv.each{|old_id, data| gone[old_id] = [nil, data[1]] } remap_last_item(contents[:party], gone) end # Removes the counts of the items in +conv+ from the party's items; # returns {weapon id now => [count, name]}. def self.take_converted(party, conv) out = {} return out if party.nil? || conv.empty? items = party.instance_variable_get(:@items) return out unless items.is_a?(Hash) conv.each{|old_id, data| new_id, name = data count = items.delete(old_id).to_i next if count < 1 prev = out[new_id] ? out[new_id][0] : 0 out[new_id] = [prev + count, name] } out end # Adds take_converted's counts to the party's weapons. def self.give_converted(party, moved) return if party.nil? || moved.empty? weapons = party.instance_variable_get(:@weapons) weapons = {} unless weapons.is_a?(Hash) moved.each{|id, data| count, name = data weapons[id] = weapons[id].to_i + count p "LonaCraft Save: #{count} x #{name} moved from the items to the weapons (a main-hand weapon since 2026-10-07)" } party.instance_variable_set(:@weapons, weapons) end def self.remap_party(party, wmap, amap, imap = {}) return if party.nil? [[:@weapons, wmap, "variant"], [:@armors, amap, "variant"], [:@items, imap, "item"]].each{|ivar, map, what| next if map.empty? counts = party.instance_variable_get(ivar) next unless counts.is_a?(Hash) if ivar == :@items # an id kept as it is (another mod's item) that is now one of ours clash = map.values.count{|new_id, name| new_id && counts.has_key?(new_id) && !map.has_key?(new_id) } p "LonaCraft Save: #{clash} other item ids in the bag are LonaCraft ids this session (shown as those items, counts added together)" if clash > 0 end fresh = {} counts.each{|id, count| if map.has_key?(id) new_id, name = map[id] if new_id.nil? p "LonaCraft Save: dropped #{count} x #{name} (#{what} no longer exists)" next end id = new_id end fresh[id] = fresh[id].to_i + count.to_i } party.instance_variable_set(ivar, fresh) } end # The item menu's cursor memo (a Game_BaseItem); cleared if the item is gone. def self.remap_last_item(party, imap) return if party.nil? || imap.empty? slot = party.instance_variable_get(:@last_item) return unless slot.is_a?(Game_BaseItem) && slot.instance_variable_get(:@class) == RPG::Item id = slot.instance_variable_get(:@item_id) return unless imap.has_key?(id) new_id = imap[id][0] slot.instance_variable_set(:@class, nil) if new_id.nil? slot.instance_variable_set(:@item_id, new_id || 0) end # Lona and every other actor object in the save that has equip slots def self.actors_in(contents) list = [] group = contents[:actors] if group data = group.instance_variable_get(:@data) list.concat(data.compact) if data.is_a?(Array) end [:game_NPCLayerMain, :game_NPCLayerSub].each{|key| list << contents[key] if contents[key] } source = contents[:game_NPC_actors] if source data = source.instance_variable_get(:@data) list.concat(data.values.compact) if data.is_a?(Hash) end list.uniq end def self.remap_equips(actor, wmap, amap, imap = {}) equips = actor.instance_variable_get(:@equips) return unless equips.is_a?(Array) equips.each{|slot| next unless slot.is_a?(Game_BaseItem) klass = slot.instance_variable_get(:@class) map = nil map = wmap if klass == RPG::Weapon map = amap if klass == RPG::Armor map = imap if klass == RPG::Item # an item in an equip slot (25_Game_Actor.rb:1402) next if map.nil? id = slot.instance_variable_get(:@item_id) next unless map.has_key?(id) new_id, name = map[id] if new_id.nil? p "LonaCraft Save: unequipped #{name} (#{klass == RPG::Item ? "item" : "variant"} no longer exists)" slot.instance_variable_set(:@class, nil) slot.instance_variable_set(:@item_id, 0) else slot.instance_variable_set(:@item_id, new_id) end } end # Box keys are item copies that carry their item_name, so no registry is # needed. +plain+ = plain_items_now (nil: leave materials/furniture alone). def self.remap_boxes(boxes, variants_ok = true, plain = nil) return if boxes.nil? list = boxes.instance_variable_get(:@boxes) return unless list.is_a?(Array) moved = 0 names = plain ? plain_item_names : [] list.each{|box| next unless box.is_a?(Hash) box.keys.each{|key| if key.is_a?(RPG::Item) name = key.item_name.to_s own = mod_weapon_now(name) if own # an item that is a weapon now (the Hatchet): the weapon takes its place count = box.delete(key).to_i next if count < 1 same = box.keys.find{|k| k.is_a?(RPG::Weapon) && k.item_name.to_s == name } same.id = own.id if same && same.id != own.id target = same || own box[target] = box[target].to_i + count p "LonaCraft Save: #{count} x #{name} in a storage box moved from the items to the weapons" next end next if plain.nil? next unless names.include?(name) cur = plain.has_key?(name) ? $data_ItemName[name] : nil if cur.nil? p "LonaCraft Save: dropped #{box[key]} x #{name} from a storage box (item no longer exists)" box.delete(key) elsif key.id != cur.id key.id = cur.id moved += 1 end next end next unless key.is_a?(RPG::Weapon) || key.is_a?(RPG::Armor) name = key.item_name.to_s own = key.is_a?(RPG::Weapon) ? mod_weapon_now(name) : nil if own # the mod's own weapon: this session's id key.id = own.id if key.id != own.id next end next unless variants_ok next unless name =~ LonaCraft::Tiers::VARIANT_RE cur = $data_ItemName[name] if cur.nil? || !LonaCraft::Tiers::BASE_OF.has_key?(name) p "LonaCraft Save: dropped #{box[key]} x #{name} from a storage box (variant no longer exists)" box.delete(key) elsif key.id != cur.id key.id = cur.id end } } p "LonaCraft Save: #{moved} material and furniture stacks in storage boxes got this session's ids" if moved > 0 end end end module DataManager unless respond_to?(:make_save_contents_lonacraft) self.singleton_class.send(:alias_method, :make_save_contents_lonacraft, :make_save_contents) def self.make_save_contents contents = make_save_contents_lonacraft begin contents[LonaCraft::Save::KEY] = LonaCraft::Save.payload if contents.is_a?(Hash) rescue => e p "LonaCraft save hook error #{e.class} #{e.message}" end contents end end unless respond_to?(:extract_save_contents_lonacraft) self.singleton_class.send(:alias_method, :extract_save_contents_lonacraft, :extract_save_contents) def self.extract_save_contents(contents) begin LonaCraft::Save.remap(contents) rescue => e p "LonaCraft load hook error #{e.class} #{e.message}" end extract_save_contents_lonacraft(contents) end end end