public AppSettingsModule(ref Entities.AppSettings appSettings) { if (!File.Exists(Program.AppEnvironment.ConfigFilePath)) { this.ApplyDefaultAppSettings(ref appSettings); return; } if (!this.LoadAppSettings(ref appSettings)) { this.ApplyDefaultAppSettings(ref appSettings); return; } }
/// <summary> /// 读取应用程序配置 /// </summary> /// <param name="appSettings"></param> /// <returns></returns> private Boolean LoadAppSettings(ref Entities.AppSettings appSettings) { FileStream fs = null; Byte[] bytes = null; try { fs = File.Open(Program.AppEnvironment.ConfigFilePath, FileMode.Open, FileAccess.Read); if (fs.Length > Int32.MaxValue) { return(false); } bytes = new Byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); }catch (Exception exception) { ConsoleColor cc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Modules.AppSettingsModule.LoadAppSettings => {exception.Message} | {exception.StackTrace}"); Console.ForegroundColor = cc; return(false); } finally { if (fs != null) { fs.Dispose(); } } if (bytes == null || bytes.Length < 1) { return(false); } String json = Encoding.UTF8.GetString(bytes); if (String.IsNullOrWhiteSpace(json) || json.IndexOf('§') > -1) { return(false); } appSettings = JsonConvert.DeserializeObject <Entities.AppSettings>(json); if (appSettings.ControlPort < 1024) { Console.WriteLine($"Modules.AppSettingsModule.LoadAppSettings[Warning] => 远程控制端口不能小于1024,已重置为25565"); appSettings.ControlPort = 25565; } return(true); }
/// <summary> /// 应用默认设置 /// </summary> /// <param name="appSettings"></param> private void ApplyDefaultAppSettings(ref Entities.AppSettings appSettings) => appSettings = new Entities.AppSettings();