From 6bc4c197fa54a71008de7f4fc89b6e528cf655f1 Mon Sep 17 00:00:00 2001 From: OpOpYaDev Date: Sun, 12 May 2024 22:12:32 +0000 Subject: [PATCH] Update README.md and LICENSE --- Editor.meta | 8 + Editor/BetterConditions.Editor.asmdef | 20 +++ Editor/BetterConditions.Editor.asmdef.meta | 7 + LICENSE | 21 +++ README.md | 6 + Runtime.meta | 8 + Runtime/BetterConditions.Runtime.asmdef | 18 +++ Runtime/BetterConditions.Runtime.asmdef.meta | 7 + Runtime/Extensions.meta | 8 + Runtime/Extensions/ConditionExtensions.cs | 144 ++++++++++++++++++ .../Extensions/ConditionExtensions.cs.meta | 11 ++ Runtime/Implementations.meta | 8 + Runtime/Implementations/Application.meta | 3 + .../Application/ApplicationCondition.cs | 12 ++ .../Application/ApplicationCondition.cs.meta | 3 + .../FocusedApplicationCondition.cs | 22 +++ .../FocusedApplicationCondition.cs.meta | 3 + .../PlatformApplicationCondition.cs | 22 +++ .../PlatformApplicationCondition.cs.meta | 3 + .../Application/SystemLanguageCondition.cs | 22 +++ .../SystemLanguageCondition.cs.meta | 3 + .../CancellationRequestedCondition.cs | 16 ++ .../CancellationRequestedCondition.cs.meta | 3 + Runtime/Implementations/Complex.meta | 3 + .../Complex/AllComplexCondition.cs | 23 +++ .../Complex/AllComplexCondition.cs.meta | 3 + .../Complex/AnyComplexCondition.cs | 23 +++ .../Complex/AnyComplexCondition.cs.meta | 3 + .../Complex/ComplexCondition.cs | 50 ++++++ .../Complex/ComplexCondition.cs.meta | 3 + Runtime/Implementations/Condition.cs | 48 ++++++ Runtime/Implementations/Condition.cs.meta | 3 + Runtime/Implementations/Object.meta | 3 + .../Object/EnabledBehaviourCondition.cs | 22 +++ .../Object/EnabledBehaviourCondition.cs.meta | 3 + .../Implementations/Object/GameObject.meta | 3 + .../GameObject/ActiveInHierarchyCondition.cs | 22 +++ .../ActiveInHierarchyCondition.cs.meta | 3 + .../Object/GameObject/ActiveSelfCondition.cs | 22 +++ .../GameObject/ActiveSelfCondition.cs.meta | 3 + .../Object/NullReferenceObjectCondition.cs | 29 ++++ .../NullReferenceObjectCondition.cs.meta | 3 + .../Implementations/Object/ObjectCondition.cs | 22 +++ .../Object/ObjectCondition.cs.meta | 3 + Runtime/Implementations/PredicateCondition.cs | 54 +++++++ .../PredicateCondition.cs.meta | 3 + Runtime/Implementations/SourceCondition.cs | 66 ++++++++ .../Implementations/SourceCondition.cs.meta | 3 + Runtime/Implementations/State.meta | 3 + .../State/EqualityStateCondition.cs | 59 +++++++ .../State/EqualityStateCondition.cs.meta | 3 + .../Implementations/State/StateCondition.cs | 36 +++++ .../State/StateCondition.cs.meta | 3 + .../State/TriggerStateCondition.cs | 43 ++++++ .../State/TriggerStateCondition.cs.meta | 3 + package.json | 31 ++++ package.json.meta | 7 + 57 files changed, 989 insertions(+) create mode 100644 Editor.meta create mode 100644 Editor/BetterConditions.Editor.asmdef create mode 100644 Editor/BetterConditions.Editor.asmdef.meta create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Runtime.meta create mode 100644 Runtime/BetterConditions.Runtime.asmdef create mode 100644 Runtime/BetterConditions.Runtime.asmdef.meta create mode 100644 Runtime/Extensions.meta create mode 100644 Runtime/Extensions/ConditionExtensions.cs create mode 100644 Runtime/Extensions/ConditionExtensions.cs.meta create mode 100644 Runtime/Implementations.meta create mode 100644 Runtime/Implementations/Application.meta create mode 100644 Runtime/Implementations/Application/ApplicationCondition.cs create mode 100644 Runtime/Implementations/Application/ApplicationCondition.cs.meta create mode 100644 Runtime/Implementations/Application/FocusedApplicationCondition.cs create mode 100644 Runtime/Implementations/Application/FocusedApplicationCondition.cs.meta create mode 100644 Runtime/Implementations/Application/PlatformApplicationCondition.cs create mode 100644 Runtime/Implementations/Application/PlatformApplicationCondition.cs.meta create mode 100644 Runtime/Implementations/Application/SystemLanguageCondition.cs create mode 100644 Runtime/Implementations/Application/SystemLanguageCondition.cs.meta create mode 100644 Runtime/Implementations/CancellationRequestedCondition.cs create mode 100644 Runtime/Implementations/CancellationRequestedCondition.cs.meta create mode 100644 Runtime/Implementations/Complex.meta create mode 100644 Runtime/Implementations/Complex/AllComplexCondition.cs create mode 100644 Runtime/Implementations/Complex/AllComplexCondition.cs.meta create mode 100644 Runtime/Implementations/Complex/AnyComplexCondition.cs create mode 100644 Runtime/Implementations/Complex/AnyComplexCondition.cs.meta create mode 100644 Runtime/Implementations/Complex/ComplexCondition.cs create mode 100644 Runtime/Implementations/Complex/ComplexCondition.cs.meta create mode 100644 Runtime/Implementations/Condition.cs create mode 100644 Runtime/Implementations/Condition.cs.meta create mode 100644 Runtime/Implementations/Object.meta create mode 100644 Runtime/Implementations/Object/EnabledBehaviourCondition.cs create mode 100644 Runtime/Implementations/Object/EnabledBehaviourCondition.cs.meta create mode 100644 Runtime/Implementations/Object/GameObject.meta create mode 100644 Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs create mode 100644 Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs.meta create mode 100644 Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs create mode 100644 Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs.meta create mode 100644 Runtime/Implementations/Object/NullReferenceObjectCondition.cs create mode 100644 Runtime/Implementations/Object/NullReferenceObjectCondition.cs.meta create mode 100644 Runtime/Implementations/Object/ObjectCondition.cs create mode 100644 Runtime/Implementations/Object/ObjectCondition.cs.meta create mode 100644 Runtime/Implementations/PredicateCondition.cs create mode 100644 Runtime/Implementations/PredicateCondition.cs.meta create mode 100644 Runtime/Implementations/SourceCondition.cs create mode 100644 Runtime/Implementations/SourceCondition.cs.meta create mode 100644 Runtime/Implementations/State.meta create mode 100644 Runtime/Implementations/State/EqualityStateCondition.cs create mode 100644 Runtime/Implementations/State/EqualityStateCondition.cs.meta create mode 100644 Runtime/Implementations/State/StateCondition.cs create mode 100644 Runtime/Implementations/State/StateCondition.cs.meta create mode 100644 Runtime/Implementations/State/TriggerStateCondition.cs create mode 100644 Runtime/Implementations/State/TriggerStateCondition.cs.meta create mode 100644 package.json create mode 100644 package.json.meta diff --git a/Editor.meta b/Editor.meta new file mode 100644 index 0000000..9808e92 --- /dev/null +++ b/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0f1491f4e9b9401429ab3b1d4a6fdc39 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/BetterConditions.Editor.asmdef b/Editor/BetterConditions.Editor.asmdef new file mode 100644 index 0000000..e22ddf8 --- /dev/null +++ b/Editor/BetterConditions.Editor.asmdef @@ -0,0 +1,20 @@ +{ + "name": "BetterConditions.Editor", + "rootNamespace": "Better.Conditions.EditorAddons", + "references": [ + "GUID:01df13aca8d01e24a911bcc3e8277031", + "GUID:6adbd2a12d24f5d429f7ea1b1086e835", + "GUID:fe96bdec06a75af4184a0252929f642e" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Editor/BetterConditions.Editor.asmdef.meta b/Editor/BetterConditions.Editor.asmdef.meta new file mode 100644 index 0000000..c089a8d --- /dev/null +++ b/Editor/BetterConditions.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 963f242ccaad23c4eb2460ea43ceaa80 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e3009e4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Arcueid D'athemon (https://github.com/uurha) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..865a4dd --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Better Conditions + +[![openupm](https://img.shields.io/npm/v/com.tdw.better.conditions?label=openupm®istry_uri=https://package.openupm.com)](https://openupm.com/packages/com.tdw.better.conditions/) + +## Install +[How to install](https://github.com/uurha/BetterPluginCollection/wiki/How-to-install) diff --git a/Runtime.meta b/Runtime.meta new file mode 100644 index 0000000..cf25ade --- /dev/null +++ b/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 46b3b2023418bba4bb002b9b92ed1fa7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/BetterConditions.Runtime.asmdef b/Runtime/BetterConditions.Runtime.asmdef new file mode 100644 index 0000000..1cdea8e --- /dev/null +++ b/Runtime/BetterConditions.Runtime.asmdef @@ -0,0 +1,18 @@ +{ + "name": "BetterConditions.Runtime", + "rootNamespace": "Better.Conditions.Runtime", + "references": [ + "GUID:01df13aca8d01e24a911bcc3e8277031", + "GUID:8bd4b41f8da90144d9006c4d926c9679", + "GUID:35101f455c979e94c9a0a4793484b7fd" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Runtime/BetterConditions.Runtime.asmdef.meta b/Runtime/BetterConditions.Runtime.asmdef.meta new file mode 100644 index 0000000..fadd109 --- /dev/null +++ b/Runtime/BetterConditions.Runtime.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: fe96bdec06a75af4184a0252929f642e +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Extensions.meta b/Runtime/Extensions.meta new file mode 100644 index 0000000..7c46dc8 --- /dev/null +++ b/Runtime/Extensions.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 63e88d414e9eb154b8e9167b2b09652c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Extensions/ConditionExtensions.cs b/Runtime/Extensions/ConditionExtensions.cs new file mode 100644 index 0000000..1f7e342 --- /dev/null +++ b/Runtime/Extensions/ConditionExtensions.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Better.Commons.Runtime.Conditions; +using Better.Commons.Runtime.Utility; + +namespace Better.Commons.Runtime.Extensions +{ + public static class ConditionExtensions + { + public static void Rebuild(this IEnumerable self) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return; + } + + foreach (var condition in self) + { + condition.Rebuild(); + } + } + + public static bool Validate(this IEnumerable self, bool logException = Condition.DefaultLogException) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + foreach (var condition in self) + { + if (!condition.Validate(logException)) + { + return false; + } + } + + return true; + } + + public static bool InvokeAll(this IEnumerable self) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + foreach (var condition in self) + { + if (!condition.Invoke()) + { + return false; + } + } + + return true; + } + + public static bool SafeInvokeAll(this IEnumerable self, bool logException = Condition.DefaultLogException) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + foreach (var condition in self) + { + if (!condition.SafeInvoke(logException)) + { + return false; + } + } + + return true; + } + + public static bool InvokeAny(this IEnumerable self) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + foreach (var condition in self) + { + if (condition.Invoke()) + { + return true; + } + } + + return false; + } + + public static bool SafeInvokeAny(this IEnumerable self, bool logException = Condition.DefaultLogException) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + foreach (var condition in self) + { + if (condition.SafeInvoke(logException)) + { + return true; + } + } + + return false; + } + + public static void CollectValidated(this ICollection self, IEnumerable source, bool logException = true) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return; + } + + if (source == null) + { + DebugUtility.LogException(nameof(source)); + return; + } + + foreach (var condition in source) + { + if (condition.Validate(logException) + && !self.Contains(condition)) + { + self.Add(condition); + } + } + } + } +} \ No newline at end of file diff --git a/Runtime/Extensions/ConditionExtensions.cs.meta b/Runtime/Extensions/ConditionExtensions.cs.meta new file mode 100644 index 0000000..ccc7fed --- /dev/null +++ b/Runtime/Extensions/ConditionExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: afaa20242ce945c40b28251a75324416 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Implementations.meta b/Runtime/Implementations.meta new file mode 100644 index 0000000..bd14b3f --- /dev/null +++ b/Runtime/Implementations.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 20685138c51229942aa7ab63ab24c875 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Implementations/Application.meta b/Runtime/Implementations/Application.meta new file mode 100644 index 0000000..f99455d --- /dev/null +++ b/Runtime/Implementations/Application.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c1adba7167b64c94aaf17658df0be728 +timeCreated: 1715373536 \ No newline at end of file diff --git a/Runtime/Implementations/Application/ApplicationCondition.cs b/Runtime/Implementations/Application/ApplicationCondition.cs new file mode 100644 index 0000000..9a0dbfd --- /dev/null +++ b/Runtime/Implementations/Application/ApplicationCondition.cs @@ -0,0 +1,12 @@ +using System; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public abstract class ApplicationCondition : StateCondition + { + protected ApplicationCondition(TState targetState) : base(targetState) + { + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Application/ApplicationCondition.cs.meta b/Runtime/Implementations/Application/ApplicationCondition.cs.meta new file mode 100644 index 0000000..4899282 --- /dev/null +++ b/Runtime/Implementations/Application/ApplicationCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ff8e88baaf1d4a17b84779eab71a75e7 +timeCreated: 1713085107 \ No newline at end of file diff --git a/Runtime/Implementations/Application/FocusedApplicationCondition.cs b/Runtime/Implementations/Application/FocusedApplicationCondition.cs new file mode 100644 index 0000000..f9591c6 --- /dev/null +++ b/Runtime/Implementations/Application/FocusedApplicationCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class FocusedApplicationCondition : ApplicationCondition + { + public FocusedApplicationCondition(bool state) : base(state) + { + } + + public FocusedApplicationCondition() : this(true) + { + } + + public override bool Invoke() + { + return Application.isFocused == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Application/FocusedApplicationCondition.cs.meta b/Runtime/Implementations/Application/FocusedApplicationCondition.cs.meta new file mode 100644 index 0000000..8d37b49 --- /dev/null +++ b/Runtime/Implementations/Application/FocusedApplicationCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f7317a3e68ac43f2a868b34c497386fd +timeCreated: 1715375424 \ No newline at end of file diff --git a/Runtime/Implementations/Application/PlatformApplicationCondition.cs b/Runtime/Implementations/Application/PlatformApplicationCondition.cs new file mode 100644 index 0000000..cc82ec4 --- /dev/null +++ b/Runtime/Implementations/Application/PlatformApplicationCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class PlatformApplicationCondition : ApplicationCondition + { + public PlatformApplicationCondition(RuntimePlatform state) : base(state) + { + } + + protected PlatformApplicationCondition() : this(RuntimePlatform.WindowsEditor) + { + } + + public override bool Invoke() + { + return Application.platform == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Application/PlatformApplicationCondition.cs.meta b/Runtime/Implementations/Application/PlatformApplicationCondition.cs.meta new file mode 100644 index 0000000..c6dc3c5 --- /dev/null +++ b/Runtime/Implementations/Application/PlatformApplicationCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d7dbacc62a9d409da7e4e6f2775c8157 +timeCreated: 1715376757 \ No newline at end of file diff --git a/Runtime/Implementations/Application/SystemLanguageCondition.cs b/Runtime/Implementations/Application/SystemLanguageCondition.cs new file mode 100644 index 0000000..f64d6ed --- /dev/null +++ b/Runtime/Implementations/Application/SystemLanguageCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class SystemLanguageCondition : ApplicationCondition + { + public SystemLanguageCondition(SystemLanguage state) : base(state) + { + } + + protected SystemLanguageCondition() : this(SystemLanguage.English) + { + } + + public override bool Invoke() + { + return Application.systemLanguage == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Application/SystemLanguageCondition.cs.meta b/Runtime/Implementations/Application/SystemLanguageCondition.cs.meta new file mode 100644 index 0000000..1d0e5a2 --- /dev/null +++ b/Runtime/Implementations/Application/SystemLanguageCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6b240c81f66e47f3b0cea9fa826960a2 +timeCreated: 1715375432 \ No newline at end of file diff --git a/Runtime/Implementations/CancellationRequestedCondition.cs b/Runtime/Implementations/CancellationRequestedCondition.cs new file mode 100644 index 0000000..c6ad642 --- /dev/null +++ b/Runtime/Implementations/CancellationRequestedCondition.cs @@ -0,0 +1,16 @@ +using System.Threading; + +namespace Better.Commons.Runtime.Conditions +{ + public class CancellationRequestedCondition : SourceCondition + { + public CancellationRequestedCondition(CancellationToken source, bool state = true) : base(source, state) + { + } + + public override bool Invoke() + { + return Source.IsCancellationRequested == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/CancellationRequestedCondition.cs.meta b/Runtime/Implementations/CancellationRequestedCondition.cs.meta new file mode 100644 index 0000000..13c3c4a --- /dev/null +++ b/Runtime/Implementations/CancellationRequestedCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4b2783d3c894483780779ec015981da7 +timeCreated: 1713083097 \ No newline at end of file diff --git a/Runtime/Implementations/Complex.meta b/Runtime/Implementations/Complex.meta new file mode 100644 index 0000000..23b2dd4 --- /dev/null +++ b/Runtime/Implementations/Complex.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 900b4166445848bcbdfa61775878244d +timeCreated: 1708395376 \ No newline at end of file diff --git a/Runtime/Implementations/Complex/AllComplexCondition.cs b/Runtime/Implementations/Complex/AllComplexCondition.cs new file mode 100644 index 0000000..3f104cd --- /dev/null +++ b/Runtime/Implementations/Complex/AllComplexCondition.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using Better.Commons.Runtime.Extensions; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class AllComplexCondition : ComplexCondition + { + public AllComplexCondition(IEnumerable conditions) : base(conditions) + { + } + + protected AllComplexCondition() + { + } + + public override bool Invoke() + { + return CurrentSet.InvokeAll(); + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Complex/AllComplexCondition.cs.meta b/Runtime/Implementations/Complex/AllComplexCondition.cs.meta new file mode 100644 index 0000000..66014e6 --- /dev/null +++ b/Runtime/Implementations/Complex/AllComplexCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a24836493cc149c48e4adb9f888c77e1 +timeCreated: 1708395339 \ No newline at end of file diff --git a/Runtime/Implementations/Complex/AnyComplexCondition.cs b/Runtime/Implementations/Complex/AnyComplexCondition.cs new file mode 100644 index 0000000..7606515 --- /dev/null +++ b/Runtime/Implementations/Complex/AnyComplexCondition.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using Better.Commons.Runtime.Extensions; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class AnyComplexCondition : ComplexCondition + { + public AnyComplexCondition(IEnumerable conditions) : base(conditions) + { + } + + protected AnyComplexCondition() + { + } + + public override bool Invoke() + { + return CurrentSet.InvokeAny(); + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Complex/AnyComplexCondition.cs.meta b/Runtime/Implementations/Complex/AnyComplexCondition.cs.meta new file mode 100644 index 0000000..67e63f6 --- /dev/null +++ b/Runtime/Implementations/Complex/AnyComplexCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ccfa81ed9f4542ecb43fea82fa20744c +timeCreated: 1708395197 \ No newline at end of file diff --git a/Runtime/Implementations/Complex/ComplexCondition.cs b/Runtime/Implementations/Complex/ComplexCondition.cs new file mode 100644 index 0000000..19d01a1 --- /dev/null +++ b/Runtime/Implementations/Complex/ComplexCondition.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Better.Attributes.Runtime.Select; +using Better.Commons.Runtime.Extensions; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public abstract class ComplexCondition : Condition + { + [Select] + [SerializeReference] private Condition[] _sourceSet; + + protected HashSet CurrentSet { get; private set; } + + public ComplexCondition(IEnumerable conditions) + { + _sourceSet = conditions.ToArray(); + } + + protected ComplexCondition() : this(Array.Empty()) + { + } + + public override void Rebuild() + { + base.Rebuild(); + + CurrentSet ??= new HashSet(); + CurrentSet.Clear(); + CurrentSet.CollectValidated(_sourceSet); + CurrentSet.Rebuild(); + } + + protected override bool Validate(out Exception exception) + { + if (_sourceSet == null) + { + var message = $"{nameof(_sourceSet)} cannot be null"; + exception = new InvalidOperationException(message); + return false; + } + + exception = null; + return true; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Complex/ComplexCondition.cs.meta b/Runtime/Implementations/Complex/ComplexCondition.cs.meta new file mode 100644 index 0000000..ec84b49 --- /dev/null +++ b/Runtime/Implementations/Complex/ComplexCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7f4ef74ae5a14f078d3af7817fc20188 +timeCreated: 1708394789 \ No newline at end of file diff --git a/Runtime/Implementations/Condition.cs b/Runtime/Implementations/Condition.cs new file mode 100644 index 0000000..39eb255 --- /dev/null +++ b/Runtime/Implementations/Condition.cs @@ -0,0 +1,48 @@ +using System; +using Better.Commons.Runtime.Utility; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public abstract class Condition + { + public const bool DefaultLogException = true; + + public virtual void Rebuild() + { + } + + public abstract bool Invoke(); + + public bool SafeInvoke(bool logException = DefaultLogException) + { + try + { + return Invoke(); + } + catch (Exception exception) + { + if (logException) + { + DebugUtility.LogException(exception); + } + + return false; + } + } + + public bool Validate(bool logException = DefaultLogException) + { + var isValid = Validate(out var exception); + if (!isValid && logException) + { + exception ??= new InvalidOperationException(); + DebugUtility.LogException(exception); + } + + return isValid; + } + + protected abstract bool Validate(out Exception exception); + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Condition.cs.meta b/Runtime/Implementations/Condition.cs.meta new file mode 100644 index 0000000..de0fc7c --- /dev/null +++ b/Runtime/Implementations/Condition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d68645f0cd7e43a390ff16e4ab6e3c4a +timeCreated: 1712893793 \ No newline at end of file diff --git a/Runtime/Implementations/Object.meta b/Runtime/Implementations/Object.meta new file mode 100644 index 0000000..e4a5e33 --- /dev/null +++ b/Runtime/Implementations/Object.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 49921092d45040c7b223825f897ff317 +timeCreated: 1713081463 \ No newline at end of file diff --git a/Runtime/Implementations/Object/EnabledBehaviourCondition.cs b/Runtime/Implementations/Object/EnabledBehaviourCondition.cs new file mode 100644 index 0000000..f066514 --- /dev/null +++ b/Runtime/Implementations/Object/EnabledBehaviourCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class EnabledBehaviourCondition : ObjectCondition + { + public EnabledBehaviourCondition(Behaviour source, bool state) : base(source, state) + { + } + + protected EnabledBehaviourCondition() : this(default, true) + { + } + + public override bool Invoke() + { + return Source.enabled == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Object/EnabledBehaviourCondition.cs.meta b/Runtime/Implementations/Object/EnabledBehaviourCondition.cs.meta new file mode 100644 index 0000000..9433922 --- /dev/null +++ b/Runtime/Implementations/Object/EnabledBehaviourCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 34b5371736d244f1a80376a8236ed584 +timeCreated: 1713081507 \ No newline at end of file diff --git a/Runtime/Implementations/Object/GameObject.meta b/Runtime/Implementations/Object/GameObject.meta new file mode 100644 index 0000000..19e89c9 --- /dev/null +++ b/Runtime/Implementations/Object/GameObject.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0bc128b6813242f5976e17df978e3331 +timeCreated: 1713081476 \ No newline at end of file diff --git a/Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs b/Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs new file mode 100644 index 0000000..3fbdf83 --- /dev/null +++ b/Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class ActiveInHierarchyCondition : ObjectCondition + { + public ActiveInHierarchyCondition(GameObject source, bool state) : base(source, state) + { + } + + protected ActiveInHierarchyCondition() : this(default, true) + { + } + + public override bool Invoke() + { + return Source.activeInHierarchy == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs.meta b/Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs.meta new file mode 100644 index 0000000..7d7e74b --- /dev/null +++ b/Runtime/Implementations/Object/GameObject/ActiveInHierarchyCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ef806ebf032f408c9de6ab72a3691624 +timeCreated: 1712895324 \ No newline at end of file diff --git a/Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs b/Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs new file mode 100644 index 0000000..20a20d2 --- /dev/null +++ b/Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class ActiveSelfCondition : ObjectCondition + { + public ActiveSelfCondition(GameObject source, bool state) : base(source, state) + { + } + + protected ActiveSelfCondition() : this(default, true) + { + } + + public override bool Invoke() + { + return Source.activeSelf == TargetState; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs.meta b/Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs.meta new file mode 100644 index 0000000..0892a02 --- /dev/null +++ b/Runtime/Implementations/Object/GameObject/ActiveSelfCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f013e7e9687d45c6952b79f9f48ced9c +timeCreated: 1713080892 \ No newline at end of file diff --git a/Runtime/Implementations/Object/NullReferenceObjectCondition.cs b/Runtime/Implementations/Object/NullReferenceObjectCondition.cs new file mode 100644 index 0000000..0a0a415 --- /dev/null +++ b/Runtime/Implementations/Object/NullReferenceObjectCondition.cs @@ -0,0 +1,29 @@ +using System; +using Object = UnityEngine.Object; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class NullReferenceObjectCondition : ObjectCondition + { + public NullReferenceObjectCondition(Object source, bool state) : base(source, state) + { + } + + protected NullReferenceObjectCondition() : this(default, true) + { + } + + public override bool Invoke() + { + var isNull = Source == null; + return isNull == TargetState; + } + + protected override bool Validate(out Exception exception) + { + exception = null; + return false; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Object/NullReferenceObjectCondition.cs.meta b/Runtime/Implementations/Object/NullReferenceObjectCondition.cs.meta new file mode 100644 index 0000000..f9eca4f --- /dev/null +++ b/Runtime/Implementations/Object/NullReferenceObjectCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7343b9edbb684ece9f0cbcc705ec2eee +timeCreated: 1713080391 \ No newline at end of file diff --git a/Runtime/Implementations/Object/ObjectCondition.cs b/Runtime/Implementations/Object/ObjectCondition.cs new file mode 100644 index 0000000..316b086 --- /dev/null +++ b/Runtime/Implementations/Object/ObjectCondition.cs @@ -0,0 +1,22 @@ +using System; +using UnityObject = UnityEngine.Object; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public abstract class ObjectCondition : SourceCondition + where TSource : UnityObject + { + public ObjectCondition(TSource source, TState state) : base(source, state) + { + } + } + + [Serializable] + public abstract class ObjectCondition : ObjectCondition + { + protected ObjectCondition(UnityObject source, TState state) : base(source, state) + { + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/Object/ObjectCondition.cs.meta b/Runtime/Implementations/Object/ObjectCondition.cs.meta new file mode 100644 index 0000000..82b7ee1 --- /dev/null +++ b/Runtime/Implementations/Object/ObjectCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1924f3f438a84c6bb306798215f01b0f +timeCreated: 1713078492 \ No newline at end of file diff --git a/Runtime/Implementations/PredicateCondition.cs b/Runtime/Implementations/PredicateCondition.cs new file mode 100644 index 0000000..22d53e5 --- /dev/null +++ b/Runtime/Implementations/PredicateCondition.cs @@ -0,0 +1,54 @@ +using System; + +namespace Better.Commons.Runtime.Conditions +{ + public class PredicateCondition : Condition + { + private readonly Action _rebuildAction; + private readonly Func _predicate; + + public PredicateCondition(Func predicate) + { + if (predicate == null) + { + throw new ArgumentNullException(nameof(predicate)); + } + + _predicate = predicate; + } + + public PredicateCondition(Action rebuildAction, Func predicate) : this(predicate) + { + if (rebuildAction == null) + { + throw new ArgumentNullException(nameof(rebuildAction)); + } + + _rebuildAction = rebuildAction; + } + + public override void Rebuild() + { + base.Rebuild(); + + _rebuildAction?.Invoke(); + } + + public override bool Invoke() + { + return _predicate.Invoke(); + } + + protected override bool Validate(out Exception exception) + { + if (_predicate == null) + { + exception = new NullReferenceException(nameof(_predicate)); + return false; + } + + exception = null; + return true; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/PredicateCondition.cs.meta b/Runtime/Implementations/PredicateCondition.cs.meta new file mode 100644 index 0000000..0c1568f --- /dev/null +++ b/Runtime/Implementations/PredicateCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 09e686130dc64697a8a37e0b3a21dee1 +timeCreated: 1708392505 \ No newline at end of file diff --git a/Runtime/Implementations/SourceCondition.cs b/Runtime/Implementations/SourceCondition.cs new file mode 100644 index 0000000..b0d9072 --- /dev/null +++ b/Runtime/Implementations/SourceCondition.cs @@ -0,0 +1,66 @@ +using System; +using Better.Commons.Runtime.Extensions; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public abstract class SourceCondition : Condition + { + [SerializeField] private TSource _source; + + protected TSource Source => _source; + + protected SourceCondition(TSource source) + { + if (typeof(TSource).IsNullable() && source == null) + { + throw new ArgumentNullException(nameof(source)); + } + + _source = source; + } + + protected override bool Validate(out Exception exception) + { + if (typeof(TSource).IsNullable() && Source == null) + { + exception = new NullReferenceException(nameof(_source)); + return false; + } + + exception = null; + return true; + } + } + + [Serializable] + public abstract class SourceCondition : StateCondition + { + [SerializeField] private TSource _source; + + protected TSource Source => _source; + + protected SourceCondition(TSource source, TState state) : base(state) + { + if (typeof(TSource).IsNullable() && source == null) + { + throw new ArgumentNullException(nameof(source)); + } + + _source = source; + } + + protected override bool Validate(out Exception exception) + { + if (typeof(TSource).IsNullable() && Source == null) + { + exception = new NullReferenceException(nameof(_source)); + return false; + } + + exception = null; + return true; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/SourceCondition.cs.meta b/Runtime/Implementations/SourceCondition.cs.meta new file mode 100644 index 0000000..67dc713 --- /dev/null +++ b/Runtime/Implementations/SourceCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 21559e25d77642978257d52466e7f7c8 +timeCreated: 1715484912 \ No newline at end of file diff --git a/Runtime/Implementations/State.meta b/Runtime/Implementations/State.meta new file mode 100644 index 0000000..1b31ace --- /dev/null +++ b/Runtime/Implementations/State.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ceb6349e00664be4bc0711a333f2ebb3 +timeCreated: 1715485873 \ No newline at end of file diff --git a/Runtime/Implementations/State/EqualityStateCondition.cs b/Runtime/Implementations/State/EqualityStateCondition.cs new file mode 100644 index 0000000..df7bcf6 --- /dev/null +++ b/Runtime/Implementations/State/EqualityStateCondition.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class EqualityStateCondition : StateCondition + { + public TState State { get; set; } + protected IEqualityComparer Comparer { get; private set; } + + public EqualityStateCondition(IEqualityComparer comparer, TState targetState, TState state = default) + : base(targetState) + { + if (comparer == null) + { + throw new ArgumentNullException(nameof(comparer)); + } + + Comparer = comparer; + State = state; + } + + public EqualityStateCondition(TState targetValue, TState state = default) + : this(EqualityComparer.Default, targetValue, state) + { + } + + protected EqualityStateCondition() : this(default) + { + } + + public override void Rebuild() + { + base.Rebuild(); + + if (Comparer == null) + { + Comparer = EqualityComparer.Default; + } + } + + public override bool Invoke() + { + if (Comparer == null) + { + return false; + } + + return Comparer.Equals(State, TargetState); + } + + protected override bool Validate(out Exception exception) + { + exception = null; + return true; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/State/EqualityStateCondition.cs.meta b/Runtime/Implementations/State/EqualityStateCondition.cs.meta new file mode 100644 index 0000000..45ed71f --- /dev/null +++ b/Runtime/Implementations/State/EqualityStateCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e1f6b18df0524c5c9d272af95e6e18c0 +timeCreated: 1708393702 \ No newline at end of file diff --git a/Runtime/Implementations/State/StateCondition.cs b/Runtime/Implementations/State/StateCondition.cs new file mode 100644 index 0000000..e15ad1a --- /dev/null +++ b/Runtime/Implementations/State/StateCondition.cs @@ -0,0 +1,36 @@ +using System; +using Better.Commons.Runtime.Extensions; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public abstract class StateCondition : Condition + { + [SerializeField] private TValue _targetState; + + protected TValue TargetState => _targetState; + + protected StateCondition(TValue targetState) + { + if (typeof(TValue).IsNullable() && targetState == null) + { + throw new ArgumentNullException(nameof(targetState)); + } + + _targetState = targetState; + } + + protected override bool Validate(out Exception exception) + { + if (typeof(TValue).IsNullable() && TargetState == null) + { + exception = new NullReferenceException(nameof(_targetState)); + return false; + } + + exception = null; + return true; + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/State/StateCondition.cs.meta b/Runtime/Implementations/State/StateCondition.cs.meta new file mode 100644 index 0000000..489048a --- /dev/null +++ b/Runtime/Implementations/State/StateCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4d10df779c3040c5a9c8bee806d602f8 +timeCreated: 1715484390 \ No newline at end of file diff --git a/Runtime/Implementations/State/TriggerStateCondition.cs b/Runtime/Implementations/State/TriggerStateCondition.cs new file mode 100644 index 0000000..95c6275 --- /dev/null +++ b/Runtime/Implementations/State/TriggerStateCondition.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace Better.Commons.Runtime.Conditions +{ + [Serializable] + public class TriggerStateCondition : EqualityStateCondition + { + [SerializeField] private TState _defaultState; + protected TState DefaultState => _defaultState; + + public TriggerStateCondition(IEqualityComparer comparer, TState targetState, TState defaultState = default) + : base(comparer, targetState, defaultState) + { + _defaultState = defaultState; + } + + public TriggerStateCondition(TState targetValue, TState defaultValue = default) + : this(EqualityComparer.Default, targetValue, defaultValue) + { + } + + protected TriggerStateCondition() : this(EqualityComparer.Default, default) + { + } + + public override void Rebuild() + { + base.Rebuild(); + + State = DefaultState; + } + } + + [Serializable] + public class TriggerStateCondition : TriggerStateCondition + { + public TriggerStateCondition() : base(true) + { + } + } +} \ No newline at end of file diff --git a/Runtime/Implementations/State/TriggerStateCondition.cs.meta b/Runtime/Implementations/State/TriggerStateCondition.cs.meta new file mode 100644 index 0000000..40ad9c8 --- /dev/null +++ b/Runtime/Implementations/State/TriggerStateCondition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: eb8969ca05d54bab90ed319a1d5faf5e +timeCreated: 1708394163 \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8ffe89e --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "com.tdw.better.conditions", + "displayName": "Better Conditions", + "version": "0.0.1", + "unity": "2021.3", + "description": " ", + "dependencies": { + "com.tdw.better.internal.core": "0.0.2", + "com.tdw.better.commons": "0.0.5", + "com.uurha.betterattributes": "3.1.3" + }, + "author": { + "name": "Better Plugins", + "url": "https://github.com/techno-dwarf-works" + }, + "changelogUrl": "https://github.com/techno-dwarf-works/better-conditions", + "documentationUrl": "https://github.com/techno-dwarf-works/better-conditions/tree/main#readme", + "license": "MIT", + "licensesUrl": "https://github.com/techno-dwarf-works/better-conditions/blob/main/LICENSE", + "keywords": [ + "condition", + "pattern" + ], + "samples": [ + { + "displayName": "Default samples", + "description": "Contains test samples", + "path": "Samples~/TestSamples" + } + ] +} diff --git a/package.json.meta b/package.json.meta new file mode 100644 index 0000000..a307beb --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2b50bde52714cdb45858050973eb5257 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: