Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw an exception when a field is missing #17203

Merged
merged 1 commit into from Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion OpenRA.Game/GameRules/ActorInfo.cs
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<ITraitInfo>(traitName + "Info");
if (info == null)
return null;

try
{
FieldLoader.Load(info, my);
Expand Down
11 changes: 8 additions & 3 deletions OpenRA.Game/ObjectCreator.cs
Expand Up @@ -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<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)
{
Expand All @@ -87,7 +87,12 @@ public T CreateObject<T>(string className, Dictionary<string, object> 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);
}

Expand Down