Ejemplo n.º 1
0
        /// <summary>
        /// Imports the specified Unreal Text file.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A <see cref="T3dMap"/> containing the imported map data.</returns>
        public T3dMap Import(string path)
        {
            // create a new map.
            T3dMap map = new T3dMap();

            // open the file for reading. we use streams for additional performance.
            // it's faster than File.ReadAllLines() as that requires two iterations.
            using (FileStream stream = new FileStream(path, FileMode.Open))
                using (StreamReader reader = new StreamReader(stream))
                {
                    // read all the lines from the file.
                    bool   inActor = false; T3dActor actor = null;
                    bool   inBrush = false; T3dBrush brush = null;
                    bool   inPolygon = false; T3dPolygon polygon = null;
                    string line;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine().Trim();

                        // if we currently parsing an actor:
                        if (inActor)
                        {
                            if (!inBrush)
                            {
                                // if we done parsing the actor:
                                if (line == "End Actor")
                                {
                                    inActor = false;
                                    continue;
                                }

                                // try parsing an actor property.
                                string key;
                                object value;
                                if (TryParseProperty(line, out key, out value))
                                {
                                    actor.Properties.Add(key, value);
                                }

                                // make sure we detect brush declarations:
                                if (line.StartsWith("Begin Brush"))
                                {
                                    // read the properties of the brush.
                                    var properties = ParseKeyValuePairs(line);
                                    if (properties.ContainsKey("Name"))
                                    {
                                        // this is a valid brush model that can be parsed further.
                                        inBrush = true;
                                        // create a new brush model and add it to the map.
                                        brush = new T3dBrush(properties["Name"]);
                                        map.BrushModels.Add(brush);
                                        continue;
                                    }
                                }
                            }
                            // we are currently parsing a brush:
                            else
                            {
                                // if we done parsing the brush:
                                if (line == "End Brush")
                                {
                                    inBrush = false;
                                    continue;
                                }

                                if (!inPolygon)
                                {
                                    // make sure we detect brush polygon declarations:
                                    if (line.StartsWith("Begin Polygon"))
                                    {
                                        inPolygon = true;
                                        // create a new brush polygon and add it to the brush.
                                        polygon = new T3dPolygon();
                                        brush.Polygons.Add(polygon);
                                        // read the properties of the brush polygon.
                                        var properties = ParseKeyValuePairs(line);
                                        if (properties.ContainsKey("Item"))
                                        {
                                            polygon.Item = properties["Item"];
                                        }
                                        if (properties.ContainsKey("Texture"))
                                        {
                                            polygon.Texture = properties["Texture"];
                                        }
                                        if (properties.ContainsKey("Flags"))
                                        {
                                            polygon.Flags = (T3dPolygonFlags)Int32.Parse(properties["Flags"]);
                                        }
                                    }
                                }
                                // we are currently parsing a brush polygon:
                                else
                                {
                                    // if we done parsing the brush polygon:
                                    if (line == "End Polygon")
                                    {
                                        inPolygon = false;
                                        continue;
                                    }

                                    if (line.StartsWith("Origin"))
                                    {
                                        polygon.Origin = ParsePolygonVector(line);
                                    }
                                    if (line.StartsWith("Normal"))
                                    {
                                        polygon.Normal = ParsePolygonVector(line);
                                    }
                                    if (line.StartsWith("TextureU"))
                                    {
                                        polygon.TextureU = ParsePolygonVector(line);
                                    }
                                    if (line.StartsWith("TextureV"))
                                    {
                                        polygon.TextureV = ParsePolygonVector(line);
                                    }
                                    if (line.StartsWith("Pan"))
                                    {
                                        int u, v;
                                        ParsePolygonUV(line, out u, out v);
                                        polygon.PanU = u;
                                        polygon.PanV = v;
                                    }
                                    if (line.StartsWith("Vertex"))
                                    {
                                        polygon.Vertices.Add(ParsePolygonVector(line));
                                    }
                                }
                            }
                        }
                        // if we are looking for another begin declaration:
                        else
                        {
                            // if we are going to parse an actor:
                            if (line.StartsWith("Begin Actor"))
                            {
                                // read the properties of the actor.
                                var properties = ParseKeyValuePairs(line);
                                if (properties.ContainsKey("Class") && properties.ContainsKey("Name"))
                                {
                                    // this is a valid actor that can be parsed further.
                                    inActor = true;
                                    // create a new actor and add it to the map.
                                    actor = new T3dActor(map, properties["Class"], properties["Name"]);
                                    map.Actors.Add(actor);
                                    continue;
                                }
                            }
                        }
                    }
                }

            // return the map data.
            return(map);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T3dActor"/> class.
 /// </summary>
 /// <param name="class">The class of the actor.</param>
 /// <param name="name">The name of the actor.</param>
 public T3dActor(T3dMap map, string @class, string name)
 {
     m_T3dMap = map;
     Class    = @class;
     Name     = name;
 }