/* This method reads a specified file from your hard-drive and copies it to local storage. * Please note that you can use Galaxy SDK FileWrite method to write bytes from memory to a * file in local storage i.e. create new file instead of copying an existing one. */ public void CopyFileToLocalStorage(string absoluteInputPath) { string fileName = null; byte[] data = null; if (File.Exists(absoluteInputPath)) { fileName = Path.GetFileName(absoluteInputPath); data = File.ReadAllBytes(absoluteInputPath); } else { Debug.Log("File " + absoluteInputPath + " does not exist"); return; } try { Debug.Log("Writing file " + absoluteInputPath + " to local storage."); GalaxyInstance.Storage().FileWrite(fileName, data, (uint)data.Length); } catch (GalaxyInstance.Error e) { Debug.LogWarning("Could not write file " + absoluteInputPath + " to local storage for reason: " + e); } }
/* Uploads specified file from local storage to online storage. Uploaded file will be then * available in cloud, meaning it can be shared with other users using SharedFileID (obtained from * FileShareListener), and will be downloaded on all machines used by this user. */ public void ShareFileFromLocalStorage(string fileName) { try { Debug.Log("GalaxyInstance.Storage.FileShare method started"); GalaxyInstance.Storage().FileShare(fileName); } catch (GalaxyInstance.Error e) { Debug.LogWarning("Error " + e + " occured during execution of GalaxyInstance.Storage.FileShare method"); } }
/* Download shared file using its SharedFileID */ public void DownloadSharedFileBySharedFileID(ulong sharedFileID) { if (sharedFileID == 0) { return; } try { GalaxyInstance.Storage().DownloadSharedFile(sharedFileID); } catch (GalaxyInstance.Error e) { Debug.LogWarning("Could not download shared file for reason " + e); } }
/* Removes specified file from local storage. The file needs to be in your local storage * when this method is used. If the file is removed when the game is running this information * will be shared with cloud storage when the game is closed. Files removed manually when * the game is not running will be downloaded again when the game is launched. */ public void RemoveFileFromLocalStorage(string fileName) { try { if (GalaxyInstance.Storage().FileExists(fileName)) { GalaxyInstance.Storage().FileDelete(fileName); } else { Debug.Log("File " + fileName + " could not be removed because it does not exist"); } } catch (GalaxyInstance.Error e) { Debug.LogWarning("Could not remove file " + fileName + " for reason " + e); } }
/* Share all files present in the local storage. Does the same thing as ShareFileFromLocalStorage, * but for all files present in local storage. */ public void ShareAllFilesFromLocalStorage() { uint fileCount = GalaxyInstance.Storage().GetFileCount(); string fileName = null; try { for (uint i = 0; i < fileCount; i++) { fileName = GalaxyInstance.Storage().GetFileNameByIndex(i); ShareFileFromLocalStorage(fileName); Debug.Log("File " + fileName + " share request sent"); } } catch (GalaxyInstance.Error e) { Debug.LogWarning("Sharing one of the files from local storage failed for reason: " + e); } }
public override void OnSharedFileDownloadSuccess(ulong sharedFileID, string fileName) { Debug.Log("Shared file with ID " + sharedFileID + " downloaded. Proceeding to write the file "); try { uint downloadedSharedFileSize = GalaxyInstance.Storage().GetSharedFileSize(sharedFileID); byte[] downloadedSharedFileBuffer = new byte [downloadedSharedFileSize]; string saveFileName = userID + "/" + fileName; GalaxyInstance.Storage().SharedFileRead(sharedFileID, downloadedSharedFileBuffer, downloadedSharedFileSize); GalaxyInstance.Storage().FileWrite(saveFileName, downloadedSharedFileBuffer, downloadedSharedFileSize); Debug.Log("Downloaded shared file written to: " + saveFileName); } catch (GalaxyInstance.Error e) { Debug.LogWarning(e); } finally { GalaxyInstance.Storage().SharedFileClose(sharedFileID); userID = "0"; } }
/* Lists all files present in the local storage. Files in Cloud Storage that are not present in * the local storage will NOT be listed. */ public string[] ListAllFilesFromOnlineStorage() { uint fileCount = 0; string[] nameList; try { fileCount = GalaxyInstance.Storage().GetFileCount(); nameList = new string[fileCount]; for (uint i = 0; i < fileCount; i++) { nameList[i] = GalaxyInstance.Storage().GetFileNameByIndex(i); } Debug.Log("List of files in storage received."); return(nameList); } catch (GalaxyInstance.Error e) { Debug.LogWarning("Getting file list from storage failed for reason: " + e); } return(null); }
void DownloadedSharedFileWrite(ulong sharedFileID, string fileName) { try { uint downloadedSharedFileSize = GalaxyInstance.Storage().GetSharedFileSize(sharedFileID); byte[] downloadedSharedFileBuffer = new byte [downloadedSharedFileSize]; if (userID != null) { GalaxyInstance.Storage().SharedFileRead(sharedFileID, downloadedSharedFileBuffer, downloadedSharedFileSize); string saveFileName = userID + "/" + fileName; GalaxyInstance.Storage().FileWrite(saveFileName, downloadedSharedFileBuffer, downloadedSharedFileSize); GalaxyInstance.Storage().SharedFileClose(sharedFileID); userID = null; } else { Debug.LogWarning("Failed to download file with ID " + sharedFileID + " because SharedFileDownloadListener.userID is null"); } } catch (GalaxyInstance.Error e) { Debug.LogWarning(e); } }