Files
GameLauncher/Engines/Rgss/Games/IiniTitleRepair/GameIniTitleRepair.cs
T
armonica ae86b53147 スタイル: コーディング規約に準拠したコードフォーマットの適用(this.の明示、空白スタイルの統一)
- 自インスタンスメンバーアクセスへの this. 付与(28ファイル)
- .editorconfig に準拠したインデント・波括弧・空白スタイルの適用
- UTF-8 (BOMあり)、CRLF改行コード、行末空白除去の整合性検証
2026-10-20 10:14:26 +09:00

116 lines
4.1 KiB
C#

using System;
using System.IO;
using GameLauncher.Engines.Tkool;
namespace GameLauncher.Engines.Rgss.Games.IiniTitleRepair
{
/// <summary>
/// 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 (<see cref="Fix"/>).
/// </summary>
public static class GameIniTitleRepair
{
/// <summary>
/// Repairs a mojibake RGSS title stored in the cache by re-reading the
/// "Title=" entry from Game.ini (direct, or one level down).
/// </summary>
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[6..].Trim();
if (!string.IsNullOrWhiteSpace(val) && !val.Contains('\uFFFD'))
{
tg.Title = val;
break;
}
}
}
}
catch { }
}
/// <summary>
/// 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.
/// </summary>
public static string[] ReadIniLinesSafe(string filePath)
{
try
{
if (!File.Exists(filePath)) return [];
byte[] bytes = File.ReadAllBytes(filePath);
if (bytes.Length == 0) return [];
// 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(["\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(["\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(["\r\n", "\r", "\n"], StringSplitOptions.None);
}
catch
{
return [];
}
}
}
}