Untitled

 avatar
unknown
plain_text
5 months ago
4.5 kB
6
Indexable
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace MapParser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    lblSelectedFolder.Text = folderDialog.SelectedPath;
                    txtStatus.AppendText($"Selected Folder: {folderDialog.SelectedPath}\n");
                }
            }
        }

        private void btnGenerateDatFile_Click(object sender, EventArgs e)
        {
            string folderPath = lblSelectedFolder.Text;
            if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath))
            {
                MessageBox.Show("Please select a valid folder containing .amf files.");
                return;
            }

            try
            {
                txtStatus.Clear(); // Clear previous messages
                string datFilePath = Path.Combine(folderPath, "node_data.dat");
                txtStatus.AppendText("Dat file generation started...\n");
                GenerateDatFile(folderPath, datFilePath);
                MessageBox.Show("Dat file generated successfully!");
                txtStatus.AppendText("Dat file generation completed successfully.\n");
            }
            catch (Exception ex)
            {
                txtStatus.AppendText($"Error: {ex.Message}\n");
            }
        }

        private void GenerateDatFile(string folderPath, string outputFilePath)
        {
            // Gather all .amf files in the folder
            var amfFiles = Directory.GetFiles(folderPath, "C*.amf", SearchOption.AllDirectories);

            using (FileStream fs = new FileStream(outputFilePath, FileMode.Create))
            using (BinaryWriter writer = new BinaryWriter(fs))
            {
                foreach (var amfFile in amfFiles)
                {
                    try
                    {
                        // Extract coordinates from the .amf file name (assume format "C<col>,<row>.amf")
                        string fileName = Path.GetFileNameWithoutExtension(amfFile);
                        string[] parts = fileName.Substring(1).Split(',');

                        if (parts.Length != 2 || !int.TryParse(parts[0], out int columnId) || !int.TryParse(parts[1], out int rowId))
                        {
                            txtStatus.AppendText($"Warning: Invalid sector ID in file name: {fileName}. Skipping...\n");
                            continue; // Skip files with invalid sector IDs
                        }

                        // Combine columnId and rowId into a single integer sector ID
                        int sectorId = (columnId << 16) | rowId;

                        // Write the sector ID (4 bytes)
                        writer.Write(sectorId);

                        // Read the .amf file and parse node data (1024 bytes expected)
                        byte[] nodeData = new byte[1024];
                        using (FileStream amfStream = new FileStream(amfFile, FileMode.Open, FileAccess.Read))
                        {
                            // Ensure the file has at least 1024 bytes
                            if (amfStream.Length < 1024)
                            {
                                txtStatus.AppendText($"Warning: File {fileName} is too small to contain node data (less than 1024 bytes). Skipping...\n");
                                continue;
                            }

                            // Read exactly 1024 bytes of node data
                            amfStream.Read(nodeData, 0, 1024);
                        }

                        // Write the node data (1024 bytes) to the .dat file
                        writer.Write(nodeData);
                    }
                    catch (Exception ex)
                    {
                        txtStatus.AppendText($"Error processing {amfFile}: {ex.Message}\n");
                    }
                }
            }

            txtStatus.AppendText("Node data file successfully generated in binary format.\n");
        }
    }
}
Editor is loading...
Leave a Comment