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 idMapPatch Parse(idLexer lexer, Vector3 origin, bool patchDef3 = true, float version = idMapFile.CurrentMapVersion) { if(lexer.ExpectTokenString("{") == false) { return null; } // read the material (we had an implicit 'textures/' in the old format...) idToken token = lexer.ReadToken(); if(token == null) { lexer.Error("idMapPatch::Parse: unexpected EOF"); return null; } // Parse it float[] info; if(patchDef3 == true) { info = lexer.Parse1DMatrix(7); if(info == null) { lexer.Error("idMapPatch::Parse: unable to Parse patchDef3 info"); return null; } } else { info = lexer.Parse1DMatrix(5); if(info == null) { lexer.Error("idMapPatch::Parse: unable to parse patchDef2 info"); return null; } } idMapPatch patch = new idMapPatch((int) info[0], (int) info[1]); if(version < 2.0f) { patch.Material = "textures/" + token.ToString(); } else { patch.Material = token.ToString(); } if(patchDef3 == true) { patch.HorizontalSubdivisions = (int) info[2]; patch.VerticalSubdivisions = (int) info[3]; patch.ExplicitlySubdivided = true; } if((patch.Width < 0) || (patch.Height < 0)) { lexer.Error("idMapPatch::Parse: bad size"); return null; } // these were written out in the wrong order, IMHO if(lexer.ExpectTokenString("(") == false) { lexer.Error("idMapPatch::Parse: bad patch vertex data"); return null; } for(int j = 0; j < patch.Width; j++) { if(lexer.ExpectTokenString("(") == false) { lexer.Error("idMapPatch::Parse: bad vertex row data"); return null; } for(int i = 0; i < patch.Height; i++) { float[] v = lexer.Parse1DMatrix(5); if(v == null) { lexer.Error("idMapPatch::Parse: bad vertex column data"); return null; } Vertex vert = new Vertex(); vert.Position.X = v[0] - origin.X; vert.Position.Y = v[1] - origin.Y; vert.Position.Z = v[2] - origin.Z; vert.TextureCoordinates = new Vector2(v[3], v[4]); patch.SetVertex(i * patch.Width + j, vert); } if(lexer.ExpectTokenString(")") == false) { lexer.Error("idMapPatch::Parse: unable to parse patch control points"); return null; } } if(lexer.ExpectTokenString(")") == false) { lexer.Error("idMapPatch::Parse: unable to parse patch control points, no closure" ); return null; } // read any key/value pairs while((token = lexer.ReadToken()) != null) { if(token.ToString() == "}") { lexer.ExpectTokenString("}"); break; } if(token.Type == TokenType.String) { string key = token.ToString(); token = lexer.ExpectTokenType(TokenType.String, 0); patch.Dict.Set(key, token.ToString()); } } return patch; }
public static idMapPatch Parse(idLexer lexer, Vector3 origin, bool patchDef3 = true, float version = idMapFile.CurrentMapVersion) { if (lexer.ExpectTokenString("{") == false) { return(null); } // read the material (we had an implicit 'textures/' in the old format...) idToken token = lexer.ReadToken(); if (token == null) { lexer.Error("idMapPatch::Parse: unexpected EOF"); return(null); } // Parse it float[] info; if (patchDef3 == true) { info = lexer.Parse1DMatrix(7); if (info == null) { lexer.Error("idMapPatch::Parse: unable to Parse patchDef3 info"); return(null); } } else { info = lexer.Parse1DMatrix(5); if (info == null) { lexer.Error("idMapPatch::Parse: unable to parse patchDef2 info"); return(null); } } idMapPatch patch = new idMapPatch((int)info[0], (int)info[1]); if (version < 2.0f) { patch.Material = "textures/" + token.ToString(); } else { patch.Material = token.ToString(); } if (patchDef3 == true) { patch.HorizontalSubdivisions = (int)info[2]; patch.VerticalSubdivisions = (int)info[3]; patch.ExplicitlySubdivided = true; } if ((patch.Width < 0) || (patch.Height < 0)) { lexer.Error("idMapPatch::Parse: bad size"); return(null); } // these were written out in the wrong order, IMHO if (lexer.ExpectTokenString("(") == false) { lexer.Error("idMapPatch::Parse: bad patch vertex data"); return(null); } for (int j = 0; j < patch.Width; j++) { if (lexer.ExpectTokenString("(") == false) { lexer.Error("idMapPatch::Parse: bad vertex row data"); return(null); } for (int i = 0; i < patch.Height; i++) { float[] v = lexer.Parse1DMatrix(5); if (v == null) { lexer.Error("idMapPatch::Parse: bad vertex column data"); return(null); } Vertex vert = new Vertex(); vert.Position.X = v[0] - origin.X; vert.Position.Y = v[1] - origin.Y; vert.Position.Z = v[2] - origin.Z; vert.TextureCoordinates = new Vector2(v[3], v[4]); patch.SetVertex(i * patch.Width + j, vert); } if (lexer.ExpectTokenString(")") == false) { lexer.Error("idMapPatch::Parse: unable to parse patch control points"); return(null); } } if (lexer.ExpectTokenString(")") == false) { lexer.Error("idMapPatch::Parse: unable to parse patch control points, no closure"); return(null); } // read any key/value pairs while ((token = lexer.ReadToken()) != null) { if (token.ToString() == "}") { lexer.ExpectTokenString("}"); break; } if (token.Type == TokenType.String) { string key = token.ToString(); token = lexer.ExpectTokenType(TokenType.String, 0); patch.Dict.Set(key, token.ToString()); } } return(patch); }