static VBlock CollapseEntity(VBlock entity, int fixupStyle, string instanceName, Vector3 instanceOrigin, Vector3 instanceAngles, ref int highID) { VBlock collapsedEntity = entity.DeepClone(); var targetNameProperty = collapsedEntity.Body.Where(node => node.Name == "targetname" && node.GetType() == typeof(VProperty)).Select(node => node as VProperty).FirstOrDefault(); var solids = entity.Body.Where(node => node.Name == "solid" && node is VBlock).Cast<VBlock>(); var relativeEntityPositionProperty = collapsedEntity.Body.Where(node => node.Name == "origin" && node.GetType() == typeof(VProperty)).Select(node => node as VProperty).FirstOrDefault(); var relativeEntityAngleProperty = collapsedEntity.Body.Where(node => node.Name == "angles" && node.GetType() == typeof(VProperty)).Select(node => node as VProperty).FirstOrDefault(); var relativeEntityPosition = new Vector3(relativeEntityPositionProperty?.Value ?? "0 0 0"); var relativeEntityAngle = new Vector3(relativeEntityAngleProperty?.Value ?? "0 0 0"); if (relativeEntityPositionProperty != null) { var newEntityPosition = relativeEntityPosition.Clone(); newEntityPosition.Rotate(instanceAngles); newEntityPosition.Offset(instanceOrigin); relativeEntityPositionProperty.Value = newEntityPosition.ToString(); } if (relativeEntityAngleProperty != null) { var newEntityAngle = relativeEntityAngle.Clone(); newEntityAngle.AddAngles(instanceAngles); relativeEntityAngleProperty.Value = newEntityAngle.ToString(); } collapsedEntity.ReID(ref highID); // Collapse the entity's sub solids foreach (var solid in solids) { entity.Body[entity.Body.IndexOf(solid)] = CollapseSolid(solid, ref highID); } // Rename Entity if (targetNameProperty != null && !targetNameProperty.Value.Contains("@")) { if (fixupStyle == 0) targetNameProperty.Value = instanceName + "-" + targetNameProperty.Value; // Prefix else if (fixupStyle == 1) targetNameProperty.Value = targetNameProperty.Value + "-" + instanceName; // Postfix } return collapsedEntity; }
/** * 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); }
static VBlock CollapseSolid(VBlock solid, ref int highID) { // TODO: transform solid return solid.DeepClone(); }
internal static bool Mod_COOPChanges() { bool hasChanged = false; //If is coop, add point entity where the elevator instance is. var coop_exit = instances.Where(instance => instance.Body.Where(property => property.Name == "file" && property.GetType() == typeof(VProperty) && ((VProperty)property).Value.EndsWith("coop_exit.vmf")) .Count() == 1).FirstOrDefault(); if (coop_exit != null) { //Then this must be coop var coop_exit_origin = coop_exit.Body.Where(property => property.Name == "origin" && property.GetType() == typeof(VProperty)).FirstOrDefault() as VProperty; if (coop_exit_origin == null) { Console.WriteLine("We have a coop exit, with no origin?"); return false; } #region Add our special entity!!! var editorVMF = new string[]{ "editor", "{", "\"color\" \"220 30 220\"", "\"visgroupshown\" \"1\"", "\"visgroupautoshown\" \"1\"", "\"logicalpos\" \"[0 0]\"", "}" }; var entity = new VBlock("entity", new List<IVNode>() { new VProperty("id", vmf.GetUniqueID().ToString()), new VProperty("classname", "info_target"), new VProperty("angles", "0 0 0"), new VProperty("targetname", "supress_blue_portalgun_spawn"), new VProperty("origin", coop_exit_origin.Value), new VBlock(new string[]{ "editor", "{", "\"color\" \"220 30 220\"", "\"visgroupshown\" \"1\"", "\"visgroupautoshown\" \"1\"", "\"logicalpos\" \"[0 0]\"", "}" }) }); vmf.Body.Add(entity); entities.Add(entity); #endregion #region Swap all singleplayer instances for coop instances. foreach (var instance in instances) { var file = instance.Body.FirstOrDefault(property => property.GetType() == typeof(VProperty) && property.Name == "file") as VProperty; if (file.Value.EndsWith("_sp.vmf")) file.Value = file.Value.Replace("_sp.vmf", "_coop.vmf"); } #endregion hasChanged = true; } return hasChanged; }