Ejemplo n.º 1
0
        void PerformTypeConversions(List <SymbolEntry> symbols)
        {
            List <SymbolEntry> outputSymbols = new List <SymbolEntry>();

            foreach (var item in symbols)
            {
                if (item.type == ESymbolType.ESYMBOLTYPE_STRUCTURE)
                {
                    List <SymbolEntry> children = (List <SymbolEntry>)(item.value);
                    SymbolEntry        result   = CondenseStructureToSymbol(children);
                    if (result != null)
                    {
                        item.value   = result.value;
                        item.type    = result.type;
                        item.subType = result.subType;
                    }
                    PerformTypeConversions(children);
                }
                else if (item.type == ESymbolType.ESYMBOLTYPE_ARRAY && item.subType == ESymbolType.ESYMBOLTYPE_STRUCTURE)
                {
                    List <object> arrayHead = (List <object>)item.value;

                    List <object> condensedChildren = new List <object>();

                    foreach (var child in arrayHead)
                    {
                        List <SymbolEntry> symbolList = (List <SymbolEntry>)child;
                        SymbolEntry        result     = CondenseStructureToSymbol(symbolList);
                        if (result != null)
                        {
                            condensedChildren.Add(result);
                        }
                        else
                        {
                            PerformTypeConversions(symbolList);
                        }
                    }

                    if (condensedChildren.Count > 0)
                    {
                        var itemList = new List <object>();
                        item.value = itemList;
                        foreach (var child in (List <object>)condensedChildren)
                        {
                            var childSymbol = (SymbolEntry)child;
                            itemList.Add(childSymbol.value);
                        }
                        item.subType = ((SymbolEntry)(condensedChildren.First())).type;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void WriteArray(SymbolEntry entry, BinaryWriter bw)
        {
            List <object> items = (List <object>)entry.value;

            bw.Write((byte)entry.subType.Value);
            bw.Write((System.UInt16)items.Count);
            for (int i = 0; i < items.Count; i++)
            {
                var subSymbol = new SymbolEntry();
                subSymbol.type  = entry.subType.Value;
                subSymbol.value = items[i];
                WriteSymbolValue(subSymbol, bw);
            }
        }
Ejemplo n.º 3
0
        private void WriteSymbolEntry(JsonWriter writer, SymbolEntry value)
        {
            if (value.type == ESymbolType.ESYMBOLTYPE_NAME)  //write as flag
            {
                if (value.name.ToString().Equals("0"))
                {
                    writer.WritePropertyName(value.value.ToString());
                    writer.WriteValue(true);
                    return;
                }
            }
            var propertyName = value.name.ToString();

            writer.WritePropertyName(propertyName);
            if (CanConvert(value.GetType()))
            {
                WriteJson(writer, value, null);
            }
            else if (value.type == ESymbolType.ESYMBOLTYPE_ARRAY)
            {
                WriteSymbolArray(writer, value.value, value);
            }
            else if (value.type == ESymbolType.ESYMBOLTYPE_NAME)
            {
                WriteNameValue(writer, value.value);
            }
            else if (value.type == ESymbolType.ESYMBOLTYPE_STRUCTURE)
            {
                WriteSymbolList(writer, (List <SymbolEntry>)value.value);
            }
            else if (value.type == ESymbolType.ESYMBOLTYPE_VECTOR || value.type == ESymbolType.ESYMBOLTYPE_PAIR)
            {
                WriteVector(writer, value.value, value);
            }
            else
            {
                writer.WriteValue(value.value);
            }
        }
Ejemplo n.º 4
0
        SymbolEntry CondenseStructureToSymbol(List <SymbolEntry> list)
        {
            SymbolEntry entry        = new SymbolEntry();
            var         typeProperty = list.Where(s => s.name.ToString().Equals("type")).FirstOrDefault();

            if (typeProperty != null)
            {
                var typeName = typeProperty.value;
                if (typeName.ToString().Equals("name"))  //handle name
                {
                    var nameValue = list.Where(s => s.name.ToString().Equals("name") || s.name.ToString().Equals("checksum")).FirstOrDefault();
                    entry.value = nameValue?.value ?? 0;
                    entry.type  = ESymbolType.ESYMBOLTYPE_NAME;
                }
                else if (typeName.ToString().Equals("vec2") || typeName.ToString().Equals("vec3"))
                {
                    var vecList = (SymbolEntry)list.Where(s => s.name.ToString().Equals("value")).FirstOrDefault();
                    entry = vecList;
                    List <object> children = (List <object>)vecList.value;
                    if (children.Count == 2)
                    {
                        entry.type = ESymbolType.ESYMBOLTYPE_PAIR;
                    }
                    else
                    {
                        entry.type = ESymbolType.ESYMBOLTYPE_VECTOR;
                    }
                }
                else
                {
                    return(null);
                }
                return(entry);
            }
            return(null);
        }
Ejemplo n.º 5
0
        private void WriteSymbol(SymbolEntry symbol, BinaryWriter bw)
        {
            byte type = (byte)symbol.type;

            if (symbol.compressedByteSize == 1)
            {
                type |= (byte)BIT_NAME_LOOKUP.MASK_8_BIT_NAME_LOOKUP;
            }
            else if (symbol.compressedByteSize == 2)
            {
                type |= (byte)BIT_NAME_LOOKUP.MASK_16_BIT_NAME_LOOKUP;
            }
            bw.Write(type);
            System.UInt32 checksum;
            if (System.UInt32.TryParse(symbol.name.ToString(), out checksum))
            {
                if (symbol.compressedByteSize == 1)
                {
                    bw.Write((byte)checksum);
                }
                else if (symbol.compressedByteSize == 2)
                {
                    bw.Write((System.UInt16)checksum);
                }
                else
                {
                    bw.Write(checksum);
                }
            }
            else
            {
                checksum = crc32.Get(Encoding.ASCII.GetBytes(symbol.name.ToString().ToLower()));
                bw.Write(checksum);
            }
            WriteSymbolValue(symbol, bw);
        }
Ejemplo n.º 6
0
        private SymbolEntry ReadSymbolEntry(JsonReader reader, string propertyName)
        {
            SymbolEntry result = new SymbolEntry();

            result.name = propertyName;
            if (System.Int64.TryParse(propertyName, out System.Int64 checksum))
            {
                result.name = checksum;
            }
            result.compressedByteSize = null;

            result.type    = GetSymbolTypeFromToken(reader);
            result.subType = ESymbolType.ESYMBOLTYPE_NONE;
            int depth = reader.Depth;

            if (result.type == ESymbolType.ESYMBOLTYPE_ARRAY)
            {
                var list           = new List <object>();
                int lastPrecedence = 0;
                do
                {
                    reader.Read();
                    if (reader.TokenType == JsonToken.StartObject)
                    {
                        var symbolList = ReadSymbolList(reader, reader.Depth);
                        list.Add(symbolList);
                        result.subType = ESymbolType.ESYMBOLTYPE_STRUCTURE;
                        lastPrecedence = 4;
                    }
                    else if (reader.TokenType != JsonToken.EndArray)
                    {
                        list.Add(reader.Value);
                        ESymbolType type = GetSymbolTypeFromToken(reader, false, false);

                        int precedence = GetSymbolTypePrecedence(type);

                        if (precedence > lastPrecedence)
                        {
                            result.subType = type;
                            lastPrecedence = precedence;
                        }
                    }
                } while(reader.TokenType != JsonToken.EndArray || reader.Depth > depth);


                result.value = list;
            }
            else if (result.type == ESymbolType.ESYMBOLTYPE_STRUCTURE)
            {
                result.value = ReadSymbolList(reader, depth);
            }
            else if (result.type == ESymbolType.ESYMBOLTYPE_NAME && reader.ValueType == typeof(bool))
            {
                result.value = result.name;
                result.name  = (System.Int64) 0;
            }
            else
            {
                result.value = reader.Value;
            }


            return(result);
        }
Ejemplo n.º 7
0
        private void WriteSymbolValue(SymbolEntry entry, BinaryWriter bw)
        {
            List <object> list = null;

            switch (entry.type)
            {
            case ESymbolType.ESYMBOLTYPE_NONE:
            case ESymbolType.ESYMBOLTYPE_ZERO_FLOAT:
            case ESymbolType.ESYMBOLTYPE_ZERO_INTEGER:
                break;

            case ESymbolType.ESYMBOLTYPE_INTEGER_ONE_BYTE:
                bw.Write(sbyte.Parse(entry.value.ToString()));
                break;

            case ESymbolType.ESYMBOLTYPE_UNSIGNED_INTEGER_ONE_BYTE:
                bw.Write(byte.Parse(entry.value.ToString()));
                break;

            case ESymbolType.ESYMBOLTYPE_INTEGER_TWO_BYTES:
                System.Int16 _sVal;
                if (System.Int16.TryParse(entry.value.ToString(), out _sVal))
                {
                    bw.Write(_sVal);
                }
                break;

            case ESymbolType.ESYMBOLTYPE_UNSIGNED_INTEGER_TWO_BYTES:
                System.UInt16 _usVal;
                if (System.UInt16.TryParse(entry.value.ToString(), out _usVal))
                {
                    bw.Write(_usVal);
                }
                break;

            case ESymbolType.ESYMBOLTYPE_FLOAT:
                bw.Write(float.Parse(entry.value.ToString()));
                break;

            case ESymbolType.ESYMBOLTYPE_VECTOR:
                list = (List <object>)entry.value;
                for (int i = 0; i < 3; i++)
                {
                    if (list[i].GetType() != typeof(System.Single))
                    {
                        bw.Write(float.Parse(list[i].ToString()));
                    }
                    else
                    {
                        bw.Write((float)list[i]);
                    }
                }
                break;

            case ESymbolType.ESYMBOLTYPE_PAIR:
                list = (List <object>)entry.value;
                for (int i = 0; i < 2; i++)
                {
                    if (list[i].GetType() != typeof(System.Single))
                    {
                        bw.Write(float.Parse(list[i].ToString()));
                    }
                    else
                    {
                        bw.Write((float)list[i]);
                    }
                }
                break;

            case ESymbolType.ESYMBOLTYPE_STRING:
                bw.Write(Encoding.ASCII.GetBytes(entry.value.ToString()));
                bw.Write((byte)0);
                break;

            case ESymbolType.ESYMBOLTYPE_STRUCTURE:
                var lst        = (IEnumerable <object>)entry.value;
                var structList = new List <SymbolEntry>();
                foreach (var structItem in lst)
                {
                    structList.Add((SymbolEntry)structItem);
                }
                SerializeBuffer(bw, structList);
                break;

            case ESymbolType.ESYMBOLTYPE_ARRAY:
                WriteArray(entry, bw);
                break;

            case ESymbolType.ESYMBOLTYPE_NAME:
                bw.Write(System.UInt32.Parse(entry.value.ToString()));
                break;

            case ESymbolType.ESYMBOLTYPE_INTEGER:
                bw.Write(System.Int32.Parse(entry.value.ToString()));
                break;

            default:
                throw new ArgumentException("Got unhandled symbol", entry.type.ToString());
            }
        }