forked from Mag-nus/PhatACCacheBinParser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGlobals.cs
367 lines (291 loc) · 15.4 KB
/
Globals.cs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.EntityFrameworkCore;
using PhatACCacheBinParser.Common;
using PhatACCacheBinParser.Properties;
using PhatACCacheBinParser.Seg1_RegionDescExtendedData;
using PhatACCacheBinParser.Seg2_SpellTableExtendedData;
using PhatACCacheBinParser.Seg3_TreasureTable;
using PhatACCacheBinParser.Seg4_CraftTable;
using PhatACCacheBinParser.Seg5_HousingPortals;
using PhatACCacheBinParser.Seg6_LandBlockExtendedData;
using PhatACCacheBinParser.Seg8_QuestDefDB;
using PhatACCacheBinParser.Seg9_WeenieDefaults;
using PhatACCacheBinParser.SegA_MutationFilters;
using PhatACCacheBinParser.SegB_GameEventDefDB;
using ACE.Database;
using ACE.Database.Models.World;
using ACE.Entity.Enum.Properties;
namespace PhatACCacheBinParser
{
static class Globals
{
public static readonly ConcurrentDictionary<uint, string> WeenieClsNames = new ConcurrentDictionary<uint, string>();
public static readonly ConcurrentDictionary<uint, string> WeenieNames = new ConcurrentDictionary<uint, string>();
public static class CacheBin
{
public static bool IsLoaded;
public static readonly RegionDescExtendedData RegionDescExtendedData = new RegionDescExtendedData();
public static readonly SpellTableExtendedData SpellTableExtendedData = new SpellTableExtendedData();
public static readonly TreasureTable TreasureTable = new TreasureTable();
public static readonly CraftingTable CraftingTable = new CraftingTable();
public static readonly HousingPortalsTable HousingPortalsTable = new HousingPortalsTable();
public static readonly LandBlockData LandBlockData = new LandBlockData();
// Segment 7
public static readonly QuestDefDB QuestDefDB = new QuestDefDB();
public static readonly WeenieDefaults WeenieDefaults = new WeenieDefaults();
public static readonly MutationFilters MutationFilters = new MutationFilters();
public static readonly GameEventDefDB GameEventDefDB = new GameEventDefDB();
public static void AddToWeenieClsNames()
{
foreach (var weenie in WeenieDefaults.Weenies)
{
//var className = weenie.Value.Description;
var className = "";
if (String.IsNullOrEmpty(className))
{
if (Enum.IsDefined(typeof(WCLASSID), (int)weenie.Value.WCID))
className = Enum.GetName(typeof(WCLASSID), weenie.Value.WCID).ToLower();
else if (Enum.IsDefined(typeof(WeenieClasses), (ushort)weenie.Value.WCID))
{
var clsName = Enum.GetName(typeof(WeenieClasses), weenie.Value.WCID).ToLower().Substring(2);
className = clsName.Substring(0, clsName.Length - 6);
}
else
{
var name = weenie.Value.StringValues.Where(x => x.Key == (int)PropertyString.Name).FirstOrDefault().Value;
if (!String.IsNullOrEmpty(name))
className = "ace" + weenie.Value.WCID.ToString() + "-" + name.Replace("'", "").Replace(" ", "").Replace(".", "").Replace("(", "").Replace(")", "").Replace("+", "").Replace(":", "").Replace("_", "").Replace("-", "").Replace(",", "").ToLower();
}
className = className.Replace("_", "-");
}
WeenieClsNames[weenie.Value.WCID] = className;
}
}
public static void AddToWeenieNames()
{
foreach (var weenie in WeenieDefaults.Weenies)
{
var name = weenie.Value.StringValues.Where(x => x.Key == (int)PropertyString.Name).FirstOrDefault().Value;
if (!String.IsNullOrEmpty(name))
WeenieNames[weenie.Value.WCID] = name;
else
WeenieNames[weenie.Value.WCID] = "ace" + weenie.Value.WCID.ToString();
}
}
}
public static class GDLE
{
public static bool IsLoaded;
/// <summary>
/// region.json + encounters.json
/// </summary>
//public static List<ACE.Database.Models.World.Encounter> Encounters;
public static ACE.Adapter.GDLE.Models.Region Region;
public static List<ACE.Adapter.GDLE.Models.TerrainData> TerrainData;
/// <summary>
/// events.json
/// </summary>
public static List<ACE.Database.Models.World.Event> Events;
/// <summary>
/// quests.json
/// </summary>
public static List<ACE.Database.Models.World.Quest> Quests;
/// <summary>
/// recipeprecursors.json
/// </summary>
public static List<ACE.Database.Models.World.CookBook> RecipePrecursors;
/// <summary>
/// recipes.json
/// </summary>
public static List<ACE.Database.Models.World.Recipe> Recipes;
// restrictedlandblocks.json
/// <summary>
/// spells.json
/// </summary>
public static List<ACE.Database.Models.World.Spell> Spells;
// treasureProfile.json
/// <summary>
/// wieldedtreasure.json
/// </summary>
public static List<ACE.Database.Models.World.TreasureWielded> WieldedTreasure;
/// <summary>
/// worldspawns.json
/// </summary>
public static List<ACE.Database.Models.World.LandblockInstance> Instances;
/// <summary>
/// worldspawns.json
/// </summary>
public static List<ACE.Database.Models.World.LandblockInstanceLink> Links;
/// <summary>
/// \weenies\*.json
/// </summary>
public static List<ACE.Database.Models.World.Weenie> Weenies;
public static void AddToWeenieNames()
{
if (Weenies is null) return;
foreach (var weenie in Weenies)
{
string name = null;
foreach (var property in weenie.WeeniePropertiesString)
{
if (property.Type == (ushort)PropertyString.Name)
{
name = property.Value;
break;
}
}
if (String.IsNullOrEmpty(name))
{
//if (!WeenieClassNames.Values.TryGetValue(weenie.ClassId, out name))
name = "ace" + weenie.ClassId;
}
WeenieNames[weenie.ClassId] = name;
}
}
}
public static class ACEDatabase
{
/// <summary>
/// This will be null if the database hasn't been init yet.<para />
/// DO NOT SET THIS TO NULL. DO NOT DISPOSE OF IT. DO NOT WRAP IT IN A USING STATEMENT.
/// </summary>
public static WorldDbContext WorldDbContext;
public static WorldDatabaseWithEntityCache WorldDatabase = new WorldDatabaseWithEntityCache();
public static List<ACE.Database.Models.World.Weenie> Weenies;
//public static List<ACE.Database.Models.World.LandblockInstance> LandblockInstances;
public static bool TryInitWorldDatabaseContext()
{
try
{
var optionsBuilder = new DbContextOptionsBuilder<WorldDbContext>();
optionsBuilder.UseMySql($"server={Settings.Default.ACEWorldServer};port={Settings.Default.ACEWorldPort};user={Settings.Default.ACEWorldUser};password={Settings.Default.ACEWorldPassword};database={Settings.Default.ACEWorldDatabase}");
WorldDbContext = new WorldDbContext(optionsBuilder.Options);
var result = WorldDatabase.GetWeenie(WorldDbContext, 1);
if (result != null)
return true;
WorldDbContext = null;
return false;
}
catch (Exception ex)
{
MessageBox.Show("TryInitWorldDatabaseContext failed with exception: " + Environment.NewLine + Environment.NewLine + ex);
return false;
}
}
/// <summary>
/// This will also update the Globals.WeenieNames dictionary
/// </summary>
public static void ReCacheAllWeeniesInParallel(bool updateWeenieNames = true)
{
WorldDatabase.ClearWeenieCache();
var weenies = new List<ACE.Database.Models.World.Weenie>();
var results = WorldDbContext.Weenie
.AsNoTracking()
.ToList();
Parallel.ForEach(results, result =>
{
var optionsBuilder = new DbContextOptionsBuilder<WorldDbContext>();
optionsBuilder.UseMySql($"server={Settings.Default.ACEWorldServer};port={Settings.Default.ACEWorldPort};user={Settings.Default.ACEWorldUser};password={Settings.Default.ACEWorldPassword};database={Settings.Default.ACEWorldDatabase}");
var worldDbContext = new WorldDbContext(optionsBuilder.Options);
var weenie = WorldDatabase.GetWeenie(worldDbContext, result.ClassId);
weenies.Add(weenie);
if (updateWeenieNames && weenie != null)
{
var name = weenie.GetProperty(PropertyString.Name);
if (!String.IsNullOrEmpty(name))
WeenieNames[weenie.ClassId] = name;
}
});
Weenies = weenies;
}
public static ACE.Database.Models.World.Weenie GetWeenie(uint weenieClassId, bool updateWeenieNames = true)
{
var optionsBuilder = new DbContextOptionsBuilder<WorldDbContext>();
optionsBuilder.UseMySql($"server={Settings.Default.ACEWorldServer};port={Settings.Default.ACEWorldPort};user={Settings.Default.ACEWorldUser};password={Settings.Default.ACEWorldPassword};database={Settings.Default.ACEWorldDatabase}");
var worldDbContext = new WorldDbContext(optionsBuilder.Options);
var weenie = WorldDatabase.GetWeenie(worldDbContext, weenieClassId);
if (updateWeenieNames && weenie != null)
{
var name = weenie.GetProperty(PropertyString.Name);
if (!String.IsNullOrEmpty(name))
WeenieNames[weenie.ClassId] = name;
}
return weenie;
}
public static List<ACE.Database.Models.World.Weenie> GetAllWeenies()
{
var weenies = new List<ACE.Database.Models.World.Weenie>();
var results = WorldDbContext.Weenie
.AsNoTracking()
.ToList();
return results;
}
public static List<ACE.Database.Models.World.Weenie> GetAllWeeniesBetween(uint startWeenieClassId, uint endWeenieClassId)
{
var weenies = new List<ACE.Database.Models.World.Weenie>();
var results = WorldDbContext.Weenie
.AsNoTracking()
.Where(w => w.ClassId >= startWeenieClassId && w.ClassId <= endWeenieClassId)
.ToList();
return results;
}
public static List<ACE.Database.Models.World.LandblockInstance> GetAllLandblockInstances()
{
var landblocks = new List<ACE.Database.Models.World.LandblockInstance>();
var results = WorldDbContext.LandblockInstance
.Include(r => r.LandblockInstanceLink)
.AsNoTracking()
.ToList();
return results;
}
public static List<ACE.Database.Models.World.LandblockInstance> GetLandblockInstancesForLandblock(ushort landblock)
{
var optionsBuilder = new DbContextOptionsBuilder<WorldDbContext>();
optionsBuilder.UseMySql($"server={Settings.Default.ACEWorldServer};port={Settings.Default.ACEWorldPort};user={Settings.Default.ACEWorldUser};password={Settings.Default.ACEWorldPassword};database={Settings.Default.ACEWorldDatabase}");
var worldDbContext = new WorldDbContext(optionsBuilder.Options);
var instance = WorldDatabase.GetCachedInstancesByLandblock(worldDbContext, landblock);
return instance;
}
public static List<ACE.Database.Models.World.LandblockInstance> CloneLandblockToAnother(uint landblockToCloneFrom, uint landblockToCloneTo)
{
var optionsBuilder = new DbContextOptionsBuilder<WorldDbContext>();
optionsBuilder.UseMySql($"server={Settings.Default.ACEWorldServer};port={Settings.Default.ACEWorldPort};user={Settings.Default.ACEWorldUser};password={Settings.Default.ACEWorldPassword};database={Settings.Default.ACEWorldDatabase}");
var worldDbContext = new WorldDbContext(optionsBuilder.Options);
var instances = WorldDatabase.GetCachedInstancesByLandblock(worldDbContext, (ushort)landblockToCloneFrom);
var results = new List<ACE.Database.Models.World.LandblockInstance>();
foreach (var instance in instances.Where(x => (x.ObjCellId >> 16) == landblockToCloneFrom).OrderBy(y => y.Guid))
{
var newGuid = (instance.Guid & 0xF0000FFF) | (landblockToCloneTo << 12);
var newObjCellId = (instance.ObjCellId & 0x0000FFFF) | (landblockToCloneTo << 16);
var newInstance = new LandblockInstance
{
AnglesW = instance.AnglesW,
AnglesX = instance.AnglesX,
AnglesY = instance.AnglesY,
AnglesZ = instance.AnglesZ,
Guid = newGuid,
IsLinkChild = instance.IsLinkChild,
ObjCellId = newObjCellId,
OriginX = instance.OriginX,
OriginY = instance.OriginY,
OriginZ = instance.OriginZ,
WeenieClassId = instance.WeenieClassId,
LastModified = DateTime.UtcNow
};
foreach (var link in instance.LandblockInstanceLink)
{
var newChildGuid = (link.ChildGuid & 0xF0000FFF) | (landblockToCloneTo << 12);
newInstance.LandblockInstanceLink.Add(new LandblockInstanceLink { ChildGuid = newChildGuid, ParentGuid = newGuid, LastModified = DateTime.UtcNow });
}
results.Add(newInstance);
}
return results;
}
}
}
}