IEnumerator FileFetchAllVersions() { JCloudDocumentOperation operation = JCloudDocument.FileFetchAllVersions("testfile.txt"); while (!operation.finished) { yield return(null); } if (operation.success && operation.result != null) { JCloudDocumentVersions versions = (JCloudDocumentVersions)operation.result; documentResultString = "cloud document test file versions :"; int offset = 1; foreach (JCloudDocumentVersionMetadata metadata in versions.versionsMetadata) { documentResultString += " " + offset + ". " + metadata.modificationDate + (metadata.isCurrent ? " (current)" : ""); offset++; } documentResultString += " (hash : " + versions.versionsHash + ")" + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document test file versions : failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator DirectoryCreate() { JCloudDocumentOperation operation = JCloudDocument.DirectoryCreate("Test"); while (!operation.finished) { yield return(null); } documentResultString = "cloud document did create test directory with " + ((bool)operation.result ? "success" : "failure") + (operation.error != null ? (" ; error : " + operation.error) : ""); }
IEnumerator DirectoryExists() { JCloudDocumentOperation operation = JCloudDocument.DirectoryExists("Test"); while (!operation.finished) { yield return(null); } documentResultString = "cloud document test directory exists : " + ((bool)operation.result ? "exists" : "does not exist") + (operation.error != null ? (" ; error : " + operation.error) : ""); }
IEnumerator DirectoryModificationDate() { JCloudDocumentOperation operation = JCloudDocument.DirectoryModificationDate("Test"); while (!operation.finished) { yield return(null); } documentResultString = "cloud document test directory modification date : " + (operation.success ? ((System.DateTime)operation.result).ToString("MM-dd-yyyy HH:mm:ss") : "failure") + (operation.error != null ? (" ; error : " + operation.error) : ""); }
IEnumerator FileDelete() { JCloudDocumentOperation operation = JCloudDocument.FileDelete("testfile.txt"); while (!operation.finished) { yield return(null); } if (operation.success && (bool)operation.result) { documentResultString = "cloud document did delete" + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document did not delete (may not exist?)" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator DirectoryDelete() { JCloudDocumentOperation operation = JCloudDocument.DirectoryDelete("Test"); while (!operation.finished) { yield return(null); } if (operation.success) { documentResultString = "cloud document test directory deleted : " + ((bool)operation.result ? "success" : "failure") + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document test directory deleted : failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator DirectoryCopy() { JCloudDocumentOperation operation = JCloudDocument.DirectoryCopy("Test", "Test Copy", true); while (!operation.finished) { documentResultString = "cloud document test directory copy progress : " + operation.progress; yield return(null); } if (operation.success) { documentResultString = "cloud document test directory copy : " + ((bool)operation.result ? "success" : "failure") + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document test directory copy : failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator FileWriteAllBytesConflict() { System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); JCloudDocumentOperation operation = JCloudDocument.FileWriteAllBytes("testfile.txt", encoder.GetBytes("this is a conflict test content")); while (!operation.finished) { yield return(null); } if (operation.success) { documentResultString = "cloud document did write bytes with : " + ((bool)operation.result ? "success" : "failure") + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document write all bytes failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator FileHasConflictVersions() { JCloudDocumentOperation operation = JCloudDocument.FileHasConflictVersions("testfile.txt"); while (!operation.finished) { yield return(null); } if (operation.success) { documentResultString = "cloud document test file has conflict versions : " + ((bool)operation.result ? "yes" : "no") + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document test file has conflict versions : failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator FileReadAllBytes() { JCloudDocumentOperation operation = JCloudDocument.FileReadAllBytes("testfile.txt"); while (!operation.finished) { documentResultString = "cloud document read bytes progress : " + operation.progress; yield return(null); } System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); if (operation.success) { documentResultString = "cloud document did read bytes ; read this : " + ((operation.result == null) ? "(null)" : encoder.GetString(operation.result as byte[])) + (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud document read bytes : failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
IEnumerator LoadGame() { JCloudDocumentOperation operation = JCloudDocument.FileReadAllBytes("Savegames/My saved game.sav"); while (!operation.finished) { yield return(null); } // Look for error -- if any, handle & stop coroutine here if (operation.error.HasValue) { HandleDocumentError(operation.error.Value); yield break; } // Success error = null; byte[] gameBytes = operation.result as byte[]; // No bytes, no savegame if (gameBytes != null) { System.IO.MemoryStream dataStream = new System.IO.MemoryStream(gameBytes); System.IO.BinaryReader reader = new System.IO.BinaryReader(dataStream); // Read player state DeserializeTransformFromReader(player.transform, reader); DeserializeRigidbodyFromReader(player.rigidbody, reader); // Read player camera state DeserializeTransformFromReader(playerCamera.transform, reader); // Read red, green & blue cubes state DeserializeTransformFromReader(redCube.transform, reader); DeserializeRigidbodyFromReader(redCube.rigidbody, reader); DeserializeTransformFromReader(greenCube.transform, reader); DeserializeRigidbodyFromReader(greenCube.rigidbody, reader); DeserializeTransformFromReader(blueCube.transform, reader); DeserializeRigidbodyFromReader(blueCube.rigidbody, reader); } }
IEnumerator DirectoryGetDirectories() { JCloudDocumentOperation operation = JCloudDocument.DirectoryGetDirectories(""); while (!operation.finished) { yield return(null); } if (operation.success) { documentResultString = "cloud directory directories list : " + (operation.result as string[]).Length; for (int i = 0; i < (operation.result as string[]).Length; i++) { documentResultString += " ; " + (operation.result as string[])[i]; } documentResultString += (operation.error != null ? (" ; error : " + operation.error) : ""); } else { documentResultString = "cloud directory directories list : failure" + (operation.error != null ? (" ; error : " + operation.error) : ""); } }
// Pick a specific file version, make it current and remove all other versions public static JCloudDocumentOperation FilePickVersion(string path, byte[] uniqueIdentifier, string versionsHash) { // Prepare a new operation, start doing stuff and return operation so user can yield JCloudDocumentOperation operation = new JCloudDocumentOperation(); JCloudManager.GetSharedManager().StartCoroutine(FilePickVersionOperation(path, uniqueIdentifier, versionsHash, operation)); return operation; }
// Reads bytes from file specific version at path protected static IEnumerator FileReadVersionBytesOperation(string path, byte[] uniqueIdentifier, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "") || uniqueIdentifier == null || uniqueIdentifier.Length == 0) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // We can now open the cloud file if (JCloudExtern.OpenCloudItemWithVersionIdentifier(documentUID, uniqueIdentifier, uniqueIdentifier.Length)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) { JCloudManager.GetDocumentProgress(documentUID, out operation.progress); yield return null; } // Attempt to read bytes if file was opened if (status.Value.success) { System.IntPtr bytesPtr; int length = JCloudExtern.ReadCloudItemContents(documentUID, out bytesPtr); if (length != -1) { // Success, read bytes from pointer operation.success = true; operation.result = new byte[length]; Marshal.Copy(bytesPtr, operation.result as byte[], 0, length); JCloudExtern.FreeMemory(bytesPtr); } else operation.error = JCloudDocumentError.NativeError; } operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Wether successfull or not, we don't need document anymore DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO operation.error = JCloudDocumentError.CloudUnavailable; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Move a directory public static JCloudDocumentOperation DirectoryMove(string sourcePath, string destinationPath, bool overwrite) { // Prepare a new operation, start doing stuff and return operation so user can yield JCloudDocumentOperation operation = new JCloudDocumentOperation(); JCloudManager.GetSharedManager().StartCoroutine(DirectoryMoveOperation(sourcePath, destinationPath, overwrite, operation)); return operation; }
// Get a directory modification date at path public static JCloudDocumentOperation DirectoryModificationDate(string path) { // Prepare a new operation, start doing stuff and return operation so user can yield JCloudDocumentOperation operation = new JCloudDocumentOperation(); JCloudManager.GetSharedManager().StartCoroutine(DirectoryModificationDateOperation(path, operation)); return operation; }
// Deletes file at path protected static IEnumerator FileDeleteOperation(string path, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // We can now attempt delete the cloud file regardless of its current state byte deleted = 0; if (JCloudExtern.DeleteCloudItem(documentUID, ref deleted)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; operation.success = status.Value.success; operation.result = (deleted != 0); operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Dismiss document handling DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { if (System.IO.File.Exists(JCloudManager.JCloudDocumentFallbackPath + path)) System.IO.File.Delete(JCloudManager.JCloudDocumentFallbackPath + path); else { operation.error = JCloudDocumentError.DocumentNotFound; operation.finished = true; yield break; } } catch (System.Exception e) { if (e is ArgumentException || e is ArgumentNullException || e is NotSupportedException || e is PathTooLongException) operation.error = JCloudDocumentError.InvalidArguments; else if (e is DirectoryNotFoundException || e is FileNotFoundException) operation.error = JCloudDocumentError.DocumentNotFound; else if (e is UnauthorizedAccessException || e is IOException) operation.error = JCloudDocumentError.NativeError; else operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } operation.success = true; operation.result = true; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Determines if a file exists at path protected static IEnumerator FileExistsOperation(string path, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // Check for existence byte exists = 0; if (JCloudExtern.GetCloudItemExistence(documentUID, ref exists)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; operation.success = status.Value.success; operation.result = (exists != 0); operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Dismiss document handling DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { operation.result = System.IO.File.Exists(JCloudManager.JCloudDocumentFallbackPath + path); } catch { // File.Exists doesn't raise any exception anyway but we have to give an error just in case operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } // We have a result, no need to let user yield any longer operation.success = true; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Reads bytes from file at path protected static IEnumerator FileReadAllBytesOperation(string path, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // We can now open the cloud file if (JCloudExtern.OpenCloudItem(documentUID)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) { JCloudManager.GetDocumentProgress(documentUID, out operation.progress); yield return null; } // Attempt to read bytes if file was opened if (status.Value.success) { System.IntPtr bytesPtr; int length = JCloudExtern.ReadCloudItemContents(documentUID, out bytesPtr); if (length != -1) { // Success, read bytes from pointer operation.success = true; operation.result = new byte[length]; Marshal.Copy(bytesPtr, operation.result as byte[], 0, length); JCloudExtern.FreeMemory(bytesPtr); } else operation.error = JCloudDocumentError.NativeError; } operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Wether successfull or not, we don't need document anymore DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { operation.result = System.IO.File.ReadAllBytes(JCloudManager.JCloudDocumentFallbackPath + path); } catch (System.Exception e) { if (e is ArgumentException || e is ArgumentNullException || e is NotSupportedException || e is PathTooLongException) operation.error = JCloudDocumentError.InvalidArguments; else if (e is DirectoryNotFoundException || e is FileNotFoundException) operation.error = JCloudDocumentError.DocumentNotFound; else if (e is UnauthorizedAccessException || e is IOException || e.GetType().FullName.Equals("System.Security.SecurityException")) operation.error = JCloudDocumentError.NativeError; else operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } operation.success = true; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Get a file modification date at path protected static IEnumerator FileModificationDateOperation(string path, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // Get modification date long modificationDate = 0; if (JCloudExtern.GetCloudItemModificationDate(documentUID, ref modificationDate)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; // Check if we did get a correct date if (status.Value.success) { operation.success = true; operation.result = new System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(modificationDate).ToLocalTime(); } operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Wether successfull or not, we don't need document anymore DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { if (System.IO.File.Exists(JCloudManager.JCloudDocumentFallbackPath + path)) operation.result = System.IO.File.GetLastWriteTime(JCloudManager.JCloudDocumentFallbackPath + path); else { operation.error = JCloudDocumentError.DocumentNotFound; operation.finished = true; yield break; } } catch (System.Exception e) { if (e is ArgumentException || e is ArgumentNullException || e is NotSupportedException || e is PathTooLongException) operation.error = JCloudDocumentError.InvalidArguments; else if (e is IOException) operation.error = JCloudDocumentError.DocumentNotFound; else if (e is UnauthorizedAccessException) operation.error = JCloudDocumentError.NativeError; else operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } operation.success = true; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Move a file protected static IEnumerator FileMoveOperation(string sourcePath, string destinationPath, bool overwrite, JCloudDocumentOperation operation) { // Failsafe if ((sourcePath == null) || (sourcePath == "") || (destinationPath == null) || (destinationPath == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(sourcePath, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // Attempt to move to destination if (JCloudExtern.MoveCloudItem(documentUID, destinationPath, overwrite)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) { JCloudManager.GetDocumentProgress(documentUID, out operation.progress); yield return null; } operation.success = status.Value.success; operation.result = operation.success; operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Wether successfull or not, we don't need document anymore DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { if (System.IO.File.Exists(JCloudManager.JCloudDocumentFallbackPath + destinationPath)) { if (overwrite) System.IO.File.Delete(JCloudManager.JCloudDocumentFallbackPath + destinationPath); else { operation.finished = true; yield break; } } System.IO.File.Move(JCloudManager.JCloudDocumentFallbackPath + sourcePath, JCloudManager.JCloudDocumentFallbackPath + destinationPath); } catch (System.Exception e) { if (e is ArgumentException || e is ArgumentNullException || e is NotSupportedException || e is PathTooLongException) operation.error = JCloudDocumentError.InvalidArguments; else if (e is FileNotFoundException) operation.error = JCloudDocumentError.DocumentNotFound; else if (e is UnauthorizedAccessException || e is DirectoryNotFoundException|| e is IOException) operation.error = JCloudDocumentError.NativeError; else operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } operation.success = true; operation.result = true; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Gets a list of directories at path. protected static IEnumerator DirectoryGetDirectoriesOperation(string path, JCloudDocumentOperation operation) { // Failsafe if (path == null) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, true); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // We can now open the cloud file if (JCloudExtern.OpenCloudItem(documentUID)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; if (status.Value.success) { // Attempt to read contents System.IntPtr bytesPtr; int length = JCloudExtern.ReadCloudItemContents(documentUID, out bytesPtr); if (length != -1) { // Success, read bytes from pointer operation.success = true; byte[] bytes = new byte[length]; Marshal.Copy(bytesPtr, bytes, 0, length); JCloudExtern.FreeMemory(bytesPtr); // Post-process read data operation.result = JCloudManager.PathListFromBytes(bytes, true); } } operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Wether successfull or not, we don't need document anymore DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { operation.result = System.IO.Directory.GetDirectories(JCloudManager.JCloudDocumentFallbackPath + path); } catch (System.Exception e) { if (e is ArgumentException || e is ArgumentNullException || e is NotSupportedException || e is PathTooLongException) operation.error = JCloudDocumentError.InvalidArguments; else if (e is DirectoryNotFoundException) operation.error = JCloudDocumentError.DocumentNotFound; else if (e is UnauthorizedAccessException || e is IOException) operation.error = JCloudDocumentError.NativeError; else operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } // Trim path from directories System.Collections.Generic.List<string> trimmedDirectories = new System.Collections.Generic.List<string>(); string[] directories = operation.result as string[]; for (int i = 0; i < directories.Length; i++) trimmedDirectories.Add(directories[i].Remove(0, (JCloudManager.JCloudDocumentFallbackPath + path).Length)); operation.success = true; operation.result = trimmedDirectories.ToArray(); operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Fetch conflict versions metadata + current live version metadata at path protected static IEnumerator FileFetchAllVersionsOperation(string path, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // Check for existence JCloudExtern.JCloudDocumentVersionsInternal versionsInternal = new JCloudExtern.JCloudDocumentVersionsInternal(); if (JCloudExtern.CloudItemFetchAllVersions(documentUID, ref versionsInternal)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; if (status.Value.success) { // Spawn data in public struct JCloudDocumentVersions versions = new JCloudDocumentVersions(); versions.versionsHash = Marshal.PtrToStringAnsi(versionsInternal.versionsHash); versions.versionsMetadata = new JCloudDocumentVersionMetadata[versionsInternal.versionsCount]; System.IntPtr[] uniqueIdentifiersPointers = new System.IntPtr[versionsInternal.versionsCount]; int[] uniqueIdentifiersLengthes = new int[versionsInternal.versionsCount]; long[] modificationDates = new long[versionsInternal.versionsCount]; byte[] isCurrent = new byte[versionsInternal.versionsCount]; Marshal.Copy(versionsInternal.versionsUniqueIdentifiers, uniqueIdentifiersPointers, 0, versionsInternal.versionsCount); Marshal.Copy(versionsInternal.versionsUniqueIdentifiersLengthes, uniqueIdentifiersLengthes, 0, versionsInternal.versionsCount); Marshal.Copy(versionsInternal.versionsModificationDates, modificationDates, 0, versionsInternal.versionsCount); Marshal.Copy(versionsInternal.versionsIsCurrent, isCurrent, 0, versionsInternal.versionsCount); for (int i = 0; i < versionsInternal.versionsCount; i++) { JCloudDocumentVersionMetadata metadata = new JCloudDocumentVersionMetadata(); metadata.uniqueIdentifier = new byte[uniqueIdentifiersLengthes[i]]; Marshal.Copy(uniqueIdentifiersPointers[i], metadata.uniqueIdentifier, 0, uniqueIdentifiersLengthes[i]); metadata.modificationDate = new System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(modificationDates[i]).ToLocalTime(); metadata.isCurrent = (isCurrent[i] != 0); versions.versionsMetadata[i] = metadata; } // Free memory JCloudExtern.FreeMemory(versionsInternal.versionsHash); for (int i = 0; i < versionsInternal.versionsCount; i++) { JCloudExtern.FreeMemory(uniqueIdentifiersPointers[i]); } JCloudExtern.FreeMemory(versionsInternal.versionsUniqueIdentifiers); JCloudExtern.FreeMemory(versionsInternal.versionsUniqueIdentifiersLengthes); JCloudExtern.FreeMemory(versionsInternal.versionsModificationDates); JCloudExtern.FreeMemory(versionsInternal.versionsIsCurrent); operation.success = true; operation.result = versions; } operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Dismiss document handling DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO operation.error = JCloudDocumentError.CloudUnavailable; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
// Determines if a file exists at path public static JCloudDocumentOperation FileExists(string path) { // Prepare a new operation, start doing stuff and return operation so user can yield JCloudDocumentOperation operation = new JCloudDocumentOperation(); JCloudManager.GetSharedManager().StartCoroutine(FileExistsOperation(path, operation)); return operation; }
// Pick a specific file version, make it current and remove all other versions protected static IEnumerator FilePickVersionOperation(string path, byte[] uniqueIdentifier, string versionsHash, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "") || uniqueIdentifier == null || uniqueIdentifier.Length == 0 || versionsHash == null || versionsHash.Length == 0) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, false); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // Check for existence if (JCloudExtern.PickCloudItemWithVersionIdentifier(documentUID, uniqueIdentifier, uniqueIdentifier.Length, versionsHash)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; operation.success = status.Value.success; operation.result = status.Value.success; operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Dismiss document handling DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO operation.error = JCloudDocumentError.CloudUnavailable; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }
private IEnumerator SaveInCloud(System.Action <UserCloudUploadedDelegateEventArgs> onResultSave = null) { Debug.Log("Saving in the Cloud"); UserCloudUploadedDelegateEventArgs args = new UserCloudUploadedDelegateEventArgs(); bool isPossibleSaveInCloud = true; byte[] bytes = null; string errorReadingLocalData = string.Empty; try { bytes = System.IO.File.ReadAllBytes(UserCloud.GetPath(FILE_NAME_LOCAL)); Debug.Log("[UserManagerCloud] Loaded local file user data: " + UserCloud.GetPath(FILE_NAME_LOCAL)); } catch (Exception ex) { Debug.LogWarning("[SaveInCloud] Error reading local user data file..."); isPossibleSaveInCloud = false; errorReadingLocalData = ex.Message; } if (isPossibleSaveInCloud == false) { Debug.LogWarning("Exception reading local user before save it: " + errorReadingLocalData); args.Error = errorReadingLocalData; args.Message = "There was an error saving to iCloud"; args.Result = false; args.SaveIn = ResultSave.NotSaved; RaiseUserHasBeenUploaded(args, onResultSave); yield break; } #if UNITY_IPHONE || UNITY_EDITOR JCloudDocumentOperation op = JCloudDocument.FileWriteAllBytes(FILE_NAME_CLOUD, bytes); while (op.finished == false) { Debug.Log("[SaveInCloud] Writting user data to cloud: " + op.progress); yield return(null); } if (op.error.HasValue) { string res = ""; switch (op.error.Value) { case JCloudDocumentError.PluginError: case JCloudDocumentError.NativeError: case JCloudDocumentError.InvalidArguments: // Look out for this one as it means you passed invalid path // Offer the user to retry res = "An error ocurred while saving text file. please retry."; Debug.LogWarning("[SaveInCloud] " + res); break; case JCloudDocumentError.DocumentNotFound: res = "There is no file present on this device. Create a new text file."; Debug.LogWarning("[SaveInCloud] " + res); break; case JCloudDocumentError.DownloadTimeout: // Offer the user to retry res = "Could not download user cloud file. Please retry."; Debug.LogWarning("[SaveInCloud] " + res); break; default: // We should never get there break; } Debug.LogWarning("[SaveInCloud] " + op.error.Value); args.Error = res; args.Message = "There was an error saving to iCloud"; args.Result = false; args.ErrorType = op.error.Value; args.SaveIn = ResultSave.NotSaved; RaiseUserHasBeenUploaded(args, onResultSave); } else { // OK Debug.Log("[SaveInCloud] Saved user file in cloud " + bytes.Length + " bytes"); args.Error = string.Empty; args.Message = "Saved in iCloud"; args.Result = true; args.SaveIn = ResultSave.Cloud; Debug.Log("[SaveInCloud] - SaveUser. Level: " + CurrentUser.LastFinishedLvl); RaiseUserHasBeenUploaded(args, onResultSave); } yield break; // byte[] bytes = System.IO.File.ReadAllBytes(UserCloud.GetPath(FILE_NAME_LOCAL)); // JCloudDocument.FileWriteAllBytes(FILE_NAME_CLOUD, bytes); // //// UserCloudUploadedDelegateEventArgs args = new UserCloudUploadedDelegateEventArgs(); //// //// if (onResultSave != null) //// onResultSave(ResultSave.Cloud); // // Debug.Log("Save File in Cloud " + bytes.Length); // yield break; #elif UNITY_ANDROID && !disableGoogleCloud if (CanUseGoogleCloud()) { while (!PlayGameServices.isSignedIn() && googleCloudTryingToLogin) { yield return(null); } if (PlayGameServices.isSignedIn() && !googleCloudTryingToLogin) { string strUserData = null; try { strUserData = System.Convert.ToBase64String(bytes); Debug.Log("[UserManagerCloud] Saving to Google Cloud services: " + bytes.Length + " bytes"); } catch (Exception) { Debug.LogWarning("[UserManagerCloud] Failed to encode user raw bytes to string. Size: " + bytes.Length + " bytes"); } if (strUserData != null) { PlayGameServices.setStateData(strUserData, 0); } } } yield break; #endif }
// Creates a directory at path protected static IEnumerator DirectoryCreateOperation(string path, JCloudDocumentOperation operation) { // Failsafe if ((path == null) || (path == "")) { operation.error = JCloudDocumentError.InvalidArguments; operation.finished = true; yield break; } #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) // Make sure our platfom is compatible if (JCloudManager.PlatformIsCloudCompatible() && (AcceptJailbrokenDevices || JCloudExtern.IsJailbroken() == false)) { // Prepare a new cloud document int documentUID = JCloudDocument.NewCloudDocument(path, true); // Any current operation on that document? Just wait yield until they end int lockId = JCloudManager.GetDocumentLock(documentUID); while (!JCloudManager.CheckDocumentLock(documentUID, lockId)) yield return null; // We can now create or open the cloud directory if (JCloudExtern.CreateCloudItem(documentUID)) { JCloudManager.DocumentStatus? status; while (!JCloudManager.GetDocumentStatus(documentUID, out status)) yield return null; operation.success = status.Value.success; operation.result = operation.success; operation.error = status.Value.error; } else operation.error = JCloudDocumentError.PluginError; // We have a result, no need to let user yield any longer operation.finished = true; // Wether successfull or not, we don't need document anymore DismissCloudDocument(documentUID); } else { #endif #if !UNITY_WEBPLAYER && !UNITY_METRO // C# fallback try { System.IO.Directory.CreateDirectory(JCloudManager.JCloudDocumentFallbackPath + path); } catch (System.Exception e) { if (e is ArgumentException || e is ArgumentNullException || e is NotSupportedException || e is PathTooLongException) operation.error = JCloudDocumentError.InvalidArguments; else if (e is UnauthorizedAccessException || e is IOException || e.GetType().FullName.Equals("System.Security.SecurityException") || e is DirectoryNotFoundException) operation.error = JCloudDocumentError.NativeError; else operation.error = JCloudDocumentError.PluginError; operation.finished = true; yield break; } operation.success = true; operation.result = true; operation.finished = true; #else operation.error = JCloudDocumentError.InvalidPlatform; operation.finished = true; #endif #if !UNITY_EDITOR && ((UNITY_IPHONE && JCLOUDPLUGIN_IOS) || (UNITY_STANDALONE_OSX && JCLOUDPLUGIN_OSX)) } #endif }