fix(shrink): Changes shrink table to use json (#446)

- [X] Replaces shrink.cfg with shrink.json
- [X] Cleans up shrink loading code
This commit is contained in:
Kamron Batman 2021-02-03 16:58:50 -08:00 • committed by GitHub
parent 5d9ebf062f
commit 3664149422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 453 additions and 517 deletions

View file

@ -1463,22 +1463,13 @@ namespace Server.Gumps
public static string FormatByteAmount(long totalBytes)
{
if (totalBytes > 1000000000)
return totalBytes switch
{
return $"{(double)totalBytes / 1073741824:F1} GB";
}
if (totalBytes > 1000000)
{
return $"{(double)totalBytes / 1048576:F1} MB";
}
if (totalBytes > 1000)
{
return $"{(double)totalBytes / 1024:F1} KB";
}
return $"{totalBytes} Bytes";
> 1000000000 => $"{(double)totalBytes / 1073741824:F1} GB",
> 1000000 => $"{(double)totalBytes / 1048576:F1} MB",
> 1000 => $"{(double)totalBytes / 1024:F1} KB",
_ => $"{totalBytes} Bytes"
};
}
public static void Initialize()

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using Server.Json;
namespace Server
{
@ -8,84 +9,44 @@ namespace Server
{
public const int DefaultItemID = 0x1870; // Yellow virtue stone
private static int[] m_Table;
private static int[] _shrinkTable; // body is the index, value is the item id
public static int Lookup(Mobile m) => Lookup(m.Body.BodyID, DefaultItemID);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Lookup(Mobile m, int defaultValue = DefaultItemID) => Lookup(m.Body.BodyID, defaultValue);
public static int Lookup(int body) => Lookup(body, DefaultItemID);
public static int Lookup(Mobile m, int defaultValue) => Lookup(m.Body.BodyID, defaultValue);
public static int Lookup(int body, int defaultValue)
public static int Lookup(int body, int defaultValue = DefaultItemID)
{
m_Table ??= Load();
var index = body >= 0 && body < m_Table.Length ? m_Table[body] : -1;
if (index < 0)
_shrinkTable ??= Load();
if (body < 0 || body >= _shrinkTable.Length)
{
return defaultValue;
}
var val = m_Table[body];
var val = _shrinkTable[body];
return val == 0 ? defaultValue : val;
}
private static int[] Load()
{
var path = Path.Combine(Core.BaseDirectory, "Data/shrink.cfg");
var table = JsonConfig.Deserialize<Dictionary<string, string>>("Data/shrink.json")
?? new Dictionary<string, string>();
if (!File.Exists(path))
int length = 0;
foreach (var key in table.Keys)
{
Console.WriteLine("No table found!");
return Array.Empty<int>();
var index = Utility.ToInt32(key);
length = Math.Max(length, index + 1);
}
var list = new List<(int, int)>();
var length = 0;
using var ip = new StreamReader(path);
string line;
while ((line = ip.ReadLine()) != null)
var list = new int[length];
foreach (var (body, item) in table)
{
line = line.Trim();
if (line.Length == 0 || line.StartsWithOrdinal("#"))
{
continue;
}
try
{
var split = line.Split('\t');
if (split.Length >= 2)
{
var body = Utility.ToInt32(split[0]);
var item = Utility.ToInt32(split[1]);
if (body < 0)
{
continue;
}
list.Add((body, item));
length = Math.Max(body + 1, length);
}
}
catch
{
// ignored
}
var index = Utility.ToInt32(body);
list[index] = Utility.ToInt32(item);
}
var table = new int[length];
foreach (var (body, item) in list)
{
table[body] = item;
}
return table;
return list;
}
}
}