-
Notifications
You must be signed in to change notification settings - Fork 4
/
TextureCache.cs
113 lines (100 loc) · 4.51 KB
/
TextureCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using Blish_HUD;
using Blish_HUD.Modules.Managers;
using Manlaan.Mounts.Things;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Manlaan.Mounts
{
public class TextureCache
{
private readonly ContentsManager contentsManager;
private readonly Dictionary<string, Texture2D> _textureCache = new Dictionary<string, Texture2D>();
public static readonly string MouseTextureName = "255329.png";
public static readonly string ModuleLogoTextureName = "514394-grey-plus-plus100.png";
public static readonly string TabBackgroundTextureName = "156006-big.png";
public static readonly string SettingsTextureName = "155052.png";
public static readonly string RadialSettingsTextureName = "1130623-32.png";
public static readonly string IconSettingsTextureName = "2208345.png";
public static readonly string SupportMeTabTextureName = "156127-32-grey.png";
public static readonly string KofiTextureName = "kofi-small.png";
public static readonly string AnetIconTextureName = "1441452.png";
public static readonly string InCombatTextureName = "1636746-cropped.png";
public static readonly string RangeIndicatorTextureName = "157133.png";
public static readonly string LaterActivationTextureName = "496252.png";
public TextureCache(ContentsManager contentsManager)
{
this.contentsManager = contentsManager;
PreCacheTextures();
}
private void PreCacheTextures()
{
Func<string, Texture2D> getTextureFromRef = (textureName) => contentsManager.GetTexture(textureName);
foreach (var mountImageFile in Module._thingImageFiles)
{
PreCacheTexture(mountImageFile.Name, PremultiplyTexture);
}
PreCacheTexture(MouseTextureName, getTextureFromRef);
PreCacheTexture(ModuleLogoTextureName, getTextureFromRef);
PreCacheTexture(TabBackgroundTextureName, getTextureFromRef);
PreCacheTexture(SettingsTextureName, getTextureFromRef);
PreCacheTexture(RadialSettingsTextureName, getTextureFromRef);
PreCacheTexture(IconSettingsTextureName, getTextureFromRef);
PreCacheTexture(SupportMeTabTextureName, getTextureFromRef);
PreCacheTexture(KofiTextureName, getTextureFromRef);
PreCacheTexture(AnetIconTextureName, getTextureFromRef);
PreCacheTexture(InCombatTextureName, getTextureFromRef);
PreCacheTexture(RangeIndicatorTextureName, getTextureFromRef);
PreCacheTexture(LaterActivationTextureName, getTextureFromRef);
}
private Texture2D PremultiplyTexture(string textureName)
{
Texture2D texture;
try
{
var filePath = Path.Combine(Module.thingsDirectory, textureName);
using (FileStream titleStream = File.OpenRead(filePath))
using (var gdc = GameService.Graphics.LendGraphicsDeviceContext())
{
texture = Texture2D.FromStream(gdc.GraphicsDevice, titleStream);
titleStream.Close();
Color[] buffer = new Color[texture.Width * texture.Height];
texture.GetData(buffer);
for (int i = 0; i < buffer.Length; i++)
buffer[i] = Color.FromNonPremultiplied(buffer[i].R, buffer[i].G, buffer[i].B, buffer[i].A);
texture.SetData(buffer);
}
}
catch
{
texture = ContentService.Textures.Error;
}
return texture;
}
private void PreCacheTexture(string textureName, Func<string, Texture2D> getTextureAction)
{
if (!_textureCache.ContainsKey(textureName))
{
_textureCache[textureName] = getTextureAction(textureName);
}
}
public Texture2D GetImgFile(string filename)
{
return GetTexture(filename);
}
public Texture2D GetThingImgFile(Thing thing)
{
return GetTexture(thing.ImageFileNameSetting.Value);
}
private Texture2D GetTexture (string filename)
{
if (_textureCache.ContainsKey(filename))
{
return _textureCache[filename];
}
return ContentService.Textures.Error;
}
}
}