Example #1
0
        public void VMMCollapse(string output)
        {
            Console.WriteLine("collapsing VMM");
            // find the topLevel vmf
            List <Dictionary <string, string> > topLevels = vmfs.FindAll(dic => dic.Keys.Contains("TopLevel"));

            if (topLevels.Count == 0)
            {
                TopLevelVmf = vmfs.First();
            }
            else
            {
                TopLevelVmf = topLevels.Aggregate((curMin, x) => (curMin == null || int.Parse(x["TopLevel"]) < int.Parse(curMin["topLevel"]) ? x : curMin));
            }
            vmfs.Remove(TopLevelVmf);

            //start file
            VMFFile topmap = new VMFFile(vmfdir + "/" + TopLevelVmf["File"]);

            VMFVector3Value originVal = new VMFVector3Value {
                X = 0, Y = 0, Z = 0
            };
            VMFVector3Value anglesVal = new VMFVector3Value {
                Pitch = 0, Roll = 0, Yaw = 0
            };
            VMFNumberValue fixup_styleVal = new VMFNumberValue {
                Value = 0
            };

            foreach (Dictionary <string, string> vmf in vmfs)
            {
                Console.WriteLine("Inserting submap of {0}", vmfdir + "/" + vmf["File"]);

                VMFFile submap = new VMFFile(vmfdir + "/" + vmf["File"]);

                foreach (VMFStructure worldStruct in submap.World)
                {
                    if (worldStruct.Type == VMFStructureType.Group || worldStruct.Type == VMFStructureType.Solid)
                    {
                        VMFStructure clone = worldStruct.Clone(topmap.LastID, topmap.LastNodeID);
                        clone.Transform(originVal, anglesVal);
                        topmap.World.Structures.Add(clone);
                    }
                }

                foreach (VMFStructure rootStruct in submap.Root)
                {
                    if (rootStruct.Type == VMFStructureType.Entity)
                    {
                        VMFStructure clone = rootStruct.Clone(topmap.LastID, topmap.LastNodeID);
                        clone.Transform(originVal, anglesVal);
                        topmap.Root.Structures.Add(clone);
                    }
                }
                topmap.updateIds();
            }
            topmap.ResolveInstances();
            topmap.Save(vmfdir + ".vmf");
        }
Example #2
0
        public VMFFile(String path, String rootDir = null)
        {
            Console.WriteLine("Parsing " + path + "...");

            OriginalPath = (rootDir != null ? rootDir + Path.DirectorySeparatorChar : "") + path;

            if (!File.Exists(OriginalPath))
            {
                if (rootDir != null && path.Contains('/') && path.Substring(0, path.IndexOf('/')) == rootDir.Substring(rootDir.LastIndexOf('\\') + 1))
                {
                    OriginalPath = rootDir + Path.DirectorySeparatorChar + path.Substring(path.IndexOf('/') + 1);
                }

                if (!File.Exists(OriginalPath))
                {
                    Console.WriteLine("File \"" + path + "\" not found!");
                    return;
                }
            }

#if !DEBUG
            try {
#endif
            using (FileStream stream = new FileStream(OriginalPath, FileMode.Open, FileAccess.Read))
                Root = new VMFStructure("file", new StreamReader(stream));
#if !DEBUG
        }
        catch (Exception e)
        {
            Console.WriteLine("Error while parsing file!");
            Console.WriteLine(e.ToString());
            return;
        }
#endif

            foreach (VMFStructure stru in Root)
            {
                if (stru.Type == VMFStructureType.World)
                {
                    World = stru;
                    break;
                }
            }

            LastID     = Root.GetLastID();
            LastNodeID = Root.GetLastNodeID();

            stVMFCache.Add(path, this);
        }
Example #3
0
        public void ResolveInstances()
        {
            Console.WriteLine("Resolving instances for " + OriginalPath + "...");
            List <VMFStructure> structures = Root.Structures;

            int autoName = 0;

            for (int i = structures.Count - 1; i >= 0; --i)
            {
                VMFStructure structure = structures[i];

                if (structure.Type == VMFStructureType.Entity)
                {
                    VMFValue classnameVal = structure["classname"];

                    if (classnameVal != null)
                    {
                        switch (classnameVal.String)
                        {
                        case "func_instance":
                            structures.RemoveAt(i);

                            VMFStringValue  fileVal   = structure["file"] as VMFStringValue;
                            VMFVector3Value originVal = (structure["origin"] as VMFVector3Value) ?? new VMFVector3Value {
                                X = 0, Y = 0, Z = 0
                            };
                            VMFVector3Value anglesVal = (structure["angles"] as VMFVector3Value) ?? new VMFVector3Value {
                                Pitch = 0, Roll = 0, Yaw = 0
                            };
                            VMFNumberValue fixup_styleVal = (structure["fixup_style"] as VMFNumberValue) ?? new VMFNumberValue {
                                Value = 0
                            };
                            VMFValue targetnameVal = structure["targetname"];

                            Regex pattern = new Regex("^replace[0-9]*$");
                            List <KeyValuePair <String, String> > replacements    = new List <KeyValuePair <String, String> >();
                            List <KeyValuePair <String, String> > matReplacements = new List <KeyValuePair <String, String> >();

                            foreach (KeyValuePair <String, VMFValue> keyVal in structure.Properties)
                            {
                                if (pattern.IsMatch(keyVal.Key))
                                {
                                    String[] split = keyVal.Value.String.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    if (split.Length < 1)
                                    {
                                        continue;
                                    }

                                    if (split[0].StartsWith("#"))
                                    {
                                        matReplacements.Add(new KeyValuePair <String, String>(split[0].Substring(1).Trim(), keyVal.Value.String.Substring(split[0].Length + 1).Trim()));
                                        continue;
                                    }

                                    if (!split[0].StartsWith("$"))
                                    {
                                        Console.WriteLine("Invalid property replacement name \"{0}\" - needs to begin with a $", split[0]);
                                        continue;
                                    }

                                    replacements.Add(new KeyValuePair <String, String>(split[0].Trim(), keyVal.Value.String.Substring(split[0].Length + 1).Trim()));
                                }
                            }

                            replacements    = replacements.OrderByDescending(x => x.Key.Length).ToList();
                            matReplacements = matReplacements.OrderByDescending(x => x.Key.Length).ToList();

                            TargetNameFixupStyle fixupStyle = (TargetNameFixupStyle)fixup_styleVal.Value;
                            String targetName = (targetnameVal != null ? targetnameVal.String : null);

                            if (fixupStyle != TargetNameFixupStyle.None && targetName == null)
                            {
                                targetName = "AutoInstance" + (autoName++);
                            }

                            if (fileVal == null)
                            {
                                Console.WriteLine("Invalid instance at (" + originVal.String + ")");
                                continue;
                            }

                            Console.WriteLine("Inserting instance of {0} at ({1}), ({2})", fileVal.String, originVal.String, anglesVal.String);

                            String  file = fileVal.String;
                            VMFFile vmf  = null;

                            if (stVMFCache.ContainsKey(file))
                            {
                                vmf = stVMFCache[file];
                            }
                            else
                            {
                                vmf = new VMFFile(file, Path.GetDirectoryName(OriginalPath));
                                if (vmf.Root != null)
                                {
                                    vmf.ResolveInstances();
                                }
                            }

                            if (vmf.Root == null)
                            {
                                Console.WriteLine("Could not insert!");
                                continue;
                            }

                            foreach (VMFStructure worldStruct in vmf.World)
                            {
                                if (worldStruct.Type == VMFStructureType.Group || worldStruct.Type == VMFStructureType.Solid)
                                {
                                    VMFStructure clone = worldStruct.Clone(LastID, LastNodeID, fixupStyle, targetName, replacements, matReplacements);
                                    clone.Transform(originVal, anglesVal);
                                    World.Structures.Add(clone);
                                }
                            }

                            int index = i;

                            foreach (VMFStructure rootStruct in vmf.Root)
                            {
                                if (rootStruct.Type == VMFStructureType.Entity)
                                {
                                    VMFStructure clone = rootStruct.Clone(LastID, LastNodeID, fixupStyle, targetName, replacements, matReplacements);
                                    clone.Transform(originVal, anglesVal);
                                    Root.Structures.Insert(index++, clone);
                                }
                            }

                            LastID     = Root.GetLastID();
                            LastNodeID = Root.GetLastNodeID();
                            break;

                        case "func_instance_parms":
                            structures.RemoveAt(i);
                            break;
                        }
                    }
                }
            }

            Console.WriteLine("Instances resolved.");
        }
Example #4
0
        static void Main(string[] args)
        {
#if !DEBUG
            Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
#endif

            List <String> paths   = new List <string>();
            bool          cleanup = false;

            string[] fgdpaths = new string[0];

            for (int i = 0; i < args.Length; ++i)
            {
                string arg = args[i];
                if (!arg.StartsWith("-"))
                {
                    paths.Add(arg);
                }
                else
                {
                    switch (arg.Substring(1).ToLower())
                    {
                    case "c":
                    case "-cleanup":
                        cleanup = true;
                        break;

                    case "d":
                    case "-fgd":
                        fgdpaths = args[++i].Split(',').Select(x => x.Trim()).ToArray();
                        break;
                    }
                }
            }

            if (paths.Count < 1)
            {
                Console.WriteLine("Unexpected arguments. Aborting...");
                return;
            }

            String vmf      = paths[0];
            String rootName = Path.GetDirectoryName(vmf) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(vmf);
            String dest     = (paths.Count >= 2 ? paths[1] : rootName + ".temp.vmf");
            String del      = "Deleting ";
            String renaming = "Renaming {0} to {1}";

            if (cleanup)
            {
                if (File.Exists(dest))
                {
                    Console.WriteLine(del + dest);
                    File.Delete(dest);
                }

                String prt     = rootName + ".prt";
                String tempPrt = rootName + ".temp.prt";

                if (File.Exists(tempPrt))
                {
                    if (File.Exists(prt))
                    {
                        Console.WriteLine(del + prt);
                        File.Delete(prt);
                    }

                    Console.WriteLine(renaming, tempPrt, prt);
                    File.Move(tempPrt, prt);
                }

                String lin     = rootName + ".lin";
                String tempLin = rootName + ".temp.lin";

                if (File.Exists(lin))
                {
                    Console.WriteLine(del + lin);
                    File.Delete(lin);
                }

                if (File.Exists(tempLin))
                {
                    Console.WriteLine(renaming, tempLin, lin);
                    File.Move(tempLin, lin);
                }
            }
            else
            {
                foreach (String path in fgdpaths)
                {
                    VMFStructure.ParseFGD(path);
                }

                VMFFile file = new VMFFile(vmf);
                file.ResolveInstances();
                file.Save(dest);
            }

#if DEBUG
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
#endif
        }