/
DataManager.cs
663 lines (556 loc) · 23.2 KB
/
DataManager.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using UnityEngine;
using Steamworks;
using UnityEngine.SceneManagement;
public class DataManager : MonoBehaviour
{
#region Vars
public static DataManager Instance;
public int Level;
public int Rank;
public int Experience;
public int Money;
/// <summary>
/// Index stores type of lootbox, value - its count
/// </summary>
public int[] Lootboxes;
/// <summary>
/// Used to store available inventory client-side.
/// Destroy on release
/// </summary>
public bool[,] AvailableItems;
[Header("Customization")]
public int[] CurrentCustomizationProfile;
[Space]
public CustomizationItem[] BodyColors;
[Space]
public CustomizationItem[] Faces;
[Space]
public CustomizationItem[] Hats;
[Space]
public CustomizationItem[] Accessories;
[Space]
public CustomizationItem[] Emotions;
[Space]
public CustomizationItem[] _Lootboxes;
[Space]
public UnityEngine.U2D.SpriteAtlas RankAtlas;
/// <summary>
/// Items available from the very beginning
/// </summary>
private List<int> StarterPack = new List<int>() { 200 };
/// Callbacks
public SteamInventoryResult_t InventoryResult, LootboxResult, ItemResult;
private CallResult<RemoteStorageFileReadAsyncComplete_t> m_FileReadAsync;
private CallResult<SteamInventoryFullUpdate_t> m_InventoryFullUpdate;
private Callback<SteamInventoryResultReady_t> m_InventoryResultReady;
private Callback<SteamInventoryResultReady_t> m_LootboxResultReady;
private Callback<SteamInventoryResultReady_t> m_ItemResultReady;
/// Other
public static Dictionary<int, int> LootboxRecipes = new Dictionary<int, int>();
private int _lootboxesRequested;
private SteamItemDetails_t[] _receivedLootboxes;
#endregion
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
Init();
LoadData();
SceneManager.sceneLoaded += OnSceneLoaded;
}
else Destroy(gameObject);
}
private void Start()
{
if (SteamManager.Initialized)
{
Debug.Log("@Data: Registered handlers");
m_InventoryResultReady = Callback<SteamInventoryResultReady_t>.Create(OnInventoryResultReady);
m_InventoryFullUpdate = CallResult<SteamInventoryFullUpdate_t>.Create(OnInventoryFullUpdate);
m_ItemResultReady = Callback<SteamInventoryResultReady_t>.Create(OnItemResultReady);
GetInventory();
}
}
string Test(string initial)
{
/// Testing "overhead" directories (roots)
var root = Directory.GetDirectoryRoot(Application.persistentDataPath);
var dirs = Directory.GetDirectories(root);
string msg = "Dirs:\n";
foreach (var dir in dirs)
{
msg += dir + "\n";
}
Debug.LogWarning(msg);
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
byte[] enc = EncryptStringToBytes_Aes(initial, aes.Key, aes.IV);
string roundtrip = DecryptStringFromBytes_Aes(enc, aes.Key, aes.IV);
return roundtrip;
}
}
private void Init()
{
Level = Rank = 1;
CurrentCustomizationProfile = new int[8];
Lootboxes = new int[5];
AvailableItems = new bool[4, 32];
}
#region ----------------- Save & load -----------------
private void LoadData()
{
if (!SteamManager.Initialized)
{
Debug.LogError("LoadData: Steam API not initialized");
return;
}
/// Process downloaded data
if (File.Exists(Application.persistentDataPath + "/savedGames.gd"))
{
print("Loading data");
var encrypted = File.ReadAllBytes(Application.persistentDataPath + "/savedGames.gd");
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
/// Key, IV
if (File.Exists(Application.persistentDataPath + "/savedGames.g"))
{
myAes.Key = File.ReadAllBytes(Application.persistentDataPath + "/savedGames.g");
myAes.IV = File.ReadAllBytes(Application.persistentDataPath + "/savedGames.d");
}
else
{
Debug.LogError("Unable to load saved data");
return;
}
string json = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
var data = JsonUtility.FromJson<SaveData>(json);
Level = data.Level;
Experience = data.XP;
Money = data.Money;
Rank = data.Rank;
Lootboxes = data.Lootboxes;
CurrentCustomizationProfile = data.CustomizationProfile;
AvailableItems = data.AvailableItems;
}
}
else Debug.LogWarning("Saved data not found");
/// Apply
StartCoroutine(Utility.DelayedAction(0.05f, () =>
{
/// Get first 4 items
var preset = new int[4];
for (int i = 0; i < 4; i++)
{
preset[i] = CurrentCustomizationProfile[i];
}
MainMenuManager.Instance.PlayerGameobjectAvatar.GetComponent<Customizer>().Preset = preset;
}));
}
private void OnSteamCloudDataLoaded(RemoteStorageFileReadAsyncComplete_t pCallback, bool bIOFailure)
{
print("Data loaded, success: " + bIOFailure);
if (pCallback.m_eResult == EResult.k_EResultOK)
{
byte[] data = new byte[pCallback.m_cubRead];
if (SteamRemoteStorage.FileReadAsyncComplete(pCallback.m_hFileReadAsync, data, pCallback.m_cubRead))
{
string m_Message = System.Text.Encoding.UTF8.GetString(data, (int)pCallback.m_nOffset, (int)pCallback.m_cubRead);
Debug.LogWarning("RemoteStorageFileReadAsyncComplete: Got data from SteamCloud: " + m_Message);
}
}
}
public void SaveData()
{
if (!SteamManager.Initialized)
{
Debug.LogError("SaveData: Steam API not initialized");
return;
}
if (C.LOG_DM) Debug.Log("Saving data");
int[] stats = new int[]
{
Level, Experience, Money, Rank
};
var data = new SaveData(stats, CurrentCustomizationProfile, Lootboxes, AvailableItems);
/// Encrypt file and send to SteamCloud
/// Order for encrypt: json -> aes -> binary;
/// Reverse order for decrypt
/// Using JSON
var json = JsonUtility.ToJson(data);
/// Using Aes
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
/// Key, IV
if (!File.Exists(Application.persistentDataPath + "/savedGames.g") /*|| !File.Exists(Application.persistentDataPath + "/savedGames.d")*/)
{
print("Creating keys");
File.WriteAllBytes(Application.persistentDataPath + "/savedGames.g", myAes.Key);
File.WriteAllBytes(Application.persistentDataPath + "/savedGames.d", myAes.IV);
}
else
{
myAes.Key = File.ReadAllBytes(Application.persistentDataPath + "/savedGames.g");
myAes.IV = File.ReadAllBytes(Application.persistentDataPath + "/savedGames.d");
}
var encrypted = EncryptStringToBytes_Aes(json, /*_key*/ myAes.Key, /*_iv*/ myAes.IV);
File.WriteAllBytes(Application.persistentDataPath + "/savedGames.gd", encrypted);
}
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
/// Check arguments
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
/// Create an AesCryptoServiceProvider object
/// with the specified key and IV.
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
/// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
/// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
/// Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
/// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
/// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
/// Declare the string used to hold
/// the decrypted text.
string plaintext = null;
/// Create an AesCryptoServiceProvider object
/// with the specified key and IV.
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
/// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
/// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
/// Read the decrypted bytes from the decrypting stream
/// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
#endregion
#region ----------------- Customization & rewards -----------------
/// <summary>
/// Returns model/sprite object from database.
/// Null if object not found by type & id
/// </summary>
public UnityEngine.Object GetItemObject(CustomizationItemType type, int id)
{
CustomizationItem[] itemArray = new CustomizationItem[0];
switch (type)
{
case CustomizationItemType.Color:
itemArray = BodyColors;
break;
case CustomizationItemType.Face:
itemArray = Faces;
break;
case CustomizationItemType.Hat:
itemArray = Hats;
break;
case CustomizationItemType.Accessory:
itemArray = Accessories;
break;
case CustomizationItemType.Emotion:
itemArray = Emotions;
break;
}
for (int i = 0; i < itemArray.Length; i++)
{
if (itemArray[i].ID == id)
return itemArray[i].ModelPrefab;
}
return null;
}
/// <summary>
/// Stores given rewards to be displayed in main menu
/// </summary>
public void StoreRewards(bool wonGame, int frags)
{
if (C.LOG_DM) Debug.Log("Storing rewards; Won game: " + wonGame + ", frags: " + frags.ToString());
var reward = new StoredReward();
if (wonGame)
{
reward.Experience = C.WIN_XP + (frags * C.FRAG_XP);
reward.Money = C.WIN_MONEY + (frags * C.FRAG_MONEY);
}
else
{
reward.Experience = C.LOSS_XP + (frags * C.FRAG_XP);
reward.Money = C.LOSS_MONEY + (frags * C.FRAG_MONEY);
}
GameManager.Instance.GotRewards = true;
GameManager.Instance.Reward = reward;
if (C.LOG_DM) Debug.Log("Stored reward: Money = " + reward.Money.ToString() + ", XP = " + reward.Experience);
}
/// <summary>
/// Applies stored rewards
/// </summary>
public void ApplyRewards()
{
var gm = GameManager.Instance;
if (C.LOG_DM) Debug.Log("Applying reward: Money = " + gm.Reward.Money.ToString() + ", XP = " + gm.Reward.Experience);
/// Applying stored rewards
Experience += gm.Reward.Experience;
Money += gm.Reward.Money;
gm.Reward = new StoredReward();
gm.GotRewards = false;
_lootboxesRequested = 0;
StartCoroutine(Utility.DelayedAction(0.5f, () => /// Don't show exactly at moment scene has loaded
{
/// Level-up logic
foreach (var xpData in C.XP_TO_LEVELUP)
{
if (Level >= xpData.Key.x && Level <= xpData.Key.y)
{
int c = 0;
while (Experience >= xpData.Value && c < 100)
{
if (C.LOG_DM) Debug.LogWarning("Level up!");
/// Xp, level
Experience -= xpData.Value;
Level++;
if (Level > C.MAX_LEVEL) Level -= C.MAX_LEVEL;
/// Rank
if (Level % C.RANKING_INTERVAL == 0)
Rank++;
if (Rank > C.MAX_RANK) Rank = C.MAX_RANK;
/// Lootbox
_lootboxesRequested++;
/// Display new stats
FindObjectOfType<MenuHUD>().UpdateStats();
/// Prevent levelup in case when it takes more (or less) xp for next level (no discounts!)
if (Level != xpData.Key.y) break;
}
if (C.LOG_DM && c >= 100) Debug.LogWarning("@DataManager ApplyRewards: Critical break of while-cycle");
}
}
/// Request lootboxes
if (_lootboxesRequested > 0)
{
if (C.LOG_DM) Debug.LogWarning("Requesting " + _lootboxesRequested + " lootboxes...");
m_LootboxResultReady = Callback<SteamInventoryResultReady_t>.Create(OnLootboxResultReady);
for (int i = 0; i < _lootboxesRequested; i++)
{
SteamInventory.TriggerItemDrop(out LootboxResult, new SteamItemDef_t(900));
}
StartCoroutine(LootboxAwait());
}
}));
}
#endregion
#region ----------------- Inventory -----------------
public void GetInventory()
{
if (C.LOG_DM) Debug.Log("Updating inventory");
if (SteamInventory.GetAllItems(out InventoryResult))
{
if (C.LOG_DM) Debug.Log("Inventory available, InventoryResult: " + InventoryResult.m_SteamInventoryResult);
}
}
private void OnInventoryResultReady(SteamInventoryResultReady_t pCallback)
{
Debug.Log("@OnInventoryResultReady called, callback params: " + pCallback.m_handle + ", " + pCallback.m_result);
InventoryResult = pCallback.m_handle;
uint size = 0;
SteamItemDetails_t[] items;
bool result = SteamInventory.GetResultItems(InventoryResult, null, ref size);
if (result && size > 0)
{
items = new SteamItemDetails_t[size];
bool ret = SteamInventory.GetResultItems(InventoryResult, items, ref size);
Debug.LogWarning("@OnInventoryResultReady: Inventory fetch result: " + ret + " with items num: " + items.Length);
/// Add anew
UpdateInventory(items, display: false);
}
else
{
Debug.LogWarning("@OnInventoryResultReady: InventoryResult = " + InventoryResult.m_SteamInventoryResult + ", Size = " + size + ". Could not get result items, problem may be in:\n" +
"- InventoryResult оказался недействительным, либо дескриптор результата действий с инвентарём оказался не готов.\n" +
"- Массив не уместился в pOutItemsArray.\n" +
"- У пользователя нет предметов.");
}
/// Dispose result
SteamInventory.DestroyResult(InventoryResult);
}
private void OnLootboxResultReady(SteamInventoryResultReady_t pCallback)
{
Debug.Log("@OnLootboxResultReady called, callback params: " + pCallback.m_handle + ", " + pCallback.m_result);
LootboxResult = pCallback.m_handle;
uint size = 0;
SteamItemDetails_t[] items;
bool result = SteamInventory.GetResultItems(LootboxResult, null, ref size);
if (result && size > 0)
{
items = new SteamItemDetails_t[size];
result = SteamInventory.GetResultItems(LootboxResult, items, ref size);
Debug.LogWarning("@OnLootboxResultReady: Get lootbox result: " + result + " with items num: " + items.Length);
UpdateInventory(items, display: true);
/// Dispose results and update
Debug.Log("Disposing resources");
SteamInventory.DestroyResult(LootboxResult);
_lootboxesRequested -= items.Length;
if (_lootboxesRequested <= 0) m_LootboxResultReady.Dispose();
}
}
private void OnItemResultReady(SteamInventoryResultReady_t pCallback)
{
Debug.Log("@OnItemResultReady called, callback params: " + pCallback.m_handle + ", " + pCallback.m_result);
ItemResult = pCallback.m_handle;
uint size = 0;
SteamItemDetails_t[] items;
bool result = SteamInventory.GetResultItems(ItemResult, null, ref size);
if (result && size > 0)
{
items = new SteamItemDetails_t[size];
result = SteamInventory.GetResultItems(ItemResult, items, ref size);
Debug.LogWarning("@OnItemResultReady: Get item result: " + result + " with items num: " + items.Length);
UpdateInventory(items, display: true);
}
}
void UpdateInventory(SteamItemDetails_t[] items, bool display)
{
if (C.LOG_DM) Debug.Log("\nAdding new items to inventory");
List<KeyValuePair<Sprite, string>> itemsToShow = new List<KeyValuePair<Sprite, string>>();
/// Process fetched items
for (int i = 0; i < items.Length; i++)
{
if (C.LOG_DM)
Debug.Log("Item #" + (i + 1).ToString() +
":\n\tDefinition: " + items[i].m_iDefinition +
"\n\tItem ID: " + items[i].m_itemId +
"\n\tQuantity: " + items[i].m_unQuantity +
"\n\tFlags: " + items[i].m_unFlags);
/// Hats
for (int k = 0; k < Hats.Length; k++)
{
if (items[i].m_iDefinition.m_SteamItemDef == Hats[k].ID)
{
if (items[i].m_unQuantity == 0)
{
if (Hats[k].Quantity > 0)
{
Hats[k].Quantity--;
Hats[k].Details.Remove(items[i]);
}
if (Hats[k].Quantity == 0)
{
Hats[k].IsAvailable = false;
}
}
else /// either it will be 1
{
Hats[k].IsAvailable = true;
Hats[k].Quantity++;
Hats[k].Details.Add(items[i]);
itemsToShow.Add(new KeyValuePair<Sprite, string>(Hats[k].PreviewImage, Hats[k].Name));
}
}
}
/// Lootbox
for (int k = 0; k < _Lootboxes.Length; k++)
{
if (items[i].m_iDefinition.m_SteamItemDef == _Lootboxes[k].ID)
{
if (items[i].m_unQuantity == 0)
{
if (_Lootboxes[k].Quantity > 0)
{
_Lootboxes[k].Quantity--;
_Lootboxes[k].Details.Remove(items[i]);
}
if (_Lootboxes[k].Quantity == 0)
{
_Lootboxes[k].IsAvailable = false;
}
}
else
{
_Lootboxes[k].IsAvailable = true;
_Lootboxes[k].Quantity++;
_Lootboxes[k].Details.Add(items[i]);
itemsToShow.Add(new KeyValuePair<Sprite, string>(_Lootboxes[k].PreviewImage, _Lootboxes[k].Name));
}
}
}
}
if (display)
MainMenuManager.Instance.ShowNewItems(itemsToShow);
}
/// <summary>
/// Sets all items unavailable (except given by default at the very beginning).
/// Should be called before Steam inventory is updated
/// </summary>
void FlushInventory()
{
/// Hats
for (int i = 0; i < Hats.Length; i++)
{
Hats[i].IsAvailable = StarterPack.Contains(Hats[i].ID);
Hats[i].Details.Clear();
}
/// Lootboxes
for (int i = 0; i < _Lootboxes.Length; i++)
{
_Lootboxes[i].IsAvailable = StarterPack.Contains(_Lootboxes[i].ID);
_Lootboxes[i].Details.Clear();
}
}
#endregion
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
print("Scene loaded: " + scene.name);
Cursor.visible = true;
}
private void OnApplicationQuit()
{
SaveData();
}
}