Skip to content

Commit

Permalink
Saves choice between system impl choices
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Oct 18, 2022
1 parent 6b33f3c commit a48f3de
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@ public static List<string> ValidateImplMethodInfo
}
#endif // UNITY_EDITOR

public static bool Enabled() {
var systemImplSource = EcsactRuntimeSettings.Get().systemImplSource;
return systemImplSource == Ecsact.SystemImplSource.Csharp;
}

[RuntimeInitializeOnLoadMethod]
internal static void Load() {
if(!Enabled()) {
return;
}

var runtimeSettings = EcsactRuntimeSettings.Get();
var runtime = EcsactRuntime.GetOrLoadDefault();
var implsAssembly = Assembly.Load(
Expand Down
3 changes: 0 additions & 3 deletions EcsactWasm/Editor/EcsactWasmRuntimeSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ public static void FindSystemImpls
private Dictionary<Int32, WasmInfo?> wasmInfos = new();
private bool allWasmInfoLoaded = false;

SerializedProperty? useDefaultLoader;
SerializedProperty? autoFindSystemImpls;

void OnEnable() {
useDefaultLoader = serializedObject.FindProperty("useDefaultLoader");
autoFindSystemImpls =
serializedObject.FindProperty("autoFindSystemImpls");
}
Expand All @@ -127,7 +125,6 @@ public override void OnInspectorGUI() {
systemLikeTypes = Ecsact.Util.GetAllSystemLikeTypes().ToList();
}

EditorGUILayout.PropertyField(useDefaultLoader);
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(autoFindSystemImpls);
if(EditorGUI.EndChangeCheck()) {
Expand Down
13 changes: 11 additions & 2 deletions EcsactWasm/Runtime/EcsactWasmRuntimeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@

namespace Ecsact {
public static class EcsactWasmRuntimeLoader {
public static bool Enabled() {
var settings = EcsactRuntimeSettings.Get();
if(settings.systemImplSource != Ecsact.SystemImplSource.WebAssembly) {
return false;
}

return true;
}

[RuntimeInitializeOnLoadMethod]
public static void OnLoad() {
var settings = EcsactWasmRuntimeSettings.Get();
if(!settings.useDefaultLoader) {
if(!Enabled()) {
return;
}

var settings = EcsactWasmRuntimeSettings.Get();
var defaultRuntime = EcsactRuntime.GetOrLoadDefault();

foreach(var entry in settings.wasmSystemEntries) {
Expand Down
1 change: 0 additions & 1 deletion EcsactWasm/Runtime/EcsactWasmRuntimeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class SystemMapEntry {
public const string resourcePath = "Settings/EcsactWasmRuntimeSettings";
public const string assetPath = "Assets/Resources/" + resourcePath + ".asset";

public bool useDefaultLoader = true;
public bool autoFindSystemImpls = true;
public List<SystemMapEntry> wasmSystemEntries = new();

Expand Down
197 changes: 111 additions & 86 deletions Editor/EcsactSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[System.Serializable]
class EcsactSettings : ScriptableObject {
public const string assetPath = "Assets/Editor/EcsactSettings.asset";
public const string path = "Project/EcsactProjectSettings";
public const string path = "Project/Ecsact";
public const SettingsScope scope = SettingsScope.Project;

public string runtimeBuilderOutputPath = "Assets/Plugins/EcsactRuntime";
Expand Down Expand Up @@ -70,7 +70,29 @@ class EcsactMethodUIBindings : ScriptableObject {
public bool methodLoaded = false;
}

static class EcsactSettingsUIElementsRegister {
class EcsactSettingsSettingsProvider : SettingsProvider {
Editor? runtimeSettingsEditor = null;
Editor? wasmRuntimeSettingsEditor = null;
Editor? csharpSystemImplSettingsEditor = null;
IMGUIContainer? runtimeSettingsContainer = null;
IMGUIContainer? csharpSystemImplSettingsContainer = null;
DropdownField? sysImplSrcDropdown = null;

public EcsactSettingsSettingsProvider()
: base(
path: EcsactSettings.path,
scopes: EcsactSettings.scope,
keywords: new HashSet<string>(new[] {
"Ecsact",
"ECS",
"ECS Plugin",
"Plugin",
"Runtime",
"Library",
})
)
{
}

internal static void SetupMethodsUI
( TemplateContainer ui
Expand Down Expand Up @@ -166,93 +188,96 @@ private static void DrawMethodsUI
}
}

[SettingsProvider]
public static SettingsProvider CreateEcsactSettingsProvider() {
Editor? runtimeSettingsEditor = null;
Editor? wasmRuntimeSettingsEditor = null;
Editor? csharpSystemImplSettingsEditor = null;
IMGUIContainer? runtimeSettingsContainer = null;
IMGUIContainer? csharpSystemImplSettingsContainer = null;

return new SettingsProvider(EcsactSettings.path, EcsactSettings.scope) {
label = "Ecsact",
inspectorUpdateHandler = () => {
if(runtimeSettingsEditor != null) {
if(runtimeSettingsEditor.RequiresConstantRepaint()) {
if(runtimeSettingsContainer != null) {
runtimeSettingsContainer.MarkDirtyRepaint();
}
}
}
Ecsact.SystemImplSource SysImplSrcDropdownValue() {
return (Ecsact.SystemImplSource)sysImplSrcDropdown!.index;
}

if(csharpSystemImplSettingsEditor != null) {
if(csharpSystemImplSettingsEditor.RequiresConstantRepaint()) {
if(csharpSystemImplSettingsContainer != null) {
csharpSystemImplSettingsContainer.MarkDirtyRepaint();
}
}
public override void OnInspectorUpdate() {
if(runtimeSettingsEditor != null) {
if(runtimeSettingsEditor.RequiresConstantRepaint()) {
if(runtimeSettingsContainer != null) {
runtimeSettingsContainer.MarkDirtyRepaint();
}
},
activateHandler = (searchContext, rootElement) => {
var settings = EcsactSettings.GetSerializedSettings();
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
"Packages/dev.ecsact.unity/Editor/EcsactSettings.uxml"
);
var ui = template.Instantiate();
BindingExtensions.Bind(ui, settings);
rootElement.Add(ui);

var runtimeSettings = EcsactRuntimeSettings.Get();
DrawMethodsUI(ui, runtimeSettings);

runtimeSettingsContainer =
ui.Q<IMGUIContainer>("runtime-settings-container");

runtimeSettingsEditor = Editor.CreateEditor(runtimeSettings);

runtimeSettingsContainer.onGUIHandler = () => {
runtimeSettingsEditor.OnInspectorGUI();
};

var systemImplsSourceDropdown =
ui.Q<DropdownField>("system-impls-source-dropdown");

var wasmRuntimeSettingsContainer =
ui.Q<IMGUIContainer>("wasm-runtime-settings-container");
wasmRuntimeSettingsEditor =
EcsactWasmEditorInternalUtil.GetEcsactWasmRuntimeSettingsEditor();
wasmRuntimeSettingsContainer.onGUIHandler = () => {
if(systemImplsSourceDropdown.index == 1) {
wasmRuntimeSettingsEditor.OnInspectorGUI();
}
};

var csharpSystemImplSettings =
Ecsact.Editor.CsharpSystemImplSettings.Get();
csharpSystemImplSettingsContainer =
ui.Q<IMGUIContainer>("csharp-system-impl-settings-container");
csharpSystemImplSettingsEditor =
Editor.CreateEditor(csharpSystemImplSettings);
csharpSystemImplSettingsContainer.onGUIHandler = () => {
if(systemImplsSourceDropdown.index == 0) {
csharpSystemImplSettingsEditor.OnInspectorGUI();
}
};
},
deactivateHandler = () => {
if(runtimeSettingsEditor != null) {
Editor.DestroyImmediate(runtimeSettingsEditor);
runtimeSettingsEditor = null;
}
}

if(csharpSystemImplSettingsEditor != null) {
if(csharpSystemImplSettingsEditor.RequiresConstantRepaint()) {
if(csharpSystemImplSettingsContainer != null) {
csharpSystemImplSettingsContainer.MarkDirtyRepaint();
}
},
keywords = new HashSet<string>(new[] {
"Ecsact",
"ECS",
"ECS Plugin",
"Plugin",
"Runtime",
"Library",
}),
}
}
}

public override void OnActivate
( string searchContext
, VisualElement rootElement
)
{
var settings = EcsactSettings.GetSerializedSettings();
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
"Packages/dev.ecsact.unity/Editor/EcsactSettings.uxml"
);
var ui = template.Instantiate();
BindingExtensions.Bind(ui, settings);
rootElement.Add(ui);

var runtimeSettings = EcsactRuntimeSettings.Get();
DrawMethodsUI(ui, runtimeSettings);

runtimeSettingsContainer =
ui.Q<IMGUIContainer>("runtime-settings-container");

runtimeSettingsEditor = Editor.CreateEditor(runtimeSettings);

runtimeSettingsContainer.onGUIHandler = () => {
runtimeSettingsEditor.OnInspectorGUI();
};

sysImplSrcDropdown =
ui.Q<DropdownField>("system-impls-source-dropdown");
sysImplSrcDropdown.index = (int)runtimeSettings.systemImplSource;

sysImplSrcDropdown.RegisterValueChangedCallback(ev => {
runtimeSettings.systemImplSource = SysImplSrcDropdownValue();
EditorUtility.SetDirty(runtimeSettings);
});

var wasmRuntimeSettingsContainer =
ui.Q<IMGUIContainer>("wasm-runtime-settings-container");
wasmRuntimeSettingsEditor =
EcsactWasmEditorInternalUtil.GetEcsactWasmRuntimeSettingsEditor();
wasmRuntimeSettingsContainer.onGUIHandler = () => {
if(SysImplSrcDropdownValue() == Ecsact.SystemImplSource.WebAssembly) {
wasmRuntimeSettingsEditor.OnInspectorGUI();
}
};

var csharpSystemImplSettings =
Ecsact.Editor.CsharpSystemImplSettings.Get();
csharpSystemImplSettingsContainer =
ui.Q<IMGUIContainer>("csharp-system-impl-settings-container");
csharpSystemImplSettingsEditor =
Editor.CreateEditor(csharpSystemImplSettings);
csharpSystemImplSettingsContainer.onGUIHandler = () => {
if(SysImplSrcDropdownValue() == Ecsact.SystemImplSource.Csharp) {
csharpSystemImplSettingsEditor.OnInspectorGUI();
}
};
}

public override void OnDeactivate() {
if(runtimeSettingsEditor != null) {
Editor.DestroyImmediate(runtimeSettingsEditor);
runtimeSettingsEditor = null;
}
}
}

static class EcsactSettingsUIElementsRegister {
[SettingsProvider]
public static SettingsProvider CreateEcsactSettingsProvider() {
return new EcsactSettingsSettingsProvider();
}
}
2 changes: 1 addition & 1 deletion Editor/EcsactSettings.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ui:GroupBox>
<ui:GroupBox>
<ui:Label text="System Implementations" display-tooltip-when-elided="true" style="font-size: 16px; -unity-font-style: bold;" />
<ui:DropdownField label="Source" index="0" choices="C#,Wasm" name="system-impls-source-dropdown" style="margin-left: 0;" />
<ui:DropdownField label="Source" index="0" choices="C#,WebAssembly" name="system-impls-source-dropdown" style="margin-left: 0;" />
<ui:IMGUIContainer name="csharp-system-impl-settings-container" />
<ui:IMGUIContainer name="wasm-runtime-settings-container" />
</ui:GroupBox>
Expand Down
1 change: 1 addition & 0 deletions Editor/Importer/EcsactImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public override void OnImportAsset(AssetImportContext ctx) {
};

try {
UnityEngine.Debug.Log(codegen.StartInfo.Arguments);
codegen.Start();
codegen.BeginOutputReadLine();
codegen.BeginErrorReadLine();
Expand Down
4 changes: 4 additions & 0 deletions Runtime/EcsactRuntimeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public struct CsharpSystemImplInfo {
public string assemblyQualifiedName;
public string implMethodName;
}

[HideInInspector]
public Ecsact.SystemImplSource systemImplSource;

[HideInInspector]
public string defaultCsharpSystemImplsAssemblyName = "";

Expand Down
6 changes: 6 additions & 0 deletions Runtime/SystemImplSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Ecsact {
public enum SystemImplSource {
Csharp,
WebAssembly,
}
}
11 changes: 11 additions & 0 deletions Runtime/SystemImplSource.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a48f3de

Please sign in to comment.