Ejemplo n.º 1
0
        /// <summary>
        /// Imports ROM data from a directory.
        /// </summary>
        /// <param name="dirName">The source directory name</param>
        /// <returns>The imported ROM data</returns>
        public static RomData ImportFromDirectory(string dirName)
        {
            if (!Directory.Exists(dirName))
            {
                throw new DirectoryNotFoundException(dirName);
            }
            string projFileName = Path.Combine(dirName, "project.map2agb");

            if (!File.Exists(projFileName))
            {
                throw new FileNotFoundException(projFileName);
            }
            string mapsDirName = Path.Combine(dirName, "maps");

            if (!Directory.Exists(mapsDirName))
            {
                throw new DirectoryNotFoundException(mapsDirName);
            }
            string tilesetsDirName = Path.Combine(dirName, "tilesets");

            if (!Directory.Exists(tilesetsDirName))
            {
                throw new DirectoryNotFoundException(tilesetsDirName);
            }
            RomData data = null;

            using (FileStream input = File.Open(projFileName, FileMode.Open, FileAccess.Read))
            {
                data = (RomData)serializer.ReadObject(input);
                input.Close();
            }
            return(data);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Exports ROM data to a directory.
        /// </summary>
        /// <param name="data">The data to export</param>
        /// <param name="dirName">The target directory. Will be replaced or created.</param>
        public static void ExportToDirectory(RomData data, string dirName)
        {
            string tempDir = Path.Combine(Path.GetTempPath(), "map2agb_save_" + Guid.NewGuid().ToString("D"));

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }
            Directory.CreateDirectory(tempDir);
            string projFileName = Path.Combine(tempDir, "project.map2agb");

            Directory.CreateDirectory(Path.Combine(tempDir, "maps"));
            Directory.CreateDirectory(Path.Combine(tempDir, "tilesets"));
            for (int i = 0; i < data.Banks.Count; i++)
            {
                if (data.Banks[i] == null)
                {
                    continue;
                }
                for (int j = 0; j < data.Banks[i].Count; j++)
                {
                    if (data.Banks[i][j] == null)
                    {
                        continue;
                    }
                    data.Banks[i][j].ExportToFile(Path.Combine((Path.Combine(tempDir, "maps")), i + "_" + j + ".map"));
                    data.Banks[i][j].AbsolutePath = Path.Combine((Path.Combine(dirName, "maps")), i + "_" + j + ".map");
                }
            }
            foreach (KeyValuePair <string, LazyReference <Tileset> > entry in data.Tilesets)
            {
                entry.Value.ExportToFile(Path.Combine((Path.Combine(tempDir, "tilesets")), entry.Key + ".tileset"));
                entry.Value.AbsolutePath = Path.Combine((Path.Combine(dirName, "tilesets")), entry.Key + ".tileset");
            }
            using (FileStream output = File.Open(projFileName, FileMode.Create, FileAccess.Write))
            {
                using (XmlWriter writer = XmlWriter.Create(output, new XmlWriterSettings()
                {
                    Indent = true
                }))
                {
                    serializer.WriteObject(writer, data);
                    writer.Flush();
                    writer.Close();
                }
                output.Flush();
                output.Close();
            }
            if (Directory.Exists(dirName))
            {
                Directory.Delete(dirName, true);
            }
            Directory.Move(tempDir, dirName);
        }