This repository was archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramework.cs
More file actions
166 lines (164 loc) · 5.4 KB
/
Framework.cs
File metadata and controls
166 lines (164 loc) · 5.4 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Uncreated.Networking.Encoding.IO;
using Uncreated.Warfare;
namespace Uncreated.Website
{
public struct PostResponse
{
public static readonly PostResponse NULL = new PostResponse(false, null);
public bool Success;
public object State;
[JsonConstructor]
public PostResponse(bool Success, object State)
{
this.Success = Success;
this.State = State;
}
public static PostResponse As<T>(object state)
{
if (state is T o)
return new PostResponse(true, o);
else return NULL;
}
public static PostResponse As<T>(T state)
{
return new PostResponse(true, state);
}
}
public struct KitData
{
//public ItemData[] ItemData;
public JSKitData Kit;
[JsonConstructor]
public KitData(Kit kit)
{
this.Kit = new JSKitData(kit);
}
}
public struct StartupData
{
public ItemData[] items;
public StartupData(ItemData[] items)
{
this.items = items;
}
}
public struct JSKitData
{
public readonly string Name;
public readonly EClass Class;
public readonly EBranch Branch;
public readonly ulong Team;
public readonly ushort Cost;
public readonly ushort RequiredLevel;
public readonly ushort TicketCost;
public readonly bool IsPremium;
public readonly float PremiumCost;
public readonly bool IsLoadout;
public readonly float TeamLimit;
public readonly float Cooldown;
public readonly bool ShouldClearInventory;
public readonly JSKitItem[] Items;
public readonly JSKitClothing[] Clothes;
public readonly ulong[] AllowedUsers;
public JSKitData(Kit k)
{
this.Name = k.Name;
this.Class = k.Class;
this.Branch = k.Branch;
this.Team = k.Team;
this.Cost = k.Cost;
this.RequiredLevel = k.RequiredLevel;
this.TicketCost = k.TicketCost;
this.IsPremium = k.IsPremium;
this.PremiumCost = k.PremiumCost;
this.IsLoadout = k.IsLoadout;
this.TeamLimit = k.TeamLimit;
this.Cooldown = k.Cooldown;
this.ShouldClearInventory = k.ShouldClearInventory;
this.Items = new JSKitItem[k.Items.Count];
for (int i = 0; i < k.Items.Count; i++)
this.Items[i] = new JSKitItem(k.Items[i]);
this.Clothes = new JSKitClothing[k.Clothes.Count];
for (int i = 0; i < k.Clothes.Count; i++)
this.Clothes[i] = new JSKitClothing(k.Clothes[i]);
this.AllowedUsers = k.AllowedUsers.ToArray();
}
}
public struct JSKitItem
{
public readonly ushort ID;
public readonly byte x;
public readonly byte y;
public readonly byte rotation;
public readonly byte quality;
public readonly string metadata;
public readonly byte amount;
public readonly byte page;
public JSKitItem(KitItem i)
{
this.ID = i.ID;
this.x = i.x;
this.y = i.y;
this.rotation = i.rotation;
this.quality = i.quality;
this.metadata = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(i.metadata));
this.amount = i.amount;
this.page = i.page;
}
}
public struct JSKitClothing
{
public readonly ushort ID;
public readonly byte quality;
public readonly string metadata;
public readonly EClothingType type;
public JSKitClothing(KitClothing c)
{
this.ID = c.ID;
this.quality = c.quality;
this.metadata = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(c.state));
this.type = c.type;
}
}
public class CachedRegistry : Dictionary<ushort, ItemData>
{
public static readonly string STORAGE_LOCATION = Environment.GetEnvironmentVariable("APPDATA") + @"\Uncreated\ASP\DataCache\Items\";
private static readonly RawByteIO<ItemData> IO = new RawByteIO<ItemData>(ItemData.Read, ItemData.Write, null);
public CachedRegistry() : base()
{
if (!System.IO.Directory.Exists(STORAGE_LOCATION))
System.IO.Directory.CreateDirectory(STORAGE_LOCATION);
}
public new void Add(ushort ID, ItemData data)
{
if (data == null) return;
base.Add(ID, data);
CacheItem(data);
}
public void AddOrUpdate(ushort ID, ItemData data)
{
if (data == null) return;
if (ContainsKey(ID))
this[ID] = data;
else
base.Add(ID, data);
CacheItem(data);
}
private void CacheItem(ItemData data)
{
IO.WriteTo(data, STORAGE_LOCATION + data.ItemID.ToString() + ".dat");
}
public ItemData GetItemData(ushort ID)
{
if (TryGetValue(ID, out ItemData rtn)) return rtn;
if (IO.ReadFrom(STORAGE_LOCATION + ID.ToString() + ".dat", out rtn))
Add(ID, rtn);
return rtn;
}
}
}