Files
LonaRPG-Online-Games/Data/Scripts/Frames/126_MailSystem_TextsDecoder.rb
2026-10-29 21:59:06 +04:00

160 lines
4.3 KiB
Ruby

#==============================================================================
# This script is created by Kslander
#==============================================================================
#==============================================================================
# Text
#------------------------------------------------------------------------------
# Maps keys to text Strings. Reference at $game_text.
# Based on Text class written by DB
#==============================================================================
class MailText
attr_accessor :base_folder
attr_accessor :filenameList
attr_accessor :map
@@folder_cache = {}
#def initialize(base_folder="Text/CHT/mail")
# super(base_folder)
# @filenameList={}
# load_file_and_create_list
#end
def initialize(base_folder = "Text/CHT/mail/") #umm
@map = {}
@base_folder = base_folder
@filenameList = {}
load_file_and_create_list(target = @base_folder)
end
def load_file(target)#umm merge
begin
tgt = Text.resolve_existing_text_path(target + ".txt")
return parse(Text.read_text_file(tgt))
rescue => ex
msgbox "ERROR: missing translation file #{target}.txt"
return Hash.new
end
end
########### GPT ver1
def parse(string)
blocks = {}
lines = string.split("\n")
current_section = nil
lines.each do |line|
next if line.strip.empty? || line.start_with?('#')
if line.include?("/")
section, key = line.split("/", 2)
blocks[section] ||= {}
current_section = section
#blocks[section][key] = line #@map={"Text/CHT/mail/DarkPot.txt"=>{"Tutorial_MainControl"=> {"Sender"=>"Tutorial_MainControl/Sender","Title"=>"Tutorial_MainControl/Title","Text"=>"DarkPotBasic1/Text"},
blocks[section] = true
end
end
blocks
end
# Simulating file scanning and data transformation
def load_file_and_create_list(target)
target = target.to_s.tr("\\", "/")
restore_cached_folder(target)
return unless @map.empty? && @filenameList.empty?
@map = {}
@filenameList = {}
seen = {}
folders = Text.language_fallback_paths(target)
folders.each do |folder|
next unless Dir.exist?(folder)
Dir[File.join(folder, "*.txt")].sort.each do |file|
next unless File.file?(file)
basename = File.basename(file).downcase
next if seen[basename]
content = Text.read_text_file(file)
parsed_data = parse(content)
@map[file] = parsed_data
create_key_list(file)
seen[basename] = true
end
end
store_cached_folder(target)
end
#for mod api load single file.
# Load a single file dynamically and update @map and @filenameList
def load_new_file(file_path)
file_path = Text.resolve_existing_text_path(file_path)
return unless File.file?(file_path)
begin
content = Text.read_text_file(file_path)
parsed_data = parse(content)
@map[file_path] = parsed_data
# Update filenameList
parsed_data.keys.each do |key|
@filenameList[key.split("/")[0]] = file_path
end
rescue => ex
p "ERROR loading file #{file_path}: #{ex.message}"
end
end
def decode_text(content, header) # $mail_text.get_text("Text/CHT/mail/DarkPot.txt","DarkPotBasic1/Sender")
#def get_text(file, header) # $mail_text.get_text("Text/CHT/mail/DarkPot.txt","DarkPotBasic1/Sender")
#content = File.read(file)
return "" if content.nil? || header.nil?
header = header.to_s.strip
return "" if header.empty?
lines = content.to_s.gsub("\r\n", "\n").split("\n")
start_index = lines.index(header)
return "" if start_index.nil?
value = []
i = start_index + 1
while i < lines.length
line = lines[i]
break if line =~ /^[A-Za-z0-9_]+\/[A-Za-z0-9_]+$/
value << line
i += 1
end
value.join("\n").strip
end
def create_key_list(file) #umm merge
@map[file].keys.each {|key|
@filenameList[key.split("/")[0]] = file
}
end
def restore_cached_folder(target)
cache = @@folder_cache[target]
return if cache.nil?
@map = deep_copy_hash(cache[:map])
@filenameList = deep_copy_hash(cache[:filename_list])
end
def store_cached_folder(target)
@@folder_cache[target] = {
:map => deep_copy_hash(@map),
:filename_list => deep_copy_hash(@filenameList)
}
end
def deep_copy_hash(hash)
Marshal.load(Marshal.dump(hash))
rescue
copied = {}
hash.each do |key, value|
copied[key] = value.is_a?(Hash) ? deep_copy_hash(value) : value
end
copied
end
end