public T Read <T>(string tag) { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); ProcessHeader(ES2Keys.Key._Null, type, null, tag); return((T)Read <T>(type)); }
/* * Gets the ES2Type with a specific 'key' byte. * Returns null if one is not found. */ public static ES2Type GetES2Type(int hash) { if (types == null) { ES2.Init(); } // We use caching of the last type as it is often likely that repeated calls to this method will be made for the same type. if (cachedType != null) { if (cachedType.hash == hash) { return(cachedType); } } // Iterate over array. foreach (KeyValuePair <System.Type, ES2Type> type in types) { if (type.Value.hash == hash) { return(cachedType = type.Value); } } return(null); }
public T[,] Read2DArray <T>(ES2Type type) { if (settings.encrypt) { return(ReadEncrypted2DArray <T>(type)); } int dimensionCount = reader.ReadInt32(); // If array we are loading does not have 2 dimensions, throw error. if (dimensionCount != 2) { Debug.LogError("Easy Save 2 Error: The array you are loading with ES2.Load2DArray must have 2 dimensions, not " + dimensionCount + " dimensions."); } int[] dimensions = new int[] { reader.ReadInt32() + 1, reader.ReadInt32() + 1 }; T[,] result = new T[dimensions[0], dimensions[1]]; for (int i = 0; i < dimensions[0]; i++) { for (int j = 0; j < dimensions[1]; j++) { result[i, j] = (T)Read <T>(type); } } return(result); }
public void ReadList <T>(string tag, List <T> c) where T : class { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); ProcessHeader(ES2Keys.Key._List, type, null, tag); ReadList <T>(type, c); }
public void ReadArray <T>(string tag, T[] c) where T : class { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); ProcessHeader(ES2Keys.Key._NativeArray, type, null, tag); ReadArray <T>(type, c); }
public T[,,] Read3DArray <T>(string tag) { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); ProcessHeader(ES2Keys.Key._NativeArray, type, null, tag); return(Read3DArray <T>(type)); }
public HashSet <T> ReadHashSet <T>(string tag) { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); ProcessHeader(ES2Keys.Key._HashSet, type, null, tag); return(ReadHashSet <T>(type)); }
public Stack <T> ReadStack <T>(string tag) { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); ProcessHeader(ES2Keys.Key._Stack, type, null, tag); return(ReadStack <T>(type)); }
public void Write <T>(T[,,] param, ES2Type type) { if (settings.encrypt) { WriteEncrypted(param, type); return; } // If multidimensional, write length of dimensions. if (param.Rank > 2) { // Write no of dimensions, followed by length of each dimension. writer.Write(param.Rank); for (int i = 0; i < param.Rank; i++) { writer.Write(param.GetUpperBound(i)); } } // Else, just write length. else { writer.Write(param.Length); } // Write each object of array sequentially. foreach (System.Object obj in param) { Write(obj, type); } }
public Dictionary <TKey, TValue> ReadDictionary <TKey, TValue>(string tag) { ES2Type keyType = ES2TypeManager.GetES2Type(typeof(TKey)); ES2Type valueType = ES2TypeManager.GetES2Type(typeof(TValue)); ProcessHeader(ES2Keys.Key._Dictionary, valueType, keyType, tag); return(ReadDictionary <TKey, TValue>(keyType, valueType)); }
public void Write <T>(Stack <T> param, string tag, ES2Type type) { WriteHeader(tag, ES2Keys.Key._Stack, type, null); Write <T>(param, type); WriteTerminator(); WriteLength(); tagsToDelete.Add(tag); }
public void Write <TKey, TValue>(Dictionary <TKey, TValue> param, string tag, ES2Type keyType, ES2Type valueType) { WriteHeader(tag, ES2Keys.Key._Dictionary, valueType, keyType); Write <TKey, TValue>(param, keyType, valueType); WriteTerminator(); WriteLength(); tagsToDelete.Add(tag); }
public void Write <T>(T[,,] param, string tag, ES2Type type) { WriteHeader(tag, ES2Keys.Key._NativeArray, type, null); Write <T>(param, type); WriteTerminator(); WriteLength(); tagsToDelete.Add(tag); }
public static Key KeyFromES2Type(ES2Type type) { if (type != null) { return((Key)type.key); } return(Key._Null); }
private void ReadComponent(ES2AutoSave autoSave, ES2Reader reader) { string componentID = reader.reader.ReadString(); int endPosition = (int)reader.stream.Position; int length = reader.reader.ReadInt32(); endPosition += length; // Read collection type byte which is not relevant to Components. reader.reader.ReadByte(); int typeHash = reader.reader.ReadInt32(); ES2Type type = ES2TypeManager.GetES2Type(typeHash); // If there's no ES2Type for this type of Component, skip it. if (type == null) { reader.stream.Position = endPosition; return; } // Get or create the Component. Component c; ES2AutoSaveComponentInfo componentInfo = autoSave.GetComponentInfo(componentID); if (componentInfo == null || componentInfo.component == null) { // If no Component info, look for the Component, or otherwise, add one. if (!(c = autoSave.gameObject.GetComponent(type.type))) { c = autoSave.gameObject.AddComponent(type.type); } } else { c = componentInfo.component; } string dataType = reader.reader.ReadString(); if (dataType == "data") { reader.Read <System.Object>(type, c); return; } else if (dataType == "vars") // Else we're reading a series of variables denoted by "vars". { while (reader.stream.Position != endPosition) { ReadVariableRecursive(autoSave, componentInfo, reader, c); } } else { reader.stream.Position = endPosition; return; } }
/* END ES2BinaryWriter functionality */ #region User-Exposed Write Methods (With Tag) // These are user-exposed methods. public void Write <T>(T param, string tag) { ES2Type type = ES2TypeManager.GetES2Type(param.GetType()); WriteHeader(tag, ES2Keys.Key._Null, type, null); Write <T>(param, type); WriteTerminator(); WriteLength(); tagsToDelete.Add(tag); }
private void WriteEncryptedIDictionary(IDictionary param, ES2Type keyType, ES2Type valueType) { using (ES2Writer encryptedWriter = CreateEncryptedWriter()) { encryptedWriter.WriteIDictionary(param, keyType, valueType); byte[] encryptedBytes = encryptedWriter.GetEncryptedBytes(settings.encryptionPassword); writer.Write(encryptedBytes.Length); writer.Write(encryptedBytes); } }
private void WriteEncryptedICollection(ICollection param, ES2Type type) { using (ES2Writer encryptedWriter = CreateEncryptedWriter()) { encryptedWriter.WriteICollection(param, type); byte[] encryptedBytes = encryptedWriter.GetEncryptedBytes(settings.encryptionPassword); writer.Write(encryptedBytes.Length); writer.Write(encryptedBytes); } }
private void WriteEncryptedSystemArray(System.Array param, ES2Type type) { using (ES2Writer encryptedWriter = CreateEncryptedWriter()) { encryptedWriter.WriteSystemArray(param, type); byte[] encryptedBytes = encryptedWriter.GetEncryptedBytes(settings.encryptionPassword); writer.Write(encryptedBytes.Length); writer.Write(encryptedBytes); } }
public override object Read(ES2Reader reader) { int hash = reader.Read <int>(); ES2Type es2Type = ES2TypeManager.GetES2Type(hash); if (es2Type == null) { Debug.LogError("Cannot load Object of this type as it is not a supported type"); return(null); } return(reader.Read <object>(es2Type)); }
public override void Write(object data, ES2Writer writer) { var dataType = data.GetType(); ES2Type es2Type = ES2TypeManager.GetES2Type(dataType); if (es2Type == null) { Debug.LogError("Cannot save Object of type " + dataType + " as it is not a supported type"); return; } writer.Write(es2Type.hash); writer.Write(data, es2Type); }
private void SaveCurrentTag(ES2Header header) { try { ES2Type valueType = ES2TypeManager.GetES2Type(header.valueType); ES2Type keyType = ES2TypeManager.GetES2Type(header.keyType); ES2Settings settings = new ES2Settings(currentFilePath); settings.encrypt = header.settings.encrypt; settings.encryptionType = header.settings.encryptionType; settings.encryptionPassword = header.settings.encryptionPassword; using (ES2Writer writer = new ES2Writer(settings)) { if (header.collectionType == ES2Keys.Key._Null) { writer.Write(currentValue, currentTag, valueType); } else if (header.collectionType == ES2Keys.Key._NativeArray) { writer.Write(currentValue as object[], currentTag, valueType); } else if (header.collectionType == ES2Keys.Key._Dictionary) { writer.Write(currentValue as Dictionary <object, object>, currentTag, keyType, valueType); } else if (header.collectionType == ES2Keys.Key._List) { writer.Write(currentValue as List <object>, currentTag, valueType); } else if (header.collectionType == ES2Keys.Key._Queue) { writer.Write(currentValue as Queue <object>, currentTag, valueType); } else if (header.collectionType == ES2Keys.Key._Stack) { writer.Write(currentValue as Stack <object>, currentTag, valueType); } else if (header.collectionType == ES2Keys.Key._HashSet) { writer.Write(currentValue as HashSet <object>, currentTag, valueType); } writer.Save(); } } catch (Exception e) { EditorUtility.DisplayDialog("Could not save file", "An error was thrown when trying to save to this file. See below for details.\n\n" + "Details: " + e.Message, "Ok"); } }
public void Write <T>(Stack <T> param, string tag) { ES2Type type = ES2TypeManager.GetES2Type(typeof(T)); if (type == null) { Debug.LogError(typeof(T).ToString() + " is not currently supported or it's ES2Type was not found, but you may be able to add support for it through Assets > Easy Save 2 > Manage Types"); } WriteHeader(tag, ES2Keys.Key._Stack, type, null); Write <T>(param, type); WriteTerminator(); WriteLength(); tagsToDelete.Add(tag); }
protected void ProcessHeader(ES2Keys.Key expectedCollectionType, ES2Type expectedValue, ES2Type expectedKey, string tag) { if (ScanToTag(tag) == false) { Debug.LogError("Easy Save 2 Error: The data, tag, file or folder you are looking for does not exist. Please ensure that ES2.Exists(string identifier) returns true before calling this method."); } ES2Header header = ReadHeader(); /* Check that we're loading the correct types */ if (expectedValue == null) { Debug.LogError("This type is not supported by Easy Save, but you may be able to add support by going to 'Manage Types' in the 'Assets/Easy Save 2' menu."); } if (expectedCollectionType != header.collectionType) { // If the user is trying to load a non-collection with a collection method ... if (expectedCollectionType == ES2Keys.Key._Null) { Debug.LogError("Easy Save 2 Error: The data you are trying to load is a Collection. Please use the Load method for that type of collection (for example, ES2.LoadArray or ES2.LoadDictionary)"); } else { Debug.LogError("Easy Save 2 Error: The data you are trying to load is not a Collection, but you are using a Collection method to use it. Use ES2.Load instead."); } } if (expectedKey != null) { if (header.keyType != expectedKey.hash) { Debug.LogError("Easy Save 2 Error: The type of key in the Dictionary you are loading does not match the key you are trying to load with."); } } if (expectedValue != null) { if (header.valueType != expectedValue.hash) { Debug.LogError("Easy Save 2 Error: The data you are trying to load does not match the Load method you are using to load it. "); } } if (header.settings.encrypt) { this.settings.encrypt = true; } }
public override void Read(ES2Reader reader, object obj) { int hash = reader.Read <int>(); ES2Type es2Type = ES2TypeManager.GetES2Type(hash); if (es2Type == null) { Debug.LogError("Textures of type " + type.ToString() + " are not currently supported."); } else { reader.Read <object>(es2Type, obj); } }
public override void Write(object data, ES2Writer writer) { System.Type type = data.GetType(); ES2Type es2Type = ES2TypeManager.GetES2Type(type); if (es2Type == null) { Debug.LogError("Textures of type " + type.ToString() + " are not currently supported."); } else { writer.Write(es2Type.hash); writer.Write(data, es2Type); } }
public void WriteICollection(ICollection param, ES2Type type) { if (settings.encrypt) { WriteEncryptedICollection(param, type); return; } writer.Write((byte)0); // TODO : Remove this line when next changing the save format. writer.Write(param.Count); foreach (System.Object obj in param) { Write(obj, type); } }
public void ReadArray <T>(ES2Type type, T[] c) where T : class { if (settings.encrypt) { ReadEncryptedArray <T>(type, c); return; } int count = reader.ReadInt32(); for (int i = 0; i < count; i++) { Read <T>(type, c[i]); } }
public static void AddType(ES2Type type) { if (ES2TypeManager.types == null) { ES2TypeManager.types = new Dictionary <Type, ES2Type>(); } ES2TypeManager.types[type.type] = type; if (_Types.Where(t => t.type.FullName == type.type.FullName).Count() > 0) { return; } _Types.Add(type); }
public void ReadList <T>(ES2Type type, List <T> c) where T : class { if (settings.encrypt) { ReadEncryptedList <T>(type, c); return; } reader.ReadByte(); // TODO: Remove this line on the next change to ES2 format. int count = reader.ReadInt32(); for (int i = 0; i < count; i++) { Read <T>(type, c[i]); } }