using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using ES3Internal; #if UNITY_2018_3_OR_NEWER using UnityEngine.Networking; #endif public static class ES3 { public enum Location { File, PlayerPrefs, InternalMS, Resources, Cache }; public enum Directory { PersistentDataPath, DataPath } public enum EncryptionType { None, AES }; public enum CompressionType { None, Gzip}; public enum Format { JSON, Binary_Alpha }; public enum ReferenceMode { ByRef, ByValue, ByRefAndValue}; #region ES3.Save /*// Saves the value to the default file with the given key. /// The key we want to use to identify our value in the file. /// The value we want to save. public static void Save(string key, object value) { Save(key, value, new ES3Settings()); } /// Saves the value to a file with the given key. /// The key we want to use to identify our value in the file. /// The value we want to save. /// The relative or absolute path of the file we want to store our value to. public static void Save(string key, object value, string filePath) { Save(key, value, new ES3Settings(filePath)); } /// Saves the value to a file with the given key. /// The key we want to use to identify our value in the file. /// The value we want to save. /// The relative or absolute path of the file we want to store our value to. /// The settings we want to use to override the default settings. public static void Save(string key, object value, string filePath, ES3Settings settings) { Save(key, value, new ES3Settings(filePath, settings)); } /// Saves the value to a file with the given key. /// The key we want to use to identify our value in the file. /// The value we want to save. /// The settings we want to use to override the default settings. public static void Save(string key, object value, ES3Settings settings) { using (var writer = ES3Writer.Create(settings)) { writer.Write(key, value); writer.Save(); } }*/ /// Saves the value to the default file with the given key. /// The type of the data that we want to save. /// The key we want to use to identify our value in the file. /// The value we want to save. public static void Save(string key, T value) { Save(key, value, new ES3Settings()); } /// Saves the value to a file with the given key. /// The type of the data that we want to save. /// The key we want to use to identify our value in the file. /// The value we want to save. /// The relative or absolute path of the file we want to store our value to. public static void Save(string key, T value, string filePath) { Save(key, value, new ES3Settings(filePath)); } /// Saves the value to a file with the given key. /// The type of the data that we want to save. /// The key we want to use to identify our value in the file. /// The value we want to save. /// The relative or absolute path of the file we want to store our value to. /// The settings we want to use to override the default settings. public static void Save(string key, T value, string filePath, ES3Settings settings) { Save(key, value, new ES3Settings(filePath, settings)); } /// Saves the value to a file with the given key. /// The type of the data that we want to save. /// The key we want to use to identify our value in the file. /// The value we want to save. /// The settings we want to use to override the default settings. public static void Save(string key, T value, ES3Settings settings) { if (settings.location == Location.Cache) { ES3File.GetOrCreateCachedFile(settings).Save(key, value); return; } using (var writer = ES3Writer.Create(settings)) { writer.Write(key, value); writer.Save(); } } /// Creates or overwrites a file with the specified raw bytes. /// The bytes we want to store. public static void SaveRaw(byte[] bytes) { SaveRaw(bytes, new ES3Settings()); } /// Creates or overwrites a file with the specified raw bytes. /// The bytes we want to store. /// The relative or absolute path of the file we want to store our bytes to. public static void SaveRaw(byte[] bytes, string filePath) { SaveRaw(bytes, new ES3Settings(filePath)); } /// Creates or overwrites a file with the specified raw bytes. /// The bytes we want to store. /// The relative or absolute path of the file we want to store our bytes to. /// The settings we want to use to override the default settings. public static void SaveRaw(byte[] bytes, string filePath, ES3Settings settings) { SaveRaw(bytes, new ES3Settings(filePath, settings)); } /// Creates or overwrites a file with the specified raw bytes. /// The bytes we want to store. /// The settings we want to use to override the default settings. public static void SaveRaw(byte[] bytes, ES3Settings settings) { if (settings.location == Location.Cache) { ES3File.GetOrCreateCachedFile(settings).SaveRaw(bytes); return; } using (var stream = ES3Stream.CreateStream(settings, ES3FileMode.Write)) { stream.Write(bytes, 0, bytes.Length); } ES3IO.CommitBackup(settings); } /// Creates or overwrites the default file with the specified raw bytes. /// The string we want to store. public static void SaveRaw(string str) { SaveRaw(str, new ES3Settings()); } /// Creates or overwrites the default file with the specified raw bytes. /// The string we want to store. /// The relative or absolute path of the file we want to store our bytes to. public static void SaveRaw(string str, string filePath) { SaveRaw(str, new ES3Settings(filePath)); } /// Creates or overwrites a file with the specified raw bytes. /// The string we want to store. /// The relative or absolute path of the file we want to store our bytes to. /// The settings we want to use to override the default settings. public static void SaveRaw(string str, string filePath, ES3Settings settings) { SaveRaw(str, new ES3Settings(filePath, settings)); } /// Creates or overwrites a file with the specified raw bytes. /// The string we want to store. /// The settings we want to use to override the default settings. public static void SaveRaw(string str, ES3Settings settings) { var bytes = settings.encoding.GetBytes(str); SaveRaw(bytes, settings); } /// Creates or appends the specified bytes to a file. /// The bytes we want to append. public static void AppendRaw(byte[] bytes) { AppendRaw(bytes, new ES3Settings()); } /// Creates or appends the specified bytes to a file. /// The bytes we want to append. /// The relative or absolute path of the file we want to append our bytes to. /// The settings we want to use to override the default settings. public static void AppendRaw(byte[] bytes, string filePath, ES3Settings settings) { AppendRaw(bytes, new ES3Settings(filePath, settings)); } /// Creates or appends the specified bytes to a file. /// The bytes we want to append. /// The settings we want to use to override the default settings. public static void AppendRaw(byte[] bytes, ES3Settings settings) { if (settings.location == Location.Cache) { ES3File.GetOrCreateCachedFile(settings).AppendRaw(bytes); return; } ES3Settings newSettings = new ES3Settings(settings.path, settings); newSettings.encryptionType = EncryptionType.None; newSettings.compressionType = CompressionType.None; using (var stream = ES3Stream.CreateStream(newSettings, ES3FileMode.Append)) stream.Write(bytes, 0, bytes.Length); } /// Creates or appends the specified bytes to the default file. /// The bytes we want to append. public static void AppendRaw(string str) { AppendRaw(str, new ES3Settings()); } /// Creates or appends the specified bytes to a file. /// The bytes we want to append. /// The relative or absolute path of the file we want to append our bytes to. /// The settings we want to use to override the default settings. public static void AppendRaw(string str, string filePath, ES3Settings settings) { AppendRaw(str, new ES3Settings(filePath, settings)); } /// Creates or appends the specified bytes to a file. /// The bytes we want to append. /// The settings we want to use to override the default settings. public static void AppendRaw(string str, ES3Settings settings) { var bytes = settings.encoding.GetBytes(str); ES3Settings newSettings = new ES3Settings(settings.path, settings); newSettings.encryptionType = EncryptionType.None; newSettings.compressionType = CompressionType.None; if (settings.location == Location.Cache) { ES3File.GetOrCreateCachedFile(settings).SaveRaw(bytes); return; } using (var stream = ES3Stream.CreateStream(newSettings, ES3FileMode.Append)) stream.Write(bytes, 0, bytes.Length); } /// Saves a Texture2D as a PNG or JPG, depending on the file extension used for the filePath. /// The Texture2D we want to save as a JPG or PNG. /// The relative or absolute path of the PNG or JPG file we want to create. public static void SaveImage(Texture2D texture, string imagePath) { SaveImage(texture, new ES3Settings(imagePath)); } /// Saves a Texture2D as a PNG or JPG, depending on the file extension used for the filePath. /// The Texture2D we want to save as a JPG or PNG. /// The relative or absolute path of the PNG or JPG file we want to create. public static void SaveImage(Texture2D texture, string imagePath, ES3Settings settings) { SaveImage(texture, new ES3Settings(imagePath, settings)); } /// Saves a Texture2D as a PNG or JPG, depending on the file extension used for the filePath. /// The Texture2D we want to save as a JPG or PNG. /// The settings we want to use to override the default settings. 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); } #endregion #region ES3.Load /* Standard load methods */ /// Loads the value from a file with the given key. /// The key which identifies the value we want to load. public static object Load(string key) { return Load(key, new ES3Settings()); } /// Loads the value from a file with the given key. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. public static object Load(string key, string filePath) { return Load(key, new ES3Settings(filePath)); } /// Loads the value from a file with the given key. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The settings we want to use to override the default settings. public static object Load(string key, string filePath, ES3Settings settings) { return Load(key, new ES3Settings(filePath, settings)); } /// Loads the value from a file with the given key. /// The key which identifies the value we want to load. /// The settings we want to use to override the default settings. public static object Load(string key, ES3Settings settings) { return Load(key, settings); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. public static T Load(string key) { return Load(key, new ES3Settings()); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. public static T Load(string key, string filePath) { return Load(key, new ES3Settings(filePath)); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The settings we want to use to override the default settings. public static T Load(string key, string filePath, ES3Settings settings) { return Load(key, new ES3Settings(filePath, settings)); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The settings we want to use to override the default settings. public static T Load(string key, ES3Settings settings) { if (settings.location == Location.Cache) return ES3File.GetOrCreateCachedFile(settings).Load(key); using (var reader = ES3Reader.Create(settings)) { if (reader == null) throw new System.IO.FileNotFoundException("File \"" + settings.FullPath + "\" could not be found."); return reader.Read(key); } } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The value we want to return if the file or key does not exist. public static T Load(string key, T defaultValue) { return Load(key, defaultValue, new ES3Settings()); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The value we want to return if the file or key does not exist. public static T Load(string key, string filePath, T defaultValue) { return Load(key, defaultValue, new ES3Settings(filePath)); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The value we want to return if the file or key does not exist. /// The settings we want to use to override the default settings. public static T Load(string key, string filePath, T defaultValue, ES3Settings settings) { return Load(key, defaultValue, new ES3Settings(filePath, settings)); } /// Loads the value from a file with the given key. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The value we want to return if the file or key does not exist. /// The settings we want to use to override the default settings. public static T Load(string key, T defaultValue, ES3Settings settings) { if (settings.location == Location.Cache) return ES3File.GetOrCreateCachedFile(settings).Load(key, defaultValue); using (var reader = ES3Reader.Create(settings)) { if (reader == null) return defaultValue; return reader.Read(key, defaultValue); } } /* Self-assigning load methods */ /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The key which identifies the value we want to load. /// The object we want to load the value into. public static void LoadInto(string key, object obj) { LoadInto(key, obj, new ES3Settings()); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The object we want to load the value into. public static void LoadInto(string key, string filePath, object obj) { LoadInto(key, obj, new ES3Settings(filePath)); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The object we want to load the value into. /// The settings we want to use to override the default settings. public static void LoadInto(string key, string filePath, object obj, ES3Settings settings) { LoadInto(key, obj, new ES3Settings(filePath, settings)); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The key which identifies the value we want to load. /// The object we want to load the value into. /// The settings we want to use to override the default settings. public static void LoadInto(string key, object obj, ES3Settings settings) { LoadInto(key, obj, settings); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The object we want to load the value into. public static void LoadInto(string key, T obj) where T : class { LoadInto(key, obj, new ES3Settings()); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The object we want to load the value into. public static void LoadInto(string key, string filePath, T obj) where T : class { LoadInto(key, obj, new ES3Settings(filePath)); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The relative or absolute path of the file we want to load from. /// The object we want to load the value into. /// The settings we want to use to override the default settings. public static void LoadInto(string key, string filePath, T obj, ES3Settings settings) where T : class { LoadInto(key, obj, new ES3Settings(filePath, settings)); } /// Loads the value from a file with the given key into an existing object, rather than creating a new instance. /// The type of the data that we want to load. /// The key which identifies the value we want to load. /// The object we want to load the value into. /// The settings we want to use to override the default settings. public static void LoadInto(string key, T obj, ES3Settings settings) where T : class { if (settings.location == Location.Cache) { ES3File.GetOrCreateCachedFile(settings).LoadInto(key, obj); return; } if (settings == null) settings = new ES3Settings(); using (var reader = ES3Reader.Create(settings)) { if (reader == null) throw new System.IO.FileNotFoundException("File \"" + settings.FullPath + "\" could not be found."); reader.ReadInto(key, obj); } } /* LoadString method, as this can be difficult with overloads. */ /// Loads the value from a file with the given key. /// The key which identifies the value we want to load. /// The value we want to return if the file or key does not exist. /// The relative or absolute path of the file we want to load from. public static string LoadString(string key, string defaultValue, string filePath="") { return Load(key, defaultValue, new ES3Settings(filePath)); } #endregion #region Other ES3.Load Methods /// Loads the default file as a byte array. public static byte[] LoadRawBytes() { return LoadRawBytes(new ES3Settings()); } /// Loads a file as a byte array. /// The relative or absolute path of the file we want to load as a byte array. public static byte[] LoadRawBytes(string filePath) { return LoadRawBytes(new ES3Settings(filePath)); } /// Loads a file as a byte array. /// The relative or absolute path of the file we want to load as a byte array. /// The settings we want to use to override the default settings. public static byte[] LoadRawBytes(string filePath, ES3Settings settings) { return LoadRawBytes(new ES3Settings(filePath, settings)); } /// Loads the default file as a byte array. /// The settings we want to use to override the default settings. public static byte[] LoadRawBytes(ES3Settings settings) { if (settings.location == Location.Cache) return ES3File.GetOrCreateCachedFile(settings).LoadRawBytes(); using (var stream = ES3Stream.CreateStream(settings, ES3FileMode.Read)) { if (stream.GetType() == typeof(System.IO.Compression.GZipStream)) { var gZipStream = (System.IO.Compression.GZipStream)stream; using (var ms = new System.IO.MemoryStream()) { ES3Stream.CopyTo(gZipStream, ms); return ms.ToArray(); } } else { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); return bytes; } } /*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(settings.FullPath); return textAsset.bytes; } return null;*/ } /// Loads the default file as a byte array. public static string LoadRawString() { return LoadRawString(new ES3Settings()); } /// Loads a file as a byte array. /// The relative or absolute path of the file we want to load as a byte array. /// The settings we want to use to override the default settings. public static string LoadRawString(string filePath) { return LoadRawString(new ES3Settings(filePath)); } /// Loads a file as a byte array. /// The relative or absolute path of the file we want to load as a byte array. /// The settings we want to use to override the default settings. public static string LoadRawString(string filePath, ES3Settings settings) { return LoadRawString(new ES3Settings(filePath, settings)); } /// Loads the default file as a byte array. /// The settings we want to use to override the default settings. public static string LoadRawString(ES3Settings settings) { var bytes = ES3.LoadRawBytes(settings); return settings.encoding.GetString(bytes, 0, bytes.Length); } /// Loads a PNG or JPG as a Texture2D. /// The relative or absolute path of the PNG or JPG file we want to load as a Texture2D. /// The settings we want to use to override the default settings. public static Texture2D LoadImage(string imagePath) { return LoadImage(new ES3Settings(imagePath)); } /// Loads a PNG or JPG as a Texture2D. /// The relative or absolute path of the PNG or JPG file we want to load as a Texture2D. /// The settings we want to use to override the default settings. public static Texture2D LoadImage(string imagePath, ES3Settings settings) { return LoadImage(new ES3Settings(imagePath, settings)); } /// Loads a PNG or JPG as a Texture2D. /// The settings we want to use to override the default settings. public static Texture2D LoadImage(ES3Settings settings) { byte[] bytes = ES3.LoadRawBytes(settings); return LoadImage(bytes); } /// Loads a PNG or JPG as a Texture2D. /// The raw bytes of the PNG or JPG. public static Texture2D LoadImage(byte[] bytes) { var texture = new Texture2D(1, 1); texture.LoadImage(bytes); return texture; } /// 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. /// The relative or absolute path of the audio file we want to load as an AudioClip. public static AudioClip LoadAudio(string audioFilePath #if UNITY_2018_3_OR_NEWER , AudioType audioType #endif ) { return LoadAudio(audioFilePath, #if UNITY_2018_3_OR_NEWER audioType, #endif new ES3Settings()); } /// 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. /// The relative or absolute path of the audio file we want to load as an AudioClip. /// The settings we want to use to override the default settings. public static AudioClip LoadAudio(string audioFilePath, #if UNITY_2018_3_OR_NEWER AudioType audioType, #endif ES3Settings settings) { if (settings.location != Location.File) throw new InvalidOperationException("ES3.LoadAudio can only be used with the File save location"); if (Application.platform == RuntimePlatform.WebGLPlayer) throw new InvalidOperationException("You cannot use ES3.LoadAudio with WebGL"); 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_2018_3_OR_NEWER using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://" + newSettings.FullPath, audioType)) { www.SendWebRequest(); while (!www.isDone) { // Wait for it to load. } if (www.isNetworkError) throw new System.Exception(www.error); else return DownloadHandlerAudioClip.GetContent(www); } #elif UNITY_2017_1_OR_NEWER WWW www = new WWW(newSettings.FullPath); while(!www.isDone) { // Wait for it to load. } if(!string.IsNullOrEmpty(www.error)) throw new System.Exception(www.error); #else WWW www = new WWW("file://"+newSettings.FullPath); while(!www.isDone) { // Wait for it to load. } if(!string.IsNullOrEmpty(www.error)) throw new System.Exception(www.error); #endif #if UNITY_2017_3_OR_NEWER && !UNITY_2018_3_OR_NEWER return www.GetAudioClip(true); #elif UNITY_5_6_OR_NEWER && !UNITY_2018_3_OR_NEWER return WWWAudioExtensions.GetAudioClip(www); #endif } #endregion #region Serialize/Deserialize public static byte[] Serialize(T value, ES3Settings settings=null) { if (settings == null) settings = new ES3Settings(); using (var stream = new System.IO.MemoryStream()) { using (var baseWriter = ES3Writer.Create(stream, settings, false, false)) baseWriter.Write(value, ES3TypeMgr.GetOrCreateES3Type(typeof(T)), settings.referenceMode); return stream.ToArray(); } } public static T Deserialize(byte[] bytes, ES3Settings settings=null) { return (T)Deserialize(ES3TypeMgr.GetOrCreateES3Type(typeof(T)), bytes, settings); } internal static object Deserialize(ES3Types.ES3Type type, byte[] bytes, ES3Settings settings = null) { if (settings == null) settings = new ES3Settings(); using (var ms = new System.IO.MemoryStream(bytes, false)) using (var reader = ES3Reader.Create(ms, settings, false)) return reader.Read(type); } public static void DeserializeInto(byte[] bytes, T obj, ES3Settings settings = null) where T : class { DeserializeInto(ES3TypeMgr.GetOrCreateES3Type(typeof(T)), bytes, obj, settings); } public static void DeserializeInto(ES3Types.ES3Type type, byte[] bytes, T obj, ES3Settings settings = null) where T : class { if (settings == null) settings = new ES3Settings(); using (var ms = new System.IO.MemoryStream(bytes, false)) using (var reader = ES3Reader.Create(ms, settings, false)) reader.ReadInto(obj, type); } #endregion #region Other ES3 Methods /// Deletes the default file. public static void DeleteFile() { DeleteFile(new ES3Settings()); } /// Deletes the file at the given path using the default settings. /// The relative or absolute path of the file we wish to delete. public static void DeleteFile(string filePath) { DeleteFile(new ES3Settings(filePath)); } /// Deletes the file at the given path using the settings provided. /// The relative or absolute path of the file we wish to delete. /// The settings we want to use to override the default settings. public static void DeleteFile(string filePath, ES3Settings settings) { DeleteFile(new ES3Settings(filePath, settings)); } /// Deletes the file specified by the ES3Settings object provided as a parameter. /// The settings we want to use to override the default settings. 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.Cache) ES3File.RemoveCachedFile(settings); else if (settings.location == Location.Resources) throw new System.NotSupportedException("Deleting files from Resources is not supported."); } /// Copies a file from one path to another. /// The relative or absolute path of the file we want to copy. /// The relative or absolute path of the copy we want to create. public static void CopyFile(string oldFilePath, string newFilePath) { CopyFile(new ES3Settings(oldFilePath), new ES3Settings(newFilePath)); } /// Copies a file from one location to another, using the ES3Settings provided to override any default settings. /// The relative or absolute path of the file we want to copy. /// The relative or absolute path of the copy we want to create. /// The settings we want to use when copying the old file. /// The settings we want to use when creating the new file. public static void CopyFile(string oldFilePath, string newFilePath, ES3Settings oldSettings, ES3Settings newSettings) { CopyFile(new ES3Settings(oldFilePath, oldSettings), new ES3Settings(newFilePath, newSettings)); } /// Copies a file from one location to another, using the ES3Settings provided to determine the locations. /// The settings we want to use when copying the old file. /// The settings we want to use when creating the new file. public static void CopyFile(ES3Settings oldSettings, ES3Settings newSettings) { if (oldSettings.location != newSettings.location) throw new InvalidOperationException("Cannot copy file from " + oldSettings.location + " to " + newSettings.location + ". Location must be the same for both source and destination."); 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.Cache) { ES3File.CopyCachedFile(oldSettings, newSettings); } else if (oldSettings.location == Location.Resources) throw new System.NotSupportedException("Modifying files from Resources is not allowed."); } /// Renames a file. /// The relative or absolute path of the file we want to rename. /// The relative or absolute path we want to rename the file to. public static void RenameFile(string oldFilePath, string newFilePath) { RenameFile(new ES3Settings(oldFilePath), new ES3Settings(newFilePath)); } /// Renames a file. /// The relative or absolute path of the file we want to rename. /// The relative or absolute path we want to rename the file to. /// The settings for the file we want to rename. /// The settings for the file we want our source file to be renamed to. public static void RenameFile(string oldFilePath, string newFilePath, ES3Settings oldSettings, ES3Settings newSettings) { RenameFile(new ES3Settings(oldFilePath, oldSettings), new ES3Settings(newFilePath, newSettings)); } /// Renames a file. /// The settings for the file we want to rename. /// The settings for the file we want our source file to be renamed to. public static void RenameFile(ES3Settings oldSettings, ES3Settings newSettings) { if (oldSettings.location != newSettings.location) throw new InvalidOperationException("Cannot rename file in " + oldSettings.location + " to " + newSettings.location + ". Location must be the same for both source and destination."); if (oldSettings.location == Location.File) { if (ES3IO.FileExists(oldSettings.FullPath)) { ES3IO.DeleteFile(newSettings.FullPath); ES3IO.MoveFile(oldSettings.FullPath, newSettings.FullPath); } } else if (oldSettings.location == Location.PlayerPrefs) { PlayerPrefs.SetString(newSettings.FullPath, PlayerPrefs.GetString(oldSettings.FullPath)); PlayerPrefs.DeleteKey(oldSettings.FullPath); } else if (oldSettings.location == Location.Cache) { ES3File.CopyCachedFile(oldSettings, newSettings); ES3File.RemoveCachedFile(oldSettings); } else if (oldSettings.location == Location.Resources) throw new System.NotSupportedException("Modifying files from Resources is not allowed."); } /// Copies a file from one path to another. /// The relative or absolute path of the directory we want to copy. /// The relative or absolute path of the copy we want to create. public static void CopyDirectory(string oldDirectoryPath, string newDirectoryPath) { CopyDirectory(new ES3Settings(oldDirectoryPath), new ES3Settings(newDirectoryPath)); } /// Copies a file from one location to another, using the ES3Settings provided to override any default settings. /// The relative or absolute path of the directory we want to copy. /// The relative or absolute path of the copy we want to create. /// The settings we want to use when copying the old directory. /// The settings we want to use when creating the new directory. public static void CopyDirectory(string oldDirectoryPath, string newDirectoryPath, ES3Settings oldSettings, ES3Settings newSettings) { CopyDirectory(new ES3Settings(oldDirectoryPath, oldSettings), new ES3Settings(newDirectoryPath, newSettings)); } /// Copies a file from one location to another, using the ES3Settings provided to determine the locations. /// The settings we want to use when copying the old file. /// The settings we want to use when creating the new file. public static void CopyDirectory(ES3Settings oldSettings, ES3Settings newSettings) { if (oldSettings.location != Location.File) throw new InvalidOperationException("ES3.CopyDirectory can only be used when the save location is 'File'"); if (!DirectoryExists(oldSettings)) throw new System.IO.DirectoryNotFoundException("Directory " + oldSettings.FullPath + " not found"); if (!DirectoryExists(newSettings)) ES3IO.CreateDirectory(newSettings.FullPath); foreach (var fileName in ES3.GetFiles(oldSettings)) CopyFile(ES3IO.CombinePathAndFilename(oldSettings.path, fileName), ES3IO.CombinePathAndFilename(newSettings.path, fileName)); foreach (var directoryName in GetDirectories(oldSettings)) CopyDirectory(ES3IO.CombinePathAndFilename(oldSettings.path, directoryName), ES3IO.CombinePathAndFilename(newSettings.path, directoryName)); } /// Renames a file. /// The relative or absolute path of the file we want to rename. /// The relative or absolute path we want to rename the file to. public static void RenameDirectory(string oldDirectoryPath, string newDirectoryPath) { RenameDirectory(new ES3Settings(oldDirectoryPath), new ES3Settings(newDirectoryPath)); } /// Renames a file. /// The relative or absolute path of the file we want to rename. /// The relative or absolute path we want to rename the file to. /// The settings for the file we want to rename. /// The settings for the file we want our source file to be renamed to. public static void RenameDirectory(string oldDirectoryPath, string newDirectoryPath, ES3Settings oldSettings, ES3Settings newSettings) { RenameDirectory(new ES3Settings(oldDirectoryPath, oldSettings), new ES3Settings(newDirectoryPath, newSettings)); } /// Renames a file. /// The settings for the file we want to rename. /// The settings for the file we want our source file to be renamed to. public static void RenameDirectory(ES3Settings oldSettings, ES3Settings newSettings) { if (oldSettings.location == Location.File) { if (ES3IO.DirectoryExists(oldSettings.FullPath)) { ES3IO.DeleteDirectory(newSettings.FullPath); ES3IO.MoveDirectory(oldSettings.FullPath, newSettings.FullPath); } } else if (oldSettings.location == Location.PlayerPrefs || oldSettings.location == Location.Cache) throw new System.NotSupportedException("Directories cannot be renamed when saving to Cache, PlayerPrefs, tvOS or using WebGL."); else if (oldSettings.location == Location.Resources) throw new System.NotSupportedException("Modifying files from Resources is not allowed."); } /// Deletes the directory at the given path using the settings provided. /// The relative or absolute path of the folder we wish to delete. public static void DeleteDirectory(string directoryPath) { DeleteDirectory(new ES3Settings(directoryPath)); } /// Deletes the directory at the given path using the settings provided. /// The relative or absolute path of the folder we wish to delete. /// The settings we want to use to override the default settings. public static void DeleteDirectory(string directoryPath, ES3Settings settings) { DeleteDirectory(new ES3Settings(directoryPath, settings)); } /// Deletes the directory at the given path using the settings provided. /// The settings we want to use to override the default settings. public static void DeleteDirectory(ES3Settings settings) { if (settings.location == Location.File) ES3IO.DeleteDirectory(settings.FullPath); else if (settings.location == Location.PlayerPrefs || settings.location == Location.Cache) throw new System.NotSupportedException("Deleting Directories using Cache or PlayerPrefs is not supported."); else if (settings.location == Location.Resources) throw new System.NotSupportedException("Deleting directories from Resources is not allowed."); } /// Deletes a key in the default file. /// The key we want to delete. public static void DeleteKey(string key) { DeleteKey(key, new ES3Settings()); } public static void DeleteKey(string key, string filePath) { DeleteKey(key, new ES3Settings(filePath)); } /// Deletes a key in the file specified. /// The key we want to delete. /// The relative or absolute path of the file we want to delete the key from. /// The settings we want to use to override the default settings. public static void DeleteKey(string key, string filePath, ES3Settings settings) { DeleteKey(key, new ES3Settings(filePath, settings)); } /// Deletes a key in the file specified by the ES3Settings object. /// The key we want to delete. /// The settings we want to use to override the default settings. public static void DeleteKey(string key, ES3Settings settings) { if (settings.location == Location.Resources) throw new System.NotSupportedException("Modifying files in Resources is not allowed."); else if (settings.location == Location.Cache) ES3File.DeleteKey(key, settings); else if (ES3.FileExists(settings)) { using (var writer = ES3Writer.Create(settings)) { writer.MarkKeyForDeletion(key); writer.Save(); } } } /// Checks whether a key exists in the default file. /// The key we want to check the existence of. /// True if the key exists, otherwise False. public static bool KeyExists(string key) { return KeyExists(key, new ES3Settings()); } /// Checks whether a key exists in the specified file. /// The key we want to check the existence of. /// The relative or absolute path of the file we want to search. /// True if the key exists, otherwise False. public static bool KeyExists(string key, string filePath) { return KeyExists(key, new ES3Settings(filePath)); } /// Checks whether a key exists in the default file. /// The key we want to check the existence of. /// The relative or absolute path of the file we want to search. /// The settings we want to use to override the default settings. /// True if the key exists, otherwise False. public static bool KeyExists(string key, string filePath, ES3Settings settings) { return KeyExists(key, new ES3Settings(filePath, settings)); } /// Checks whether a key exists in a file. /// The key we want to check the existence of. /// The settings we want to use to override the default settings. /// True if the file exists, otherwise False. public static bool KeyExists(string key, ES3Settings settings) { if (settings.location == Location.Cache) return ES3File.KeyExists(key, settings); using (var reader = ES3Reader.Create(settings)) { if (reader == null) return false; return reader.Goto(key); } } /// Checks whether the default file exists. /// The relative or absolute path of the file we want to check the existence of. /// True if the file exists, otherwise False. public static bool FileExists() { return FileExists(new ES3Settings()); } /// Checks whether a file exists. /// The relative or absolute path of the file we want to check the existence of. /// True if the file exists, otherwise False. public static bool FileExists(string filePath) { return FileExists(new ES3Settings(filePath)); } /// Checks whether a file exists. /// The relative or absolute path of the file we want to check the existence of. /// The settings we want to use to override the default settings. /// True if the file exists, otherwise False. public static bool FileExists(string filePath, ES3Settings settings) { return FileExists(new ES3Settings(filePath, settings)); } /// Checks whether a file exists. /// The settings we want to use to override the default settings. /// True if the file exists, otherwise False. 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.Cache) return ES3File.FileExists(settings); else if (settings.location == Location.Resources) return Resources.Load(settings.FullPath) != null; return false; } /// Checks whether a folder exists. /// The relative or absolute path of the folder we want to check the existence of. /// True if the folder exists, otherwise False. public static bool DirectoryExists(string folderPath) { return DirectoryExists(new ES3Settings(folderPath)); } /// Checks whether a file exists. /// The relative or absolute path of the folder we want to check the existence of. /// The settings we want to use to override the default settings. /// True if the folder exists, otherwise False. public static bool DirectoryExists(string folderPath, ES3Settings settings) { return DirectoryExists(new ES3Settings(folderPath, settings)); } /// Checks whether a folder exists. /// The settings we want to use to override the default settings. /// True if the folder exists, otherwise False. public static bool DirectoryExists(ES3Settings settings) { if (settings.location == Location.File) return ES3IO.DirectoryExists(settings.FullPath); else if (settings.location == Location.PlayerPrefs || settings.location == Location.Cache) throw new System.NotSupportedException("Directories are not supported for the Cache and PlayerPrefs location."); else if (settings.location == Location.Resources) throw new System.NotSupportedException("Checking existence of folder in Resources not supported."); return false; } /// Gets an array of all of the key names in the default file. public static string[] GetKeys() { return GetKeys(new ES3Settings()); } /// Gets an array of all of the key names in a file. /// The relative or absolute path of the file we want to get the key names from. public static string[] GetKeys(string filePath) { return GetKeys(new ES3Settings(filePath)); } /// Gets an array of all of the key names in a file. /// The relative or absolute path of the file we want to get the key names from. /// The settings we want to use to override the default settings. public static string[] GetKeys(string filePath, ES3Settings settings) { return GetKeys(new ES3Settings(filePath, settings)); } /// Gets an array of all of the key names in a file. /// The settings we want to use to override the default settings. public static string[] GetKeys(ES3Settings settings) { if (settings.location == Location.Cache) return ES3File.GetKeys(settings); var keys = new List(); using (var reader = ES3Reader.Create(settings)) { foreach (string key in reader.Properties) { keys.Add(key); reader.Skip(); } } return keys.ToArray(); } /// Gets an array of all of the file names in a directory. public static string[] GetFiles() { return GetFiles(new ES3Settings()); } /// Gets an array of all of the file names in a directory. /// The relative or absolute path of the directory we want to get the file names from. public static string[] GetFiles(string directoryPath) { return GetFiles(new ES3Settings(directoryPath)); } /// Gets an array of all of the file names in a directory. /// The relative or absolute path of the directory we want to get the file names from. /// The settings we want to use to override the default settings. public static string[] GetFiles(string directoryPath, ES3Settings settings) { return GetFiles(new ES3Settings(directoryPath, settings)); } /// Gets an array of all of the file names in a directory. /// The settings we want to use to override the default settings. public static string[] GetFiles(ES3Settings settings) { if (settings.location == Location.Cache) return ES3File.GetFiles(); else if (settings.location != ES3.Location.File) throw new System.NotSupportedException("ES3.GetFiles can only be used when the location is set to File or Cache."); return ES3IO.GetFiles(settings.FullPath, false); } /// Gets an array of all of the sub-directory names in a directory. public static string[] GetDirectories() { return GetDirectories(new ES3Settings()); } /// Gets an array of all of the sub-directory names in a directory. /// The relative or absolute path of the directory we want to get the sub-directory names from. public static string[] GetDirectories(string directoryPath) { return GetDirectories(new ES3Settings(directoryPath)); } /// Gets an array of all of the sub-directory names in a directory. /// The relative or absolute path of the directory we want to get the sub-directory names from. /// The settings we want to use to override the default settings. public static string[] GetDirectories(string directoryPath, ES3Settings settings) { return GetDirectories(new ES3Settings(directoryPath, settings)); } /// Gets an array of all of the sub-directory names in a directory. /// The settings we want to use to override the default settings. 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); } /// Creates a backup of the default file . /// A backup is created by copying the file and giving it a .bak extension. /// If a backup already exists it will be overwritten, so you will need to ensure that the old backup will not be required before calling this method. public static void CreateBackup() { CreateBackup(new ES3Settings()); } /// Creates a backup of a file. /// A backup is created by copying the file and giving it a .bak extension. /// If a backup already exists it will be overwritten, so you will need to ensure that the old backup will not be required before calling this method. /// The relative or absolute path of the file we wish to create a backup of. public static void CreateBackup(string filePath) { CreateBackup(new ES3Settings(filePath)); } /// Creates a backup of a file. /// A backup is created by copying the file and giving it a .bak extension. /// If a backup already exists it will be overwritten, so you will need to ensure that the old backup will not be required before calling this method. /// The relative or absolute path of the file we wish to create a backup of. /// The settings we want to use to override the default settings. public static void CreateBackup(string filePath, ES3Settings settings) { CreateBackup(new ES3Settings(filePath, settings)); } /// Creates a backup of a file. /// A backup is created by copying the file and giving it a .bak extension. /// If a backup already exists it will be overwritten, so you will need to ensure that the old backup will not be required before calling this method. /// The settings we want to use to override the default settings. public static void CreateBackup(ES3Settings settings) { var backupSettings = new ES3Settings(settings.path + ES3IO.backupFileSuffix, settings); ES3.CopyFile(settings, backupSettings); } /// Restores a backup of a file. /// The relative or absolute path of the file we wish to restore the backup of. /// True if a backup was restored, or False if no backup could be found. public static bool RestoreBackup(string filePath) { return RestoreBackup(new ES3Settings(filePath)); } /// Restores a backup of a file. /// The relative or absolute path of the file we wish to restore the backup of. /// The settings we want to use to override the default settings. /// True if a backup was restored, or False if no backup could be found. public static bool RestoreBackup(string filePath, ES3Settings settings) { return RestoreBackup(new ES3Settings(filePath, settings)); } /// Restores a backup of a file. /// The settings we want to use to override the default settings. /// True if a backup was restored, or False if no backup could be found. public static bool RestoreBackup(ES3Settings settings) { var backupSettings = new ES3Settings(settings.path + ES3IO.backupFileSuffix, settings); if (!FileExists(backupSettings)) return false; ES3.RenameFile(backupSettings, settings); return true; } public static DateTime GetTimestamp() { return GetTimestamp(new ES3Settings()); } public static DateTime GetTimestamp(string filePath) { return GetTimestamp(new ES3Settings(filePath)); } public static DateTime GetTimestamp(string filePath, ES3Settings settings) { return GetTimestamp(new ES3Settings(filePath, settings)); } /// Gets the date and time the file was last updated, in the UTC timezone. /// The settings we want to use to override the default settings. /// A DateTime object represeting the UTC date and time the file was last updated. 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 if (settings.location == Location.Cache) return ES3File.GetTimestamp(settings); else return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); } /// Stores the default cached file to persistent storage. /// A backup is created by copying the file and giving it a .bak extension. /// If a backup already exists it will be overwritten, so you will need to ensure that the old backup will not be required before calling this method. public static void StoreCachedFile() { ES3File.Store(); } /// Stores a cached file to persistent storage. /// The filename or path of the file we want to store the cached file to. public static void StoreCachedFile(string filePath) { StoreCachedFile(new ES3Settings(filePath)); } /// Creates a backup of a file. /// The filename or path of the file we want to store the cached file to. /// The settings of the file we want to store to. public static void StoreCachedFile(string filePath, ES3Settings settings) { StoreCachedFile(new ES3Settings(filePath, settings)); } /// Stores a cached file to persistent storage. /// The settings of the file we want to store to. public static void StoreCachedFile(ES3Settings settings) { ES3File.Store(settings); } /// Stores the default cached file to persistent storage. /// A backup is created by copying the file and giving it a .bak extension. /// If a backup already exists it will be overwritten, so you will need to ensure that the old backup will not be required before calling this method. public static void CacheFile() { CacheFile(new ES3Settings()); } /// Stores a cached file to persistent storage. /// The filename or path of the file we want to store the cached file to. public static void CacheFile(string filePath) { CacheFile(new ES3Settings(filePath)); } /// Creates a backup of a file. /// The filename or path of the file we want to store the cached file to. /// The settings of the file we want to store to. public static void CacheFile(string filePath, ES3Settings settings) { CacheFile(new ES3Settings(filePath, settings)); } /// Stores a cached file to persistent storage. /// The settings of the file we want to store to. public static void CacheFile(ES3Settings settings) { ES3File.CacheFile(settings); } /// Initialises Easy Save. This happens automatically when any ES3 methods are called, but is useful if you want to perform initialisation before calling an ES3 method. public static void Init() { var settings = ES3Settings.defaultSettings; ES3TypeMgr.Init(); } #endregion }