diff --git a/PCL.Neo.Core/Models/Configuration/GlobalSettings.cs b/PCL.Neo.Core/Models/Configuration/ConfigurationInfo.cs similarity index 95% rename from PCL.Neo.Core/Models/Configuration/GlobalSettings.cs rename to PCL.Neo.Core/Models/Configuration/ConfigurationInfo.cs index 73ebe653..828f3a7c 100644 --- a/PCL.Neo.Core/Models/Configuration/GlobalSettings.cs +++ b/PCL.Neo.Core/Models/Configuration/ConfigurationInfo.cs @@ -3,7 +3,7 @@ namespace PCL.Neo.Core.Models.Configuration; /// /// 全局配置设置,集中管理配置文件路径和默认值 /// -public static class GlobalSettings +public static class ConfigurationInfo { /// /// 应用程序配置文件路径 diff --git a/PCL.Neo.Core/Models/Configuration/ConfigurationManager.cs b/PCL.Neo.Core/Models/Configuration/ConfigurationManager.cs index a3313f47..c8ed8275 100644 --- a/PCL.Neo.Core/Models/Configuration/ConfigurationManager.cs +++ b/PCL.Neo.Core/Models/Configuration/ConfigurationManager.cs @@ -138,12 +138,12 @@ private static string GetConfigPath(string attributePath) return typeof(T).Name switch { // 特殊处理已知的配置类型 - "AppSettings" => GlobalSettings.GetConfigFilePath(GlobalSettings.AppSettingsFile), - "OAuth2Configurations" => GlobalSettings.GetConfigFilePath(GlobalSettings.OAuth2ConfigurationFile), + "AppSettings" => ConfigurationInfo.GetConfigFilePath(ConfigurationInfo.AppSettingsFile), + "OAuth2Configurations" => ConfigurationInfo.GetConfigFilePath(ConfigurationInfo.OAuth2ConfigurationFile), _ => attributePath.Contains(Path.DirectorySeparatorChar) || attributePath.Contains(Path.AltDirectorySeparatorChar) ? attributePath // 已经是完整路径 - : GlobalSettings.GetConfigFilePath(attributePath) + : ConfigurationInfo.GetConfigFilePath(attributePath) }; } @@ -283,7 +283,7 @@ private void ApplyMigrations(object config) var configPath = GetConfigPath(attribute.FilePath); var backupPath = $"{configPath}.{DateTime.Now:yyyyMMdd_HHmmss}.bak"; - var content = File.ReadAllText(configPath); + var content = await File.ReadAllTextAsync(configPath); await File.WriteAllTextAsync(backupPath, content); return true; diff --git a/PCL.Neo.Core/Models/Configuration/Data/AppSettings.cs b/PCL.Neo.Core/Models/Configuration/Data/AppSettings.cs index 805c2e32..e13d813b 100644 --- a/PCL.Neo.Core/Models/Configuration/Data/AppSettings.cs +++ b/PCL.Neo.Core/Models/Configuration/Data/AppSettings.cs @@ -1,15 +1,20 @@ +using Avalonia.Styling; +using Newtonsoft.Json; +using PCL.Neo.Core.Models.Minecraft.Java; +using System.Diagnostics; + namespace PCL.Neo.Core.Models.Configuration.Data; /// /// 应用程序全局设置 /// [ConfigurationInfo("appSettings.json")] -public record AppSettings +public record AppSettingsData { /// /// 应用程序主题 /// - public string Theme { get; set; } = "Light"; + public ThemeVariant Theme { get; set; } = ThemeVariant.Light; /// /// 应用程序语言 @@ -17,12 +22,90 @@ public record AppSettings public string Language { get; set; } = "zh-CN"; /// - /// 下载线程数 + /// 全局最大内存 /// - public int DownloadThreads { get; set; } = 4; + public int MaxMemoryMb { get; set; } = 2048; + + /// + /// 全局最小内存 + /// + public int MinMemoryMb { get; set; } = 512; + + /// + /// 是否启用内存优化 + /// + public bool MemoryOptimize { get; set; } = false; + + /// + /// 当前游戏档案 + /// + public string CurrentGameProfile { get; set; } = "Default"; + + /// + /// 当前游戏的名称 + /// + public string CurrentGame { get; set; } = string.Empty; + + /// + /// 版本隔离 + /// + public bool VersionIndie { get; set; } = true; + + /// + /// 游戏窗口标题 + /// + public string GameTitle { get; set; } = string.Empty; + + /// + /// 游戏自定义信息(暂未知作用) + /// + public string CustomizedInfo { get; set; } = string.Empty; + + /// + /// 启动器可见性 + /// + public LauncherVisibleType LauncherVisible { get; set; } = LauncherVisibleType.None; + + /// + /// 游戏进程优先级 + /// + public ProcessPriorityClass ProcessPriority { get; set; } = ProcessPriorityClass.Normal; + + /// + /// 窗口大小 + /// + public WindowSizeType WindowSize { get; set; } = WindowSizeType.Default; // TODO: 修改游戏启动代码,适配这个 /// /// 记住的Java路径 /// public List JavaPaths { get; set; } = []; + + [JsonIgnore] + public JavaRuntime? GameJava { get; set; } + + /// + /// 默认Java路径 + /// + public string DefaultJava { get; set; } = string.Empty; + + /// + /// 默认下载源 + /// + public DownloadProviderType DownloadProvider { get; set; } = DownloadProviderType.PreferOfficial; + + /// + /// 下载线程数 + /// + public int DownloadThreads { get; set; } = 4; + + /// + /// 下载速度限制,0为无限制 + /// + public ulong DownloadSpeedLimit { get; set; } = 0; + + /// + /// 标题栏内容(空为无;以Ψ开头的合法文件路径为图片标题;否则为文本) + /// + public string TitleBar { get; set; } = string.Empty; } diff --git a/PCL.Neo.Core/Models/Configuration/Data/DownloadProviderType.cs b/PCL.Neo.Core/Models/Configuration/Data/DownloadProviderType.cs new file mode 100644 index 00000000..4d3e4498 --- /dev/null +++ b/PCL.Neo.Core/Models/Configuration/Data/DownloadProviderType.cs @@ -0,0 +1,9 @@ +namespace PCL.Neo.Core.Models.Configuration.Data; + +public enum DownloadProviderType +{ + Official, + Mirrored, + PreferMirrored, + PreferOfficial +} diff --git a/PCL.Neo.Core/Models/Configuration/Data/LauncherVisibleType.cs b/PCL.Neo.Core/Models/Configuration/Data/LauncherVisibleType.cs new file mode 100644 index 00000000..4b63687e --- /dev/null +++ b/PCL.Neo.Core/Models/Configuration/Data/LauncherVisibleType.cs @@ -0,0 +1,10 @@ +namespace PCL.Neo.Core.Models.Configuration.Data; + +public enum LauncherVisibleType +{ + None, // 无动作 + CLose, // 启动后关闭 + HideThenClose, // 启动后隐藏,游戏退出后关闭 + HideThenOpen, // 启动后隐藏,游戏退出后显示 + Hide // 启动后隐藏 +} diff --git a/PCL.Neo.Core/Models/Configuration/Data/WindowSizeType.cs b/PCL.Neo.Core/Models/Configuration/Data/WindowSizeType.cs new file mode 100644 index 00000000..f928f57f --- /dev/null +++ b/PCL.Neo.Core/Models/Configuration/Data/WindowSizeType.cs @@ -0,0 +1,10 @@ +namespace PCL.Neo.Core.Models.Configuration.Data; + +public enum WindowSizeType +{ + Default, + FillScreen, + SameAsLauncher, + Customized, + Maximize +} diff --git a/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationExample.cs b/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationExample.cs deleted file mode 100644 index 996da7dd..00000000 --- a/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationExample.cs +++ /dev/null @@ -1,42 +0,0 @@ -using PCL.Neo.Core.Models.Configuration.Data; - -namespace PCL.Neo.Core.Models.Configuration.Examples; - -/// -/// 配置系统使用示例 -/// -public static class ConfigurationExample -{ - /// - /// 使用配置系统的示例方法 - /// - public static async Task ExampleAsync() - { - // 获取配置管理器实例 - var configManager = ConfigurationManager.Instance; - - // 示例1: 使用GlobalSettings和ConfigurationInfo特性加载配置 - var appSettings = await configManager.GetOrCreateConfiguration(); - - // 修改配置 - appSettings.Theme = "Dark"; - appSettings.DownloadThreads = 8; - - // 更新配置 - var updateSuccess = await configManager.UpdateConfiguration(appSettings, null); - - // 示例2: 使用显式路径加载配置 - var customConfigPath = GlobalSettings.GetConfigFilePath("custom-config.json"); - var customConfig = new AppSettings - { - Theme = "Custom", - Language = "en-US" - }; - - // 保存到指定路径 - var saveSuccess = await configManager.SaveToPath(customConfig, customConfigPath); - - // 从指定路径加载 - var loadedCustomConfig = configManager.LoadFromPath(customConfigPath); - } -} diff --git a/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationHybridExample.cs b/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationHybridExample.cs deleted file mode 100644 index 2a371969..00000000 --- a/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationHybridExample.cs +++ /dev/null @@ -1,108 +0,0 @@ -using PCL.Neo.Core.Models.Configuration.Data; - -namespace PCL.Neo.Core.Models.Configuration.Examples; - -/// -/// 配置系统混合方式使用示例 -/// -public static class ConfigurationHybridExample -{ - /// - /// 基础配置类 - 所有配置类可继承此类 - /// - public abstract class BaseSettings - { - /// - /// 配置版本 - /// - public string Version { get; set; } = "1.0.0"; - } - - /// - /// 用户设置类 - 继承自基础配置类 - /// - [ConfigurationInfo("UserSettings.json")] - public class UserSettings : BaseSettings - { - /// - /// 用户名 - /// - public string Username { get; set; } = string.Empty; - - /// - /// 上次登录时间 - /// - public string LastLoginTime { get; set; } = string.Empty; - } - - /// - /// 配置提供者类 - 包装配置管理器,提供更简便的API - /// - public class SettingsProvider - { - private readonly ConfigurationManager _configManager = ConfigurationManager.Instance; - - /// - /// 获取应用程序设置 (使用GlobalSettings静态类方式) - /// - public async Task GetAppSettingsAsync() - { - var configPath = GlobalSettings.GetConfigFilePath(GlobalSettings.AppSettingsFile); - - // 从路径加载,如果不存在则创建默认配置 - var settings = _configManager.LoadFromPath(configPath); - if (settings == null) - { - settings = new AppSettings(); - await _configManager.SaveToPath(settings, configPath); - } - - return settings; - } - - /// - /// 获取用户设置 (使用ConfigurationInfo特性方式) - /// - public async Task GetUserSettingsAsync() - { - return await _configManager.GetOrCreateConfiguration(); - } - - /// - /// 保存设置 - /// - public async Task SaveSettingsAsync(T settings) where T : class, new() - { - if (settings is AppSettings appSettings) - { - var configPath = GlobalSettings.GetConfigFilePath(GlobalSettings.AppSettingsFile); - return await _configManager.SaveToPath(appSettings, configPath); - } - - return await _configManager.UpdateConfiguration(settings, null); - } - } - - /// - /// 混合使用示例方法 - /// - public static async Task RunExampleAsync() - { - var provider = new SettingsProvider(); - - // 1. 使用GlobalSettings静态类方式 - var appSettings = await provider.GetAppSettingsAsync(); - appSettings.Theme = "Dark"; - await provider.SaveSettingsAsync(appSettings); - - // 2. 使用ConfigurationInfo特性方式 - var userSettings = await provider.GetUserSettingsAsync(); - userSettings.Username = "TestUser"; - userSettings.LastLoginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - await provider.SaveSettingsAsync(userSettings); - - // 3. 直接使用ConfigurationManager - var directConfig = await ConfigurationManager.Instance.GetOrCreateConfiguration(); - Console.WriteLine($"当前用户: {directConfig.Username}"); - } -} diff --git a/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationMigrationExample.cs b/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationMigrationExample.cs deleted file mode 100644 index 3c1d0fd6..00000000 --- a/PCL.Neo.Core/Models/Configuration/Examples/ConfigurationMigrationExample.cs +++ /dev/null @@ -1,118 +0,0 @@ -namespace PCL.Neo.Core.Models.Configuration.Examples; - -/// -/// 配置迁移和访问器使用示例 -/// -public static class ConfigurationMigrationExample -{ - /// - /// 游戏设置配置类 - 用于演示配置迁移 - /// - [ConfigurationInfo("GameSettings.json")] - public class GameSettings - { - /// - /// 配置版本 - /// - public int Version { get; set; } = 1; - - /// - /// 游戏分辨率宽度 - /// - public int ResolutionWidth { get; set; } = 1280; - - /// - /// 游戏分辨率高度 - /// - public int ResolutionHeight { get; set; } = 720; - - /// - /// 游戏音量 - /// - public int Volume { get; set; } = 80; - - // 版本2新增字段 - /// - /// 是否启用垂直同步 - /// - public bool VSync { get; set; } = true; - - // 版本3新增字段 - /// - /// 图形质量 - /// - public string GraphicsQuality { get; set; } = "Medium"; - } - - /// - /// 注册迁移示例 - /// - public static void RegisterMigrations() - { - var manager = ConfigurationManager.Instance; - - // 注册从版本1迁移到版本2的操作 - manager.RegisterMigration(settings => - { - if (settings.Version == 1) - { - Console.WriteLine("正在将GameSettings从版本1迁移到版本2..."); - // 设置新增的VSync字段默认值 - settings.VSync = true; - // 更新版本号 - settings.Version = 2; - } - }); - - // 注册从版本2迁移到版本3的操作 - manager.RegisterMigration(settings => - { - if (settings.Version == 2) - { - Console.WriteLine("正在将GameSettings从版本2迁移到版本3..."); - // 设置新增的GraphicsQuality字段默认值 - settings.GraphicsQuality = "Medium"; - // 更新版本号 - settings.Version = 3; - } - }); - } - - /// - /// 使用配置访问器示例 - /// - public static async Task UseAccessorExampleAsync() - { - // 确保已注册迁移 - RegisterMigrations(); - - // 获取配置访问器 - var accessor = ConfigurationManager.Instance.GetAccessor(); - - // 加载配置 (会自动应用迁移) - var settings = await accessor.GetConfigAsync(); - Console.WriteLine($"当前配置版本: {settings.Version}"); - Console.WriteLine($"分辨率: {settings.ResolutionWidth}x{settings.ResolutionHeight}"); - - // 更新配置 - await accessor.UpdateAsync(s => - { - s.ResolutionWidth = 1920; - s.ResolutionHeight = 1080; - s.GraphicsQuality = "High"; - }); - - Console.WriteLine("配置已更新"); - - // 备份配置 - var backupResult = await accessor.BackupAsync(); - Console.WriteLine($"配置备份{(backupResult ? "成功" : "失败")}"); - - // 重置为默认值 - if (await accessor.ResetToDefaultAsync()) - { - var defaultSettings = await accessor.GetConfigAsync(); - Console.WriteLine($"已重置为默认值: {defaultSettings.ResolutionWidth}x{defaultSettings.ResolutionHeight}"); - } - } -} diff --git a/PCL.Neo.Core/Shared.cs b/PCL.Neo.Core/Shared.cs index c963ea80..bf314503 100644 --- a/PCL.Neo.Core/Shared.cs +++ b/PCL.Neo.Core/Shared.cs @@ -1,7 +1,17 @@ +using PCL.Neo.Core.Models.Configuration; +using PCL.Neo.Core.Models.Configuration.Data; + namespace PCL.Neo.Core; // you should put every shared resources here! internal static class Shared { public static readonly HttpClient HttpClient = new(); + + public static readonly Lazy> AppSettings = + new(() => + { + var accessor = ConfigurationManager.Instance.GetAccessor(); + return accessor; + }, true); }