Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
AssetLoader.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using Loot.Api.ModContent;
6 using Microsoft.Xna.Framework.Graphics;
7 using Terraria.ModLoader;
8 
9 namespace Loot.Api.Loaders
10 {
15  public static class AssetLoader
16  {
22  public static void RegisterAssets(Mod mod, string folder, bool clearOwnTextures = true)
23  {
24  RegistryLoader.CheckModLoading(mod, "RegisterAssets");
25  if (!RegistryLoader.CheckModRegistered(mod, "RegisterAssets", false))
26  {
27  throw new NullReferenceException($"Mod {mod.Name} must be registered before calling RegisterAssets");
28  }
29 
30  if (Loot.ModContentManager == null)
31  {
32  throw new NullReferenceException("Loot.ContentManager is null in RegisterAssets");
33  }
34 
35  var graphicsContent = Loot.ModContentManager.GetContent<GraphicsModContent>();
36  if (graphicsContent == null)
37  {
38  throw new NullReferenceException("ModGraphicsContent is null in RegisterAssets");
39  }
40 
41  if (folder.StartsWith($"{mod.Name}/"))
42  {
43  folder = folder.Replace($"{mod.Name}/", "");
44  }
45 
46  string keyPass = $"{mod.Name}/{folder}";
47  graphicsContent.AddKeyPass(mod.Name, keyPass);
48 
49  FieldInfo texturesField = typeof(Mod).GetField("textures", BindingFlags.Instance | BindingFlags.NonPublic);
50  Dictionary<string, Texture2D> dictionary = ((Dictionary<string, Texture2D>) texturesField?.GetValue(mod));
51  if (dictionary == null)
52  {
53  throw new NullReferenceException($"textures dictionary for mod {mod.Name} was null");
54  }
55 
56  var textures = dictionary.Where(x => x.Key.StartsWith(folder)).ToList();
57  var glowmasks = textures.Where(x => x.Key.EndsWith("_Glowmask") || x.Key.EndsWith("_Glow")).ToList();
58  var shaders = textures.Where(x => x.Key.EndsWith("_Shader") || x.Key.EndsWith("_Shad")).ToList();
59 
60  foreach (var kvp in glowmasks)
61  {
62  string assetKey = graphicsContent.GetAssetKey(kvp.Value.Name, mod);
63  if (assetKey == null)
64  {
65  continue;
66  }
67 
68  if (graphicsContent.AnyGlowmaskAssetExists(assetKey, mod))
69  {
70  throw new Exception($"{mod.Name} attempted to add a glowmask asset already present: {assetKey}");
71  }
72 
73  graphicsContent.AddGlowmaskTexture(assetKey, kvp.Value);
74  if (clearOwnTextures)
75  {
76  dictionary.Remove(kvp.Key);
77  }
78  }
79 
80  foreach (var kvp in shaders)
81  {
82  string assetKey = graphicsContent.GetAssetKey(kvp.Value.Name, mod);
83  if (assetKey == null)
84  {
85  continue;
86  }
87 
88  if (graphicsContent.AnyShaderAssetExists(assetKey, mod))
89  {
90  throw new Exception($"{mod.Name} attempted to add a shader asset already present: {assetKey}");
91  }
92 
93  graphicsContent.AddShaderTexture(assetKey, kvp.Value);
94  if (clearOwnTextures)
95  {
96  dictionary.Remove(kvp.Key);
97  }
98  }
99 
100  // sanity check
101  // prepare throws an exception if keyPass not registered in keyStore correctly
102  graphicsContent.Prepare(mod);
103  graphicsContent.ClearPreparation();
104  }
105  }
106 }
static void RegisterAssets(Mod mod, string folder, bool clearOwnTextures=true)
Registers the assets folder for the given mod
Definition: AssetLoader.cs:22
This class stores glowmask and shader textures that can be looked up during drawing ...
This Loader is responsible for loading graphics assets for a mod These include shader and glowmask te...
Definition: AssetLoader.cs:15
This class is responsible for loading mods into Even More Modifiers Mods can be registered and trigge...