-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShaderLoader.cs
298 lines (249 loc) · 10.8 KB
/
ShaderLoader.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using ShaderLib.System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using System.Reflection;
using Terraria.DataStructures;
using Terraria.Graphics.Shaders;
using Terraria.ModLoader;
namespace ShaderLib
{
public static class ShaderLoader
{
internal static IDictionary<string, Assembly> Mods;
internal static IDictionary<string, IDictionary<string, ModArmorShaderData>> ModShaders;
internal static IDictionary<int, ModArmorShaderData> ModShadersByID;
internal static IDictionary<string, IDictionary<string, GlobalShader>> GlobalShaders;
private static int itemID;
internal static void Initialize() {
itemID = int.MinValue;
Mods = new ConcurrentDictionary<string, Assembly>();
ModShaders = new Dictionary<string, IDictionary<string, ModArmorShaderData>>();
ModShadersByID = new Dictionary<int, ModArmorShaderData>();
GlobalShaders = new Dictionary<string, IDictionary<string, GlobalShader>>();
}
internal static void Unload() {
itemID = int.MinValue;
Mods = null;
ModShaders = null;
ModShadersByID = null;
GlobalShaders = null;
}
internal static void SetupContent() {
if(Main.netMode == Terraria.ID.NetmodeID.Server) return;
foreach(var kvp in Mods) {
var ordered = kvp.Value
.GetTypes()
.OrderBy(x => x.FullName, StringComparer.InvariantCulture)
.Where(t => t.IsClass && !t.IsAbstract); /* || type.GetConstructor(new Type[0]) == null*/
var globalShaders = ordered.Where(x => x.IsSubclassOf(typeof(GlobalShader)));
var modShaderData = ordered.Where(x => x.IsSubclassOf(typeof(ModArmorShaderData)));
foreach(Type type in globalShaders) {
AutoloadGlobalShader(type, ModLoader.GetMod(kvp.Key));
}
foreach(Type type in modShaderData) {
AutoloadModArmorShaderData(type, ModLoader.GetMod(kvp.Key));
}
}
//ErrorLogger.ClearLog();
//ErrorLogger.Log(string.Join("\n", Rarities.Select(r => r.Value.Name)));
//ErrorLogger.Log(string.Join("\n", Effects.Select(e => e.Value.Description)));
}
/// <summary>
/// Registers the given mod as a mod to support loading of this library's classes.
/// </summary>
/// <param name="mod"></param>
public static void RegisterMod(Mod mod) {
if(Main.netMode == Terraria.ID.NetmodeID.Server) return;
if(Mods == null) Initialize();
bool? b = mod.GetType().GetField("loading", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(mod) as bool?;
if(b != null && !b.Value) {
throw new Exception("RegisterMod can only be called from Mod.Load or Mod.Autoload");
}
if(Mods.ContainsKey(mod.Name)) {
throw new Exception($"Mod {mod.Name} is already registered");
}
Mods.Add(mod.Name, mod.Code);
ModShaders.Add(mod.Name, new Dictionary<string, ModArmorShaderData>());
GlobalShaders.Add(mod.Name, new Dictionary<string, GlobalShader>());
}
private static void AutoloadGlobalShader(Type type, Mod mod) {
var shader = (GlobalShader)Activator.CreateInstance(type);
if(shader.Autoload()) AddGlobalShader(shader, mod);
}
/// <summary>
/// Adds the given GlobalShader.
/// </summary>
/// <param name="shader"></param>
/// <param name="mod"></param>
public static void AddGlobalShader(GlobalShader shader, Mod mod) {
if(Main.netMode == Terraria.ID.NetmodeID.Server) return;
//bool? b = mod.GetType().GetField("loading", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(mod) as bool?;
//if (b != null && !b.Value)
// throw new Exception("AddGlobalShader can only be called from Mod.Load or Mod.Autoload");
if(!Mods.ContainsKey(mod.Name))
throw new Exception($"Mod {mod.Name} is not registered, please register before adding");
if(GlobalShaders[mod.Name].ContainsKey(shader.Name))
throw new Exception($"You have already added a GlobalShader with the name {shader.Name}");
shader.Mod = mod;
GlobalShaders[mod.Name].Add(shader.Name, shader);
}
private static void AutoloadModArmorShaderData(Type type, Mod mod) {
var shader = (ModArmorShaderData)Activator.CreateInstance(type);
shader.Mod = mod;
if(shader.Autoload()) AddModArmorShaderData(shader, mod, shader.BoundItemID);
}
/// <summary>
/// Adds and binds the given ModArmorShaderData.
/// Optional item ID may be given to bind the shader to (default starts at minimum value).
/// Returns the shader's given shader ID.
/// </summary>
/// <param name="shader"></param>
/// <param name="mod"></param>
/// <param name="item"></param>
public static ShaderID AddModArmorShaderData(ModArmorShaderData shader, Mod mod, int? item = null) {
if(Main.netMode == Terraria.ID.NetmodeID.Server)
throw new Exception("Cannot load shaders on server");
if(!Mods.ContainsKey(mod.Name))
throw new Exception($"Mod {mod.Name} is not registered, please register before adding");
if(ModShaders[mod.Name].ContainsKey(shader.Name))
throw new Exception($"You have already added a ModArmorShaderData with the name {shader.Name}");
if(item == null) item = itemID++;
shader.Mod = mod;
ModShaders[mod.Name].Add(shader.Name, shader);
GameShaders.Armor.BindShader(item.Value, shader);
shader.ShaderID = new ShaderID(mod.Name, shader.Name, GameShaders.Armor.GetShaderIdFromItemId(item.Value));
ModShadersByID[shader.ShaderID.ID] = shader;
return shader.ShaderID;
}
public static T GetModShader<T>(string modName, string name) where T : ModArmorShaderData {
return (T)GetModShader(modName, name);
}
public static T GetModShader<T>(Mod mod, string name) where T : ModArmorShaderData {
return GetModShader<T>(mod.Name, name);
}
public static T GetModShader<T>(int shaderID) where T : ModArmorShaderData {
return (T)ModShadersByID[shaderID];
}
public static T GetModShader<T>(Mod mod) where T : ModArmorShaderData {
return GetModShader<T>(mod.Name, typeof(T).Name);
}
public static ModArmorShaderData GetModShader(Mod mod, string name) {
return GetModShader<ModArmorShaderData>(mod.Name, name);
}
public static ModArmorShaderData GetModShader(string modName, string name) {
return ModShaders[modName][name];
}
public static ModArmorShaderData GetModShader(ShaderID id) {
return GetModShader(id.ModName, id.ShaderName);
}
public static T GetGlobalShader<T>(string modName, string name) where T : GlobalShader {
return (T)GlobalShaders[modName][name];
}
public static T GetGlobalShader<T>(Mod mod, string name) where T : GlobalShader {
return GetGlobalShader<T>(mod.Name, name);
}
public static T GetGlobalShader<T>(Mod mod) where T : GlobalShader {
return GetGlobalShader<T>(mod.Name, typeof(T).Name);
}
public static ShaderID GetShaderID(Item item) {
if(item.modItem != null && item.modItem as IDye != null) return (item.modItem as IDye).DyeID;
return new ShaderID(GameShaders.Armor.GetShaderIdFromItemId(item.type));
}
public static int GetShaderIDNum(Item item) {
return GetShaderID(item).ID;
}
public static void ItemInventoryShader(Item item, SpriteBatch spriteBatch, Vector2 position, Rectangle frame, Color drawColor, Color itemColor, Vector2 origin, float scale) {
foreach(var mod in GlobalShaders.Values) {
foreach(var shader in mod.Values) {
int shaderID = shader.ItemInventoryShader(item, spriteBatch, position, frame, drawColor, itemColor, origin, scale);
if(shaderID > 0) {
spriteBatch.Restart(Main.UIScaleMatrix, worldDraw: false);
DrawData data = new DrawData
{
position = position - Main.screenPosition,
scale = new Vector2(scale, scale),
sourceRect = frame,
texture = Main.itemTexture[item.type]
};
GameShaders.Armor.ApplySecondary(shaderID, Main.player[item.owner], data);
break;
}
}
}
}
public static void ItemWorldShader(Item item, SpriteBatch spriteBatch, Color lightColor, Color alphaColor, ref float rotation, ref float scale, int whoAmI) {
foreach(var mod in GlobalShaders.Values) {
foreach(var shader in mod.Values) {
int shaderID = shader.ItemWorldShader(item, spriteBatch, lightColor, alphaColor, ref rotation, ref scale, whoAmI);
if(shaderID > 0) {
spriteBatch.Restart(Main.LocalPlayer.gravDir == 1f? Main.GameViewMatrix.ZoomMatrix : Main.GameViewMatrix.TransformationMatrix);
DrawData data = new DrawData
{
position = item.position - Main.screenPosition,
scale = new Vector2(scale, scale),
texture = Main.itemTexture[item.type],
rotation = rotation
};
GameShaders.Armor.ApplySecondary(shaderID, Main.player[item.owner], data);
}
}
}
}
public static void ProjectileShader(Projectile projectile, SpriteBatch spriteBatch, Color lightColor) {
foreach(var mod in GlobalShaders.Values) {
foreach(var shader in mod.Values) {
int shaderID = shader.ProjectileShader(projectile, spriteBatch, lightColor);
if(shaderID > 0) {
spriteBatch.Restart(Main.LocalPlayer.gravDir == 1f ? Main.GameViewMatrix.ZoomMatrix : Main.GameViewMatrix.TransformationMatrix);
DrawData data = new DrawData
{
position = projectile.position - Main.screenPosition,
scale = new Vector2(projectile.scale, projectile.scale),
texture = Main.projectileTexture[projectile.type],
rotation = projectile.rotation
};
data.sourceRect = data.texture.Frame(1, Main.projFrames[projectile.type], 0, projectile.frame);
GameShaders.Armor.ApplySecondary(shaderID, Main.player[projectile.owner], data);
}
}
}
}
public static void NPCShader(NPC npc, SpriteBatch spriteBatch, Color drawColor) {
foreach(var mod in GlobalShaders.Values) {
foreach(var shader in mod.Values) {
int shaderID = shader.NPCShader(npc, spriteBatch, drawColor);
if(shaderID > 0) {
spriteBatch.Restart(Main.LocalPlayer.gravDir == 1f ? Main.GameViewMatrix.ZoomMatrix : Main.GameViewMatrix.TransformationMatrix);
DrawData data = new DrawData
{
position = npc.position - Main.screenPosition,
scale = new Vector2(npc.scale, npc.scale),
texture = Main.npcTexture[npc.type],
sourceRect = npc.frame,//data.texture.Frame(1, Main.npcFrameCount[npc.type], 0, npc.frame);
rotation = npc.rotation
};
GameShaders.Armor.ApplySecondary(shaderID, npc, data);
}
}
}
}
public static PlayerShaderData PlayerShader(PlayerDrawInfo drawInfo) {
var data = new PlayerShaderData(0);
foreach(var mod in GlobalShaders.Values)
foreach(var shader in mod.Values)
shader.PlayerShader(ref data, drawInfo);
return data;
}
public static void HeldItemShader(out int shaderID, Item item, PlayerDrawInfo drawInfo) {
shaderID = 0;
foreach(var mod in GlobalShaders.Values)
foreach(var shader in mod.Values)
shader.HeldItemShader(ref shaderID, item, drawInfo);
}
}
}