Ejemplo n.º 1
0
        public static void SaveAfs2File(string filePath)
        {
            if (!Directory.Exists(Path.GetDirectoryName(filePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            }
            string        saveLocation = String.Format("{0}/{1}", Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
            YAXSerializer serializer   = new YAXSerializer(typeof(AFS2_File), YAXSerializationOptions.DontSerializeNullObjects);
            AFS2_File     utfFile      = (AFS2_File)serializer.DeserializeFromFile(filePath);

            byte[] header;
            byte[] bytes = WriteAfs2File(utfFile, out header);
            File.WriteAllBytes(saveLocation, bytes);
        }
Ejemplo n.º 2
0
        public static byte[] WriteAfs2File(AFS2_File afs2File, out byte[] header)
        {
            header = null;
            List <byte> bytes = new List <byte>();

            if (afs2File == null)
            {
                return(bytes.ToArray());
            }

            //Init
            int        count        = (afs2File.Entries != null) ? afs2File.Entries.Count : 0;
            int        finalPointer = 0;
            List <int> Pointers     = new List <int>();

            afs2File.SortEntries();

            //Header
            bytes.AddRange(BitConverter.GetBytes((uint)AFS2_SIGNATURE));
            bytes.Add(afs2File.I_04);
            bytes.Add((byte)afs2File.Pointer_Size);
            bytes.Add(afs2File.I_06);
            bytes.Add(afs2File.I_07);
            bytes.AddRange(BitConverter.GetBytes((uint)count));
            bytes.AddRange(BitConverter.GetBytes((uint)afs2File.ByteAlignment));

            //Cue ID list
            for (int i = 0; i < count; i++)
            {
                bytes.AddRange(BitConverter.GetBytes(afs2File.Entries[i].AwbId));
            }

            //Pointer List
            for (int i = 0; i < count + 1; i++)
            {
                switch (afs2File.Pointer_Size)
                {
                case PointerSize.UInt16:
                    Pointers.Add(bytes.Count());
                    finalPointer = bytes.Count();
                    bytes.AddRange(new byte[2]);
                    break;

                case PointerSize.UInt32:
                    Pointers.Add(bytes.Count());
                    finalPointer = bytes.Count();
                    bytes.AddRange(new byte[4]);
                    break;

                default:
                    throw new Exception(String.Format("Undefined PointerSize encountered: {0}\nSave failed.", afs2File.Pointer_Size));
                }
            }

            int headerEnd = bytes.Count;

            for (int i = 0; i < count; i++)
            {
                //Fill pointer
                switch (afs2File.Pointer_Size)
                {
                case PointerSize.UInt16:
                    bytes = Utils.ReplaceRange(bytes, BitConverter.GetBytes((UInt16)bytes.Count()), Pointers[i]);
                    break;

                case PointerSize.UInt32:
                    bytes = Utils.ReplaceRange(bytes, BitConverter.GetBytes(bytes.Count()), Pointers[i]);
                    break;
                }

                //Add padding
                bytes.AddRange(new byte[CalculatePadding(bytes.Count, afs2File.ByteAlignment)]);

                //Add data
                bytes.AddRange(afs2File.Entries[i].bytes);
            }

            //Fill in final pointer
            switch (afs2File.Pointer_Size)
            {
            case PointerSize.UInt16:
                bytes = Utils.ReplaceRange(bytes, BitConverter.GetBytes((UInt16)bytes.Count()), finalPointer);
                break;

            case PointerSize.UInt32:
                bytes = Utils.ReplaceRange(bytes, BitConverter.GetBytes(bytes.Count()), finalPointer);
                break;
            }

            header = bytes.GetRange(0, headerEnd).ToArray();

            return(bytes.ToArray());
        }
Ejemplo n.º 3
0
        public static AFS2_File LoadAfs2File(byte[] rawBytes, int offset = 0, int size = -1)
        {
            if (size == -1)
            {
                size = rawBytes.Length;
            }

            AFS2_File afs2File = new AFS2_File()
            {
                Entries = new List <AFS2_Entry>()
            };

            if (BitConverter.ToInt32(rawBytes, offset + 0) != AFS2_SIGNATURE)
            {
                throw new InvalidDataException(String.Format("AFS2 Signature not found at offset 0x{0}.\nParse failed.", offset.ToString("x")));
            }

            int count = BitConverter.ToInt32(rawBytes, offset + 8);

            afs2File.ByteAlignment = BitConverter.ToInt32(rawBytes, offset + 12);
            afs2File.I_04          = rawBytes[offset + 4];
            afs2File.Pointer_Size  = (PointerSize)rawBytes[offset + 5];
            afs2File.I_06          = rawBytes[offset + 6];
            afs2File.I_07          = rawBytes[offset + 7];

            //Init
            int        cueIdOffset       = 16 + offset;
            int        pointerListOffset = ((2 * count) + 16) + offset;
            List <int> Pointers          = new List <int>();
            List <int> PointersFiltered  = new List <int>();

            //Cue IDs
            for (int i = 0; i < count; i++)
            {
                //IDs
                afs2File.Entries.Add(new AFS2_Entry()
                {
                    AwbId = BitConverter.ToUInt16(rawBytes, cueIdOffset)
                });
                cueIdOffset += 2;

                //Pointers
                switch (afs2File.Pointer_Size)
                {
                case PointerSize.UInt16:
                    Pointers.Add(BitConverter.ToUInt16(rawBytes, pointerListOffset));
                    PointersFiltered.Add(BitConverter.ToUInt16(rawBytes, pointerListOffset));
                    pointerListOffset += 2;
                    break;

                case PointerSize.UInt32:
                    Pointers.Add(BitConverter.ToInt32(rawBytes, pointerListOffset));
                    PointersFiltered.Add(BitConverter.ToInt32(rawBytes, pointerListOffset));
                    pointerListOffset += 4;
                    break;
                }
            }
            if (afs2File.Pointer_Size == PointerSize.UInt16)
            {
                Pointers.Add(BitConverter.ToUInt16(rawBytes, pointerListOffset));
                PointersFiltered.Add(BitConverter.ToUInt16(rawBytes, pointerListOffset));
            }
            else
            {
                Pointers.Add(BitConverter.ToInt32(rawBytes, pointerListOffset));
                PointersFiltered.Add(BitConverter.ToInt32(rawBytes, pointerListOffset));
            }

            //Filter offsets
            for (int i = 0; i < count + 1; i++)
            {
                PointersFiltered[i] = GetActualOffset(PointersFiltered[i], afs2File.ByteAlignment) + offset;
                Pointers[i]        += offset;
            }

            //Data
            for (int i = 0; i < count; i++)
            {
                //Get length
                int length = 0;
                length = Pointers[i + 1] - PointersFiltered[i];

                afs2File.Entries[i].bytes = rawBytes.GetRange(PointersFiltered[i], length);
            }

            return(afs2File);
        }