|
4 | 4 |
|
5 | 5 | namespace RustPlusBot.ItemData.Generator.Sources; |
6 | 6 |
|
7 | | -/// <summary>Reads recycle, craft, and research data from the offline RustLabs JSON files.</summary> |
| 7 | +/// <summary>Reads recycle, craft, research, decay, and upkeep data from the offline RustLabs JSON files.</summary> |
8 | 8 | /// <param name="RecycleFilePath">Path to the <c>rustlabsRecycleData.json</c> file.</param> |
9 | 9 | /// <param name="CraftFilePath">Path to the <c>rustlabsCraftData.json</c> file.</param> |
10 | 10 | /// <param name="ResearchFilePath">Path to the <c>rustlabsResearchData.json</c> file.</param> |
| 11 | +/// <param name="DecayFilePath">Path to the <c>rustlabsDecayData.json</c> file.</param> |
| 12 | +/// <param name="UpkeepFilePath">Path to the <c>rustlabsUpkeepData.json</c> file.</param> |
11 | 13 | internal sealed class OfflineRustLabsSource( |
12 | 14 | string RecycleFilePath, |
13 | 15 | string CraftFilePath, |
14 | | - string ResearchFilePath) : IRustLabsSource |
| 16 | + string ResearchFilePath, |
| 17 | + string DecayFilePath, |
| 18 | + string UpkeepFilePath) : IRustLabsSource |
15 | 19 | { |
16 | 20 | private static readonly Dictionary<string, int> WorkbenchLevels = new() |
17 | 21 | { |
@@ -160,4 +164,83 @@ public IReadOnlyDictionary<int, ResearchCost> LoadResearchCosts() |
160 | 164 |
|
161 | 165 | return result; |
162 | 166 | } |
| 167 | + |
| 168 | + /// <inheritdoc/> |
| 169 | + public IReadOnlyDictionary<int, DecayInfo> LoadDecay() |
| 170 | + { |
| 171 | + using var stream = File.OpenRead(DecayFilePath); |
| 172 | + using var doc = JsonDocument.Parse(stream); |
| 173 | + |
| 174 | + var result = new Dictionary<int, DecayInfo>(); |
| 175 | + if (!doc.RootElement.TryGetProperty("items", out var itemsEl)) |
| 176 | + { |
| 177 | + return result; |
| 178 | + } |
| 179 | + |
| 180 | + foreach (var prop in itemsEl.EnumerateObject()) |
| 181 | + { |
| 182 | + if (!int.TryParse(prop.Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id)) |
| 183 | + { |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + result[id] = new DecayInfo( |
| 188 | + ReadInt(prop.Value, "decay"), |
| 189 | + ReadInt(prop.Value, "decayOutside"), |
| 190 | + ReadInt(prop.Value, "decayInside"), |
| 191 | + ReadInt(prop.Value, "decayUnderwater"), |
| 192 | + ReadInt(prop.Value, "hp")); |
| 193 | + } |
| 194 | + |
| 195 | + return result; |
| 196 | + |
| 197 | + static int? ReadInt(JsonElement element, string name) => |
| 198 | + element.TryGetProperty(name, out var p) && p.ValueKind == JsonValueKind.Number |
| 199 | + ? p.GetInt32() |
| 200 | + : null; |
| 201 | + } |
| 202 | + |
| 203 | + /// <inheritdoc/> |
| 204 | + public IReadOnlyDictionary<int, UpkeepCost> LoadUpkeep() |
| 205 | + { |
| 206 | + using var stream = File.OpenRead(UpkeepFilePath); |
| 207 | + using var doc = JsonDocument.Parse(stream); |
| 208 | + |
| 209 | + var result = new Dictionary<int, UpkeepCost>(); |
| 210 | + if (!doc.RootElement.TryGetProperty("items", out var itemsEl)) |
| 211 | + { |
| 212 | + return result; |
| 213 | + } |
| 214 | + |
| 215 | + foreach (var prop in itemsEl.EnumerateObject()) |
| 216 | + { |
| 217 | + if (!int.TryParse(prop.Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id)) |
| 218 | + { |
| 219 | + continue; |
| 220 | + } |
| 221 | + |
| 222 | + var entries = new List<UpkeepEntry>(); |
| 223 | + foreach (var entry in prop.Value.EnumerateArray()) |
| 224 | + { |
| 225 | + var entryIdStr = entry.GetProperty("id").GetString(); |
| 226 | + if (entryIdStr is null || |
| 227 | + !int.TryParse(entryIdStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out var entryId)) |
| 228 | + { |
| 229 | + continue; |
| 230 | + } |
| 231 | + |
| 232 | + var quantity = entry.GetProperty("quantity").GetString() |
| 233 | + ?? throw new InvalidOperationException($"upkeep {id}: null quantity"); |
| 234 | + var (min, max) = UpkeepQuantity.Parse(quantity); |
| 235 | + entries.Add(new UpkeepEntry(entryId, min, max)); |
| 236 | + } |
| 237 | + |
| 238 | + if (entries.Count > 0) |
| 239 | + { |
| 240 | + result[id] = new UpkeepCost(entries); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + return result; |
| 245 | + } |
163 | 246 | } |
0 commit comments