-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPlayFabMigrateTitlePackage.cs
735 lines (662 loc) · 29.7 KB
/
PlayFabMigrateTitlePackage.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using PlayFab.AdminModels;
using PlayFabPowerTools.Services;
namespace PlayFabPowerTools.Packages
{
public class PlayFabMigrateTitlePackage : iStatePackage
{
private enum States
{
Idle,
TitleData,
TitleInternalData,
CloudScript,
Files,
Currency,
Catalogs,
DropTables,
Stores,
Complete
}
private States _state = States.Idle;
private struct commandArgs
{
public string FromTitleId;
public string ToTitleId;
}
private commandArgs _commandArgs;
private CancellationTokenSource _cts = new CancellationTokenSource();
private TitleDataMigration _titleData;
private TitleDataMigration _titleInternalData;
private CurrencyDataMigration _currencyData;
private CloudScriptDataMigration _cloudScriptData;
private CdnFileDataMigration _cdnData;
private CatalogDataMigration _catalogData;
private DropTableDataMigration _droptableData;
private StoreDataMigration _storeData;
public void RegisterMainPackageStates(iStatePackage package)
{
List<MainPackageStates> states = new List<MainPackageStates>()
{
MainPackageStates.Migrate
};
PackageManagerService.RegisterMainPackageStates(states, package);
}
public bool SetState(string line)
{
//Parse Command Line args
_commandArgs = new commandArgs();
// migrate T381 T390
var argsList = line.Split(' ');
var lineSplit = argsList.ToList().FindAll(s => !s.ToLower().Contains("setstores") && !string.IsNullOrEmpty(s)).ToList();
if (lineSplit.Count != 3)
{
Console.WriteLine("usage migrate [From TitleId] [To TitleId]");
_state = States.Complete;
return false;
}
_commandArgs.FromTitleId = lineSplit[1];
_commandArgs.ToTitleId = lineSplit[2];
//SetUp Data Objects
_titleData = new TitleDataMigration();
_titleInternalData = new TitleDataMigration();
_currencyData = new CurrencyDataMigration();
_cloudScriptData = new CloudScriptDataMigration();
_cdnData = new CdnFileDataMigration();
_catalogData = new CatalogDataMigration();
_droptableData = new DropTableDataMigration();
_storeData = new StoreDataMigration
{
StoreList = PlayFabService.Settings.StoreList
};
SetNextState();
return false;
}
public bool Loop()
{
_cts = new CancellationTokenSource();
var worker = new Task(() =>
{
while (!_cts.Token.WaitHandle.WaitOne(2000))
{
Execute();
}
//_cts.Token.ThrowIfCancellationRequested();
},_cts.Token, TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning);
//_worker.ContinueWith(x =>
//{
// Trace.TraceError(x.Exception.InnerException.Message);
//}, TaskContinuationOptions.OnlyOnFaulted);
worker.Start();
do
{
//block until _worker is completed.
} while (!_cts.IsCancellationRequested);
//Console.WriteLine("exit migrate");
return false;
}
private void SetNextState()
{
switch (_state)
{
case States.Complete:
case States.Idle:
_state = States.TitleData;
break;
case States.TitleData:
_state = States.TitleInternalData;
break;
case States.TitleInternalData:
_state = States.Currency;
break;
case States.Currency:
_state = States.CloudScript;
break;
case States.CloudScript:
_state = States.Files;
break;
case States.Files:
_state = States.Catalogs;
break;
case States.Catalogs:
_state = States.Stores;
break;
case States.Stores:
_state = States.DropTables;
break;
default:
_state = States.Complete;
break;
}
}
private void Execute()
{
switch (_state)
{
case States.TitleData:
#region Update Title Data Keys
if (!_titleData.FromProcessed)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Getting Title Data from: " + _commandArgs.FromTitleId);
PlayFabService.GetTitleData(_commandArgs.FromTitleId,(success,result) =>
{
if (!success || result.Data.Count == 0)
{
Console.WriteLine("No Title Data found, skipping");
SetNextState();
}
else
{
Console.WriteLine("Title Data Keys Found: " + result.Data.Count.ToString());
_titleData.Data = result.Data;
_titleData.FromProcessed = true;
}
});
}
if (!_titleData.ToProcessed && _titleData.FromProcessed)
{
if (_titleData.Data.Count == 0)
{
_titleData.ToProcessed = true;
SetNextState();
break;
}
var kvp = _titleData.Pop();
Console.WriteLine("Saving Title Data from: " + _commandArgs.FromTitleId + " To: " + _commandArgs.ToTitleId);
PlayFabService.UpdateTitleData(_commandArgs.ToTitleId, kvp , (success) =>
{
if (!success) {
Console.WriteLine("Save Title Data Failed, skipping");
SetNextState();
}
});
}
#endregion
break;
case States.TitleInternalData:
#region Update Title Internal Data Keys
if (!_titleInternalData.FromProcessed)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Getting Title Interal Data from: " + _commandArgs.FromTitleId);
PlayFabService.GetTitleInternalData(_commandArgs.FromTitleId, (success, result) =>
{
if (!success || result.Data.Count == 0)
{
Console.WriteLine("No Title Internal Data found, skipping");
SetNextState();
}
else
{
Console.WriteLine("Title Internal Data Keys Found: " + result.Data.Count.ToString());
_titleInternalData.Data = result.Data;
_titleInternalData.FromProcessed = true;
}
});
}
if (!_titleInternalData.ToProcessed && _titleInternalData.FromProcessed)
{
if (_titleInternalData.Data.Count == 0)
{
_titleInternalData.ToProcessed = true;
SetNextState();
break;
}
var kvp = _titleInternalData.Pop();
Console.WriteLine("Saving Title Interal Data from: " + _commandArgs.FromTitleId + " To: " + _commandArgs.ToTitleId);
PlayFabService.UpdateTitleInternalData(_commandArgs.ToTitleId, kvp, (success) =>
{
if (!success)
{
Console.WriteLine("Save Title Interal Data Failed, skipping");
SetNextState();
}
});
}
#endregion
break;
case States.Currency:
#region Update Currency Types
if (!_currencyData.FromProcessed)
{
Console.WriteLine("Getting Currency Types Data from: " + _commandArgs.FromTitleId);
PlayFabService.GetCurrencyData(_commandArgs.FromTitleId, (success, vcData) =>
{
if (!success || vcData.Count == 0)
{
Console.WriteLine("Error Fetching Currency Type Data, skipping");
SetNextState();
return;
}
_currencyData.Data = vcData;
_currencyData.FromProcessed = true;
});
}
if (!_currencyData.ToProcessed && _currencyData.FromProcessed)
{
if (_currencyData.Data == null)
{
_currencyData.ToProcessed = true;
SetNextState();
break;
}
PlayFabService.UpdateCurrencyData(_commandArgs.ToTitleId, _currencyData.Data,
(success) =>
{
if (!success)
{
Console.WriteLine("Save Title Data Failed.");
_cts.Cancel();
}
_currencyData.Data = null;
});
}
#endregion
break;
case States.CloudScript:
#region Update CloudScript File
if (!_cloudScriptData.FromProcessed)
{
Console.WriteLine("Getting CloudScript Data from: " + _commandArgs.FromTitleId);
PlayFabService.GetCloudScript(_commandArgs.FromTitleId, (success, data) =>
{
if (!success || data.Count == 0)
{
Console.WriteLine("Error Fetching CloudScript Data, skipping.");
SetNextState();
return;
}
_cloudScriptData.Data = data;
_cloudScriptData.FromProcessed = true;
});
}
if (!_cloudScriptData.ToProcessed && _cloudScriptData.FromProcessed)
{
if (_cloudScriptData.Data == null)
{
_cloudScriptData.ToProcessed = true;
SetNextState();
break;
}
Console.WriteLine("Updating CloudScript on Title: " + _commandArgs.ToTitleId);
PlayFabService.UpdateCloudScript(_commandArgs.ToTitleId, _cloudScriptData.Data,
(success) =>
{
//if (!success)
//{
// Console.WriteLine("Save CloudScript Failed.");
// _cts.Cancel();
//}
_cloudScriptData.Data = null;
});
}
#endregion
break;
case States.Files:
#region Update Content Files
//Start by creating a temp directory
var path = AppDomain.CurrentDomain.BaseDirectory + "temp";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!_cdnData.FromProcessed)
{
PlayFabService.GetContentList(_commandArgs.FromTitleId, (success, data) =>
{
if (!success)
{
Console.WriteLine("Error Fetching CloudScript Data, skipping");
SetNextState();
return;
}
_cdnData.Data = data;
_cdnData.FromProcessed = true;
});
}
if (!_cdnData.ToProcessed && _cdnData.FromProcessed)
{
if (_cdnData.Data.Count == 0 && _cdnData.FileList.Count == 0)
{
_cdnData.ToProcessed = true;
_cdnData.Data = null;
_cdnData.FileList = null;
Directory.Delete(path, true);
SetNextState();
break;
}
if (_cdnData.Data.Count > 0)
{
var isDone = false;
PlayFabService.DownloadFile(_commandArgs.FromTitleId, path, _cdnData.popData(), (success, filePath, data) =>
{
if (success)
{
_cdnData.FileList.Add(new CdnFileDataMigration.UploadFile()
{
Data = data,
FilePath = filePath
});
}
isDone = true;
});
do
{
//block until done.
} while (!isDone);
break;
}
if (_cdnData.Data.Count == 0 && _cdnData.FileList.Count > 0)
{
var isUploadDone = false;
PlayFabService.UploadFile(_commandArgs.ToTitleId, _cdnData.popFileList(), (success) =>
{
isUploadDone = true;
});
do
{
//Block until this file upload is done.
} while (!isUploadDone);
}
}
#endregion
break;
case States.Catalogs:
#region Update Catalog
if (_catalogData.FromProcessed && _catalogData.ToProcessed)
{
SetNextState();
break;
}
if (!_catalogData.FromProcessed)
{
//TODO: Make this support multiple catalogs
Console.WriteLine("Getting Catalog Data for Main Catalog only");
PlayFabService.GetCatalogData(_commandArgs.FromTitleId,
(success, catalogVersion, catalog) =>
{
if (!success)
{
Console.WriteLine("Error Fetching CloudScript Data, skipping");
SetNextState();
}
_catalogData.Data = catalog;
_catalogData.CatalogVersion = catalogVersion;
_catalogData.FromProcessed = true;
});
}
if (!_catalogData.ToProcessed && _catalogData.FromProcessed)
{
Console.WriteLine("Updating Catalog on Title: " + _commandArgs.ToTitleId);
PlayFabService.UpdateCatalogData(_commandArgs.ToTitleId, _catalogData.CatalogVersion, true, _catalogData.Data,
(success) =>
{
if (!success)
{
Console.WriteLine("Save Catalog Failed, skipping.");
SetNextState();
}
_catalogData.Data = null;
_catalogData.CatalogVersion = null;
_catalogData.ToProcessed = true;
});
}
#endregion
break;
case States.Stores:
#region Update Stores
if (_storeData.IsComplete)
{
_storeData.Data = null;
_storeData.MarketingModel = null;
_storeData.StoreId = null;
_storeData.CatalogVersion = null;
SetNextState();
break;
}
if (!_storeData.FromProcessed)
{
if (_storeData.StoreList.Count == 0)
{
SetNextState();
break;
}
var currentStoreId = _storeData.popStoreId();
Console.WriteLine("Getting Store Data for StoreID: " + currentStoreId);
PlayFabService.GetStoreData(_commandArgs.FromTitleId, currentStoreId,
(success, catalogVersion, storeId, marketingModel, store) =>
{
if (!success)
{
Console.WriteLine("Error Fetching Store Data, Skipping.");
SetNextState();
}
_storeData.FromProcessed = true;
_storeData.Data = store;
_storeData.CatalogVersion = catalogVersion;
_storeData.StoreId = storeId;
_storeData.MarketingModel = marketingModel;
});
}
if (!_storeData.ToProcessed && _storeData.FromProcessed)
{
var currentStoreId = _storeData.StoreId;
PlayFabService.UpdateStoreData(_commandArgs.ToTitleId, _storeData.StoreId, _storeData.CatalogVersion, _storeData.MarketingModel, _storeData.Data,
(success) =>
{
if (!success)
{
Console.WriteLine("Save Store Failed, Skipping.");
SetNextState();
}
_storeData.Data = null;
_storeData.CatalogVersion = null;
if (_storeData.StoreList.Count == 0)
{
_storeData.ToProcessed = true;
_storeData.IsComplete = true;
}
else
{
_storeData.ToProcessed = false;
_storeData.FromProcessed = false;
}
});
}
#endregion
break;
case States.DropTables:
#region Update Drop Tables
if (_droptableData.FromProcessed && _droptableData.ToProcessed)
{
SetNextState();
break;
}
if (!_droptableData.FromProcessed)
{
//TODO: Make this support multiple catalogs
Console.WriteLine("Getting Drop Table Data for Main Catalog only");
PlayFabService.GetDropTableData(_commandArgs.FromTitleId,
(success, catalog) =>
{
if (!success)
{
Console.WriteLine("Error Fetching CloudScript Data, skipping");
SetNextState();
}
_droptableData.Data = new Queue<List<RandomResultTable>>();
Dictionary<string, RandomResultTableListing> listing = new Dictionary<string, RandomResultTableListing>(catalog);
List<RandomResultTable> thisList = new List<RandomResultTable>();
foreach (var v in listing)
{
bool referencesDropTable = false;
foreach (var item in v.Value.Nodes)
{
if (item.ResultItemType == ResultTableNodeType.TableId)
{
referencesDropTable = true;
break;
}
}
if (!referencesDropTable)
{
RandomResultTable table = new RandomResultTable();
table.TableId = v.Value.TableId;
table.Nodes = v.Value.Nodes;
thisList.Add(table);
}
}
if (thisList.Count != 0)
{
while (true)
{
_droptableData.Data.Enqueue(thisList);
foreach (var item in thisList) //remove from pending list
{
listing.Remove(item.TableId);
}
//Add any references
List<RandomResultTable> nextList = new List<RandomResultTable>();
foreach (var item in listing)
{
bool referencesCurrent = false;
foreach (var node in item.Value.Nodes)
{
if (node.ResultItemType == ResultTableNodeType.TableId && thisList.Any(x => x.TableId == node.ResultItem ))
{
referencesCurrent = true;
break;
}
}
if (referencesCurrent)
{
RandomResultTable table = new RandomResultTable();
table.TableId = item.Value.TableId;
table.Nodes = item.Value.Nodes;
nextList.Add(table);
}
}
if (nextList.Count == 0)
{
break;
}
thisList = nextList;
}
}
_droptableData.FromProcessed = true;
});
}
if (!_droptableData.ToProcessed && _droptableData.FromProcessed)
{
Console.WriteLine("Updating Drop Tables on Title: " + _commandArgs.ToTitleId);
PlayFabService.UpdateDropTableData(_commandArgs.ToTitleId, _droptableData.Data.Dequeue(),
(success) =>
{
if (!success)
{
Console.WriteLine("Save Catalog Failed, skipping.");
SetNextState();
}
//_catalogData.Data = null;
//_catalogData.CatalogVersion = null;
if (_droptableData.Data.Count == 0)
{
_droptableData.Data = null;
_droptableData.ToProcessed = true;
}
});
}
#endregion
break;
case States.Complete:
Console.WriteLine("Migration Complete.");
Console.ForegroundColor = ConsoleColor.White;
PackageManagerService.SetState(MainPackageStates.Idle);
_cts.Cancel();
break;
}
}
}
public class DataMigration
{
public bool ToProcessed;
public bool FromProcessed;
}
public class StoreDataMigration : DataMigration
{
public List<StoreItem> Data;
public string CatalogVersion;
public string StoreId;
public StoreMarketingModel MarketingModel;
public List<string> StoreList = new List<string>();
public bool IsComplete;
public string popStoreId()
{
var storeid = StoreList[0];
StoreList.Remove(storeid);
return storeid;
}
}
public class CatalogDataMigration : DataMigration
{
public List<CatalogItem> Data;
public string CatalogVersion;
}
public class DropTableDataMigration : DataMigration
{
public Queue<List<PlayFab.AdminModels.RandomResultTable>> Data;
}
public class CdnFileDataMigration : DataMigration
{
public List<ContentInfo> Data = new List<ContentInfo>();
public List<UploadFile> FileList = new List<UploadFile>();
public class UploadFile
{
public ContentInfo Data;
public string FilePath;
}
public ContentInfo popData()
{
var data = Data[0];
Data.Remove(data);
return data;
}
public UploadFile popFileList()
{
var data = FileList[0];
FileList.Remove(data);
return data;
}
}
public class CloudScriptDataMigration : DataMigration
{
public List<CloudScriptFile> Data = new List<CloudScriptFile>();
}
public class CurrencyDataMigration : DataMigration
{
public List<PlayFab.AdminModels.VirtualCurrencyData> Data = new List<VirtualCurrencyData>();
}
public class TitleDataMigration : DataMigration
{
public Dictionary<string,string> Data = new Dictionary<string, string>();
public KeyValuePair<string, string> Pop()
{
Dictionary<string, string> _tempDictionary = Data;
foreach (var kvp in _tempDictionary)
{
Data.Remove(kvp.Key);
return kvp;
}
return new KeyValuePair<string, string>();
}
}
}