public ES2Header(ES2Keys.Key collectionType, int keyType, int valueType, ES2Settings settings) { this.collectionType = collectionType; this.keyType = keyType; this.valueType = valueType; this.settings = settings; }
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 void WriteHeader(string tag, ES2Keys.Key collectionType, ES2Type valueType, ES2Type keyType) { if (valueType == null) { Debug.LogError("Easy Save does not support saving of this type. For more information on supported types, and how to add support for your own types, please go to http://docs.moodkie.com/easy-save-2/supported-types/"); return; } writer.Write((byte)ES2Keys.Key._Tag); writer.Write(tag); lengthPosition = writer.BaseStream.Position; writer.Write((int)0); // Write blank length so we can store it later. if (settings.encrypt) { if (settings.encryptionType == ES2Settings.EncryptionType.AES128) { writer.Write((byte)ES2Keys.Key._Encrypt); } else { writer.Write((byte)ES2Keys.Key._Obfuscate); } } // Write type data. if (collectionType != ES2Keys.Key._Null) { writer.Write((byte)collectionType); } writer.Write((byte)255); // We write the 255 byte to say that this uses a hash integer to identify types rather than an ES2Key. writer.Write(valueType.hash); if (keyType != null) { writer.Write(keyType.hash); } }
/* * Gets the ES2Type with a specific ES2Key. * Returns null if one is not found. */ public static ES2Type GetES2Type(ES2Keys.Key key) { return(GetES2Type((byte)key)); }
public ES2Header ReadHeader() { ES2Keys.Key collectionType = ES2Keys.Key._Null; int keyType = 0; int valueType = 0; ES2Settings settings = new ES2Settings(); while (true) { byte currentByte = reader.ReadByte(); if (currentByte == (byte)ES2Keys.Key._Encrypt) // If encrypt, set encrypted in settings. { settings.encrypt = true; settings.encryptionType = ES2Settings.EncryptionType.AES128; } else if (currentByte == (byte)ES2Keys.Key._Obfuscate) // If encrypt, set encrypted in settings. { settings.encrypt = true; settings.encryptionType = ES2Settings.EncryptionType.Obfuscate; } else if (currentByte == (byte)ES2Keys.Key._Terminator) { continue; } else if ((int)currentByte == 255) // We're loading data after v2.54 where types are identified by integer hashes. { if (collectionType == ES2Keys.Key._Dictionary) // keyType comes second if we're loading a Dictionary. { keyType = reader.ReadInt32(); } else { valueType = reader.ReadInt32(); } return(new ES2Header(collectionType, keyType, valueType, settings)); } else if ((int)currentByte < 81) // LEGACY SAVE DATA before v2.54: If data type, set data type in settings. End of header. { if (collectionType == ES2Keys.Key._Dictionary) // keyType comes second if we're loading a Dictionary. { keyType = ES2TypeManager.GetES2Type(currentByte).hash; } else { valueType = ES2TypeManager.GetES2Type(currentByte).hash; } return(new ES2Header(collectionType, keyType, valueType, settings)); } else if ((int)currentByte < 101) //Array Type { collectionType = (ES2Keys.Key)currentByte; if (currentByte == (byte)ES2Keys.Key._Dictionary) //If it's a Dictionary, the value byte comes first. { byte valueByte = reader.ReadByte(); if ((int)valueByte == 255) // Handle value bytes after v2.54. { valueType = reader.ReadInt32(); keyType = reader.ReadInt32(); return(new ES2Header(collectionType, keyType, valueType, settings)); } else // Handle value bytes before v2.54. { valueType = ES2TypeManager.GetES2Type(valueByte).hash; } } } else { throw new ES2InvalidDataException(); } } }
protected void ProcessHeader(ES2Keys.Key expectedCollectionType, ES2Type expectedValue, ES2Type expectedKey) { ProcessHeader(expectedCollectionType, expectedValue, expectedKey, settings.filenameData.tag); }
public void WriteHeader(ES2Keys.Key collectionType, ES2Type valueType, ES2Type keyType) { WriteHeader(settings.filenameData.tag, collectionType, valueType, keyType); }
private void ReadVariableRecursive(ES2AutoSave autoSave, ES2AutoSaveVariableInfo variable, ES2Reader reader, object obj) { string variableID = reader.reader.ReadString(); int endPosition = (int)reader.stream.Position; int length = reader.reader.ReadInt32(); endPosition += length; // Read Type data ES2Keys.Key collectionType = (ES2Keys.Key)reader.reader.ReadByte(); int typeHash = reader.reader.ReadInt32(); int dictValueHash = 0; if (collectionType == ES2Keys.Key._Dictionary) { dictValueHash = reader.reader.ReadInt32(); } ES2AutoSaveVariableInfo info = autoSave.GetCachedVariableInfo(variableID); if (info == null) { reader.stream.Position = endPosition; return; } string dataType = reader.reader.ReadString(); if (dataType == "data") { // Get ES2Types. ES2Type type = ES2TypeManager.GetES2Type(typeHash); ES2Type dictValueType = null; if (collectionType == ES2Keys.Key._Dictionary) { dictValueType = ES2TypeManager.GetES2Type(dictValueHash); } // If the type of data we're loading isn't supported, skip it. if (type == null || (collectionType == ES2Keys.Key._Dictionary && dictValueType == null)) { reader.stream.Position = endPosition; return; } bool valueWasSet = false; if (collectionType == ES2Keys.Key._Null) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.Read <System.Object>(type), info.isProperty); } else if (collectionType == ES2Keys.Key._NativeArray) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.ReadSystemArray(type), info.isProperty); } else if (collectionType == ES2Keys.Key._List) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.ReadICollection(typeof(List <>), type), info.isProperty); } else if (collectionType == ES2Keys.Key._Queue) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.ReadICollection(typeof(Queue <>), type), info.isProperty); } else if (collectionType == ES2Keys.Key._Stack) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.ReadICollection(typeof(Stack <>), type), info.isProperty); } else if (collectionType == ES2Keys.Key._HashSet) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.ReadICollection(typeof(HashSet <>), type), info.isProperty); } else if (collectionType == ES2Keys.Key._Dictionary) { valueWasSet = ES2Reflection.SetValue(obj, info.name, reader.ReadIDictionary(typeof(Dictionary <,>), type, dictValueType), info.isProperty); } if (!valueWasSet) { reader.stream.Position = endPosition; return; } } // Else, we have variables to load. else if (dataType == "vars") { object thisObj = ES2Reflection.GetValue(obj, info.name, info.isProperty); if (thisObj == null) { reader.stream.Position = endPosition; } ReadVariableRecursive(autoSave, info, reader, thisObj); } else { reader.stream.Position = endPosition; return; } }
private void WriteVariableRecursive(ES2AutoSave autoSave, ES2AutoSaveVariableInfo variable, ES2Writer writer, object obj) { // Get the Type data for this variable, or return if a type is not supported. ES2Type es2Type = null; ES2Type dictValueType = null; ES2Keys.Key collectionType = ES2Keys.GetCollectionType(variable.type); // If it's not a collection type, just get the basic ES2Type. if (collectionType == ES2Keys.Key._Null) { es2Type = ES2TypeManager.GetES2Type(variable.type); } // Otherwise, get the collection element types. else { if (collectionType == ES2Keys.Key._NativeArray) // Get Array Element Type. { es2Type = ES2TypeManager.GetES2Type(variable.type.GetElementType()); } else { // Get ES2Types for generic type arguments. Type[] genericArgs = ES2Reflection.GetGenericArguments(variable.type); if (genericArgs.Length > 0) { es2Type = ES2TypeManager.GetES2Type(genericArgs[0]); if (genericArgs.Length > 1) { dictValueType = ES2TypeManager.GetES2Type(genericArgs[1]); if (dictValueType == null) { return; } } } } } // Skip if unsupported. Enums are not supported unless they have an explicit ES2Type. if (es2Type == null || es2Type.GetType() == typeof(ES2_Enum)) { return; } writer.writer.Write(variable.id); int startPosition = writer.WritePropertyPrefix(); // Write the Type data for this variable. writer.writer.Write((byte)collectionType); writer.writer.Write(es2Type.hash); // If Dictionary, also write the Dictionary value type. if (dictValueType != null) { writer.writer.Write(dictValueType.hash); } int variableCount = 0; foreach (string variableID in variable.variableIDs) { ES2AutoSaveVariableInfo variableInfo = autoSave.GetCachedVariableInfo(variableID); if (variableInfo.buttonSelected) { variableCount++; if (variableCount == 1) { writer.writer.Write("vars"); } object variableObj = ES2Reflection.GetValue(obj, variableInfo.name, variableInfo.isProperty); WriteVariableRecursive(autoSave, variableInfo, writer, variableObj); } } // If no variables were written, we want to save this variable. if (variableCount == 0) { writer.writer.Write("data"); if (collectionType == ES2Keys.Key._Null) { writer.Write(obj, es2Type); } else if (collectionType == ES2Keys.Key._NativeArray) { writer.WriteSystemArray((Array)obj, es2Type); } else if (collectionType == ES2Keys.Key._List) { writer.WriteICollection((ICollection)obj, es2Type); } else if (collectionType == ES2Keys.Key._Queue) { writer.WriteICollection((ICollection)obj, es2Type); } else if (collectionType == ES2Keys.Key._Stack) { writer.WriteICollection((ICollection)obj, es2Type); } else if (collectionType == ES2Keys.Key._HashSet) { writer.WriteICollection((ICollection)obj, es2Type); } else if (collectionType == ES2Keys.Key._Dictionary) { writer.WriteIDictionary((IDictionary)obj, es2Type, dictValueType); } } writer.WritePropertyLength(startPosition); }