ModernUO/Projects/UOContent/Misc/ShrinkTable.cs
Kamron Batman 3664149422
fix(shrink): Changes shrink table to use json (#446)
- [X] Replaces shrink.cfg with shrink.json
- [X] Cleans up shrink loading code
2021-02-03 16:58:50 -08:00

52 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Server.Json;
namespace Server
{
public static class ShrinkTable
{
public const int DefaultItemID = 0x1870; // Yellow virtue stone
private static int[] _shrinkTable; // body is the index, value is the item id
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Lookup(Mobile m, int defaultValue = DefaultItemID) => Lookup(m.Body.BodyID, defaultValue);
public static int Lookup(int body, int defaultValue = DefaultItemID)
{
_shrinkTable ??= Load();
if (body < 0 || body >= _shrinkTable.Length)
{
return defaultValue;
}
var val = _shrinkTable[body];
return val == 0 ? defaultValue : val;
}
private static int[] Load()
{
var table = JsonConfig.Deserialize<Dictionary<string, string>>("Data/shrink.json")
?? new Dictionary<string, string>();
int length = 0;
foreach (var key in table.Keys)
{
var index = Utility.ToInt32(key);
length = Math.Max(length, index + 1);
}
var list = new int[length];
foreach (var (body, item) in table)
{
var index = Utility.ToInt32(body);
list[index] = Utility.ToInt32(item);
}
return list;
}
}
}