-
Notifications
You must be signed in to change notification settings - Fork 101
/
Utility.cs
647 lines (501 loc) · 21.9 KB
/
Utility.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
using MagicStorage.Common;
using MagicStorage.Common.Systems.RecurrentRecipes;
using MagicStorage.Common.Systems.Shimmering;
using MagicStorage.Components;
using MagicStorage.Edits;
using MagicStorage.Sorting;
using Microsoft.Xna.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.Config;
using Terraria.ModLoader.IO;
namespace MagicStorage {
public static partial class Utility {
public static bool DownedAllMechs => NPC.downedMechBoss1 && NPC.downedMechBoss2 && NPC.downedMechBoss3;
public static int GetCardinality(this BitArray bitArray) {
int[] ints = new int[(bitArray.Count >> 5) + 1];
bitArray.CopyTo(ints, 0);
int count = 0;
// fix for not truncated bits in last integer that may have been set to true with SetAll()
ints[^1] &= ~(-1 << (bitArray.Count % 32));
for (int i = 0; i < ints.Length; i++) {
int c = ints[i];
// magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
unchecked {
c -= (c >> 1) & 0x55555555;
c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
count += c;
}
return count;
}
public static bool AreStrictlyEqual(Item item1, Item item2, bool checkStack = false, bool checkPrefix = true) => AreStrictlyEqual(item1, item2, checkStack, checkPrefix, null);
public static bool AreStrictlyEqual(Item item1, Item item2, bool checkStack, bool checkPrefix, ConditionalWeakTable<Item, byte[]> savedItemTagIO) {
int stack1 = item1.stack;
int stack2 = item2.stack;
int prefix1 = item1.prefix;
int prefix2 = item2.prefix;
bool favorite1 = item1.favorited;
bool favorite2 = item2.favorited;
item1.favorited = false;
item2.favorited = false;
bool equal;
if (!checkPrefix) {
item1.prefix = 0;
item2.prefix = 0;
}
if (!checkStack) {
item1.stack = 1;
item2.stack = 1;
}
if (!ItemData.Matches(item1, item2)) {
equal = false;
goto ReturnFromMethod;
}
try {
equal = TagIOSave(item1, savedItemTagIO).SequenceEqual(TagIOSave(item2, savedItemTagIO));
} catch {
// Swallow the exception and disallow stacking
equal = false;
}
ReturnFromMethod:
item1.stack = stack1;
item2.stack = stack2;
item1.prefix = prefix1;
item2.prefix = prefix2;
item1.favorited = favorite1;
item2.favorited = favorite2;
return equal;
}
private static byte[] TagIOSave(Item item, ConditionalWeakTable<Item, byte[]> savedItemTagIO)
{
if (savedItemTagIO?.TryGetValue(item, out byte[] retVal) is true)
{
return retVal;
}
using MemoryStream memoryStream = new(200);
TagIO.ToStream(ItemIO.Save(item), memoryStream, false);
retVal = memoryStream.ToArray();
savedItemTagIO?.Add(item, retVal);
return retVal;
}
public static void Write(this BinaryWriter writer, Point16 position) {
writer.Write(position.X);
writer.Write(position.Y);
}
public static Point16 ReadPoint16(this BinaryReader reader)
=> new(reader.ReadInt16(), reader.ReadInt16());
public static void GetResearchStats(int itemType, out bool canBeResearched, out int sacrificesNeeded, out int currentSacrificeTotal) {
// NOTE: 1.4.4 adds this handy method which does all of the work for me. cool!
canBeResearched = Main.LocalPlayerCreativeTracker.ItemSacrifices.TryGetSacrificeNumbers(itemType, out currentSacrificeTotal, out sacrificesNeeded);
}
public static bool IsFullyResearched(int itemType, bool mustBeResearchable) {
GetResearchStats(itemType, out bool canBeResearched, out int sacrificesNeeded, out int currentSacrificeTotal);
return (!mustBeResearchable || (canBeResearched && sacrificesNeeded > 0)) && currentSacrificeTotal >= sacrificesNeeded;
}
public static SafeOrdering<T> AsSafe<T>(this IComparer<T> comparer, Func<T, string> reportObjectFunc) => new(comparer, reportObjectFunc);
private class ItemTypeComparer : IEqualityComparer<Item> {
public static ItemTypeComparer Instance { get; } = new();
public bool Equals(Item x, Item y) => x.type == y.type;
public int GetHashCode([DisallowNull] Item obj) => obj.type;
}
internal static bool RecipesMatchForHistory(Recipe recipe1, Recipe recipe2) {
return recipe1.createItem.type == recipe2.createItem.type
&& recipe1.requiredItem.SequenceEqual(recipe2.requiredItem, ItemTypeComparer.Instance)
&& recipe1.requiredTile.SequenceEqual(recipe2.requiredTile, EqualityComparer<int>.Default);
}
public static IEnumerable<TEStorageCenter> GetNearbyCenters(this Player self) {
Point16 centerTile = self.Center.ToTileCoordinates16();
HashSet<Point16> foundAccesses = new();
const int range = 39;
int startX = centerTile.X - range, startY = centerTile.Y - range;
int endX = centerTile.X + range, endY = centerTile.Y + range;
startX = Utils.Clamp(startX, 0, Main.maxTilesX - 1);
startY = Utils.Clamp(startY, 0, Main.maxTilesY - 1);
endX = Utils.Clamp(endX, 0, Main.maxTilesX - 1);
endY = Utils.Clamp(endY, 0, Main.maxTilesY - 1);
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
Tile tile = Main.tile[x, y];
if (TileLoader.GetTile(tile.TileType) is StorageAccess access)
foundAccesses.Add(new Point16(x - tile.TileFrameX / 18, y - tile.TileFrameY / 18));
}
}
return foundAccesses
.Select(static p => TileEntity.ByPosition.TryGetValue(p, out TileEntity entity) && entity is TEStorageCenter ? p : TEStorageComponent.FindStorageCenter(p))
.Where(static p => p != Point16.NegativeOne)
.Distinct()
.Select(static p => TileEntity.ByPosition.TryGetValue(p, out TileEntity entity) ? entity : null)
.OfType<TEStorageCenter>();
}
public static TEStorageHeart GetHeartFromAccess(Point16 access) {
if (access.X < 0 || access.Y < 0)
return null;
Tile tile = Main.tile[access.X, access.Y];
if (TileLoader.GetTile(tile.TileType) is not StorageAccess storage)
return null;
return storage.GetHeart(access.X, access.Y);
}
public static IEnumerable<TeleportPylonInfo> NearbyPylons(Player player, float range) {
if (!Main.PylonSystem.HasAnyPylon() || range == 0)
yield break;
Point16 playerCenter = player.Center.ToTileCoordinates16();
short pX = playerCenter.X, pY = playerCenter.Y;
float r = range / 16;
foreach (TeleportPylonInfo pylon in Main.PylonSystem.Pylons) {
if (!IsPylonValidForRemoteAccessLinking(player, pylon, checkNPCDanger: false))
continue;
if (range < 0) {
yield return pylon;
continue;
}
int x = pylon.PositionInTiles.X, y = pylon.PositionInTiles.Y;
float xMin = x - r, xMax = x + r + 1, yMin = y - r, yMax = y + r + 1;
//Range < 0 is treated as infinite range
if (xMin <= pX && pX <= xMax && yMin <= pY && pY <= yMax)
yield return pylon;
}
}
public static bool StorageSystemHasNearbyPylon(Player player, TEStorageHeart heart, int tileRange) {
if (!Main.PylonSystem.HasAnyPylon() || heart is null || tileRange == 0)
return false;
List<TeleportPylonInfo> validPylons = Main.PylonSystem.Pylons.Where(pylon => IsPylonValidForRemoteAccessLinking(player, pylon, checkNPCDanger: false)).ToList();
if (validPylons.Count == 0)
return false;
//Tile range < 0 is treated as infinite range
if (tileRange < 0)
return true;
foreach (var center in TileEntity.ByPosition.Values.OfType<TEStorageCenter>()) {
if (GetHeartFromAccess(center.Position)?.Position != heart.Position)
continue;
int x = center.Position.X, y = center.Position.Y;
int xMin = x - tileRange, xMax = x + tileRange + 1, yMin = y - tileRange, yMax = y + tileRange + 1;
foreach (TeleportPylonInfo pylon in validPylons) {
int pX = pylon.PositionInTiles.X, pY = pylon.PositionInTiles.Y;
if (xMin <= pX && pX <= xMax && yMin <= pY && pY <= yMax)
return true;
}
}
return false;
}
public static bool PlayerIsNearStorageSystem(Player player, TEStorageHeart heart, float range) {
if (heart is null || range == 0)
return false;
//Range < 0 is treated as infinite range
if (range < 0)
return true;
Point16 playerCenter = player.Center.ToTileCoordinates16();
short pX = playerCenter.X, pY = playerCenter.Y;
float r = range / 16;
foreach (var center in TileEntity.ByPosition.Values.OfType<TEStorageCenter>()) {
if (GetHeartFromAccess(center.Position)?.Position != heart.Position)
continue;
int x = center.Position.X, y = center.Position.Y;
float xMin = x - r, xMax = x + r + 1, yMin = y - r, yMax = y + r + 1;
if (xMin <= pX && pX <= xMax && yMin <= pY && pY <= yMax)
return true;
}
return false;
}
public static bool PlayerIsNearAccess(Player player, Point16 access, float range) {
if (range == 0)
return false;
//Infinite range
if (range < 0)
return true;
float r = range / 16;
float pX = player.Center.X / 16, pY = player.Center.Y / 16;
int x = access.X, y = access.Y;
float xMin = x - r, xMax = x + r + 1, yMin = y - r, yMax = y + r + 1;
return xMin <= pX && pX <= xMax && yMin <= pY && pY <= yMax;
}
public static bool PlayerIsNearPylon(Player player, TeleportPylonInfo pylon, float range) {
if (range == 0)
return false;
//Range < 0 is treated as infinite range
if (range < 1)
return true;
Point16 playerCenter = player.Center.ToTileCoordinates16();
short pX = playerCenter.X, pY = playerCenter.Y;
float r = range / 16;
if (!IsPylonValidForRemoteAccessLinking(player, pylon, checkNPCDanger: false))
return false;
int x = pylon.PositionInTiles.X, y = pylon.PositionInTiles.Y;
float xMin = x - r, xMax = x + r + 1, yMin = y - r, yMax = y + r + 1;
return xMin <= pX && pX <= xMax && yMin <= pY && pY <= yMax;
}
//Copy for Common/Systems/PortableAccessAreas.cs
internal static bool PlayerIsNearPylonIgnoreValidity(Player player, TeleportPylonInfo pylon, float range) {
if (range == 0)
return false;
//Range < 0 is treated as infinite range
if (range < 1)
return true;
Point16 playerCenter = player.Center.ToTileCoordinates16();
short pX = playerCenter.X, pY = playerCenter.Y;
float r = range / 16;
int x = pylon.PositionInTiles.X, y = pylon.PositionInTiles.Y;
float xMin = x - r, xMax = x + r + 1, yMin = y - r, yMax = y + r + 1;
return xMin <= pX && pX <= xMax && yMin <= pY && pY <= yMax;
}
public static bool IsPylonValidForRemoteAccessLinking(Player player, TeleportPylonInfo info, bool checkNPCDanger) {
string key = null;
NearbyEffectsBlockingDuringPylonScanningDetour.DoBlockHooks = true;
int necessaryNPCCount = HowManyNPCsDoesPylonNeed(info);
bool flag = DoesPylonHaveEnoughNPCsAroundIt(info, necessaryNPCCount);
if (!flag)
key = "Net.CannotTeleportToPylonBecauseNotEnoughNPCs";
if (flag && checkNPCDanger) {
CheckNPCDanger(info, ref flag);
if (!flag)
key = "Net.CannotTeleportToPylonBecauseThereIsDanger";
}
if (flag) {
CheckLihzahrdPylon(info, ref flag);
if (!flag)
key = "Net.CannotTeleportToPylonBecauseAccessingLihzahrdTempleEarly";
}
if (flag) {
CheckValidDestination(info, ref flag);
if (!flag)
key = "Net.CannotTeleportToPylonBecauseNotMeetingBiomeRequirements";
}
if (info.ModPylon is ModPylon destinationPylon)
destinationPylon.ValidTeleportCheck_DestinationPostCheck(info, ref flag, ref key);
player.ForceUpdateBiomes();
NearbyEffectsBlockingDuringPylonScanningDetour.DoBlockHooks = false;
return flag;
}
public static void CallOnStackHooks(Item destination, Item source, int numTransfered) {
ItemLoader.OnStack(destination, source, numTransfered);
}
/// <summary>
/// Forces an enumeration to be evaluated, then returns the result
/// </summary>
public static IEnumerable<T> Evaluate<T>(this IEnumerable<T> enumerable)
=> enumerable.ToArray();
internal static int ConstrainedSum(this IEnumerable<int> source) {
ClampedArithmetic sum = 0;
foreach (int i in source)
sum += i;
return sum;
}
public static bool HasRecursiveRecipe(this Recipe recipe) => RecursiveRecipe.recipeToRecursiveRecipe.TryGetValue(recipe, out _);
public static RecursiveRecipe GetRecursiveRecipe(this Recipe recipe) => RecursiveRecipe.recipeToRecursiveRecipe.TryGetValue(recipe, out RecursiveRecipe recursive) ? recursive : null;
public static bool TryGetRecursiveRecipe(this Recipe recipe, out RecursiveRecipe recursive) => RecursiveRecipe.recipeToRecursiveRecipe.TryGetValue(recipe, out recursive);
public static void ConvertToGPSCoordinates(Vector2 worldCoordinate, out int compassCoordinate, out int depthCoordinate) {
// Copy/paste of logic from the info accessories
compassCoordinate = (int)(worldCoordinate.X * 2f / 16f - Main.maxTilesX);
depthCoordinate = (int)(worldCoordinate.Y * 2f / 16f - Main.worldSurface * 2.0);
}
public static void ConvertToGPSCoordinates(Point16 tileCoordinate, out int compassCoordinate, out int depthCoordinate) {
// Copy/paste of logic from the info accessories
compassCoordinate = (int)(tileCoordinate.X * 2f - Main.maxTilesX);
depthCoordinate = (int)(tileCoordinate.Y * 2f - Main.worldSurface * 2.0);
}
public static void ConvertToGPSCoordinates(Vector2 worldCoordinate, out string compassText, out string depthText) {
// Copy/paste of logic from the info accessories
ConvertToGPSCoordinates(worldCoordinate, out int compass, out int depth);
// Get the compass text
compassText = compass switch {
>0 => Language.GetTextValue("GameUI.CompassEast", compass),
0 => Language.GetTextValue("GameUI.CompassCenter"),
<0 => Language.GetTextValue("GameUI.CompassWest", compass)
};
// Get the depth text
float sizeFactor = Main.maxTilesX / 4200f;
sizeFactor *= sizeFactor;
int cavernsOffset = 1200;
float surface = (float)((worldCoordinate.Y / 16f - (65f + 10f * sizeFactor)) / (Main.worldSurface / 5.0));
string layerText;
if (worldCoordinate.Y > (Main.maxTilesY - 204) * 16)
layerText = Language.GetTextValue("GameUI.LayerUnderworld");
else if (worldCoordinate.Y > Main.rockLayer * 16.0 + cavernsOffset / 2f + 16.0)
layerText = Language.GetTextValue("GameUI.LayerCaverns");
else if (depth > 0)
layerText = Language.GetTextValue("GameUI.LayerUnderground");
else if (surface < 1f)
layerText = Language.GetTextValue("GameUI.LayerSpace");
else
layerText = Language.GetTextValue("GameUI.LayerSurface");
depth = Math.Abs(depth);
string coordText = depth switch {
0 => Language.GetTextValue("GameUI.DepthLevel"),
_ => Language.GetTextValue("GameUI.Depth", depth)
};
depthText = $"{coordText} {layerText}";
}
public static void AddOrSumCount(this Dictionary<int, int> itemCounts, int type, int count) {
if (!itemCounts.ContainsKey(type))
itemCounts[type] = count;
else
itemCounts[type] += count;
}
public static byte[] ToByteArrayNoCompression(Item item) {
MemoryStream ms = new MemoryStream();
TagIO.ToStream(ItemIO.Save(item), ms, false);
return ms.ToArray();
}
public static ReadOnlySpan<byte> ToByteSpanNoCompression(Item item) => ToByteArrayNoCompression(item);
public static string ToBase64NoCompression(Item item) => Convert.ToBase64String(ToByteArrayNoCompression(item));
public static byte[] ToByteArrayNoCompression(TagCompound tag) {
MemoryStream ms = new MemoryStream();
TagIO.ToStream(tag, ms, false);
return ms.ToArray();
}
public static ReadOnlySpan<byte> ToByteSpanNoCompression(TagCompound tag) => ToByteArrayNoCompression(tag);
public static string ToBase64NoCompression(TagCompound tag) => Convert.ToBase64String(ToByteArrayNoCompression(tag));
public static Item FromByteArrayNoCompression(byte[] bytes) {
MemoryStream ms = new MemoryStream(bytes);
return SafelyLoadItem(TagIO.FromStream(ms, false));
}
public static Item FromByteSpanNoCompression(ReadOnlySpan<byte> bytes) => FromByteArrayNoCompression(bytes.ToArray());
public static Item FromBase64NoCompression(string base64) => FromByteArrayNoCompression(Convert.FromBase64String(base64));
public static void SetVanillaAdjTiles(Item item, out bool hasSnow, out bool hasGraveyard) {
hasSnow = false;
hasGraveyard = false;
Player player = Main.LocalPlayer;
bool[] adjTiles = player.adjTile;
if (item.createTile >= TileID.Dirt) {
adjTiles[item.createTile] = true;
switch (item.createTile) {
case TileID.GlassKiln:
case TileID.Hellforge:
adjTiles[TileID.Furnaces] = true;
break;
case TileID.AdamantiteForge:
adjTiles[TileID.Furnaces] = true;
adjTiles[TileID.Hellforge] = true;
break;
case TileID.MythrilAnvil:
adjTiles[TileID.Anvils] = true;
break;
case TileID.BewitchingTable:
case TileID.Tables2:
adjTiles[TileID.Tables] = true;
break;
case TileID.AlchemyTable:
adjTiles[TileID.Bottles] = true;
adjTiles[TileID.Tables] = true;
break;
case TileID.Tombstones:
hasGraveyard = true;
break;
}
switch (item.createTile) {
case TileID.WorkBenches:
case TileID.Tables:
case TileID.Tables2:
adjTiles[TileID.Chairs] = true;
break;
}
TileLoader.AdjTiles(Main.LocalPlayer, item.createTile);
if (TileID.Sets.CountsAsWaterSource[item.createTile])
player.adjWater = true;
if (TileID.Sets.CountsAsLavaSource[item.createTile])
player.adjLava = true;
if (TileID.Sets.CountsAsHoneySource[item.createTile])
player.adjHoney = true;
if (player.adjTile[TileID.Tombstones])
hasGraveyard = true;
}
int globeItem = ModContent.ItemType<Items.BiomeGlobe>();
if (item.type == ItemID.WaterBucket || item.type == ItemID.BottomlessBucket || item.type == globeItem)
player.adjWater = true;
if (item.type == ItemID.LavaBucket || item.type == ItemID.BottomlessLavaBucket || item.type == globeItem)
player.adjLava = true;
if (item.type == ItemID.HoneyBucket || item.type == ItemID.BottomlessHoneyBucket || item.type == globeItem)
player.adjHoney = true;
if (item.type == ItemID.BottomlessShimmerBucket)
player.adjShimmer = true;
if (item.type == ModContent.ItemType<Items.SnowBiomeEmulator>() || item.type == globeItem)
hasSnow = true;
if (item.type == globeItem) {
adjTiles[TileID.Campfire] = true;
adjTiles[TileID.DemonAltar] = true;
hasGraveyard = true;
}
}
private static Action<ModConfig> ConfigManagerSave;
public static void SaveModConfig(ModConfig config) {
(ConfigManagerSave ??= CreateConfigManagerSave())(config);
static Action<ModConfig> CreateConfigManagerSave() {
MethodInfo configManagerSaveMethod = typeof(ConfigManager).GetMethod("Save", BindingFlags.Static | BindingFlags.NonPublic, new[] { typeof(ModConfig) })
?? throw new InvalidOperationException("Cannot get 'Terraria.ModLoader.Config.ConfigManager.Save' method.");
ParameterExpression modConfigParameter = Expression.Parameter(typeof(ModConfig));
return Expression.Lambda<Action<ModConfig>>(Expression.Call(configManagerSaveMethod, modConfigParameter), modConfigParameter).Compile();
}
}
public static Item GetItemSample(int item) => ContentSamples.ItemsByType[item];
public static bool IsSuccessful(this ShimmerInfo.ShimmerAttemptResult result) => result != ShimmerInfo.ShimmerAttemptResult.None;
public static bool IsSuccessfulButNotDecraftable(this ShimmerInfo.ShimmerAttemptResult result) => result != ShimmerInfo.ShimmerAttemptResult.None && result != ShimmerInfo.ShimmerAttemptResult.DecraftedItem;
public static IEnumerable<IShimmerResultReport> GetShimmerReports(this IShimmerResult result, int item) => result.GetShimmerReports(ContentSamples.ItemsByType[item], item);
internal static IEnumerable<T> Filter<T>(this IEnumerable<T> source, StorageGUI.ThreadContext thread, Func<T, Item> objToItem) {
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(thread);
ArgumentNullException.ThrowIfNull(objToItem);
return new ThreadFilterEnumerator<T>(thread, source, objToItem);
}
internal static IEnumerable<Item> Filter(this IEnumerable<Item> source, StorageGUI.ThreadContext thread) {
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(thread);
return new ThreadFilterItemEnumerator(thread, source);
}
internal static ParallelQuery<T> Filter<T>(this ParallelQuery<T> query, StorageGUI.ThreadContext thread, Func<T, Item> objToItem) {
ArgumentNullException.ThrowIfNull(query);
ArgumentNullException.ThrowIfNull(thread);
ArgumentNullException.ThrowIfNull(objToItem);
return new ThreadFilterParallelEnumerator<T>(thread, query, objToItem).GetQuery();
}
internal static ParallelQuery<Item> Filter(this ParallelQuery<Item> query, StorageGUI.ThreadContext thread) {
ArgumentNullException.ThrowIfNull(query);
ArgumentNullException.ThrowIfNull(thread);
return new ThreadFilterParallelItemEnumerator(thread, query).GetQuery();
}
public static Item SafelyLoadItem(TagCompound tag) {
try {
return ItemIO.Load(tag);
} catch (KeyNotFoundException) {
// Item was malformed
return new Item();
} catch (Exception ex) {
MagicStorageMod.Instance.Logger.Error("Error loading item from tag", ex);
return new Item();
}
}
/// <summary>
/// Generates a hash for a byte array. This method is <b>NOT</b> optimized for <see cref="object.GetHashCode"/> usage!
/// </summary>
public static int ComputeDataHash(byte[] data) {
if (data is not { Length: >0 })
return 0;
// Taken from: https://stackoverflow.com/a/468084
unchecked {
const int p = 16777619;
int hash = (int)2166136261;
for (int i = 0; i < data.Length; i++)
hash = (hash ^ data[i]) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
}
}
}