/// <summary>
        /// Load a binary save from a given path.
        /// </summary>
        /// <param name="path"></param>
        internal static UniversalSave Load(string path, DataFormat dataFormat)
        {
            if (File.Exists(path))
            {
                Dictionary <string, object> dictionary = null;
                UniversalSave universalSave            = null;

                using (var fileStream = new FileStream(path, FileMode.Open))
                {
                    dictionary = SerializationUtility.DeserializeValue <Dictionary <string, object> >(SerializationUtility.CreateReader(fileStream, new DeserializationContext(), dataFormat));
                }

                if (dictionary != null)
                {
                    return(new UniversalSave()
                    {
                        variables = dictionary
                    });
                }

                using (var fileStream = new FileStream(path, FileMode.Open))
                {
                    universalSave = SerializationUtility.DeserializeValue <UniversalSave>(SerializationUtility.CreateReader(fileStream, new DeserializationContext(), dataFormat));
                }

                if (universalSave != null)
                {
                    return(universalSave);
                }
            }

            return(null);
        }
        private ControlOutput SaveUniversal(Flow flow)
        {
            var binary = promoteToInputPort == true?flow.GetValue <UniversalSave>(binarySave) : new UniversalSave()
            {
                dataFormat = format
            };
            var loadedSave = append ? UniversalSave.Load(GetPath(flow), format) : null;

            if (!promoteToInputPort)
            {
                for (int i = 0; i < values.Count; i++)
                {
                    if (!string.IsNullOrEmpty(values[i].key))
                    {
                        binary.Set(flow.GetValue <string>(names[i]), flow.GetValue <object>(values[i]));
                    }
                }
            }

            if (loadedSave != null)
            {
                if (!promoteToInputPort)
                {
                    for (int i = 0; i < binary.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(values[i].key))
                        {
                            loadedSave.Set(flow.GetValue <string>(names[i]), flow.GetValue <object>(values[i]));
                        }
                    }
                }
                else
                {
                    var saveKeys   = binary.variables.Keys.ToArrayPooled();
                    var saveValues = binary.variables.Values.ToArrayPooled();

                    for (int i = 0; i < binary.Count; i++)
                    {
                        loadedSave.Set(saveKeys[i], saveValues[i]);
                    }
                }

                Save(flow, loadedSave);

                lastSave = loadedSave;
            }
            else
            {
                Save(flow, binary);
                lastSave = binary;
            }


            return(complete);
        }
        private UniversalSave GetUniversalOutput(Flow flow)
        {
            var binary = new UniversalSave();

            binary.dataFormat = format;

            for (int i = 0; i < count; i++)
            {
                binary.variables.Add(flow.GetValue <string>(names[i]), flow.GetValue <object>(values[i]));
            }

            return(binary);
        }
Exemple #4
0
        protected override void Definition()
        {
            if (!usePersistantDataPath)
            {
                path = ValueInput <string>("path", string.Empty);
            }
            fileName = ValueInput <string>(nameof(fileName), string.Empty);

            complete = ControlOutput("complete");
            delete   = ControlInput("delete", (flow) => {
                UniversalSave.Delete((usePersistantDataPath) ? Application.persistentDataPath + "/" + flow.GetValue <string>(fileName) : flow.GetValue <string>(path) + "/" + flow.GetValue <string>(fileName));
                return(complete);
            });

            Requirement(fileName, delete);
            Succession(delete, complete);
        }
        protected override void Definition()
        {
            if (!usePersistantDataPath)
            {
                path = ValueInput <string>("path", string.Empty);
            }
            fileName = ValueInput <string>(nameof(fileName), string.Empty);
            binary   = ValueOutput <UniversalSave>(nameof(binary));

            complete = ControlOutput("complete");
            load     = ControlInput("load", (flow) => {
                flow.SetValue(binary, UniversalSave.Load((usePersistantDataPath) ? Application.persistentDataPath + "/" + flow.GetValue <string>(fileName) : flow.GetValue <string>(path) + "/" + flow.GetValue <string>(fileName), format));
                return(complete);
            });

            Requirement(fileName, binary);
            Requirement(fileName, load);
            Succession(load, complete);
        }
        /// <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));
            }
        }
 private void Save(Flow flow, UniversalSave binary)
 {
     UniversalSave.Save(GetPath(flow), binary);
 }
 public static byte[] GetBytes(UniversalSave universalSave)
 {
     return(SerializationUtility.SerializeValue <UniversalSave>(universalSave, universalSave.dataFormat));
 }