Weaver: move writing out of Weave()

This commit is contained in:
vis2k 2021-08-24 12:45:28 +08:00
parent 7088938bb7
commit e8cf20847d
2 changed files with 17 additions and 6 deletions

View File

@ -172,7 +172,15 @@ static bool WeaveFromFile(string assemblyPath, string[] dependencies, string mir
// create weaver with logger
weaver = new Weaver(new CompilationFinishedLogger());
return weaver.Weave(assembly, asmResolver);
if (weaver.Weave(assembly, asmResolver, out bool modified))
{
// write changes to file if modified
if (modified)
assembly.Write(new WriterParameters{WriteSymbols = true});
return true;
}
return false;
}
}
}

View File

@ -132,9 +132,10 @@ void CreateGeneratedCodeClass()
// would not be guaranteed to be resolve-able because
// for ILPostProcessor we can't assume where Mirror.dll
// is etc.
public bool Weave(AssemblyDefinition assembly, IAssemblyResolver resolver)
public bool Weave(AssemblyDefinition assembly, IAssemblyResolver resolver, out bool modified)
{
WeavingFailed = false;
modified = false;
try
{
Resolver = resolver;
@ -170,7 +171,7 @@ public bool Weave(AssemblyDefinition assembly, IAssemblyResolver resolver)
Stopwatch rwstopwatch = Stopwatch.StartNew();
// Need to track modified from ReaderWriterProcessor too because it could find custom read/write functions or create functions for NetworkMessages
bool modified = ReaderWriterProcessor.Process(CurrentAssembly, resolver, Log, writers, readers, ref WeavingFailed);
modified = ReaderWriterProcessor.Process(CurrentAssembly, resolver, Log, writers, readers, ref WeavingFailed);
rwstopwatch.Stop();
Console.WriteLine($"Find all reader and writers took {rwstopwatch.ElapsedMilliseconds} milliseconds");
@ -193,9 +194,11 @@ public bool Weave(AssemblyDefinition assembly, IAssemblyResolver resolver)
ReaderWriterProcessor.InitializeReaderAndWriters(CurrentAssembly, weaverTypes, writers, readers, GeneratedCodeClass);
// write to outputDir if specified, otherwise perform in-place write
WriterParameters writeParams = new WriterParameters { WriteSymbols = true };
CurrentAssembly.Write(writeParams);
// DO NOT WRITE here.
// CompilationFinishedHook writes to the file.
// ILPostProcessor writes to in-memory assembly.
// it depends on the caller.
//CurrentAssembly.Write(new WriterParameters{ WriteSymbols = true });
}
return true;