2 using System.Collections.Generic;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
10 namespace Loot.Api.ModContent
28 _glowmaskTextures =
new Dictionary<string, Texture2D>();
29 _shaderTextures =
new Dictionary<string, Texture2D>();
31 _keyStore =
new Dictionary<string, string>();
33 _lookupTable =
new Dictionary<string, Texture2D>();
34 _lookupGlowmaskTable =
new Dictionary<string, Texture2D>();
35 _lookupShaderTable =
new Dictionary<string, Texture2D>();
40 _glowmaskTextures = null;
41 _shaderTextures = null;
46 _lookupGlowmaskTable = null;
47 _lookupShaderTable = null;
55 internal void AddKeyPass(
string key,
string keyPass)
57 if (!_keyStore.ContainsKey(key))
59 _keyStore.Add(key, keyPass);
67 _vanillaNamesCache =
new Dictionary<string, int>();
68 for (
int i = 1; i < ItemID.Count; i++)
70 Item item =
new Item();
72 if (_vanillaNamesCache.ContainsKey(item.Name))
74 Loot.Logger.Error(
"There was a problem during initialization of vanilla names cache");
77 _vanillaNamesCache.Add($
"{item.Name.Replace(" ", "")}_{i}", i);
81 internal string GetAssetKey(
string key,
Mod mod)
83 if (_vanillaNamesCache == null)
85 FillVanillaNamesCache();
89 if (key.Contains(
'/'))
91 key = key.Substring(key.LastIndexOf(
'/') + 1);
95 if (
char.IsLetter(key.First()))
97 int index = key.LastIndexOf(
'_');
98 key =
new string(key.TakeWhile((x, i) => i < index).ToArray());
100 Item item = mod.GetItem(key)?.item;
103 if (!_vanillaNamesCache.Keys.Any(x => x.StartsWith(key.Replace(
" ",
""))))
109 item.SetDefaults(_vanillaNamesCache[_vanillaNamesCache.Keys.First(x => x.StartsWith(key.Replace(
" ",
"")))]);
112 return kvpName.Replace(itemName, item.type.ToString());
116 if (
char.IsNumber(key.First()))
118 int index = key.LastIndexOf(
'_');
119 string itemId =
new string(key.TakeWhile((x, i) => i < index).ToArray());
120 if (
int.Parse(itemId) >= ItemLoader.ItemCount)
return null;
127 internal bool AnyGlowmaskAssetExists(
string key,
Mod mod)
130 return _glowmaskTextures.Any(x => x.Key.StartsWith(mod.Name) && x.Key.Contains($
"{key}_Glowmask") || x.Key.Contains($
"{key}_Glow"));
133 internal bool AnyShaderAssetExists(
string key,
Mod mod)
135 key = GetItemKey(key);
136 return _shaderTextures.Any(x => x.Key.StartsWith(mod.Name) && x.Key.Contains($
"{key}_Shader") || x.Key.Contains($
"{key}_Shad"));
141 if (key.Contains(
'/'))
143 key = key.Substring(key.LastIndexOf(
'/') + 1);
146 int index = key.LastIndexOf(
'_');
147 return new string(key.TakeWhile((x, i) => i < index).ToArray());
153 if (_keyStore.TryGetValue(mod.Name, out var keyPass))
155 var glowmasks = _glowmaskTextures.Where(x => x.Key.StartsWith(keyPass)).ToArray();
156 var shaders = _glowmaskTextures.Where(x => x.Key.StartsWith(keyPass)).ToArray();
159 _textures.Where(x => x.Key.StartsWith(keyPass))
162 .ToDictionary(x => SubstringLastIndex(
'/', x.Key), x => x.Value);
164 _lookupGlowmaskTable = glowmasks.ToDictionary(x => SubstringLastIndex(
'/', x.Key), x => x.Value);
165 _lookupShaderTable = shaders.ToDictionary(x => SubstringLastIndex(
'/', x.Key), x => x.Value);
169 throw new Exception($
"Tried to get keyPass from keyStore for mod {mod.Name} but key not present");
175 string itemId = item.type.ToString();
176 var glowmasks = _glowmaskTextures.Where(x => GetItemKey(x.Key).Equals(itemId)).ToArray();
177 var shaders = _shaderTextures.Where(x => GetItemKey(x.Key).Equals(itemId)).ToArray();
180 _textures.Where(x => GetItemKey(x.Key).Equals(itemId))
183 .ToDictionary(x => SubstringLastIndex(
'/', x.Key), x => x.Value);
185 _lookupGlowmaskTable = glowmasks.ToDictionary(x => SubstringLastIndex(
'/', x.Key), x => x.Value);
186 _lookupShaderTable = shaders.ToDictionary(x => SubstringLastIndex(
'/', x.Key), x => x.Value);
191 _lookupTable.Clear();
192 _lookupGlowmaskTable.Clear();
193 _lookupShaderTable.Clear();
198 return GetPreparedAsset(_lookupTable, key, clearPreparation);
203 Texture2D glowmask = GetPreparedAsset(_lookupGlowmaskTable, $
"{key}_Glowmask", clearPreparation);
204 if (glowmask != null)
return glowmask;
205 return GetPreparedAsset(_lookupGlowmaskTable, $
"{key}_Glow", clearPreparation);
210 Texture2D shader = GetPreparedAsset(_lookupShaderTable, $
"{key}_Shader", clearPreparation);
211 if (shader != null)
return shader;
212 return GetPreparedAsset(_lookupShaderTable, $
"{key}_Shad", clearPreparation);
215 private Texture2D
GetPreparedAsset(IDictionary<string, Texture2D> dict,
string key,
bool clearPreparation =
true)
217 Texture2D texture = GetFrom(dict, key);
218 if (clearPreparation && texture != null)
228 return GetFrom(_glowmaskTextures, key);
233 AddTo(_glowmaskTextures, key, texture);
238 return GetFrom(_shaderTextures, key);
243 AddTo(_shaderTextures, key, texture);
248 texture = MultiplyColorsByAlpha(texture);
260 Color[] data =
new Color[texture.Width * texture.Height];
261 texture.GetData(data);
262 for (
int i = 0; i < data.Length; i++)
264 Vector4 we = data[i].ToVector4();
265 data[i] =
new Color(we.X * we.W, we.Y * we.W, we.Z * we.W, we.W);
268 texture.SetData(data);
274 int index = str.LastIndexOf(chr);
277 return str.Substring(index + 1);
IDictionary< string, Texture2D > _lookupGlowmaskTable
IDictionary< string, Texture2D > _lookupShaderTable
Texture2D GetShaderTexture(string key)
void FillVanillaNamesCache()
void AddShaderTexture(string key, Texture2D texture)
IDictionary< string, Texture2D > _lookupTable
void AddGlowmaskTexture(string key, Texture2D texture)
IDictionary< string, int > _vanillaNamesCache
string GetItemKey(string key)
Texture2D GetPreparedAsset(IDictionary< string, Texture2D > dict, string key, bool clearPreparation=true)
Texture2D GetPreparedShader(string key, bool clearPreparation=true)
override string GetRegistryKey()
IDictionary< string, Texture2D > _shaderTextures
static Texture2D MultiplyColorsByAlpha(Texture2D texture)
override void Initialize()
override void ProcessTexture2D(ref Texture2D texture)
An abstract class that contains a mapping of strings to texture instances
Texture2D GetGlowmaskTexture(string key)
Texture2D GetPreparedTexture(string key, bool clearPreparation=true)
IDictionary< string, Texture2D > _glowmaskTextures
IDictionary< string, string > _keyStore
This class stores glowmask and shader textures that can be looked up during drawing ...
Texture2D GetPreparedGlowmask(string key, bool clearPreparation=true)
string SubstringLastIndex(char chr, string str)