Made Compiler more modular

This commit is contained in:
Maruno17
2024-05-14 22:11:12 +01:00
parent 5bef70fb3a
commit 01c13ada76
6 changed files with 387 additions and 335 deletions

View File

@@ -54,6 +54,8 @@ end
# Compiler
#===============================================================================
module Compiler
@@categories = {}
module_function
def findIndex(a)
@@ -89,9 +91,9 @@ module Compiler
return csvQuote(str, true)
end
#=============================================================================
#-----------------------------------------------------------------------------
# PBS file readers
#=============================================================================
#-----------------------------------------------------------------------------
def pbEachFileSectionEx(f, schema = nil)
lineno = 1
havesection = false
@@ -248,10 +250,10 @@ module Compiler
end
end
#=============================================================================
#-----------------------------------------------------------------------------
# Splits a string containing comma-separated values into an array of those
# values.
#=============================================================================
#-----------------------------------------------------------------------------
def split_csv_line(string)
# Split the string into an array of values, using a comma as the separator
values = string.split(",")
@@ -292,9 +294,9 @@ module Compiler
return values
end
#=============================================================================
#-----------------------------------------------------------------------------
# Convert a string to certain kinds of values
#=============================================================================
#-----------------------------------------------------------------------------
# Unused
# NOTE: This method is about 10 times slower than split_csv_line.
def csvfield!(str)
@@ -508,9 +510,9 @@ module Compiler
return nil
end
#=============================================================================
#-----------------------------------------------------------------------------
# Convert a string to values using a schema
#=============================================================================
#-----------------------------------------------------------------------------
# Unused
# @deprecated This method is slated to be removed in v22.
def pbGetCsvRecord(rec, lineno, schema)
@@ -684,9 +686,9 @@ module Compiler
return (!repeat && schema_length == 1) ? record[0] : record
end
#=============================================================================
#-----------------------------------------------------------------------------
# Convert a string to values using a schema
#=============================================================================
#-----------------------------------------------------------------------------
def get_csv_record(rec, schema)
ret = []
repeat = false
@@ -736,9 +738,9 @@ module Compiler
return (!repeat && schema_length == 1) ? ret[0] : ret
end
#=============================================================================
#-----------------------------------------------------------------------------
# Write values to a file using a schema
#=============================================================================
#-----------------------------------------------------------------------------
def pbWriteCsvRecord(record, file, schema)
rec = (record.is_a?(Array)) ? record.flatten : [record]
start = (["*", "^"].include?(schema[1][0, 1])) ? 1 : 0
@@ -841,10 +843,10 @@ module Compiler
return record
end
#=============================================================================
#-----------------------------------------------------------------------------
# Parse string into a likely constant name and return its ID number (if any).
# Last ditch attempt to figure out whether a constant is defined.
#=============================================================================
#-----------------------------------------------------------------------------
# Unused
def pbGetConst(mod, item, err)
isDef = false
@@ -918,9 +920,9 @@ module Compiler
return typ.id
end
#=============================================================================
#-----------------------------------------------------------------------------
# Replace text in PBS files before compiling them
#=============================================================================
#-----------------------------------------------------------------------------
def edit_and_rewrite_pbs_file_text(filename)
return if !block_given?
lines = []
@@ -946,160 +948,40 @@ module Compiler
end
end
#=============================================================================
#-----------------------------------------------------------------------------
# Compile all data
#=============================================================================
def compile_pbs_file_message_start(filename)
# The `` around the file's name turns it cyan
Console.echo_li(_INTL("Compiling PBS file `{1}`...", filename.split("/").last))
end
def write_pbs_file_message_start(filename)
# The `` around the file's name turns it cyan
Console.echo_li(_INTL("Writing PBS file `{1}`...", filename.split("/").last))
end
def process_pbs_file_message_end
Console.echo_done(true)
Graphics.update
end
def get_all_pbs_files_to_compile
# Get the GameData classes and their respective base PBS filenames
ret = GameData.get_all_pbs_base_filenames
ret.merge!({
:BattleFacility => "battle_facility_lists",
:Connection => "map_connections",
:RegionalDex => "regional_dexes"
})
ret.each { |key, val| ret[key] = [val] } # [base_filename, ["PBS/file.txt", etc.]]
# Look through all PBS files and match them to a GameData class based on
# their base filenames
text_files_keys = ret.keys.sort! { |a, b| ret[b][0].length <=> ret[a][0].length }
Dir.chdir("PBS/") do
Dir.glob("*.txt") do |f|
base_name = File.basename(f, ".txt")
text_files_keys.each do |key|
next if base_name != ret[key][0] && !f.start_with?(ret[key][0] + "_")
ret[key][1] ||= []
ret[key][1].push("PBS/" + f)
break
end
end
#-----------------------------------------------------------------------------
def categories_to_compile(all_categories = false)
ret = []
Input.update
if all_categories || $full_compile || Input.press?(Input::CTRL)
ret = @@categories.keys.clone
return ret
end
@@categories.each_pair do |category, procs|
ret.push(category) if procs[:should_compile]&.call
end
return ret
end
def compile_pbs_files
text_files = get_all_pbs_files_to_compile
modify_pbs_file_contents_before_compiling
compile_town_map(*text_files[:TownMap][1])
compile_connections(*text_files[:Connection][1])
compile_types(*text_files[:Type][1])
compile_abilities(*text_files[:Ability][1])
compile_moves(*text_files[:Move][1]) # Depends on Type
compile_items(*text_files[:Item][1]) # Depends on Move
compile_berry_plants(*text_files[:BerryPlant][1]) # Depends on Item
compile_pokemon(*text_files[:Species][1]) # Depends on Move, Item, Type, Ability
compile_pokemon_forms(*text_files[:Species1][1]) # Depends on Species, Move, Item, Type, Ability
compile_pokemon_metrics(*text_files[:SpeciesMetrics][1]) # Depends on Species
compile_shadow_pokemon(*text_files[:ShadowPokemon][1]) # Depends on Species
compile_regional_dexes(*text_files[:RegionalDex][1]) # Depends on Species
compile_ribbons(*text_files[:Ribbon][1])
compile_encounters(*text_files[:Encounter][1]) # Depends on Species
compile_trainer_types(*text_files[:TrainerType][1])
compile_trainers(*text_files[:Trainer][1]) # Depends on Species, Item, Move
compile_trainer_lists # Depends on TrainerType
compile_metadata(*text_files[:Metadata][1]) # Depends on TrainerType
compile_map_metadata(*text_files[:MapMetadata][1])
compile_dungeon_tilesets(*text_files[:DungeonTileset][1])
compile_dungeon_parameters(*text_files[:DungeonParameters][1])
compile_phone(*text_files[:PhoneMessage][1]) # Depends on TrainerType
end
def compile_all(mustCompile)
Console.echo_h1(_INTL("Checking game data"))
if !mustCompile
Console.echoln_li(_INTL("Game data was not compiled"))
echoln ""
return
end
def compile_all(all_categories = false)
FileLineData.clear
compile_pbs_files
compile_animations
compile_trainer_events(mustCompile)
Console.echo_li(_INTL("Saving messages..."))
Translator.gather_script_and_event_texts
MessageTypes.save_default_messages
MessageTypes.load_default_messages if FileTest.exist?("Data/messages_core.dat")
Console.echo_done(true)
Console.echoln_li_done(_INTL("Successfully compiled all game data"))
to_compile = categories_to_compile(all_categories)
@@categories.each_pair do |category, procs|
Console.echo_h1(procs[:header_text]&.call || _INTL("Compiling {1}", category))
if to_compile.include?(category)
@@categories[category][:compile].call
else
Console.echoln_li(procs[:skipped_text]&.call || _INTL("Not compiled"))
end
echoln ""
end
end
def main
return if !$DEBUG
begin
mustCompile = false
# If no PBS file, create one and fill it, then recompile
if !FileTest.directory?("PBS")
Dir.mkdir("PBS") rescue nil
GameData.load_all
write_all
mustCompile = true
end
# Get all data files and PBS files to be checked for their last modified times
data_files = GameData.get_all_data_filenames
data_files += [ # Extra .dat files for data that isn't a GameData class
["map_connections.dat", true],
["regional_dexes.dat", true],
["trainer_lists.dat", true]
]
text_files = get_all_pbs_files_to_compile
latestDataTime = 0
latestTextTime = 0
# Should recompile if new maps were imported
mustCompile |= import_new_maps
# Check data files for their latest modify time
data_files.each do |filename| # filename = [string, boolean (whether mandatory)]
if FileTest.exist?("Data/" + filename[0])
begin
File.open("Data/#{filename[0]}") do |file|
latestDataTime = [latestDataTime, file.mtime.to_i].max
end
rescue SystemCallError
mustCompile = true
end
elsif filename[1]
mustCompile = true
break
end
end
# Check PBS files for their latest modify time
text_files.each do |key, value|
next if !value || !value[1].is_a?(Array)
value[1].each do |filepath|
begin
File.open(filepath) { |file| latestTextTime = [latestTextTime, file.mtime.to_i].max }
rescue SystemCallError
end
end
end
# Decide to compile if a PBS file was edited more recently than any .dat files
mustCompile |= (latestTextTime >= latestDataTime)
# Should recompile if holding Ctrl
Input.update
mustCompile = true if $full_compile || Input.press?(Input::CTRL)
# Delete old data files in preparation for recompiling
if mustCompile
data_files.each do |filename|
begin
File.delete("Data/#{filename[0]}") if FileTest.exist?("Data/#{filename[0]}")
rescue SystemCallError
end
end
end
# Recompile all data
compile_all(mustCompile)
compile_all
rescue Exception
e = $!
raise e if e.class.to_s == "Reset" || e.is_a?(Reset) || e.is_a?(SystemExit)