Facebook
From Masny, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 147
  1. namespace CitizenFX.Core
  2. {
  3.     using CitizenFX.Core.Native;
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.     using System.Runtime.CompilerServices;
  8.     using System.Security;
  9.     using System.Threading;
  10.     using System.Threading.Tasks;
  11.  
  12.     public abstract class BaseScript
  13.     {
  14.         private Player m_player;
  15.  
  16.         [field: CompilerGenerated]
  17.         protected event Func<Task> Tick;
  18.  
  19.         protected BaseScript()
  20.         {
  21.             this.EventHandlers = new EventHandlerDictionary();
  22.             this.Exports = new ExportDictionary();
  23.             this.CurrentTaskList = new Dictionary<Delegate, Task>();
  24.             this.Players = new PlayerList();
  25.         }
  26.  
  27.         private static IAsyncResult BeginDelay(int delay, AsyncCallback callback, object state)
  28.         {
  29.             InternalManager.AddDelay(delay, callback);
  30.             return new DummyAsyncResult();
  31.         }
  32.  
  33.         public static Task Delay(int msecs)
  34.         {
  35.             return Task.Factory.FromAsync<int>(new Func<int, AsyncCallback, object, IAsyncResult>(BaseScript.BeginDelay), new Action<IAsyncResult>(BaseScript.EndDelay), msecs, null);
  36.         }
  37.  
  38.         private static void EndDelay(IAsyncResult result)
  39.         {
  40.         }
  41.  
  42.         internal void RegisterEventHandler(string eventName, Delegate callback)
  43.         {
  44.             EventHandlerDictionary eventHandlers = this.EventHandlers;
  45.             string str = eventName;
  46.             eventHandlers[str] += callback;
  47.         }
  48.  
  49.         public static void RegisterScript(BaseScript script)
  50.         {
  51.             InternalManager.AddScript(script);
  52.         }
  53.  
  54.         internal void RegisterTick(Func<Task> tick)
  55.         {
  56.             this.Tick += tick;
  57.         }
  58.  
  59.         internal void ScheduleRun()
  60.         {
  61.             if (this.Tick != null)
  62.             {
  63.                 foreach (Delegate delegate2 in this.Tick.GetInvocationList())
  64.                 {
  65.                     this.ScheduleTick(delegate2);
  66.                 }
  67.             }
  68.         }
  69.  
  70.         internal void ScheduleTick(Delegate call)
  71.         {
  72.             if (!this.CurrentTaskList.ContainsKey(call))
  73.             {
  74.                 this.CurrentTaskList.Add(call, Task.Factory.StartNew<Task>((Func<Task>) call).Unwrap().ContinueWith(delegate (Task a) {
  75.                     if (a.IsFaulted)
  76.                     {
  77.                         Debug.WriteLine("Failed to run a tick for " + this.GetType().Name + ": " + ((a.Exception == null) ? null : a.Exception.InnerExceptions).Aggregate<Exception, string>("", (b, s) => (s + b.ToString() + "\n")));
  78.                     }
  79.                     this.CurrentTaskList.Remove(call);
  80.                 }));
  81.             }
  82.         }
  83.  
  84.         [SecuritySafeCritical]
  85.         public static void TriggerEvent(string eventName, params object[] args)
  86.         {
  87.             byte[] argsSerialized = MsgPackSerializer.Serialize(args);
  88.             TriggerEventInternal(eventName, argsSerialized, false);
  89.         }
  90.  
  91.         [SecurityCritical]
  92.         private static unsafe void TriggerEventInternal(string eventName, byte[] argsSerialized, bool isRemote)
  93.         {
  94.             Hash hash = Hash.TRIGGER_EVENT_INTERNAL;
  95.             if (isRemote)
  96.             {
  97.                 hash = Hash.TRIGGER_SERVER_EVENT_INTERNAL;
  98.             }
  99.             fixed (byte* numRef = argsSerialized)
  100.             {
  101.                 byte* numPtr = numRef;
  102.                 InputArgument[] arguments = new InputArgument[] { eventName, numPtr, argsSerialized.Length };
  103.                 Function.Call(hash, arguments);
  104.             }
  105.         }
  106.  
  107.         [SecuritySafeCritical]
  108.         public static void TriggerServerEvent(string eventName, params object[] args)
  109.         {
  110.             byte[] argsSerialized = MsgPackSerializer.Serialize(args);
  111.             TriggerEventInternal(eventName, argsSerialized, true);
  112.         }
  113.  
  114.         public static void UnregisterScript(BaseScript script)
  115.         {
  116.             InternalManager.RemoveScript(script);
  117.         }
  118.  
  119.         private Dictionary<Delegate, Task> CurrentTaskList { get; set; }
  120.  
  121.         protected internal EventHandlerDictionary EventHandlers { get; private set; }
  122.  
  123.         protected ExportDictionary Exports { get; private set; }
  124.  
  125.         protected Player LocalPlayer
  126.         {
  127.             get
  128.             {
  129.                 int handle = API.PlayerId();
  130.                 if ((this.m_player == null) || (handle != this.m_player.Handle))
  131.                 {
  132.                     this.m_player = new Player(handle);
  133.                 }
  134.                 return this.m_player;
  135.             }
  136.         }
  137.  
  138.         protected PlayerList Players { get; private set; }
  139.  
  140.         [Serializable, CompilerGenerated]
  141.         private sealed class <>c
  142.         {
  143.             public static readonly BaseScript.<>c <>9 = new BaseScript.<>c();
  144.             public static Func<string, Exception, string> <>9__26_1;
  145.  
  146.             internal string <ScheduleTick>b__26_1(string b, Exception s)
  147.             {
  148.                 return (s + b.ToString() + "\n");
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154.