Terraria ModLoader  0.11.5
A framework for Terraria mods
Terraria.ModLoader.ModLoader Class Reference

This serves as the central class which loads mods. It contains many static fields and methods related to mods and their contents. More...

+ Collaboration diagram for Terraria.ModLoader.ModLoader:

Static Public Member Functions

static bool FileExists (string name)
 
static byte[] GetFileBytes (string name)
 
static string[] GetLoadedMods ()
 
static Mod GetMod (string name)
 Gets the instance of the Mod with the specified name. More...
 
static Mod GetMod (int index)
 
static Music GetMusic (string name)
 
static SoundEffect GetSound (string name)
 
static Texture2D GetTexture (string name)
 
static bool IsSignedBy (TmodFile mod, string xmlPublicKey)
 
static bool MusicExists (string name)
 
static bool SoundExists (string name)
 
static bool TextureExists (string name)
 

Static Public Attributes

static readonly int beta = 0
 
static readonly string branchName = ""
 
static readonly string compressedPlatformRepresentation = Platform.IsWindows ? "w" : (Platform.IsLinux ? "l" : "m")
 
static string CompressedPlatformRepresentation => (Platform.IsWindows ? "w" : (Platform.IsLinux ? "l" : "m")) + (GoGVerifier.IsGoG ? "g" : "s") + (FrameworkVersion.Framework == Framework.NetFramework ? "n" : (FrameworkVersion.Framework == Framework.Mono ? "o" : "u"))
 
static Version LastLaunchedTModLoaderVersion
 
static readonly bool linux = Platform.IsLinux
 
static Mod[] LoadedMods => Mods
 
static readonly bool mac = Platform.IsOSX
 
static int ModCount => Mods.Length
 
static string ModPath => ModOrganizer.modPath
 
static bool ShowFirstLaunchWelcomeMessage
 
static readonly Version version = new Version(0, 11, 5)
 
static readonly string versionedName
 
static readonly string versionTag
 
static readonly bool windows = Platform.IsWindows
 

Properties

static Mod[] Mods [get, private set]
 

Static Private Member Functions

static void DisplayLoadError (string msg, Exception e, bool fatal, bool continueIsRetry=false)
 
static void DotNet45Check ()
 
static void Load (CancellationToken token=default)
 
static bool Unload ()
 

Static Private Attributes

static HashSet< string > _enabledMods
 A cached list of enabled mods (not necessarily currently loaded or even installed), mirroring the enabled.json file. More...
 
static bool isLoading = false
 
static readonly IDictionary< string, ModmodsByName = new Dictionary<string, Mod>(StringComparer.OrdinalIgnoreCase)
 
static string steamID64 = ""
 
static WeakReference[] weakModReferences = new WeakReference[0]
 

Detailed Description

This serves as the central class which loads mods. It contains many static fields and methods related to mods and their contents.

Definition at line 26 of file ModLoader.cs.

Member Function Documentation

static void Terraria.ModLoader.ModLoader.DisplayLoadError ( string  msg,
Exception  e,
bool  fatal,
bool  continueIsRetry = false 
)
staticprivate

Definition at line 249 of file ModLoader.cs.

References Terraria.ModLoader.Console, and Terraria.ModLoader.Environment.

249  {
250  msg += "\n\n" + (e.Data.Contains("hideStackTrace") ? e.Message : e.ToString());
251 
252  if (Main.dedServ) {
253  Console.ForegroundColor = ConsoleColor.Red;
254  Console.WriteLine(msg);
255  Console.ResetColor();
256 
257  if (fatal) {
258  Console.WriteLine("Press any key to exit...");
259  Console.ReadKey();
260  Environment.Exit(-1);
261  }
262  else {
263  Reload();
264  }
265  }
266  else {
267  Interface.errorMessage.Show(msg,
268  gotoMenu: fatal ? -1 : Interface.reloadModsID,
269  webHelpURL: e.HelpLink,
270  showRetry: continueIsRetry,
271  showSkip: !fatal);
272  }
273  }
Command can be used in server console during MP.
Sandstorm, Hell, Above surface during Eclipse, Space
static void Terraria.ModLoader.ModLoader.DotNet45Check ( )
staticprivate

Definition at line 182 of file ModLoader.cs.

References Terraria.ModLoader.Console, Terraria.ModLoader.Environment, Terraria.ModLoader.FrameworkVersion.Framework, and Terraria.ModLoader.FrameworkVersion.Version.

182  {
183  if (FrameworkVersion.Framework != Framework.NetFramework || FrameworkVersion.Version >= new Version(4, 5))
184  return;
185 
186  var msg = Language.GetTextValue("tModLoader.LoadErrorDotNet45Required");
187 #if CLIENT
188  Interface.MessageBoxShow(msg);
189  Process.Start("https://www.microsoft.com/net/download/thank-you/net472");
190 #else
191  Console.ForegroundColor = ConsoleColor.Red;
192  Console.WriteLine(msg);
193  Console.ResetColor();
194  Console.WriteLine("Press any key to exit...");
195  Console.ReadKey();
196 #endif
197  Environment.Exit(-1);
198  }
Command can be used in server console during MP.
Sandstorm, Hell, Above surface during Eclipse, Space
static bool Terraria.ModLoader.ModLoader.FileExists ( string  name)
static
static byte [] Terraria.ModLoader.ModLoader.GetFileBytes ( string  name)
static
static string [] Terraria.ModLoader.ModLoader.GetLoadedMods ( )
static
static Mod Terraria.ModLoader.ModLoader.GetMod ( int  index)
static
static Music Terraria.ModLoader.ModLoader.GetMusic ( string  name)
static
static SoundEffect Terraria.ModLoader.ModLoader.GetSound ( string  name)
static
static Texture2D Terraria.ModLoader.ModLoader.GetTexture ( string  name)
static
static bool Terraria.ModLoader.ModLoader.IsSignedBy ( TmodFile  mod,
string  xmlPublicKey 
)
static

Definition at line 276 of file ModLoader.cs.

276  {
277  var f = new RSAPKCS1SignatureDeformatter();
278  var v = AsymmetricAlgorithm.Create("RSA");
279  f.SetHashAlgorithm("SHA1");
280  v.FromXmlString(xmlPublicKey);
281  f.SetKey(v);
282  return f.VerifySignature(mod.hash, mod.signature);
283  }
static void Terraria.ModLoader.ModLoader.Load ( CancellationToken  token = default)
staticprivate

Definition at line 114 of file ModLoader.cs.

114  {
115  try {
116  if (isLoading)
117  throw new Exception("Load called twice");
118  isLoading = true;
119 
120  if (!Unload())
121  return;
122 
123  var modInstances = ModOrganizer.LoadMods(token);
124 
125  weakModReferences = modInstances.Select(x => new WeakReference(x)).ToArray();
126  modInstances.Insert(0, new ModLoaderMod());
127  Mods = modInstances.ToArray();
128  foreach (var mod in Mods)
129  modsByName[mod.Name] = mod;
130 
131  ModContent.Load(token);
132 
133  if (OnSuccessfulLoad != null) {
134  OnSuccessfulLoad();
135  OnSuccessfulLoad = null;
136  }
137  else {
138  Main.menuMode = 0;
139  }
140  }
141  catch when (token.IsCancellationRequested) {
142  if (Unload())
143  Main.menuMode = Interface.modsMenuID;
144  }
145  catch (Exception e) {
146  var responsibleMods = new List<string>();
147  if (e.Data.Contains("mod"))
148  responsibleMods.Add((string)e.Data["mod"]);
149  if (e.Data.Contains("mods"))
150  responsibleMods.AddRange((IEnumerable<string>)e.Data["mods"]);
151  responsibleMods.Remove("ModLoader");
152 
153  if (responsibleMods.Count == 0 && AssemblyManager.FirstModInStackTrace(new StackTrace(e), out var stackMod))
154  responsibleMods.Add(stackMod);
155 
156  var msg = Language.GetTextValue("tModLoader.LoadError", string.Join(", ", responsibleMods));
157  if (responsibleMods.Count == 1) {
158  var mod = ModOrganizer.FindMods().FirstOrDefault(m => m.Name == responsibleMods[0]); //use First rather than Single, incase of "Two mods with the same name" error message from ModOrganizer (#639)
159  if (mod != null && mod.tModLoaderVersion != version)
160  msg += "\n" + Language.GetTextValue("tModLoader.LoadErrorVersionMessage", mod.tModLoaderVersion, versionedName);
161  }
162  if (responsibleMods.Count > 0)
163  msg += "\n" + Language.GetTextValue("tModLoader.LoadErrorDisabled");
164  else
165  msg += "\n" + Language.GetTextValue("tModLoader.LoadErrorCulpritUnknown");
166 
167  if (e is ReflectionTypeLoadException reflectionTypeLoadException)
168  msg += "\n\n" + string.Join("\n", reflectionTypeLoadException.LoaderExceptions.Select(x => x.Message));
169 
170  Logging.tML.Error(msg, e);
171 
172  foreach (var mod in responsibleMods)
173  DisableMod(mod);
174 
175  DisplayLoadError(msg, e, e.Data.Contains("fatal"), responsibleMods.Count == 0);
176  }
177  finally {
178  isLoading = false;
179  }
180  }
static readonly string versionedName
Definition: ModLoader.cs:38
static WeakReference[] weakModReferences
Definition: ModLoader.cs:60
static void DisplayLoadError(string msg, Exception e, bool fatal, bool continueIsRetry=false)
Definition: ModLoader.cs:249
static readonly IDictionary< string, Mod > modsByName
Definition: ModLoader.cs:59
static readonly Version version
Definition: ModLoader.cs:28
static bool Terraria.ModLoader.ModLoader.MusicExists ( string  name)
static
static bool Terraria.ModLoader.ModLoader.SoundExists ( string  name)
static
static bool Terraria.ModLoader.ModLoader.TextureExists ( string  name)
static
static bool Terraria.ModLoader.ModLoader.Unload ( )
staticprivate

Definition at line 208 of file ModLoader.cs.

References Terraria.ModLoader.Console.

208  {
209  if (Mods.Length == 0)
210  return true;
211 
212  try {
213  Logging.tML.Info("Unloading mods");
214  if (Main.dedServ) {
215  Console.WriteLine("Unloading mods...");
216  } else {
217  Interface.loadMods.SetLoadStage("tModLoader.MSUnloading", Mods.Length);
218  }
219 
220  ModContent.UnloadModContent();
221  Mods = new Mod[0];
222  modsByName.Clear();
223  ModContent.Unload();
224 
225  MemoryTracking.Clear();
226  Thread.MemoryBarrier();
227  GC.Collect();
228  badUnloaders.Clear();
229  foreach (var mod in weakModReferences.Where(r => r.IsAlive).Select(r => (Mod)r.Target)) {
230  Logging.tML.WarnFormat("{0} not fully unloaded during unload.", mod.Name);
231  badUnloaders.Add(mod.Name);
232  }
233 
234  return true;
235  }
236  catch (Exception e) {
237  var msg = Language.GetTextValue("tModLoader.UnloadError");
238 
239  if (e.Data.Contains("mod"))
240  msg += "\n" + Language.GetTextValue("tModLoader.DefensiveUnload", e.Data["mod"]);
241 
242  Logging.tML.Fatal(msg, e);
243  DisplayLoadError(msg, e, true);
244 
245  return false;
246  }
247  }
Command can be used in server console during MP.
static WeakReference[] weakModReferences
Definition: ModLoader.cs:60
static void DisplayLoadError(string msg, Exception e, bool fatal, bool continueIsRetry=false)
Definition: ModLoader.cs:249
static readonly IDictionary< string, Mod > modsByName
Definition: ModLoader.cs:59

Member Data Documentation

HashSet<string> Terraria.ModLoader.ModLoader._enabledMods
staticprivate

A cached list of enabled mods (not necessarily currently loaded or even installed), mirroring the enabled.json file.

Definition at line 286 of file ModLoader.cs.

readonly int Terraria.ModLoader.ModLoader.beta = 0
static

Definition at line 36 of file ModLoader.cs.

readonly string Terraria.ModLoader.ModLoader.branchName = ""
static

Definition at line 34 of file ModLoader.cs.

readonly string Terraria.ModLoader.ModLoader.compressedPlatformRepresentation = Platform.IsWindows ? "w" : (Platform.IsLinux ? "l" : "m")
static

Definition at line 53 of file ModLoader.cs.

string Terraria.ModLoader.ModLoader.CompressedPlatformRepresentation => (Platform.IsWindows ? "w" : (Platform.IsLinux ? "l" : "m")) + (GoGVerifier.IsGoG ? "g" : "s") + (FrameworkVersion.Framework == Framework.NetFramework ? "n" : (FrameworkVersion.Framework == Framework.Mono ? "o" : "u"))
static

Definition at line 55 of file ModLoader.cs.

bool Terraria.ModLoader.ModLoader.isLoading = false
staticprivate

Definition at line 113 of file ModLoader.cs.

Version Terraria.ModLoader.ModLoader.LastLaunchedTModLoaderVersion
static

Definition at line 30 of file ModLoader.cs.

readonly bool Terraria.ModLoader.ModLoader.linux = Platform.IsLinux
static

Definition at line 48 of file ModLoader.cs.

Mod [] Terraria.ModLoader.ModLoader.LoadedMods => Mods
static

Definition at line 94 of file ModLoader.cs.

readonly bool Terraria.ModLoader.ModLoader.mac = Platform.IsOSX
static

Definition at line 50 of file ModLoader.cs.

int Terraria.ModLoader.ModLoader.ModCount => Mods.Length
static

Definition at line 97 of file ModLoader.cs.

string Terraria.ModLoader.ModLoader.ModPath => ModOrganizer.modPath
static

Definition at line 57 of file ModLoader.cs.

readonly IDictionary<string, Mod> Terraria.ModLoader.ModLoader.modsByName = new Dictionary<string, Mod>(StringComparer.OrdinalIgnoreCase)
staticprivate

Definition at line 59 of file ModLoader.cs.

bool Terraria.ModLoader.ModLoader.ShowFirstLaunchWelcomeMessage
static

Definition at line 32 of file ModLoader.cs.

string Terraria.ModLoader.ModLoader.steamID64 = ""
staticprivate

Definition at line 65 of file ModLoader.cs.

readonly Version Terraria.ModLoader.ModLoader.version = new Version(0, 11, 5)
static

Definition at line 28 of file ModLoader.cs.

readonly string Terraria.ModLoader.ModLoader.versionedName
static
Initial value:
= $"tModLoader v{version}" +
(branchName.Length == 0 ? "" : $" {branchName}") +
(beta == 0 ? "" : $" Beta {beta}")

Definition at line 38 of file ModLoader.cs.

readonly string Terraria.ModLoader.ModLoader.versionTag
static
Initial value:
= $"v{version}" +
(branchName.Length == 0 ? "" : $"-{branchName.ToLower()}") +
(beta == 0 ? "" : $"-beta{beta}")

Definition at line 41 of file ModLoader.cs.

WeakReference [] Terraria.ModLoader.ModLoader.weakModReferences = new WeakReference[0]
staticprivate

Definition at line 60 of file ModLoader.cs.

readonly bool Terraria.ModLoader.ModLoader.windows = Platform.IsWindows
static

Definition at line 46 of file ModLoader.cs.

Property Documentation