Cant save modified files in archive

163 views Asked by At

I'm working on a map editor tool for a game. I can load the contant of a specific *.box archive and load it's contents (files) to listView1 component.

If the file is a "IsReadableFile", then the content of the selected file will be loaded in the richTextBox1 component.

The problem comes when I modify something in a file, save it, and then load it with my program...

So only the saving method is problematic.

If i remove a character in such a file, then save, and load it again, I see a ; after the removed character... If I write a long text in the selected file, than the text continues in another files...

Again, The program is operational, I only have problem with saving...enter image description here enter image description here

namespace MapDataBuilder {

using static Commandlists.ListBoxInitializer;

public partial class Form1 : Form
{
    private List<BoxItem> boxItems = new List<BoxItem>();
    private string directoryPath = "";
    private string boxFilePath = "";
    private ImageList customImageList = new ImageList();

    public Form1()
    {
        InitializeComponent();
        directoryPath = Path.GetDirectoryName(Application.ExecutablePath);
        customImageList.ImageSize = new Size(40, 40);                                                           // Icon size in listView1
        InitializeCustomImageList();
        listView1.SmallImageList = customImageList;
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        MessageBox.Show("The Outforce - MapDataBuilder tool V1.0.\n\nPart of DevTools\nVersion: V1.0\nProgramming by: K.K.\nIconset by: Icons8 com", "Product information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void InitializeCustomImageList()                                                                    //  Add icons to Imagelist...
    {
        customImageList.Images.Add("init.oms", Properties.Resources.icons8_settings_48);                        //  init.oms icon
        customImageList.Images.Add("information.oms", Properties.Resources.icons8_code_48);                     //  information.oms icon
        customImageList.Images.Add("radar.bik", Properties.Resources.icons8_export_file_32);                    //  radar.bik icon
        customImageList.Images.Add("background.bik", Properties.Resources.icons8_export_file_32);               //  background.bik icon
        customImageList.Images.Add("visual.bin", Properties.Resources.icons8_v_32);                             //  visual.bin icon
        customImageList.Images.Add("defaultIcon", Properties.Resources.icons8_file_48);                         //  Default icon
    }

    private void button3_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.Filter = "MapData Box Archives (*.box)|MapData.box";
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                boxFilePath = openFileDialog.FileName;

                directoryPath = Path.GetDirectoryName(boxFilePath);

                try
                {
                    boxItems.Clear();

                    using (BinaryReader reader = new BinaryReader(File.OpenRead(boxFilePath), Encoding.ASCII))
                    {
                        reader.BaseStream.Seek(-4, SeekOrigin.End);
                        uint directoryOffset = reader.ReadUInt32();
                        reader.BaseStream.Seek(directoryOffset, SeekOrigin.Begin);
                        uint numFiles = reader.ReadUInt32();

                        for (int i = 0; i < numFiles; i++)
                        {
                            BoxItem item = new BoxItem();
                            item.Filename = NullTerminator.ReadNullTerminatedString(reader);
                            item.Offset = reader.ReadUInt32();
                            item.Size = reader.ReadUInt32();
                            item.IsReadable = EditorFunctions.IsReadableFile(item.Filename);

                            // Full path to MapData.box
                            item.FullPath = Path.Combine(directoryPath, item.Filename);
                            Console.WriteLine($"Trying to read file: {item.FullPath}");
                            boxItems.Add(item);
                        }
                    }

                    listView1.Items.Clear();

                    foreach (var item in boxItems)
                    {
                        ListViewItem listViewItem = new ListViewItem(item.Filename);
                        string iconKey = Path.GetFileName(item.Filename);
                        iconKey = iconKey.ToLower();

                        if (customImageList.Images.ContainsKey(iconKey))
                        {
                            listViewItem.ImageKey = iconKey;
                        }
                        else
                        {
                            listViewItem.ImageKey = "defaultIcon";
                        }

                        listViewItem.Name = item.Filename;

                        listViewItem.Tag = listViewItem.Name;

                        listView1.Items.Add(listViewItem);
                    }

                    double archiveSizeKB = new FileInfo(boxFilePath).Length / 1024.0;
                    textBox8.Text = $"{archiveSizeKB:F2} KB";

                    MessageBox.Show("*.box archive contents loaded successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error loading *.box archive contents: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }

    private void tabPage4_Click(object sender, EventArgs e)
    {
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            ListViewItem selectedListItem = listView1.SelectedItems[0];
            string selectedFileName = selectedListItem.Text.ToLower();

            int selectedIndex = selectedListItem.Index;
            if (selectedIndex >= 0 && selectedIndex < boxItems.Count)
            {
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(selectedFileName).ToLower();

                switch (fileNameWithoutExtension)
                {
                    case "init":
                        toolStripStatusLabel2.Text = "Your map initialisation file";
                        break;
                    case "information":
                        toolStripStatusLabel2.Text = "Your map configuration file";
                        break;
                    case "radar":
                        toolStripStatusLabel2.Text = "Background image for Radar";
                        break;
                    case "background":
                        toolStripStatusLabel2.Text = "Background image for your map";
                        break;
                    default:
                        toolStripStatusLabel2.Text = $"Your map file: {fileNameWithoutExtension}";
                        break;
                }

                uint fileSize = boxItems[selectedIndex].Size;
                uint fileOffset = boxItems[selectedIndex].Offset;

                textBox9.Text = fileOffset.ToString();

                double fileSizeKB = fileSize / 1024.0;
                textBox10.Text = $"{fileSizeKB:F2} KB";

                if (EditorFunctions.IsReadableFile(selectedFileName))
                {
                    byte[] fileContent = ExtractBytes.FileHelper.ExtractBytesFromBox(boxFilePath, fileOffset, fileSize);

                    if (fileContent != null)
                    {
                        string fileContentString = System.Text.Encoding.ASCII.GetString(fileContent);
                        richTextBox1.Text = fileContentString;

                        textBox11.Text = Path.GetFileName(selectedFileName);
                    }
                    else
                    {
                        richTextBox1.Text = string.Empty;
                        MessageBox.Show($"Error reading file content: file not found: {selectedFileName}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    richTextBox1.Text = "not readable format file";
                }
            }
        }
        else
        {
            textBox9.Text = string.Empty;
            textBox10.Text = string.Empty;
            richTextBox1.Text = string.Empty;
            toolStripStatusLabel2.Text = string.Empty;
        }
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void restartProgramToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Restart();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            try
            {

                ListViewItem selectedListItem = listView1.SelectedItems[0];
                int selectedIndex = selectedListItem.Index;

                if (selectedIndex >= 0 && selectedIndex < boxItems.Count)
                {

                    string newContent = richTextBox1.Text;
                    byte[] newContentBytes = Encoding.ASCII.GetBytes(newContent);

                    BoxItem modifiedItem = boxItems[selectedIndex];
                    modifiedItem.Size = (uint)newContentBytes.Length;
                    boxItems[selectedIndex] = modifiedItem;

                    // Refresh the contents of MapData.box
                    using (FileStream fs = new FileStream(boxFilePath, FileMode.Open, FileAccess.Write))
                    {
                        fs.Seek((int)boxItems[selectedIndex].Offset, SeekOrigin.Begin);
                        fs.Write(newContentBytes, 0, newContentBytes.Length);
                    }

                    MessageBox.Show("Modifications saved!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An unknown error has occured: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show("Choose a file you want to edit and save!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (radioButton1.Checked)
        {
            listView1.View = View.List;
        }

        else
        {
            listView1.View = View.SmallIcon;
        }
    }
}

}

Thank you very much in advance for your helps

0

There are 0 answers