Ejemplo n.º 1
0
        public override sealed bool ParseData(byte[] buffer)
        {
            if ((buffer == null))
            {
                return(false);
            }
            int offset = 0;

            StringsHeader stringsHeader = FileTools.ByteArrayToStructure <StringsHeader>(buffer, ref offset);

            for (int i = 0; i < stringsHeader.Count; i++)
            {
                StringBlock stringBlock = new StringBlock
                {
                    ReferenceId = FileTools.ByteArrayToInt32(buffer, ref offset),
                    Unknown     = FileTools.ByteArrayToInt32(buffer, ref offset)
                };

                int count = FileTools.ByteArrayToInt32(buffer, ref offset);
                stringBlock.StringId = FileTools.ByteArrayToStringASCII(buffer, offset);
                offset += count + 1;

                stringBlock.Reserved = FileTools.ByteArrayToInt32(buffer, ref offset);

                count = FileTools.ByteArrayToInt32(buffer, ref offset);
                stringBlock.String = FileTools.ByteArrayToStringUnicode(buffer, offset, count);
                offset            += count;

                int attributeCount = FileTools.ByteArrayToInt32(buffer, ref offset);

                for (int j = 0; j < attributeCount; j++)
                {
                    count = FileTools.ByteArrayToInt32(buffer, ref offset);
                    int byteCount = (count + 1) * 2;

                    switch (j)
                    {
                    case 0:
                        stringBlock.Attribute1 = FileTools.ByteArrayToStringUnicode(buffer, offset, byteCount);
                        break;

                    case 1:
                        stringBlock.Attribute2 = FileTools.ByteArrayToStringUnicode(buffer, offset, byteCount);
                        break;

                    case 2:
                        stringBlock.Attribute3 = FileTools.ByteArrayToStringUnicode(buffer, offset, byteCount);
                        break;

                    case 3:
                        stringBlock.Attribute4 = FileTools.ByteArrayToStringUnicode(buffer, offset, byteCount);
                        break;
                    }

                    offset += byteCount;
                }

                Rows.Add(stringBlock);
            }

            return(HasIntegrity = ((offset == buffer.Length)) ? true : false);
        }
Ejemplo n.º 2
0
        private void _ParseDataType(byte[] buffer)
        {
            int offset = 0;

            // header
            FileHeader fileHeader = FileTools.ByteArrayToStructure <FileHeader>(buffer, ref offset);

            if (fileHeader.FileToken != Token.Head)
            {
                throw new Exceptions.UnexpectedTokenException("Expected Head token but got 0x" + fileHeader.FileToken.ToString("X8"));
            }
            if (fileHeader.FileVersion != RequiredVersion)
            {
                throw new Exceptions.NotSupportedFileVersionException(RequiredVersion, fileHeader.FileVersion);
            }

            // Read the strings section
            StringsHeader stringsHeader = FileTools.ByteArrayToStructure <StringsHeader>(buffer, ref offset);

            if (stringsHeader.StringsToken != Token.Sect)
            {
                throw new Exceptions.UnexpectedTokenException(Token.Sect, stringsHeader.StringsToken);
            }
            List <String> strings = new List <String>();

            for (int i = 0; i < stringsHeader.StringsCount; i++)
            {
                strings.Add(FileTools.ByteArrayToStringASCII(buffer, offset));
                offset += strings[i].Length + 1; // +1 for \0
            }

            // String Details
            UInt32 stringsDetailsToken = FileTools.ByteArrayToUInt32(buffer, ref offset);

            if (stringsDetailsToken != Token.Sect)
            {
                throw new Exceptions.UnexpectedTokenException(Token.Sect, stringsDetailsToken);
            }
            // Skip over the details struct because we don't need it.
            offset += stringsHeader.StringsCount * Marshal.SizeOf(typeof(StringDetailsStruct));

            // Files Structure details
            UInt32 filesToken = FileTools.ByteArrayToUInt32(buffer, ref offset);

            if (filesToken != Token.Sect)
            {
                throw new Exceptions.UnexpectedTokenException(Token.Sect, stringsDetailsToken);
            }
            FileDetails.AddRange(FileTools.ByteArrayToArray <FileEntryStruct>(buffer, ref offset, fileHeader.FileCount));

            // The Files list is the public interface
            for (int i = 0; i < fileHeader.FileCount; i++)
            {
                PackFileEntry fileEntry = new FileEntry(FileDetails[i])
                {
                    Pack = this,
                    Path = System.IO.Path.Combine(strings[FileDetails[i].DirectoryIndex], strings[FileDetails[i].NameIndex]),
                };

                Files.Add(fileEntry);
            }

            HasIntegrity = true;
        }
Ejemplo n.º 3
0
        public override bool ParseDataTable(DataTable table, FileManager fileManager = null)
        {
            byte[] buffer     = new byte[1024];
            int    offset     = 0;
            int    lastOffset = 4;

            // write main header first
            StringsHeader stringsHeader = new StringsHeader
            {
                Header  = Token.Header,
                Version = Version,
                Count   = table.Rows.Count
            };

            FileTools.WriteToBuffer(ref buffer, ref offset, stringsHeader);

            // write string blocks
            int row = 0;

            foreach (DataRow dr in table.Rows)
            {
                String stringId = dr["StringID"] as String;
                if (String.IsNullOrEmpty(stringId))
                {
                    continue;
                }


                // ReferenceId
                Int32 referenceId = (Int32)dr[0];
                FileTools.WriteToBuffer(ref buffer, ref offset, referenceId);


                // Unknown
                Int32 unknownValue = (Int32)dr[1];
                FileTools.WriteToBuffer(ref buffer, ref offset, unknownValue);


                // StringId
                byte[] stringIdBytes = FileTools.StringToASCIIByteArray(stringId);
                Int32  byteCount     = stringIdBytes.Length;
                FileTools.WriteToBuffer(ref buffer, ref offset, byteCount);
                FileTools.WriteToBuffer(ref buffer, ref offset, stringIdBytes);
                offset++; // \0


                // Reserved
                offset += sizeof(Int32);


                // String
                String str = dr["String"] as String;
                byteCount = 2; // \0
                if (String.IsNullOrEmpty(str))
                {
                    FileTools.WriteToBuffer(ref buffer, ref offset, byteCount);
                }
                else
                {
                    byte[] stringBytes = FileTools.StringToUnicodeByteArray(str);
                    byteCount += stringBytes.Length;
                    FileTools.WriteToBuffer(ref buffer, ref offset, byteCount);
                    FileTools.WriteToBuffer(ref buffer, ref offset, stringBytes);
                }
                offset += 2; // \0


                // Attributes
                List <String> attributeStrings = new List <string>();
                for (int i = 0; i < MaxAttributes; i++)
                {
                    String colName = "Attribute" + (i + 1);
                    if (!table.Columns.Contains(colName))
                    {
                        continue;
                    }

                    String attribute = dr[colName] as String;
                    if (String.IsNullOrEmpty(attribute))
                    {
                        continue;
                    }

                    attributeStrings.Add(attribute);
                }

                Int32 attributeCount = attributeStrings.Count;
                FileTools.WriteToBuffer(ref buffer, ref offset, attributeCount);

                foreach (String s in attributeStrings)
                {
                    Int32 charCount = s.Length;
                    FileTools.WriteToBuffer(ref buffer, ref offset, charCount);
                    byte[] attributeBytes = FileTools.StringToUnicodeByteArray(s);
                    FileTools.WriteToBuffer(ref buffer, offset, attributeBytes);
                    lastOffset = attributeBytes.Length + 2; // \0
                    offset    += lastOffset;
                }

                row++;
            }

            //offset -= lastOffset;
            byte[] returnBuffer = new byte[offset];
            Buffer.BlockCopy(buffer, 0, returnBuffer, 0, offset);

            return(true);
        }