-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathKitData.cs
More file actions
221 lines (178 loc) · 8.89 KB
/
Copy pathKitData.cs
File metadata and controls
221 lines (178 loc) · 8.89 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#region License
/*
* This file is part of uEssentials project.
* https://uessentials.github.io/
*
* Copyright (C) 2015-2018 leonardosnt
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Essentials.Api;
using Essentials.Common.Util;
using Essentials.Core.Storage;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Rocket.Unturned.Items;
using SDG.Unturned;
using Essentials.NativeModules.Kit.Item;
namespace Essentials.NativeModules.Kit.Data {
internal class KitData : IData<Dictionary<string, Kit>> {
internal static string DataFilePath => Path.Combine(UEssentials.Folder, "kits.json");
public virtual void Save(Dictionary<string, Kit> warps) {
JsonUtil.Serialize(DataFilePath, warps.Values);
}
public virtual Dictionary<string, Kit> Load() {
var loadedKits = new Dictionary<string, Kit>();
if (!File.Exists(DataFilePath)) {
File.Create(DataFilePath).Close();
LoadDefault();
}
JArray kitArr;
try {
kitArr = JArray.Parse(File.ReadAllText(DataFilePath));
} catch (JsonReaderException ex) {
UEssentials.Logger.LogError($"Failed to parse kit configuration ({DataFilePath}). Invalid JSON!");
UEssentials.Logger.LogException(ex);
return new Dictionary<string, Kit>();
}
foreach (var kitObj in kitArr.Children<JObject>()) {
T GetKitObjValueOrDefault<T>(string key) {
var val = kitObj.GetValue(key, StringComparison.InvariantCultureIgnoreCase);
return val == null ? default(T) : val.Value<T>();
}
var name = GetKitObjValueOrDefault<string>("Name");
if (name == null) {
UEssentials.Logger.LogError($"Missing required attribute 'Name' in the kit at index {kitObj.Path}.");
continue;
}
if (loadedKits.ContainsKey(name.ToLowerInvariant())) {
UEssentials.Logger.LogWarning($"Duplicated kit ({name})");
continue;
}
var kit = new Kit(
name,
GetKitObjValueOrDefault<uint>("Cooldown"),
GetKitObjValueOrDefault<decimal>("Cost"),
GetKitObjValueOrDefault<bool>("ResetCooldownWhenDie")
);
foreach (var itemObj in kitObj.GetValue("Items", StringComparison.InvariantCultureIgnoreCase).Children<JObject>()) {
var kitItem = ParseKitItem(kit, itemObj);
if (kitItem != null) {
kit.Items.Add(kitItem);
}
}
loadedKits.Add(kit.Name.ToLowerInvariant(), kit);
}
return loadedKits;
}
private AbstractKitItem ParseKitItem(Kit kit, JObject itemObj) {
const StringComparison strCmp = StringComparison.InvariantCultureIgnoreCase;
if (itemObj.TryGetValue("money", strCmp, out var moneyToken)) {
if (!UEssentials.EconomyProvider.IsPresent) {
UEssentials.Logger.LogWarning("Cannot add 'Money' item because there is no active economy system.");
return null;
}
return new KitItemMoney(moneyToken.Value<decimal>());
}
if (itemObj.TryGetValue("xp", strCmp, out var expToken)) {
return new KitItemExperience(expToken.Value<uint>());
}
if (itemObj.TryGetValue("vehicle", strCmp, out var vehicleIdToken)) {
var vehicleId = vehicleIdToken.Value<ushort>();
if (Assets.find(EAssetType.VEHICLE, vehicleId) == null) {
UEssentials.Logger.LogWarning($"Invalid vehicle id '{vehicleId}' in the item at {itemObj.Path} in the kit '{kit.Name}'");
return null;
}
return new KitItemVehicle(vehicleId);
}
var itemIdToken = itemObj.GetValue("id", strCmp);
if (itemIdToken == null) {
UEssentials.Logger.LogWarning($"Missing attribute 'Id' in the item at {itemObj.Path} in the kit '{kit.Name}'");
return null;
}
var itemId = itemIdToken.Value<ushort>();
var itemAsset = (ItemAsset) Assets.find(EAssetType.ITEM, itemId);
var tokKitItemDurability = itemObj.GetValue("Durability", strCmp);
var tokKitItemAmount = itemObj.GetValue("Amount", strCmp);
var tokAmmo = itemObj.GetValue("Ammo", strCmp);
var kitItemAmount = tokKitItemAmount?.Value<byte>() ?? 1;
var kitItemDurability = tokKitItemDurability?.Value<byte>() ?? 100;
// Parse weapon specific attributes
if (itemAsset is ItemGunAsset) {
var tokFireMode = itemObj.GetValue("FireMode", strCmp);
EFiremode? fireMode = null;
if (tokFireMode != null) {
try {
fireMode = (EFiremode) Enum.Parse(typeof(EFiremode), tokFireMode.Value<string>(), true);
} catch (ArgumentException) {
UEssentials.Logger.LogWarning($"Invalid firemode '{tokFireMode}' in the item at {itemObj.Path} in the kit '{kit.Name}'. " +
$"Valid Firemodes: ${string.Join(", ", Enum.GetNames(typeof(EFiremode)))}");
}
}
var weaponItem = new KitItemWeapon(itemId,
kitItemDurability,
kitItemAmount,
tokAmmo?.Value<byte>() ?? null,
fireMode
);
weaponItem.Barrel = itemObj.GetValue("Barrel", strCmp)?.ToObject<Attachment>();
weaponItem.Sight = itemObj.GetValue("Sight", strCmp)?.ToObject<Attachment>();
weaponItem.Tactical = itemObj.GetValue("Tactical", strCmp)?.ToObject<Attachment>();
weaponItem.Grip = itemObj.GetValue("Grip", strCmp)?.ToObject<Attachment>();
weaponItem.Magazine = itemObj.GetValue("Magazine", strCmp)?.ToObject<Attachment>();
return weaponItem;
}
if (itemAsset is ItemMagazineAsset || itemAsset is ItemSupplyAsset) {
var magazineAmmo = tokAmmo?.Value<byte>() ?? itemAsset.amount;
return new KitItemMagazine(itemId, kitItemDurability, kitItemAmount, magazineAmmo);
}
var kitItem = new KitItem(itemId, kitItemDurability, kitItemAmount);
if (itemAsset is ItemFuelAsset) {
var item = kitItem;
var fuelPercentage = itemObj.GetValue("FuelPercentage", strCmp)?.Value<float>() ?? 100;
ItemUtil.Refuel(item.Metadata, item.Id, fuelPercentage);
}
return kitItem;
}
private void LoadDefault() {
var defaultKits = new Dictionary<string, Kit>();
var defaultKit = new Kit("default", 120, true);
var weaponKit = new Kit("default2", 1200, false);
var planeKit = new Kit("plane", 9000, 100.0m, false);
var xpKit = new Kit("xp", 1200, 50.0m, false);
defaultKit.Items.Add(new KitItem(16, 100, 1));
defaultKit.Items.Add(new KitItem(13, 100, 2));
defaultKit.Items.Add(new KitItem(14, 100, 1));
weaponKit.Items.Add(new KitItemWeapon(4, 100, 1, 30, EFiremode.BURST) {
Barrel = new Attachment(7, 100),
Grip = new Attachment(8, 100),
Sight = new Attachment(146, 100),
Magazine = new Attachment(17, 100),
Tactical = new Attachment(151, 100)
});
planeKit.Items.Add(new KitItemVehicle(92));
xpKit.Items.Add(new KitItemExperience(100));
defaultKits.Add("default", defaultKit);
defaultKits.Add("weapon", weaponKit);
defaultKits.Add("plane", planeKit);
defaultKits.Add("xp", xpKit);
Save(defaultKits);
}
}
}