From f63bc029c4e32a618c78ddd344de192fa4fad510 Mon Sep 17 00:00:00 2001 From: uurha Date: Sat, 10 Feb 2024 17:06:13 +0000 Subject: [PATCH] Update README.md and LICENSE --- Editor.meta | 8 +++ Editor/BetterServices.Editor.asmdef | 18 ++++++ Editor/BetterServices.Editor.asmdef.meta | 7 +++ LICENSE | 21 +++++++ README.md | 5 ++ Runtime.meta | 8 +++ Runtime/BetterServices.Runtime.asmdef | 17 ++++++ Runtime/BetterServices.Runtime.asmdef.meta | 7 +++ Runtime/Interfaces.meta | 8 +++ Runtime/Interfaces/IService.cs | 14 +++++ Runtime/Interfaces/IService.cs.meta | 11 ++++ Runtime/Services.meta | 8 +++ Runtime/Services/MonoService.cs | 67 +++++++++++++++++++++ Runtime/Services/MonoService.cs.meta | 11 ++++ Runtime/Services/PocoService.cs | 68 ++++++++++++++++++++++ Runtime/Services/PocoService.cs.meta | 11 ++++ package.json | 31 ++++++++++ package.json.meta | 7 +++ 18 files changed, 327 insertions(+) create mode 100644 Editor.meta create mode 100644 Editor/BetterServices.Editor.asmdef create mode 100644 Editor/BetterServices.Editor.asmdef.meta create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Runtime.meta create mode 100644 Runtime/BetterServices.Runtime.asmdef create mode 100644 Runtime/BetterServices.Runtime.asmdef.meta create mode 100644 Runtime/Interfaces.meta create mode 100644 Runtime/Interfaces/IService.cs create mode 100644 Runtime/Interfaces/IService.cs.meta create mode 100644 Runtime/Services.meta create mode 100644 Runtime/Services/MonoService.cs create mode 100644 Runtime/Services/MonoService.cs.meta create mode 100644 Runtime/Services/PocoService.cs create mode 100644 Runtime/Services/PocoService.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..5d61e86 --- /dev/null +++ b/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a33ab10bacb9667439900aec355ddde6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/BetterServices.Editor.asmdef b/Editor/BetterServices.Editor.asmdef new file mode 100644 index 0000000..35290fe --- /dev/null +++ b/Editor/BetterServices.Editor.asmdef @@ -0,0 +1,18 @@ +{ + "name": "BetterServices.Editor", + "rootNamespace": "Better.Services", + "references": [ + "GUID:1c5574afb40e5fe4ca9e9c156009297b" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Editor/BetterServices.Editor.asmdef.meta b/Editor/BetterServices.Editor.asmdef.meta new file mode 100644 index 0000000..3567095 --- /dev/null +++ b/Editor/BetterServices.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: db20c11e55ff43e4fb64a8f16d7f8494 +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..d0f6543 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Better Services +This plugins provides basic implementation for services pattern + +## 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..48da5e7 --- /dev/null +++ b/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6a7ac1d17fbdcc247b01df0305b3780b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/BetterServices.Runtime.asmdef b/Runtime/BetterServices.Runtime.asmdef new file mode 100644 index 0000000..9e4e061 --- /dev/null +++ b/Runtime/BetterServices.Runtime.asmdef @@ -0,0 +1,17 @@ +{ + "name": "BetterServices.Runtime", + "rootNamespace": "Better.Services", + "references": [ + "GUID:441b78e90a9cf724ab41c6eed8c0b93d", + "GUID:a59e3daedde9ca94bba45364d4ead25f" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Runtime/BetterServices.Runtime.asmdef.meta b/Runtime/BetterServices.Runtime.asmdef.meta new file mode 100644 index 0000000..a93d2f0 --- /dev/null +++ b/Runtime/BetterServices.Runtime.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1c5574afb40e5fe4ca9e9c156009297b +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Interfaces.meta b/Runtime/Interfaces.meta new file mode 100644 index 0000000..c22e6d4 --- /dev/null +++ b/Runtime/Interfaces.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a9acbc7e45c2562418038eca7dfa2585 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Interfaces/IService.cs b/Runtime/Interfaces/IService.cs new file mode 100644 index 0000000..d489ea0 --- /dev/null +++ b/Runtime/Interfaces/IService.cs @@ -0,0 +1,14 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace Better.Services.Runtime.Interfaces +{ + public interface IService + { + public bool Initialized { get; } + + public Task InitializeAsync(CancellationToken cancellationToken); + + public Task PostInitializeAsync(CancellationToken cancellationToken); + } +} \ No newline at end of file diff --git a/Runtime/Interfaces/IService.cs.meta b/Runtime/Interfaces/IService.cs.meta new file mode 100644 index 0000000..165b102 --- /dev/null +++ b/Runtime/Interfaces/IService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a60b5c1fc833c3043a051256e47970fd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Services.meta b/Runtime/Services.meta new file mode 100644 index 0000000..6602949 --- /dev/null +++ b/Runtime/Services.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7133eb9baf5855b468b9c7d86f4521d0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Services/MonoService.cs b/Runtime/Services/MonoService.cs new file mode 100644 index 0000000..50daf82 --- /dev/null +++ b/Runtime/Services/MonoService.cs @@ -0,0 +1,67 @@ +using System.Threading; +using System.Threading.Tasks; +using Better.Services.Runtime.Interfaces; +using UnityEngine; + +namespace Better.Services.Runtime +{ + public abstract class MonoService : MonoBehaviour, IService + { + // TODO: Add version dependency + private CancellationTokenSource _destroyCancellationToken; + + public bool Initialized { get; private set; } + protected CancellationToken DestroyCancellationToken => _destroyCancellationToken.Token; + + protected virtual void Awake() + { + _destroyCancellationToken = new(); + } + + async Task IService.InitializeAsync(CancellationToken cancellationToken) + { + Debug.Log($"[{GetType().Name}] {nameof(IService.InitializeAsync)}"); + + if (Initialized) + { + Debug.LogError($"[{GetType().Name}] {nameof(IService.InitializeAsync)}: already initialized"); + return; + } + + var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(DestroyCancellationToken, cancellationToken); + cancellationToken = linkedTokenSource.Token; + + await OnInitializeAsync(cancellationToken); + if (cancellationToken.IsCancellationRequested) + { + return; + } + + Initialized = true; + } + + Task IService.PostInitializeAsync(CancellationToken cancellationToken) + { + if (!Initialized) + { + Debug.LogError($"[{GetType().Name}] {nameof(IService.PostInitializeAsync)}: not initialized"); + return Task.CompletedTask; + } + + Debug.Log($"[{GetType().Name}] {nameof(IService.PostInitializeAsync)}"); + + var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(DestroyCancellationToken, cancellationToken); + cancellationToken = linkedTokenSource.Token; + + return OnPostInitializeAsync(cancellationToken); + } + + protected abstract Task OnInitializeAsync(CancellationToken cancellationToken); + protected abstract Task OnPostInitializeAsync(CancellationToken cancellationToken); + + protected virtual void OnDestroy() + { + _destroyCancellationToken?.Cancel(); + } + } +} \ No newline at end of file diff --git a/Runtime/Services/MonoService.cs.meta b/Runtime/Services/MonoService.cs.meta new file mode 100644 index 0000000..b5f1d82 --- /dev/null +++ b/Runtime/Services/MonoService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 02845d575aa8fb846bb2f5fcb5e1ab39 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Services/PocoService.cs b/Runtime/Services/PocoService.cs new file mode 100644 index 0000000..6b55544 --- /dev/null +++ b/Runtime/Services/PocoService.cs @@ -0,0 +1,68 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Better.Services.Runtime.Interfaces; +using Better.Validation.Runtime.Attributes; +using UnityEngine; + +namespace Better.Services.Runtime +{ + [Serializable] + public abstract class PocoService : IService + { + public bool Initialized { get; private set; } + + async Task IService.InitializeAsync(CancellationToken cancellationToken) + { + Debug.Log($"[{GetType().Name}] {nameof(IService.InitializeAsync)}"); + + if (Initialized) + { + Debug.LogError($"[{GetType().Name}] {nameof(IService.InitializeAsync)}: already initialized"); + return; + } + + Initialized = true; + await OnInitializeAsync(cancellationToken); + Initialized = !cancellationToken.IsCancellationRequested; + } + + Task IService.PostInitializeAsync(CancellationToken cancellationToken) + { + if (!Initialized) + { + Debug.LogError($"[{GetType().Name}] {nameof(IService.PostInitializeAsync)}: not initialized"); + return Task.CompletedTask; + } + + Debug.Log($"[{GetType().Name}] {nameof(IService.PostInitializeAsync)}"); + return OnPostInitializeAsync(cancellationToken); + } + + protected abstract Task OnInitializeAsync(CancellationToken cancellationToken); + protected abstract Task OnPostInitializeAsync(CancellationToken cancellationToken); + } + + [Serializable] + public abstract class PocoService : PocoService where TSettings : ScriptableObject + { + [NotNull] [SerializeField] private TSettings _settings; + + protected TSettings Settings => _settings; + + protected override Task OnInitializeAsync(CancellationToken cancellationToken) + { + if (Settings == null) + { + throw new NullReferenceException($"[{GetType().Name}] {nameof(OnInitializeAsync)}: {nameof(Settings)} cannot be null"); + } + + return Task.CompletedTask; + } + + protected override Task OnPostInitializeAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/Runtime/Services/PocoService.cs.meta b/Runtime/Services/PocoService.cs.meta new file mode 100644 index 0000000..c984a0b --- /dev/null +++ b/Runtime/Services/PocoService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 108a89f48728abf4ba2f9b6ab88d97b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json new file mode 100644 index 0000000..ce69706 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "com.tdw.betterservices", + "displayName": "Better Services", + "version": "0.0.1", + "unity": "2021.3", + "description": " ", + "dependencies": { + "com.uurha.bettervalidation": "1.0.55" + }, + "author": { + "name": "Techno Dwarf Works", + "url": "https://github.com/techno-dwarf-works" + }, + "changelogUrl": "https://github.com/techno-dwarf-works/better-services", + "documentationUrl": "https://github.com/techno-dwarf-works/better-services/tree/main#readme", + "license": "MIT", + "licensesUrl":"https://github.com/techno-dwarf-works/better-services/blob/main/LICENSE", + "keywords": [ + "better", + "services", + "reference managment", + "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..1a05263 --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 7192e15369bf3144bb253d8ea45a4b85 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: