Advertisement
Guest User

RandomCallouts.cs

a guest
Apr 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System.Reflection;
  2. using LSPD_First_Response.Mod.API;
  3. using LSPD_First_Response.Mod.Callouts;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Rage;
  8.  
  9. namespace ForceACallout.Utils
  10. {
  11.     internal static class RandomCallouts
  12.     {
  13.         internal static List<string> RandomCalloutCache = new List<string>();
  14.         internal static int[] CalloutProbabilityRegistrationCount =
  15.         {
  16.             95, 80, 70, 50, 30, 15, 0
  17.         };
  18.  
  19.         static RandomCallouts()
  20.         {
  21.             GameFiber.StartNew(delegate
  22.             {
  23.                 GameFiber.WaitWhile(() => !Globals.Application.SettingsLoaded);
  24.                 CacheCallouts();
  25.             }, "CalloutCacheLoader");
  26.         }
  27.  
  28.         internal static void CacheCallouts()
  29.         {
  30.             foreach (Assembly Assem in Functions.GetAllUserPlugins())
  31.             {
  32.                 AssemblyName AssemName = Assem.GetName();
  33.                 List<Type> AssemCallouts = (from Callout in Assem.GetTypes()
  34.                                             where Callout.IsClass && Callout.BaseType == typeof(LSPD_First_Response.Mod.Callouts.Callout)
  35.                                             select Callout).ToList();
  36.  
  37.                 if (AssemCallouts.Count() < 1)
  38.                 {
  39.                     Logger.Log(Assem.GetName().Name + " No callouts detected.");
  40.                 }
  41.  
  42.                 else
  43.                 {
  44.                     int AddCount = 0;
  45.                     foreach (Type Callout in AssemCallouts)
  46.                     {
  47.                         object[] CalloutAttributes = Callout.GetCustomAttributes(typeof(CalloutInfoAttribute), true);
  48.  
  49.                         if (CalloutAttributes.Count() > 0)
  50.                         {
  51.                             CalloutInfoAttribute CalloutAttribute = (CalloutInfoAttribute)(from a in CalloutAttributes select a).FirstOrDefault();
  52.  
  53.                             if (CalloutAttribute != null)
  54.                             {
  55.                                 if (Globals.Config.CalloutProbability == false)
  56.                                     RandomCalloutCache.Add(CalloutAttribute.Name);
  57.  
  58.                                 else
  59.                                 {
  60.                                     for (int LoopCount = 0; LoopCount < CalloutProbabilityRegistrationCount[(int)CalloutAttribute.CalloutProbability] * Globals.Config.CalloutProbabilityModifier; LoopCount++)
  61.                                     {
  62.                                         RandomCalloutCache.Add(CalloutAttribute.Name);
  63.                                     }
  64.                                 }
  65.                                 AddCount++;
  66.                             }
  67.                         }
  68.                     }
  69.  
  70.                     Logger.Log(Assem.GetName().Name + $" detected {AddCount} callouts and added them to the ForceACallout cache.");
  71.                 }
  72.             }
  73.  
  74.             if (Globals.Config.CalloutProbability == true)
  75.             {
  76.                 Logger.Log($"{RandomCalloutCache.Count} total probabilities registered in ForceACallout.");
  77.             }
  78.         }
  79.  
  80.         internal static string StartRandomCallout()
  81.         {
  82.             Logger.DebugLog("StartRandomCallout() Started");
  83.             Random RandomValue = new Random();
  84.  
  85.             try
  86.             {
  87.                 string RandomCallout = RandomCalloutCache[RandomValue.Next(0, RandomCalloutCache.Count)];
  88.  
  89.                 Functions.StartCallout(RandomCallout);
  90.                 Logger.Log($"Starting callout {RandomCallout}");
  91.                 return RandomCallout;
  92.             }
  93.  
  94.             catch
  95.             {
  96.                 Logger.Log("Could not start callout!");
  97.                 return null;
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement