public void Save(ES3Settings settings, bool append) { using (var writer = new StreamWriter(ES3Stream.CreateStream(settings, append ? ES3FileMode.Append : ES3FileMode.Write))) { // If data already exists and we're appending, we need to prepend a newline. if (append && ES3.FileExists(settings)) { writer.Write(NEWLINE_CHAR); } var array = ToArray(); for (int row = 0; row < rows; row++) { if (row != 0) { writer.Write(NEWLINE_CHAR); } for (int col = 0; col < cols; col++) { if (col != 0) { writer.Write(COMMA_CHAR); } writer.Write(Escape(array [col, row])); } } } if (!append) { ES3IO.CommitBackup(settings); } }
/// <summary>Saves a Texture2D as a PNG or JPG, depending on the file extension used for the filePath.</summary> /// <param name="texture">The Texture2D we want to save as a JPG or PNG.</param> /// <param name="settings">The settings we want to use to override the default settings.</param> public static void SaveImage(Texture2D texture, ES3Settings settings) { // Get the file extension to determine what format we want to save the image as. string extension = ES3IO.GetExtension(settings.path).ToLower(); if (string.IsNullOrEmpty(extension)) { throw new System.ArgumentException("File path must have a file extension when using ES3.SaveImage."); } byte[] bytes; if (extension == ".jpg" || extension == ".jpeg") { bytes = texture.EncodeToJPG(); } else if (extension == ".png") { bytes = texture.EncodeToPNG(); } else { throw new System.ArgumentException("File path must have extension of .png, .jpg or .jpeg when using ES3.SaveImage."); } ES3.SaveRaw(bytes, settings); }
/// <summary>Gets an array of all of the sub-directory names in a directory.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> public static string[] GetDirectories(ES3Settings settings) { if (settings.location != ES3.Location.File) { throw new System.NotSupportedException("ES3.GetDirectories can only be used when the location is set to File."); } return(ES3IO.GetDirectories(settings.FullPath, false)); }
/// <summary>Creates or overwrites a file with the specified raw bytes.</summary> /// <param name="bytes">The bytes we want to store.</param> /// <param name="settings">The settings we want to use to override the default settings.</param> public static void SaveRaw(byte[] bytes, ES3Settings settings) { using (var stream = ES3Stream.CreateStream(settings, ES3FileMode.Write)) { stream.Write(bytes, 0, bytes.Length); } ES3IO.CommitBackup(settings); }
/// <summary>Stores the contents of the writer and overwrites any existing keys if overwriting is enabled.</summary> /// <param name="overwriteKeys">Whether we should overwrite existing keys.</param> public virtual void Save(bool overwriteKeys) { if (overwriteKeys) { Merge(); } EndWriteFile(); Dispose(); // If we're writing to a location which can become corrupted, rename the backup file to the file we want. // This prevents corrupt data. ES3IO.CommitBackup(settings); }
/// <summary>Deletes the directory at the given path using the settings provided.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> public static void DeleteDirectory(ES3Settings settings) { if (settings.location == Location.File) { ES3IO.DeleteDirectory(settings.FullPath); } else if (settings.location == Location.PlayerPrefs) { throw new System.NotSupportedException("Deleting Directories using PlayerPrefs is not supported."); } else if (settings.location == Location.Resources) { throw new System.NotSupportedException("Deleting directories from Resources is not allowed."); } }
/// <summary>Deletes the file specified by the ES3Settings object provided as a parameter.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> public static void DeleteFile(ES3Settings settings) { if (settings.location == Location.File) { ES3IO.DeleteFile(settings.FullPath); } else if (settings.location == Location.PlayerPrefs) { PlayerPrefs.DeleteKey(settings.FullPath); } else if (settings.location == Location.Resources) { throw new System.NotSupportedException("Deleting files from Resources is not supported."); } }
/// <summary>Gets the date and time the file was last updated, in the UTC timezone.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> /// <returns>A DateTime object represeting the UTC date and time the file was last updated.</returns> public static DateTime GetTimestamp(ES3Settings settings) { if (settings.location == Location.File) { return(ES3IO.GetTimestamp(settings.FullPath)); } else if (settings.location == Location.PlayerPrefs) { return(new DateTime(long.Parse(PlayerPrefs.GetString("timestamp_" + settings.FullPath, "0")), DateTimeKind.Utc)); } else { return(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); } }
/// <summary>Checks whether a folder exists.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> /// <returns>True if the folder exists, otherwise False.</returns> public static bool DirectoryExists(ES3Settings settings) { if (settings.location == Location.File) { return(ES3IO.DirectoryExists(settings.FullPath)); } else if (settings.location == Location.PlayerPrefs) { throw new System.NotSupportedException("Directories are not supported for the PlayerPrefs location."); } else if (settings.location == Location.Resources) { throw new System.NotSupportedException("Checking existence of folder in Resources not supported."); } return(false); }
/// <summary>Checks whether a file exists.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> /// <returns>True if the file exists, otherwise False.</returns> public static bool FileExists(ES3Settings settings) { if (settings.location == Location.File) { return(ES3IO.FileExists(settings.FullPath)); } else if (settings.location == Location.PlayerPrefs) { return(PlayerPrefs.HasKey(settings.FullPath)); } else if (settings.location == Location.Resources) { return(Resources.Load(settings.FullPath) != null); } return(false); }
/// <summary>Loads the default file as a byte array.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> public static byte[] LoadRawBytes(ES3Settings settings) { if (settings.location == Location.File) { return(ES3IO.ReadAllBytes(settings.FullPath)); } else if (settings.location == Location.PlayerPrefs) { return(System.Convert.FromBase64String(PlayerPrefs.GetString(settings.FullPath))); } else if (settings.location == Location.Resources) { var textAsset = Resources.Load <TextAsset>(settings.FullPath); return(textAsset.bytes); } return(null); }
/// <summary>Loads an audio file as an AudioClip. Note that MP3 files are not supported on standalone platforms and Ogg Vorbis files are not supported on mobile platforms.</summary> /// <param name="imagePath">The relative or absolute path of the audio file we want to load as an AudioClip.</param> /// <param name="settings">The settings we want to use to override the default settings.</param> public static AudioClip LoadAudio(string audioFilePath, ES3Settings settings) { if (Application.platform == RuntimePlatform.WebGLPlayer) { Debug.LogError("You cannot use ES3.LoadAudio with Unity Web Player"); } string extension = ES3IO.GetExtension(audioFilePath).ToLower(); if (extension == ".mp3" && (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXPlayer)) { throw new System.InvalidOperationException("You can only load Ogg, WAV, XM, IT, MOD or S3M on Unity Standalone"); } if (extension == ".ogg" && (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WSAPlayerARM)) { throw new System.InvalidOperationException("You can only load MP3, WAV, XM, IT, MOD or S3M on Unity Standalone"); } var newSettings = new ES3Settings(audioFilePath, settings); #if UNITY_2017_1_OR_NEWER WWW www = new WWW(newSettings.FullPath); #else WWW www = new WWW("file://" + newSettings.FullPath); #endif while (!www.isDone) { // Wait for it to load. } if (!string.IsNullOrEmpty(www.error)) { throw new System.Exception(www.error); } #if UNITY_2017_3_OR_NEWER return(www.GetAudioClip(true)); #elif UNITY_5_6_OR_NEWER return(WWWAudioExtensions.GetAudioClip(www)); #else return(www.audioClip); #endif }
/// <summary>Copies a file from one location to another, using the ES3Settings provided to determine the locations.</summary> /// <param name="oldSettings">The settings we want to use when copying the old file.</param> /// <param name="newSettings">The settings we want to use when creating the new file.</param> public static void CopyFile(ES3Settings oldSettings, ES3Settings newSettings) { if (oldSettings.location == Location.File) { if (ES3IO.FileExists(oldSettings.FullPath)) { ES3IO.DeleteFile(newSettings.FullPath); ES3IO.CopyFile(oldSettings.FullPath, newSettings.FullPath); } } else if (oldSettings.location == Location.PlayerPrefs) { PlayerPrefs.SetString(newSettings.FullPath, PlayerPrefs.GetString(oldSettings.FullPath)); } else if (oldSettings.location == Location.Resources) { throw new System.NotSupportedException("Modifying files from Resources is not allowed."); } }
public void Save(ES3Settings settings, bool append) { using (var writer = new StreamWriter(ES3Stream.CreateStream(settings, append ? ES3FileMode.Append : ES3FileMode.Write))) { var array = ToArray(); for (int row = 0; row < rows; row++) { if (row != 0) { writer.Write('\n'); } for (int col = 0; col < cols; col++) { if (col != 0) { writer.Write(','); } writer.Write(Escape(array [col, row])); } } } ES3IO.CommitBackup(settings); }