Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
LoadingFunneler.cs
Go to the documentation of this file.
1 using System;
2 using System.Diagnostics;
3 using System.Reflection;
4 using Loot.Api.Attributes;
5 using Loot.Api.ModContent;
6 using Loot.Ext;
7 using Loot.ILEditing;
8 using Loot.ModSupport;
9 using Loot.UI;
10 using Microsoft.Xna.Framework.Graphics;
11 using Terraria;
12 using Terraria.UI;
13 
14 namespace Loot.Api.Loaders
15 {
19  internal static class LoadingFunneler
20  {
21  internal static void Load()
22  {
23  LoadMod();
24  if (!Main.dedServ)
25  {
26  LoadModForClient();
27  }
28  else
29  {
30  ModSupportTunneler.AddServerSupport();
31  }
32  }
33 
34  internal static void Unload()
35  {
36  UnloadMod();
37  Loot.IsLoaded = false;
38  }
39 
40  internal static void PostLoad()
41  {
42  RegistryLoader.ProcessMods();
43  if (!Main.dedServ)
44  {
45  Loot.ModContentManager.Load();
46  }
47  LoadILEdits();
48  Loot.IsLoaded = true;
49  }
50 
51  // Load EMM for both Client and Server
52  private static void LoadMod()
53  {
54  ModSupportTunneler.Init();
55 
56  RegistryLoader.Initialize();
57  RegistryLoader.Load();
58  ContentLoader.Initialize();
59  ContentLoader.Load();
60  }
61 
62  // Load EMM for Client only (this doesn't need to be loaded for server)
63  private static void LoadModForClient()
64  {
65  LoadStaticAssets();
66 
67  Loot.ModContentManager = new ModContentManager();
68  Loot.ModContentManager.Initialize(Loot.Instance);
69 
70  Loot.Instance.GuiInterface = new UserInterface();
71  Loot.Instance.GuiState = new GuiTabWindow();
72  Loot.Instance.GuiState.Activate();
73 
74  AssetLoader.RegisterAssets(Loot.Instance, "GraphicsAssets");
75  ModSupportTunneler.AddClientSupport();
76  }
77 
78  private static void UnloadMod()
79  {
80  ContentLoader.Unload();
81  RegistryLoader.Unload();
82  Loot.ModContentManager?.Unload();
83  Loot.ModContentManager = null;
84  UnloadStaticAssets();
85  }
86 
87  private static void LoadStaticAssets()
88  {
89  foreach (var mem in ReflectUtils.GetStaticAssetTypes())
90  {
91  var attr = (StaticAssetAttribute)mem.GetCustomAttribute(typeof(StaticAssetAttribute));
92  Debug.Assert(attr != null);
93  if (mem is PropertyInfo prop && prop.PropertyType == typeof(Texture2D))
94  prop.SetValue(null, attr.LoadTexture2D());
95  if (mem is FieldInfo field && field.FieldType == typeof(Texture2D))
96  field.SetValue(null, attr.LoadTexture2D());
97  }
98  }
99 
100  private static void UnloadStaticAssets()
101  {
102  foreach (var mem in ReflectUtils.GetStaticAssetTypes())
103  {
104  if (!mem.GetType().IsValueType || Nullable.GetUnderlyingType(mem.GetType()) != null)
105  {
106  if (mem is PropertyInfo prop)
107  prop.SetValue(null, null);
108  if (mem is FieldInfo field)
109  field.SetValue(null, null);
110  }
111  }
112  }
113 
114  private static void LoadILEdits()
115  {
116  foreach (var type in ReflectUtils.GetILEdits())
117  {
118  // We do not need to store IL Edits after application: tModLoader auto unloads edits
119  ((ILEdit)Activator.CreateInstance(type)).Apply(Main.dedServ);
120  }
121  }
122 
123  // TODO unused
124  //private static void UnloadStaticRefs()
125  //{
126  // // TODO causes trouble in unload?
127  // // Attempt to unload our static variables
128  // Stack<Type> typesToProcess = new Stack<Type>(Loot.Instance.Code.GetTypes());
129  // while (typesToProcess.Count > 0)
130  // {
131  // Type type = typesToProcess.Pop();
132  // foreach (FieldInfo info in type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
133  // {
134  // info.SetValue(null, info.FieldType.IsValueType ? Activator.CreateInstance(info.FieldType) : null);
135  // }
136  // foreach (Type nestedType in type.GetNestedTypes(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
137  // {
138  // typesToProcess.Push(nestedType);
139  // }
140  // }
141  //}
142  }
143 }
This class is responsible for managing instances of TextureModContent for registered mods...