diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-06-04 19:00:48 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-06-04 19:00:48 -0400 |
commit | 92aaf3fb8ac55ac36ba2a84854a26b5d25c7a135 (patch) | |
tree | 2dc321ed32abdf5c36d856585c6b48cf973713ff /src/SMAPI/Framework | |
parent | 74a8399b9c51d9f21036a052dcedcf42453ddc14 (diff) | |
download | SMAPI-92aaf3fb8ac55ac36ba2a84854a26b5d25c7a135.tar.gz SMAPI-92aaf3fb8ac55ac36ba2a84854a26b5d25c7a135.tar.bz2 SMAPI-92aaf3fb8ac55ac36ba2a84854a26b5d25c7a135.zip |
simplify parallel rewriting logic
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs index 47cc6508..b304a732 100644 --- a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs @@ -1,6 +1,6 @@ using System; using System.Linq; -using System.Threading; +using System.Threading.Tasks; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Collections.Generic; @@ -58,20 +58,15 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework /// <returns>Returns whether the module was modified.</returns> public bool RewriteModule() { - // rewrite each type in the assembly, tracking whether any type was rewritten (Item1) - // and any exception that occurred during rewriting (Item2). - var cancellationToken = new CancellationTokenSource(); - Tuple<bool, Exception> result = this.Module - .GetTypes() - .Where(type => type.BaseType != null) // skip special types like <Module> - .AsParallel() - .WithExecutionMode(ParallelExecutionMode.ForceParallelism) - .Select(type => + bool anyRewritten = false; + Exception exception = null; + Parallel.ForEach( + source: this.Module.GetTypes().Where(type => type.BaseType != null), // skip special types like <Module> + body: type => { - if (cancellationToken.IsCancellationRequested) - return Tuple.Create(false, null as Exception); + if (exception != null) + return; - bool anyRewritten = false; try { anyRewritten |= this.RewriteCustomAttributes(type.CustomAttributes); @@ -118,22 +113,17 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework } } } - - return Tuple.Create(anyRewritten, null as Exception); } - catch (Exception e) + catch (Exception ex) { - cancellationToken.Cancel(); - return Tuple.Create(anyRewritten, e); + exception ??= ex; } - }) - .Aggregate(Tuple.Create(false, null as Exception), (a, b) => Tuple.Create(a.Item1 || b.Item1, a.Item2 ?? b.Item2)); + } + ); - bool rewritten = result.Item1; - Exception exception = result.Item2; return exception == null - ? rewritten - : throw exception; + ? anyRewritten + : throw new Exception($"Rewriting {this.Module.Name} failed.", exception); } |