Skip to content

Commit 2ae26a9

Browse files
committed
Some codegen wip stuff and removed permutation code
1 parent 3b3e131 commit 2ae26a9

13 files changed

+439
-140
lines changed

packages/com.seaube.ecs-idl/Editor/EcsIdl.Editor.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "EcsIdl.Editor",
33
"rootNamespace": "",
44
"references": [
5-
"EcsIdl"
5+
"EcsIdl",
6+
"Unity.EditorCoroutines.Editor"
67
],
78
"includePlatforms": [
89
"Editor"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public abstract class EcsIdlCodegenPlugin {
2+
public abstract System.Threading.Tasks.Task Generate
3+
( EcsIdlPackage pkg
4+
, string outputPath
5+
);
6+
}

packages/com.seaube.ecs-idl/Editor/EcsIdlCodegenPlugin.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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
[AttributeUsage(AttributeTargets.Class)]
4+
public class EcsIdlCodegenPluginAttribute : Attribute {
5+
public string name = "";
6+
public string extname = "";
7+
}

packages/com.seaube.ecs-idl/Editor/EcsIdlCodegenPluginAttribute.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.

packages/com.seaube.ecs-idl/Editor/EcsIdlPackagesPostprocessor.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static void OnPostprocessAllAssets
5454
}
5555
}
5656

57+
UnityEngine.Debug.Log("any packages update?!");
5758
if(importedPkgs.Count > 0 || deletedPkgs.Count > 0 || movedPkgs.Count > 0) {
5859
RefreshEcsIdlCodegen(importedPkgs, deletedPkgs, movedPkgs);
5960
}
@@ -115,5 +116,37 @@ static void RefreshEcsIdlCodegen
115116

116117
Progress.Report(progressId, 0.1f);
117118
codegen.Start();
119+
120+
UnityEngine.Debug.Log("getting plugins!");
121+
foreach(var plugin in GetCodegenPlugins()) {
122+
UnityEngine.Debug.Log("plugin!");
123+
}
124+
}
125+
126+
static IEnumerable<EcsIdlCodegenPlugin> GetCodegenPlugins() {
127+
var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
128+
129+
foreach(var assembly in assemblies) {
130+
foreach(var type in assembly.GetTypes()) {
131+
var pluginAttrs = type.GetCustomAttributes(
132+
typeof(EcsIdlCodegenPluginAttribute),
133+
inherit: true
134+
);
135+
136+
if(pluginAttrs.Length > 0) {
137+
var pluginAttr = pluginAttrs[0] as EcsIdlCodegenPluginAttribute;
138+
UnityEngine.Debug.Log($"Plugin Name: {pluginAttr!.name}");
139+
var plugin =
140+
System.Activator.CreateInstance(type) as EcsIdlCodegenPlugin;
141+
if(plugin == null) {
142+
UnityEngine.Debug.Log(
143+
$"Invalid EcsIdlCodegenPlugin: {pluginAttr!.name}"
144+
);
145+
} else {
146+
yield return plugin;
147+
}
148+
}
149+
}
150+
}
118151
}
119152
}

packages/com.seaube.ecs-idl/Editor/EcsIdlSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class EcsIdlSettings : ScriptableObject {
1111
public const string path = "Project/EcsIdlProjectSettings";
1212
public const SettingsScope scope = SettingsScope.Project;
1313

14+
/// <summary>Path to <c>ecs_idl_parser_info_codegen.exe</c>. If unset
15+
/// the latest will be downloaded and used instead.</summary>
16+
public string parseInfoCodegenPluginPath = "";
17+
18+
/// <summary>Path to <c>ecs_idl_csharp_codegen.exe</c>. If unset the latest
19+
/// will be downloaded and used instead.</summary>
20+
public string csharpCodegenPluginPath = "";
21+
1422
internal static EcsIdlSettings GetOrCreateSettings() {
1523
var settings = AssetDatabase.LoadAssetAtPath<EcsIdlSettings>(assetPath);
1624
if(settings == null) {
@@ -45,6 +53,8 @@ public static SettingsProvider CreateEcsIdlSettingsProvider() {
4553
"ECS IDL",
4654
"ECS-IDL",
4755
"ECS",
56+
"ECS Plugin",
57+
"Plugin",
4858
"Runtime",
4959
"Library",
5060
}),

packages/com.seaube.ecs-idl/Editor/EcsIdlUnitySyncDebugWindow.cs

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
using EcsIdl.UnitySync;
88
using UnityEngine.SceneManagement;
99
using UnityEditor.SceneManagement;
10+
using Unity.EditorCoroutines.Editor;
11+
using System.Threading.Tasks;
12+
using System.Collections.Concurrent;
1013

1114
using ComponentIdsList = System.Collections.Generic.SortedSet<System.Int32>;
1215

16+
#nullable enable
17+
1318
class EcsIdlUnitySyncGameObjectPreview : PreviewSceneStage {
1419

1520
public static EcsIdlUnitySyncGameObjectPreview CreateInstance() {
@@ -18,14 +23,21 @@ public static EcsIdlUnitySyncGameObjectPreview CreateInstance() {
1823
);
1924
}
2025

21-
public EntityGameObjectPool pool;
22-
public event System.Action onOpenStage;
26+
public EntityGameObjectPool? pool;
27+
public event System.Action? onOpenStage;
2328

2429
protected override void OnEnable() {
2530
base.OnEnable();
2631
scene = EditorSceneManager.NewPreviewScene();
2732
pool = EntityGameObjectPool.CreateInstance();
2833
pool.targetScene = scene;
34+
35+
if(Camera.main != null) {
36+
SceneManager.MoveGameObjectToScene(
37+
Instantiate(Camera.main.gameObject),
38+
scene
39+
);
40+
}
2941
}
3042

3143
protected override bool OnOpenStage() {
@@ -45,6 +57,8 @@ protected override GUIContent CreateHeaderContent() {
4557

4658
public class EcsIdlUnitySyncDebugWindow : EditorWindow {
4759
static bool allMonoBehavioursFoldout = false;
60+
static bool refreshing = false;
61+
static Vector2 scrollPosition = new Vector2();
4862
static ComponentIdsList testComponentIds = new ComponentIdsList();
4963

5064
[MenuItem("Window/ECS IDL/Unity Sync Debug")]
@@ -56,12 +70,11 @@ static void Init() {
5670
private List<System.Type> allMonoBehaviourTypes = new List<System.Type>();
5771
private List<System.Type> monoBehaviourTypes = new List<System.Type>();
5872
private List<System.Type> componentTypes = new List<System.Type>();
59-
private EcsIdlUnitySyncGameObjectPreview previewSceneStage;
73+
private EcsIdlUnitySyncGameObjectPreview? previewSceneStage;
6074

6175
void OnEnable() {
6276
titleContent = new GUIContent("Unity Sync Debug");
63-
RefreshTypes();
64-
RefreshComponentMonoBehaviourTypes();
77+
StartRefresh();
6578

6679
CompilationPipeline.compilationFinished += OnCompilationFinished;
6780
}
@@ -78,26 +91,59 @@ void OnCompilationFinished
7891
( object _
7992
)
8093
{
81-
RefreshTypes();
82-
RefreshComponentMonoBehaviourTypes();
94+
StartRefresh();
95+
}
8396

84-
// Re-open preview scene if it was open before a compilation started. It can
85-
// get messed up after a compile is done.
86-
if(previewSceneStage != null) {
87-
OpenPreviewScene();
97+
void StartRefresh() {
98+
refreshing = true;
99+
EditorCoroutineUtility.StartCoroutine(Refresh(), this);
100+
}
101+
102+
IEnumerator<string> Refresh() {
103+
int progressId = Progress.Start("Finding ECS IDL types");
104+
bool cancelledRequested = false;
105+
bool cancelled = false;
106+
107+
Progress.RegisterCancelCallback(progressId, () => {
108+
if(cancelled) return true;
109+
110+
cancelledRequested = true;
111+
return false;
112+
});
113+
UnityEngine.Debug.Log("Refresh()?");
114+
115+
try {
116+
foreach(var (pc, typeName) in RefreshTypes()) {
117+
if(cancelledRequested) {
118+
cancelled = true;
119+
Progress.Cancel(progressId);
120+
refreshing = false;
121+
yield break;
122+
}
123+
124+
Progress.Report(progressId, pc, typeName);
125+
126+
yield return "";
127+
}
128+
129+
RefreshComponentMonoBehaviourTypes();
130+
} finally {
131+
Progress.Remove(progressId);
132+
refreshing = false;
88133
}
89134
}
90135

91-
void RefreshTypes() {
136+
IEnumerable<(float, string)> RefreshTypes() {
92137
if(!EditorApplication.isPlaying) {
93138
UnitySyncMonoBehaviours.ClearRegisteredMonoBehaviourTypes();
139+
yield return (0F, "");
94140
}
141+
142+
allMonoBehaviourTypes.Clear();
143+
componentTypes.Clear();
144+
95145
foreach(var assembly in System.AppDomain.CurrentDomain.GetAssemblies()) {
96146
foreach(var type in assembly.GetTypes()) {
97-
if(!EditorApplication.isPlaying) {
98-
UnitySyncMonoBehaviours.RegisterMonoBehaviourType(type);
99-
}
100-
101147
if(EcsIdl.Util.IsComponent(type)) {
102148
componentTypes.Add(type);
103149
} else {
@@ -108,6 +154,29 @@ void RefreshTypes() {
108154
}
109155
}
110156
}
157+
158+
componentTypes = componentTypes.OrderBy(t => t.FullName).ToList();
159+
allMonoBehaviourTypes =
160+
allMonoBehaviourTypes.OrderBy(t => t.FullName).ToList();
161+
162+
yield return (0F, "");
163+
164+
if(!EditorApplication.isPlaying) {
165+
var registerProgress =
166+
UnitySyncMonoBehaviours.RegisterMonoBehaviourTypes(
167+
allMonoBehaviourTypes
168+
);
169+
int registeredTypes = 0;
170+
foreach(var type in registerProgress) {
171+
yield return (
172+
((float)registeredTypes / (float)allMonoBehaviourTypes.Count()),
173+
type.FullName
174+
);
175+
registeredTypes += 1;
176+
}
177+
} else {
178+
yield return (1F, "");
179+
}
111180
}
112181

113182
void RefreshComponentMonoBehaviourTypes() {
@@ -172,6 +241,7 @@ void PreviewEntityGameObjectPoolRemove
172241
}
173242

174243
void OnGUI() {
244+
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
175245
allMonoBehavioursFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(
176246
allMonoBehavioursFoldout,
177247
"All MonoBehaviour Scripts"
@@ -202,6 +272,7 @@ void OnGUI() {
202272
EditorGUILayout.Space();
203273

204274
++EditorGUI.indentLevel;
275+
205276
EditorGUILayout.LabelField("Test Components", EditorStyles.boldLabel);
206277
foreach(var componentType in componentTypes) {
207278
var componentId = EcsIdl.Util.GetComponentID(componentType);
@@ -252,6 +323,10 @@ void OnGUI() {
252323
}
253324

254325
--EditorGUI.indentLevel;
326+
327+
GUILayout.Space(20);
328+
329+
EditorGUILayout.EndScrollView();
255330
}
256331

257332
private void OpenPreviewScene() {

packages/com.seaube.ecs-idl/Editor/Importer/EcsIdlPackageEditor.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
using UnityEngine;
2-
using System.Collections.Generic;
3-
using UnityEditor;
1+
using UnityEngine;
2+
using System.Collections.Generic;
3+
using UnityEditor;
44

5+
#nullable enable
6+
57
[CustomEditor(typeof(EcsIdlPackage))]
68
[CanEditMultipleObjects]
79
public class EcsIdlPackageEditor : Editor {
810
private static Dictionary<string, bool> pkgFoldouts =
911
new Dictionary<string, bool>();
1012

1113
public override void OnInspectorGUI() {
12-
EcsIdlPackage mainPkg = null;
14+
EcsIdlPackage? mainPkg = null;
1315
List<EcsIdlPackage> pkgList = new List<EcsIdlPackage>();
1416

1517
foreach(var target in targets) {

packages/com.seaube.ecs-idl/Runtime/EcsIdl.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ public static System.Int32 GetComponentID
6363
return (System.Int32)idField.GetValue(null);
6464
}
6565

66+
public static System.Int32 GetActionID<T>() where T : EcsIdl.Action {
67+
return GetActionID(typeof(T));
68+
}
69+
70+
public static System.Int32 GetActionID
71+
( System.Type actionType
72+
)
73+
{
74+
var idField = actionType.GetField(
75+
"id",
76+
BindingFlags.Static | BindingFlags.Public
77+
);
78+
79+
return (System.Int32)idField.GetValue(null);
80+
}
81+
6682
public static IEnumerable<ComponentIdsList> GetComponentIdPermutations
6783
( ComponentIdsList componentIds
6884
)
@@ -72,15 +88,24 @@ public static IEnumerable<ComponentIdsList> GetComponentIdPermutations
7288
var count = componentIds.Count;
7389
if(count == 0) yield return new ComponentIdsList();
7490

91+
UnityEngine.Debug.Log("PERMUTATION START");
7592
for(;count > 0; --count) {
7693
if( count == componentIds.Count ) yield return componentIds;
7794
if( count > componentIds.Count ) yield break;
7895
var ptrs = Enumerable.Range(0, count).ToArray();
7996

8097
while(ptrs[0] <= componentIdsList.Count - count) {
81-
yield return new ComponentIdsList(
98+
var permutationList = new ComponentIdsList(
8299
ptrs.Select(p => componentIdsList[p])
83100
);
101+
yield return permutationList;
102+
string msg = "\t{";
103+
foreach(var id in permutationList) {
104+
msg += id + ", ";
105+
}
106+
msg = msg.TrimEnd(new char[]{',', ' '});
107+
msg += "}";
108+
UnityEngine.Debug.Log(msg);
84109

85110
++ptrs[count - 1];
86111

@@ -96,6 +121,7 @@ public static IEnumerable<ComponentIdsList> GetComponentIdPermutations
96121
}
97122
}
98123
}
124+
UnityEngine.Debug.Log("PERMUTATION END");
99125
}
100126

101127
}

0 commit comments

Comments
 (0)