Skip to content

Commit

Permalink
fix: Crashs Unity Editor when there is a compilation error
Browse files Browse the repository at this point in the history
  • Loading branch information
labbbirder committed Oct 7, 2023
1 parent adefc05 commit 072d2b6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Editor/InjectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public struct AssemblyRecord

[SerializeField]
public List<AssemblyRecord> injectionSources = new();

[SerializeField]
public List<string> compilationErrorAssemblies = new();

public bool ShouldAutoInjectEditor => enabled && autoInjectEditor;
public bool ShouldAutoInjectBuild => enabled && autoInjectBuild;
Expand Down
11 changes: 7 additions & 4 deletions Editor/InjectionSettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ void CreateGUI()
var uiAsset = GetVisualTreeAssetByGUID(rootUiAssetGUID);
var uiEleAsset = GetVisualTreeAssetByGUID(ElementUiAssetGUID);
uiAsset.CloneTree(rootVisualElement);
var lst = rootVisualElement.Q<ListView>();
var lstSource = rootVisualElement.Q<ListView>("lstSource");
var lstError = rootVisualElement.Q<ListView>("lstError");

lst.makeItem = uiEleAsset.CloneTree;
lst.bindItem = (v, i) =>
lstSource.makeItem = uiEleAsset.CloneTree;
lstSource.bindItem = (v, i) =>
{
var data = settings.injectionSources[i];
v.Q<Label>().text = Path.GetFileName(data.path);
};

lstError.makeItem = () => new Label();
var btnInject = rootVisualElement.Q<Button>("btnInject");
btnInject.clicked += ()=>{
btnInject.clicked += () =>
{
UnityInjectUtils.InjectEditor(AppDomain.CurrentDomain.GetAssemblies());
};

Expand Down
1 change: 1 addition & 0 deletions Editor/InjectionWindowUI.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ui:Toggle binding-path="autoInjectBuild" value="true" text="Build" />
</ui:GroupBox>
<ui:ListView focusable="true" name="lstSource" binding-path="injectionSources" fixed-item-height="18" show-alternating-row-backgrounds="ContentOnly" show-foldout-header="true" header-title="Injection Sources" selection-type="None" show-bound-collection-size="false" style="border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-left-color: rgb(82, 82, 82); border-right-color: rgb(82, 82, 82); border-top-color: rgb(82, 82, 82); border-bottom-color: rgb(82, 82, 82); border-top-left-radius: 6px; border-bottom-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; margin-left: 4px; margin-right: 4px; margin-top: 4px; margin-bottom: 4px; flex-shrink: 1; height: 0; flex-grow: 1;" />
<ui:ListView focusable="true" name="lstError" binding-path="compilationErrorAssemblies" fixed-item-height="18" show-alternating-row-backgrounds="ContentOnly" show-foldout-header="true" header-title="Error Assemblies" selection-type="None" show-bound-collection-size="false" style="border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-left-color: rgb(82, 82, 82); border-right-color: rgb(82, 82, 82); border-top-color: rgb(82, 82, 82); border-bottom-color: rgb(82, 82, 82); border-top-left-radius: 6px; border-bottom-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; margin-left: 4px; margin-right: 4px; margin-top: 4px; margin-bottom: 4px; flex-shrink: 1; height: 0; flex-grow: 1;" />
<ui:VisualElement style="margin-bottom: 14px; flex-direction: row;">
<ui:Button text="Save" display-tooltip-when-elided="true" name="btnSave" style="max-width: 128px; width: 100%; display: none;" />
<ui:Button text="Force Inject (Editor)" display-tooltip-when-elided="true" name="btnInject" style="max-width: 128px; width: 100%;" />
Expand Down
50 changes: 39 additions & 11 deletions Editor/UnityInjectUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static class UnityInjectUtils
[InitializeOnLoadMethod]
static void Install()
{
var previousCompiledAssemblies = new HashSet<string>();
CompilationPipeline.compilationStarted += (o) =>
{
var settings = GetScriptAssemblySettings();
Expand All @@ -40,11 +39,24 @@ static void Install()
}
};

// CompilationPipeline.assemblyCompilationFinished += (path, msg) =>
// {
// Debug.Log("compile assembly " + path);
// };


CompilationPipeline.assemblyCompilationFinished += (path, msg) =>
{
var fullPath = Path.GetFullPath(path);
var hasError = msg.Any(m => m.type == CompilerMessageType.Error);
if (hasError)
{
var list = InjectionSettings.instance.compilationErrorAssemblies;
if (!list.Contains(fullPath)) list.Add(fullPath);
}
else
{
InjectionSettings.instance.compilationErrorAssemblies.RemoveAll(p => p.Equals(fullPath));
}
EditorUtility.SetDirty(InjectionSettings.instance);
AssetDatabase.SaveAssetIfDirty(InjectionSettings.instance);
};

CompilationPipeline.compilationFinished += (o) =>
{
var settings = GetScriptAssemblySettings();
Expand All @@ -60,7 +72,8 @@ static void Install()
}
};

if(InjectionSettings.instance.ShouldAutoInjectEditor){
if (InjectionSettings.instance.ShouldAutoInjectEditor)
{
EditorApplication.delayCall += InjectEditorDelayed;
}

Expand All @@ -85,7 +98,22 @@ void InjectEditorDelayed()
{
return;
}
var assemblies = GetAssemblies(outdatedSources.ToHashSet());

// filter out assemblies who has compilation error
var injectingSources = outdatedSources.Distinct().Where(p =>
{
var fullPath = Path.GetFullPath(p);
var hasError = InjectionSettings.instance.compilationErrorAssemblies
.Contains(fullPath)
;
if (hasError)
{
Debug.LogWarning($"ignore target {p} due to compilation error");
}
return !hasError;
}).ToList();

var assemblies = GetAssemblies(injectingSources.ToHashSet());
if (assemblies.Length > 0)
{
InjectEditor(assemblies);
Expand All @@ -109,12 +137,12 @@ public static void InjectEditor(Assembly[] assemblies)
var allInjections = FixHelper.allInjections;
var freshInjections = FixHelper.GetAllInjections(assemblies);
var freshAssemblies = freshInjections
.Where(inj=>inj.InjectedMethod != null)
.Where(inj => inj.InjectedMethod != null)
.Select(inj => inj.InjectedMethod.DeclaringType.Assembly)
.Distinct().
ToHashSet();
var outcomeInjections = allInjections
.Where(inj=>inj.InjectedMethod != null)
.Where(inj => inj.InjectedMethod != null)
.Where(inj => freshAssemblies.Contains(inj.InjectedMethod.DeclaringType.Assembly))
.ToArray();
Debug.Log($"auto inject {allInjections.Length} injections, {outcomeInjections.Length} to inject");
Expand Down Expand Up @@ -142,7 +170,7 @@ public static void InjectRuntime()
static bool InjectTargetMode(InjectionInfo[] injections, bool isEditor, BuildTarget buildTarget)
{
var group = injections
.Where(inj=>inj.InjectedMethod != null)
.Where(inj => inj.InjectedMethod != null)
.GroupBy(inj => inj.InjectedMethod.DeclaringType.Assembly);
var isWritten = false;
foreach (var g in group)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.bbbirder.injection",
"displayName": "Unity Injection",
"description": "Unity注入模块,可以运行时改变被注入函数实现。",
"version": "1.3.11",
"version": "1.3.13",
"hideInEditor": false,
"author": "bbbirder <502100554@qq.com>",
"dependencies": {
Expand Down

0 comments on commit 072d2b6

Please sign in to comment.