/// <summary> /// Same as with gameObjects in SaveLoader, we lookup originals by guid in Storage, and /// overwrite those. /// </summary> /// <param name="data"></param> /// <param name="storageType"></param> /// <returns></returns> public override object CreateInstance(fsData data, Type storageType) { if (!data.IsDictionary) { UnityEngine.Debug.LogError("SaveSystem: tried to deserialize scriptable, which data layout was not a dictionary."); return(0); } Dictionary <string, fsData> keyValuePairs = data.AsDictionary; fsData guidData; keyValuePairs.TryGetValue("_guid", out guidData); if (guidData == null || !guidData.IsString) { UnityEngine.Debug.LogError("SaveSystem: could not scriptable in database using guid."); return(0); } string guid = guidData.AsString; SavedScriptable so = SaveLoader.System.FindScriptable(guid); if (so == null) { UnityEngine.Debug.LogError("SaveSystem: scriptable object with such guid was not found."); return(0); } return(ScriptableObject.Instantiate(so)); }
/// <summary> /// Attempts to deserialize a value from a serialized state. /// </summary> public fsResult TryDeserialize(fsData data, Type storageType, Type overrideConverterType, ref object result) { if (data.IsNull) { result = null; var processors = GetProcessors(storageType); Invoke_OnBeforeDeserialize(processors, storageType, ref data); Invoke_OnAfterDeserialize(processors, storageType, null); return(fsResult.Success); } // Convert legacy data into modern style data ConvertLegacyData(ref data); try { // We wrap the entire deserialize call in a reference group so // that we can properly deserialize a "parallel" set of // references, ie, a list of objects that are cyclic w.r.t. the // list _references.Enter(); List <fsObjectProcessor> processors; var r = InternalDeserialize_1_CycleReference(overrideConverterType, data, storageType, ref result, out processors); if (r.Succeeded) { Invoke_OnAfterDeserialize(processors, storageType, result); if (result != null) { if (typeof(SavedComponent).IsAssignableFrom(storageType)) { SavedComponent component = result as SavedComponent; component.OnAfterLoad(); } else if (typeof(SavedScriptable).IsAssignableFrom(storageType)) { SavedScriptable scriptable = result as SavedScriptable; scriptable.OnAfterLoad(); } } } return(r); } finally { _references.Exit(); } }
/// <summary> /// Serialize the given value. /// </summary> /// <param name="storageType"> /// The type of field/property that stores the object instance. This is /// important particularly for inheritance, as a field storing an /// IInterface instance should have type information included. /// </param> /// <param name="overrideConverterType"> /// An fsBaseConverter derived type that will be used to serialize the /// object instead of the converter found via the normal discovery /// mechanisms. /// </param> /// <param name="instance"> /// The actual object instance to serialize. /// </param> /// <param name="data">The serialized state of the object.</param> /// <returns>If serialization was successful.</returns> public fsResult TrySerialize(Type storageType, Type overrideConverterType, object instance, out fsData data) { var processors = GetProcessors(instance == null ? storageType : instance.GetType()); Invoke_OnBeforeSerialize(processors, storageType, instance); if (instance != null) { if (typeof(SavedComponent).IsAssignableFrom(storageType)) { SavedComponent component = instance as SavedComponent; component.OnBeforeSave(); } else if (typeof(SavedScriptable).IsAssignableFrom(storageType)) { SavedScriptable scriptable = instance as SavedScriptable; scriptable.OnBeforeSave(); } } // We always serialize null directly as null if (ReferenceEquals(instance, null)) { data = new fsData(); Invoke_OnAfterSerialize(processors, storageType, instance, ref data); return(fsResult.Success); } var result = InternalSerialize_1_ProcessCycles(storageType, overrideConverterType, instance, out data); Invoke_OnAfterSerialize(processors, storageType, instance, ref data); return(result); }