| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- using System.Collections.Generic;
- using TapSDK.Core;
- using System;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using TapSDK.CloudSave.Internal;
- using TapSDK.Core.Internal.Log;
- namespace TapSDK.CloudSave.Mobile
- {
- public class ErrorResponse
- {
- [JsonProperty("errorCode")] public int ErrorCode { get; set; }
- [JsonProperty("errorMessage")] public string ErrorMessage { get; set; }
- }
- public class TapCloudSaveBridge : ITapCloudSaveBridge
- {
- public static string TAP_CLOUDSAVE_SERVICE = "BridgeCloudSaveService";
- public static string TDS_CLOUDSAVE_SERVICE_CLZ = "com.taptap.sdk.cloudsave.unity.BridgeCloudSaveService";
- public static string TDS_CLOUDSAVE_SERVICE_IMPL = "com.taptap.sdk.cloudsave.unity.BridgeCloudSaveServiceImpl";
- public TapCloudSaveBridge()
- {
- EngineBridge.GetInstance().Register(TDS_CLOUDSAVE_SERVICE_CLZ, TDS_CLOUDSAVE_SERVICE_IMPL);
- }
- public void Init(TapTapSdkOptions options)
- {
- // 原生由原生内部实现
- }
- public void RegisterCloudSaveCallback(ITapCloudSaveCallback callback)
- {
- TapLog.Log("[TapCloudSaveBridge] RegisterCloudSaveCallback called (UNCHANGED callback pattern)");
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("registerCloudSaveCallback")
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(), (response) =>
- {
- if (callback == null) return;
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- callback.OnResult(-1);
- return;
- }
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var resultCode = JsonConvert.DeserializeObject<int>(result.content);
- if (resultCode != null)
- {
- callback.OnResult(resultCode);
- }
- else
- {
- callback.OnResult(-1);
- }
- }
- else
- {
- callback.OnResult(-1);
- }
- }
- catch (Exception e)
- {
- callback.OnResult(-1);
- }
- });
- }
- public Task<ArchiveData> CreateArchive(ArchiveMetadata metadata, string archiveFilePath, string archiveCoverPath)
- {
- TapLog.Log("[TapCloudSaveBridge] CreateArchive called with Task<ArchiveData> return type (NEW API)");
- var taskSource = new TaskCompletionSource<ArchiveData>();
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("createArchive")
- .Args("archiveMetadata", JsonConvert.SerializeObject(metadata))
- .Args("archiveFilePath", archiveFilePath)
- .Args("archiveCoverPath", archiveCoverPath)
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(),
- response =>
- {
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- taskSource.TrySetException(new TapException(-1, "Failed to create archive: code="+response.code + " content="+response.content));
- return;
- }
-
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var archive = JsonConvert.DeserializeObject<ArchiveData>(result.content);
- if (archive != null)
- {
- taskSource.TrySetResult(archive);
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "json convert failed: content="+result.content));
- }
- }
- else
- {
- try
- {
- var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(result.content);
- if (errorResponse != null)
- {
- taskSource.TrySetException(new TapException(errorResponse.ErrorCode, errorResponse.ErrorMessage));
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "Failed to create archive: content="+response.content));
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to create archive: content="+response.content));
- }
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to create archive: error=" + e.Message + ", content=" + response.content));
- }
- });
- return taskSource.Task;
- }
-
- public Task<ArchiveData> UpdateArchive(string archiveUuid, ArchiveMetadata metadata, string archiveFilePath, string archiveCoverPath)
- {
- TapLog.Log("[TapCloudSaveBridge] UpdateArchive called with Task<ArchiveData> return type (NEW API)");
- var taskSource = new TaskCompletionSource<ArchiveData>();
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("updateArchive")
- .Args("archiveUUIDForUpdate", archiveUuid)
- .Args("archiveMetadataForUpdate", JsonConvert.SerializeObject(metadata))
- .Args("archiveFilePathForUpdate", archiveFilePath)
- .Args("archiveCoverPathForUpdate", archiveCoverPath)
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(), (response) =>
- {
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- taskSource.TrySetException(new TapException(-1, "Failed to update archive: code="+response.code + " content="+response.content));
- return;
- }
-
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var archive = JsonConvert.DeserializeObject<ArchiveData>(result.content);
- if (archive != null)
- {
- taskSource.TrySetResult(archive);
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "json convert failed: content="+result.content));
- }
- }
- else
- {
- try
- {
- var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(result.content);
- if (errorResponse != null)
- {
- taskSource.TrySetException(new TapException(errorResponse.ErrorCode, errorResponse.ErrorMessage));
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "Failed to update archive: content="+response.content));
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to update archive: content="+response.content));
- }
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to update archive: error=" + e.Message + ", content=" + response.content));
- }
- });
- return taskSource.Task;
- }
-
- public Task<ArchiveData> DeleteArchive(string archiveUuid)
- {
- TapLog.Log("[TapCloudSaveBridge] DeleteArchive called with Task<ArchiveData> return type (NEW API)");
- var taskSource = new TaskCompletionSource<ArchiveData>();
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("deleteArchive")
- .Args("archiveUUID", archiveUuid)
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(), (response) =>
- {
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- taskSource.TrySetException(new TapException(-1, "Failed to delete archive: code="+response.code + " content="+response.content));
- return;
- }
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var archive = JsonConvert.DeserializeObject<ArchiveData>(result.content);
- if (archive != null)
- {
- taskSource.TrySetResult(archive);
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "json convert failed: content="+result.content));
- }
- }
- else
- {
- try
- {
- var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(result.content);
- if (errorResponse != null)
- {
- taskSource.TrySetException(new TapException(errorResponse.ErrorCode, errorResponse.ErrorMessage));
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "Failed to delete archive: content="+response.content));
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to delete archive: content="+response.content));
- }
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to delete archive: error=" + e.Message + ", content=" + response.content));
- }
- });
- return taskSource.Task;
- }
- public Task<List<ArchiveData>> GetArchiveList()
- {
- TapLog.Log("[TapCloudSaveBridge] GetArchiveList called with Task<List<ArchiveData>> return type (NEW API)");
- var taskSource = new TaskCompletionSource<List<ArchiveData>>();
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("getArchiveList")
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(), (response) =>
- {
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive list: code="+response.code + " content="+response.content));
- return;
- }
-
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var archiveList = JsonConvert.DeserializeObject<List<ArchiveData>>(result.content);
- if (archiveList != null)
- {
- taskSource.TrySetResult(archiveList);
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "json convert failed: content="+result.content));
- }
- }
- else
- {
- try
- {
- var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(result.content);
- if (errorResponse != null)
- {
- taskSource.TrySetException(new TapException(errorResponse.ErrorCode, errorResponse.ErrorMessage));
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive list: content="+response.content));
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive list: content="+response.content));
- }
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive list: error=" + e.Message + ", content=" + response.content));
- }
- });
- return taskSource.Task;
- }
-
- public Task<byte[]> GetArchiveData(string archiveUuid, string archiveFileId)
- {
- TapLog.Log("[TapCloudSaveBridge] GetArchiveData called with Task<byte[]> return type (NEW API)");
- var taskSource = new TaskCompletionSource<byte[]>();
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("getArchiveData")
- .Args("archiveUUID", archiveUuid)
- .Args("archiveFileID", archiveFileId)
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(), (response) =>
- {
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive data: code=" + response.code + " content="+response.content));
- return;
- }
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var archiveData = Convert.FromBase64String(result.content);
- if (archiveData != null)
- {
- taskSource.TrySetResult(archiveData);
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "json convert failed: content="+result.content));
- }
- }
- else
- {
- try
- {
- var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(result.content);
- if (errorResponse != null)
- {
- taskSource.TrySetException(new TapException(errorResponse.ErrorCode, errorResponse.ErrorMessage));
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive data: content="+response.content));
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive data: content="+response.content));
- }
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive data: error=" + e.Message + ", content=" + response.content));
- }
- });
- return taskSource.Task;
- }
-
- public Task<byte[]> GetArchiveCover(string archiveUuid, string archiveFileId)
- {
- TapLog.Log("[TapCloudSaveBridge] GetArchiveCover called with Task<byte[]> return type (NEW API)");
- var taskSource = new TaskCompletionSource<byte[]>();
- EngineBridge.GetInstance().CallHandler(new Command.Builder()
- .Service(TAP_CLOUDSAVE_SERVICE)
- .Method("getArchiveCover")
- .Args("archiveUUIDForCover", archiveUuid)
- .Args("archiveFileIDForCover", archiveFileId)
- .Callback(true)
- .OnceTime(true)
- .CommandBuilder(), (response) =>
- {
- try
- {
- if (response.code != Result.RESULT_SUCCESS || string.IsNullOrEmpty(response.content))
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive cover: code="+response.code + " content="+response.content));
- return;
- }
-
- var result = JsonConvert.DeserializeObject<TapEngineBridgeResult>(response.content);
- if (result != null && result.code == TapEngineBridgeResult.RESULT_SUCCESS)
- {
- var coverData = Convert.FromBase64String(result.content);
- if (coverData != null)
- {
- taskSource.TrySetResult(coverData);
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "json convert failed: content="+result.content));
- }
- }
- else
- {
- try
- {
- var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(result.content);
- if (errorResponse != null)
- {
- taskSource.TrySetException(new TapException(errorResponse.ErrorCode, errorResponse.ErrorMessage));
- }
- else
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive cover: content="+response.content));
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive cover: content="+response.content));
- }
- }
- }
- catch (Exception e)
- {
- taskSource.TrySetException(new TapException(-1, "Failed to get archive cover: error=" + e.Message + ", content=" + response.content));
- }
- });
- return taskSource.Task;
- }
- }
- }
|