Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace InventoryModule.Executables
- {
- internal class CustomExecutable
- {
- public static Dictionary<string, object> Methods = new Dictionary<string, object>();
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
- private static void ResetStatics()
- {
- Methods.Clear();
- }
- private static string GenerateKey(string name, params Type[] types)
- {
- if (types.Length == 0) return name;
- var sb = new System.Text.StringBuilder(name);
- foreach (var t in types)
- {
- sb.Append('_');
- sb.Append(t.Name);
- }
- return sb.ToString();
- }
- public static void AddExecutable(string name, Action Executable)
- {
- //generate a UniqueKey
- var generatedKey = GenerateKey(name);
- if (Executable is not null)
- {
- Methods[generatedKey] = Executable;
- }
- }
- //Inputs must be of Type Delegate
- public static void AddExecutable<T1>(string name, Action<T1> Executable)
- {
- if (Executable is not null)
- {
- Methods[name] = Executable;
- }
- }
- public static void AddExecutable<T1, T2>(string name, Action<T1, T2> Executable)
- {
- var generatedKey = GenerateKey(name);
- if (Executable is not null)
- {
- Methods[name] = Executable;
- }
- }
- public static void AddExecutable<T1, T2, T3>(string name, Action<T1, T2, T3> Executable)
- {
- if (Executable is not null)
- {
- Methods[name] = Executable;
- }
- }
- public static void Execute(string name)
- {
- if (Methods.TryGetValue(GenerateKey(name), out var handler) && handler is Action action)
- action();
- else
- Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
- }
- public static void Execute<T1>(string name, T1 arg)
- {
- if (Methods.TryGetValue(GenerateKey(name, typeof(T1)), out var handler) && handler is Action<T1> action)
- action(arg);
- else
- Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
- }
- public static void Execute<T1, T2>(string name, T1 arg1, T2 arg2)
- {
- if (Methods.TryGetValue(GenerateKey(name, typeof(T1), typeof(T2)), out var handler) && handler is Action<T1, T2> action)
- action(arg1, arg2);
- else
- Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
- }
- public static void Execute<T1, T2, T3>(string name, T1 arg1, T2 arg2, T3 arg3)
- {
- if (Methods.TryGetValue(GenerateKey(name, typeof(T1), typeof(T2), typeof(T3)), out var handler) && handler is Action<T1, T2, T3> action)
- action(arg1, arg2, arg3);
- else
- Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment