summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-06-03 18:48:32 -0400
committerGitHub <noreply@github.com>2020-06-03 18:48:32 -0400
commitcfc7233a0641b4c571068f67be7f2b4dba6d1c3f (patch)
treeec54a0fa1f64addd0a9b0c4c20ba2c26894de217 /src/SMAPI/Framework
parent73e3735dcd3b5789d4541e22988db9db2854a69f (diff)
parent9b41397a019e6cc32b5c1ab502fa4abdb9a9ef49 (diff)
downloadSMAPI-cfc7233a0641b4c571068f67be7f2b4dba6d1c3f.tar.gz
SMAPI-cfc7233a0641b4c571068f67be7f2b4dba6d1c3f.tar.bz2
SMAPI-cfc7233a0641b4c571068f67be7f2b4dba6d1c3f.zip
Merge pull request #718 from ZaneYork/develop
Parallel exception aggregate fix
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r--src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs100
1 files changed, 57 insertions, 43 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs
index c774c038..579c3a08 100644
--- a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs
+++ b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs
@@ -57,60 +57,74 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework
/// <returns>Returns whether the module was modified.</returns>
public bool RewriteModule()
{
- bool anyRewritten = false;
-
- foreach (TypeDefinition type in this.Module.GetTypes())
- {
- if (type.BaseType == null)
- continue; // special type like <Module>
-
- anyRewritten |= this.RewriteCustomAttributes(type.CustomAttributes);
- anyRewritten |= this.RewriteGenericParameters(type.GenericParameters);
-
- foreach (InterfaceImplementation @interface in type.Interfaces)
- anyRewritten |= this.RewriteTypeReference(@interface.InterfaceType, newType => @interface.InterfaceType = newType);
-
- if (type.BaseType.FullName != "System.Object")
- anyRewritten |= this.RewriteTypeReference(type.BaseType, newType => type.BaseType = newType);
-
- foreach (MethodDefinition method in type.Methods)
+ Tuple<bool,Exception> aggregateResult = this.Module.GetTypes()
+ .AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)
+ .Select(type =>
{
- anyRewritten |= this.RewriteTypeReference(method.ReturnType, newType => method.ReturnType = newType);
- anyRewritten |= this.RewriteGenericParameters(method.GenericParameters);
- anyRewritten |= this.RewriteCustomAttributes(method.CustomAttributes);
+ try
+ {
+ bool anyRewritten = false;
+ if (type.BaseType == null)
+ return new Tuple<bool, Exception>(anyRewritten, null); // special type like <Module>
- foreach (ParameterDefinition parameter in method.Parameters)
- anyRewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType);
+ anyRewritten |= this.RewriteCustomAttributes(type.CustomAttributes);
+ anyRewritten |= this.RewriteGenericParameters(type.GenericParameters);
- foreach (var methodOverride in method.Overrides)
- anyRewritten |= this.RewriteMethodReference(methodOverride);
+ foreach (InterfaceImplementation @interface in type.Interfaces)
+ anyRewritten |= this.RewriteTypeReference(@interface.InterfaceType, newType => @interface.InterfaceType = newType);
- if (method.HasBody)
- {
- foreach (VariableDefinition variable in method.Body.Variables)
- anyRewritten |= this.RewriteTypeReference(variable.VariableType, newType => variable.VariableType = newType);
+ if (type.BaseType.FullName != "System.Object")
+ anyRewritten |= this.RewriteTypeReference(type.BaseType, newType => type.BaseType = newType);
- // check CIL instructions
- ILProcessor cil = method.Body.GetILProcessor();
- Collection<Instruction> instructions = cil.Body.Instructions;
- for (int i = 0; i < instructions.Count; i++)
+ foreach (MethodDefinition method in type.Methods)
{
- var instruction = instructions[i];
- if (instruction.OpCode.Code == Code.Nop)
- continue;
+ anyRewritten |= this.RewriteTypeReference(method.ReturnType, newType => method.ReturnType = newType);
+ anyRewritten |= this.RewriteGenericParameters(method.GenericParameters);
+ anyRewritten |= this.RewriteCustomAttributes(method.CustomAttributes);
- anyRewritten |= this.RewriteInstruction(instruction, cil, newInstruction =>
+ foreach (ParameterDefinition parameter in method.Parameters)
+ anyRewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType);
+
+ foreach (var methodOverride in method.Overrides)
+ anyRewritten |= this.RewriteMethodReference(methodOverride);
+
+ if (method.HasBody)
{
- anyRewritten = true;
- cil.Replace(instruction, newInstruction);
- instruction = newInstruction;
- });
+ foreach (VariableDefinition variable in method.Body.Variables)
+ anyRewritten |= this.RewriteTypeReference(variable.VariableType, newType => variable.VariableType = newType);
+
+ // check CIL instructions
+ ILProcessor cil = method.Body.GetILProcessor();
+ Collection<Instruction> instructions = cil.Body.Instructions;
+ for (int i = 0; i < instructions.Count; i++)
+ {
+ var instruction = instructions[i];
+ if (instruction.OpCode.Code == Code.Nop)
+ continue;
+
+ anyRewritten |= this.RewriteInstruction(instruction, cil, newInstruction =>
+ {
+ anyRewritten = true;
+ cil.Replace(instruction, newInstruction);
+ instruction = newInstruction;
+ });
+ }
+ }
}
+
+ return new Tuple<bool, Exception>(anyRewritten, null);
}
- }
+ catch (Exception e)
+ {
+ return new Tuple<bool, Exception>(false, e.InnerException ?? e);
+ }
+ })
+ .Aggregate((tupleA, tupleB) => new Tuple<bool, Exception>(tupleA.Item1 | tupleB.Item1, tupleA.Item2 ?? tupleB.Item2)); // Aggregate result and exception
+ if (aggregateResult.Item2 != null)
+ {
+ throw aggregateResult.Item2; // rethrow inner Exception
}
-
- return anyRewritten;
+ return aggregateResult.Item1;
}