ICYHOTDDOY

Untitled

Jul 13th, 2026
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace InventoryModule.Executables
  6. {
  7.     internal class CustomExecutable
  8.     {
  9.         public static Dictionary<string, object> Methods = new Dictionary<string, object>();
  10.  
  11.         [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  12.         private static void ResetStatics()
  13.         {
  14.             Methods.Clear();
  15.         }
  16.  
  17.         private static string GenerateKey(string name, params Type[] types)
  18.         {
  19.             if (types.Length == 0) return name;
  20.  
  21.             var sb = new System.Text.StringBuilder(name);
  22.             foreach (var t in types)
  23.             {
  24.                 sb.Append('_');
  25.                 sb.Append(t.Name);
  26.             }
  27.             return sb.ToString();
  28.         }
  29.  
  30.         public static void AddExecutable(string name, Action Executable)
  31.         {
  32.             //generate a UniqueKey
  33.             var generatedKey = GenerateKey(name);
  34.             if (Executable is not null)
  35.             {
  36.                 Methods[generatedKey] = Executable;
  37.             }
  38.         }
  39.  
  40.         //Inputs must be of Type Delegate
  41.         public static void AddExecutable<T1>(string name, Action<T1> Executable)
  42.         {
  43.             if (Executable is not null)
  44.             {
  45.                 Methods[name] = Executable;
  46.             }
  47.  
  48.         }
  49.         public static void AddExecutable<T1, T2>(string name, Action<T1, T2> Executable)
  50.         {
  51.             var generatedKey = GenerateKey(name);
  52.             if (Executable is not null)
  53.             {
  54.                 Methods[name] = Executable;
  55.             }
  56.         }
  57.         public static void AddExecutable<T1, T2, T3>(string name, Action<T1, T2, T3> Executable)
  58.         {
  59.             if (Executable is not null)
  60.             {
  61.                 Methods[name] = Executable;
  62.             }
  63.         }
  64.  
  65.         public static void Execute(string name)
  66.         {
  67.             if (Methods.TryGetValue(GenerateKey(name), out var handler) && handler is Action action)
  68.                 action();
  69.             else
  70.                 Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
  71.         }
  72.  
  73.         public static void Execute<T1>(string name, T1 arg)
  74.         {
  75.             if (Methods.TryGetValue(GenerateKey(name, typeof(T1)), out var handler) && handler is Action<T1> action)
  76.                 action(arg);
  77.             else
  78.                 Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
  79.         }
  80.  
  81.         public static void Execute<T1, T2>(string name, T1 arg1, T2 arg2)
  82.         {
  83.             if (Methods.TryGetValue(GenerateKey(name, typeof(T1), typeof(T2)), out var handler) && handler is Action<T1, T2> action)
  84.                 action(arg1, arg2);
  85.             else
  86.                 Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
  87.         }
  88.  
  89.         public static void Execute<T1, T2, T3>(string name, T1 arg1, T2 arg2, T3 arg3)
  90.         {
  91.             if (Methods.TryGetValue(GenerateKey(name, typeof(T1), typeof(T2), typeof(T3)), out var handler) && handler is Action<T1, T2, T3> action)
  92.                 action(arg1, arg2, arg3);
  93.             else
  94.                 Debug.LogWarning($"[CustomExecutable] '{name}' not found or wrong signature.");
  95.         }
  96.  
  97.  
  98.     }
  99. }
  100.  
Tags: C#
Advertisement
Add Comment
Please, Sign In to add comment