public void SetCell <T>(int col, int row, T value) { ES3Debug.Log("Setting cell (" + col + "," + row + ") to value " + value); // If we're writing a string, add it without formatting. if (value.GetType() == typeof(string)) { SetCellString(col, row, (string)(object)value); return; } var settings = new ES3Settings(); using (var ms = new MemoryStream()) { using (var jsonWriter = new ES3JSONWriter(ms, settings, false, false)) jsonWriter.Write(value, ES3.ReferenceMode.ByValue); SetCellString(col, row, settings.encoding.GetString(ms.ToArray())); } // Expand the spreadsheet if necessary. if (col >= cols) { cols = (col + 1); } if (row >= rows) { rows = (row + 1); } }
internal object GetCell(System.Type type, int col, int row) { string value; if (col >= cols || row >= rows) { throw new System.IndexOutOfRangeException("Cell (" + col + ", " + row + ") is out of bounds of spreadsheet (" + cols + ", " + rows + ")."); } if (!cells.TryGetValue(new Index(col, row), out value) || string.IsNullOrEmpty(value)) { ES3Debug.Log("Getting cell (" + col + "," + row + ") is empty, so default value is being returned"); return(null); } // If we're loading a string, simply return the string value. if (type == typeof(string)) { var str = (object)value; ES3Debug.Log("Getting cell (" + col + "," + row + ") with value " + str); return(str); } var settings = new ES3Settings(); using (var ms = new MemoryStream(settings.encoding.GetBytes(value))) { using (var jsonReader = new ES3JSONReader(ms, settings, false)) { var obj = ES3TypeMgr.GetOrCreateES3Type(type, true).Read <object>(jsonReader); ES3Debug.Log("Getting cell (" + col + "," + row + ") with value " + obj); return(obj); } } }
internal virtual void StartWriteProperty(string name) { if (name == null) { throw new ArgumentNullException("Key or field name cannot be NULL when saving data."); } ES3Debug.Log("<b>" + name + "</b> (writing property)", null, serializationDepth); }
protected bool SerializationDepthLimitExceeded() { if (serializationDepth > settings.serializationDepthLimit) { ES3Debug.LogWarning("Serialization depth limit of " + settings.serializationDepthLimit + " has been exceeded, indicating that there may be a circular reference.\nIf this is not a circular reference, you can increase the depth by going to Window > Easy Save 3 > Settings > Advanced Settings > Serialization Depth Limit"); return(true); } return(false); }
public override void ReadInto(ES3Reader reader, object obj) { var collection = (IList)obj; if (collection.Count == 0) { ES3Debug.LogWarning("LoadInto/ReadInto expects a collection containing instances to load data in to, but the collection is empty."); } if (reader.StartReadCollection()) { throw new NullReferenceException("The Collection we are trying to load is stored as null, which is not allowed when using ReadInto methods."); } int itemsLoaded = 0; // Iterate through each item in the collection and try to load it. foreach (var item in collection) { itemsLoaded++; if (!reader.StartReadCollectionItem()) { break; } reader.ReadInto <object>(item, elementType); // If we find a ']', we reached the end of the array. if (reader.EndReadCollectionItem()) { break; } // If there's still items to load, but we've reached the end of the collection we're loading into, throw an error. if (itemsLoaded == collection.Count) { throw new IndexOutOfRangeException("The collection we are loading is longer than the collection provided as a parameter."); } } // If we loaded fewer items than the parameter collection, throw index out of range exception. if (itemsLoaded != collection.Count) { throw new IndexOutOfRangeException("The collection we are loading is shorter than the collection provided as a parameter."); } reader.EndReadCollection(); }
internal static void Store(ES3Settings settings) { ES3File cachedFile; if (!cachedFiles.TryGetValue(settings.path, out cachedFile)) { throw new FileNotFoundException("The file '" + settings.path + "' could not be stored because it could not be found in the cache."); } if (settings.location == ES3.Location.Cache) { settings = (ES3Settings)settings.Clone(); settings.location = ES3.Location.File; ES3Debug.LogWarning("Location is set to 'Cache' when trying to store a cached file, but this should be set to a location in persistent storage (e.g. File, PlayerPrefs). Easy Save will store this cached file to ES3.Location.File."); } cachedFile.Sync(settings); }
public static string PathToEasySaveFolder() { // If the path has not yet been cached, get the path and cache it. if (string.IsNullOrEmpty(pathToEasySaveFolder)) { string[] guids = AssetDatabase.FindAssets("ES3Window"); if (guids.Length == 0) { ES3Debug.LogError("Could not locate the Easy Save 3 folder because the ES3Window script has been moved or removed."); } if (guids.Length > 1) { ES3Debug.LogError("Could not locate the Easy Save 3 folder because more than one ES3Window script exists in the project, but this needs to be unique to locate the folder."); } pathToEasySaveFolder = AssetDatabase.GUIDToAssetPath(guids[0]).Split(new string[] { "Editor" }, System.StringSplitOptions.RemoveEmptyEntries)[0]; } return(pathToEasySaveFolder); }
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); } ES3Debug.Log("Writing cell (" + col + "," + row + ") to file with value " + array[col, row]); writer.Write(Escape(array[col, row])); } } } if (!append) { ES3IO.CommitBackup(settings); } }
internal virtual void StartWriteProperty(string name) { ES3Debug.Log("<b>" + name + "</b> (writing property)", null, serializationDepth); }
public ES3Spreadsheet() { ES3Debug.Log("ES3Spreadsheet created"); }
private void Load(Stream stream, ES3Settings settings) { using (var reader = new StreamReader(stream)) { int c_int; char c; string value = ""; int col = 0; int row = 0; ES3Debug.Log("Reading spreadsheet " + settings.path + " from " + settings.location); // Read until the end of the stream. while (true) { c_int = reader.Read(); c = (char)c_int; if (c == QUOTE_CHAR) { while (true) { c = (char)reader.Read(); if (c == QUOTE_CHAR) { // If this quote isn't escaped by another, it is the last quote, so we should stop parsing this value. if (((char)reader.Peek()) != QUOTE_CHAR) { break; } else { c = (char)reader.Read(); } } value += c; } } // If this is the end of a column, row, or the stream, add the value to the spreadsheet. else if (c == COMMA_CHAR || c == NEWLINE_CHAR || c_int == -1) { SetCell(col, row, value); value = ""; if (c == COMMA_CHAR) { col++; } else if (c == NEWLINE_CHAR) { col = 0; row++; } else { break; } } else { value += c; } } } ES3Debug.Log("Finished reading spreadsheet " + settings.path + " from " + settings.location); }