Advertisement
dimmpixeye

Untitled

May 16th, 2018
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. /*===============================================================
  2. Product:    Cryoshock
  3. Developer:  Dimitry Pixeye - pixeye@hbrew.store
  4. Company:    Homebrew - http://hbrew.store
  5. Date:       4/29/2018  4:53 PM
  6. ================================================================*/
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10.  
  11. namespace Homebrew
  12. {
  13.     [Serializable]
  14.     public class ProcessingSignals : IComponent
  15.     {
  16.         public static ProcessingSignals Default;
  17.         public readonly Dictionary<int, List<IRecieve>> signals = new Dictionary<int, List<IRecieve>>();
  18.  
  19.         #region LOGIC
  20.  
  21.         public void Send<T>(T val = default(T))
  22.         {
  23.             List<IRecieve> cachedSignals;
  24.  
  25.             if (!signals.TryGetValue(typeof(T).GetHashCode(), out cachedSignals)) return;
  26.  
  27.             var len = cachedSignals.Count;
  28.  
  29.             for (int i = 0; i < len; i++)
  30.             {
  31.                 (cachedSignals[i] as IRecieve<T>).HandleSignal(val);
  32.             }
  33.         }
  34.  
  35.         public void Add<T>(IRecieve recieve)
  36.         {
  37.             List<IRecieve> cachedSignals;
  38.             if (signals.TryGetValue(typeof(T).GetHashCode(), out cachedSignals))
  39.             {
  40.                 cachedSignals.Add(recieve);
  41.                 return;
  42.             }
  43.  
  44.             signals.Add(typeof(T).GetHashCode(), new List<IRecieve> {recieve});
  45.         }
  46.  
  47.         public void Remove<T>(IRecieve recieve)
  48.         {
  49.             List<IRecieve> cachedSignals;
  50.             Timer.Add(Time.DeltaTime, () =>
  51.             {
  52.                 if (signals.TryGetValue(typeof(T).GetHashCode(), out cachedSignals))
  53.                 {
  54.                     cachedSignals.Remove(recieve);
  55.                 }
  56.             });
  57.         }
  58.  
  59.  
  60.         public void Add(IRecieve recieve, Type type)
  61.         {
  62.             List<IRecieve> cachedSignals;
  63.             if (signals.TryGetValue(type.GetHashCode(), out cachedSignals))
  64.             {
  65.                 cachedSignals.Add(recieve);
  66.                 return;
  67.             }
  68.  
  69.             signals.Add(type.GetHashCode(), new List<IRecieve> {recieve});
  70.         }
  71.  
  72.         public void Remove(IRecieve recieve, Type type)
  73.         {
  74.             List<IRecieve> cachedSignals;
  75.             Timer.Add(Time.DeltaTime, () =>
  76.             {
  77.                 if (signals.TryGetValue(type.GetHashCode(), out cachedSignals))
  78.                 {
  79.                     cachedSignals.Remove(recieve);
  80.                 }
  81.             });
  82.         }
  83.  
  84.  
  85.         public void Add(object obj)
  86.         {
  87.             var reciever = obj as IRecieve;
  88.             if (reciever == null) return;
  89.  
  90.             var all = obj.GetType().GetInterfaces();
  91.  
  92.             foreach (var intType in all)
  93.             {
  94.                 if (intType.IsGenericType && intType.GetGenericTypeDefinition() == typeof(IRecieveGlobal<>))
  95.                 {
  96.                     Default.Add(reciever, intType.GetGenericArguments()[0]);
  97.                 }
  98.                 else if (intType.IsGenericType && intType.GetGenericTypeDefinition() == typeof(IRecieve<>))
  99.                 {
  100.              
  101.                     Add(reciever, intType.GetGenericArguments()[0]);
  102.                 }
  103.             }
  104.         }
  105.  
  106.         public void Remove(object obj)
  107.         {
  108.             var reciever = obj as IRecieve;
  109.             if (reciever == null) return;
  110.             var all = obj.GetType().GetInterfaces();
  111.  
  112.             foreach (Type intType in all)
  113.             {
  114.                 if (intType.IsGenericType && intType.GetGenericTypeDefinition() == typeof(IRecieveGlobal<>))
  115.                 {
  116.                     Default.Remove(reciever, intType.GetGenericArguments()[0]);
  117.                 }
  118.                 else if (intType.IsGenericType && intType.GetGenericTypeDefinition() == typeof(IRecieve<>))
  119.                 {
  120.                     Remove(reciever, intType.GetGenericArguments()[0]);
  121.                 }
  122.             }
  123.         }
  124.  
  125.         public void Dispose()
  126.         {
  127.             signals.Clear();
  128.         }
  129.  
  130.         #endregion
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement