fix: Fixes CAGLoader NPE (#1006)
This commit is contained in:
parent
848db6fc4b
commit
0f304d77c4
1 changed files with 76 additions and 63 deletions
|
|
@ -20,78 +20,82 @@ using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using Server.Items;
|
using Server.Items;
|
||||||
using Server.Json;
|
using Server.Json;
|
||||||
|
using Server.Logging;
|
||||||
using Server.Utilities;
|
using Server.Utilities;
|
||||||
|
|
||||||
namespace Server.Commands
|
namespace Server.Commands;
|
||||||
|
|
||||||
|
public static class CAGLoader
|
||||||
{
|
{
|
||||||
public static class CAGLoader
|
private static readonly ILogger logger = LogFactory.GetLogger(typeof(CAGLoader));
|
||||||
|
|
||||||
|
public static CAGCategory Load()
|
||||||
{
|
{
|
||||||
public static CAGCategory Load()
|
var root = new CAGCategory("Add Menu");
|
||||||
|
var path = Path.Combine(Core.BaseDirectory, "Data/categorization.json");
|
||||||
|
|
||||||
|
var list = JsonConfig.Deserialize<List<CAGJson>>(path);
|
||||||
|
if (list == null)
|
||||||
{
|
{
|
||||||
var root = new CAGCategory("Add Menu");
|
throw new JsonException($"Failed to deserialize {path}.");
|
||||||
var path = Path.Combine(Core.BaseDirectory, "Data/categorization.json");
|
}
|
||||||
|
|
||||||
var list = JsonConfig.Deserialize<List<CAGJson>>(path);
|
// Not an optimized solution
|
||||||
if (list == null)
|
foreach (var cag in list)
|
||||||
|
{
|
||||||
|
var parent = root;
|
||||||
|
// Navigate through the dot notation categories until we find the last one
|
||||||
|
var categories = cag.Category.Split(".");
|
||||||
|
for (var i = 0; i < categories.Length; i++)
|
||||||
{
|
{
|
||||||
throw new JsonException($"Failed to deserialize {path}.");
|
var category = categories[i];
|
||||||
}
|
|
||||||
|
|
||||||
// Not an optimized solution
|
// No children, so let's make one
|
||||||
foreach (var cag in list)
|
if (parent.Nodes == null)
|
||||||
{
|
|
||||||
var parent = root;
|
|
||||||
// Navigate through the dot notation categories until we find the last one
|
|
||||||
var categories = cag.Category.Split(".");
|
|
||||||
for (var i = 0; i < categories.Length; i++)
|
|
||||||
{
|
{
|
||||||
var category = categories[i];
|
var cat = new CAGCategory(category, parent);
|
||||||
|
parent.Nodes = new CAGNode[] { cat };
|
||||||
|
parent = cat;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// No children, so let's make one
|
var oldParent = parent;
|
||||||
if (parent.Nodes == null)
|
for (var j = 0; j < parent.Nodes.Length; j++)
|
||||||
|
{
|
||||||
|
var node = parent.Nodes[j];
|
||||||
|
if (category == node.Title && node is CAGCategory cat)
|
||||||
{
|
{
|
||||||
var cat = new CAGCategory(category, parent);
|
|
||||||
parent.Nodes = new CAGNode[] { cat };
|
|
||||||
parent = cat;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var oldParent = parent;
|
|
||||||
for (var j = 0; j < parent.Nodes.Length; j++)
|
|
||||||
{
|
|
||||||
var node = parent.Nodes[j];
|
|
||||||
if (category == node.Title && node is CAGCategory cat)
|
|
||||||
{
|
|
||||||
parent = cat;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Didn't find the child, let's add it
|
|
||||||
if (oldParent == parent)
|
|
||||||
{
|
|
||||||
var nodes = parent.Nodes;
|
|
||||||
parent.Nodes = new CAGNode[nodes.Length + 1];
|
|
||||||
Array.Copy(nodes, parent.Nodes, nodes.Length);
|
|
||||||
var cat = new CAGCategory(category, parent);
|
|
||||||
parent.Nodes[^1] = cat;
|
|
||||||
parent = cat;
|
parent = cat;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the objects associated with the child most node
|
// Didn't find the child, let's add it
|
||||||
parent.Nodes = new CAGNode[cag.Objects.Length];
|
if (oldParent == parent)
|
||||||
for (var i = 0; i < cag.Objects.Length; i++)
|
|
||||||
{
|
{
|
||||||
var cagObj = cag.Objects[i];
|
var nodes = parent.Nodes;
|
||||||
cagObj.Parent = parent;
|
parent.Nodes = new CAGNode[nodes.Length + 1];
|
||||||
parent.Nodes[i] = cagObj;
|
Array.Copy(nodes, parent.Nodes, nodes.Length);
|
||||||
|
var cat = new CAGCategory(category, parent);
|
||||||
|
parent.Nodes[^1] = cat;
|
||||||
|
parent = cat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set ItemID and Hue
|
// Set the objects associated with the child most node
|
||||||
if (cagObj.Hue == null || cagObj.ItemID == null)
|
var pooledList = new List<CAGNode>(cag.Objects.Length);
|
||||||
|
for (var i = 0; i < cag.Objects.Length; i++)
|
||||||
|
{
|
||||||
|
var cagObj = cag.Objects[i];
|
||||||
|
cagObj.Parent = parent;
|
||||||
|
|
||||||
|
// Set ItemID and Hue
|
||||||
|
if (cagObj.Hue == null || cagObj.ItemID == null)
|
||||||
|
{
|
||||||
|
var type = cagObj.Type;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var type = cagObj.Type;
|
|
||||||
|
|
||||||
if (type.IsAssignableTo(typeof(Item)))
|
if (type.IsAssignableTo(typeof(Item)))
|
||||||
{
|
{
|
||||||
var item = cagObj.Type.CreateInstance<Item>();
|
var item = cagObj.Type.CreateInstance<Item>();
|
||||||
|
|
@ -138,19 +142,28 @@ namespace Server.Commands
|
||||||
m.Delete();
|
m.Delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.Warning(e, "Failed to instantiate type {Type}.", type);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pooledList.Add(cagObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
return root;
|
parent.Nodes = pooledList.ToArray();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public record CAGJson
|
return root;
|
||||||
{
|
|
||||||
[JsonPropertyName("category")]
|
|
||||||
public string Category { get; init; }
|
|
||||||
|
|
||||||
[JsonPropertyName("objects")]
|
|
||||||
public CAGObject[] Objects { get; init; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record CAGJson
|
||||||
|
{
|
||||||
|
[JsonPropertyName("category")]
|
||||||
|
public string Category { get; init; }
|
||||||
|
|
||||||
|
[JsonPropertyName("objects")]
|
||||||
|
public CAGObject[] Objects { get; init; }
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue