From 82e453d9a38dc75691c25e5e03513faa53eca08d Mon Sep 17 00:00:00 2001 From: abcdefg30 Date: Tue, 8 Oct 2019 17:28:30 +0200 Subject: [PATCH] Don't throw an exception when a field is missing --- OpenRA.Game/GameRules/ActorInfo.cs | 12 +++++++++++- OpenRA.Game/ObjectCreator.cs | 11 ++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/OpenRA.Game/GameRules/ActorInfo.cs b/OpenRA.Game/GameRules/ActorInfo.cs index b96c8bfe5498..4e706babe123 100644 --- a/OpenRA.Game/GameRules/ActorInfo.cs +++ b/OpenRA.Game/GameRules/ActorInfo.cs @@ -44,7 +44,11 @@ public ActorInfo(ObjectCreator creator, string name, MiniYaml node) { try { - traits.Add(LoadTraitInfo(creator, t.Key.Split('@')[0], t.Value)); + // HACK: The linter does not want to crash when a trait doesn't exist but only print an error instead + // LoadTraitInfo will only return null to signal us to abort here if the linter is running + var trait = LoadTraitInfo(creator, t.Key.Split('@')[0], t.Value); + if (trait != null) + traits.Add(trait); } catch (FieldLoader.MissingFieldsException e) { @@ -73,7 +77,13 @@ static ITraitInfo LoadTraitInfo(ObjectCreator creator, string traitName, MiniYam if (!string.IsNullOrEmpty(my.Value)) throw new YamlException("Junk value `{0}` on trait node {1}" .F(my.Value, traitName)); + + // HACK: The linter does not want to crash when a trait doesn't exist but only print an error instead + // ObjectCreator will only return null to signal us to abort here if the linter is running var info = creator.CreateObject(traitName + "Info"); + if (info == null) + return null; + try { FieldLoader.Load(info, my); diff --git a/OpenRA.Game/ObjectCreator.cs b/OpenRA.Game/ObjectCreator.cs index 4d0e7266c29a..d09907e0a55f 100644 --- a/OpenRA.Game/ObjectCreator.cs +++ b/OpenRA.Game/ObjectCreator.cs @@ -74,8 +74,8 @@ Assembly ResolveAssembly(object sender, ResolveEventArgs e) return assemblies.Select(a => a.First).FirstOrDefault(a => a.FullName == e.Name); } - public static Action MissingTypeAction = - s => { throw new InvalidOperationException("Cannot locate type: {0}".F(s)); }; + // Only used by the linter to prevent exceptions from being thrown during a lint run + public static Action MissingTypeAction = null; public T CreateObject(string className) { @@ -87,7 +87,12 @@ public T CreateObject(string className, Dictionary args) var type = typeCache[className]; if (type == null) { - MissingTypeAction(className); + // HACK: The linter does not want to crash but only print an error instead + if (MissingTypeAction != null) + MissingTypeAction(className); + else + throw new InvalidOperationException("Cannot locate type: {0}".F(className)); + return default(T); }