diff --git a/src/Shared.AIalike/Chara/CharacterApi.Hooks.AI.cs b/src/Shared.AIalike/Chara/CharacterApi.Hooks.AI.cs index 9c7b86a..61060bb 100644 --- a/src/Shared.AIalike/Chara/CharacterApi.Hooks.AI.cs +++ b/src/Shared.AIalike/Chara/CharacterApi.Hooks.AI.cs @@ -1,12 +1,12 @@ -using AIChara; +using ADV; +using AIChara; +using ExtensibleSaveFormat; +using HarmonyLib; using KKAPI.Maker; using System; using System.Collections; using System.Diagnostics; using System.Linq; -using ADV; -using HarmonyLib; -using ExtensibleSaveFormat; namespace KKAPI.Chara { @@ -58,12 +58,12 @@ private static void OnCopyChaFile(ChaFile destination, ChaFile source) { foreach (var handler in _registeredHandlers) { - if (handler.ExtendedDataCopier == null) + if (handler.Value.ExtendedDataCopier == null) continue; try { - handler.ExtendedDataCopier(destination, source); + handler.Value.ExtendedDataCopier(destination, source); } catch (Exception e) { diff --git a/src/Shared.Core/Chara/CharacterApi.cs b/src/Shared.Core/Chara/CharacterApi.cs index 4d496f0..66e4655 100644 --- a/src/Shared.Core/Chara/CharacterApi.cs +++ b/src/Shared.Core/Chara/CharacterApi.cs @@ -1,11 +1,11 @@ -using KKAPI.Maker; +using ExtensibleSaveFormat; +using KKAPI.Maker; +using KKAPI.Utilities; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using ExtensibleSaveFormat; -using KKAPI.Utilities; using UniRx; using UniRx.Triggers; using UnityEngine; @@ -24,12 +24,12 @@ public static partial class CharacterApi { internal static readonly HashSet ChaControls = new HashSet(); - private static readonly List _registeredHandlers = new List(); + private readonly static SortedList _registeredHandlers = new SortedList(); /// /// All currently registered kinds of controllers. /// - public static IEnumerable RegisteredHandlers => _registeredHandlers.AsReadOnly(); + public static IEnumerable RegisteredHandlers => _registeredHandlers.Values; /// /// Override to supply custom extended data copying logic. @@ -53,7 +53,7 @@ public static ChaControl FileControlToChaControl(ChaFileControl fileControl) public static IEnumerable GetBehaviours(ChaControl character = null) { if (character == null) - return _registeredHandlers.SelectMany(x => x.Instances); + return _registeredHandlers.SelectMany(x => x.Value.Instances); return character.GetComponents(); } @@ -63,7 +63,7 @@ public static IEnumerable GetBehaviours(ChaContro /// public static ControllerRegistration GetRegisteredBehaviour(string extendedDataId) { - return _registeredHandlers.Find(registration => string.Equals(registration.ExtendedDataId, extendedDataId, StringComparison.Ordinal)); + return _registeredHandlers.Values.First(registration => string.Equals(registration.ExtendedDataId, extendedDataId, StringComparison.Ordinal)); } /// @@ -71,7 +71,7 @@ public static ControllerRegistration GetRegisteredBehaviour(string extendedDataI /// public static ControllerRegistration GetRegisteredBehaviour(Type controllerType) { - return _registeredHandlers.Find(registration => registration.ControllerType == controllerType); + return _registeredHandlers.Values.First(registration => registration.ControllerType == controllerType); } /// @@ -79,7 +79,7 @@ public static ControllerRegistration GetRegisteredBehaviour(Type controllerType) /// public static ControllerRegistration GetRegisteredBehaviour(Type controllerType, string extendedDataId) { - return _registeredHandlers.Find(registration => registration.ControllerType == controllerType && string.Equals(registration.ExtendedDataId, extendedDataId, StringComparison.Ordinal)); + return _registeredHandlers.Values.First(registration => registration.ControllerType == controllerType && string.Equals(registration.ExtendedDataId, extendedDataId, StringComparison.Ordinal)); } /// @@ -91,6 +91,20 @@ public static ControllerRegistration GetRegisteredBehaviour(Type controllerType, /// Type with your custom logic to add to a character /// Extended data ID used by this behaviour. Set to null if not used. Needed to copy the data in some situations. public static void RegisterExtraBehaviour(string extendedDataId) where T : CharaCustomFunctionController, new() + { + RegisterExtraBehaviour(extendedDataId, 1000); + } + + /// + /// Register new functionality that will be automatically added to all characters (where applicable). + /// Offers easy API for saving and loading extended data, and for running logic to apply it to the characters. + /// All necessary hooking and event subscribing is done for you. All you have to do is create a type + /// that inherits from CharaExtraBehaviour (don't make instances, the API will make them for you). + /// + /// Type with your custom logic to add to a character + /// Extended data ID used by this behaviour. Set to null if not used. Needed to copy the data in some situations. + /// Default value is 1000, Decrease to increase priority or vice versa such on or related functions. + public static void RegisterExtraBehaviour(string extendedDataId, int priority) where T : CharaCustomFunctionController, new() { void BasicCopier(ChaFile dst, ChaFile src) { @@ -100,9 +114,10 @@ void BasicCopier(ChaFile dst, ChaFile src) var copier = extendedDataId == null ? (CopyExtendedDataFunc)null : BasicCopier; - RegisterExtraBehaviour(extendedDataId, copier); + RegisterExtraBehaviour(extendedDataId, copier, priority); } + /// /// Register new functionality that will be automatically added to all characters (where applicable). /// Offers easy API for saving and loading extended data, and for running logic to apply it to the characters. @@ -114,7 +129,27 @@ void BasicCopier(ChaFile dst, ChaFile src) /// Override default extended data copy logic public static void RegisterExtraBehaviour(string extendedDataId, CopyExtendedDataFunc customDataCopier) where T : CharaCustomFunctionController, new() { - _registeredHandlers.Add(new ControllerRegistration(typeof(T), extendedDataId, customDataCopier)); + RegisterExtraBehaviour(extendedDataId, customDataCopier, 1000); + } + + + /// + /// Register new functionality that will be automatically added to all characters (where applicable). + /// Offers easy API for saving and loading extended data, and for running logic to apply it to the characters. + /// All necessary hooking and event subscribing is done for you. All you have to do is create a type + /// that inherits from CharaExtraBehaviour (don't make instances, the API will make them for you). + /// + /// Type with your custom logic to add to a character + /// Extended data ID used by this behaviour. Set to null if not used. + /// Override default extended data copy logic + /// Default value is 1000, Decrease to increase priority or vice versa such on or related functions. + public static void RegisterExtraBehaviour(string extendedDataId, CopyExtendedDataFunc customDataCopier, int priority) where T : CharaCustomFunctionController, new() + { + while (_registeredHandlers.ContainsKey(priority)) + { + priority++; + } + _registeredHandlers.Add(priority, new ControllerRegistration(typeof(T), extendedDataId, customDataCopier)); } internal static void Init() @@ -188,15 +223,15 @@ private static void CreateOrAddBehaviours(ChaControl target) { foreach (var handler in _registeredHandlers) { - var existing = target.gameObject.GetComponents(handler.ControllerType) + var existing = target.gameObject.GetComponents(handler.Value.ControllerType) .Cast() - .FirstOrDefault(x => x.ExtendedDataId == handler.ExtendedDataId); + .FirstOrDefault(x => x.ExtendedDataId == handler.Value.ExtendedDataId); if (existing == null) { try { - handler.CreateInstance(target); + handler.Value.CreateInstance(target); } catch (Exception e) { diff --git a/src/Shared.KKalike/Chara/CharacterApi.Hooks.KK.cs b/src/Shared.KKalike/Chara/CharacterApi.Hooks.KK.cs index 131bcd3..3f3b405 100644 --- a/src/Shared.KKalike/Chara/CharacterApi.Hooks.KK.cs +++ b/src/Shared.KKalike/Chara/CharacterApi.Hooks.KK.cs @@ -1,4 +1,6 @@ -using ChaCustom; +using ADV; +using ChaCustom; +using HarmonyLib; using KKAPI.Maker; using System; using System.Collections; @@ -6,8 +8,6 @@ using System.Linq; using System.Reflection; using System.Reflection.Emit; -using ADV; -using HarmonyLib; namespace KKAPI.Chara { @@ -87,12 +87,12 @@ private static void OnCopyChaFile(ChaFile destination, ChaFile source) { foreach (var handler in _registeredHandlers) { - if (handler.ExtendedDataCopier == null) + if (handler.Value.ExtendedDataCopier == null) continue; try { - handler.ExtendedDataCopier(destination, source); + handler.Value.ExtendedDataCopier(destination, source); } catch (Exception e) { @@ -209,5 +209,5 @@ public static void clothesFileControl_InitializePostHook() ClothesFileControlLoading = false; } } - } + } }