Forum > Miscellaneous
[QUESTION] Basic ADT C# Reader
<< < (4/5) > >>
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: --- 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; } --- End code --- You can then read stuff from the file to your structures — the ones from the wiki.
--- Code: ---[StructLayout(LayoutKind.Explicit)] struct MVER { [FieldOffset(0)] public int magic; [FieldOffset(4)] public int size; [FieldOffset(8)] public int version; } --- End code ---
--- Code: ---BinaryReader foo; MVER mver = ByteToType<MVER> (foo); if (mver.magic == 'MVER' && mver.size == 4 && mver.version == 18) proceed; else this_is_no_wotlk_adt;
--- End code ---
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.
Keta:
--- 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: --- 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; } --- End code --- You can then read stuff from the file to your structures — the ones from the wiki.
--- Code: ---[StructLayout(LayoutKind.Explicit)] struct MVER { [FieldOffset(0)] public int magic; [FieldOffset(4)] public int size; [FieldOffset(8)] public int version; } --- End code ---
--- Code: ---BinaryReader foo; MVER mver = ByteToType<MVER> (foo); if (mver.magic == 'MVER' && mver.size == 4 && mver.version == 18) proceed; else this_is_no_wotlk_adt;
--- End code ---
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. --- End quote ---
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: ---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; } --- End code ---
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.
Keta:
--- 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. --- End quote --- 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
schlumpf:
--- 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. --- End quote ---
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.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
|