Files
LonaRPG-Online-Games/Data/Scripts/0_UserData_Setup.rb
T
2026-10-29 21:59:06 +04:00

200 lines
4.8 KiB
Ruby

unless defined?(Ruby310Compat)
begin
compat_candidates = []
begin
compat_candidates << File.expand_path('000_Ruby310_Compat.rb', File.dirname(__FILE__))
rescue StandardError
end
begin
cwd = Dir.pwd.to_s
compat_candidates << File.expand_path('Data/Scripts/000_Ruby310_Compat.rb', cwd) unless cwd.empty?
compat_candidates << File.expand_path('000_Ruby310_Compat.rb', cwd) unless cwd.empty?
rescue StandardError
end
compat_candidates.compact.uniq.each do |compat_path|
next unless File.file?(compat_path)
load compat_path
break if defined?(Ruby310Compat)
end
rescue StandardError
end
end
# NoerHome_UserData_Fix.rb
#
# Положить сюда:
# game.exe\Data\Scripts\NoerHome_UserData_Fix.rb
#
# Что делает:
# 1) game.exe\UserData\GameLona.ini
# [LonaRPG]
# Language = RUS2
#
# Если нужные значения уже стоят:
# - ничего не переписывает
# - окно не показывает
# - игру не закрывает
#
# Если были изменения:
# - записывает файлы
# - показывает окно "Запустите игру заново"
# - после OK закрывает игру
def read_text(path)
if defined?(Ruby310Compat)
Ruby310Compat.read_text_file(path)
else
File.open(path, 'rb') { |f| f.read }
end
end
def write_text(path, text)
File.open(path, 'wb') { |f| f.write(text) }
end
def detect_eol(text)
text.include?("\r\n") ? "\r\n" : "\n"
end
def normalize_lines(text)
text.gsub("\r\n", "\n").split("\n", -1)
end
def ensure_trailing_eol(text, eol)
return '' if text.nil? || text.empty?
text.end_with?(eol) ? text : (text + eol)
end
def find_section_range(lines, section_name)
section_start = nil
section_end = lines.length
i = 0
while i < lines.length
stripped = lines[i].strip
if stripped == "[#{section_name}]"
section_start = i
j = i + 1
while j < lines.length
s = lines[j].strip
if s.start_with?('[') && s.end_with?(']')
section_end = j
break
end
j += 1
end
break
end
i += 1
end
[section_start, section_end]
end
def update_section_keys_if_needed(text, section_name, pairs, create_section_if_missing)
eol = detect_eol(text)
lines = normalize_lines(text)
section_start, section_end = find_section_range(lines, section_name)
if section_start.nil?
if !create_section_if_missing
raise "Секция [#{section_name}] не найдена"
end
while !lines.empty? && lines[-1] == ''
lines.pop
end
lines << '' unless lines.empty?
lines << "[#{section_name}]"
pairs.each do |k, v|
lines << "#{k} = #{v}"
end
result = ensure_trailing_eol(lines.join(eol), eol)
return [result, true]
end
existing = {}
i = section_start + 1
while i < section_end
stripped = lines[i].strip
if !stripped.empty? && !stripped.start_with?(';') && !stripped.start_with?('#')
if stripped =~ /^([^=]+?)\s*=\s*(.*)$/
key = Regexp.last_match(1).strip
value = Regexp.last_match(2).strip
existing[key] = { :index => i, :value => value }
end
end
i += 1
end
changed = false
pairs.each do |k, v|
if existing.key?(k)
if existing[k][:value] != v
lines[existing[k][:index]] = "#{k} = #{v}"
changed = true
end
else
lines.insert(section_end, "#{k} = #{v}")
section_end += 1
changed = true
end
end
return [text, false] unless changed
result = ensure_trailing_eol(lines.join(eol), eol)
[result, true]
end
def show_restart_and_exit
begin
msgbox('Запустите игру заново')
rescue
p 'Запустите игру заново'
end
exit
end
script_dir = File.dirname(File.expand_path(__FILE__))
candidate_root_1 = Dir.pwd
candidate_root_2 = File.expand_path('../..', script_dir)
if File.directory?(File.join(candidate_root_1, 'UserData'))
game_root = candidate_root_1
elsif File.directory?(File.join(candidate_root_2, 'UserData'))
game_root = candidate_root_2
else
raise 'Не найдена папка UserData'
end
user_data_dir = File.join(game_root, 'UserData')
game_lona_path = File.join(user_data_dir, 'GameLona.ini')
raise "Не найден файл: #{game_lona_path}" unless File.exist?(game_lona_path)
updated_files = []
# GameLona.ini -> [LonaRPG] Language = RUS2
game_lona_text = read_text(game_lona_path)
new_game_lona_text, lona_changed = update_section_keys_if_needed(
game_lona_text,
'LonaRPG',
{ 'Language' => 'RUS2' },
false
)
if lona_changed
write_text(game_lona_path, new_game_lona_text)
updated_files << game_lona_path
end
# Mods are loaded automatically without any GameMods.ini.
show_restart_and_exit unless updated_files.empty?