Ejemplo n.º 1
0
 public void Save()
 {
     using (var stream = new FileStream(this.Path, FileMode.Create))
     {
         var context = new SerializationContext();
         // var policy = SerializationPolicies.Unity;
         // context.Config.SerializationPolicy = policy;
         var writer     = SerializationUtility.CreateWriter(stream, context, DataFormat.JSON);
         var serializer = Serializer.Get <Transform>();
         serializer.WriteValue(this.transform, writer);
         writer.FlushToStream();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Save a binary save to a file path.
        /// </summary>
        public static void Save(string path, BinarySave binary)
        {
            string filelessPath = string.Empty;

            if (path.Contains("/"))
            {
                filelessPath = path.Remove(path.LastIndexOf("/"));
            }
            else
            {
                filelessPath = path.Remove(path.LastIndexOf(@"\"));
            }

            if (!Directory.Exists(filelessPath))
            {
                Directory.CreateDirectory(filelessPath);
            }

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                SerializationUtility.SerializeValue <BinarySave>(binary, SerializationUtility.CreateWriter(fileStream, new SerializationContext(), DataFormat.Binary));
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Sets the save type as being a Binary Encrypted file.
 /// </summary>
 public static void Encrypted(this HUMIO.Data.Persistant persistantData)
 {
     using (var fileStream = new FileStream(persistantData.path, FileMode.Create))
     {
         SerializationUtility.SerializeValue <object>(persistantData.save.value, SerializationUtility.CreateWriter(fileStream, new SerializationContext(), DataFormat.Binary));
     }
 }
Ejemplo n.º 4
0
    void Save(Stream stream, DataFormat format)
    {
        var context = new SerializationContext();

        if (RequireSaveGameAttributeEverywhere)
        {
            context.Config.SerializationPolicy = _serializationPolicy;
        }
        var resolver = new ReferenceResolver();

        context.IndexReferenceResolver  = resolver;
        context.StringReferenceResolver = resolver;
        var writer = SerializationUtility.CreateWriter(stream, context, format);

        var prefabs   = new List <SaveGameObject>();
        var saveables = new List <ISaveable>();

        foreach (GameObject go in gameObject.scene.GetRootGameObjects())
        {
            FindSaveableObjects(go.transform, prefabs, saveables);
        }

#if UNITY_EDITOR
        if (UnitySerializationUtility.ForceEditorModeSerialization)
        {
            // ensure all types are visited for AOT scan
            foreach (var prefab in SaveablePrefabs)
            {
                FindSaveableObjects(prefab.transform, prefabs, saveables);
            }
        }
#endif

        // for each prefab instance store the prefab name and a reference to its parent transform
        var transformSerializer = Serializer.Get <Transform>();
        var prefabsByName       = PrefabsByName();
        writer.BeginStructNode("prefabs", null);
        foreach (SaveGameObject obj in prefabs)
        {
            if (prefabsByName.ContainsKey(obj.PrefabName))
            {
                writer.BeginStructNode(null, null);
                writer.WriteString("prefab", obj.PrefabName);
                transformSerializer.WriteValue("parent", obj.transform.parent, writer);
                resolver.Register(obj.transform);
                writer.EndNode(null);
            }
            else
            {
                Debug.LogErrorFormat("Failed serialising {0} due to missing prefab {1} in SaveablePrefabs list", obj, obj.PrefabName);
            }
        }
        writer.EndNode("prefabs");

        var saveableSerializer = Serializer.Get <MonoBehaviour>();
        writer.BeginStructNode("saveables", null);
        foreach (ISaveable saveable in saveables)
        {
            MonoBehaviour unityObject = saveable as MonoBehaviour;
            writer.BeginStructNode(unityObject.name, unityObject.GetType());
            saveableSerializer.WriteValueWeak("$object", unityObject, writer);
            resolver.Register(unityObject);
            saveable.OnBeforeSave();
            IFormatter formatter = FormatterLocator.GetFormatter(unityObject.GetType(), _serializationPolicy);
            formatter.Serialize(saveable, writer);
            writer.EndNode(unityObject.name);
        }
        writer.EndNode("saveables");
        writer.FlushToStream();
    }
        /// <summary>
        /// Save a binary save to a file path.
        /// </summary>
        internal static void Save(string path, UniversalSave universalSave)
        {
            string filelessPath = string.Empty;

            if (path.Contains("/"))
            {
                filelessPath = path.Remove(path.LastIndexOf("/"));
            }
            else
            {
                filelessPath = path.Remove(path.LastIndexOf(@"\"));
            }

            if (!Directory.Exists(filelessPath))
            {
                Directory.CreateDirectory(filelessPath);
            }

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                SerializationUtility.SerializeValue <Dictionary <string, object> >(universalSave.variables, SerializationUtility.CreateWriter(fileStream, new SerializationContext(), universalSave.dataFormat));
            }
        }