From 1d5069f961c9189df3e1d46663204764e91bdfea Mon Sep 17 00:00:00 2001 From: Markus Date: Wed, 18 Sep 2024 17:03:32 +0100 Subject: [PATCH] CoreConfig: Prevent "Error invoking callback" if core.json not found System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null. (Parameter 'value') at System.ArgumentNullException.Throw(String paramName) at System.Globalization.CultureInfo.set_CurrentCulture(CultureInfo value) at CounterStrikeSharp.API.Core.Translations.WithTemporaryCulture.SetCulture(CultureInfo cultureInfo) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/Translations/WithTemporaryCulture.cs:line 17 at CounterStrikeSharp.API.Core.Translations.WithTemporaryCulture..ctor(CultureInfo culture) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/Translations/WithTemporaryCulture.cs:line 12 at CounterStrikeSharp.API.Core.Commands.CommandManager.HandleCommandInternal(Int32 playerSlot, IntPtr commandInfo) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/Commands/CommandManager.cs:line 69 at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr) --- End of inner exception stack trace --- at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr) at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Delegate.DynamicInvokeImpl(Object[] args) at CounterStrikeSharp.API.Core.FunctionReference.b__18_0(fxScriptContext* context) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/FunctionReference.cs:line 100 --- .../CounterStrikeSharp.API/Core/CoreConfig.cs | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs index e7a1088e..7771456b 100644 --- a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs +++ b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs @@ -151,27 +151,28 @@ public void Load() _commandsRegistered = true; } - if (!File.Exists(_coreConfigPath)) + if (File.Exists(_coreConfigPath)) { - _logger.LogWarning( - "Core configuration could not be found at path \"{CoreConfigPath}\", fallback values will be used.", - _coreConfigPath); - return; - } - - try - { - var data = JsonSerializer.Deserialize(File.ReadAllText(_coreConfigPath), - new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip }); - - if (data != null) + try + { + var data = JsonSerializer.Deserialize(File.ReadAllText(_coreConfigPath), + new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip }); + + if (data != null) + { + _coreConfig = data; + } + } + catch (Exception ex) { - _coreConfig = data; + _logger.LogWarning(ex, "Failed to load core configuration, fallback values will be used"); } } - catch (Exception ex) + else { - _logger.LogWarning(ex, "Failed to load core configuration, fallback values will be used"); + _logger.LogWarning( + "Core configuration could not be found at path \"{CoreConfigPath}\", fallback values will be used.", + _coreConfigPath); } var serverCulture = CultureInfo.GetCultures(CultureTypes.AllCultures)