using System; using System.IO; using GameLauncher.Engines.Tkool; namespace GameLauncher.Engines.Rgss.Games.IiniTitleRepair { /// /// Game-specific handling for RPG Maker RGSS titles whose cached title was /// stored corrupted (mojibake). The title is re-read from Game.ini using an /// encoding-safe reader. /// /// This logic is isolated per engine/per concern so that it applies only to /// the RGSS title-repair case and cannot affect any other engine's games. /// Call sites invoke it explicitly (). /// public static class GameIniTitleRepair { /// /// Repairs a mojibake RGSS title stored in the cache by re-reading the /// "Title=" entry from Game.ini (direct, or one level down). /// public static void Fix(TkoolGameItem tg) { try { string? iniPath = null; var directIni = Path.Combine(tg.RootDirectory, "Game.ini"); if (File.Exists(directIni)) { iniPath = directIni; } else if (Directory.Exists(tg.RootDirectory)) { foreach (var sub in Directory.GetDirectories(tg.RootDirectory)) { var subIni = Path.Combine(sub, "Game.ini"); if (File.Exists(subIni)) { iniPath = subIni; break; } } } if (iniPath == null) { return; } foreach (var line in ReadIniLinesSafe(iniPath)) { var trim = line.Trim(); if (trim.StartsWith("Title=", StringComparison.OrdinalIgnoreCase)) { var val = trim.Substring(6).Trim(); if (!string.IsNullOrWhiteSpace(val) && !val.Contains('\uFFFD')) { tg.Title = val; break; } } } } catch { } } /// /// Reads an INI file line-by-line, auto-detecting BOM (UTF-8/UTF-16 LE) /// and falling back to Shift_JIS (CP932), which is the standard for /// RPG Maker VX/VX Ace and Wolf RPG. /// public static string[] ReadIniLinesSafe(string filePath) { try { if (!File.Exists(filePath)) return Array.Empty(); byte[] bytes = File.ReadAllBytes(filePath); if (bytes.Length == 0) return Array.Empty(); // 1. UTF-8 BOM (EF BB BF) if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { var text = System.Text.Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3); return text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); } // 2. UTF-16 LE BOM (FF FE) if (bytes.Length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) { var text = System.Text.Encoding.Unicode.GetString(bytes, 2, bytes.Length - 2); return text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); } // 3. Shift_JIS (CP932) System.Text.Encoding encoding; try { encoding = System.Text.Encoding.GetEncoding(932); } catch { encoding = System.Text.Encoding.GetEncoding("shift_jis"); } var decoded = encoding.GetString(bytes); return decoded.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); } catch { return Array.Empty(); } } } }