This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: [QUESTION] [C# Dev] Parsing MTEX  (Read 2289 times)

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
[QUESTION] [C# Dev] Parsing MTEX
« on: January 15, 2012, 12:28:58 am »
Hello Modcraft

This question is mainly directed to all the people who knows about reading the ADT files with a scripting language. Its written in C# (C Sharp)
So my question is, Steff has helped me out with a code that works pretty fine so far, only I have encountered 2 problems. First problem is when I parse the MTEX paths, it also gives me a few lines of random stuff before the actual paths. This is however not the reason why I'm making this post. The reason is my second problem. You see, when I run the program, as I said before it gives me all the texture paths. However the last 2 texture paths are on the same line, apparently. And that is the same case in all of the ADTs I've tried. And yes, I have tested all ADTs in-game, and they do work.

So... Anyone that can help me out with telling me why that is? I am 99% sure you're gonna wanna see the source, and that is no problem :)
Pastebin: http://pastebin.com/EBdRpjRq

This is the output it gives me when I run the program:

As you can see, it posts the last 2 paths on the same path, even though (as visible in the source) I split up and make a new line after each path.

Any help would be appreciated! :)



Thanks
Keta
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #1 on: January 15, 2012, 12:52:04 am »
You seem to not have realized, that mtex is a chunk as well. That's the "gibberish" bytes there.

On the last two always being on the same line: maybe string is not the right type, idk.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #2 on: January 15, 2012, 01:12:40 am »
If you are referring to the fact that I should make a "public struct" with the info of the MTEX, like I did with the 2 others, I can then tell you that I've already tried that, and it gave me an error every time I tried, and no matter how i tried.
But if your certain, please do tell me how you would accomplish that.

Thanks
Keta
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #3 on: January 15, 2012, 01:58:59 am »
I would not tell you that you are wrong, if I don't know you are.

You are not caring about the magIc and size fields.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #4 on: January 15, 2012, 04:17:12 am »
So let me get this straight, you want me to add a
Code: [Select]
[StructLayout(LayoutKind.Explicit)]
        public struct MTEX
        {
            [FieldOffset(0)]
            public int magic;
            [FieldOffset(4)]
            public int size;
            [FieldOffset(8)]
            public int mtex;
        }
correct? And then from the main function, call it like I did with the other two?
Code: [Select]
MTEX mtex = ByteToType<MTEX>(foo);Yes? Well, that gives me the following number: 1740
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? Cause I tried reading FROM THERE, didn't really return any positive result, and I've tried to read that many BYTES forward from the MHDR.mtex position, and that just gave me a VERY long string... So please, do explain.

Also, this is how the code ended up looking after you wanted me to try that out:
http://pastebin.com/udDcLQqu

Thanks
Keta
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #5 on: January 15, 2012, 01:26:15 pm »
Quote from: "Keta"
So let me get this straight, you want me to add a
Code: [Select]
[StructLayout(LayoutKind.Explicit)]
        public struct MTEX
        {
            [FieldOffset(0)]
            public int magic;
            [FieldOffset(4)]
            public int size;
            [FieldOffset(8)]
            public int mtex;
        }
correct?
No.

Quote
And then from the main function, call it like I did with the other two?
Code: [Select]
MTEX mtex = ByteToType<MTEX>(foo);
Yes.

Quote
Yes? Well, that gives me the following number: 1740
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?
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)
. 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)
and a chunk MHDR
Quote
((uint32_t magic, uint32_t size), uint32_t flags, uint32_t offsets[size/sizeof (uint32_t) - 1] | size == 64)
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])
. 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: [Select]
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];
  }
}
or the simplified
Code: [Select]
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;
}

That way  you get all filenames in texture_filenames.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #6 on: January 15, 2012, 05:32:40 pm »
Quote from: "schlumpf"
Code: [Select]
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];
  }
}
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: [Select]
[StructLayout(LayoutKind.Explicit)]
        public struct MTEX
        {
            [FieldOffset(0)]
            public int magic;
            [FieldOffset(4)]
            public int size;
            [FieldOffset(8)]
            public char[] filenames;
        }
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
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #7 on: January 15, 2012, 06:15:37 pm »
Quote
It gives me an error when I run the program. And its the same error, no matter whether I use string[] or char[].

Ah, _that_ error. m(
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #8 on: January 15, 2012, 07:05:50 pm »
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[].

Ah, _that_ error. m(
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
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #9 on: January 15, 2012, 08:00:22 pm »
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.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] [C# Dev] Parsing MTEX
« Reply #10 on: January 15, 2012, 08:20:15 pm »
Schlumpf, you have to agree with me at this point. It obviously can't be an access violation when I can easily read from the file when I just use a bit of a different code >.<

Despite that, I need to make sure, it IS a Char Array I need to make, called "filenames" in the MTEX, right?

Thanks
Keta
« Last Edit: January 01, 1970, 01:00:00 am by Admin »