CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  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:
    01
    using System;
    02
    using System.Collections.Generic;
    03
    using System.Drawing;
    04
    using System.IO;
    05
    using System.Linq;
    06
    using System.Text;
    07
     
    08
    namespace ADFReader
    09
    {
    10
        class Program
    11
        {
    12
            public static int ApplyOffset(int data, int offset)
    13
            {
    14
                return (data - offset);
    15
            }
    16
     
    17
            public static byte ApplyOffsetByte(byte data, int offset)
    18
            {
    19
                if (offset > data)
    20
                {
    21
                    data = (byte)(data + (0x100 - offset));
    22
                    return data;
    23
                }
    24
                data = (byte)(data - offset);
    25
                return data;
    26
            }
    27
     
    28
            public static int RealSize(int datasize, int chunksize)
    29
            {
    30
                return (datasize - (datasize / (chunksize + 1)));
    31
            }
    32
     
    33
            static void Main(string[] args)
    34
            {
    35
                foreach (string file in Directory.GetFiles(@"data\", "*.adf")) ;
    36
                {
    37
                    byte[] bytes = File.ReadAllBytes(file);
    38
     
    39
                    // so the last byte happens to be twice the "offset" number.
    40
                    // but that doesn't matter because the second to last byte is *always?* the offset number anyways!
    41
                    byte offset = bytes[bytes.Length - 2];
    42
     
    43
                    BinaryReader reader = new BinaryReader(new MemoryStream(bytes));
    44
                    StreamWriter writer = new StreamWriter(@"asppp\" + Path.GetFileName(file) + "-header.txt");
    45
     
    46
                    byte fileType = reader.ReadByte();
    47
                    int extraLength = reader.ReadInt32() + 1;
    48
                    writer.WriteLine("Type: {0}, Extra Length: {1}", fileType, extraLength);
    49
                    // not sure what these extra bytes are
    50
                    writer.Write("Extra Bytes: ");
    51
                    for (int i = 0; i < extraLength; i++) writer.Write("{0,4}", ApplyOffsetByte(reader.ReadByte(), offset));
    52
                    writer.WriteLine();
    53
                    int numberOfFrames = ApplyOffset(reader.ReadInt32(), offset);
    54
                    writer.WriteLine("NumberOfFrames: {0}", numberOfFrames);
    55
     
    56
                    for (int i = 0; i < numberOfFrames; i++)
    57
                    {
    58
                        int frameId = ApplyOffset(reader.ReadInt32(), offset);
    59
                        // always 1 as far as i have seen
    60
                        byte unknownByte = ApplyOffsetByte(reader.ReadByte(), offset);
    61
                        int x = ApplyOffset(reader.ReadInt32(), offset);
    62
                        int y = ApplyOffset(reader.ReadInt32(), offset);
    63
                        int width = ApplyOffset(reader.ReadInt32(), offset);
    64
                        int height = ApplyOffset(reader.ReadInt32(), offset);
    65
     
    66
                        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);
    67
                    }
    68
     
    69
                    // not sure what this is, 36 = wav though i think
    70
                    int unknown = ApplyOffset(reader.ReadInt32(), offset);
    71
                    writer.WriteLine("U: {0}", unknown);
    72
     
    73
                    writer.Close();
    74
     
    75
                    int length = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
    76
                    byte[] buffer = reader.ReadBytes(length);
    77
                    byte[] data = new byte[RealSize(buffer.Length, 0x315)];
    78
                    for (int k = 0; k < buffer.Length; k++)
    79
                    {
    80
                        data[k - (k / 790)] = ApplyOffsetByte(buffer[k], offset);
    81
                    }
    82
     
    83
                    string ext = ".bmp";
    84
                    if (unknown == 36) ext = ".wav";
    85
     
    86
                    File.WriteAllBytes(@"asppp\" + Path.GetFileName(file) + ext, data);
    87
                }
    88
            }
    89
        }
    90
    }







    The main problem is this part




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

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: What am i doing wrong?

    It looks like you are looking in a sub directory named data off of wherever the current directory is when the program is running.
    Could be that the current directory is not what you think it is.

    You may want to step through the code line by line or add some code to show you what the path is that it is trying to use
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Aug 2013
    Posts
    8

    Re: What am i doing wrong?

    DataMiser is right. Incorrect subdir could be a problem issue. If it fails use absolute path, like below:
    Code:
    foreach (string file in Directory.GetFiles(@"c:\data\", "*.adf")) ;

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured