Skip to content

Commit

Permalink
Custom Validation Support
Browse files Browse the repository at this point in the history
  • Loading branch information
knowlife4 committed Mar 9, 2023
1 parent c84d0c1 commit 172ce36
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 59 deletions.
10 changes: 8 additions & 2 deletions ECT/Assets/Editor/ScriptTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ public class ScriptTemplates
[MenuItem("Assets/ECT/C# Script/Entity", false, -10)]
static void EntityTemplate()
{
ProjectWindowUtil.CreateScriptAssetFromTemplateFile("Assets/ScriptTemplates/ECTEntityTemplate.txt", "MyEntity.cs");
ProjectWindowUtil.CreateScriptAssetFromTemplateFile($"{GetScriptTemplateDir()}/ECTEntityTemplate.txt", "MyEntity.cs");
}

[MenuItem("Assets/ECT/C# Script/Component", false, -10)]
static void ComponentTemplate()
{
ProjectWindowUtil.CreateScriptAssetFromTemplateFile("Assets/ScriptTemplates/ECTComponentTemplate.txt", "MyComponent.cs");
ProjectWindowUtil.CreateScriptAssetFromTemplateFile($"{GetScriptTemplateDir()}/ECTComponentTemplate.txt", "MyComponent.cs");
}

static string GetScriptTemplateDir()
{
bool inPackage = AssetDatabase.IsValidFolder("Packages/com.slice.ect");
return inPackage ? "Packages/com.slice.ect/ScriptTemplates" : "Assets/ScriptTemplates";
}
}
}
8 changes: 4 additions & 4 deletions ECT/Assets/Runtime/ECTEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ public void UpdateSystems()
return dataGroup.GetSystem<TSystem>();
}

public ECTValidation QuerySystem<TSystem>(out TSystem system) where TSystem : class, ISystem
public IValidation QuerySystem<TSystem>(out TSystem system) where TSystem : class, ISystem
{
system = GetSystem<TSystem>();

return new(system != null);
return new ECTBoolValidation(system != null);
}

public TReference? GetReference<TReference>() where TReference : struct, ISceneReference
{
return referenceGroup.Get<TReference>();
}

public ECTValidation QueryReference<TReference>(out TReference reference) where TReference : struct, ISceneReference
public IValidation QueryReference<TReference>(out TReference reference) where TReference : struct, ISceneReference
{
TReference? found = GetReference<TReference>();
reference = found ?? default;

return found != null ? ECTValidation.Validate(reference.Validations) : new(false);
return found != null ? ECTValidation.ValidateMany(reference.Validations) : new(false);
}

public IEnumerable<IComponent> GetComponentsRecursively()
Expand Down
14 changes: 7 additions & 7 deletions ECT/Assets/Runtime/ECTSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ public abstract class ECTSystem<TRoot, TParent, TComponent, TSystemData> : ISyst

public void SetData(ECTSystemData data) => Data = (TSystemData)data;

public ECTValidation IsValid() => ECTValidation.Validate(Validations);
protected virtual ECTValidation[] Validations => System.Array.Empty<ECTValidation>();
public IValidation IsValid() => ECTValidation.ValidateMany(Validations);
protected virtual IValidation[] Validations => System.Array.Empty<IValidation>();

public void Initialize() => OnInitialize();
protected virtual void OnInitialize() { }

public void Update() => OnUpdate();
protected abstract void OnUpdate();

public ECTValidation QuerySystem<TSystem>(out TSystem found) where TSystem : class, ISystem => Root.QuerySystem(out found);
public ECTValidation QueryReference<TReference>(out TReference found) where TReference : struct, ISceneReference => Root.QueryReference(out found);
public ECTValidation ValidateReference<T>(T input, out T output) where T : class => ECTValidation.ValidateReference(input, out output);
public ECTValidation Subscribe(ECTAction ectAction, Action action)
public IValidation QuerySystem<TSystem>(out TSystem found) where TSystem : class, ISystem => Root.QuerySystem(out found);
public IValidation QueryReference<TReference>(out TReference found) where TReference : struct, ISceneReference => Root.QueryReference(out found);
public IValidation ValidateReference<T>(T input, out T output) where T : class => ECTValidation.ValidateReference(input, out output);
public ECTBoolValidation Subscribe(ECTAction ectAction, Action action)
{
ectAction.Subscribe(action);
return new(true);
Expand All @@ -42,7 +42,7 @@ public abstract class Parallel<TParallelData> : ECTParallelSystem<TRoot, TParent

public interface ISystem
{
public ECTValidation IsValid();
public IValidation IsValid();

public void Initialize();
public void Update();
Expand Down
19 changes: 0 additions & 19 deletions ECT/Assets/Runtime/ECTValidation.cs

This file was deleted.

4 changes: 2 additions & 2 deletions ECT/Assets/Runtime/Interfaces/IRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public interface IRoot : IParent, IReferenceSystem
public ECTSceneReferenceGroup ReferenceGroup { get; }
public ECTSystemDataGroup DataGroup { get; }

public ECTValidation QuerySystem<TSystem>(out TSystem found) where TSystem : class, ISystem;
public IValidation QuerySystem<TSystem>(out TSystem found) where TSystem : class, ISystem;

public ECTValidation QueryReference<TReference>(out TReference found) where TReference : struct, ISceneReference;
public IValidation QueryReference<TReference>(out TReference found) where TReference : struct, ISceneReference;
}
}
2 changes: 1 addition & 1 deletion ECT/Assets/Runtime/Interfaces/ISceneReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace ECT
{
public interface ISceneReference
{
public ECTValidation[] Validations { get; }
public IValidation[] Validations { get; }
}
}
8 changes: 8 additions & 0 deletions ECT/Assets/Runtime/Validations.meta

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

11 changes: 11 additions & 0 deletions ECT/Assets/Runtime/Validations/ECTBoolValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Linq;

namespace ECT
{
public struct ECTBoolValidation : IValidation
{
public ECTBoolValidation(bool successful) => Successful = successful;

public bool Successful { get; }
}
}
File renamed without changes.
10 changes: 10 additions & 0 deletions ECT/Assets/Runtime/Validations/ECTReferenceValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ECT
{
public struct ECTReferenceValidation<T> : IValidation where T : class
{
public ECTReferenceValidation(T reference) => Reference = reference;
T Reference { get; }

public bool Successful => Reference != null;
}
}
11 changes: 11 additions & 0 deletions ECT/Assets/Runtime/Validations/ECTReferenceValidation.cs.meta

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

15 changes: 15 additions & 0 deletions ECT/Assets/Runtime/Validations/ECTValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Linq;

namespace ECT
{
public static class ECTValidation
{
public static ECTBoolValidation ValidateMany(params IValidation[] validations) => new(validations.All(validation => validation.Successful == true));

public static ECTReferenceValidation<T> ValidateReference<T> (T input, out T output) where T : class
{
output = input;
return new ECTReferenceValidation<T>(input);
}
}
}
11 changes: 11 additions & 0 deletions ECT/Assets/Runtime/Validations/ECTValidation.cs.meta

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

7 changes: 7 additions & 0 deletions ECT/Assets/Runtime/Validations/IValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ECT
{
public interface IValidation
{
public bool Successful { get; }
}
}
11 changes: 11 additions & 0 deletions ECT/Assets/Runtime/Validations/IValidation.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class PlayerMovementRotate : PlayerMovement.Component
public struct SceneReference : ISceneReference
{
public Transform Target;
public ECTValidation[] Validations => new ECTValidation[]

public IValidation[] Validations => new IValidation[]
{
new(Target != null)
new ECTBoolValidation(Target != null)
};
}

Expand All @@ -28,7 +29,7 @@ public class System : System<PlayerMovementRotate>
{
SceneReference reference;

protected override ECTValidation[] Validations => new[]
protected override IValidation[] Validations => new[]
{
QueryReference(out reference)
};
Expand Down Expand Up @@ -56,7 +57,7 @@ public class ParallelSystem : System<PlayerMovementRotate>.Parallel<Data>

SceneReference reference;

protected override ECTValidation[] Validations => new[]
protected override IValidation[] Validations => new[]
{
QueryReference(out reference),
ValidateReference(Root.transform, out transform),
Expand Down
40 changes: 20 additions & 20 deletions ECT/UserSettings/Layouts/default-2022.dwlt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MonoBehaviour:
width: 1920
height: 1037
m_ShowMode: 4
m_Title: Hierarchy
m_Title: Inspector
m_RootView: {fileID: 2}
m_MinSize: {x: 875, y: 300}
m_MaxSize: {x: 10000, y: 10000}
Expand Down Expand Up @@ -119,7 +119,7 @@ MonoBehaviour:
m_MinSize: {x: 300, y: 100}
m_MaxSize: {x: 24288, y: 16192}
vertical: 0
controlID: 39
controlID: 5463
--- !u!114 &6
MonoBehaviour:
m_ObjectHideFlags: 52
Expand All @@ -144,7 +144,7 @@ MonoBehaviour:
m_MinSize: {x: 200, y: 100}
m_MaxSize: {x: 16192, y: 16192}
vertical: 1
controlID: 40
controlID: 5464
--- !u!114 &7
MonoBehaviour:
m_ObjectHideFlags: 52
Expand Down Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
m_Enabled: 1
m_EditorHideFlags: 1
m_Script: {fileID: 12006, guid: 0000000000000000e000000000000000, type: 0}
m_Name: ConsoleWindow
m_Name: ProjectBrowser
m_EditorClassIdentifier:
m_Children: []
m_Position:
Expand All @@ -242,15 +242,15 @@ MonoBehaviour:
y: 693
width: 1575
height: 294
m_MinSize: {x: 100, y: 100}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 16}
m_MinSize: {x: 231, y: 271}
m_MaxSize: {x: 10001, y: 10021}
m_ActualView: {fileID: 15}
m_Panes:
- {fileID: 15}
- {fileID: 16}
- {fileID: 17}
m_Selected: 1
m_LastSelected: 0
m_Selected: 0
m_LastSelected: 1
--- !u!114 &11
MonoBehaviour:
m_ObjectHideFlags: 52
Expand Down Expand Up @@ -742,9 +742,9 @@ MonoBehaviour:
m_PlayAudio: 0
m_AudioPlay: 0
m_Position:
m_Target: {x: 4.478275, y: 8.892512, z: 13.180664}
m_Target: {x: 3.9986882, y: 5.8860927, z: 9.1015415}
speed: 2
m_Value: {x: 4.478275, y: 8.892512, z: 13.180664}
m_Value: {x: 3.9986882, y: 5.8860927, z: 9.1015415}
m_RenderMode: 0
m_CameraMode:
drawMode: 0
Expand Down Expand Up @@ -790,9 +790,9 @@ MonoBehaviour:
m_GridAxis: 1
m_gridOpacity: 0.5
m_Rotation:
m_Target: {x: -0.010571462, y: -0.9893, z: 0.115078084, w: -0.09059773}
m_Target: {x: 0.022528378, y: -0.9761104, z: 0.12395664, w: 0.17780331}
speed: 2
m_Value: {x: -0.010569998, y: -0.989163, z: 0.11506215, w: -0.09058518}
m_Value: {x: 0.02271113, y: -0.9757069, z: 0.12390547, w: 0.17924234}
m_Size:
m_Target: 0.8660254
speed: 2
Expand Down Expand Up @@ -868,23 +868,23 @@ MonoBehaviour:
m_SkipHidden: 0
m_SearchArea: 1
m_Folders:
- Assets/Samples/Platformer/Scripts/Player
- Assets
m_Globs: []
m_OriginalText:
m_ImportLogFlags: 0
m_ViewMode: 1
m_StartGridSize: 64
m_LastFolders:
- Assets/Samples/Platformer/Scripts/Player
- Assets
m_LastFoldersGridSize: -1
m_LastProjectPath: F:\Documents\EntityComponentTree\ECT
m_LockTracker:
m_IsLocked: 0
m_FolderTreeState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: 8a5d0000
m_LastClickedID: 23946
m_ExpandedIDs: 000000003c5d00003e5d0000405d0000425d00004c5d000000ca9a3b
m_SelectedIDs: 3c5d0000
m_LastClickedID: 23868
m_ExpandedIDs: 000000003c5d00003e5d0000405d0000425d000000ca9a3b
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
Expand Down Expand Up @@ -937,8 +937,8 @@ MonoBehaviour:
m_Icon: {fileID: 0}
m_ResourceFile:
m_ListAreaState:
m_SelectedInstanceIDs: fc5c0000
m_LastClickedInstanceID: 23804
m_SelectedInstanceIDs:
m_LastClickedInstanceID: 0
m_HadKeyboardFocusLastEvent: 1
m_ExpandedInstanceIDs: c623000000000000
m_RenameOverlay:
Expand Down

0 comments on commit 172ce36

Please sign in to comment.