-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlocksManager.cs
453 lines (427 loc) · 18.5 KB
/
BlocksManager.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using Engine;
using Engine.Graphics;
using Engine.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Game
{
public static class BlocksManager
{
private static Block[] m_blocks;
private static FluidBlock[] m_fluidBlocks;
private static readonly List<string> m_categories = new List<string>();
private static readonly DrawBlockEnvironmentData m_defaultEnvironmentData = new DrawBlockEnvironmentData();
private static readonly Vector4[] m_slotTexCoords = new Vector4[256];
public static Block[] Blocks
{ get { return m_blocks; } }
public static FluidBlock[] FluidBlocks
{ get { return m_fluidBlocks; } }
public static ReadOnlyList<string> Categories
{ get { return new ReadOnlyList<string>(m_categories); } }
public static void Initialize()
{
CalculateSlotTexCoordTables();
int i = 0;
var dictionary = new Dictionary<int, Block>();
foreach (var definedType in GetBlockTypes())
if (definedType.IsSubclassOf(typeof(Block)) && !definedType.IsAbstract)
{
var fieldInfo = definedType.AsType().GetRuntimeFields().FirstOrDefault(fi => {
if (fi.Name == "Index" && fi.IsPublic)
return fi.IsStatic;
return false;
});
if (fieldInfo != null && fieldInfo.FieldType == typeof(int))
{
var num = (int) fieldInfo.GetValue(null);
// Removed 'Index of block type \"{0}\" conflicts with another block' error
var instance = (Block) Activator.CreateInstance(definedType.AsType());
dictionary[instance.BlockIndex = num] = instance;
if (num > i)
i = num;
}
else
{
throw new InvalidOperationException(string.Format(
"Block type \"{0}\" does not have static field Index of type int.",
new object[1] {definedType.FullName}));
}
}
m_blocks = new Block[i + 1];
m_fluidBlocks = new FluidBlock[i + 1];
for (var e = dictionary.GetEnumerator(); e.MoveNext();)
{
var keyValuePair = e.Current;
m_blocks[keyValuePair.Key] = keyValuePair.Value;
m_fluidBlocks[keyValuePair.Key] = keyValuePair.Value as FluidBlock;
}
for (i = 0; i < m_blocks.Length; ++i)
if (m_blocks[i] == null)
m_blocks[i] = Blocks[0];
var data = ContentManager.Get<string>("BlocksData");
ContentManager.Dispose("BlocksData");
LoadBlocksData(data);
var enumerator = ModsManager.GetEntries(".csv").GetEnumerator();
while (enumerator.MoveNext())
{
var reader = new StreamReader(enumerator.Current.Stream);
try
{
LoadBlocksData(reader.ReadToEnd());
}
catch (Exception e)
{
ModsManager.ErrorHandler(enumerator.Current, e);
}
finally
{
reader.Dispose();
}
}
i = 0;
for (int length = Blocks.Length; i < length; i++)
Blocks[i].Initialize();
m_categories.Add("Terrain");
m_categories.Add("Plants");
m_categories.Add("Construction");
m_categories.Add("Items");
m_categories.Add("Tools");
m_categories.Add("Clothes");
m_categories.Add("Electrics");
m_categories.Add("Food");
m_categories.Add("Spawner Eggs");
m_categories.Add("Painted");
m_categories.Add("Dyed");
m_categories.Add("Fireworks");
for (i = 0; i < Blocks.Length; i++)
{
var block = Blocks[i];
for (var j = block.GetCreativeValues().GetEnumerator(); j.MoveNext();)
{
var category = block.GetCategory(j.Current);
if (!m_categories.Contains(category))
m_categories.Add(category);
}
}
}
public static IEnumerable<TypeInfo> GetBlockTypes()
{
var list = new List<TypeInfo>();
list.AddRange(typeof(BlocksManager).Assembly.DefinedTypes);
var enumerator = TypeCache.LoadedAssemblies.GetEnumerator();
while (enumerator.MoveNext())
list.AddRange(enumerator.Current.DefinedTypes);
return list;
}
public static Block FindBlockByTypeName(string typeName, bool throwIfNotFound)
{
var block = Blocks.FirstOrDefault(b => b.GetType().Name == typeName);
if ((block == null) & throwIfNotFound)
throw new InvalidOperationException(string.Format("Block with type {0} not found.",
new object[1] {typeName}));
return block;
}
public static Block[] FindBlocksByCraftingId(string craftingId)
{
return Blocks.Where(b => b.CraftingId == craftingId).ToArray();
}
public static void DrawCubeBlock(PrimitivesRenderer3D primitivesRenderer, int value, Vector3 size,
ref Matrix matrix, Color color, Color topColor, DrawBlockEnvironmentData environmentData)
{
environmentData = environmentData ?? m_defaultEnvironmentData;
var texture = environmentData.SubsystemTerrain != null
? environmentData.SubsystemTerrain.SubsystemAnimatedTextures.AnimatedBlocksTexture
: BlocksTexturesManager.DefaultBlocksTexture;
var texturedBatch3D = primitivesRenderer.TexturedBatch(texture, true, 0, null,
RasterizerState.CullCounterClockwiseScissor, null, SamplerState.PointClamp);
var s = LightingManager.LightIntensityByLightValue[environmentData.Light];
color = Color.MultiplyColorOnly(color, s);
topColor = Color.MultiplyColorOnly(topColor, s);
var translation = matrix.Translation;
var vector3_1 = matrix.Right * size.X;
var vector3_2 = matrix.Up * size.Y;
var vector3_3 = matrix.Forward * size.Z;
var result1 = translation + 0.5f * (-vector3_1 - vector3_2 - vector3_3);
var result2 = translation + 0.5f * (vector3_1 - vector3_2 - vector3_3);
var result3 = translation + 0.5f * (-vector3_1 + vector3_2 - vector3_3);
var result4 = translation + 0.5f * (vector3_1 + vector3_2 - vector3_3);
var result5 = translation + 0.5f * (-vector3_1 - vector3_2 + vector3_3);
var result6 = translation + 0.5f * (vector3_1 - vector3_2 + vector3_3);
var result7 = translation + 0.5f * (-vector3_1 + vector3_2 + vector3_3);
var result8 = translation + 0.5f * (vector3_1 + vector3_2 + vector3_3);
if (environmentData.ViewProjectionMatrix.HasValue)
{
var m = environmentData.ViewProjectionMatrix.Value;
Vector3.Transform(ref result1, ref m, out result1);
Vector3.Transform(ref result2, ref m, out result2);
Vector3.Transform(ref result3, ref m, out result3);
Vector3.Transform(ref result4, ref m, out result4);
Vector3.Transform(ref result5, ref m, out result5);
Vector3.Transform(ref result6, ref m, out result6);
Vector3.Transform(ref result7, ref m, out result7);
Vector3.Transform(ref result8, ref m, out result8);
}
var block = Blocks[Terrain.ExtractContents(value)];
var slotTexCoord1 = m_slotTexCoords[block.GetFaceTextureSlot(0, value)];
var color1 = Color.MultiplyColorOnly(color, LightingManager.CalculateLighting(-matrix.Forward));
texturedBatch3D.QueueQuad(result1, result3, result4, result2, new Vector2(slotTexCoord1.X, slotTexCoord1.W),
new Vector2(slotTexCoord1.X, slotTexCoord1.Y), new Vector2(slotTexCoord1.Z, slotTexCoord1.Y),
new Vector2(slotTexCoord1.Z, slotTexCoord1.W), color1);
var slotTexCoord2 = m_slotTexCoords[block.GetFaceTextureSlot(2, value)];
var color2 = Color.MultiplyColorOnly(color, LightingManager.CalculateLighting(matrix.Forward));
texturedBatch3D.QueueQuad(result5, result6, result8, result7, new Vector2(slotTexCoord2.Z, slotTexCoord2.W),
new Vector2(slotTexCoord2.X, slotTexCoord2.W), new Vector2(slotTexCoord2.X, slotTexCoord2.Y),
new Vector2(slotTexCoord2.Z, slotTexCoord2.Y), color2);
var slotTexCoord3 = m_slotTexCoords[block.GetFaceTextureSlot(5, value)];
var color3 = Color.MultiplyColorOnly(color, LightingManager.CalculateLighting(-matrix.Up));
texturedBatch3D.QueueQuad(result1, result2, result6, result5, new Vector2(slotTexCoord3.X, slotTexCoord3.Y),
new Vector2(slotTexCoord3.Z, slotTexCoord3.Y), new Vector2(slotTexCoord3.Z, slotTexCoord3.W),
new Vector2(slotTexCoord3.X, slotTexCoord3.W), color3);
var slotTexCoord4 = m_slotTexCoords[block.GetFaceTextureSlot(4, value)];
var color4 = Color.MultiplyColorOnly(topColor, LightingManager.CalculateLighting(matrix.Up));
texturedBatch3D.QueueQuad(result3, result7, result8, result4, new Vector2(slotTexCoord4.X, slotTexCoord4.W),
new Vector2(slotTexCoord4.X, slotTexCoord4.Y), new Vector2(slotTexCoord4.Z, slotTexCoord4.Y),
new Vector2(slotTexCoord4.Z, slotTexCoord4.W), color4);
var slotTexCoord5 = m_slotTexCoords[block.GetFaceTextureSlot(1, value)];
var color5 = Color.MultiplyColorOnly(color, LightingManager.CalculateLighting(-matrix.Right));
texturedBatch3D.QueueQuad(result1, result5, result7, result3, new Vector2(slotTexCoord5.Z, slotTexCoord5.W),
new Vector2(slotTexCoord5.X, slotTexCoord5.W), new Vector2(slotTexCoord5.X, slotTexCoord5.Y),
new Vector2(slotTexCoord5.Z, slotTexCoord5.Y), color5);
var slotTexCoord6 = m_slotTexCoords[block.GetFaceTextureSlot(3, value)];
var color6 = Color.MultiplyColorOnly(color, LightingManager.CalculateLighting(matrix.Right));
texturedBatch3D.QueueQuad(result2, result4, result8, result6, new Vector2(slotTexCoord6.X, slotTexCoord6.W),
new Vector2(slotTexCoord6.X, slotTexCoord6.Y), new Vector2(slotTexCoord6.Z, slotTexCoord6.Y),
new Vector2(slotTexCoord6.Z, slotTexCoord6.W), color6);
}
public static void DrawFlatBlock(PrimitivesRenderer3D primitivesRenderer, int value, float size,
ref Matrix matrix, Texture2D texture, Color color, bool isEmissive,
DrawBlockEnvironmentData environmentData)
{
environmentData = environmentData ?? m_defaultEnvironmentData;
if (!isEmissive)
color = Color.MultiplyColorOnly(color, LightingManager.LightIntensityByLightValue[environmentData.Light]);
var translation = matrix.Translation;
Vector3 v2;
Vector3 vector3;
if (environmentData.BillboardDirection.HasValue)
{
v2 = Vector3.Normalize(Vector3.Cross(environmentData.BillboardDirection.Value, Vector3.UnitY));
vector3 = -Vector3.Normalize(Vector3.Cross(environmentData.BillboardDirection.Value, v2));
}
else
{
v2 = matrix.Right;
vector3 = matrix.Up;
}
var result1 = translation + 0.85f * size * (-v2 - vector3);
var result2 = translation + 0.85f * size * (v2 - vector3);
var result3 = translation + 0.85f * size * (-v2 + vector3);
var result4 = translation + 0.85f * size * (v2 + vector3);
if (environmentData.ViewProjectionMatrix.HasValue)
{
var m = environmentData.ViewProjectionMatrix.Value;
Vector3.Transform(ref result1, ref m, out result1);
Vector3.Transform(ref result2, ref m, out result2);
Vector3.Transform(ref result3, ref m, out result3);
Vector3.Transform(ref result4, ref m, out result4);
}
var block = Blocks[Terrain.ExtractContents(value)];
Vector4 vector4;
if (texture == null)
{
texture = environmentData.SubsystemTerrain != null
? environmentData.SubsystemTerrain.SubsystemAnimatedTextures.AnimatedBlocksTexture
: BlocksTexturesManager.DefaultBlocksTexture;
vector4 = m_slotTexCoords[block.GetFaceTextureSlot(-1, value)];
}
else
{
vector4 = new Vector4(0.0f, 0.0f, 1f, 1f);
}
var texturedBatch3D = primitivesRenderer.TexturedBatch(texture, true, 0, null,
RasterizerState.CullCounterClockwiseScissor, null, SamplerState.PointClamp);
texturedBatch3D.QueueQuad(result1, result3, result4, result2, new Vector2(vector4.X, vector4.W),
new Vector2(vector4.X, vector4.Y), new Vector2(vector4.Z, vector4.Y), new Vector2(vector4.Z, vector4.W),
color);
if (environmentData.BillboardDirection.HasValue)
return;
texturedBatch3D.QueueQuad(result1, result2, result4, result3, new Vector2(vector4.X, vector4.W),
new Vector2(vector4.Z, vector4.W), new Vector2(vector4.Z, vector4.Y), new Vector2(vector4.X, vector4.Y),
color);
}
public static void DrawMeshBlock(PrimitivesRenderer3D primitivesRenderer, BlockMesh blockMesh, float size,
ref Matrix matrix, DrawBlockEnvironmentData environmentData)
{
environmentData = environmentData ?? m_defaultEnvironmentData;
var texture = environmentData.SubsystemTerrain != null
? environmentData.SubsystemTerrain.SubsystemAnimatedTextures.AnimatedBlocksTexture
: BlocksTexturesManager.DefaultBlocksTexture;
DrawMeshBlock(primitivesRenderer, blockMesh, texture, Color.White, size, ref matrix, environmentData);
}
public static void DrawMeshBlock(PrimitivesRenderer3D primitivesRenderer, BlockMesh blockMesh, Color color,
float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
{
environmentData = environmentData ?? m_defaultEnvironmentData;
var texture = environmentData.SubsystemTerrain != null
? environmentData.SubsystemTerrain.SubsystemAnimatedTextures.AnimatedBlocksTexture
: BlocksTexturesManager.DefaultBlocksTexture;
DrawMeshBlock(primitivesRenderer, blockMesh, texture, color, size, ref matrix, environmentData);
}
public static void DrawMeshBlock(PrimitivesRenderer3D primitivesRenderer, BlockMesh blockMesh,
Texture2D texture, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
{
environmentData = environmentData ?? m_defaultEnvironmentData;
var num1 = LightingManager.LightIntensityByLightValue[environmentData.Light];
var vector4 = new Vector4(color);
vector4.X *= num1;
vector4.Y *= num1;
vector4.Z *= num1;
var flag1 = vector4 == Vector4.One;
var texturedBatch3D = primitivesRenderer.TexturedBatch(texture, true, 0, null,
RasterizerState.CullCounterClockwiseScissor, null, SamplerState.PointClamp);
var flag2 = false;
var m = !environmentData.ViewProjectionMatrix.HasValue
? matrix
: matrix * environmentData.ViewProjectionMatrix.Value;
if (size != 1.0)
m = Matrix.CreateScale(size) * m;
flag2 |= m.M14 != 0.0 || m.M24 != 0.0 || m.M34 != 0.0 || m.M44 != 1.0;
var count1 = blockMesh.Vertices.Count;
var array1 = blockMesh.Vertices.Array;
var count2 = blockMesh.Indices.Count;
var array2 = blockMesh.Indices.Array;
var triangleVertices = texturedBatch3D.TriangleVertices;
var count3 = triangleVertices.Count;
var count4 = triangleVertices.Count;
triangleVertices.Count += count1;
for (var index = 0; index < count1; ++index)
{
var blockMeshVertex = array1[index];
if (flag2)
{
var result = new Vector4(blockMeshVertex.Position, 1f);
Vector4.Transform(ref result, ref m, out result);
var num2 = 1f / result.W;
blockMeshVertex.Position = new Vector3(result.X * num2, result.Y * num2, result.Z * num2);
}
else
{
Vector3.Transform(ref blockMeshVertex.Position, ref m, out blockMeshVertex.Position);
}
if (flag1 || blockMeshVertex.IsEmissive)
{
triangleVertices.Array[count4++] = new VertexPositionColorTexture(blockMeshVertex.Position,
blockMeshVertex.Color, blockMeshVertex.TextureCoordinates);
}
else
{
var color1 = new Color((byte) (blockMeshVertex.Color.R * (double) vector4.X),
(byte)(blockMeshVertex.Color.G * (double) vector4.Y),
(byte)(blockMeshVertex.Color.B * (double) vector4.Z),
(byte)(blockMeshVertex.Color.A * (double) vector4.W));
triangleVertices.Array[count4++] = new VertexPositionColorTexture(blockMeshVertex.Position, color1,
blockMeshVertex.TextureCoordinates);
}
}
var triangleIndices = texturedBatch3D.TriangleIndices;
var count5 = triangleIndices.Count;
triangleIndices.Count += count2;
for (var index = 0; index < count2; ++index)
triangleIndices.Array[count5++] = (ushort) ((uint) count3 + array2[index]);
}
public static int DamageItem(int value, int damageCount)
{
var block = Blocks[Terrain.ExtractContents(value)];
if (block.Durability < 0)
return value;
int damage = block.GetDamage(value) + damageCount;
if (damage <= block.Durability)
return block.SetDamage(value, damage);
return block.GetDamageDestructionValue(value);
}
public static void LoadBlocksData(string data)
{
data = data.Replace("\r", string.Empty);
var strArray1 = data.Split(new char[1] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
string[] strArray2 = null;
for (var index1 = 0; index1 < strArray1.Length; ++index1)
{
var strArray3 = strArray1[index1].Split(';');
if (index1 == 0)
{
strArray2 = new string[strArray3.Length - 1];
Array.Copy(strArray3, 1, strArray2, 0, strArray3.Length - 1);
}
else
{
if (strArray3.Length != strArray2.Length + 1)
throw new InvalidOperationException(string.Format("Not enough field values for block \"{0}\".",
new object[1] {strArray3.Length != 0 ? strArray3[0] : "unknown"}));
var typeName = strArray3[0];
if (!string.IsNullOrEmpty(typeName))
{
var key1 = Blocks.FirstOrDefault(v => v.GetType().Name == typeName);
if (key1 == null)
throw new InvalidOperationException(string.Format(
"Block \"{0}\" not found when loading block data.", new object[1] {typeName}));
var dictionary2 = new Dictionary<string, FieldInfo>();
foreach (var runtimeField in key1.GetType().GetRuntimeFields())
if (runtimeField.IsPublic && !runtimeField.IsStatic)
dictionary2.Add(runtimeField.Name, runtimeField);
for (var index2 = 1; index2 < strArray3.Length; ++index2)
{
var key2 = strArray2[index2 - 1];
var data1 = strArray3[index2];
if (!string.IsNullOrEmpty(data1))
{
if (!dictionary2.TryGetValue(key2, out FieldInfo fieldInfo))
throw new InvalidOperationException(string.Format(
"Field \"{0}\" not found or not accessible when loading block data.",
new object[1] { key2 }));
object obj;
if (data1.StartsWith("#"))
{
var refTypeName = data1.Substring(1);
if (string.IsNullOrEmpty(refTypeName))
{
obj = key1.BlockIndex;
}
else
{
var block = Blocks.FirstOrDefault(v => v.GetType().Name == refTypeName);
if (block == null)
throw new InvalidOperationException(
string.Format(
"Reference block \"{0}\" not found when loading block data.",
new object[1] {refTypeName}));
obj = block.BlockIndex;
}
}
else
{
obj = HumanReadableConverter.ConvertFromString(fieldInfo.FieldType, data1);
}
fieldInfo.SetValue(key1, obj);
}
}
}
}
}
}
private static void CalculateSlotTexCoordTables()
{
for (var slot = 0; slot < 256; ++slot)
m_slotTexCoords[slot] = TextureSlotToTextureCoords(slot);
}
public static Vector4 TextureSlotToTextureCoords(int slot)
{
int num1 = slot % 16;
int num2 = slot / 16;
return new Vector4((float) ((num1 + 1.0 / 1000.0) / 16.0), (float) ((num2 + 1.0 / 1000.0) / 16.0),
(float) ((num1 + 1 - 1.0 / 1000.0) / 16.0), (float) ((num2 + 1 - 1.0 / 1000.0) / 16.0));
}
}
}