From bcf734e8efa352185da950bb673a8a5e10fbfce7 Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Wed, 6 Sep 2023 22:29:17 -0400 Subject: [PATCH] Added error handling. --- .../LuaCs/Plugins/AssemblyManager.cs | 64 ++++++++++++++----- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/AssemblyManager.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/AssemblyManager.cs index fef4e1dc0a..109119dcb8 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/AssemblyManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/AssemblyManager.cs @@ -564,25 +564,34 @@ internal AssemblyManager() .SelectMany(a => a.GetSafeTypes()) .ToImmutableDictionary(t => t.FullName ?? t.Name, t => t); } - catch (ArgumentException _) + catch(ArgumentException _) { - // some types must've had duplicate type names, build the list while filtering - Dictionary types = new(); - foreach (var type in AssemblyLoadContext.Default.Assemblies.SelectMany(a => a.GetSafeTypes())) + try { - if (!types.ContainsKey(type.FullName ?? type.Name)) - types.Add(type.FullName ?? type.Name, type); - } + // some types must've had duplicate type names, build the list while filtering + Dictionary types = new(); + foreach (var type in AssemblyLoadContext.Default.Assemblies.SelectMany(a => a.GetSafeTypes())) + { + try + { + types.TryAdd(type.FullName ?? type.Name, type); + } + catch + { + // ignore, null key exception + } + } - _defaultContextTypes = types.ToImmutableDictionary(); - } - catch(Exception e) - { - ModUtils.Logging.PrintError($"{nameof(AssemblyManager)}: Unable to create list of default assembly types! Default AssemblyLoadContext types searching not available."); + _defaultContextTypes = types.ToImmutableDictionary(); + } + catch (Exception e) + { + ModUtils.Logging.PrintError($"{nameof(AssemblyManager)}: Unable to create list of default assembly types! Default AssemblyLoadContext types searching not available."); #if DEBUG - ModUtils.Logging.PrintError($"{nameof(AssemblyManager)}: Exception Details :{e.Message} | {e.InnerException}"); + ModUtils.Logging.PrintError($"{nameof(AssemblyManager)}: Exception Details :{e.Message} | {e.InnerException}"); #endif - _defaultContextTypes = ImmutableDictionary.Empty; + _defaultContextTypes = ImmutableDictionary.Empty; + } } } @@ -622,9 +631,30 @@ internal LoadedACL(Guid id, AssemblyManager manager) internal void RebuildTypesList() { ClearTypesList(); - _assembliesTypes = this.Acl.Assemblies - .SelectMany(a => a.GetSafeTypes()) - .ToImmutableDictionary(t => t.FullName ?? t.Name, t => t); + try + { + _assembliesTypes = this.Acl.Assemblies + .SelectMany(a => a.GetSafeTypes()) + .ToImmutableDictionary(t => t.FullName ?? t.Name, t => t); + } + catch(ArgumentException _) + { + // some types must've had duplicate type names, build the list while filtering + Dictionary types = new(); + foreach (var type in this.Acl.Assemblies.SelectMany(a => a.GetSafeTypes())) + { + try + { + types.TryAdd(type.FullName ?? type.Name, type); + } + catch + { + // ignore, null key exception + } + } + + _assembliesTypes = types.ToImmutableDictionary(); + } } internal void ClearTypesList() => _assembliesTypes.Clear();