Beispiel #1
0
        /// <summary>
        /// Appends a tree to the stored JSON of trees.
        /// </summary>
        internal static string SaveTree(Save save, bool overwrite)
        {
            lock (_fileLock)
            {
                // Read in the old stored JSON.
                var oldJson = ReadJson();

                // Handle the case where the name is already taken.
                try
                {
                    // Add the save to the old JSON string.
                    var newJson = JsonSaveManager.AddSave(oldJson, save, overwrite);

                    WriteJson(newJson);

                    return string.Empty; // Success.
                }
                catch (Exception exception)
                {
                    if (exception.Message == "exists")
                    {
                        return exception.Message;
                    }

                    throw; // Any other error should still throw.
                }
            }
        }
        /// <summary>
        /// Adds a new save to the JSON string of existing saves if the save name is available.
        /// If overwrite is true, then it will overwrite the existing save instead of throwing an exception.
        /// </summary>
        internal static string AddSave(string oldJson, Save save, bool overwrite)
        {
            // Parse the old saves.
            var oldSaves = ParseSaves(oldJson);

            // Get the save name.
            var name = (string)save.Settings["data"];

            if (overwrite)
            {
                // Select all saves not sharing that save name, essentially deleting the save.
                oldSaves = oldSaves.Where(dictionary => (string)((Dictionary<string, object>)dictionary)["data"] != name).ToList();
            }
            else
            {
                // Check if the save name is taken and throw an exception instead of saving.
                var isNameTaken = oldSaves.Count(dictionary => (string)((Dictionary<string, object>)dictionary)["data"] == name) >= 1;
                if (isNameTaken)
                {
                    throw new Exception("exists");
                }
            }

            // Add the new save.
            oldSaves.Add(save.Settings);

            return SortAndSerializeSaves(oldSaves);
        }
        /// <summary>
        /// Requests a tree's state to be saved.
        /// </summary>
        internal static string SaveTree(Save save, bool overwrite)
        {
            // Prepare the JSON serializer.
            ConfigureFastJson();

            // Request a save.
            return SaveManager.SaveTree(save, overwrite);
        }