#if !DISABLE_WEB using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.Networking; using System; using ES3Internal; public class ES3Cloud : ES3WebClass { /// Constructs an new ES3Cloud object with the given URL to an ES3.php file. /// The URL of the ES3.php file on your server you want to use. public ES3Cloud(string url, string apiKey) : base(url, apiKey) { } #region Downloaded Data Handling /// The encoding to use when encoding and decoding data as strings. public System.Text.Encoding encoding = System.Text.Encoding.UTF8; private byte[] _data = null; /// Any downloaded data, if applicable. This may also contain an error message, so you should check the 'ifError' variable before reading data. public byte[] data { get{ return _data; } } /// The downloaded data as text, decoded using the encoding specified by the 'encoding' variable. public string text { get { if(data == null) return null; return encoding.GetString(data); } } /// An array of filenames downloaded from the server. This must only be accessed after calling the 'DownloadFilenames' routine. public string[] filenames { get { if(data == null || data.Length == 0) return new string[0]; return text.Split(';'); } } /// A UTC DateTime object representing the date and time a file on the server was last updated. This should only be called after calling the 'DownloadTimestamp' routine. public DateTime timestamp { get { if(data == null || data.Length == 0) return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); double timestamp; if(!double.TryParse(text, out timestamp)) throw new FormatException("Could not convert downloaded data to a timestamp. Data downloaded was: " + text); return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp); } } #endregion #region Sync /// Synchronises the default file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. public IEnumerator Sync() { return Sync(new ES3Settings(), "", ""); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The relative or absolute path of the local file we want to synchronise. public IEnumerator Sync(string filePath) { return Sync(new ES3Settings(filePath), "", ""); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The relative or absolute path of the local file we want to synchronise. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator Sync(string filePath, string user) { return Sync(new ES3Settings(filePath), user, ""); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The relative or absolute path of the local file we want to synchronise. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator Sync(string filePath, string user, string password) { return Sync(new ES3Settings(filePath), user, password); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The relative or absolute path of the local file we want to synchronise. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator Sync(string filePath, ES3Settings settings) { return Sync(new ES3Settings(filePath, settings), "", ""); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The relative or absolute path of the file we want to use. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The settings we want to use to override the default settings. public IEnumerator Sync(string filePath, string user, ES3Settings settings) { return Sync(new ES3Settings(filePath, settings), user, ""); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The relative or absolute path of the local file we want to synchronise. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator Sync(string filePath, string user, string password, ES3Settings settings) { return Sync(new ES3Settings(filePath, settings), user, password); } /// Synchronises a local file with a file on the server. If the file on the server is newer than the local copy, the local file will be overwritten by the file on the server. Otherwise, the file on the server will be overwritten. /// The settings we want to use to override the default settings. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. private IEnumerator Sync(ES3Settings settings, string user, string password) { Reset(); yield return DownloadFile(settings, user, password, GetFileTimestamp(settings)); if(errorCode == 3) { // Clear the error. Reset(); // File does not exist on server, or is older than locally stored data, so upload the local file to the server if it exists. if(ES3.FileExists(settings)) yield return UploadFile(settings, user, password); } isDone = true; } #endregion #region UploadFile /// Uploads the default file to the server, overwriting any existing file. public IEnumerator UploadFile() { return UploadFile(new ES3Settings(), "", ""); } /// Uploads a local file to the server, overwriting any existing file. /// The relative or absolute path of the file we want to use. public IEnumerator UploadFile(string filePath) { return UploadFile(new ES3Settings(filePath), "", ""); } /// Uploads a local file to the server, overwriting any existing file. /// The relative or absolute path of the file we want to use. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator UploadFile(string filePath, string user) { return UploadFile(new ES3Settings(filePath), user, ""); } /// Uploads a local file to the server, overwriting any existing file. /// The relative or absolute path of the file we want to use. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator UploadFile(string filePath, string user, string password) { return UploadFile(new ES3Settings(filePath), user, password); } /// Uploads a local file to the server, overwriting any existing file. /// The relative or absolute path of the file we want to use. /// The settings we want to use to override the default settings. public IEnumerator UploadFile(string filePath, ES3Settings settings) { return UploadFile(new ES3Settings(filePath, settings), "", ""); } /// Uploads a local file to the server, overwriting any existing file. /// The relative or absolute path of the file we want to use. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The settings we want to use to override the default settings. public IEnumerator UploadFile(string filePath, string user, ES3Settings settings) { return UploadFile(new ES3Settings(filePath, settings), user, ""); } /// Uploads a local file to the server, overwriting any existing file. /// The relative or absolute path of the file we want to use. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator UploadFile(string filePath, string user, string password, ES3Settings settings) { return UploadFile(new ES3Settings(filePath, settings), user, password); } /// Uploads a local file to the server, overwriting any existing file. /// An ES3File containing the data we want to upload. public IEnumerator UploadFile(ES3File es3File) { return UploadFile(es3File.LoadRawBytes(), es3File.settings, "", "", DateTimeToUnixTimestamp(DateTime.Now)); } /// Uploads a local file to the server, overwriting any existing file. /// An ES3File containing the data we want to upload. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator UploadFile(ES3File es3File, string user) { return UploadFile(es3File.LoadRawBytes(), es3File.settings, user, "", DateTimeToUnixTimestamp(DateTime.Now)); } /// Uploads a local file to the server, overwriting any existing file. /// An ES3File containing the data we want to upload. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator UploadFile(ES3File es3File, string user, string password) { return UploadFile(es3File.LoadRawBytes(), es3File.settings, user, password, DateTimeToUnixTimestamp(DateTime.Now)); } /// Uploads a local file to the server, overwriting any existing file. /// An ES3File containing the data we want to upload. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator UploadFile(ES3Settings settings, string user, string password) { return UploadFile(ES3.LoadRawBytes(settings), settings, user, password); } private IEnumerator UploadFile(byte[] bytes, ES3Settings settings, string user, string password) { return UploadFile(bytes, settings, user, password, DateTimeToUnixTimestamp(ES3.GetTimestamp(settings))); } private IEnumerator UploadFile(byte[] bytes, ES3Settings settings, string user, string password, long fileTimestamp) { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("putFile", settings.path); form.AddField("timestamp", fileTimestamp.ToString()); form.AddField("user", GetUser(user, password)); form.AddBinaryData("data", bytes, "data.dat", "multipart/form-data"); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); HandleError(webRequest, true); } isDone = true; } #endregion #region DownloadFile /// Downloads the default file from the server and saves it locally, overwriting the existing local default file. An error is returned if the file does not exist. public IEnumerator DownloadFile() { return DownloadFile(new ES3Settings(), "", "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The relative or absolute path of the file we want to download. public IEnumerator DownloadFile(string filePath) { return DownloadFile(new ES3Settings(filePath), "", "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The relative or absolute path of the file we want to download. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator DownloadFile(string filePath, string user) { return DownloadFile(new ES3Settings(filePath), user, "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The relative or absolute path of the file we want to download. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator DownloadFile(string filePath, string user, string password) { return DownloadFile(new ES3Settings(filePath), user, password, 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The relative or absolute path of the file we want to download. /// The settings we want to use to override the default settings. public IEnumerator DownloadFile(string filePath, ES3Settings settings) { return DownloadFile(new ES3Settings(filePath, settings), "", "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The relative or absolute path of the file we want to download. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The settings we want to use to override the default settings. public IEnumerator DownloadFile(string filePath, string user, ES3Settings settings) { return DownloadFile(new ES3Settings(filePath, settings), user, "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The relative or absolute path of the file we want to download. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator DownloadFile(string filePath, string user, string password, ES3Settings settings) { return DownloadFile(new ES3Settings(filePath, settings), user, password, 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The ES3File we want to load our data into. The filename in the settings of the ES3File will be used when downloading. public IEnumerator DownloadFile(ES3File es3File) { return DownloadFile(es3File, "", "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The ES3File we want to load our data into. The filename in the settings of the ES3File will be used when downloading. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator DownloadFile(ES3File es3File, string user) { return DownloadFile(es3File, user, "", 0); } /// Downloads a file from the server and saves it locally, overwriting any existing local file. An error is returned if the file does not exist. /// The ES3File we want to load our data into. The filename in the settings of the ES3File will be used when downloading. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator DownloadFile(ES3File es3File, string user, string password) { return DownloadFile(es3File, user, password, 0); } private IEnumerator DownloadFile(ES3File es3File, string user, string password, long timestamp) { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("getFile", es3File.settings.path); form.AddField("user", GetUser(user, password)); if(timestamp > 0) form.AddField("timestamp", timestamp.ToString()); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); if(!HandleError(webRequest, false)) { if(webRequest.downloadedBytes > 0) { es3File.Clear(); es3File.SaveRaw(webRequest.downloadHandler.data); } else { error = string.Format("File {0} was not found on the server.", es3File.settings.path); errorCode = 3; } } } isDone = true; } private IEnumerator DownloadFile(ES3Settings settings, string user, string password, long timestamp) { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("getFile", settings.path); form.AddField("user", GetUser(user, password)); if(timestamp > 0) form.AddField("timestamp", timestamp.ToString()); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); if(!HandleError(webRequest, false)) { if(webRequest.downloadedBytes > 0) { ES3.SaveRaw(webRequest.downloadHandler.data, settings); } else { error = string.Format("File {0} was not found on the server.", settings.path); errorCode = 3; } } } isDone = true; } #endregion #region DeleteFile /// Deletes the default file from the server. An error is *not* returned if the file does not exist. public IEnumerator DeleteFile() { return DeleteFile(new ES3Settings(), "", ""); } /// Deletes a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. public IEnumerator DeleteFile(string filePath) { return DeleteFile(new ES3Settings(filePath), "", ""); } /// Deletes a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator DeleteFile(string filePath, string user) { return DeleteFile(new ES3Settings(filePath), user, ""); } /// Deletes a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator DeleteFile(string filePath, string user, string password) { return DeleteFile(new ES3Settings(filePath), user, password); } /// Deletes a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The settings we want to use to override the default settings. public IEnumerator DeleteFile(string filePath, ES3Settings settings) { return DeleteFile(new ES3Settings(filePath, settings), "", ""); } /// Deletes a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The settings we want to use to override the default settings. public IEnumerator DeleteFile(string filePath, string user, ES3Settings settings) { return DeleteFile(new ES3Settings(filePath, settings), user, ""); } /// Deletes a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator DeleteFile(string filePath, string user, string password, ES3Settings settings) { return DeleteFile(new ES3Settings(filePath, settings), user, password); } private IEnumerator DeleteFile(ES3Settings settings, string user, string password) { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("deleteFile", settings.path); form.AddField("user", GetUser(user, password)); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); HandleError(webRequest, true); } isDone = true; } #endregion #region RenameFile /// Renames a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. public IEnumerator RenameFile(string filePath, string newFilePath) { return RenameFile(new ES3Settings(filePath), new ES3Settings(newFilePath), "", ""); } /// Renames a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator RenameFile(string filePath, string newFilePath, string user) { return RenameFile(new ES3Settings(filePath), new ES3Settings(newFilePath), user, ""); } /// Renames a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator RenameFile(string filePath, string newFilePath, string user, string password) { return RenameFile(new ES3Settings(filePath), new ES3Settings(newFilePath), user, password); } /// Renames a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The settings we want to use to override the default settings. public IEnumerator RenameFile(string filePath, string newFilePath, ES3Settings settings) { return RenameFile(new ES3Settings(filePath, settings), new ES3Settings(newFilePath, settings), "", ""); } /// Renames a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The settings we want to use to override the default settings. public IEnumerator RenameFile(string filePath, string newFilePath, string user, ES3Settings settings) { return RenameFile(new ES3Settings(filePath, settings), new ES3Settings(newFilePath, settings), user, ""); } /// Renames a file from the server. An error is *not* returned if the file does not exist. /// The relative or absolute path of the file we want to delete. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator RenameFile(string filePath, string newFilePath, string user, string password, ES3Settings settings) { return RenameFile(new ES3Settings(filePath, settings), new ES3Settings(newFilePath, settings), user, password); } private IEnumerator RenameFile(ES3Settings settings, ES3Settings newSettings, string user, string password) { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("renameFile", settings.path); form.AddField("newFilename", newSettings.path); form.AddField("user", GetUser(user, password)); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); HandleError(webRequest, true); } isDone = true; } #endregion #region DownloadFilenames /// Downloads the names of all of the files on the server. Downloaded filenames are stored in the 'filenames' variable of the ES3Cloud object. /// The unique name of the user we want to find the filenames of. /// The password of the user we want to find the filenames of. public IEnumerator DownloadFilenames(string user="", string password="") { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("getFilenames", ""); form.AddField("user", GetUser(user, password)); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); if(!HandleError(webRequest, false)) _data = webRequest.downloadHandler.data; } isDone = true; } /// Downloads the names of all of the files on the server. Downloaded filenames are stored in the 'filenames' variable of the ES3Cloud object. /// The unique name of the user we want to find the filenames of. /// The password of the user we want to find the filenames of. /// A search pattern containing '%' or '_' wildcards where '%' represents zero, one, or multiple characters, and '_' represents a single character. public IEnumerator SearchFilenames(string searchPattern, string user="", string password="") { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("getFilenames", ""); form.AddField("user", GetUser(user, password)); if (!string.IsNullOrEmpty(searchPattern)) form.AddField("pattern", searchPattern); using (var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); if (!HandleError(webRequest, false)) _data = webRequest.downloadHandler.data; } isDone = true; } #endregion #region DownloadTimestamp /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. public IEnumerator DownloadTimestamp() { return DownloadTimestamp(new ES3Settings(), "", ""); } /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. /// The relative or absolute path of the file we want to get the timestamp of. public IEnumerator DownloadTimestamp(string filePath) { return DownloadTimestamp(new ES3Settings(filePath), "", ""); } /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. /// The relative or absolute path of the file we want to get the timestamp of. /// The unique name of the user this file belongs to, if the file isn't globally accessible. public IEnumerator DownloadTimestamp(string filePath, string user) { return DownloadTimestamp(new ES3Settings(filePath), user, ""); } /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. /// The relative or absolute path of the file we want to get the timestamp of. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. public IEnumerator DownloadTimestamp(string filePath, string user, string password) { return DownloadTimestamp(new ES3Settings(filePath), user, password); } /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. /// The relative or absolute path of the file we want to get the timestamp of. /// The settings we want to use to override the default settings. public IEnumerator DownloadTimestamp(string filePath, ES3Settings settings) { return DownloadTimestamp(new ES3Settings(filePath, settings), "", ""); } /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. /// The relative or absolute path of the file we want to get the timestamp of. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The settings we want to use to override the default settings. public IEnumerator DownloadTimestamp(string filePath, string user, ES3Settings settings) { return DownloadTimestamp(new ES3Settings(filePath, settings), user, ""); } /// Downloads the timestamp representing when the server file was last updated. The downloaded timestamp is stored in the 'timestamp' variable of the ES3Cloud object. /// The relative or absolute path of the file we want to get the timestamp of. /// The unique name of the user this file belongs to, if the file isn't globally accessible. /// The password of the user this file belongs to. /// The settings we want to use to override the default settings. public IEnumerator DownloadTimestamp(string filePath, string user, string password, ES3Settings settings) { return DownloadTimestamp(new ES3Settings(filePath, settings), user, password); } private IEnumerator DownloadTimestamp(ES3Settings settings, string user, string password) { Reset(); var form = CreateWWWForm(); form.AddField("apiKey", apiKey); form.AddField("getTimestamp", settings.path); form.AddField("user", GetUser(user, password)); using(var webRequest = UnityWebRequest.Post(url, form)) { yield return SendWebRequest(webRequest); if(!HandleError(webRequest, false)) _data = webRequest.downloadHandler.data; } isDone = true; } #endregion #region Internal Methods private long DateTimeToUnixTimestamp(DateTime dt) { return Convert.ToInt64((dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds); } private long GetFileTimestamp(ES3Settings settings) { return DateTimeToUnixTimestamp(ES3.GetTimestamp(settings)); } protected override void Reset() { _data = null; base.Reset(); } #endregion } #endif