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] Basic ADT C# Reader  (Read 5578 times)

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #15 on: December 22, 2011, 02:32:15 am »
fopen: open file.
fseek: modify reading / writing position in file.
fread: read from there.
fwrite: write there.
fclose: close the file.

What exactly is your problem? That it's not even a bit like this in C#? Well, C is not object orientated and old as fuck.

[I DID NOT READ THIS WHOLE THREAD. ALL I KNOW IS THAT YOU SEARCHED SOME C APPLICATIONS TO BASE YOUR C# STUFF ON IT AND THAT YOU ARE CONFUSED BY FSEEK AND FREAD.]

I don't really understand why you want to base your C# application on some other C one. It would be way better just writing your own stuff and read the wiki for the format.

I can write you a lot of code in C/C++ you can't really "port" to C# without changing pretty much everything. There is no sense in trying to "port" stuff when writing a completely new application.

What you want is a BinaryReader for the file, then
Code: [Select]
   public static T ByteToType<T>(BinaryReader reader)
    {
        byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));

        GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
        T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
        handle.Free();

        return theStructure;
    }
You can then read stuff from the file to your structures — the ones from the wiki.
Code: [Select]
[StructLayout(LayoutKind.Explicit)]
struct MVER
{
    [FieldOffset(0)]
    public int magic;
    [FieldOffset(4)]
    public int size;
    [FieldOffset(8)]
    public int version;
}
Code: [Select]
BinaryReader foo;
MVER mver = ByteToType<MVER> (foo);
if (mver.magic == 'MVER' && mver.size == 4 && mver.version == 18)
  proceed;
else
  this_is_no_wotlk_adt;

And I know _nothing_ about C# (I therefore don't guarantee anything is working in this code). This has just been some googling. (http://stackoverflow.com/questions/2384 ... ct-c-sharp)

With these code fragments, you should be able to read from the file and query data. Writing back should be about the same.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #16 on: December 23, 2011, 04:14:10 am »
Quote from: "schlumpf"
fopen: open file.
fseek: modify reading / writing position in file.
fread: read from there.
fwrite: write there.
fclose: close the file.

What exactly is your problem? That it's not even a bit like this in C#? Well, C is not object orientated and old as fuck.

[I DID NOT READ THIS WHOLE THREAD. ALL I KNOW IS THAT YOU SEARCHED SOME C APPLICATIONS TO BASE YOUR C# STUFF ON IT AND THAT YOU ARE CONFUSED BY FSEEK AND FREAD.]

I don't really understand why you want to base your C# application on some other C one. It would be way better just writing your own stuff and read the wiki for the format.

I can write you a lot of code in C/C++ you can't really "port" to C# without changing pretty much everything. There is no sense in trying to "port" stuff when writing a completely new application.

What you want is a BinaryReader for the file, then
Code: [Select]
   public static T ByteToType<T>(BinaryReader reader)
    {
        byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));

        GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
        T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
        handle.Free();

        return theStructure;
    }
You can then read stuff from the file to your structures — the ones from the wiki.
Code: [Select]
[StructLayout(LayoutKind.Explicit)]
struct MVER
{
    [FieldOffset(0)]
    public int magic;
    [FieldOffset(4)]
    public int size;
    [FieldOffset(8)]
    public int version;
}
Code: [Select]
BinaryReader foo;
MVER mver = ByteToType<MVER> (foo);
if (mver.magic == 'MVER' && mver.size == 4 && mver.version == 18)
  proceed;
else
  this_is_no_wotlk_adt;

And I know _nothing_ about C# (I therefore don't guarantee anything is working in this code). This has just been some googling. (http://stackoverflow.com/questions/2384 ... ct-c-sharp)

With these code fragments, you should be able to read from the file and query data. Writing back should be about the same.

After studying the code a bit, I've come to the conclusion that this works for giving me the version of the ADT :P Thanks ^^ Anyhow, mind me when I ask what the "FieldOffset(X)" (where X is a number) means? I'm pretty sure its the location of the different things in the structure, but how exactly do I change them to something else? I mean, currently it reads the version. What number would I change it to, for it to read the texture files?



Thanks
Keta

PS. This is my code. I had to comment out the "if magic = MVER" thing, cause I couldn't make it work.
Code: [Select]
static void Main(string[] args)
        {
            BinaryReader foo = new BinaryReader(File.Open("E:\WoW WoTLK\World\Maps\Azeroth\Azeroth_31_35.adt", FileMode.Open));
            MVER mver = ByteToType<MVER>(foo);
            if (/*mver.magic == 'MVER' && */mver.size == 4 && mver.version == 18)
            {
                Console.WriteLine(mver.version);
                Console.WriteLine(mver.size);
                Console.WriteLine(mver.magic);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("This is no WoTLK ADT");
                Console.ReadLine();
            }
        }

        public static T ByteToType<T>(BinaryReader reader)
        {
            byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));

            GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
            handle.Free();

            return theStructure;
        }

        [StructLayout(LayoutKind.Explicit)]
        struct MVER
        {
            [FieldOffset(0)]
            public int magic;
            [FieldOffset(4)]
            public int size;
            [FieldOffset(8)]
            public int version;
        }
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Steff

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 4551
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #17 on: December 23, 2011, 08:07:04 am »
You have first the version and then the file header.
This you must read and is fix in every adt. Then the data chunks follow.

Try to implement this in a class.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »
Please mark as solved if solved.
Don't ask if you could ask a question... JUST ask the Question.
You can send me also offline messages. I will answer if I get online.
Skype: project.modcraft
Discord: steff#6954

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #18 on: December 23, 2011, 11:19:19 am »
Quote from: "Steff"
You have first the version and then the file header.
This you must read and is fix in every adt. Then the data chunks follow.

Try to implement this in a class.
Excuse me when I ask you what exactly you mean with the first two sentences. Either its my english, or my computing knowledge which fails me, but I don't exactly understand what you mean with "is fix in every adt." :s
You mean that I should read the file header cause it tells me where in the file I can find the different numbers and whatnot? Please, explain.

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

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #19 on: December 23, 2011, 11:40:12 am »
Quote from: "Keta"
You mean that I should read the file header cause it tells me where in the file I can find the different numbers and whatnot? Please, explain.

Yes. MPHD holds offsets to other positions of chunks. You can then "fseek" into the file (the stream) and read from there.

To continue, I'd suggest writing the next chunk from the wiki into a C# structure. Then read it in.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Keta

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 210
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #20 on: December 25, 2011, 12:27:33 pm »
Quote from: "schlumpf"
Yes. MPHD holds offsets to other positions of chunks. You can then "fseek" into the file (the stream) and read from there..
What exactly is MPHD? Cause there was nothing about it on the Wiki. :s Not in the ADT section anyway.

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

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] Basic ADT C# Reader
« Reply #21 on: December 25, 2011, 03:36:08 pm »
Quote from: "Keta"
Quote from: "schlumpf"
Yes. MPHD holds offsets to other positions of chunks. You can then "fseek" into the file (the stream) and read from there..
What exactly is MPHD? Cause there was nothing about it on the Wiki. :s Not in the ADT section anyway.

Thanks
Keta
Sorry, MHDR. MPHD was WDTs.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »