I need my C# code to be aware if the C# files match what is built in the assembly. I.e. if there are compilation errors then obviously it doesn’t. If there is a method that can easily tell me if Assembly-CSharp.dll currently matches the code base, that should solve it. But I can’t find a solution.
ADDITIONAL NOTES:
I tried CompilationPipeline, using the AssemblyCompilationFinished. This reports each assembly it tries to build and any errors that stopped it.
- On a failed compile, it reports failure. (EXCELLENT)
- On a successful compile, it reports no failure, but still reports the compilation completed. (EXCELLENT)
However, if the code was in a failed state and my repair was to return the file to its previous state, (removing the “j” I accidentally typed, or undoing the repo changes) It won’t compile that. It seems to detect that a file changed is no longer different from the compiled version, and doesn’t try to recompile. possibly a CRC check?
This means that if I captured the failed compilation prior, my code believes its broken, it will continue to think its broken, even if my code is fixed in some cases.
Obviously Unity internally has a method to determine if any CS files don’t match what was last successfully compiled into an assembly. But I do not know how to determine the same.
Use of “CompilationPipeline.assemblyCompilationNotRequired” doesn’t report anything either if the no compilation happened, but the code was fixed.
Here is the code I created:
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;
[InitializeOnLoad]
public static class CompilationErrorMonitor
{
public static bool LatestBuildBroken { get; private set; }
static CompilationErrorMonitor()
{
LatestBuildBroken = EditorPrefs.GetBool("LatestBuildBrokenFlag", false);
CompilationPipeline.assemblyCompilationFinished += (path, messages) =>
{
if (path != "Library/ScriptAssemblies/Assembly-CSharp.dll") return;
LatestBuildBroken = false;
foreach (var message in messages)
{
if (message.type == CompilerMessageType.Error)
{
LatestBuildBroken = true;
break;
}
}
if (LatestBuildBroken) Debug.Log($"Latest build has errors: {path}");
EditorPrefs.SetBool("LatestBuildBrokenFlag", LatestBuildBroken);
};
}
}