-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathScriptPlatformInterface.cpp
More file actions
1204 lines (1050 loc) · 34.6 KB
/
ScriptPlatformInterface.cpp
File metadata and controls
1204 lines (1050 loc) · 34.6 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
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=============================================================================
ScriptPlatformInterface.cpp: Base functionality for the various script accessible platform-interface code
Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.
=============================================================================*/
#include "EnginePrivate.h"
#include "EngineGameEngineClasses.h"
#include "EnginePlatformInterfaceClasses.h"
#include "AES.h"
#if IPHONE
#include "IPhoneObjCWrapper.h"
#endif
IMPLEMENT_CLASS(UPlatformInterfaceBase);
IMPLEMENT_CLASS(UPlatformInterfaceWebResponse);
IMPLEMENT_CLASS(UCloudStorageBase);
IMPLEMENT_CLASS(UCloudStorageUpgradeHelper);
IMPLEMENT_CLASS(UMicroTransactionBase);
IMPLEMENT_CLASS(UAnalyticEventsBase);
IMPLEMENT_CLASS(UTwitterIntegrationBase);
IMPLEMENT_CLASS(UAppNotificationsBase);
IMPLEMENT_CLASS(UInAppMessageBase);
// @todo: For now this is copied from UnEngine.cpp, we could try to merge code at some point
static UINT EncryptedMagic = 0xC0DEDBAD;
/**
* Load a file and possibly decrypt if it has the magic tag
*/
static UBOOL LoadFileAndDecrypt(TArray<BYTE>& Result, const TCHAR* Filename)
{
Result.Empty();
// always decrypt data
TArray<BYTE> LoadedData;
if (!appLoadFileToArray(LoadedData, Filename))
{
return FALSE;
}
// is the first 4 bytes the magic?
if (LoadedData.Num() >= 4 && ((UINT*)LoadedData.GetData())[0] == EncryptedMagic)
{
// make sure the encrypted data is a multiple of 16 bytes
if (((LoadedData.Num() - 4) & 15) != 0)
{
debugf(TEXT("Loading an encrypted file (%s) that wasn't padded to 16 bytes, which it must have been to be encrypted. Failing."), Filename);
return FALSE;
}
// copy encrypted data to the output
Result.Add(LoadedData.Num() - 4);
appMemcpy(Result.GetData(), LoadedData.GetTypedData() + 4, Result.Num());
// decrypt the data
appDecryptData(Result.GetTypedData(), Result.Num());
}
else
{
// if it wasn't encrypted, just copy it over
Result = LoadedData;
}
return TRUE;
}
/**
* Encrypt a block of data, then save it to disk
*/
static UBOOL EncryptAndSaveFile(const TArray<BYTE>& Array, const TCHAR* Filename)
{
// we need to be aligned to 16 bytes
TArray<BYTE> WorkArray;
// add 4 bytes for the magic
WorkArray.Add(4);
((UINT*)WorkArray.GetData())[0] = EncryptedMagic;
// put on the input data, unencrypted
WorkArray.Append(Array);
// make sure the encrypted part is a multiple of 16
WorkArray.AddZeroed(Align(Array.Num(), 16) - Array.Num());
// encrypt the data
appEncryptData(WorkArray.GetTypedData() + 4, WorkArray.Num() - 4);
// save to disk!
return appSaveArrayToFile(WorkArray, Filename);
}
/*******************************************
* Platform Interface Base *
*******************************************/
/**
* Determines if there are any delegates of the given type on this platform interface object.
* This is useful to skip a bunch of FPlatformInterfaceDelegateResult if there is no
* one even listening!
*
* @param DelegateType The type of delegate to look up delegates for
*
* @return TRUE if there are any delegates set of the given type
*/
UBOOL UPlatformInterfaceBase::HasDelegates(INT DelegateType)
{
// has script ever put anything in for this delegate type?
if (AllDelegates.Num() > DelegateType)
{
// if so are there currently any set?
if (AllDelegates(DelegateType).Delegates.Num() > 0)
{
return TRUE;
}
}
return FALSE;
}
/**
* Call all the delegates currently set for the given delegate type with the given data
*
* @param DelegateType Which set of delegates to call (this is defined in the subclass of PlatformInterfaceBase)
* @param Result Data to pass to each delegate
*/
void UPlatformInterfaceBase::CallDelegates(INT DelegateType, FPlatformInterfaceDelegateResult& Result)
{
// make sure that script has ever put anything in for this delegate type
if (AllDelegates.Num() <= DelegateType)
{
return;
}
// call all the delegates that are set
FDelegateArray& DelegateArray = AllDelegates(DelegateType);
// copy the array in case delegates are removed from the class's delegates array
TArray<FScriptDelegate> ArrayCopy = DelegateArray.Delegates;
for (INT DelegateIndex = 0; DelegateIndex < ArrayCopy.Num(); DelegateIndex++)
{
ProcessDelegate(NAME_None, &ArrayCopy(DelegateIndex), &Result);
}
}
/**
* Check for certain exec commands that map to the various subclasses (it will only
* get/create the singleton if the first bit of the exec command matches a one of
* the special strings, like "ad" for ad manager)
*/
UBOOL UPlatformInterfaceBase::StaticExec(const TCHAR* Cmd, FOutputDevice& Ar)
{
// @todo: Ask each subclass via a static function like GetExecBaseCommand(), then
// get the singleton, and pass the rest of the command to it
if (ParseCommand( &Cmd, TEXT("Ad")))
{
UInGameAdManager* AdManager = UPlatformInterfaceBase::GetInGameAdManagerSingleton();
if (ParseCommand(&Cmd, TEXT("Show")))
{
AdManager->ShowBanner(appAtoi(Cmd));
}
else if (ParseCommand(&Cmd, TEXT("Hide")))
{
AdManager->HideBanner();
}
else if (ParseCommand(&Cmd, TEXT("Close")))
{
AdManager->ForceCloseAd();
}
return TRUE;
}
else if (ParseCommand( &Cmd, TEXT("FB")))
{
UFacebookIntegration* FB = UPlatformInterfaceBase::GetFacebookIntegrationSingleton();
if (ParseCommand(&Cmd, TEXT("auth")))
{
FB->eventAuthorize();
}
else if (ParseCommand(&Cmd, TEXT("isauthed")))
{
Ar.Logf(TEXT("Authorized? %d"), FB->eventIsAuthorized());
}
else if (ParseCommand(&Cmd, TEXT("username")))
{
Ar.Logf(TEXT("FB username is %s"), *FB->UserName);
}
else if (ParseCommand(&Cmd, TEXT("disconnect")))
{
FB->eventDisconnect();
}
return TRUE;
}
return FALSE;
}
#define IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(Class, ClassDesc) \
/** C++ interface to get the singleton */ \
Class* UPlatformInterfaceBase::Get##ClassDesc##Singleton() \
{ \
/* the singleton object */ \
static Class* Singleton = NULL; \
\
/* have we created the singleton yet? */ \
if (Singleton == NULL) \
{ \
/* load the class name from the .ini */ \
FString SingletonClassName; \
GConfig->GetString(TEXT("PlatformInterface"), TEXT(#ClassDesc) TEXT("ClassName"), SingletonClassName, GEngineIni); \
\
/* load the class (most likely intrinsic) */ \
UClass* SingletonClass = LoadClass<Class>(NULL, *SingletonClassName, NULL, LOAD_None, NULL); \
if (SingletonClass == NULL) \
{ \
/* if something failed, then try loading the fallback class */ \
/* load the class name from the .ini */ \
GConfig->GetString(TEXT("PlatformInterface"), TEXT(#ClassDesc) TEXT("FallbackClassName"), SingletonClassName, GEngineIni); \
\
/* load the class (most likely intrinsic) */ \
SingletonClass = LoadClass<Class>(NULL, *SingletonClassName, NULL, LOAD_None, NULL); \
if (SingletonClass == NULL) \
{ \
/* if something failed with the fallback, then use the default */ \
SingletonClass = Class::StaticClass(); \
} \
} \
\
/* make the singleton object that never goes away */ \
Singleton = ConstructObject<Class>(SingletonClass); \
check(Singleton); \
Singleton->AddToRoot(); \
\
/* never Garbage Collect this singleton */ \
Singleton->AddToRoot(); \
\
/* initialize it */ \
Singleton->eventInit(); \
} \
\
/* there will be a Singleton by this point (or an error above) */ \
return Singleton; \
} \
\
/** This is called on the default object, call the class static function */ \
Class* UPlatformInterfaceBase::Get##ClassDesc() \
{ \
return Get##ClassDesc##Singleton(); \
}
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UCloudStorageBase, CloudStorageInterface)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UCloudStorageBase, LocalStorageInterface)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UFacebookIntegration, FacebookIntegration)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UInGameAdManager, InGameAdManager)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UMicroTransactionBase, MicroTransactionInterface)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UAnalyticEventsBase,AnalyticEventsInterface)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UTwitterIntegrationBase,TwitterIntegration)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UAppNotificationsBase,AppNotificationsInterface)
IMPLEMENT_PLATFORM_INTERFACE_SINGLETON(UInAppMessageBase,InAppMessageInterface)
/*******************************************
* Cloud Storage *
*******************************************/
/**
* Perform any initialization
*/
void UCloudStorageBase::Init()
{
// Make a note on the local system that we need to upgrade these local files if we ever move to the cloud
// @todo ib2merge: Are these useful to everyone? Or just for IB? And is it needed for IB3?
FString NeedsUpgradeKeyName = TEXT("UpgradeKey");
FPlatformInterfaceData NeedsUpgradeValue;
NeedsUpgradeValue.IntValue = 1;
NeedsUpgradeValue.Type = PIDT_Int;
WriteKeyValue(NeedsUpgradeKeyName, NeedsUpgradeValue);
}
/**
* @return True if we are actually using local cloud storage emulation
*/
UBOOL UCloudStorageBase::IsUsingLocalStorage()
{
return TRUE;
}
/**
* Initiate reading a key/value pair from cloud storage. A CSD_KeyValueReadComplete
* delegate will be called when it completes (if this function returns true)
*
* @param KeyName String name of the key to retrieve
* @param Type Type of data to retrieve
* @param SerializedObj If you are reading an object (PIDT_Object), it will de-serialize the binary blob into this object
*
* @return True if successful
*/
UBOOL UCloudStorageBase::ReadKeyValue(const FString& KeyName, BYTE Type, struct FPlatformInterfaceDelegateResult& Value)
{
appMemzero(&Value, sizeof(Value));
Value.bSuccessful = TRUE;
Value.Data.Type = Type;
Value.Data.DataName = FName(*KeyName);
UBOOL bFileOpsDisabled = GConfig->AreFileOperationsDisabled();
if (bFileOpsDisabled)
GConfig->EnableFileOperations();
static FString CloudStorageIni(appCloudDir() + TEXT("CloudStorage.ini"));
switch (Type)
{
case PIDT_Int:
GConfig->GetInt(TEXT("CloudStorageEmulation"), *KeyName, Value.Data.IntValue, *CloudStorageIni);
break;
case PIDT_Float:
GConfig->GetFloat(TEXT("CloudStorageEmulation"), *KeyName, Value.Data.FloatValue, *CloudStorageIni);
break;
case PIDT_String:
GConfig->GetString(TEXT("CloudStorageEmulation"), *KeyName, Value.Data.StringValue, *CloudStorageIni);
break;
case PIDT_Object:
{
debugf(TEXT("CONVERT SOME BYTE ARRAY TO UOBJECT HERE?"));
}
break;
}
if (bFileOpsDisabled)
GConfig->DisableFileOperations();
return TRUE;
}
/**
* Reads a key/value pair from the local backup of the cloud KVS storage
*
*
* @param KeyName String name of the key to retrieve
* @param Type Type of data to retrieve
* @param Result The resulting value of the key we just read (supports multiple types of value)
*
* @return True if successful
*/
UBOOL UCloudStorageBase::ReadKeyValueFromLocalStore(const FString& KeyName, BYTE Type, struct FPlatformInterfaceDelegateResult& Value)
{
//Base implementation for PC emulation always stores data on disk we should never need to do any resolution
return ReadKeyValue(KeyName, Type, Value);
}
/**
* Write a key/value pair to the cloud. A CSD_KeyValueWriteComplete
* delegate will be called when it completes (if this function returns true)
*
* @param KeyName String name of the key to write
* @param Value The type and value to write
*
* @return True if successful
*/
UBOOL UCloudStorageBase::WriteKeyValue(const FString& KeyName, const FPlatformInterfaceData& Value)
{
static FString CloudStorageIni(appCloudDir() + TEXT("CloudStorage.ini"));
UBOOL bAreFileOperationsDisabled = GConfig->AreFileOperationsDisabled();
GConfig->EnableFileOperations();
switch (Value.Type)
{
case PIDT_Int:
GConfig->SetInt(TEXT("CloudStorageEmulation"), *KeyName, Value.IntValue, *CloudStorageIni);
break;
case PIDT_Float:
GConfig->SetFloat(TEXT("CloudStorageEmulation"), *KeyName, Value.FloatValue, *CloudStorageIni);
break;
case PIDT_String:
GConfig->SetString(TEXT("CloudStorageEmulation"), *KeyName, *Value.StringValue, *CloudStorageIni);
break;
case PIDT_Object:
{
debugf(TEXT("CONVERT SOME BYTE ARRAY TO UOBJECT HERE"));
}
break;
default:
{
debugf(TEXT("WriteKeyValue failed: Value.Type not set to supported type!"));
}
break;
};
// write it out
GConfig->Flush(FALSE, *CloudStorageIni);
if (bAreFileOperationsDisabled)
{
GConfig->DisableFileOperations();
}
return TRUE;
}
/**
* Kick off an async query of documents that exist in the cloud. If any documents have
* already been retrieved, this will flush those documents, and refresh the set. A
* CSD_DocumentQueryComplete delegate will be called when it completes (if this
* function returns true). Then use GetNumCloudDocuments() and GetCloudDocumentName()
* to get the information about any existing documents.
*
* @return True if successful
*/
UBOOL UCloudStorageBase::QueryForCloudDocuments()
{
// look for the files
LocalCloudFiles.Empty();
appFindFilesInDirectory(LocalCloudFiles, *appCloudDir(), TRUE, TRUE);
if (!bSuppressDelegateCalls)
{
// and we're done, call the delegates
FPlatformInterfaceDelegateResult Result(EC_EventParm);
Result.bSuccessful = TRUE;
CallDelegates(CSD_DocumentQueryComplete, Result);
}
return TRUE;
}
/**
* @return the number of documents that are known to exist in the cloud
*/
INT UCloudStorageBase::GetNumCloudDocuments(UBOOL bIsForConflict)
{
if (bIsForConflict)
{
return 0;
}
return LocalCloudFiles.Num();
}
/**
* @return the name of the given cloud by index (up to GetNumCloudDocuments() - 1)
*/
FString UCloudStorageBase::GetCloudDocumentName(INT Index)
{
// verify the input
if (Index < 0 || Index >= LocalCloudFiles.Num())
{
return FString(TEXT(""));
}
// pull apart the URL to get the filename
return FFilename(LocalCloudFiles(Index)).GetCleanFilename();
}
/**
* Create a new document in the cloud (uninitialized, unsaved, use the Write/Save functions)
*
* @param Filename Filename for the cloud document (with any extension you want)
*
* @return Index of the new document, or -1 on failure
*/
INT UCloudStorageBase::CreateCloudDocument(const FString& Filename)
{
FString FinalFilename = appCloudDir() + Filename;
return LocalCloudFiles.AddItem(FinalFilename);
}
/**
* Removes all of the files in the LocalCloudFiles array.
*/
void UCloudStorageBase::DeleteAllCloudDocuments()
{
INT Index = 0;
for (Index = 0; Index < LocalCloudFiles.Num(); ++Index)
{
GFileManager->Delete(*LocalCloudFiles(Index));
}
LocalCloudFiles.Empty();
}
/**
* Reads a document into memory (or whatever is needed so that the ParseDocumentAs* functions
* operate synchronously without stalling the game). A CSD_DocumentReadComplete delegate
* will be called (if this function returns true).
*
* @param Index index of the document to read
*
* @param True if successful
*/
UBOOL UCloudStorageBase::ReadCloudDocument(INT Index, UBOOL bIsForConflict)
{
// verify the input
if (bIsForConflict || Index < 0 || Index >= LocalCloudFiles.Num())
{
return FALSE;
}
// just call the delegate, we'll read in in the Parse function
if (GFileManager->FileSize(*LocalCloudFiles(Index)) != -1)
{
if (!bSuppressDelegateCalls)
{
FPlatformInterfaceDelegateResult Result(EC_EventParm);
Result.bSuccessful = TRUE;
// which document is this?
Result.Data.Type = PIDT_Int;
Result.Data.IntValue = Index;
CallDelegates(CSD_DocumentReadComplete, Result);
}
return TRUE;
}
return FALSE;
}
/**
* Once a document has been read in, use this to return a string representing the
* entire document. This should only be used if SaveDocumentWithString was used to
* generate the document.
*
* @param Index index of the document to read
*
* @param The document as a string. It will be empty if anything went wrong.
*/
FString UCloudStorageBase::ParseDocumentAsString(INT Index, UBOOL bIsForConflict)
{
// verify the input
if (bIsForConflict || Index < 0 || Index >= LocalCloudFiles.Num())
{
return FString(TEXT(""));
}
FString Result;
appLoadFileToString(Result, *LocalCloudFiles(Index));
return Result;
}
/**
* Once a document has been read in, use this to return a string representing the
* entire document. This should only be used if SaveDocumentWithString was used to
* generate the document.
*
* @param Index index of the document to read
* @param ByteData The array of bytes to be filled out
*
* @return The bytes from the file. It will be empty if anything went wrong
*/
void UCloudStorageBase::ParseDocumentAsBytes(INT Index, TArray<BYTE>& ByteData, UBOOL bIsForConflict)
{
// make sure a clean slate
ByteData.Empty();
// verify the input
if (bIsForConflict || Index < 0 || Index >= LocalCloudFiles.Num())
{
return;
}
LoadFileAndDecrypt(ByteData, *LocalCloudFiles(Index));
}
/**
* Once a document has been read in, use this to return a string representing the
* entire document. This should only be used if SaveDocumentWithString was used to
* generate the document.
*
* @param Index index of the document to read
* @param ExpectedVersion Version number expected to be in the save data. If this doesn't match what's there, this function will return NULL
* @param ObjectClass The class of the object to create
*
* @return The object deserialized from the document. It will be none if anything went wrongs
*/
UObject* UCloudStorageBase::ParseDocumentAsObject(INT Index, UClass* ObjectClass, INT ExpectedVersion, UBOOL bIsForConflict)
{
TArray<BYTE> ObjectBytes;
// read in a BYTE array
ParseDocumentAsBytes(Index, ObjectBytes, bIsForConflict);
// make sure we got some bytes
if (ObjectBytes.Num() == 0)
{
return NULL;
}
FMemoryReader MemoryReader(ObjectBytes, TRUE);
// load the version the object was saved with
INT SavedVersion;
MemoryReader << SavedVersion;
// make sure it matches
if (SavedVersion != ExpectedVersion)
{
// note that it failed to read
debugf(TEXT("Load failed: Cloud document was saved with an incompatibly version (%d, expected %d)."), SavedVersion, ExpectedVersion);
return NULL;
}
// use a wrapper archive that converts FNames and UObject*'s to strings that can be read back in
FObjectAndNameAsStringProxyArchive Ar(MemoryReader);
// NOTE: The following should be in shared functionality in UCloudStorageBase
// create the object
UObject* Obj = StaticConstructObject(ObjectClass);
// serialize the object
Obj->Serialize(Ar);
// return the deserialized object
return Obj;
}
/**
* Writes a document that has been already "saved" using the SaveDocumentWith* functions.
* A CSD_DocumentWriteComplete delegate will be called (if this function returns true).
*
* @param Index index of the document to read
*
* @param True if successful
*/
UBOOL UCloudStorageBase::WriteCloudDocument(INT Index)
{
// verify the input
if (Index < 0 || Index >= LocalCloudFiles.Num())
{
return FALSE;
}
if (!bSuppressDelegateCalls)
{
// was already written out in the SaveDocument, so just call the delegate
FPlatformInterfaceDelegateResult Result(EC_EventParm);
Result.bSuccessful = TRUE;
// which document is this?
Result.Data.Type = PIDT_Int;
Result.Data.IntValue = Index;
CallDelegates(CSD_DocumentWriteComplete, Result);
}
return TRUE;
}
/**
* Checks whether there are any pending writes.
* Always false in base system that uses synchronous writes.
*/
UBOOL UCloudStorageBase::IsStillWritingFiles()
{
return FALSE;
}
/**
* Waits until there are no more pending writes, or returns false if the time runs out.
*/
UBOOL UCloudStorageBase::WaitForWritesToFinish(FLOAT MaxTimeSeconds)
{
DOUBLE KillTime = appSeconds() + (DOUBLE)MaxTimeSeconds;
while(IsStillWritingFiles())
{
// just waiting...
appSleep(0.1f);
// Kill this process if it is taking too long
if( MaxTimeSeconds >= 0 && appSeconds() > KillTime )
{
return FALSE;
}
}
return TRUE;
}
/**
* Prepare a document for writing to the cloud with a string as input data. This is
* synchronous
*
* @param Index index of the document to save
* @param StringData The data to put into the document
*
* @param True if successful
*/
UBOOL UCloudStorageBase::SaveDocumentWithString(INT Index, const FString& StringData)
{
// verify the input
if (Index < 0 || Index >= LocalCloudFiles.Num())
{
return FALSE;
}
return appSaveStringToFile(StringData, *LocalCloudFiles(Index));
}
/**
* Prepare a document for writing to the cloud with an array of bytes as input data. This is
* synchronous
*
* @param Index index of the document to save
* @param ByteData The array of generic bytes to put into the document
*
* @param True if successful
*/
UBOOL UCloudStorageBase::SaveDocumentWithBytes(INT Index, const TArray<BYTE>& ByteData)
{
// verify the input
if (Index < 0 || Index >= LocalCloudFiles.Num())
{
return FALSE;
}
return EncryptAndSaveFile(ByteData, *LocalCloudFiles(Index));
}
/**
* Prepare a document for writing to the cloud with an object as input data. This is
* synchronous
*
* @param Index index of the document to save
* @param ObjectData The object to serialize to bytes and put into the document
* @param SaveVersion The version of this object. Used so that when you load this object, if the version doesn't match it will skip loading the object
*
* @param True if successful
*/
UBOOL UCloudStorageBase::SaveDocumentWithObject(INT Index, UObject* ObjectData, INT SaveVersion)
{
// verify the input
if (GetCloudDocumentName(Index) == TEXT(""))
{
return FALSE;
}
TArray<BYTE> ObjectBytes;
FMemoryWriter MemoryWriter(ObjectBytes);
// save out a version
MemoryWriter << SaveVersion;
// use a wrapper archive that converts FNames and UObject*'s to strings that can be read back in
FObjectAndNameAsStringProxyArchive Ar(MemoryWriter);
// serialize the object
ObjectData->Serialize(Ar);
// now, push the byte array into the document
SaveDocumentWithBytes(Index, ObjectBytes);
return TRUE;
}
/**
* If there was a conflict notification, this will simply tell the cloud interface
* to choose the most recently modified version, and toss any others
*/
UBOOL UCloudStorageBase::ResolveConflictWithNewestDocument()
{
return FALSE;
}
/**
* If there was a conflict notification, this will tell the cloud interface
* to choose the version with a given Index to be the master version, and to
* toss any others
*
* @param Index Conflict version index
*/
UBOOL UCloudStorageBase::ResolveConflictWithVersionIndex(INT /*Index*/)
{
return FALSE;
}
/**
* Check if there are local files on disk. These would come from systems that allow both
* cloud files and local files (e.g. iOS users not signed into iCloud).
* HandleLocalDocument() will be called for each file this function finds, which you can
* override to handle each document.
*
* @return True if there were any documents processed
*/
UBOOL UCloudStorageBase::UpgradeLocalStorageToCloud(const TScriptInterface<class ICloudStorageUpgradeHelper>& UpgradeHelper, UBOOL bForceSearchAgain)
{
#if IPHONE
return FALSE;
#elif 1
// For testing on PC only
// Check if we have already upgraded the local system files
FPlatformInterfaceDelegateResult LocalValue;
FString NeedsUpgradeKeyName = TEXT("LOCAL::UpgradeKey");
if( !bForceSearchAgain && ReadKeyValue(NeedsUpgradeKeyName, PIDT_Int, LocalValue) )
{
if( LocalValue.Data.IntValue == 0 )
{
// We've already done this, no need to do it again
return FALSE;
}
}
// make sure we don't send any delegate calls while performing some cloud upgrading
bSuppressDelegateCalls = TRUE;
// look for the files
TArray<FString> LocalTestFiles;
appFindFilesInDirectory(LocalTestFiles, *(appCloudDir() + FString(TEXT("..\\CloudLOCAL"))), TRUE, TRUE);
// if we have any legacy saves, we need to upgrade them
INT NumLegacySaves = LocalTestFiles.Num();
if (NumLegacySaves > 0)
{
// @todo: Show an alert message about what we are doing!
debugf(TEXT("Converting %d legacy saves to iCloud saves!"), NumLegacySaves);
for (INT SaveIndex = 0; SaveIndex < NumLegacySaves; SaveIndex++)
{
FString DocName = LocalTestFiles(SaveIndex);
INT bShouldCreateCloudDoc = TRUE;
INT bShouldDeleteLocalFile = TRUE;
UpgradeHelper->eventHandleLocalDocument(DocName, bShouldCreateCloudDoc, bShouldDeleteLocalFile);
if (bShouldCreateCloudDoc == TRUE)
{
// get the bytes
TArray<BYTE> SaveData;
LoadFileAndDecrypt(SaveData, *LocalTestFiles(SaveIndex));
// create a cloud document
INT CloudIndex = CreateCloudDocument(DocName);
SaveDocumentWithBytes(CloudIndex, SaveData);
WriteCloudDocument(CloudIndex);
}
if (bShouldDeleteLocalFile == TRUE)
{
// TODO delete the local file
//Super::DeleteCloudDocument(SaveIndex);
}
}
}
// move over existing key/value pairs as well (read from an ini file)
TArray<FString> CloudKeys;
GConfig->GetArray( TEXT("CloudStorage.CloudUpgradeKeys"), TEXT("CloudKey"), CloudKeys, GEngineIni );
UpgradeHelper->eventGetCloudUpgradeKeys(CloudKeys);
// iterate over all the keys specified in the ini file, and write them out to the cloud
FPlatformInterfaceData CloudValue;
for(INT i=0; i<CloudKeys.Num(); i++)
{
FString& Entry = CloudKeys(i);
// Trim whitespace at the beginning.
Entry = Entry.Trim();
// Remove brackets.
Entry = Entry.Replace( TEXT("("), TEXT("") );
Entry = Entry.Replace( TEXT(")"), TEXT("") );
// Parse the key name
FString CloudKeyName;
Parse( *Entry, TEXT("KeyName="), CloudKeyName );
FString LocalKeyName = FString::Printf(TEXT("LOCAL::%s"), *CloudKeyName);
appMemZero(CloudValue);
CloudValue.DataName = NAME_None;
// Parse the key type
CloudValue.Type = PIDT_String;
FString CloudKeyType;
if( Parse( *Entry, TEXT("KeyType="), CloudKeyType ) )
{
if( CloudKeyType == TEXT("PIDT_Int") )
CloudValue.Type = PIDT_Int;
else if( CloudKeyType == TEXT("PIDT_Float") )
CloudValue.Type = PIDT_Float;
else if( CloudKeyType == TEXT("PIDT_Object") )
CloudValue.Type = PIDT_Object;
else
CloudValue.Type = PIDT_String;
}
// read from local storage
if( ReadKeyValue(LocalKeyName, CloudValue.Type, LocalValue) )
{
// fill in the correct value based on type
switch( CloudValue.Type )
{
case PIDT_Int:
CloudValue.IntValue = LocalValue.Data.IntValue;
break;
case PIDT_Float:
CloudValue.FloatValue = LocalValue.Data.FloatValue;
break;
case PIDT_String:
CloudValue.StringValue = LocalValue.Data.StringValue;
break;
case PIDT_Object:
CloudValue.ObjectValue = LocalValue.Data.ObjectValue;
break;
}
INT bShouldMoveToCloud = TRUE;
INT bShouldDeleteLocalValue = FALSE;
UpgradeHelper->eventHandleLocalKeyValue(CloudKeyName, CloudValue, bShouldMoveToCloud, bShouldDeleteLocalValue);
// write to the cloud
if( bShouldMoveToCloud == TRUE )
{
WriteKeyValue(CloudKeyName, CloudValue);
}
// Destroy local value
if( bShouldDeleteLocalValue == TRUE )
{
// TODO - actually remove the key/value
CloudValue.IntValue = 0;
CloudValue.FloatValue = 0;
CloudValue.StringValue = TEXT("");
CloudValue.ObjectValue = NULL;
WriteKeyValue(LocalKeyName, CloudValue);
}
}
}
// Make a note on the local system that we don't need to do this again
FPlatformInterfaceData NeedsUpgradeValue;
NeedsUpgradeValue.IntValue = 0;
NeedsUpgradeValue.Type = PIDT_Int;
WriteKeyValue(NeedsUpgradeKeyName, NeedsUpgradeValue);
// and back to normal
bSuppressDelegateCalls = FALSE;
return NumLegacySaves > 0;
#else
return FALSE;
#endif
}
/*******************************************
* Microtransactions *
*******************************************/
/**
* Perform any initialization
*/
void UMicroTransactionBase::Init()
{
}
/**
* Query system for what purchases are available. Will fire a MTD_PurchaseQueryComplete
* if this function returns true.
*
* @return True if the query started successfully (delegate will receive final result)
*/
UBOOL UMicroTransactionBase::QueryForAvailablePurchases()
{
return FALSE;
}
/**
* @return True if the user is allowed to make purchases - should give a nice message if not
*/
UBOOL UMicroTransactionBase::IsAllowedToMakePurchases()
{
return FALSE;
}
/**
* Triggers a product purchase. Will fire a MTF_PurchaseComplete if this function
* returns true.
*
* @param Index which product to purchase
*
* @param True if the purchase was kicked off (delegate will receive final result)
*/
UBOOL UMicroTransactionBase::BeginPurchase(INT Index)
{
return FALSE;
}
/**
* Returns the product's index from an ID
*
* @param Identifier the product Identifier
*
* @return The index of the product in the AvailableProducts array
*/
INT UMicroTransactionBase::GetProductIndex(const FString& Identifier)
{
for( INT i=0; i<AvailableProducts.Num(); i++ )
{
if( AvailableProducts(i).Identifier == Identifier )
{
return i;