Example #1
0
        public CPFFile(byte[] file)
        {
            if (Encoding.UTF8.GetChars(file)[0] == '<')
            {
                ReadAsXML(file);
                return;
            }
            var stream = new MemoryStream(file);
            var io     = new IoBuffer(stream);

            io.ByteOrder = ByteOrder.LITTLE_ENDIAN;
            var typeID        = io.ReadUInt32();
            var version       = io.ReadUInt16();
            var numberOfItems = io.ReadUInt32();

            for (var i = 0; i < numberOfItems; i++)
            {
                var dataType        = (DataType)io.ReadUInt32();
                var fieldNameLength = io.ReadInt32();
                var fieldName       = io.ReadCString(fieldNameLength);
                if (fieldName[0] != '"')
                {
                    switch (dataType)
                    {
                    case DataType.UInt:
                        var data = new CPFEntry(fieldName, dataType, io.ReadUInt32());
                        entries.Add(fieldName, data);
                        break;

                    case DataType.Int2:
                        var data5 = new CPFEntry(fieldName, dataType, io.ReadInt32());
                        entries.Add(fieldName, data5);
                        break;

                    case DataType.String:
                        var dataLength = io.ReadInt32();
                        var data2      = new CPFEntry(fieldName, dataType, io.ReadCString(dataLength));
                        entries.Add(fieldName, data2);
                        break;

                    case DataType.Float:
                        var data3 = new CPFEntry(fieldName, dataType, io.ReadFloat());
                        entries.Add(fieldName, data3);
                        break;

                    case DataType.Boolean:
                        var boole = false;
                        if (io.ReadByte() == (byte)1)
                        {
                            boole = true;
                        }
                        var data4 = new CPFEntry(fieldName, dataType, boole);
                        entries.Add(fieldName, data4);
                        break;
                    }
                }
            }
        }
Example #2
0
        void ReadAsXML(byte[] file)
        {
            var doc = new XmlDocument();

            doc.Load(new MemoryStream(file));
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                var key  = node.Attributes["key"].InnerText;
                var type = StringToDataType[node.Attributes["type"].InnerText.ToUpper()];
                switch (type)
                {
                case DataType.UInt:
                    var entri2 = new CPFEntry(key, type, UInt32.Parse(node.InnerText));
                    entries[key] = entri2;
                    break;

                case DataType.String:
                    var entri3 = new CPFEntry(key, type, node.InnerText);
                    entries[key] = entri3;
                    break;

                case DataType.Float:
                    var entri4 = new CPFEntry(key, type, float.Parse(node.InnerText));
                    entries[key] = entri4;
                    break;

                case DataType.Boolean:
                    var boolval = false;
                    if (node.InnerText != "false")
                    {
                        boolval = true;
                    }
                    var entri = new CPFEntry(key, type, boolval);
                    entries[key] = entri;
                    break;

                case DataType.Int2:
                    var entri5 = new CPFEntry(key, type, int.Parse(node.InnerText));
                    entries[key] = entri5;
                    break;
                }
            }
        }