-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRainWorldCE.cs
648 lines (599 loc) · 24.1 KB
/
RainWorldCE.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
using BepInEx;
using BepInEx.Logging;
using RainWorldCE.Attributes;
using RainWorldCE.Config;
using RainWorldCE.Config.CustomChaos;
using RainWorldCE.Events;
using RainWorldCE.PostProcessing;
using RainWorldCE.RWHUD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using UnityEngine;
using Random = System.Random;
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
namespace RainWorldCE;
[BepInPlugin(MOD_ID, "Rain World Chaos Edition", "2.5.4")]
public class RainWorldCE : BaseUnityPlugin
{
public const string MOD_ID = "Gamer025.RainworldCE";
/// <summary>
/// Time in seconds since game start
/// </summary>
static public int gameTimer = 0;
/// <summary>
/// gameTimer value at which last chaos event was triggered
/// </summary>
static float CETriggerTime = 0;
/// <summary>
/// Time between chaos events
/// </summary>
public static Configurable<int> eventTimeout;
/// <summary>
/// Offset (negative and positive) applied to eventTimeout to randomize trigger time
/// </summary>
public static Configurable<int> eventTimeoutOffset;
/// <summary>
/// Percentage of events to not repeat
/// </summary>
public static Configurable<int> blockedEventPercent;
/// <summary>
/// CEHUD which contains the labels for new events happening and active events
/// </summary>
public static CEHUD CEHUD;
/// <summary>
/// Are we displaying active events as text to the player? (CM)
/// </summary>
public static Configurable<bool> _showActiveEvents;
public static bool showActiveEvents => _showActiveEvents.Value;
/// <summary>
/// Currently active chaos events
/// </summary>
static public List<CEEvent> activeEvents = new List<CEEvent>();
/// <summary>
/// All CEEVent classes, will be set to all enabled events by CM otherwise all events (CM)
/// </summary>
public static List<Type> eventTypes = new List<Type>();
/// <summary>
/// Events that can't be triggered because they happened too recently
/// </summary>
public static Type[] blockedEvents;
/// <summary>
/// Current position in blockedEvents
/// </summary>
static int blockedEventCounter = 0;
/// <summary>
/// Amount of events already triggered
/// </summary>
static int eventCounter = 0;
/// <summary>
/// Maximum amount of events to execute per cycle (CM)
/// </summary>
static public Configurable<int> maxEventCount;
static public Configurable<int> _eventDurationMult;
/// <summary>
/// Multiplier for event length
/// </summary>
static public float eventDurationMult => (float)(_eventDurationMult.Value) / 10;
/// <summary>
/// CE will only Update() (create events/call event triggers etc.) if game is active
/// </summary>
private static bool CEactive = false;
/// <summary>
/// Mod is running in CustomChaos mods and events are file driven
/// </summary>
public static bool CCMode = false;
//Debug/Extra
/// <summary>
/// Key for manually triggering a random event
/// </summary>
public static Configurable<KeyCode> triggerEventKey;
public static Configurable<bool> debugLogs;
//Rainworld game loop
private RainWorldGame game;
/// <summary>
/// BepInEx Plugin Version
/// </summary>
public static Version modVersion;
public static UnityEngine.AssetBundle CEAssetBundle;
static readonly Random rnd = new Random();
public RainWorldCE()
{
__me = new(this);
BepInPlugin attribute =
(BepInPlugin)Attribute.GetCustomAttribute(typeof(RainWorldCE), typeof(BepInPlugin));
modVersion = attribute.Version;
}
//Logging
private static WeakReference __me;
public static RainWorldCE ME => __me?.Target as RainWorldCE;
public ManualLogSource Logger_p => Logger;
public void OnEnable()
{
On.RainWorld.OnModsInit += OnModsInitHook;
}
bool initDone = false;
private void OnModsInitHook(On.RainWorld.orig_OnModsInit orig, RainWorld self)
{
orig(self);
if (!initDone)
{
ME.Logger_p.Log(LogLevel.Info, "RainWorldCE init");
try
{
MachineConnector.SetRegisteredOI(MOD_ID, new RainWorldCEOI());
}
catch (Exception e)
{
ME.Logger_p.Log(LogLevel.Error, $"Error creating options interface:\n {e}");
}
//Used for starting up everything
On.RainWorldGame.ctor += RainWorldGameCtorHook;
//Used for key input
On.RainWorldGame.Update += RainWorldGameUpdateHook;
//Triggers for resetting CEs state
On.RainWorldGame.ExitGame += RainWorldGameExitGameHook;
On.RainWorldGame.Win += RainWorldGameWinHook;
//Used to pause CE on process switch (death screen ...)
On.ProcessManager.RequestMainProcessSwitch_ProcessID += ProcessManagerRequestMainProcessSwitchHook;
//Add own HUD to the game
On.HUD.HUD.InitSinglePlayerHud += HUDInitSinglePlayerHudHook;
//Needed for fixing teleports
On.ShortcutHandler.TeleportingCreatureArrivedInRealizedRoom += ShortcutHandler_TeleportingCreatureArrivedInRealizedRoom;
//Used for creating PostProcessing container
On.RoomCamera.ctor += RoomCameraExtension.RoomCameraCtorHook;
//Used for updating PostProcessing effects
On.RoomCamera.DrawUpdate += RoomCameraExtension.RoomCameraDrawUpdateHook;
//Used as trigger for PlayerChangingRoomTrigger
On.ShortcutHandler.SuckInCreature += ShortcutHandlerSuckInCreatureHook;
//Used as trigger for PlayerChangedRoomTrigger
On.RoomCamera.ChangeRoom += RoomCameraChangeRoomHook;
//Load asset bundle containing shaders, can only loaded once otherwise error
try
{
CEAssetBundle = UnityEngine.AssetBundle.LoadFromFile(AssetManager.ResolveFilePath("AssetBundles/gamer025.rainworldce.assets"));
if (CEAssetBundle == null)
{
ME.Logger_p.Log(LogLevel.Error, $"RainWorldCE: Failed to load AssetBundle from {AssetManager.ResolveFilePath("AssetBundles/gamer025.rainworldce.assets")}");
Destroy(this);
}
ME.Logger_p.Log(LogLevel.Debug, $"Assetbundle content: {String.Join(", ", CEAssetBundle.GetAllAssetNames())}");
self.Shaders.Add("FlipScreenPP", FShader.CreateShader("FlipScreenPP", CEAssetBundle.LoadAsset<UnityEngine.Shader>("flipscreen.shader")));
self.Shaders.Add("MeltingPP", FShader.CreateShader("MeltingPP", CEAssetBundle.LoadAsset<UnityEngine.Shader>("melt.shader")));
self.Shaders.Add("PixelizePP", FShader.CreateShader("PixelizePP", CEAssetBundle.LoadAsset<UnityEngine.Shader>("pixelize.shader")));
}
catch (Exception e)
{
ME.Logger_p.Log(LogLevel.Error, $"Error loading asset bundle:\n {e}");
}
initDone = true;
}
}
//Start at -1 to give the game some time to start up fully
static float timepool = -1;
/// <summary>
/// Used to drive SecondUpdate if the main game is active
/// Doesn't really on the games internal updates/timing
/// </summary>
void Update()
{
//Only tick if the game seems to be running and is in story mode
if (CEactive && game is not null && game.IsStorySession && game.pauseMenu == null && game.AllowRainCounterToTick() && game.manager.currentMainLoop is RainWorldGame)
{
timepool += Time.deltaTime;
//We only need second precision, so lets only do stuff every full second
if (timepool > 1)
{
timepool--;
if (debugLogs.Value)
Logger_p.Log(LogLevel.Debug, $"{DateTime.Now:HH:mm:ss:fff} Calling SecondUpdate [{gameTimer}]");
SecondUpdate();
}
//Reset if overtiming
if (timepool > 2)
{
timepool = 0;
}
}
}
bool triggerEventDown = false;
private void RainWorldGameUpdateHook(On.RainWorldGame.orig_Update orig, RainWorldGame self)
{
orig(self);
bool triggerEvent = Input.GetKey(triggerEventKey.Value);
if (triggerEvent && !triggerEventDown)
{
Logger_p.Log(LogLevel.Info, $"Triggering event from keypress");
CreateNewEvent();
}
triggerEventDown = triggerEvent;
}
/// <summary>
/// Creates and enables a new <see cref="CEEvent">chaos event</see>
/// </summary>
void CreateNewEvent()
{
Type eventClass = PickEvent();
eventCounter++;
CEEvent selectedEvent = (CEEvent)Activator.CreateInstance(eventClass);
Logger_p.Log(LogLevel.Info, $"Triggering '{selectedEvent.Name}' event");
ActivateEvent(selectedEvent);
}
public static void ActivateEvent(CEEvent ceevent)
{
if (ceevent.ImplementsMethod("StartupTrigger"))
{
ME.Logger_p.Log(LogLevel.Debug, $"Calline StartupTrigger of '{ceevent.Name}' event");
try
{
ceevent.StartupTrigger();
}
catch (Exception e)
{
ME.Logger_p.Log(LogLevel.Error, $"'{ceevent.Name}' errored on startup, cancel event");
ME.Logger_p.Log(LogLevel.Error, e.ToString());
CEHUD.StopEventSelection(ceevent);
return;
}
}
//Stop the selecton after calling startup so that event can change its name/description before its displayed
CEHUD.StopEventSelection(ceevent);
if (ceevent.ActiveTime > 0)
{
activeEvents.Add(ceevent);
if (showActiveEvents) CEHUD.AddActiveEvent(ceevent.Name);
if (ceevent.RepeatEverySec > 0)
{
ceevent.RecurringEventTime = gameTimer;
}
}
}
public static Type PickEvent()
{
Type eventClass;
if (eventTypes.Count == 0)
{
eventClass = typeof(NoEventsEnabled);
//Disalbe CE since we have nothign to do
CEactive = false;
return eventClass;
}
//All events that aren't blocked or already active while not allowing multiple
List<Type> allowedEvents = eventTypes.Except(blockedEvents).ToList();
RemoveAlreadyActiveEvents(ref allowedEvents);
//Can't find any possible event to trigger
if (allowedEvents.Count == 0)
{
//Try again with normally blocked events included
ME.Logger_p.Log(LogLevel.Info, $"All non blocked events already active, trying again with blocked events included");
allowedEvents = eventTypes.ToList();
RemoveAlreadyActiveEvents(ref allowedEvents);
}
//Still can't find possible event, even with blocked events included
if (allowedEvents.Count == 0)
{
ME.Logger_p.Log(LogLevel.Warning, $"All enabled events already active, can't create new event");
eventClass = typeof(AllEventsFiltered);
//Disalbe CE since we have nothign to do
return eventClass;
}
eventClass = allowedEvents[rnd.Next(allowedEvents.Count)];
//Should fill up the array and then start overwriting the oldest blocked event
if (blockedEvents.Length > 0)
{
blockedEvents[blockedEventCounter] = eventClass;
blockedEventCounter = (blockedEventCounter + 1) % blockedEvents.Length;
if (debugLogs.Value)
ME.Logger_p.Log(LogLevel.Debug, $"Blocked events: {String.Join(",", blockedEvents.OfType<Type>().Select(x => x.ToString()).ToArray())}");
}
return eventClass;
}
private static void RemoveAlreadyActiveEvents(ref List<Type> allowedEvents)
{
for (int i = allowedEvents.Count - 1; i >= 0; i--)
{
Type allowedEvent = allowedEvents[i];
foreach (CEEvent activeEvent in activeEvents)
{
if (activeEvent.AllowMultiple == false && allowedEvent == activeEvent.GetType())
{
allowedEvents.Remove(allowedEvent);
}
}
}
}
int currentEventOffset = 0;
/// <summary>
/// Runs every second and triggers new chaos events / RecurringTrigger methods and expiring events
/// </summary>
void SecondUpdate()
{
//Process active events first bevore potentially spawning a new event
//This prevents a newly triggered events timmer getting ticked down while it isn't even active
foreach (CEEvent activeEvent in activeEvents.Where(x => !x.expired))
{
//Enough time has passed till last RecurringTrigger() call
if (activeEvent.RepeatEverySec > 0 && gameTimer - activeEvent.RecurringEventTime >= activeEvent.RepeatEverySec)
{
Logger_p.Log(LogLevel.Debug, $"Calling RecurringTrigger of {activeEvent.Name}");
try
{
activeEvent.RecurringTrigger();
}
catch (Exception e)
{
Logger_p.Log(LogLevel.Error, $"'{activeEvent.Name}' caused error during RecurringTrigger execution, removing event.");
Logger_p.Log(LogLevel.Error, e.ToString());
ShutdownCEEvent(activeEvent);
}
activeEvent.RecurringEventTime = gameTimer;
}
activeEvent.ActiveTime--;
}
//Either trigger events as defined in file or randomly picked
if (CCMode)
{
CustomChaos.CustomChaosUpdate();
}
else
{
//Only create new event if we didn't yet reach the limit for the cycle
if (eventCounter < maxEventCount.Value)
{
//Start the event selection HUD magic 3 seconds before the actual event
if (gameTimer - (CETriggerTime - 3) >= eventTimeout.Value + currentEventOffset)
{
CEHUD.StartEventSelection();
}
//Enough time has passed till last chaos event
if (gameTimer - CETriggerTime >= eventTimeout.Value + currentEventOffset)
{
CreateNewEvent();
CETriggerTime = gameTimer;
currentEventOffset = rnd.Next(-eventTimeoutOffset.Value, eventTimeoutOffset.Value);
}
}
}
//Deal with expired events
foreach (CEEvent ceevent in activeEvents.Where(ceevent => ceevent.ActiveTime <= 0))
{
ShutdownCEEvent(ceevent);
}
activeEvents = activeEvents.Where(x => !x.expired).ToList();
gameTimer++;
}
/// <summary>
/// Reset CEs state by clearing timers, active events etc.
/// Also deals with ShutdownTrigger of events
/// </summary>
void ResetState()
{
Logger_p.Log(LogLevel.Debug, $"Resetting state");
CEactive = false;
gameTimer = 0;
CETriggerTime = 0;
eventCounter = 0;
CustomChaos.step = 0;
CustomChaos.nextTrigger = 0;
//Call the shutdown event of active events before we remove the reference to the game
//In theory most events shouldn't need to cleanup since world is getting rebuild but some might have hooked stuff and need to undo these hooks
foreach (var ceevent in activeEvents.Where(ceevent => ceevent.ImplementsMethod("ShutdownTrigger")))
{
ShutdownCEEvent(ceevent);
}
activeEvents.Clear();
CEEvent.game = null;
}
/// <summary>
/// Safely tries to shutdown a event, it will be removed from activeEvents on next SecondUpdate run
/// </summary>
/// <param name="ceevent">Event to shutdown</param>
void ShutdownCEEvent(CEEvent ceevent)
{
try
{
if (ceevent.ImplementsMethod("ShutdownTrigger"))
{
ME.Logger_p.Log(LogLevel.Debug, $"Calline ShutdownTrigger of '{ceevent.Name}' event");
ceevent.ShutdownTrigger();
}
}
catch (Exception e)
{
ME.Logger_p.Log(LogLevel.Error, $"Error during '{ceevent.Name}' shutdown");
ME.Logger_p.Log(LogLevel.Error, e.ToString());
}
finally
{
ceevent.expired = true;
if (showActiveEvents) CEHUD.RemoveActiveEvent(ceevent.Name);
}
}
public static void TryLoadCC()
{
string configFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..\\CC.txt");
ME.Logger_p.Log(LogLevel.Info, $"Checking for CC.txt at {configFile}");
CCMode = File.Exists(configFile);
ME.Logger_p.Log(LogLevel.Info, $"CCMode is {CCMode}");
if (CCMode)
{
CustomChaos.parseConfig(File.ReadAllLines(configFile));
}
}
#region Hooks
void RainWorldGameCtorHook(On.RainWorldGame.orig_ctor orig, RainWorldGame self, ProcessManager manager)
{
orig(self, manager);
TryLoadCC();
ME.Logger_p.Log(LogLevel.Info, $"Starting event cycle with {eventTimeout.Value} second timer");
ResetState();
CEEvent.game = self;
game = self;
//Determine enabled events
eventTypes.Clear();
foreach (KeyValuePair<string, Configurable<bool>> entry in RainWorldCEOI.eventStatus)
{
bool enabled = entry.Value.Value;
ME.Logger_p.Log(LogLevel.Debug, $"Setting {entry.Key} to: {enabled} from Remix");
if (enabled)
{
eventTypes.Add(Type.GetType($"RainWorldCE.Events.{entry.Key.Replace("CEEvent_", "")}"));
}
}
//Calculate blocked event count
if (eventTypes.Count > 1)
{
int blockCount =
Convert.ToInt32(Math.Min(
(double)eventTypes.Count * blockedEventPercent.Value / 100,
(double)eventTypes.Count - 1));
if (blockedEvents is null || blockedEvents.Length != blockCount)
{
ME.Logger_p.Log(LogLevel.Debug, $"Setting blockedEvents to {blockCount} from Remix");
blockedEvents = new Type[blockCount];
blockedEventCounter = 0;
}
}
else
{
ME.Logger_p.Log(LogLevel.Debug, $"Less than two enabled events, setting blockedEvents to 0 from Remix");
blockedEvents = Array.Empty<Type>();
}
currentEventOffset = rnd.Next(-eventTimeoutOffset.Value, eventTimeoutOffset.Value);
CEactive = true;
}
void RainWorldGameExitGameHook(On.RainWorldGame.orig_ExitGame orig, RainWorldGame self, bool asDeath, bool asQuit)
{
ResetState();
orig(self, asDeath, asQuit);
}
void RainWorldGameWinHook(On.RainWorldGame.orig_Win orig, RainWorldGame self, bool malnourished)
{
ResetState();
orig(self, malnourished);
}
void ProcessManagerRequestMainProcessSwitchHook(On.ProcessManager.orig_RequestMainProcessSwitch_ProcessID orig, ProcessManager self, ProcessManager.ProcessID ID)
{
//Disable CE if game not in game mode
if (ID != ProcessManager.ProcessID.Game)
CEactive = false;
else
CEactive = true;
orig(self, ID);
}
void ShortcutHandlerSuckInCreatureHook(On.ShortcutHandler.orig_SuckInCreature orig, ShortcutHandler self, Creature creature, Room room, ShortcutData shortCut)
{
foreach (CEEvent activeEvent in activeEvents)
{
if (activeEvent.ImplementsMethod("PlayerChangingRoomTrigger") && creature is Player && shortCut.shortCutType == ShortcutData.Type.RoomExit)
{
try
{
activeEvent.PlayerChangingRoomTrigger(ref self, ref creature, ref room, ref shortCut);
}
catch (Exception e)
{
ME.Logger_p.Log(LogLevel.Error, $"Error during '{activeEvent.Name}' PlayerChangingRoomTrigger, removing event");
ME.Logger_p.Log(LogLevel.Error, e.ToString());
ShutdownCEEvent(activeEvent);
}
}
}
orig(self, creature, room, shortCut);
}
void RoomCameraChangeRoomHook(On.RoomCamera.orig_ChangeRoom orig, RoomCamera self, Room room, int camPos)
{
orig(self, room, camPos);
//Check if room has important story elements and pause if true
CEactive = true;
for (int i = 0; i < room.physicalObjects.Length; i++)
{
for (int j = 0; j < room.physicalObjects[i].Count; j++)
{
if (room.physicalObjects[i][j] is Oracle)
{
CEactive = false;
}
}
}
foreach (IDrawable drawable in room.drawableObjects)
{
if (drawable is Ghost)
{
CEactive = false;
}
}
foreach (CEEvent activeEvent in activeEvents)
{
if (activeEvent.ImplementsMethod("PlayerChangedRoomTrigger"))
{
try
{
activeEvent.PlayerChangedRoomTrigger(ref self, ref room, ref camPos);
}
catch (Exception e)
{
Logger_p.Log(LogLevel.Error, $"Error during '{activeEvent.Name}' PlayerChangedRoomTrigger, removing event");
Logger_p.Log(LogLevel.Error, e.ToString());
ShutdownCEEvent(activeEvent);
}
}
}
}
void HUDInitSinglePlayerHudHook(On.HUD.HUD.orig_InitSinglePlayerHud orig, HUD.HUD self, RoomCamera cam)
{
self.AddPart(new CEHUD(self, activeEvents));
orig(self, cam);
}
//Thanks to Henpemaz for providing this code and explaining the issue to me
//https://github.com/henpemaz/PartModPartMeme/blob/master/MapWarp/MapWarp.cs#L282
private void ShortcutHandler_TeleportingCreatureArrivedInRealizedRoom(On.ShortcutHandler.orig_TeleportingCreatureArrivedInRealizedRoom orig, ShortcutHandler self, ShortcutHandler.TeleportationVessel tVessel)
{
try
{
orig(self, tVessel);
}
catch (System.NullReferenceException)
{
if (tVessel.creature is not ITeleportingCreature)
{
WorldCoordinate arrival = tVessel.destination;
if (!arrival.TileDefined)
{
arrival = tVessel.room.realizedRoom.LocalCoordinateOfNode(tVessel.entranceNode);
arrival.abstractNode = tVessel.entranceNode;
}
tVessel.creature.abstractCreature.pos = arrival;
tVessel.creature.SpitOutOfShortCut(arrival.Tile, tVessel.room.realizedRoom, true);
}
}
}
#endregion
/// <summary>
/// Get all children classes of a type that aren't abstract and in the same namespace
/// </summary>
/// <typeparam name="T">Parent class</typeparam>
/// <returns>Children types</returns>
public static IEnumerable<Type> GetEnumerableOfType<T>() where T : class
{
IEnumerable<Type> objects =
Assembly.GetAssembly(typeof(T)).GetTypes()
.Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)));
return objects;
}
/// <summary>
/// Get all chaos events that are valid for random selection
/// </summary>
/// <returns></returns>
public static IEnumerable<Type> GetAllCEEventTypes()
{
IEnumerable<Type> eventTypes =
GetEnumerableOfType<CEEvent>().Where(x => !x.IsDefined(typeof(InternalCEEventAttribute), true));
if (!ModManager.MSC)
eventTypes = eventTypes.Where(x => !x.IsDefined(typeof(MSCEventAttribute), true));
return eventTypes;
}
}