-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathDynamicItemsConfig.cs
More file actions
91 lines (82 loc) · 2.75 KB
/
DynamicItemsConfig.cs
File metadata and controls
91 lines (82 loc) · 2.75 KB
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
/***************************************************************************
*
* $Author: Turley
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml;
using Microsoft.Extensions.Logging;
using Ultima;
namespace UoFiddler.Controls.Classes
{
public static class DynamicItemsConfig
{
private static readonly ILogger _log = AppLog.For(typeof(DynamicItemsConfig));
private static bool _loaded;
public static void EnsureLoaded()
{
if (_loaded)
{
return;
}
_loaded = true;
string path = Path.Combine(Options.AppDataPath, "DynamicItems.xml");
if (!File.Exists(path))
{
_log.LogWarning("DynamicItems.xml not found at {Path}", path);
return;
}
var ids = new HashSet<ushort>();
var doc = new XmlDocument();
doc.Load(path);
foreach (XmlNode node in doc.DocumentElement!.ChildNodes)
{
if (node is not XmlElement elem)
{
continue;
}
switch (elem.LocalName)
{
case "Item":
if (TryParseId(elem.GetAttribute("id"), out ushort id))
{
ids.Add(id);
}
break;
case "Range":
if (TryParseId(elem.GetAttribute("from"), out ushort from) &&
TryParseId(elem.GetAttribute("to"), out ushort to))
{
for (ushort i = from; i <= to; i++)
{
ids.Add(i);
}
}
break;
}
}
MultiComponentList.DynamicItemIds = ids;
}
private static bool TryParseId(string value, out ushort result)
{
result = 0;
if (string.IsNullOrEmpty(value))
{
return false;
}
if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
return ushort.TryParse(value[2..], NumberStyles.HexNumber, null, out result);
}
return ushort.TryParse(value, out result);
}
}
}