Skip to content

Commit b6eebfa

Browse files
authored
Merge pull request #99 from KuraiAndras/develop
9.0.2
2 parents a7a9449 + 65bc667 commit b6eebfa

File tree

7 files changed

+143
-77
lines changed

7 files changed

+143
-77
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 9.0.2
2+
- Fix override handling when running the editor tool
3+
14
# 9.0.1
25
- Fix Serilog Template
36

MainProject/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<LangVersion>9.0</LangVersion>
44
<Nullable>enable</Nullable>
55

6-
<CurrentVersion>9.0.1</CurrentVersion>
6+
<CurrentVersion>9.0.2</CurrentVersion>
77

88
<Version>$(CurrentVersion)</Version>
99
<PakcageVersion>$(CurrentVersion)</PakcageVersion>

UnityProject/Injecter.Unity/Assets/Injecter.Hosting.Unity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.injecter.hosting.unity",
3-
"version": "9.0.1",
3+
"version": "9.0.2",
44
"displayName": "Injecter.Hosting.Unity",
55
"license": "MIT",
66
"licensesUrl": "https://github.com/KuraiAndras/Injecter/blob/master/LICENSE.md",
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.IO;
2+
using System.Linq;
3+
using UnityEditor;
4+
using UnityEditor.SceneManagement;
5+
using UnityEngine;
6+
using UnityEngine.SceneManagement;
7+
8+
namespace Injecter.Unity.Editor
9+
{
10+
internal delegate bool FilterGameObject(GameObject gameObject);
11+
internal delegate void PatchGameObject(GameObject gameObject, string location);
12+
13+
internal static class GameObjectPatcher
14+
{
15+
public static void AddComponentsToEveryPrefab(FilterGameObject filter, PatchGameObject patch)
16+
{
17+
foreach (var prefabPath in AssetDatabase.GetAllAssetPaths().Where(s => s.EndsWith(".prefab")))
18+
{
19+
var prefab = AssetDatabase.LoadMainAssetAtPath(prefabPath);
20+
21+
if (prefab is GameObject prefabObject)
22+
{
23+
AddScriptsToGameObject(prefabObject, filter, patch, $"prefab: {prefabPath}");
24+
AssetDatabase.SaveAssets();
25+
}
26+
}
27+
}
28+
29+
public static void AddComponentsToCurrentScene(FilterGameObject filter, PatchGameObject patch)
30+
{
31+
var sceneName = SceneManager.GetActiveScene().name;
32+
33+
var rootObjects = SceneManager
34+
.GetActiveScene()
35+
.GetRootGameObjects();
36+
37+
foreach (var gameObject in rootObjects)
38+
{
39+
AddScriptsToGameObject(gameObject, filter, patch, $"scene: {sceneName}");
40+
}
41+
}
42+
43+
public static void AddComponentsToEveryScene(FilterGameObject filter, PatchGameObject patch)
44+
{
45+
var dataPathFull = Path.GetFullPath(Application.dataPath);
46+
var scenes = Directory
47+
.EnumerateFiles(dataPathFull, "*.unity", SearchOption.AllDirectories)
48+
.Select(s => s.Replace(dataPathFull, string.Empty))
49+
.Select(s => $"Assets{s}")
50+
.ToArray();
51+
52+
var originalScenePath = SceneManager.GetActiveScene().path;
53+
EditorSceneManager.SaveOpenScenes();
54+
55+
foreach (var scene in scenes)
56+
{
57+
EditorSceneManager.OpenScene(scene);
58+
AddComponentsToCurrentScene(filter, patch);
59+
EditorSceneManager.SaveOpenScenes();
60+
}
61+
62+
EditorSceneManager.OpenScene(originalScenePath);
63+
}
64+
65+
public static void AddComponentsToEverything(FilterGameObject filter, PatchGameObject patch)
66+
{
67+
AddComponentsToEveryPrefab(filter, patch);
68+
AddComponentsToEveryScene(filter, patch);
69+
}
70+
71+
public static void EnsureComponentSafe<T>(MonoBehaviour instance, GameObject holder, string location) where T : Component
72+
{
73+
if (instance.TryGetComponent<T>(out _)) return;
74+
75+
var component = Undo.AddComponent<T>(holder);
76+
77+
if (component == null) return;
78+
79+
if (PrefabUtility.IsAddedComponentOverride(component) && !PrefabUtility.IsAddedGameObjectOverride(holder))
80+
{
81+
Undo.DestroyObjectImmediate(component);
82+
}
83+
else
84+
{
85+
Debug.Log($"Adding script: {typeof(T).Name} to GameObjec: {holder.name} at {location}", holder);
86+
}
87+
}
88+
89+
private static void AddScriptsToGameObject(GameObject gameObject, FilterGameObject filter, PatchGameObject patch, string location)
90+
{
91+
var instances = gameObject
92+
.GetComponentsInChildren<Transform>(true)
93+
.Where(t => t != null && filter(t.gameObject))
94+
.Select(t => t.gameObject)
95+
.ToArray();
96+
97+
for (var i = 0; i < instances.Length; i++)
98+
{
99+
var instance = instances[i];
100+
if (instance == null) continue;
101+
patch(instance, location);
102+
}
103+
}
104+
}
105+
}

UnityProject/Injecter.Unity/Assets/Injecter.Unity/Editor/GameObjectPatcher.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Injecter.Unity/Assets/Injecter.Unity/Editor/MonoInjecterFinder.cs

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#nullable enable
22
using System;
33
using System.Collections.Generic;
4-
using System.IO;
54
using System.Linq;
65
using UnityEditor;
7-
using UnityEditor.SceneManagement;
86
using UnityEngine;
9-
using UnityEngine.SceneManagement;
107

118
namespace Injecter.Unity.Editor
129
{
@@ -15,99 +12,49 @@ public static class MonoInjecterFinder
1512
private static readonly Dictionary<Type, bool> _typeCache = new Dictionary<Type, bool>();
1613

1714
[MenuItem("Tools / Injecter / Ensure injection scripts in every prefab")]
18-
public static void AddComponentsToEveryPrefab()
19-
{
20-
foreach (var prefabPath in AssetDatabase.GetAllAssetPaths().Where(s => s.EndsWith(".prefab")))
21-
{
22-
var prefab = AssetDatabase.LoadMainAssetAtPath(prefabPath);
23-
24-
if (prefab is GameObject prefabObject)
25-
{
26-
AddScriptsToGameObject(prefabObject, $"prefab: {prefabPath}");
27-
AssetDatabase.SaveAssets();
28-
}
29-
}
30-
}
15+
public static void AddComponentsToEveryPrefab() => GameObjectPatcher.AddComponentsToEveryPrefab(FilterInjectables, AddScriptsToGameObject);
3116

3217
[MenuItem("Tools / Injecter / Ensure injection scripts in current scene")]
33-
public static void AddComponentsToCurrentScene()
34-
{
35-
var sceneName = SceneManager.GetActiveScene().name;
18+
public static void AddComponentsToCurrentScene() => GameObjectPatcher.AddComponentsToCurrentScene(FilterInjectables, AddScriptsToGameObject);
3619

37-
var rootObjects = SceneManager
38-
.GetActiveScene()
39-
.GetRootGameObjects();
20+
[MenuItem("Tools / Injecter / Ensure injection scripts in every scene")]
21+
public static void AddComponentsToEveryScene() => GameObjectPatcher.AddComponentsToEveryScene(FilterInjectables, AddScriptsToGameObject);
4022

41-
foreach (var gameObject in rootObjects)
42-
{
43-
AddScriptsToGameObject(gameObject, $"scene: {sceneName}");
44-
}
45-
}
23+
[MenuItem("Tools / Injecter / Ensure injection scripts on everyting")]
24+
public static void AddComponentsToEverything() => GameObjectPatcher.AddComponentsToEverything(FilterInjectables, AddScriptsToGameObject);
4625

47-
[MenuItem("Tools / Injecter / Ensure injection scripts in every scene")]
48-
public static void AddComponentsToEveryScene()
26+
private static bool FilterInjectables(GameObject gameObject) => gameObject
27+
.GetComponents<MonoBehaviour>()
28+
.Any(b => CanBeInjected(b));
29+
30+
private static bool CanBeInjected(MonoBehaviour component)
4931
{
50-
var dataPathFull = Path.GetFullPath(Application.dataPath);
51-
var scenes = Directory
52-
.EnumerateFiles(dataPathFull, "*.unity", SearchOption.AllDirectories)
53-
.Select(s => s.Replace(dataPathFull, string.Empty))
54-
.Select(s => $"Assets{s}")
55-
.ToArray();
32+
if (component == null) return false;
5633

57-
var originalScenePath = SceneManager.GetActiveScene().path;
58-
EditorSceneManager.SaveOpenScenes();
34+
var type = component.GetType();
5935

60-
foreach (var scene in scenes)
36+
if (!_typeCache.TryGetValue(type, out var isInjectable))
6137
{
62-
EditorSceneManager.OpenScene(scene);
63-
AddComponentsToCurrentScene();
64-
EditorSceneManager.SaveOpenScenes();
38+
var members = TypeHelpers.GetInjectableMembers(type);
39+
isInjectable = members.Count != 0;
40+
_typeCache.Add(type, isInjectable);
6541
}
6642

67-
EditorSceneManager.OpenScene(originalScenePath);
68-
}
69-
70-
[MenuItem("Tools / Injecter / Ensure injection scripts on everyting")]
71-
public static void AddComponentsToEverything()
72-
{
73-
AddComponentsToEveryPrefab();
74-
AddComponentsToEveryScene();
43+
return isInjectable;
7544
}
7645

7746
private static void AddScriptsToGameObject(GameObject gameObject, string location)
7847
{
7948
var instances = gameObject
8049
.GetComponentsInChildren<MonoBehaviour>(true)
81-
.Where(b => b != null)
82-
.Where(b =>
83-
{
84-
var type = b.GetType();
85-
86-
if (!_typeCache.TryGetValue(type, out var isInjectable))
87-
{
88-
var members = TypeHelpers.GetInjectableMembers(type);
89-
isInjectable = members.Count != 0;
90-
_typeCache.Add(type, isInjectable);
91-
}
92-
93-
return isInjectable;
94-
})
50+
.Where(b => CanBeInjected(b))
9551
.ToArray();
9652

9753
for (var i = 0; i < instances.Length; i++)
9854
{
9955
var instance = instances[i];
100-
EnsureComponent<MonoInjector>(instance, location);
101-
EnsureComponent<MonoDisposer>(instance, location);
102-
}
103-
}
104-
105-
private static void EnsureComponent<T>(MonoBehaviour instance, string location) where T : Component
106-
{
107-
if (!instance.gameObject.TryGetComponent<T>(out var _))
108-
{
109-
Debug.Log($"Adding script: {typeof(T).Name} to GameObjec: {instance.gameObject.name} at {location}");
110-
Undo.AddComponent<T>(instance.gameObject);
56+
GameObjectPatcher.EnsureComponentSafe<MonoInjector>(instance, gameObject, location);
57+
GameObjectPatcher.EnsureComponentSafe<MonoDisposer>(instance, gameObject, location);
11158
}
11259
}
11360
}

UnityProject/Injecter.Unity/Assets/Injecter.Unity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.injecter.unity",
3-
"version": "9.0.1",
3+
"version": "9.0.2",
44
"displayName": "Injecter.Unity",
55
"license": "MIT",
66
"licensesUrl": "https://github.com/KuraiAndras/Injecter/blob/master/LICENSE.md",

0 commit comments

Comments
 (0)