Beispiel #1
0
        public List <Data.Module> FromProtocol(Messages.Protocol protocol)
        {
            var modules = new List <Data.Module>();
            var types   = new Dictionary <string, Data.Type>();

            var protocolModule = new Data.Module(protocol.Name);

            modules.Add(protocolModule);

            foreach (var protocolType in protocol.Types)
            {
                BuildType(protocol, protocolType, protocolModule, 0);
            }

            foreach (var protocolMessage in protocol.Messages)
            {
                var messageAsType = new Messages.Type
                {
                    Fields  = protocolMessage.Fields,
                    Name    = protocolMessage.Name,
                    NumBits = 0
                };
                BuildType(protocol, messageAsType, protocolModule, 0);
            }

            return(modules);
        }
Beispiel #2
0
        public void BuildType(Messages.Protocol protocol, Messages.Type protoType, Data.Module module, int depth)
        {
            if (module.GetType(protoType.Name) != null)
            {
                return;
            }

            if (depth == 100)
            {
                // Save to assume this is nested waaaay too deep
                throw new Exception($"Too much type nesting in protocol {protocol.Name}");
            }

            if (protoType.Fields != null && protoType.Fields.Count > 0)
            {
                // Ensure all types of our fields exist
                int bitOffset = 0;
                var fields    = new List <Data.Field>();
                foreach (var field in protoType.Fields)
                {
                    var fieldType = protocol.Types.FirstOrDefault(t => t.Name == field.Type);
                    if (fieldType == null)
                    {
                        throw new Exception($"Missing type {field.Type} in protocol {protocol.Name}");
                    }
                    BuildType(protocol, fieldType, module, depth + 1);

                    var type = module.GetType(field.Type);
                    fields.Add(new Data.Field(type, field.Name, bitOffset / 8, bitOffset % 8));
                    bitOffset += type.SizeInBits;
                }
                // Create our type
                module.AddType(new Data.Type(module, protoType.Name, fields));
            }
            else
            {
                // Create our type
                module.AddType(new Data.Type(module, protoType.Name, protoType.NumBits));
            }
        }