Example #1
0
 public virtual Entity ent42ToEntM510(Entity inputData)
 {
     if (inputData.BrushBased)
     {
         Vector3D origin = inputData.Origin;
         inputData.Attributes.Remove("origin");
         inputData.Attributes.Remove("model");
         if ((origin[0] != 0 || origin[1] != 0 || origin[2] != 0) && !Settings.noOriginBrushes)
         {
             // If this brush uses the "origin" attribute
             MAPBrush newOriginBrush = MAPBrush.createBrush(new Vector3D(-Settings.originBrushSize, -Settings.originBrushSize, -Settings.originBrushSize), new Vector3D(Settings.originBrushSize, Settings.originBrushSize, Settings.originBrushSize), "special/origin");
             inputData.Brushes.Add(newOriginBrush);
         }
         for (int i = 0; i < inputData.Brushes.Count; i++)
         {
             MAPBrush currentBrush = inputData.Brushes[i];
             currentBrush.translate(new Vector3D(origin));
         }
     }
     return(inputData);
 }
    // -entityToByteArray()
    // Converts the entity and its brushes into byte arrays rather than Strings,
    // which can then be written to a file much faster. Concatenating Strings is
    // a costly operation, especially when hundreds of thousands of Strings are
    // in play. This is one of two parts to writing a file quickly. The second
    // part is to call the FileOutputStream.write() method only once, with a
    // gigantic array, rather than several times with many small arrays. File I/O
    // from a hard drive is another costly operation, best done by handling
    // massive amounts of data in one go, rather than tiny amounts of data thousands
    // of times.
    private byte[] entityToByteArray(Entity inputData, int num)
    {
        byte[]   outputData;
        Vector3D origin;

        if (inputData.BrushBased)
        {
            origin = inputData.Origin;
            inputData.Remove("origin");
            inputData.Remove("model");
            if (origin[0] != 0 || origin[1] != 0 || origin[2] != 0)
            {
                // If this entity uses the "origin" attribute
                MAPBrush newOriginBrush = MAPBrush.createBrush(new Vector3D(-Settings.originBrushSize, -Settings.originBrushSize, -Settings.originBrushSize), new Vector3D(Settings.originBrushSize, Settings.originBrushSize, Settings.originBrushSize), "special/origin");
                inputData.Brushes.Add(newOriginBrush);
            }
        }
        else
        {
            origin = Vector3D.ZERO;
        }
        string temp = "";

        if (!inputData["classname"].Equals("worldspawn", StringComparison.InvariantCultureIgnoreCase))
        {
            temp += "// entity " + num + (char)0x0D + (char)0x0A;
        }
        temp += "{";
        int len = temp.Length + 5;

        // Get the lengths of all attributes together
        foreach (string key in inputData.Attributes.Keys)
        {
            len += key.Length + inputData[key].Length + 7;             // Four quotes, a space and a newline
        }
        outputData = new byte[len];
        int offset = 0;

        for (int i = 0; i < temp.Length; i++)
        {
            outputData[offset++] = (byte)temp[i];
        }
        outputData[offset++] = (byte)0x0D;
        outputData[offset++] = (byte)0x0A;
        foreach (string key in inputData.Attributes.Keys)
        {
            // For each attribute
            outputData[offset++] = (byte)'\"';             // 1
            for (int j = 0; j < key.Length; j++)
            {
                // Then for each byte in the attribute
                outputData[offset++] = (byte)key[j];      // add it to the output array
            }
            outputData[offset++] = (byte)'\"';            // 2
            outputData[offset++] = (byte)' ';             // 3
            outputData[offset++] = (byte)'\"';            // 4
            for (int j = 0; j < inputData.Attributes[key].Length; j++)
            {
                // Then for each byte in the attribute
                outputData[offset++] = (byte)inputData.Attributes[key][j]; // add it to the output array
            }
            outputData[offset++] = (byte)'\"';                             // 5
            outputData[offset++] = (byte)0x0D;                             // 6
            outputData[offset++] = (byte)0x0A;                             // 7
        }
        int brushArraySize = 0;

        byte[][] brushes = new byte[inputData.Brushes.Count][];
        for (int j = 0; j < inputData.Brushes.Count; j++)
        {
            brushes[j]      = brushToByteArray(inputData.Brushes[j], j);
            brushArraySize += brushes[j].Length;
        }
        int brushoffset = 0;

        byte[] brushArray = new byte[brushArraySize];
        for (int j = 0; j < inputData.Brushes.Count; j++)
        {
            // For each brush in the entity
            for (int k = 0; k < brushes[j].Length; k++)
            {
                brushArray[brushoffset + k] = brushes[j][k];
            }
            brushoffset += brushes[j].Length;
        }
        if (brushArray.Length != 0)
        {
            len += brushArray.Length;
            byte[] newOut = new byte[len];
            for (int j = 0; j < outputData.Length; j++)
            {
                newOut[j] = outputData[j];
            }
            for (int j = 0; j < brushArray.Length; j++)
            {
                newOut[j + outputData.Length - 3] = brushArray[j];
            }
            offset    += brushArray.Length;
            outputData = newOut;
        }
        outputData[offset++] = (byte)'}';
        outputData[offset++] = (byte)0x0D;
        outputData[offset++] = (byte)0x0A;
        return(outputData);
    }