From c3d27409830ba215c4380d707e443c09cc9caa20 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Thu, 10 Sep 2020 23:38:06 -0700 Subject: [PATCH] Fixes bug with configurations (#236) - [X] Changes brace style to enforced - [X] Fixes null source with assemblies.json - [X] Fixes bad path for missing json files Bumps release version --- .editorconfig | 12 ++++++ .../{Configuration => Data}/assemblies.json | 34 ++++++++-------- .../Configuration/ServerConfiguration.cs | 39 +++++++++++++++++-- .../Server/JsonConfiguration/JsonConfig.cs | 21 ++++++++-- Projects/Server/Main.cs | 9 +++-- 5 files changed, 88 insertions(+), 27 deletions(-) rename Distribution/{Configuration => Data}/assemblies.json (97%) diff --git a/.editorconfig b/.editorconfig index 1a46533b6..c105aabf5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,6 +12,7 @@ max_line_length=125 # Microsoft .NET properties csharp_new_line_before_members_in_object_initializers=false csharp_preferred_modifier_order=public, private, protected, internal, new, abstract, virtual, sealed, override, static, readonly, extern, unsafe, volatile, async:suggestion +csharp_prefer_braces=true:suggestion csharp_space_after_cast=false dotnet_diagnostic.bc42024.severity=suggestion dotnet_diagnostic.cs0078.severity=none @@ -137,6 +138,7 @@ dotnet_style_require_accessibility_modifiers=for_non_interface_members:suggestio # ReSharper properties resharper_apply_auto_detected_rules=false +resharper_braces_redundant=true resharper_case_block_braces=next_line_shifted_2 resharper_constructor_or_destructor_body=expression_body resharper_csharp_int_align_comments=true @@ -238,6 +240,16 @@ resharper_web_config_type_not_resolved_highlighting=warning resharper_web_config_wrong_module_highlighting=warning resharper_wrong_indent_size_highlighting=warning +# ReSharper inspection severities +resharper_enforce_do_while_statement_braces_highlighting=hint +resharper_enforce_fixed_statement_braces_highlighting=hint +resharper_enforce_foreach_statement_braces_highlighting=hint +resharper_enforce_for_statement_braces_highlighting=hint +resharper_enforce_if_statement_braces_highlighting=hint +resharper_enforce_lock_statement_braces_highlighting=hint +resharper_enforce_using_statement_braces_highlighting=hint +resharper_enforce_while_statement_braces_highlighting=hint + [*.{sln,csproj,bat}] end_of_line=crlf diff --git a/Distribution/Configuration/assemblies.json b/Distribution/Data/assemblies.json similarity index 97% rename from Distribution/Configuration/assemblies.json rename to Distribution/Data/assemblies.json index d01c73761..27a2c2715 100644 --- a/Distribution/Configuration/assemblies.json +++ b/Distribution/Data/assemblies.json @@ -1,17 +1,17 @@ -[ - "Argon2.Bindings.dll", - "BouncyCastle.Crypto.dll", - "MailKit.dll", - "Microsoft.AspNetCore.Connections.Abstractions.dll", - "Microsoft.AspNetCore.Http.Features.dll", - "Microsoft.Extensions.Configuration.Abstractions.dll", - "Microsoft.Extensions.DependencyInjection.Abstractions.dll", - "Microsoft.Extensions.FileProviders.Abstractions.dll", - "Microsoft.Extensions.Hosting.Abstractions.dll", - "Microsoft.Extensions.Logging.Abstractions.dll", - "Microsoft.Extensions.Primitives.dll", - "MimeKit.dll", - "System.IO.Pipelines.dll", - "Zlib.Bindings.dll", - "UOContent.dll" -] +[ + "Argon2.Bindings.dll", + "BouncyCastle.Crypto.dll", + "MailKit.dll", + "Microsoft.AspNetCore.Connections.Abstractions.dll", + "Microsoft.AspNetCore.Http.Features.dll", + "Microsoft.Extensions.Configuration.Abstractions.dll", + "Microsoft.Extensions.DependencyInjection.Abstractions.dll", + "Microsoft.Extensions.FileProviders.Abstractions.dll", + "Microsoft.Extensions.Hosting.Abstractions.dll", + "Microsoft.Extensions.Logging.Abstractions.dll", + "Microsoft.Extensions.Primitives.dll", + "MimeKit.dll", + "System.IO.Pipelines.dll", + "Zlib.Bindings.dll", + "UOContent.dll" +] diff --git a/Projects/Server/Configuration/ServerConfiguration.cs b/Projects/Server/Configuration/ServerConfiguration.cs index 75186089e..ed8e2ddc1 100644 --- a/Projects/Server/Configuration/ServerConfiguration.cs +++ b/Projects/Server/Configuration/ServerConfiguration.cs @@ -60,7 +60,9 @@ namespace Server public static string GetOrUpdateSetting(string key, string defaultValue) { if (m_Settings.settings.TryGetValue(key, out var value)) + { return value; + } SetSetting(key, value = defaultValue); return value; @@ -71,9 +73,13 @@ namespace Server int value; if (m_Settings.settings.TryGetValue(key, out var strValue)) + { value = int.TryParse(strValue, out value) ? value : defaultValue; + } else + { SetSetting(key, (value = defaultValue).ToString()); + } return value; } @@ -83,9 +89,13 @@ namespace Server bool value; if (m_Settings.settings.TryGetValue(key, out var strValue)) + { value = bool.TryParse(strValue, out value) ? value : defaultValue; + } else + { SetSetting(key, (value = defaultValue).ToString()); + } return value; } @@ -95,9 +105,13 @@ namespace Server TimeSpan value; if (m_Settings.settings.TryGetValue(key, out var strValue)) + { value = TimeSpan.TryParse(strValue, out value) ? value : defaultValue; + } else + { SetSetting(key, (value = defaultValue).ToString()); + } return value; } @@ -107,9 +121,13 @@ namespace Server T value; if (m_Settings.settings.TryGetValue(key, out var strValue)) + { value = Enum.TryParse(strValue, out value) ? value : defaultValue; + } else + { SetSetting(key, (value = defaultValue).ToString()); + } return value; } @@ -159,7 +177,9 @@ namespace Server } if (mocked) + { return; + } if (m_Settings.dataDirectories.Count == 0) { @@ -198,7 +218,10 @@ namespace Server { Console.Write("{0}> ", directories.Count > 0 ? "[finish] " : " "); var directory = Console.ReadLine(); - if (string.IsNullOrWhiteSpace(directory)) break; + if (string.IsNullOrWhiteSpace(directory)) + { + break; + } if (Directory.Exists(directory)) { @@ -227,10 +250,15 @@ namespace Server // IP:Port? Console.Write("[{0}]> ", ips.Count > 0 ? "finish" : "0.0.0.0:2593"); var ipStr = Console.ReadLine(); - if (string.IsNullOrWhiteSpace(ipStr)) break; + if (string.IsNullOrWhiteSpace(ipStr)) + { + break; + } if (ipStr.IndexOf(":", StringComparison.Ordinal) == -1) + { ipStr += ":2593"; + } if (IPEndPoint.TryParse(ipStr, out var ip)) { @@ -244,14 +272,19 @@ namespace Server } while (true); if (ips.Count == 0) + { ips.Add(new IPEndPoint(IPAddress.Any, 2593)); + } return ips; } public static void Save() { - if (m_Mocked) return; + if (m_Mocked) + { + return; + } JsonConfig.Serialize(m_FilePath, m_Settings); } diff --git a/Projects/Server/JsonConfiguration/JsonConfig.cs b/Projects/Server/JsonConfiguration/JsonConfig.cs index 2720ff551..3bc89af0b 100644 --- a/Projects/Server/JsonConfiguration/JsonConfig.cs +++ b/Projects/Server/JsonConfiguration/JsonConfig.cs @@ -44,21 +44,33 @@ namespace Server.Json options.Converters.Add(new NullableStructSerializerFactory()); options.Converters.Add(new TypeConverterFactory()); - for (var i = 0; i < converters.Length; i++) options.Converters.Add(converters[i]); + for (var i = 0; i < converters.Length; i++) + { + options.Converters.Add(converters[i]); + } return options; } public static T Deserialize(string filePath, JsonSerializerOptions options = null) { - if (!File.Exists(filePath)) return default; + if (!File.Exists(filePath)) + { + return default; + } + var text = File.ReadAllText(filePath, Utility.UTF8); return JsonSerializer.Deserialize(text, options ?? DefaultOptions); } public static void Serialize(string filePath, object value, JsonSerializerOptions options = null) { - if (File.Exists(filePath)) File.Delete(filePath); + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + Directory.CreateDirectory(Path.GetDirectoryName(filePath)); File.WriteAllText(filePath, JsonSerializer.Serialize(value, options ?? DefaultOptions)); } @@ -80,7 +92,10 @@ namespace Server.Json public static T ToObject(this JsonDocument document, JsonSerializerOptions options = null) { if (document == null) + { throw new ArgumentNullException(nameof(document)); + } + return document.RootElement.ToObject(options); } } diff --git a/Projects/Server/Main.cs b/Projects/Server/Main.cs index 444dc5e77..19ba7496c 100644 --- a/Projects/Server/Main.cs +++ b/Projects/Server/Main.cs @@ -80,7 +80,7 @@ namespace Server public static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || IsFreeBSD; public static readonly bool Unix = IsDarwin || IsFreeBSD || IsLinux; - private static readonly string assembliesConfiguration = "Configuration/assemblies.json"; + private const string assembliesConfiguration = "Data/assemblies.json"; public static bool IsRunningFromXUnit => m_IsRunningFromXUnit ??= AppDomain.CurrentDomain.GetAssemblies() @@ -444,12 +444,13 @@ namespace Server ServerConfiguration.Load(); + var assemblyPath = Path.Join(BaseDirectory, assembliesConfiguration); + // Load UOContent.dll - var assemblyFiles = JsonConfig.Deserialize>( - Path.Join(BaseDirectory, assembliesConfiguration) - ) + var assemblyFiles = JsonConfig.Deserialize>(assemblyPath) .Select(t => Path.Join(BaseDirectory, "Assemblies", t)) .ToArray(); + AssemblyHandler.LoadScripts(assemblyFiles); VerifySerialization();