Exemplo n.º 1
0
        protected void RebuildBlockIDTableRecursive(VBlock container)
        {
            VProperty idProperty = container["id"].VProperty();

            if (idProperty != null)
            {
                RegisterID(idProperty.GetValueAsInt(), container);
            }

            foreach (var node in container.Body.WhereClass <VBlock>())
            {
                RebuildBlockIDTableRecursive(node);
            }
        }
Exemplo n.º 2
0
        /**
         * Imports a VMF file to create a NailsMap. Uses the file name passed in when this adapter was constructed.
         * <author>1upD</author>
         * TODO Add more error checking
         */
        public NailsMap Import()
        {
            try {
                // Reset adapter VMF
                this._vmf = new VMF();


                NailsMap map = new NailsMap();

                List <string> lines = new List <string>();

                using (StreamReader sr = new StreamReader(new FileStream(this._filename, FileMode.OpenOrCreate)))
                {
                    while (!sr.EndOfStream)
                    {
                        lines.Add(sr.ReadLine());
                    }
                }


                VMF input_vmf = new VMF(lines.ToArray());

                for (int blockIndex = 0; blockIndex < input_vmf.Body.Count; blockIndex++)
                {
                    // Should this object be included when the VMF is output?
                    bool includeThisBlock = true;
                    // Get the next object from the VMF
                    var obj = input_vmf.Body[blockIndex];

                    try
                    {
                        // Try to cast to block
                        VMFParser.VBlock block = (VMFParser.VBlock)obj;


                        // If this block is an entity
                        if (block.Name == "entity")
                        {
                            var body = block.Body;
                            foreach (var innerObj in body)
                            {
                                try
                                {
                                    VMFParser.VProperty prop = (VMFParser.VProperty)innerObj;

                                    // If this block is an instance
                                    if (prop.Name == "classname" && prop.Value == "func_instance")
                                    {
                                        VProperty fileProperty  = (VProperty)body.Where(p => p.Name == "file").ToList()[0];
                                        var       filePathParts = fileProperty.Value.Split('/');

                                        // If this is a nails instance
                                        if (filePathParts[0] == "Nails")
                                        {
                                            // Get position
                                            VProperty originProperty = (VProperty)body.Where(p => p.Name == "origin").ToList()[0];
                                            var       originParts    = originProperty.Value.Split(' ');
                                            int       x_pos          = int.Parse(originParts[0]) / this._horizontal_scale;
                                            int       y_pos          = int.Parse(originParts[1]) / this._horizontal_scale;
                                            int       z_pos          = int.Parse(originParts[2]) / this._vertical_scale;
                                            string    style          = filePathParts[1];
                                            map.MarkLocation(style, x_pos, y_pos, z_pos);

                                            // Remove this block from the vmf
                                            includeThisBlock = false;
                                        }

                                        break;
                                    }
                                } catch (InvalidCastException e)
                                {
                                    log.Error("Invalid cast exception. VMF Object is not a VProperty.", e);
                                }
                            }
                        }
                    } catch (InvalidCastException e)
                    {
                        log.Error("Invalid cast exception. VMF object is not a VBlock.", e);
                    }
                    // If this object is not a Nails block, add it to the adapter's VMF to be output later
                    if (includeThisBlock)
                    {
                        this._vmf.Body.Add(obj);
                    }
                }

                return(map);
            } catch (Exception e)
            {
                log.Error("VMFAdapter.Import(): Caught exception: ", e);
                log.Info(string.Format("Failed to read from VMF file: {0}", this._filename));
            }

            return(null);
        }