Skip to content

Commit

Permalink
Add priority parameter to CharacterApi.RegisterExtraBehaviour (#32)
Browse files Browse the repository at this point in the history
* Priority functionality

* Update CharacterApi.cs

Removed defaulting
removed to list as already readonly

* Update CharacterApi.cs

Re-Added indication of Default value (for comparison) and changed Capitalization of priority
  • Loading branch information
jalil49 committed May 24, 2021
1 parent 6f68772 commit 545a450
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
12 changes: 6 additions & 6 deletions 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
{
Expand Down Expand Up @@ -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)
{
Expand Down
63 changes: 49 additions & 14 deletions 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;
Expand All @@ -24,12 +24,12 @@ public static partial class CharacterApi
{
internal static readonly HashSet<ChaControl> ChaControls = new HashSet<ChaControl>();

private static readonly List<ControllerRegistration> _registeredHandlers = new List<ControllerRegistration>();
private readonly static SortedList<int, ControllerRegistration> _registeredHandlers = new SortedList<int, ControllerRegistration>();

/// <summary>
/// All currently registered kinds of <see cref="CharaCustomFunctionController"/> controllers.
/// </summary>
public static IEnumerable<ControllerRegistration> RegisteredHandlers => _registeredHandlers.AsReadOnly();
public static IEnumerable<ControllerRegistration> RegisteredHandlers => _registeredHandlers.Values;

/// <summary>
/// Override to supply custom extended data copying logic.
Expand All @@ -53,7 +53,7 @@ public static ChaControl FileControlToChaControl(ChaFileControl fileControl)
public static IEnumerable<CharaCustomFunctionController> GetBehaviours(ChaControl character = null)
{
if (character == null)
return _registeredHandlers.SelectMany(x => x.Instances);
return _registeredHandlers.SelectMany(x => x.Value.Instances);

return character.GetComponents<CharaCustomFunctionController>();
}
Expand All @@ -63,23 +63,23 @@ public static IEnumerable<CharaCustomFunctionController> GetBehaviours(ChaContro
/// </summary>
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));
}

/// <summary>
/// Get the first controller of the specified type that was registered. The type has to be an exact match.
/// </summary>
public static ControllerRegistration GetRegisteredBehaviour(Type controllerType)
{
return _registeredHandlers.Find(registration => registration.ControllerType == controllerType);
return _registeredHandlers.Values.First(registration => registration.ControllerType == controllerType);
}

/// <summary>
/// Get the first controller of the specified type that was registered with the specified extendedDataId. The type has to be an exact match.
/// </summary>
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));
}

/// <summary>
Expand All @@ -91,6 +91,20 @@ public static ControllerRegistration GetRegisteredBehaviour(Type controllerType,
/// <typeparam name="T">Type with your custom logic to add to a character</typeparam>
/// <param name="extendedDataId">Extended data ID used by this behaviour. Set to null if not used. Needed to copy the data in some situations.</param>
public static void RegisterExtraBehaviour<T>(string extendedDataId) where T : CharaCustomFunctionController, new()
{
RegisterExtraBehaviour<T>(extendedDataId, 1000);
}

/// <summary>
/// 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 <code>CharaExtraBehaviour</code> (don't make instances, the API will make them for you).
/// </summary>
/// <typeparam name="T">Type with your custom logic to add to a character</typeparam>
/// <param name="extendedDataId">Extended data ID used by this behaviour. Set to null if not used. Needed to copy the data in some situations.</param>
/// <param name="priority">Default value is 1000, Decrease to increase priority or vice versa such on <see cref="CharaCustomFunctionController.OnReload(GameMode)"/> or related functions.</param>
public static void RegisterExtraBehaviour<T>(string extendedDataId, int priority) where T : CharaCustomFunctionController, new()
{
void BasicCopier(ChaFile dst, ChaFile src)
{
Expand All @@ -100,9 +114,10 @@ void BasicCopier(ChaFile dst, ChaFile src)

var copier = extendedDataId == null ? (CopyExtendedDataFunc)null : BasicCopier;

RegisterExtraBehaviour<T>(extendedDataId, copier);
RegisterExtraBehaviour<T>(extendedDataId, copier, priority);
}


/// <summary>
/// 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.
Expand All @@ -114,7 +129,27 @@ void BasicCopier(ChaFile dst, ChaFile src)
/// <param name="customDataCopier">Override default extended data copy logic</param>
public static void RegisterExtraBehaviour<T>(string extendedDataId, CopyExtendedDataFunc customDataCopier) where T : CharaCustomFunctionController, new()
{
_registeredHandlers.Add(new ControllerRegistration(typeof(T), extendedDataId, customDataCopier));
RegisterExtraBehaviour<T>(extendedDataId, customDataCopier, 1000);
}


/// <summary>
/// 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 <code>CharaExtraBehaviour</code> (don't make instances, the API will make them for you).
/// </summary>
/// <typeparam name="T">Type with your custom logic to add to a character</typeparam>
/// <param name="extendedDataId">Extended data ID used by this behaviour. Set to null if not used.</param>
/// <param name="customDataCopier">Override default extended data copy logic</param>
/// <param name="priority">Default value is 1000, Decrease to increase priority or vice versa such on <see cref="CharaCustomFunctionController.OnReload(GameMode)"/> or related functions.</param>
public static void RegisterExtraBehaviour<T>(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()
Expand Down Expand Up @@ -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<CharaCustomFunctionController>()
.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)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Shared.KKalike/Chara/CharacterApi.Hooks.KK.cs
@@ -1,13 +1,13 @@
using ChaCustom;
using ADV;
using ChaCustom;
using HarmonyLib;
using KKAPI.Maker;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using ADV;
using HarmonyLib;

namespace KKAPI.Chara
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -209,5 +209,5 @@ public static void clothesFileControl_InitializePostHook()
ClothesFileControlLoading = false;
}
}
}
}
}

0 comments on commit 545a450

Please sign in to comment.