-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Importing.cs
418 lines (364 loc) · 14.6 KB
/
Importing.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Numerics;
using System.Text;
using Dalamud.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace QoLBar;
public class QoLSerializer : DefaultSerializationBinder
{
private static readonly Type exportType = typeof(Importing.ExportInfo);
private static readonly Type barType = typeof(BarConfig);
private static readonly Type shortcutType = typeof(Shortcut);
private static readonly Type barType2 = typeof(BarCfg);
private static readonly Type shortcutType2 = typeof(ShCfg);
private static readonly Type conditionSetType = typeof(CndSetCfg);
private static readonly Type conditionType = typeof(CndCfg);
private static readonly Type vector2Type = typeof(Vector2);
private static readonly Type vector4Type = typeof(Vector4);
private const string exportShortName = "e";
private const string barShortName = "b";
private const string shortcutShortName = "s";
private const string barShortName2 = "b2";
private const string shortcutShortName2 = "s2";
private const string conditionSetShortName = "cs";
private const string conditionShortName = "c";
private const string vector2ShortName = "2";
private const string vector4ShortName = "4";
private static readonly Dictionary<string, Type> types = new()
{
[exportType.FullName!] = exportType,
[exportShortName] = exportType,
[barType.FullName!] = barType,
[barShortName] = barType,
[shortcutType.FullName!] = shortcutType,
[shortcutShortName] = shortcutType,
[barType2.FullName!] = barType2,
[barShortName2] = barType2,
[shortcutType2.FullName!] = shortcutType2,
[shortcutShortName2] = shortcutType2,
[conditionSetType.FullName!] = conditionSetType,
[conditionSetShortName] = conditionSetType,
[conditionType.FullName!] = conditionType,
[conditionShortName] = conditionType,
[vector2Type.FullName!] = vector2Type,
[vector2ShortName] = vector2Type,
[vector4Type.FullName!] = vector4Type,
[vector4ShortName] = vector4Type
};
private static readonly Dictionary<Type, string> typeNames = new()
{
[exportType] = exportShortName,
[barType] = barShortName,
[shortcutType] = shortcutShortName,
[barType2] = barShortName2,
[shortcutType2] = shortcutShortName2,
[conditionSetType] = conditionSetShortName,
[conditionType] = conditionShortName,
[vector2Type] = vector2ShortName,
[vector4Type] = vector4ShortName
};
public override Type BindToType(string assemblyName, string typeName)
{
if (types.ContainsKey(typeName))
return types[typeName];
else
return base.BindToType(assemblyName, typeName);
}
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
if (typeNames.ContainsKey(serializedType))
{
assemblyName = null;
typeName = typeNames[serializedType];
}
else
base.BindToName(serializedType, out assemblyName, out typeName);
}
}
public static class Importing
{
public class ImportInfo
{
public BarCfg bar;
public ShCfg shortcut;
public CndSetCfg conditionSet;
}
public class ExportInfo
{
public BarConfig b1;
public BarCfg b2;
public Shortcut s1;
public ShCfg s2;
public CndSetCfg cs;
public string v = QoLBar.Config.PluginVersion;
}
private static readonly QoLSerializer qolSerializer = new();
private static void CleanBarConfig(BarCfg bar)
{
if (bar.DockSide == BarCfg.BarDock.Undocked)
{
bar.Alignment = bar.GetDefaultValue(x => x.Alignment);
bar.RevealAreaScale = bar.GetDefaultValue(x => x.RevealAreaScale);
bar.Hint = bar.GetDefaultValue(x => x.Hint);
}
else
{
bar.RevealAreaScale = bar.GetDefaultValue(x => x.RevealAreaScale);
if (bar.Visibility == BarCfg.BarVisibility.Always)
{
bar.RevealAreaScale = bar.GetDefaultValue(x => x.RevealAreaScale);
bar.Hint = bar.GetDefaultValue(x => x.Hint);
}
}
CleanShortcut(bar.ShortcutList);
}
private static void CleanShortcut(List<ShCfg> shortcuts)
{
foreach (var sh in shortcuts)
CleanShortcut(sh);
}
private static void CleanShortcut(ShCfg sh)
{
if (sh.Type != ShCfg.ShortcutType.Category)
{
sh.SubList = sh.GetDefaultValue(x => x.SubList);
sh.CategoryColumns = sh.GetDefaultValue(x => x.CategoryColumns);
sh.CategoryStaysOpen = sh.GetDefaultValue(x => x.CategoryStaysOpen);
sh.CategoryWidth = sh.GetDefaultValue(x => x.CategoryWidth);
sh.CategorySpacing = sh.GetDefaultValue(x => x.CategorySpacing);
sh.CategoryScale = sh.GetDefaultValue(x => x.CategoryScale);
sh.CategoryFontScale = sh.GetDefaultValue(x => x.CategoryFontScale);
sh.CategoryNoBackground = sh.GetDefaultValue(x => x.CategoryNoBackground);
sh.CategoryOnHover = sh.GetDefaultValue(x => x.CategoryOnHover);
sh.CategoryHoverClose = sh.GetDefaultValue(x => x.CategoryHoverClose);
}
else
{
if (sh.Mode != ShCfg.ShortcutMode.Default)
sh.Command = sh.GetDefaultValue(x => x.Command);
CleanShortcut(sh.SubList);
}
if (sh.Type == ShCfg.ShortcutType.Spacer)
{
sh.Command = sh.GetDefaultValue(x => x.Command);
sh.Mode = sh.GetDefaultValue(x => x.Mode);
}
if (!sh.Name.Contains("::"))
{
sh.IconZoom = sh.GetDefaultValue(x => x.IconZoom);
sh.IconOffset = sh.GetDefaultValue(x => x.IconOffset);
sh.CooldownAction = sh.GetDefaultValue(x => x.CooldownAction);
sh.CooldownStyle = sh.GetDefaultValue(x => x.CooldownStyle);
}
//else if (sh.ColorAnimation == 0)
// sh.ColorBg = sh.GetDefaultValue(x => x.ColorBg);
sh._i = sh.GetDefaultValue(x => x._i);
}
public static T CopyObject<T>(T o)
{
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, SerializationBinder = qolSerializer };
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(o, settings), settings);
}
public static string SerializeObject(object o, bool saveAllValues) => !saveAllValues
? JsonConvert.SerializeObject(o, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
SerializationBinder = qolSerializer
})
: JsonConvert.SerializeObject(o, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
public static T DeserializeObject<T>(string o) => JsonConvert.DeserializeObject<T>(o, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
SerializationBinder = qolSerializer
});
public static string CompressString(string s)
{
var bytes = Encoding.UTF8.GetBytes(s);
using var ms = new MemoryStream();
using (var gs = new GZipStream(ms, CompressionMode.Compress))
gs.Write(bytes, 0, bytes.Length);
return Convert.ToBase64String(ms.ToArray());
}
public static string DecompressString(string s)
{
var data = Convert.FromBase64String(s);
using var ms = new MemoryStream(data);
using var gs = new GZipStream(ms, CompressionMode.Decompress);
using var r = new StreamReader(gs);
return r.ReadToEnd();
}
public static string ExportObject(object o, bool saveAllValues) => CompressString(SerializeObject(o, saveAllValues));
public static T ImportObject<T>(string import) => DeserializeObject<T>(DecompressString(import));
public static string ExportBar(BarCfg bar, bool saveAllValues)
{
if (!saveAllValues)
{
bar = CopyObject(bar);
CleanBarConfig(bar);
}
return ExportObject(new ExportInfo { b2 = bar }, saveAllValues);
}
public static bool allowImportConditions = false;
public static bool allowImportHotkeys = false;
public static BarConfig ImportBar(string import)
{
var bar = ImportObject<BarConfig>(import);
if (!allowImportConditions)
bar.ConditionSet = bar.GetDefaultValue(x => x.ConditionSet);
if (!allowImportHotkeys)
{
static void removeHotkeys(Shortcut sh)
{
sh.Hotkey = sh.GetDefaultValue(x => x.Hotkey);
sh.KeyPassthrough = sh.GetDefaultValue(x => x.KeyPassthrough);
if (sh.SubList is not { Count: > 0 }) return;
foreach (var sub in sh.SubList)
removeHotkeys(sub);
}
foreach (var sh in bar.ShortcutList)
removeHotkeys(sh);
}
return bar;
}
public static string ExportShortcut(ShCfg sh, bool saveAllValues)
{
if (!saveAllValues)
{
sh = CopyObject(sh);
CleanShortcut(sh);
}
return ExportObject(new ExportInfo { s2 = sh }, saveAllValues);
}
public static Shortcut ImportShortcut(string import)
{
var sh = ImportObject<Shortcut>(import);
if (!allowImportHotkeys)
{
static void removeHotkeys(Shortcut sh)
{
sh.Hotkey = sh.GetDefaultValue(x => x.Hotkey);
sh.KeyPassthrough = sh.GetDefaultValue(x => x.KeyPassthrough);
if (sh.SubList is not { Count: > 0 }) return;
foreach (var sub in sh.SubList)
removeHotkeys(sub);
}
removeHotkeys(sh);
}
return sh;
}
public static bool allowExportingSensitiveConditionSets = false;
public static string ExportConditionSet(CndSetCfg cndSet) => ExportObject(new ExportInfo { cs = cndSet }, false);
public static ImportInfo TryImport(string import, bool printError = false)
{
ExportInfo imported;
try { imported = ImportObject<ExportInfo>(import); }
catch (Exception e)
{
// If we failed to import the ExportInfo then this is an old version
imported = ImportLegacy(import);
if (imported == null && printError)
{
DalamudApi.LogError($"Invalid import string!\n{e}");
switch (e)
{
case FormatException:
QoLBar.PrintError("Failed to import from clipboard! Import string is invalid or incomplete.");
break;
case JsonSerializationException:
QoLBar.PrintError("Failed to import from clipboard! Import string does not contain an importable object.");
break;
default:
QoLBar.PrintError($"Failed to import from clipboard!\n{e}");
break;
}
}
}
if (imported != null)
{
Legacy.UpdateImport(imported);
if (imported.b1 != null)
imported.b2 = imported.b1.Upgrade();
else if (imported.s1 != null)
imported.s2 = imported.s1.Upgrade(new BarConfig(), false);
var conditionRemoved = false;
if (!allowImportConditions && imported.b2 != null)
{
var d = imported.b2.GetDefaultValue(x => x.ConditionSet);
if (imported.b2.ConditionSet != d)
{
imported.b2.ConditionSet = d;
conditionRemoved = true;
}
}
var pieRemoved = false;
var hotkeyRemoved = false;
if (!allowImportHotkeys)
{
void removeHotkeys(ShCfg sh)
{
var d = sh.GetDefaultValue(x => x.Hotkey);
if (sh.Hotkey != d)
{
sh.Hotkey = d;
sh.KeyPassthrough = sh.GetDefaultValue(x => x.KeyPassthrough);
hotkeyRemoved = true;
}
if (sh.SubList is not { Count: > 0 }) return;
foreach (var sub in sh.SubList)
removeHotkeys(sub);
}
if (imported.b2 != null)
{
var d = imported.b2.GetDefaultValue(x => x.Hotkey);
if (imported.b2.Hotkey != d)
{
imported.b2.Hotkey = d;
hotkeyRemoved = true;
pieRemoved = true;
}
foreach (var sh in imported.b2.ShortcutList)
removeHotkeys(sh);
}
if (imported.s2 != null)
removeHotkeys(imported.s2);
}
if (printError)
{
var msg = "This import contained {0} automatically removed, please enable \"{1}\" inside the \"Settings\" tab on the plugin config and then try importing again if you did not intend to do this.";
if (conditionRemoved)
QoLBar.PrintEcho(string.Format(msg, "a condition set that was", "Allow importing conditions"));
if (hotkeyRemoved)
QoLBar.PrintEcho(string.Format(msg, "one or more hotkeys that were", "Allow importing hotkeys"));
if (pieRemoved)
QoLBar.PrintEcho("It appears that this bar was meant to be used as a pie. You should add a hotkey to it by right clicking on the bar and clicking the \"Pie Hotkey\" input box.");
}
}
return new ImportInfo
{
bar = imported?.b2,
shortcut = imported?.s2,
conditionSet = imported?.cs
};
}
private static ExportInfo ImportLegacy(string import)
{
var imported = new ExportInfo();
try { imported.b1 = ImportBar(import); }
catch
{
try { imported.s1 = ImportShortcut(import); }
catch { return null; }
}
imported.v = "1.3.2.0";
return imported;
}
}