What am i doing wrong?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: What am i doing wrong?

  1. #1
    Join Date
    Jul 2013
    Posts
    2

    What am i doing wrong?

    Hey i cant figure out what i am doing wrong, Its to extract some images from .adf files, I have had it working about 2 years ago but now i cant figure out the right directory. I can post the files i need to extract and the project if it helps.

    Thanks



    Code:
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace ADFReader
    {
        class Program
        {
            public static int ApplyOffset(int data, int offset)
            {
                return (data - offset);
            }
    
            public static byte ApplyOffsetByte(byte data, int offset)
            {
                if (offset > data)
                {
                    data = (byte)(data + (0x100 - offset));
                    return data;
                }
                data = (byte)(data - offset);
                return data;
            }
    
            public static int RealSize(int datasize, int chunksize)
            {
                return (datasize - (datasize / (chunksize + 1)));
            }
    
            static void Main(string[] args)
            {
                foreach (string file in Directory.GetFiles(@"data\", "*.adf")) ;
                {
                    byte[] bytes = File.ReadAllBytes(file);
    
                    // so the last byte happens to be twice the "offset" number.
                    // but that doesn't matter because the second to last byte is *always?* the offset number anyways!
                    byte offset = bytes[bytes.Length - 2];
    
                    BinaryReader reader = new BinaryReader(new MemoryStream(bytes));
                    StreamWriter writer = new StreamWriter(@"asppp\" + Path.GetFileName(file) + "-header.txt");
    
                    byte fileType = reader.ReadByte();
                    int extraLength = reader.ReadInt32() + 1;
                    writer.WriteLine("Type: {0}, Extra Length: {1}", fileType, extraLength);
                    // not sure what these extra bytes are
                    writer.Write("Extra Bytes: ");
                    for (int i = 0; i < extraLength; i++) writer.Write("{0,4}", ApplyOffsetByte(reader.ReadByte(), offset));
                    writer.WriteLine();
                    int numberOfFrames = ApplyOffset(reader.ReadInt32(), offset);
                    writer.WriteLine("NumberOfFrames: {0}", numberOfFrames);
    
                    for (int i = 0; i < numberOfFrames; i++)
                    {
                        int frameId = ApplyOffset(reader.ReadInt32(), offset);
                        // always 1 as far as i have seen
                        byte unknownByte = ApplyOffsetByte(reader.ReadByte(), offset);
                        int x = ApplyOffset(reader.ReadInt32(), offset);
                        int y = ApplyOffset(reader.ReadInt32(), offset);
                        int width = ApplyOffset(reader.ReadInt32(), offset);
                        int height = ApplyOffset(reader.ReadInt32(), offset);
    
                        writer.WriteLine("Id: {0,4} U: {1,4} X: {2,4} Y: {3,4} W: {4,4} H: {5,4}", frameId, unknownByte, x, y, width, height);
                    }
    
                    // not sure what this is, 36 = wav though i think
                    int unknown = ApplyOffset(reader.ReadInt32(), offset);
                    writer.WriteLine("U: {0}", unknown);
    
                    writer.Close();
    
                    int length = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
                    byte[] buffer = reader.ReadBytes(length);
                    byte[] data = new byte[RealSize(buffer.Length, 0x315)];
                    for (int k = 0; k < buffer.Length; k++)
                    {
                        data[k - (k / 790)] = ApplyOffsetByte(buffer[k], offset);
                    }
    
                    string ext = ".bmp";
                    if (unknown == 36) ext = ".wav";
    
                    File.WriteAllBytes(@"asppp\" + Path.GetFileName(file) + ext, data);
                }
            }
        }
    }
    Sorry for the double post, The main problem is

    Code:
    static void Main(string[] args)
            {
                foreach (string file in Directory.GetFiles(@"data\", "*.adf")) ;
                {
                    byte[] bytes = File.ReadAllBytes(file);
    
                    // so the last byte happens to be twice the "offset" number.
                    // but that doesn't matter because the second to last byte is *always?* the offset number anyways!
                    byte offset = bytes[bytes.Length - 2];
    
                    BinaryReader reader = new BinaryReader(new MemoryStream(bytes));
                    StreamWriter writer = new StreamWriter(@"asppp\" + Path.GetFileName(file) + "-header.txt");

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,985

    Re: What am i doing wrong?

    Is this c# code? This forum is for c++ issues. I suggest you repost to the c-sharp programming forum.

    [moderator could you move please]
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center