diff --git a/Data/Scripts/001_Technical/002_RubyUtilities.rb b/Data/Scripts/001_Technical/002_RubyUtilities.rb index c61f97377..311f9cb94 100644 --- a/Data/Scripts/001_Technical/002_RubyUtilities.rb +++ b/Data/Scripts/001_Technical/002_RubyUtilities.rb @@ -141,6 +141,36 @@ module Enumerable end end +#=============================================================================== +# class File +#=============================================================================== +class File + # Copies the source file to the destination path. + def self.copy(source, destination) + data = "" + t = Time.now + File.open(source, 'rb') do |f| + while r = f.read(4096) + if Time.now - t > 1 + Graphics.update + t = Time.now + end + data += r + end + end + File.delete(destination) if File.file?(destination) + f = File.new(destination, 'wb') + f.write data + f.close + end + + # Copies the source to the destination and deletes the source. + def self.move(source, destination) + File.copy(source, destination) + File.delete(source) + end +end + #=============================================================================== # Kernel methods #=============================================================================== diff --git a/Data/Scripts/012_Overworld/006_Overworld_BerryPlants.rb b/Data/Scripts/012_Overworld/006_Overworld_BerryPlants.rb index 07c3701c2..ead5445f7 100644 --- a/Data/Scripts/012_Overworld/006_Overworld_BerryPlants.rb +++ b/Data/Scripts/012_Overworld/006_Overworld_BerryPlants.rb @@ -1,4 +1,4 @@ -$player#=============================================================================== +#=============================================================================== # Represents a planted berry. Stored in $PokemonGlobal.eventvars. #=============================================================================== class BerryPlantData @@ -255,7 +255,7 @@ class BerryPlantSprite @event.character_name = "berrytreeplanted" # Common to all berries @event.turn_down else - filename = sprintf("berrytree%s", GameData::Item.get(berry_plant.berry_id).id.to_s) + filename = sprintf("berrytree_%s", GameData::Item.get(berry_plant.berry_id).id.to_s) if pbResolveBitmap("Graphics/Characters/" + filename) @event.character_name = filename case berry_plant.growth_stage diff --git a/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb b/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb index 5fa0525ed..fadc35012 100644 --- a/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb +++ b/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb @@ -1191,6 +1191,19 @@ DebugMenuCommands.register("invalidtiles", { } }) +DebugMenuCommands.register("renamefiles", { + "parent" => "othermenu", + "name" => _INTL("Rename Outdated Files"), + "description" => _INTL("Check for files with outdated names and rename/move them. Can alter map data."), + "always_show" => true, + "effect" => proc { + if pbConfirmMessage(_INTL("Are you sure you want to automatically rename outdated files?")) + FilenameUpdater.rename_files + pbMessage(_INTL("Done.")) + end + } +}) + DebugMenuCommands.register("reloadsystemcache", { "parent" => "othermenu", "name" => _INTL("Reload System Cache"), diff --git a/Data/Scripts/020_Debug/003_Debug menus/006_Debug_FilenameUpdater.rb b/Data/Scripts/020_Debug/003_Debug menus/006_Debug_FilenameUpdater.rb new file mode 100644 index 000000000..92907f94f --- /dev/null +++ b/Data/Scripts/020_Debug/003_Debug menus/006_Debug_FilenameUpdater.rb @@ -0,0 +1,91 @@ +#=============================================================================== +# +#=============================================================================== +module FilenameUpdater + module_function + + def readDirectoryFiles(directory, formats) + files = [] + Dir.chdir(directory) { + for i in 0...formats.length + Dir.glob(formats[i]) { |f| files.push(f) } + end + } + return files + end + + def rename_berry_plant_charsets + src_dir = "Graphics/Characters/" + return false if !FileTest.directory?(src_dir) + Console.echo_li _INTL("Renaming berry tree charsets...") + ret = false + # generates a list of all graphic files + files = readDirectoryFiles(src_dir, ["berrytree*.png"]) + # starts automatic renaming + files.each_with_index do |file, i| + next if file[/^berrytree_/] + next if ["berrytreewet", "berrytreedamp", "berrytreedry", "berrytreeplanted"].include?(file.split('.')[0]) + new_file = file.gsub('berrytree', 'berrytree_') + File.move(src_dir + file, src_dir + new_file) + ret = true + end + Console.echo_done(true) + return ret + end + + def update_berry_tree_event_charsets + ret = [] + mapData = Compiler::MapData.new + t = Time.now.to_i + Graphics.update + Console.echo_li _INTL("Checking {1} maps for used berry tree charsets...", mapData.mapinfos.keys.length) + idx = 0 + for id in mapData.mapinfos.keys.sort + echo "." if idx % 20 == 0 + idx += 1 + Graphics.update if idx % 250 == 0 + map = mapData.getMap(id) + next if !map || !mapData.mapinfos[id] + changed = false + for key in map.events.keys + if Time.now.to_i - t >= 5 + Graphics.update + t = Time.now.to_i + end + map.events[key].pages.each do |page| + next if nil_or_empty?(page.graphic.character_name) + char_name = page.graphic.character_name + next if !char_name[/^berrytree[^_]+/] + next if ["berrytreewet", "berrytreedamp", "berrytreedry", "berrytreeplanted"].include?(char_name.split('.')[0]) + new_file = page.graphic.character_name.gsub('berrytree', 'berrytree_') + page.graphic.character_name = new_file + changed = true + end + end + next if !changed + mapData.saveMap(id) + any_changed = true + ret.push(_INTL("Map {1}: '{2}' was modified and saved.", id, mapData.mapinfos[id].name)) + end + Console.echo_done(true) + return ret + end + + def rename_files + Console.echo_h1 "Updating file names and locations" + change_record = [] + changed = false + # Add underscore to berry plant charsets + if rename_berry_plant_charsets + Console.echo_warn _INTL("Berry plant charset files were renamed.") + end + change_record += update_berry_tree_event_charsets + # Warn if any map data has been changed + if !change_record.empty? + change_record.each { |msg| Console.echo_warn msg } + Console.echo_warn _INTL("RMXP data was altered. Close RMXP now to ensure changes are applied.") + end + echoln "" + Console.echo_h2("Finished updating file names and locations", text: :green) + end +end