using System; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using GameLauncher.Models; namespace GameLauncher.Engines.Unity { /// /// PlayerPrefs(レジストリ)の1エントリを表すモデル /// public class PlayerPrefsEntry : INotifyPropertyChanged { private string _rawKey = string.Empty; private string _cleanKey = string.Empty; private PlayerPrefsType _type = PlayerPrefsType.Int; private object? _value; private string _editValue = string.Empty; private bool _isModified; public event PropertyChangedEventHandler? PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } /// レジストリの生キー名(例: ClearedLevel_h1378399435) public string RawKey { get => this._rawKey; set { if (this._rawKey != value) { this._rawKey = value; this.OnPropertyChanged(); } } } /// Unityのハッシュを除去した整形式キー名(例: ClearedLevel) public string CleanKey { get => this._cleanKey; set { if (this._cleanKey != value) { this._cleanKey = value; this.OnPropertyChanged(); } } } /// 値のデータ型 public PlayerPrefsType Type { get => this._type; set { if (this._type != value) { this._type = value; this.OnPropertyChanged(); this.OnPropertyChanged(nameof(this.TypeDisplay)); } } } /// 実際の値(int, float, string, byte[]) public object? Value { get => this._value; set { if (!Equals(this._value, value)) { this._value = value; this.OnPropertyChanged(); this.UpdateEditValue(); } } } /// UI表示・編集用の文字列値 public string EditValue { get => this._editValue; set { if (this._editValue != value) { this._editValue = value; this._isModified = true; this.OnPropertyChanged(); this.OnPropertyChanged(nameof(this.IsModified)); } } } /// 値が変更されたかどうか public bool IsModified { get => this._isModified; set { if (this._isModified != value) { this._isModified = value; this.OnPropertyChanged(); } } } public string TypeDisplay => this.Type switch { PlayerPrefsType.Int => "Int (数値)", PlayerPrefsType.Float => "Float (小数)", PlayerPrefsType.String => "String (文字列)", PlayerPrefsType.Binary => "Binary (バイナリ)", _ => "Unknown" }; private void UpdateEditValue() { if (this._value == null) { this._editValue = string.Empty; } else if (this._value is byte[] bytes) { try { var text = System.Text.Encoding.UTF8.GetString(bytes).TrimEnd('\0'); if (text.All(c => !char.IsControl(c) || c == '\r' || c == '\n' || c == '\t')) { this._editValue = text; this._type = PlayerPrefsType.String; return; } } catch { } if (bytes.Length == 4) { float f = BitConverter.ToSingle(bytes, 0); if (!float.IsNaN(f) && !float.IsInfinity(f)) { this._editValue = f.ToString("G"); this._type = PlayerPrefsType.Float; return; } } this._editValue = BitConverter.ToString(bytes).Replace("-", " "); } else { this._editValue = this._value.ToString() ?? string.Empty; } this._isModified = false; this.OnPropertyChanged(nameof(this.EditValue)); this.OnPropertyChanged(nameof(this.IsModified)); } } /// /// ディスク上のセーブファイル(LocalLow または ゲームフォルダ内)を表すモデル /// public class SaveFileEntry { public string FileName { get; set; } = string.Empty; public string FilePath { get; set; } = string.Empty; public long FileSize { get; set; } public DateTime LastModified { get; set; } public string LocationType { get; set; } = string.Empty; // "LocalLow" または "ゲームフォルダ" public bool IsTextOrJson { get; set; } public string FileSizeDisplay { get { if (this.FileSize < 1024) return $"{this.FileSize} B"; if (this.FileSize < 1024 * 1024) return $"{this.FileSize / 1024.0:F1} KB"; return $"{this.FileSize / (1024.0 * 1024.0):F2} MB"; } } public string LastModifiedDisplay => this.LastModified.ToString("yyyy/MM/dd HH:mm:ss"); } /// /// セーブデータのバックアップ履歴モデル /// public class SaveBackupEntry { public string Name { get; set; } = string.Empty; public string BackupPath { get; set; } = string.Empty; public DateTime Timestamp { get; set; } public string BackupType { get; set; } = string.Empty; // "Registry (.reg)" または "ファイル" public string Note { get; set; } = string.Empty; public string TimestampDisplay => this.Timestamp.ToString("yyyy/MM/dd HH:mm:ss"); } }