コード例 #1
0
        /// <summary>
        /// Saves metadata relating to the level saved
        /// </summary>
        /// <param name="editorGrid">editorGrid to save metadata about</param>
        /// <param name="filePath">File path to save metadata to</param>
        /// /// <param name="levelName">Level Name to save</param>
        private void SaveGridMetadata(EditorGrid editorGrid, string filePath, string levelName)
        {
            //Create a metadata object, for data storage
            LevelMetadata levelMetadata = new LevelMetadata
            {
                levelName        = levelName,
                levelSaved       = DateTime.Now,
                levelGridWidth   = editorGrid.GridHeight,
                levelGridHeight  = editorGrid.GridHeight,
                levelGridBoxSize = editorGrid.GridBoxSize
            };

            //Serialize as JSON
            string jsonMetadata = JsonConvert.SerializeObject(levelMetadata, Formatting.Indented);

            //Save File
            File.WriteAllText(filePath, jsonMetadata);
        }
コード例 #2
0
        /// <summary>
        /// Loads the metadata of a level
        /// </summary>
        /// <param name="filePath">File Path of the metadata</param>
        /// <returns>Loaded Metadata</returns>
        private LevelMetadata LoadGridMetadata(string filePath)
        {
            //Check that metadata file path exists
            if (File.Exists(filePath))
            {
                //Deserialise JSON file to metadata object
                using (StreamReader jsonFile = File.OpenText(filePath))
                {
                    JsonSerializer serializer     = new JsonSerializer();
                    LevelMetadata  loadedMetadata = (LevelMetadata)serializer.Deserialize(jsonFile, typeof(LevelMetadata));

                    jsonFile.Close();

                    //Return the got metadata object
                    return(loadedMetadata);
                }
            }
            else
            {
                throw new FileNotFoundException();
            }
        }