tModLoader v0.11.8.9
A mod to make and play 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 (int index)
 
static Mod GetMod (string name)
 Gets the instance of the Mod with the specified name. More...
 
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 Version LastLaunchedTModLoaderVersion
 
static readonly bool linux = Platform.IsLinux
 
static readonly bool mac = Platform.IsOSX
 
static bool ShowFirstLaunchWelcomeMessage
 
static readonly Version version = new Version(0, 11, 8, 9)
 
static readonly string versionedName
 
static readonly string versionTag
 
static readonly bool windows = Platform.IsWindows
 

Properties

static string CompressedPlatformRepresentation [get]
 
static Mod[] LoadedMods [get]
 
static int ModCount [get]
 
static string ModPath [get]
 
static Mod[] Mods = new Mod[0] [get, private set]
 

Static Private Member Functions

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

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 _needsSavingEnabledMods
 
static bool _pauseSavingEnabledMods
 
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 28 of file ModLoader.cs.

Member Function Documentation

◆ DisplayLoadError()

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

Definition at line 272 of file ModLoader.cs.

273 {
274 msg += "\n\n" + (e.Data.Contains("hideStackTrace") ? e.Message : e.ToString());
275
276 if (Main.dedServ) {
277 Console.ForegroundColor = ConsoleColor.Red;
278 Console.WriteLine(msg);
279 Console.ResetColor();
280
281 if (fatal) {
282 Console.WriteLine("Press any key to exit...");
283 Console.ReadKey();
284 Environment.Exit(-1);
285 }
286 else {
287 Reload();
288 }
289 }
290 else {
291 Interface.errorMessage.Show(msg,
292 gotoMenu: fatal ? -1 : Interface.reloadModsID,
293 webHelpURL: e.HelpLink,
294 continueIsRetry: continueIsRetry,
295 showSkip: !fatal);
296 }
297 }
@ Environment
Sandstorm, Hell, Above surface during Eclipse, Space
@ Console
Command can be used in server console during MP.

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

Referenced by Terraria.ModLoader.ModLoader.Load(), and Terraria.ModLoader.ModLoader.Unload().

+ Here is the caller graph for this function:

◆ do_Unload()

static void Terraria.ModLoader.ModLoader.do_Unload ( )
staticprivate

Definition at line 244 of file ModLoader.cs.

245 {
246 Logging.tML.Info("Unloading mods");
247 if (Main.dedServ) {
248 Console.WriteLine("Unloading mods...");
249 }
250 else {
251 Interface.loadMods.SetLoadStage("tModLoader.MSUnloading", Mods.Length);
252 }
253
254 ModContent.UnloadModContent();
255 Mods = new Mod[0];
256 modsByName.Clear();
257 ModContent.Unload();
258
259 MemoryTracking.Clear();
260 Thread.MemoryBarrier();
261 GC.Collect();
262 }
static readonly IDictionary< string, Mod > modsByName
Definition: ModLoader.cs:63

References Terraria.ModLoader.Console, Terraria.ModLoader.ModLoader.Mods, and Terraria.ModLoader.ModLoader.modsByName.

Referenced by Terraria.ModLoader.ModLoader.Unload().

+ Here is the caller graph for this function:

◆ DotNet45Check()

static void Terraria.ModLoader.ModLoader.DotNet45Check ( )
staticprivate

Definition at line 196 of file ModLoader.cs.

197 {
198 if (FrameworkVersion.Framework != Framework.NetFramework || FrameworkVersion.Version >= new Version(4, 5))
199 return;
200
201 var msg = Language.GetTextValue("tModLoader.LoadErrorDotNet45Required");
202#if CLIENT
203 Interface.MessageBoxShow(msg);
204 Process.Start("https://dotnet.microsoft.com/download/dotnet-framework");
205#else
206 Console.ForegroundColor = ConsoleColor.Red;
207 Console.WriteLine(msg);
208 Console.ResetColor();
209 Console.WriteLine("Press any key to exit...");
210 Console.ReadKey();
211#endif
212 Environment.Exit(-1);
213 }
System.Version Version
Definition: ModLoader.cs:21

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

Referenced by Terraria.ModLoader.ModLoader.GetLoadedMods().

+ Here is the caller graph for this function:

◆ FileExists()

static bool Terraria.ModLoader.ModLoader.FileExists ( string  name)
static

◆ GetFileBytes()

static byte[] Terraria.ModLoader.ModLoader.GetFileBytes ( string  name)
static

◆ GetLoadedMods()

static string[] Terraria.ModLoader.ModLoader.GetLoadedMods ( )
static

References Terraria.ModLoader.ModLoader.DotNet45Check().

+ Here is the call graph for this function:

◆ GetMod() [1/2]

static Mod Terraria.ModLoader.ModLoader.GetMod ( int  index)
static

◆ GetMod() [2/2]

◆ GetMusic()

static Music Terraria.ModLoader.ModLoader.GetMusic ( string  name)
static

◆ GetSound()

static SoundEffect Terraria.ModLoader.ModLoader.GetSound ( string  name)
static

◆ GetTexture()

static Texture2D Terraria.ModLoader.ModLoader.GetTexture ( string  name)
static

◆ IsSignedBy()

static bool Terraria.ModLoader.ModLoader.IsSignedBy ( TmodFile  mod,
string  xmlPublicKey 
)
static

Definition at line 300 of file ModLoader.cs.

301 {
302 var f = new RSAPKCS1SignatureDeformatter();
303 var v = AsymmetricAlgorithm.Create("RSA");
304 f.SetHashAlgorithm("SHA1");
305 v.FromXmlString(xmlPublicKey);
306 f.SetKey(v);
307 return f.VerifySignature(mod.hash, mod.signature);
308 }

◆ Load()

static void Terraria.ModLoader.ModLoader.Load ( CancellationToken  token = default)
staticprivate

Definition at line 120 of file ModLoader.cs.

121 {
122 try {
123 if (isLoading)
124 throw new Exception("Load called twice");
125 isLoading = true;
126
127 if (!Unload())
128 return;
129
130 var modInstances = ModOrganizer.LoadMods(token);
131
132 weakModReferences = modInstances.Select(x => new WeakReference(x)).ToArray();
133 modInstances.Insert(0, new ModLoaderMod());
134 Mods = modInstances.ToArray();
135 foreach (var mod in Mods)
136 modsByName[mod.Name] = mod;
137
138 ModContent.Load(token);
139
140 if (OnSuccessfulLoad != null) {
141 OnSuccessfulLoad();
142 }
143 else {
144 Main.menuMode = 0;
145 }
146 }
147 catch when (token.IsCancellationRequested) {
148 // cancel needs to reload with ModLoaderMod and all others skipped
149 skipLoad = true;
150 OnSuccessfulLoad += () => Main.menuMode = Interface.modsMenuID;
151
152 isLoading = false;
153 Load(); // don't provide a token, loading just ModLoaderMod should be quick
154 }
155 catch (Exception e) {
156 var responsibleMods = new List<string>();
157 if (e.Data.Contains("mod"))
158 responsibleMods.Add((string)e.Data["mod"]);
159 if (e.Data.Contains("mods"))
160 responsibleMods.AddRange((IEnumerable<string>)e.Data["mods"]);
161 responsibleMods.Remove("ModLoader");
162
163 if (responsibleMods.Count == 0 && AssemblyManager.FirstModInStackTrace(new StackTrace(e), out var stackMod))
164 responsibleMods.Add(stackMod);
165
166 var msg = Language.GetTextValue("tModLoader.LoadError", string.Join(", ", responsibleMods));
167 if (responsibleMods.Count == 1) {
168 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)
169 if (mod != null && (mod.tModLoaderVersion.Major != version.Major || mod.tModLoaderVersion.Minor != version.Minor || mod.tModLoaderVersion.Build != version.Build))
170 msg += "\n" + Language.GetTextValue("tModLoader.LoadErrorVersionMessage", mod.tModLoaderVersion, versionedName);
171 }
172 if (responsibleMods.Count > 0)
173 msg += "\n" + Language.GetTextValue("tModLoader.LoadErrorDisabled");
174 else
175 msg += "\n" + Language.GetTextValue("tModLoader.LoadErrorCulpritUnknown");
176
177 if (e is ReflectionTypeLoadException reflectionTypeLoadException)
178 msg += "\n\n" + string.Join("\n", reflectionTypeLoadException.LoaderExceptions.Select(x => x.Message));
179
180 Logging.tML.Error(msg, e);
181
182 foreach (var mod in responsibleMods)
183 DisableMod(mod);
184
185 isLoading = false; // disable loading flag, because server will just instantly retry reload
186 DisplayLoadError(msg, e, e.Data.Contains("fatal"), responsibleMods.Count == 0);
187 }
188 finally {
189 isLoading = false;
190 OnSuccessfulLoad = null;
191 skipLoad = false;
192 ModNet.NetReloadActive = false;
193 }
194 }
static readonly Version version
Definition: ModLoader.cs:30
static void Load(CancellationToken token=default)
Definition: ModLoader.cs:120
static void DisplayLoadError(string msg, Exception e, bool fatal, bool continueIsRetry=false)
Definition: ModLoader.cs:272
static readonly string versionedName
Definition: ModLoader.cs:41
static WeakReference[] weakModReferences
Definition: ModLoader.cs:64

References Terraria.ModLoader.ModLoader.DisplayLoadError(), Terraria.ModLoader.ModLoader.isLoading, Terraria.ModLoader.ModLoader.Load(), Terraria.ModLoader.ModLoader.Mods, Terraria.ModLoader.ModLoader.modsByName, Terraria.ModLoader.ModLoader.Unload(), Terraria.ModLoader.ModLoader.version, Terraria.ModLoader.ModLoader.versionedName, and Terraria.ModLoader.ModLoader.weakModReferences.

Referenced by Terraria.ModLoader.ModLoader.Load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MusicExists()

static bool Terraria.ModLoader.ModLoader.MusicExists ( string  name)
static

◆ SoundExists()

static bool Terraria.ModLoader.ModLoader.SoundExists ( string  name)
static

◆ TextureExists()

static bool Terraria.ModLoader.ModLoader.TextureExists ( string  name)
static

◆ Unload()

static bool Terraria.ModLoader.ModLoader.Unload ( )
staticprivate

Definition at line 223 of file ModLoader.cs.

224 {
225 try {
226 // have to move unload logic to a separate method so the stack frame is cleared. Otherwise unloading can capture mod instances in local variables, even with memory barriers (thanks compiler weirdness)
227 do_Unload();
229 return true;
230 }
231 catch (Exception e) {
232 var msg = Language.GetTextValue("tModLoader.UnloadError");
233
234 if (e.Data.Contains("mod"))
235 msg += "\n" + Language.GetTextValue("tModLoader.DefensiveUnload", e.Data["mod"]);
236
237 Logging.tML.Fatal(msg, e);
238 DisplayLoadError(msg, e, true);
239
240 return false;
241 }
242 }
static void WarnModsStillLoaded()
Definition: ModLoader.cs:265

References Terraria.ModLoader.ModLoader.DisplayLoadError(), Terraria.ModLoader.ModLoader.do_Unload(), and Terraria.ModLoader.ModLoader.WarnModsStillLoaded().

Referenced by Terraria.ModLoader.ModLoader.Load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ WarnModsStillLoaded()

static void Terraria.ModLoader.ModLoader.WarnModsStillLoaded ( )
staticprivate

Definition at line 265 of file ModLoader.cs.

266 {
267 badUnloaders = weakModReferences.Where(r => r.IsAlive).Select(r => ((Mod)r.Target).Name).ToList();
268 foreach (var modName in badUnloaders)
269 Logging.tML.WarnFormat("{0} not fully unloaded during unload.", modName);
270 }

References Terraria.ModLoader.ModLoader.weakModReferences.

Referenced by Terraria.ModLoader.ModLoader.Unload().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _enabledMods

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 324 of file ModLoader.cs.

◆ _needsSavingEnabledMods

bool Terraria.ModLoader.ModLoader._needsSavingEnabledMods
staticprivate

Definition at line 311 of file ModLoader.cs.

◆ _pauseSavingEnabledMods

bool Terraria.ModLoader.ModLoader._pauseSavingEnabledMods
staticprivate

Definition at line 310 of file ModLoader.cs.

◆ beta

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

Definition at line 38 of file ModLoader.cs.

◆ branchName

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

Definition at line 36 of file ModLoader.cs.

◆ compressedPlatformRepresentation

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

Definition at line 57 of file ModLoader.cs.

◆ isLoading

bool Terraria.ModLoader.ModLoader.isLoading = false
staticprivate

Definition at line 119 of file ModLoader.cs.

Referenced by Terraria.ModLoader.ModLoader.Load().

◆ LastLaunchedTModLoaderVersion

Version Terraria.ModLoader.ModLoader.LastLaunchedTModLoaderVersion
static

Definition at line 32 of file ModLoader.cs.

◆ linux

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

Definition at line 52 of file ModLoader.cs.

◆ mac

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

Definition at line 54 of file ModLoader.cs.

◆ modsByName

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

◆ ShowFirstLaunchWelcomeMessage

bool Terraria.ModLoader.ModLoader.ShowFirstLaunchWelcomeMessage
static

Definition at line 34 of file ModLoader.cs.

◆ steamID64

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

Definition at line 69 of file ModLoader.cs.

◆ version

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

Definition at line 30 of file ModLoader.cs.

Referenced by Terraria.ModLoader.ModLoader.Load().

◆ versionedName

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

Definition at line 41 of file ModLoader.cs.

Referenced by Terraria.ModLoader.ModLoader.Load().

◆ versionTag

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

Definition at line 45 of file ModLoader.cs.

◆ weakModReferences

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

◆ windows

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

Definition at line 50 of file ModLoader.cs.

Property Documentation

◆ CompressedPlatformRepresentation

string Terraria.ModLoader.ModLoader.CompressedPlatformRepresentation
staticget

Definition at line 59 of file ModLoader.cs.

◆ LoadedMods

Mod [] Terraria.ModLoader.ModLoader.LoadedMods
staticget

Definition at line 99 of file ModLoader.cs.

◆ ModCount

int Terraria.ModLoader.ModLoader.ModCount
staticget

Definition at line 102 of file ModLoader.cs.

◆ ModPath

string Terraria.ModLoader.ModLoader.ModPath
staticget

Definition at line 61 of file ModLoader.cs.

◆ Mods