Example #1
0
		/// <summary>
		/// Creates a new <see cref="Texture"/> object from a <c>byte</c> array.
		/// </summary>
		/// <param name="data"><c>byte</c> array to parse.</param>
		/// <param name="type">The map type.</param>
		/// <param name="version">The version of this lump.</param>
		/// <exception cref="ArgumentNullException"><paramref name="data"/> was <c>null</c>.</exception>
		/// <exception cref="ArgumentException">This structure is not implemented for the given maptype.</exception>
		public Texture(byte[] data, MapType type, int version = 0) : this() {
			if (data == null) {
				throw new ArgumentNullException();
			}
			name = "";
			mask = "ignore";
			flags = 0;
			contents = 0;
			switch (type) {
				case MapType.Quake:
				case MapType.Nightfire: {
					name = data.ToNullTerminatedString();
					break;
				}
				case MapType.Quake2:
				case MapType.SoF:
				case MapType.Daikatana: {
					texAxes = new TextureInfo(new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), BitConverter.ToSingle(data, 12), 1, new Vector3(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), BitConverter.ToSingle(data, 28), 1, -1, -1);
					flags = BitConverter.ToInt32(data, 32);
					name = data.ToNullTerminatedString(40, 32);
					break;
				}
				case MapType.MOHAA: {
					mask = data.ToNullTerminatedString(76, 64);
					goto case MapType.STEF2;
				}
				case MapType.STEF2:
				case MapType.STEF2Demo:
				case MapType.Raven:
				case MapType.Quake3:
				case MapType.CoD:
				case MapType.CoD2:
				case MapType.CoD4:
				case MapType.FAKK: {
					name = data.ToNullTerminatedString(0, 64);
					flags = BitConverter.ToInt32(data, 64);
					contents = BitConverter.ToInt32(data, 68);
					break;
				}
				case MapType.Source17:
				case MapType.Source18:
				case MapType.Source19:
				case MapType.Source20:
				case MapType.Source21:
				case MapType.Source22:
				case MapType.Source23:
				case MapType.Source27:
				case MapType.L4D2:
				case MapType.TacticalInterventionEncrypted:
				case MapType.Vindictus:
				case MapType.DMoMaM: {
					name = data.ToRawString();
					break;
				}
				case MapType.SiN: {
					texAxes = new TextureInfo(new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), BitConverter.ToSingle(data, 12), 1, new Vector3(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), BitConverter.ToSingle(data, 28), 1, -1, -1);
					flags = BitConverter.ToInt32(data, 32);
					name = data.ToNullTerminatedString(36, 64);
					break;
				}
				default: {
					throw new ArgumentException("Map type " + type + " isn't supported by the Texture class.");
				}
			}
		}
Example #2
0
        /// <summary>
        /// Constructs a <see cref="MAPBrushSide"/> object using the provided <c>string</c> array as the data.
        /// </summary>
        /// <param name="lines">Data to parse.</param>
        public MAPBrushSide(string[] lines)
        {
            // If lines.Length is 1, then this line contains all data for a brush side
            if (lines.Length == 1)
            {
                string[] tokens = lines[0].SplitUnlessInContainer(' ', '\"', StringSplitOptions.RemoveEmptyEntries);

                float dist = 0;

                // If this succeeds, assume brushDef3
                if (float.TryParse(tokens[4], out dist))
                {
                    plane       = new Plane(new Vector3(float.Parse(tokens[1], _format), float.Parse(tokens[2], _format), float.Parse(tokens[3], _format)), dist);
                    textureInfo = new TextureInfo(new Vector3(float.Parse(tokens[8], _format), float.Parse(tokens[9], _format), float.Parse(tokens[10], _format)),
                                                  new Vector3(float.Parse(tokens[13], _format), float.Parse(tokens[14], _format), float.Parse(tokens[15], _format)),
                                                  new Vector2(0, 0),
                                                  new Vector2(1, 1),
                                                  0, 0, 0);
                    texture = tokens[18];
                }
                else
                {
                    Vector3 v1 = new Vector3(float.Parse(tokens[1], _format), float.Parse(tokens[2], _format), float.Parse(tokens[3], _format));
                    Vector3 v2 = new Vector3(float.Parse(tokens[6], _format), float.Parse(tokens[7], _format), float.Parse(tokens[8], _format));
                    Vector3 v3 = new Vector3(float.Parse(tokens[11], _format), float.Parse(tokens[12], _format), float.Parse(tokens[13], _format));
                    vertices = new Vector3[] { v1, v2, v3 };
                    plane    = PlaneExtensions.CreateFromVertices(v1, v2, v3);
                    texture  = tokens[15];
                    // GearCraft
                    if (tokens[16] == "[")
                    {
                        textureInfo = new TextureInfo(new Vector3(float.Parse(tokens[17], _format), float.Parse(tokens[18], _format), float.Parse(tokens[19], _format)),
                                                      new Vector3(float.Parse(tokens[23], _format), float.Parse(tokens[24], _format), float.Parse(tokens[25], _format)),
                                                      new Vector2(float.Parse(tokens[20], _format), float.Parse(tokens[26], _format)),
                                                      new Vector2(float.Parse(tokens[29], _format), float.Parse(tokens[30], _format)),
                                                      int.Parse(tokens[31]), 0, float.Parse(tokens[28], _format));
                        material = tokens[32];
                    }
                    else
                    {
                        //<x_shift> <y_shift> <rotation> <x_scale> <y_scale> <content_flags> <surface_flags> <value>
                        Vector3[] axes = TextureInfo.TextureAxisFromPlane(plane);
                        textureInfo = new TextureInfo(axes[0],
                                                      axes[1],
                                                      new Vector2(float.Parse(tokens[16], _format), float.Parse(tokens[17], _format)),
                                                      new Vector2(float.Parse(tokens[19], _format), float.Parse(tokens[20], _format)),
                                                      int.Parse(tokens[22]), 0, float.Parse(tokens[18], _format));
                    }
                }
            }
            else
            {
                bool inDispInfo = false;
                int  braceCount = 0;
                textureInfo = new TextureInfo();
                List <string> child = new List <string>(37);
                foreach (string line in lines)
                {
                    if (line == "{")
                    {
                        ++braceCount;
                    }
                    else if (line == "}")
                    {
                        --braceCount;
                        if (braceCount == 1)
                        {
                            child.Add(line);
                            displacement = new MAPDisplacement(child.ToArray());
                            child        = new List <string>(37);
                            inDispInfo   = false;
                        }
                    }
                    else if (line == "dispinfo")
                    {
                        inDispInfo = true;
                        continue;
                    }

                    if (braceCount == 1)
                    {
                        string[] tokens = line.SplitUnlessInContainer(' ', '\"', StringSplitOptions.RemoveEmptyEntries);
                        switch (tokens[0])
                        {
                        case "material": {
                            texture = tokens[1];
                            break;
                        }

                        case "plane": {
                            string[] points     = tokens[1].SplitUnlessBetweenDelimiters(' ', '(', ')', StringSplitOptions.RemoveEmptyEntries);
                            string[] components = points[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            Vector3  v1         = new Vector3(float.Parse(components[0], _format), float.Parse(components[1], _format), float.Parse(components[2], _format));
                            components = points[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            Vector3 v2 = new Vector3(float.Parse(components[0], _format), float.Parse(components[1], _format), float.Parse(components[2], _format));
                            components = points[2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            Vector3 v3 = new Vector3(float.Parse(components[0], _format), float.Parse(components[1], _format), float.Parse(components[2], _format));
                            plane = PlaneExtensions.CreateFromVertices(v1, v2, v3);
                            break;
                        }

                        case "uaxis": {
                            string[] split = tokens[1].SplitUnlessBetweenDelimiters(' ', '[', ']', StringSplitOptions.RemoveEmptyEntries);
                            textureInfo.scale       = new Vector2(float.Parse(split[1], _format), textureInfo.scale.Y());
                            split                   = split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            textureInfo.uAxis       = new Vector3(float.Parse(split[0], _format), float.Parse(split[1], _format), float.Parse(split[2], _format));
                            textureInfo.translation = new Vector2(float.Parse(split[3], _format), textureInfo.translation.Y());
                            break;
                        }

                        case "vaxis": {
                            string[] split = tokens[1].SplitUnlessBetweenDelimiters(' ', '[', ']', StringSplitOptions.RemoveEmptyEntries);
                            textureInfo.scale       = new Vector2(textureInfo.scale.X(), float.Parse(split[1], _format));
                            split                   = split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            textureInfo.vAxis       = new Vector3(float.Parse(split[0], _format), float.Parse(split[1], _format), float.Parse(split[2], _format));
                            textureInfo.translation = new Vector2(textureInfo.translation.X(), float.Parse(split[3], _format));
                            break;
                        }

                        case "rotation": {
                            textureInfo.rotation = float.Parse(tokens[1], _format);
                            break;
                        }
                        }
                    }
                    else if (braceCount > 1)
                    {
                        if (inDispInfo)
                        {
                            child.Add(line);
                        }
                    }
                }
            }
        }