mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
124 lines
3.2 KiB
C#
124 lines
3.2 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using RadicadeServer.Extensions;
|
|
using RadicadeServer.RecNet.Enums.Store;
|
|
|
|
namespace RadicadeServer.RecNet.Avatar
|
|
{
|
|
public class EquipmentSkinItem
|
|
{
|
|
[JsonPropertyName("prefab_name")]
|
|
public string PrefabName { get; set; } = "";
|
|
|
|
[JsonPropertyName("modification_guid")]
|
|
public string ModificationGuid { get; set; } = "";
|
|
|
|
[JsonPropertyName("display_name")]
|
|
public string DisplayName { get; set; } = "";
|
|
|
|
[JsonPropertyName("tooltip")]
|
|
public string Tooltip { get; set; } = "";
|
|
|
|
[JsonPropertyName("rarity")]
|
|
public Rarity Rarity { get; set; } = Rarity.Common;
|
|
|
|
[JsonPropertyName("storefront")]
|
|
public string Storefront { get; set; } = "";
|
|
|
|
[JsonPropertyName("price")]
|
|
public long Price { get; set; }
|
|
|
|
[JsonPropertyName("image_name")]
|
|
public string ImageName { get; set; } = "";
|
|
}
|
|
|
|
public class EquipmentSkinFile
|
|
{
|
|
[JsonPropertyName("items")]
|
|
public List<EquipmentSkinItem> Items { get; set; } = new();
|
|
}
|
|
|
|
public static class EquipmentSkinCatalog
|
|
{
|
|
private static readonly JsonSerializerOptions ReadOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
ReadCommentHandling = JsonCommentHandling.Skip,
|
|
AllowTrailingCommas = true
|
|
};
|
|
|
|
public static List<EquipmentSkinItem> Parse(string? json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json)) return new List<EquipmentSkinItem>();
|
|
|
|
try
|
|
{
|
|
var file = JsonSerializer.Deserialize<EquipmentSkinFile>(json, ReadOptions);
|
|
return (file?.Items ?? new List<EquipmentSkinItem>())
|
|
.Where(item => item != null
|
|
&& !string.IsNullOrWhiteSpace(item.PrefabName)
|
|
&& !string.IsNullOrWhiteSpace(item.ModificationGuid))
|
|
.ToList();
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
return new List<EquipmentSkinItem>();
|
|
}
|
|
}
|
|
|
|
public static List<Equipment> ToEquipment(IEnumerable<EquipmentSkinItem> skins)
|
|
{
|
|
return skins.Select(skin => new Equipment
|
|
{
|
|
PrefabName = skin.PrefabName.Trim(),
|
|
ModificationGuid = skin.ModificationGuid.Trim(),
|
|
UnlockedLevel = 0,
|
|
Favorited = false,
|
|
FriendlyName = string.IsNullOrWhiteSpace(skin.DisplayName)
|
|
? skin.PrefabName.Trim()
|
|
: skin.DisplayName.Trim(),
|
|
Tooltip = skin.Tooltip ?? "",
|
|
Rarity = skin.Rarity
|
|
}).ToList();
|
|
}
|
|
|
|
public static string MergeInto(string? equipmentJson, string? skinsJson)
|
|
{
|
|
var skins = ToEquipment(Parse(skinsJson));
|
|
if (skins.Count == 0) return equipmentJson ?? "[]";
|
|
|
|
var equipment = ParseEquipment(equipmentJson);
|
|
if (equipment == null) return equipmentJson ?? "[]";
|
|
|
|
var known = new HashSet<string>(
|
|
equipment.Select(Key), StringComparer.OrdinalIgnoreCase);
|
|
|
|
var added = 0;
|
|
foreach (var skin in skins)
|
|
{
|
|
if (!known.Add(Key(skin))) continue;
|
|
equipment.Add(skin);
|
|
added++;
|
|
}
|
|
|
|
return added == 0 ? equipmentJson ?? "[]" : equipment.ToJson();
|
|
}
|
|
|
|
private static List<Equipment>? ParseEquipment(string? equipmentJson)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(equipmentJson)) return new List<Equipment>();
|
|
try
|
|
{
|
|
return equipmentJson.FromJson<List<Equipment>>(ReadOptions) ?? new List<Equipment>();
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static string Key(Equipment item) =>
|
|
$"{(item.PrefabName ?? "").Trim()}\u001f{(item.ModificationGuid ?? "").Trim()}";
|
|
}
|
|
}
|