Forum > Miscellaneous

[QUESTION] [C# Dev] Parsing MTEX

<< < (2/3) > >>

schlumpf:

--- Quote from: "Keta" ---So let me get this straight, you want me to add a

--- Code: ---[StructLayout(LayoutKind.Explicit)]
        public struct MTEX
        {
            [FieldOffset(0)]
            public int magic;
            [FieldOffset(4)]
            public int size;
            [FieldOffset(8)]
            public int mtex;
        }
--- End code ---
correct?
--- End quote ---
No.


--- Quote ---And then from the main function, call it like I did with the other two?

--- Code: ---MTEX mtex = ByteToType<MTEX>(foo);
--- End code ---

--- End quote ---
Yes.


--- Quote ---Yes? Well, that gives me the following number: 1740
--- End quote ---
No idea which number you took there, as you have three in your struct. If it is size: It might be correct. If it is "mtex" (which does not make any sense), it seems to be wrong.


--- Quote ---And you probably might say "sure, that's the number of the bytes you have to move forward to, to reach the part of the ADT that tells about texture paths. Well in that case, I already had that from the MHDR. So either, I'm like "da fuq?" or I still don't know what you mean?
--- End quote ---
Well, that's not what I would say.


Point is: you seem to not have understand that in an ADT, all parts of the file are chunked. That means, all blocks have a header consisting of
--- Quote ---(uint32_t magic, uint32_t size)
--- End quote ---
. Then there is specific data. The type of that data is generally unknown, but of the same size as a char8_t data[size].

So if there is a chunk MVER
--- Quote ---((uint32_t magic, uint32_t size), uint32_t version | size == 4)
--- End quote ---
and a chunk MHDR
--- Quote ---((uint32_t magic, uint32_t size), uint32_t flags, uint32_t offsets[size/sizeof (uint32_t) - 1] | size == 64)
--- End quote ---
and mhdr.offsets[2] is the pointer to MTEX, then there will be a chunk of type MTEX with
--- Quote ---((uint32_t magic, uint32_t size), char8_t data[size])
--- End quote ---
. Not int, not string, char8_t. Just plain bytes.

This chunk will then contain "something". That something is in the case of MTEX a list of filenames that are zero terminated. You therefore can do the following, depending on your language.

--- Code: ---std::vector<std::string> texture_filenames (0);
std::string temporary_string ("");
for (size_t i (0); i < mtex.size; ++i)
{
  if (mtex.data[i] == 0)
  {
    texture_filenames.push_back (temporary_string);
  }
  else
  {
    temporary_string += mtex.data[i];
  }
}
--- End code ---
or the simplified

--- Code: ---char* cur_pos = &mtex.data[0];
char* end = cur_pos + mtex.size;
while( cur_pos < end )
{
  texture_filenames.push_back (std::string (cur_pos));
  cur_pos += strlen (cur_pos) + 1;
}

--- End code ---

That way  you get all filenames in texture_filenames.

Keta:

--- Quote from: "schlumpf" ---
--- Code: ---std::vector<std::string> texture_filenames (0);
std::string temporary_string ("");
for (size_t i (0); i < mtex.size; ++i)
{
  if (mtex.data[i] == 0)
  {
    texture_filenames.push_back (temporary_string);
  }
  else
  {
    temporary_string += mtex.data[i];
  }
}
--- End code ---

--- End quote ---
I'm 99% sure I got it right now. However I would like you to inform me what exactly you mean with "mtex.data". I know that its a variable from mtex thats called data, and that it is an array, and that I am targeting the location in the array which I has. However, whenever I try to do this:

--- Code: ---[StructLayout(LayoutKind.Explicit)]
        public struct MTEX
        {
            [FieldOffset(0)]
            public int magic;
            [FieldOffset(4)]
            public int size;
            [FieldOffset(8)]
            public char[] filenames;
        }
--- End code ---
It gives me an error when I run the program. And its the same error, no matter whether I use string[] or char[].
So please, what am I misunderstanding here?

Thanks
Keta

PS. Thanks for helping me Schlumpf :p

schlumpf:

--- Quote ---It gives me an error when I run the program. And its the same error, no matter whether I use string[] or char[].
--- End quote ---

Ah, _that_ error. m(

Keta:

--- Quote from: "schlumpf" ---
--- Quote ---It gives me an error when I run the program. And its the same error, no matter whether I use string[] or char[].
--- End quote ---

Ah, _that_ error. m(
--- End quote ---
Alright yeah you want the error, of course. Sorry u_u

(You might need to view in a second tab to be able to read it)
I do want to let you know that I doubt it will tell you anything useful, as I'm 100% sure it ISN'T an access violation. Cause I mean, I've been able to read from it all the time xD Hopefully this'll help tho :P

Thanks
Keta

schlumpf:
If your system says, it is an access violation, it is.

I have no idea what you have done wrong though, as I have no idea of your code and C#. I have supplied working C++ code and can't help you more.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version