Terraria ModLoader  0.10.1.5
A framework for Terraria mods
ModInternals.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework.Audio;
2 using Microsoft.Xna.Framework.Graphics;
3 using MP3Sharp;
4 using NVorbis;
5 using ReLogic.Graphics;
6 using ReLogic.Utilities;
7 using System;
8 using System.Collections.Generic;
9 using System.IO;
10 using System.Linq;
11 using System.Threading.Tasks;
12 using Terraria.GameContent.Liquid;
13 using Terraria.ID;
14 using Terraria.Localization;
17 using Terraria.ModLoader.IO;
18 using Terraria.Utilities;
19 
20 namespace Terraria.ModLoader
21 {
22  partial class Mod
23  {
24  internal bool loading;
25  private readonly Queue<Task> AsyncLoadQueue = new Queue<Task>();
26  internal readonly IDictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
27  internal readonly IDictionary<string, SoundEffect> sounds = new Dictionary<string, SoundEffect>();
28  internal readonly IDictionary<string, Music> musics = new Dictionary<string, Music>();
29  internal readonly IDictionary<string, DynamicSpriteFont> fonts = new Dictionary<string, DynamicSpriteFont>();
30  internal readonly IDictionary<string, Effect> effects = new Dictionary<string, Effect>();
31  internal readonly IList<ModRecipe> recipes = new List<ModRecipe>();
32  internal readonly IDictionary<string, ModItem> items = new Dictionary<string, ModItem>();
33  internal readonly IDictionary<string, GlobalItem> globalItems = new Dictionary<string, GlobalItem>();
34  internal readonly IDictionary<Tuple<string, EquipType>, EquipTexture> equipTextures = new Dictionary<Tuple<string, EquipType>, EquipTexture>();
35  internal readonly IDictionary<string, ModPrefix> prefixes = new Dictionary<string, ModPrefix>();
36  internal readonly IDictionary<string, ModDust> dusts = new Dictionary<string, ModDust>();
37  internal readonly IDictionary<string, ModTile> tiles = new Dictionary<string, ModTile>();
38  internal readonly IDictionary<string, GlobalTile> globalTiles = new Dictionary<string, GlobalTile>();
39  internal readonly IDictionary<string, ModTileEntity> tileEntities = new Dictionary<string, ModTileEntity>();
40  internal readonly IDictionary<string, ModWall> walls = new Dictionary<string, ModWall>();
41  internal readonly IDictionary<string, GlobalWall> globalWalls = new Dictionary<string, GlobalWall>();
42  internal readonly IDictionary<string, ModProjectile> projectiles = new Dictionary<string, ModProjectile>();
43  internal readonly IDictionary<string, GlobalProjectile> globalProjectiles = new Dictionary<string, GlobalProjectile>();
44  internal readonly IDictionary<string, ModNPC> npcs = new Dictionary<string, ModNPC>();
45  internal readonly IDictionary<string, GlobalNPC> globalNPCs = new Dictionary<string, GlobalNPC>();
46  internal readonly IDictionary<string, ModPlayer> players = new Dictionary<string, ModPlayer>();
47  internal readonly IDictionary<string, ModMountData> mountDatas = new Dictionary<string, ModMountData>();
48  internal readonly IDictionary<string, ModBuff> buffs = new Dictionary<string, ModBuff>();
49  internal readonly IDictionary<string, GlobalBuff> globalBuffs = new Dictionary<string, GlobalBuff>();
50  internal readonly IDictionary<string, ModWorld> worlds = new Dictionary<string, ModWorld>();
51  internal readonly IDictionary<string, ModUgBgStyle> ugBgStyles = new Dictionary<string, ModUgBgStyle>();
52  internal readonly IDictionary<string, ModSurfaceBgStyle> surfaceBgStyles = new Dictionary<string, ModSurfaceBgStyle>();
53  internal readonly IDictionary<string, GlobalBgStyle> globalBgStyles = new Dictionary<string, GlobalBgStyle>();
54  internal readonly IDictionary<string, ModWaterStyle> waterStyles = new Dictionary<string, ModWaterStyle>();
55  internal readonly IDictionary<string, ModWaterfallStyle> waterfallStyles = new Dictionary<string, ModWaterfallStyle>();
56  internal readonly IDictionary<string, GlobalRecipe> globalRecipes = new Dictionary<string, GlobalRecipe>();
57  internal readonly IDictionary<string, ModTranslation> translations = new Dictionary<string, ModTranslation>();
58 
59  private void LoadTexture(string path, Stream stream, bool rawimg) {
60  try {
61  var texTask = rawimg
62  ? ImageIO.RawToTexture2DAsync(Main.instance.GraphicsDevice, new BinaryReader(stream))
63  : ImageIO.PngToTexture2DAsync(Main.instance.GraphicsDevice, stream);
64 
65  AsyncLoadQueue.Enqueue(texTask.ContinueWith(t => {
66  if (t.Exception != null)
67  throw new ResourceLoadException(
68  Language.GetTextValue("tModLoader.LoadErrorTextureFailedToLoad", path), t.Exception);
69 
70  var tex = t.Result;
71  tex.Name = Name + "/" + path;
72  lock (textures)
73  textures[path] = tex;
74  }));
75  }
76  catch (Exception e) {
77  throw new ResourceLoadException(Language.GetTextValue("tModLoader.LoadErrorTextureFailedToLoad", path), e);
78  }
79  finally {
80  stream.Close();
81  }
82  }
83 
84  private SoundEffect LoadSound(Stream stream, int length, string extension) {
85  switch (extension) {
86  case ".wav":
87  if (!stream.CanSeek)
88  stream = new MemoryStream(stream.ReadBytes(length));
89  return SoundEffect.FromStream(stream);
90  case ".mp3":
91  using (var mp3Stream = new MP3Stream(stream))
92  using (var ms = new MemoryStream()) {
93  mp3Stream.CopyTo(ms);
94  return new SoundEffect(ms.ToArray(), mp3Stream.Frequency, (AudioChannels)mp3Stream.ChannelCount);
95  }
96  case ".ogg":
97  using (var reader = new VorbisReader(stream, true)) {
98  var buffer = new byte[reader.TotalSamples * 2 * reader.Channels];
99  var floatBuf = new float[buffer.Length / 2];
100  reader.ReadSamples(floatBuf, 0, floatBuf.Length);
101  MusicStreamingOGG.Convert(floatBuf, buffer);
102  return new SoundEffect(buffer, reader.SampleRate, (AudioChannels)reader.Channels);
103  }
104  }
105  throw new ResourceLoadException("Unknown sound extension "+extension);
106  }
107 
108  private Music LoadMusic(string path, string extension) {
109  path = "tmod:"+Name+'/'+path+extension;
110  switch (extension) {
111  case ".wav": return new MusicStreamingWAV(path);
112  case ".mp3": return new MusicStreamingMP3(path);
113  case ".ogg": return new MusicStreamingOGG(path);
114  }
115  throw new ResourceLoadException("Unknown music extension "+extension);
116  }
117 
118  internal void SetupContent() {
119  foreach (ModItem item in items.Values) {
120  ItemLoader.SetDefaults(item.item, false);
121  item.AutoStaticDefaults();
122  item.SetStaticDefaults();
123  }
124  foreach (ModPrefix prefix in prefixes.Values) {
125  prefix.AutoDefaults();
126  prefix.SetDefaults();
127  }
128  foreach (ModDust dust in dusts.Values) {
129  dust.SetDefaults();
130  }
131  foreach (ModTile tile in tiles.Values) {
132  Main.tileTexture[tile.Type] = ModContent.GetTexture(tile.texture);
133  TileLoader.SetDefaults(tile);
134  if (TileID.Sets.HasOutlines[tile.Type]) {
135  Main.highlightMaskTexture[tile.Type] = ModContent.GetTexture(tile.HighlightTexture);
136  }
137  if (!string.IsNullOrEmpty(tile.chest)) {
138  TileID.Sets.BasicChest[tile.Type] = true;
139  }
140  }
141  foreach (GlobalTile globalTile in globalTiles.Values) {
142  globalTile.SetDefaults();
143  }
144  foreach (ModWall wall in walls.Values) {
145  Main.wallTexture[wall.Type] = ModContent.GetTexture(wall.texture);
146  wall.SetDefaults();
147  }
148  foreach (GlobalWall globalWall in globalWalls.Values) {
149  globalWall.SetDefaults();
150  }
151  foreach (ModProjectile projectile in projectiles.Values) {
152  ProjectileLoader.SetDefaults(projectile.projectile, false);
153  projectile.AutoStaticDefaults();
154  projectile.SetStaticDefaults();
155  }
156  foreach (ModNPC npc in npcs.Values) {
157  NPCLoader.SetDefaults(npc.npc, false);
158  npc.AutoStaticDefaults();
159  npc.SetStaticDefaults();
160  }
161  foreach (ModMountData modMountData in mountDatas.Values) {
162  var mountData = modMountData.mountData;
163  mountData.modMountData = modMountData;
164  MountLoader.SetupMount(mountData);
165  Mount.mounts[modMountData.Type] = mountData;
166  }
167  foreach (ModBuff buff in buffs.Values) {
168  Main.buffTexture[buff.Type] = ModContent.GetTexture(buff.texture);
169  buff.SetDefaults();
170  }
171  foreach (ModWaterStyle waterStyle in waterStyles.Values) {
172  LiquidRenderer.Instance._liquidTextures[waterStyle.Type] = ModContent.GetTexture(waterStyle.texture);
173  Main.liquidTexture[waterStyle.Type] = ModContent.GetTexture(waterStyle.blockTexture);
174  }
175  foreach (ModWaterfallStyle waterfallStyle in waterfallStyles.Values) {
176  Main.instance.waterfallManager.waterfallTexture[waterfallStyle.Type]
177  = ModContent.GetTexture(waterfallStyle.texture);
178  }
179  }
180 
181  internal void UnloadContent() {
182  Unload();
183  recipes.Clear();
184  items.Clear();
185  globalItems.Clear();
186  equipTextures.Clear();
187  prefixes.Clear();
188  dusts.Clear();
189  tiles.Clear();
190  globalTiles.Clear();
191  tileEntities.Clear();
192  walls.Clear();
193  globalWalls.Clear();
194  players.Clear();
195  projectiles.Clear();
196  globalProjectiles.Clear();
197  npcs.Clear();
198  globalNPCs.Clear();
199  buffs.Clear();
200  globalBuffs.Clear();
201  mountDatas.Clear();
202  worlds.Clear();
203  ugBgStyles.Clear();
204  surfaceBgStyles.Clear();
205  globalBgStyles.Clear();
206  waterStyles.Clear();
207  waterfallStyles.Clear();
208  globalRecipes.Clear();
209  translations.Clear();
210  if (!Main.dedServ) {
211  // Manually Dispose IDisposables to free up unmanaged memory immediately
212  /* Skip this for now, too many mods don't unload properly and run into exceptions.
213  foreach (var sound in sounds)
214  {
215  sound.Value.Dispose();
216  }
217  foreach (var effect in effects)
218  {
219  effect.Value.Dispose();
220  }
221  foreach (var texture in textures)
222  {
223  texture.Value.Dispose();
224  }
225  */
226  }
227  sounds.Clear();
228  effects.Clear();
229  foreach (var tex in textures.Values)
230  tex?.Dispose();
231  textures.Clear();
232  musics.Clear();
233  fonts.Clear();
234  }
235 
236  internal void Autoload() {
237  if (Code == null)
238  return;
239 
240  Interface.loadMods.SubProgressText = Language.GetTextValue("tModLoader.MSFinishingResourceLoading");
241  while (AsyncLoadQueue.Count > 0)
242  AsyncLoadQueue.Dequeue().Wait();
243 
244  AutoloadLocalization();
245  IList<Type> modGores = new List<Type>();
246  IList<Type> modSounds = new List<Type>();
247  foreach (Type type in Code.GetTypes().OrderBy(type => type.FullName, StringComparer.InvariantCulture)) {
248  if (type.IsAbstract || type.GetConstructor(new Type[0]) == null)//don't autoload things with no default constructor
249  {
250  continue;
251  }
252  if (type.IsSubclassOf(typeof(ModItem))) {
253  AutoloadItem(type);
254  }
255  else if (type.IsSubclassOf(typeof(GlobalItem))) {
256  AutoloadGlobalItem(type);
257  }
258  else if (type.IsSubclassOf(typeof(ModPrefix))) {
259  AutoloadPrefix(type);
260  }
261  else if (type.IsSubclassOf(typeof(ModDust))) {
262  AutoloadDust(type);
263  }
264  else if (type.IsSubclassOf(typeof(ModTile))) {
265  AutoloadTile(type);
266  }
267  else if (type.IsSubclassOf(typeof(GlobalTile))) {
268  AutoloadGlobalTile(type);
269  }
270  else if (type.IsSubclassOf(typeof(ModTileEntity))) {
271  AutoloadTileEntity(type);
272  }
273  else if (type.IsSubclassOf(typeof(ModWall))) {
274  AutoloadWall(type);
275  }
276  else if (type.IsSubclassOf(typeof(GlobalWall))) {
277  AutoloadGlobalWall(type);
278  }
279  else if (type.IsSubclassOf(typeof(ModProjectile))) {
280  AutoloadProjectile(type);
281  }
282  else if (type.IsSubclassOf(typeof(GlobalProjectile))) {
283  AutoloadGlobalProjectile(type);
284  }
285  else if (type.IsSubclassOf(typeof(ModNPC))) {
286  AutoloadNPC(type);
287  }
288  else if (type.IsSubclassOf(typeof(GlobalNPC))) {
289  AutoloadGlobalNPC(type);
290  }
291  else if (type.IsSubclassOf(typeof(ModPlayer))) {
292  AutoloadPlayer(type);
293  }
294  else if (type.IsSubclassOf(typeof(ModBuff))) {
295  AutoloadBuff(type);
296  }
297  else if (type.IsSubclassOf(typeof(GlobalBuff))) {
298  AutoloadGlobalBuff(type);
299  }
300  else if (type.IsSubclassOf(typeof(ModMountData))) {
301  AutoloadMountData(type);
302  }
303  else if (type.IsSubclassOf(typeof(ModGore))) {
304  modGores.Add(type);
305  }
306  else if (type.IsSubclassOf(typeof(ModSound))) {
307  modSounds.Add(type);
308  }
309  else if (type.IsSubclassOf(typeof(ModWorld))) {
310  AutoloadModWorld(type);
311  }
312  else if (type.IsSubclassOf(typeof(ModUgBgStyle))) {
313  AutoloadUgBgStyle(type);
314  }
315  else if (type.IsSubclassOf(typeof(ModSurfaceBgStyle))) {
316  AutoloadSurfaceBgStyle(type);
317  }
318  else if (type.IsSubclassOf(typeof(GlobalBgStyle))) {
319  AutoloadGlobalBgStyle(type);
320  }
321  else if (type.IsSubclassOf(typeof(ModWaterStyle))) {
322  AutoloadWaterStyle(type);
323  }
324  else if (type.IsSubclassOf(typeof(ModWaterfallStyle))) {
325  AutoloadWaterfallStyle(type);
326  }
327  else if (type.IsSubclassOf(typeof(GlobalRecipe))) {
328  AutoloadGlobalRecipe(type);
329  }
330  else if (type.IsSubclassOf(typeof(ModCommand))) {
331  AutoloadCommand(type);
332  }
333  }
334  if (Properties.AutoloadGores) {
335  AutoloadGores(modGores);
336  }
337  if (Properties.AutoloadSounds) {
338  AutoloadSounds(modSounds);
339  }
340  if (Properties.AutoloadBackgrounds) {
341  AutoloadBackgrounds();
342  }
343  }
344 
345  private void AutoloadItem(Type type) {
346  ModItem item = (ModItem)Activator.CreateInstance(type);
347  item.mod = this;
348  string name = type.Name;
349  if (item.Autoload(ref name)) {
350  AddItem(name, item);
351  var autoloadEquip = type.GetAttribute<AutoloadEquip>();
352  if (autoloadEquip != null)
353  foreach (var equip in autoloadEquip.equipTypes)
354  AddEquipTexture(item, equip, item.Name, item.Texture + '_' + equip,
355  item.Texture + "_Arms", item.Texture + "_FemaleBody");
356  }
357  }
358 
359  private void AutoloadGlobalItem(Type type) {
360  GlobalItem globalItem = (GlobalItem)Activator.CreateInstance(type);
361  globalItem.mod = this;
362  string name = type.Name;
363  if (globalItem.Autoload(ref name)) {
364  AddGlobalItem(name, globalItem);
365  }
366  }
367 
368  private void AutoloadPrefix(Type type) {
369  ModPrefix prefix = (ModPrefix)Activator.CreateInstance(type);
370  prefix.mod = this;
371  string name = type.Name;
372  if (prefix.Autoload(ref name)) {
373  AddPrefix(name, prefix);
374  }
375  }
376 
377  private void AutoloadDust(Type type) {
378  ModDust dust = (ModDust)Activator.CreateInstance(type);
379  dust.mod = this;
380  string name = type.Name;
381  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
382  if (dust.Autoload(ref name, ref texture)) {
383  AddDust(name, dust, texture);
384  }
385  }
386 
387  private void AutoloadTile(Type type) {
388  ModTile tile = (ModTile)Activator.CreateInstance(type);
389  tile.mod = this;
390  string name = type.Name;
391  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
392  if (tile.Autoload(ref name, ref texture)) {
393  AddTile(name, tile, texture);
394  }
395  }
396 
397  private void AutoloadGlobalTile(Type type) {
398  GlobalTile globalTile = (GlobalTile)Activator.CreateInstance(type);
399  globalTile.mod = this;
400  string name = type.Name;
401  if (globalTile.Autoload(ref name)) {
402  AddGlobalTile(name, globalTile);
403  }
404  }
405 
406  private void AutoloadTileEntity(Type type) {
407  ModTileEntity tileEntity = (ModTileEntity)Activator.CreateInstance(type);
408  tileEntity.mod = this;
409  string name = type.Name;
410  if (tileEntity.Autoload(ref name)) {
411  AddTileEntity(name, tileEntity);
412  }
413  }
414 
415  private void AutoloadWall(Type type) {
416  ModWall wall = (ModWall)Activator.CreateInstance(type);
417  wall.mod = this;
418  string name = type.Name;
419  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
420  if (wall.Autoload(ref name, ref texture)) {
421  AddWall(name, wall, texture);
422  }
423  }
424 
425  private void AutoloadGlobalWall(Type type) {
426  GlobalWall globalWall = (GlobalWall)Activator.CreateInstance(type);
427  globalWall.mod = this;
428  string name = type.Name;
429  if (globalWall.Autoload(ref name)) {
430  AddGlobalWall(name, globalWall);
431  }
432  }
433 
434  private void AutoloadProjectile(Type type) {
435  ModProjectile projectile = (ModProjectile)Activator.CreateInstance(type);
436  projectile.mod = this;
437  string name = type.Name;
438  if (projectile.Autoload(ref name)) {
439  AddProjectile(name, projectile);
440  }
441  }
442 
443  private void AutoloadGlobalProjectile(Type type) {
444  GlobalProjectile globalProjectile = (GlobalProjectile)Activator.CreateInstance(type);
445  globalProjectile.mod = this;
446  string name = type.Name;
447  if (globalProjectile.Autoload(ref name)) {
448  AddGlobalProjectile(name, globalProjectile);
449  }
450  }
451 
452  private void AutoloadNPC(Type type) {
453  ModNPC npc = (ModNPC)Activator.CreateInstance(type);
454  npc.mod = this;
455  string name = type.Name;
456  if (npc.Autoload(ref name)) {
457  AddNPC(name, npc);
458  var autoloadHead = type.GetAttribute<AutoloadHead>();
459  if (autoloadHead != null) {
460  string headTexture = npc.HeadTexture;
461  AddNPCHeadTexture(npc.npc.type, headTexture);
462  }
463  var autoloadBossHead = type.GetAttribute<AutoloadBossHead>();
464  if (autoloadBossHead != null) {
465  string headTexture = npc.BossHeadTexture;
466  AddBossHeadTexture(headTexture, npc.npc.type);
467  }
468  }
469  }
470 
471  private void AutoloadGlobalNPC(Type type) {
472  GlobalNPC globalNPC = (GlobalNPC)Activator.CreateInstance(type);
473  globalNPC.mod = this;
474  string name = type.Name;
475  if (globalNPC.Autoload(ref name)) {
476  AddGlobalNPC(name, globalNPC);
477  }
478  }
479 
480  private void AutoloadPlayer(Type type) {
481  ModPlayer player = (ModPlayer)Activator.CreateInstance(type);
482  player.mod = this;
483  string name = type.Name;
484  if (player.Autoload(ref name)) {
485  AddPlayer(name, player);
486  }
487  }
488 
489  private void AutoloadBuff(Type type) {
490  ModBuff buff = (ModBuff)Activator.CreateInstance(type);
491  buff.mod = this;
492  string name = type.Name;
493  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
494  if (buff.Autoload(ref name, ref texture)) {
495  AddBuff(name, buff, texture);
496  }
497  }
498 
499  private void AutoloadGlobalBuff(Type type) {
500  GlobalBuff globalBuff = (GlobalBuff)Activator.CreateInstance(type);
501  globalBuff.mod = this;
502  string name = type.Name;
503  if (globalBuff.Autoload(ref name)) {
504  AddGlobalBuff(name, globalBuff);
505  }
506  }
507 
508  private void AutoloadMountData(Type type) {
509  ModMountData mount = (ModMountData)Activator.CreateInstance(type);
510  mount.mod = this;
511  string name = type.Name;
512  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
513  var extraTextures = new Dictionary<MountTextureType, string>();
514  foreach (MountTextureType textureType in Enum.GetValues(typeof(MountTextureType))) {
515  extraTextures[textureType] = texture + "_" + textureType.ToString();
516  }
517  if (mount.Autoload(ref name, ref texture, extraTextures)) {
518  AddMount(name, mount, texture, extraTextures);
519  }
520  }
521 
522  private void AutoloadModWorld(Type type) {
523  ModWorld modWorld = (ModWorld)Activator.CreateInstance(type);
524  modWorld.mod = this;
525  string name = type.Name;
526  if (modWorld.Autoload(ref name)) {
527  AddModWorld(name, modWorld);
528  }
529  }
530 
531  private void AutoloadBackgrounds() {
532  foreach (string texture in textures.Keys.Where(t => t.StartsWith("Backgrounds/"))) {
533  AddBackgroundTexture(Name + '/' + texture);
534  }
535  }
536 
537  private void AutoloadUgBgStyle(Type type) {
538  ModUgBgStyle ugBgStyle = (ModUgBgStyle)Activator.CreateInstance(type);
539  ugBgStyle.mod = this;
540  string name = type.Name;
541  if (ugBgStyle.Autoload(ref name)) {
542  AddUgBgStyle(name, ugBgStyle);
543  }
544  }
545 
546  private void AutoloadSurfaceBgStyle(Type type) {
547  ModSurfaceBgStyle surfaceBgStyle = (ModSurfaceBgStyle)Activator.CreateInstance(type);
548  surfaceBgStyle.mod = this;
549  string name = type.Name;
550  if (surfaceBgStyle.Autoload(ref name)) {
551  AddSurfaceBgStyle(name, surfaceBgStyle);
552  }
553  }
554 
555  private void AutoloadGlobalBgStyle(Type type) {
556  GlobalBgStyle globalBgStyle = (GlobalBgStyle)Activator.CreateInstance(type);
557  globalBgStyle.mod = this;
558  string name = type.Name;
559  if (globalBgStyle.Autoload(ref name)) {
560  AddGlobalBgStyle(name, globalBgStyle);
561  }
562  }
563 
564  private void AutoloadWaterStyle(Type type) {
565  ModWaterStyle waterStyle = (ModWaterStyle)Activator.CreateInstance(type);
566  waterStyle.mod = this;
567  string name = type.Name;
568  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
569  string blockTexture = texture + "_Block";
570  if (waterStyle.Autoload(ref name, ref texture, ref blockTexture)) {
571  AddWaterStyle(name, waterStyle, texture, blockTexture);
572  }
573  }
574 
575  private void AutoloadWaterfallStyle(Type type) {
576  ModWaterfallStyle waterfallStyle = (ModWaterfallStyle)Activator.CreateInstance(type);
577  waterfallStyle.mod = this;
578  string name = type.Name;
579  string texture = (type.Namespace + "." + type.Name).Replace('.', '/');
580  if (waterfallStyle.Autoload(ref name, ref texture)) {
581  AddWaterfallStyle(name, waterfallStyle, texture);
582  }
583  }
584 
585  private void AutoloadGores(IList<Type> modGores) {
586  var modGoreNames = modGores.ToDictionary(t => t.Namespace + "." + t.Name);
587  foreach (var texture in textures.Keys.Where(t => t.StartsWith("Gores/"))) {
588  ModGore modGore = null;
589  Type t;
590  if (modGoreNames.TryGetValue(Name + "." + texture.Replace('/', '.'), out t))
591  modGore = (ModGore)Activator.CreateInstance(t);
592 
593  AddGore(Name + '/' + texture, modGore);
594  }
595  }
596 
597  private void AutoloadSounds(IList<Type> modSounds) {
598  var modSoundNames = modSounds.ToDictionary(t => t.Namespace + "." + t.Name);
599  foreach (var sound in sounds.Keys.Where(t => t.StartsWith("Sounds/"))) {
600  string substring = sound.Substring("Sounds/".Length);
601  SoundType soundType = SoundType.Custom;
602  if (substring.StartsWith("Item/")) {
603  soundType = SoundType.Item;
604  }
605  else if (substring.StartsWith("NPCHit/")) {
606  soundType = SoundType.NPCHit;
607  }
608  else if (substring.StartsWith("NPCKilled/")) {
609  soundType = SoundType.NPCKilled;
610  }
611  ModSound modSound = null;
612  Type t;
613  if (modSoundNames.TryGetValue((Name + '/' + sound).Replace('/', '.'), out t))
614  modSound = (ModSound)Activator.CreateInstance(t);
615 
616  AddSound(soundType, Name + '/' + sound, modSound);
617  }
618  foreach (var music in musics.Keys.Where(t => t.StartsWith("Sounds/"))) {
619  string substring = music.Substring("Sounds/".Length);
620  if (substring.StartsWith("Music/")) {
621  AddSound(SoundType.Music, Name + '/' + music);
622  }
623  }
624  }
625 
626  private void AutoloadGlobalRecipe(Type type) {
627  GlobalRecipe globalRecipe = (GlobalRecipe)Activator.CreateInstance(type);
628  globalRecipe.mod = this;
629  string name = type.Name;
630  if (globalRecipe.Autoload(ref name)) {
631  AddGlobalRecipe(name, globalRecipe);
632  }
633  }
634 
635  private void AutoloadCommand(Type type) {
636  var mc = (ModCommand)Activator.CreateInstance(type);
637  mc.mod = this;
638  var name = type.Name;
639  if (mc.Autoload(ref name))
640  AddCommand(name, mc);
641  }
642 
646  private void AutoloadLocalization() {
647  var modTranslationDictionary = new Dictionary<string, ModTranslation>();
648  foreach (var translationFile in File.Where(entry => Path.GetExtension(entry.Name) == ".lang")) {
649  // .lang files need to be UTF8 encoded.
650  string translationFileContents = System.Text.Encoding.UTF8.GetString(File.GetBytes(translationFile));
651  GameCulture culture = GameCulture.FromName(Path.GetFileNameWithoutExtension(translationFile.Name));
652 
653  using (StringReader reader = new StringReader(translationFileContents)) {
654  string line;
655  while ((line = reader.ReadLine()) != null) {
656  int split = line.IndexOf('=');
657  if (split < 0)
658  continue; // lines witout a = are ignored
659  string key = line.Substring(0, split).Trim().Replace(" ", "_");
660  string value = line.Substring(split + 1).Trim();
661  if (value.Length == 0) {
662  continue;
663  }
664  value = value.Replace("\\n", "\n");
665  // TODO: Maybe prepend key with filename: en.US.ItemName.lang would automatically assume "ItemName." for all entries.
666  //string key = key;
667  if (!modTranslationDictionary.TryGetValue(key, out ModTranslation mt))
668  modTranslationDictionary[key] = mt = CreateTranslation(key);
669  mt.AddTranslation(culture, value);
670  }
671  }
672  }
673 
674  foreach (var value in modTranslationDictionary.Values) {
675  AddTranslation(value);
676  }
677  }
678  }
679 }
void AutoloadGlobalBuff(Type type)
int Type
The index of this ModMountData in the Mount.mounts array.
Definition: ModMountData.cs:32
virtual void AutoStaticDefaults()
Automatically sets certain static defaults. Override this if you do not want the properties to be set...
Definition: ModNPC.cs:189
virtual void SetStaticDefaults()
This is where you set all your item&#39;s static properties, such as names/translations and the arrays in...
Definition: ModItem.cs:169
A ModPlayer instance represents an extension of a Player instance. You can store fields in the ModPla...
Definition: ModPlayer.cs:15
This class represents a type of tile that can be added by a mod. Only one instance of this class will...
Definition: ModTile.cs:12
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalBuff instead of using Mod.AddGlobalBuff. Return true to allo...
Definition: GlobalBuff.cs:31
This class serves as a place for you to place all your properties and hooks for each NPC...
Definition: ModNPC.cs:14
Each background style determines in its own way how exactly the background is drawn. This class serves as a collection of functions for above-ground backgrounds.
void AutoloadTile(Type type)
Mod mod
The mod which has added this type of ModTile.
Definition: ModTile.cs:17
void AutoloadTileEntity(Type type)
This serves as the central class from which projectile-related functions are carried out...
Mod mod
The mod that added this style of waterfall.
SoundEffect LoadSound(Stream stream, int length, string extension)
Definition: ModInternals.cs:84
Mod mod
The mod to which this GlobalWall belongs.
Definition: GlobalWall.cs:13
Tile Entities are Entities tightly coupled with tiles, allowing the possibility of tiles to exhibit c...
void AutoloadGlobalItem(Type type)
static Texture2D GetTexture(string name)
Gets the texture with the specified name. The name is in the format of "ModFolder/OtherFolders/FileNa...
Definition: ModContent.cs:70
void AutoloadSounds(IList< Type > modSounds)
Mod mod
That mod that added this global background style.
void AutoloadGlobalTile(Type type)
void AutoloadGlobalWall(Type type)
This class allows you to modify the behavior of any tile in the game. Create an instance of an overri...
Definition: GlobalTile.cs:10
virtual bool Autoload(ref string name, ref string texture)
Allows you to automatically add a type of dust without having to use Mod.AddDust. By default returns ...
Definition: ModDust.cs:118
void AutoloadItem(Type type)
virtual bool Autoload(ref string name, ref string texture, ref string blockTexture)
Allows you to automatically add a ModWaterStyle instead of using Mod.AddWaterStyle. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name, and texture is initialized to the namespace and overriding class name with periods replaced with slashes. BlockTexture is initialized to texture with "_Block" added at the end. Use this to either force or stop an autoload, change the name that identifies this type of ModWaterStyle, and/or change the texture paths used by this ModWaterStyle.
virtual string HeadTexture
The file name of this NPC&#39;s head texture file, to be used in autoloading.
Definition: ModNPC.cs:61
void AutoloadNPC(Type type)
Mod mod
The mod that added this ModBuff.
Definition: ModBuff.cs:11
void AutoloadGlobalProjectile(Type type)
Mod mod
The mod that added this ModNPC.
Definition: ModNPC.cs:29
virtual bool Autoload(ref string name)
Definition: ModPrefix.cs:126
This serves as a place for you to program behaviors of equipment textures. This is useful for equipme...
Definition: EquipTexture.cs:8
void LoadTexture(string path, Stream stream, bool rawimg)
Definition: ModInternals.cs:59
Mod mod
The mod to which this GlobalTile belongs to.
Definition: GlobalTile.cs:15
virtual void SetDefaults()
Allows you to set this ModDust&#39;s updateType field and modify the Terraria.GameContent.ChildSafety.SafeDust array.
Definition: ModDust.cs:125
Item item
The item object that this ModItem controls.
Definition: ModItem.cs:26
virtual bool Autoload(ref string name)
Allows you to automatically add a ModUgBgStyle instead of using Mod.AddUgBgStyle. Return true to allo...
void AutoloadModWorld(Type type)
void AutoloadWaterStyle(Type type)
virtual string HighlightTexture
The highlight texture used when this tile is selected by smart interact. Defaults to adding "_Highlig...
Definition: ModTile.cs:42
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalNPC instead of using Mod.AddGlobalNPC. Return true to allow ...
Definition: GlobalNPC.cs:37
void AutoloadSurfaceBgStyle(Type type)
void AutoloadCommand(Type type)
This serves as the central class from which tile-related functions are supported and carried out...
Definition: TileLoader.cs:14
virtual void AutoStaticDefaults()
Automatically sets certain static defaults. Override this if you do not want the properties to be set...
Definition: ModItem.cs:175
Mod mod
The mod to which this GlobalItem belongs.
Definition: GlobalItem.cs:20
virtual void SetDefaults()
Allows you to set the prefix&#39;s name/translations and to set its category.
Definition: ModPrefix.cs:138
virtual bool Autoload(ref string name)
Allows you to automatically add a GlobalBgStyle instead of using Mod.AddGlobalBgStyle. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name. Use this to either force or stop an autoload, or change the name that identifies this type of GlobalBgStyle.
virtual void SetDefaults()
This is where all buff related assignments go. For example: Main.buffName[Type] = "Display Name"; Mai...
Definition: ModBuff.cs:72
virtual bool Autoload(ref string name, ref string texture)
Allows you to automatically load a buff instead of using Mod.AddBuff. Return true to allow autoloadin...
Definition: ModBuff.cs:57
Mod mod
The mod that added this type of dust.
Definition: ModDust.cs:39
virtual void SetDefaults()
Allows you to modify the properties of any tile in the game. Most properties are stored as arrays thr...
Definition: GlobalTile.cs:77
ushort Type
The internal ID of this type of wall.
Definition: ModWall.cs:33
This class allows you to customize the behavior of a custom gore. Create a new instance of this and p...
Definition: ModGore.cs:12
virtual bool Autoload(ref string name, ref string texture)
Allows you to modify the name and texture path of this wall when it is autoloaded. Return true to autoload this wall. When a wall is autoloaded, that means you do not need to manually call Mod.AddWall. By default returns the mod&#39;s autoload property.
Definition: ModWall.cs:123
Music LoadMusic(string path, string extension)
NPC npc
The NPC object that this ModNPC controls.
Definition: ModNPC.cs:21
Mod mod
The mod which has added this ModMountData.
Definition: ModMountData.cs:24
void AutoloadWaterfallStyle(Type type)
This class serves to collect functions that operate on any kind of background style, without being specific to one single background style.
virtual bool Autoload(ref string name)
Allows you to automatically add a ModWorld instead of using Mod.AddModWorld. Return true to allow aut...
Definition: ModWorld.cs:32
Mod mod
The mod that added this type of ModPlayer.
Definition: ModPlayer.cs:20
void AutoloadDust(Type type)
Represents a style of water that gets drawn, based on factors such as the background. This is used to determine the color of the water, as well as other things as determined by the hooks below.
Definition: ModWaterStyle.cs:8
Mod mod
The mod that added this style of water.
Manages content added by mods. Liasons between mod content and Terraria&#39;s arrays and oversees the Loa...
Definition: ModContent.cs:22
Mod mod
The mod which has added this type of ModWall.
Definition: ModWall.cs:17
Projectile projectile
The projectile object that this ModProjectile controls.
This class allows you to modify the behavior of any wall in the game (although admittedly walls don&#39;t...
Definition: GlobalWall.cs:8
Mod mod
The mod object that this ModProjectile originates from.
This class allows you to modify and use hooks for all items, including vanilla items. Create an instance of an overriding class then call Mod.AddGlobalItem to use this.
Definition: GlobalItem.cs:15
Mod mod
Gets the mod.
Definition: ModItem.cs:37
virtual void SetStaticDefaults()
Allows you to set all your NPC&#39;s static properties, such as names/translations and the arrays in NPCI...
Definition: ModNPC.cs:183
This class allows you to customize how a sound you add is played. To use this, pass an instance to Mo...
Definition: ModSound.cs:8
virtual bool Autoload(ref string name)
Allows you to automatically add a ModSurfaceBgStyle instead of using Mod.AddSurfaceBgStyle. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name. Use this to either force or stop an autoload, or change the name that identifies this type of ModSurfaceBgStyle.
static Task< Texture2D > RawToTexture2DAsync(GraphicsDevice graphicsDevice, BinaryReader r)
Definition: ImageIO.cs:94
virtual bool Autoload(ref string name)
Allows you to automatically load an item instead of using Mod.AddItem. Return true to allow autoloadi...
Definition: ModItem.cs:99
void AutoloadGlobalNPC(Type type)
virtual bool Autoload(ref string name)
Allows you to automatically load an NPC instead of using Mod.AddNPC. Return true to allow autoloading...
Definition: ModNPC.cs:121
void AutoloadMountData(Type type)
Mod mod
The mod to which this GlobalNPC belongs.
Definition: GlobalNPC.cs:16
This class represents a type of wall that can be added by a mod. Only one instance of this class will...
Definition: ModWall.cs:12
This serves as the central class from which item-related functions are carried out. It also stores a list of mod items by ID.
Definition: ItemLoader.cs:21
virtual string Texture
The file name of this item&#39;s texture file in the mod loader&#39;s file space.
Definition: ModItem.cs:69
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalRecipe instead of using Mod.AddGlobalRecipe. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name. Use this method to either force or stop an autoload, and to change the default internal name.
Definition: GlobalRecipe.cs:27
virtual void SetDefaults()
Allows you to modify the properties of any wall in the game. Most properties are stored as arrays thr...
Definition: GlobalWall.cs:36
This class serves as a place for you to place all your properties and hooks for each mount...
Definition: ModMountData.cs:9
This class allows you to modify and use hooks for all NPCs, including vanilla mobs. Create an instance of an overriding class then call Mod.AddGlobalNPC to use this.
Definition: GlobalNPC.cs:11
ushort Type
The internal ID of this type of tile.
Definition: ModTile.cs:33
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalWall instead of using Mod.AddGlobalWall. Return true to allo...
Definition: GlobalWall.cs:29
Represents a style of waterfalls that gets drawn. This is mostly used to determine the color of the w...
virtual string BossHeadTexture
This file name of this NPC&#39;s boss head texture file, to be used in autoloading.
Definition: ModNPC.cs:65
void AutoloadGlobalBgStyle(Type type)
int Type
The ID of this waterfall style.
MountTextureType
This is an enum of all possible types of extra mount textures for custom mounts. Use these as keys in...
This class serves as a place for you to define a new buff and how that buff behaves.
Definition: ModBuff.cs:6
void AutoloadGores(IList< Type > modGores)
SoundType
This is an enum of the types of sound you can add to the game. This is used for determining whether a...
Definition: SoundType.cs:12
virtual bool Autoload(ref string name)
Allows you to automatically add a ModPlayer instead of using Mod.AddPlayer. Return true to allow auto...
Definition: ModPlayer.cs:67
virtual bool Autoload(ref string name)
Allows you to automatically load a projectile instead of using Mod.AddProjectile. Return true to allo...
void AutoloadProjectile(Type type)
void AutoloadLocalization()
Loads .lang files
void AutoloadUgBgStyle(Type type)
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalItem instead of using Mod.AddGlobalItem. Return true to allo...
Definition: GlobalItem.cs:39
void AutoloadBuff(Type type)
virtual string Name
Stores the name of the mod. This name serves as the mod&#39;s identification, and also helps with saving ...
Definition: Mod.cs:41
This serves as the central class from which NPC-related functions are carried out. It also stores a list of mod NPCs by ID.
Definition: NPCLoader.cs:19
Mod mod
The mod that added this type of ModWorld.
Definition: ModWorld.cs:16
This class serves as a place for you to place all your properties and hooks for each projectile...
string chest
The default name of this chest that is displayed when this 2x2 chest is open. Defaults to the empty s...
Definition: ModTile.cs:94
A ModWorld instance represents an extension of a World. You can store fields in the ModWorld classes ...
Definition: ModWorld.cs:11
Mount.MountData mountData
The vanilla MountData object that is controlled by this ModMountData.
Definition: ModMountData.cs:16
Mod mod
The mod to which this GlobalProjectile belongs.
This class represents a chat or console command. Use the CommandType to specify the scope of the comm...
Definition: ModCommand.cs:31
This class allows you to modify the behavior of any buff in the game.
Definition: GlobalBuff.cs:10
virtual bool Autoload(ref string name, ref string texture)
Allows you to automatically add a ModWaterfallStyle instead of using Mod.AddWaterfallStyle. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name, and texture is initialized to the namespace and overriding class name with periods replaced with slashes. Use this to either force or stop an autoload, change the name that identifies this type of ModWaterStyle, or change the texture path used by this ModWaterfallStyle.
virtual void AutoStaticDefaults()
Automatically sets certain static defaults. Override this if you do not want the properties to be set...
This class provides hooks that control all recipes in the game.
Definition: GlobalRecipe.cs:6
This class serves as a place for you to place all your properties and hooks for each item...
Definition: ModItem.cs:16
void AutoloadPlayer(Type type)
virtual bool Autoload(ref string name)
Allows you to automatically load a tile entity instead of using Mod.AddTileEntity. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name. Use this method to either force or stop an autoload, or change the default display name.
This class represents a type of dust that is added by a mod. Only one instance of this class will eve...
Definition: ModDust.cs:13
void AutoloadGlobalRecipe(Type type)
Mod mod
The mod which added this GlobalRecipe.
Definition: GlobalRecipe.cs:11
virtual void SetDefaults()
Allows you to set the properties of this wall. Many properties are stored as arrays throughout Terrar...
Definition: ModWall.cs:130
int Type
The buff id of this buff.
Definition: ModBuff.cs:27
Mod mod
The mod that added this ModTileEntity.
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalProjectile instead of using Mod.AddGlobalProjectile. Return true to allow autoloading; by default returns the mod&#39;s autoload property. Name is initialized to the overriding class name. Use this method to either force or stop an autoload or to control the internal name.
Mod mod
The mod that added this surface background style.
Mod mod
The mod that added this underground background style.
void AutoloadWall(Type type)
virtual void SetStaticDefaults()
Allows you to set all your projectile&#39;s static properties, such as names/translations and the arrays ...
virtual void AutoDefaults()
Definition: ModPrefix.cs:130
This class allows you to modify and use hooks for all projectiles, including vanilla projectiles...
string Name
The internal name of this ModItem.
Definition: ModItem.cs:45
This serves as the central place from which mounts are stored and mount-related functions are carried...
Definition: MountLoader.cs:11
Mod mod
The mod to which this GlobalBuff belongs.
Definition: GlobalBuff.cs:15
int Type
The ID of the water style.
Each background style determines in its own way how exactly the background is drawn. This class serves as a collection of functions for underground backgrounds.
void AutoloadPrefix(Type type)
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalTile instead of using Mod.AddGlobalTile. Return true to allo...
Definition: GlobalTile.cs:70
static Task< Texture2D > PngToTexture2DAsync(GraphicsDevice graphicsDevice, Stream stream)
Definition: ImageIO.cs:103
virtual bool Autoload(ref string name, ref string texture)
Allows you to modify the name and texture path of this tile when it is autoloaded. Return true to autoload this tile. When a tile is autoloaded, that means you do not need to manually call Mod.AddTile. By default returns the mod&#39;s autoload property.
Definition: ModTile.cs:222
static void Convert(float[] floatBuf, byte[] buffer)