RicherSims
Resource contributor
- Messages
- 560
- Country
-
I'm coding a tool to perform a batch edit of the Friendly Names in the MDLN section of MDL files. (Becasue sometimes I have files with different file names but identical friendly names, and I need to batch process them in ModelConverterX, but MCX will overwrite previous files as they all have the same friendly name).
I've mostly worked with text files before and I'm teaching myself to work with binary files for the first time.
I understand from reading the MDL File Format documentation that the files are split into sections each with 3 parts: A four letter section name, an integer representing the length of the content, and the content itself.
I've managed to find the MDLN section and can successfully edit the integer representing the length of the friendly name and edit the friendly name itself.
My problem arises when I try to read the rest of the file after the friendly name and copy it downwards (as you can't really insert in a binary file).
Despite my best effors to read and write the data exactly where it previously occurred after the MDLN section, on re-opening the file in MCX, it reports the following error:
MDLXReader Error Invalid section length for section MDLD
However, comparing the edited file to the original in a Hex or Text Editor the ONLY change I can see is the changed friendly name, and the changed integer representing the new length of the friendly name.
The MDLD section should not have changed at all. The rest of the bytes are unchanged. Simply shifted downwards.
I am stumped.
Here is the code I use in the relevant section (yes I code in Pascal).
Attached are 2 files:
The edited one has a friendly name changed from "1" to "1_09".
What am I missing? Any guidance on this would be greatly appreciated.
I've mostly worked with text files before and I'm teaching myself to work with binary files for the first time.
I understand from reading the MDL File Format documentation that the files are split into sections each with 3 parts: A four letter section name, an integer representing the length of the content, and the content itself.
I've managed to find the MDLN section and can successfully edit the integer representing the length of the friendly name and edit the friendly name itself.
My problem arises when I try to read the rest of the file after the friendly name and copy it downwards (as you can't really insert in a binary file).
Despite my best effors to read and write the data exactly where it previously occurred after the MDLN section, on re-opening the file in MCX, it reports the following error:
MDLXReader Error Invalid section length for section MDLD
However, comparing the edited file to the original in a Hex or Text Editor the ONLY change I can see is the changed friendly name, and the changed integer representing the new length of the friendly name.
The MDLD section should not have changed at all. The rest of the bytes are unchanged. Simply shifted downwards.
I am stumped.
Here is the code I use in the relevant section (yes I code in Pascal).
Code:
//integer value size of NewName
datalength := length(NewName);
//difference between space left for old name and space required for new name
InsertLength := datalength-aSection.size; //asection.size = length of old friendly name
//write new data length
FS.write(datalength, sizeof(datalength));
//Save current position in binary file to come back to later
InsertPosition := FS.Position;
{We will need to read all characters from current position + length of oldname (i.e. section.size) to EOF and copy them downwards "Insert" spaces to make space for NewName length}
//We are now at the beginning of the Section Length integer for MDLN. Seek forward to the next section(i.e PARA)
FS.Seek(aSection.size, soFromCurrent);
//prepare array of bytes for rest of file
data := nil;
SetLength(data, FS.Size);
For idx := 1 to length(data) do
data[idx] := 0;
//read all characters from current position to EOF and save how many bytes we've read in datalength
datalength := FS.Read(data, FS.size);
//seek back to insertion point which is beginning of friendly name in MDLN section
FS.Position := InsertPosition;
//convert NewName to character bytes and write
for idx := 1 to length(NewName) do
begin
b := Byte(NewName[idx]);
FS.Write(b,sizeof(b));
end;
//append rest of file
FS.Write(data, datalength);
Attached are 2 files:
The edited one has a friendly name changed from "1" to "1_09".
What am I missing? Any guidance on this would be greatly appreciated.