Don't throw an exception when a field is missing

This commit is contained in:
abcdefg30
2019-10-08 17:28:30 +02:00
committed by teinarss
parent f037436536
commit 9a5eaa7cb7
2 changed files with 19 additions and 4 deletions

View File

@@ -44,7 +44,11 @@ namespace OpenRA
{
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 @@ namespace OpenRA
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<ITraitInfo>(traitName + "Info");
if (info == null)
return null;
try
{
FieldLoader.Load(info, my);

View File

@@ -74,8 +74,8 @@ namespace OpenRA
return assemblies.Select(a => a.First).FirstOrDefault(a => a.FullName == e.Name);
}
public static Action<string> 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<string> MissingTypeAction = null;
public T CreateObject<T>(string className)
{
@@ -87,7 +87,12 @@ namespace OpenRA
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);
}