public static idMapBrush ParseQ3(idLexer lexer, Vector3 origin) { int rotate; int[] shift = new int[2]; float[] scale = new float[2]; Vector3[] planePoints = new Vector3[3]; List<idMapBrushSide> sides = new List<idMapBrushSide>(); idMapBrushSide side; idToken token; do { if(lexer.CheckTokenString("}") == true) { break; } side = new idMapBrushSide(); sides.Add(side); // read the three point plane definition float[] tmp = lexer.Parse1DMatrix(3); float[] tmp2 = lexer.Parse1DMatrix(3); float[] tmp3 = lexer.Parse1DMatrix(3); if((tmp == null) || (tmp2 == null) || (tmp3 == null)) { lexer.Error("idMapBrush::ParseQ3: unable to read brush side plane definition"); return null; } planePoints[0] = new Vector3(tmp[0], tmp[1], tmp[2]) - origin; planePoints[1] = new Vector3(tmp2[0], tmp2[1], tmp2[2]) - origin; planePoints[2] = new Vector3(tmp3[0], tmp3[1], tmp3[2]) - origin; side.Plane.FromPoints(planePoints[0], planePoints[1], planePoints[2]); // read the material token = lexer.ReadTokenOnLine(); if(token == null) { lexer.Error("idMapBrush::ParseQ3: unable to read brush side material"); return null; } // we have an implicit 'textures/' in the old format side.Material = "textures/" + token.ToString(); // read the texture shift, rotate and scale shift[0] = lexer.ParseInt(); shift[1] = lexer.ParseInt(); rotate = lexer.ParseInt(); scale[0] = lexer.ParseFloat(); scale[1] = lexer.ParseFloat(); side.TextureMatrix[0] = new Vector3(0.03125f, 0.0f, 0.0f); side.TextureMatrix[1] = new Vector3(0.0f, 0.03125f, 0.0f); side.Origin = origin; // Q2 allowed override of default flags and values, but we don't any more if(lexer.ReadTokenOnLine() != null) { if(lexer.ReadTokenOnLine() != null) { if(lexer.ReadTokenOnLine() != null) { } } } } while(true); idMapBrush brush = new idMapBrush(); for(int i = 0; i < sides.Count; i++) { brush.AddSide(sides[i]); } brush.Dict = new idDict(); return brush; }
public static idMapEntity Parse(idLexer lexer, bool isWordSpawn = false, float version = idMapFile.CurrentMapVersion) { idToken token; if ((token = lexer.ReadToken()) == null) { return(null); } if (token.ToString() != "{") { lexer.Error("idMapEntity.Parse: {{ not found, found {0}", token.ToString()); return(null); } idMapEntity mapEnt = new idMapEntity(); idMapBrush mapBrush = null; idMapPatch mapPatch = null; Vector3 origin = Vector3.Zero; bool worldEnt = false; string tokenValue; do { if ((token = lexer.ReadToken()) == null) { lexer.Error("idMapEntity.Parse: EOF without closing brace"); return(null); } if (token.ToString() == "}") { break; } if (token.ToString() == "{") { // parse a brush or patch if ((token = lexer.ReadToken()) == null) { lexer.Error("idMapEntity.Parse: unexpected EOF"); return(null); } if (worldEnt == true) { origin = Vector3.Zero; } tokenValue = token.ToString(); // if is it a brush: brush, brushDef, brushDef2, brushDef3 if (tokenValue.StartsWith("brush", StringComparison.OrdinalIgnoreCase) == true) { mapBrush = idMapBrush.Parse(lexer, origin, (tokenValue.Equals("brushDef2", StringComparison.OrdinalIgnoreCase) || tokenValue.Equals("brushDef3", StringComparison.OrdinalIgnoreCase)), version); if (mapBrush == null) { return(null); } mapEnt.AddPrimitive(mapBrush); } // if is it a patch: patchDef2, patchDef3 else if (tokenValue.StartsWith("patch", StringComparison.OrdinalIgnoreCase) == true) { mapPatch = idMapPatch.Parse(lexer, origin, tokenValue.Equals("patchDef3", StringComparison.OrdinalIgnoreCase), version); if (mapPatch == null) { return(null); } mapEnt.AddPrimitive(mapPatch); } // assume it's a brush in Q3 or older style else { lexer.UnreadToken = token; mapBrush = idMapBrush.ParseQ3(lexer, origin); if (mapBrush == null) { return(null); } mapEnt.AddPrimitive(mapBrush); } } else { // parse a key / value pair string key = token.ToString(); token = lexer.ReadTokenOnLine(); string value = token.ToString(); // strip trailing spaces that sometimes get accidentally added in the editor value = value.Trim(); key = key.Trim(); mapEnt.Dict.Set(key, value); if (key.Equals("origin", StringComparison.OrdinalIgnoreCase) == true) { // scanf into doubles, then assign, so it is idVec size independent string[] parts = value.Split(' '); float.TryParse(parts[0], out origin.X); float.TryParse(parts[1], out origin.Y); float.TryParse(parts[2], out origin.Z); } else if ((key.Equals("classname", StringComparison.OrdinalIgnoreCase) == true) && (value.Equals("worldspawn", StringComparison.OrdinalIgnoreCase) == true)) { worldEnt = true; } } }while(true); return(mapEnt); }
public static idMapBrush Parse(idLexer lexer, Vector3 origin, bool newFormat = true, float version = idMapFile.CurrentMapVersion) { idToken token; idMapBrushSide side; List<idMapBrushSide> sides = new List<idMapBrushSide>(); idDict dict = new idDict(); Vector3[] planePoints = new Vector3[3]; if(lexer.ExpectTokenString("{") == false) { return null; } do { if((token = lexer.ReadToken()) == null) { lexer.Error("idMapBrush::Parse: unexpected EOF"); return null; } if(token.ToString() == "}") { break; } // here we may have to jump over brush epairs ( only used in editor ) do { // if token is a brace if(token.ToString() == "(") { break; } // the token should be a key string for a key/value pair if(token.Type != TokenType.String) { lexer.Error("idMapBrush::Parse: unexpected {0}, expected ( or epair key string", token.ToString()); return null; } string key = token.ToString(); if(((token = lexer.ReadTokenOnLine()) == null) || (token.Type != TokenType.String)) { lexer.Error("idMapBrush::Parse: expected epair value string not found"); return null; } dict.Set(key, token.ToString()); // try to read the next key if((token = lexer.ReadToken()) == null) { lexer.Error("idMapBrush::Parse: unexpected EOF"); return null; } } while(true); lexer.UnreadToken = token; side = new idMapBrushSide(); sides.Add(side); if(newFormat == true) { float[] tmp = lexer.Parse1DMatrix(4); if(tmp == null) { lexer.Error("idMapBrush::Parse: unable to read brush side plane definition"); return null; } else { side.Plane = new Plane(tmp[0], tmp[1], tmp[2], tmp[3]); } } else { // read the three point plane definition float[] tmp, tmp2, tmp3; if(((tmp = lexer.Parse1DMatrix(3)) == null) || ((tmp2 = lexer.Parse1DMatrix(3)) == null) || ((tmp3 = lexer.Parse1DMatrix(3)) == null)) { lexer.Error("idMapBrush::Parse: unable to read brush side plane definition"); return null; } planePoints[0] = new Vector3(tmp[0], tmp[1], tmp[2]) - origin; planePoints[1] = new Vector3(tmp2[0], tmp2[1], tmp2[2]) - origin; planePoints[2] = new Vector3(tmp3[0], tmp3[1], tmp3[2]) - origin; side.Plane.FromPoints(planePoints[0], planePoints[1], planePoints[2]); } // read the texture matrix // this is odd, because the texmat is 2D relative to default planar texture axis float[,] tmp5 = lexer.Parse2DMatrix(2, 3); if(tmp5 == null) { lexer.Error("idMapBrush::Parse: unable to read brush side texture matrix"); return null; } side.TextureMatrix[0] = new Vector3(tmp5[0, 0], tmp5[0, 1], tmp5[0, 2]); side.TextureMatrix[1] = new Vector3(tmp5[1, 0], tmp5[1, 1], tmp5[1, 2]); side.Origin = origin; // read the material if((token = lexer.ReadTokenOnLine()) == null) { lexer.Error("idMapBrush::Parse: unable to read brush side material"); return null; } // we had an implicit 'textures/' in the old format... if(version < 2.0f) { side.Material = "textures/" + token.ToString(); } else { side.Material = token.ToString(); } // Q2 allowed override of default flags and values, but we don't any more if(lexer.ReadTokenOnLine() != null) { if(lexer.ReadTokenOnLine() != null) { if(lexer.ReadTokenOnLine() != null) { } } } } while(true); if(lexer.ExpectTokenString("}") == false) { return null; } idMapBrush brush = new idMapBrush(); foreach(idMapBrushSide s in sides) { brush.AddSide(s); } brush.Dict = dict; return brush; }
/// <summary> /// /// </summary> /// <remarks> /// Normally this will use a .reg file instead of a .map file if it exists, /// which is what the game and dmap want, but the editor will want to always /// load a .map file. /// </remarks> /// <param name="fileName">Does not require an extension.</param> /// <param name="ignoreRegion"></param> /// <param name="osPath"></param> /// <returns></returns> public bool Parse(string fileName, bool ignoreRegion = false, bool osPath = false) { if (this.Disposed == true) { throw new ObjectDisposedException(this.GetType().Name); } _hasPrimitiveData = false; _name = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName)); string fullName = _name; // no string concatenation for epairs and allow path names for materials idLexer lexer = new idLexer(LexerOptions.NoStringConcatination | LexerOptions.NoStringEscapeCharacters | LexerOptions.AllowPathNames); idMapEntity mapEnt; if (ignoreRegion == false) { // try loading a .reg file first lexer.LoadFile(fullName + ".reg", osPath); } if (lexer.IsLoaded == false) { // now try a .map file lexer.LoadFile(fullName + ".map", osPath); if (lexer.IsLoaded == false) { // didn't get anything at all return(false); } } _version = idMapFile.OldMapVersion; _fileTime = lexer.FileTime; _entities.Clear(); if (lexer.CheckTokenString("Version") == true) { _version = lexer.ReadTokenOnLine().ToFloat(); } while (true) { if ((mapEnt = idMapEntity.Parse(lexer, (_entities.Count == 0), _version)) == null) { break; } _entities.Add(mapEnt); } idConsole.Warning("TODO: SetGeometryCRC();"); // if the map has a worldspawn if (_entities.Count > 0) { // "removeEntities" "classname" can be set in the worldspawn to remove all entities with the given classname foreach (KeyValuePair <string, string> removeEntities in _entities[0].Dict.MatchPrefix("removeEntities")) { RemoveEntities(removeEntities.Value); } // "overrideMaterial" "material" can be set in the worldspawn to reset all materials string material; int entityCount = _entities.Count; int primitiveCount = 0; int sideCount = 0; if ((material = (_entities[0].Dict.GetString("overrideMaterial", ""))) != string.Empty) { for (int i = 0; i < entityCount; i++) { mapEnt = _entities[i]; primitiveCount = mapEnt.Primitives.Count; for (int j = 0; j < primitiveCount; j++) { idMapPrimitive mapPrimitive = mapEnt.GetPrimitive(j); switch (mapPrimitive.Type) { case MapPrimitiveType.Brush: idMapBrush mapBrush = (idMapBrush)mapPrimitive; sideCount = mapBrush.SideCount; for (int k = 0; k < sideCount; k++) { mapBrush.GetSide(k).Material = material; } break; case MapPrimitiveType.Patch: idConsole.Warning("TODO: PATCH"); // TODO: ((idMapPatch) mapPrimitive).Material = material; break; } } } } // force all entities to have a name key/value pair if (_entities[0].Dict.GetBool("forceEntityNames") == true) { for (int i = 1; i < entityCount; i++) { mapEnt = _entities[i]; if (mapEnt.Dict.ContainsKey("name") == false) { mapEnt.Dict.Set("name", string.Format("{0}{1}", mapEnt.Dict.GetString("classname", "forcedName"), i)); } } } // move the primitives of any func_group entities to the worldspawn if (_entities[0].Dict.GetBool("moveFuncGroups") == true) { for (int i = 1; i < entityCount; i++) { mapEnt = _entities[i]; if (mapEnt.Dict.GetString("classname").ToLower() == "func_group") { _entities[0].Primitives.AddRange(mapEnt.Primitives); mapEnt.Primitives.Clear(); } } } } _hasPrimitiveData = true; return(true); }