示例#1
0
    // this is designed for pasting stuff copied in the workshop - no group support, no environment settings, only bricks
    public static BrickList ParseBRKBricks(string text)
    {
        List <BrickData> bricks = new List <BrickData>();

        string[] lines = text.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < lines.Length; i++)
        {
            string line = lines[i].Trim();
            if (line[0] != '+')
            {
                // defining brick
                BrickData brick     = new BrickData();
                float[]   brickInfo = Helper.StringToFloatArray(line);
                brick.Position = new Vector3(brickInfo[0], brickInfo[1], brickInfo[2]);
                brick.Scale    = new Vector3(brickInfo[3], brickInfo[4], brickInfo[5]);
                brick.ConvertTransformToUnity();
                brick.BrickColor   = new Color(brickInfo[6], brickInfo[7], brickInfo[8]);
                brick.Transparency = brickInfo[9];

                bricks.Add(brick);
            }
            else
            {
                // defining brick properties
                BrickData brick = bricks[bricks.Count - 1];
                if (line.StartsWith("+NAME"))
                {
                    if (line.Length == 5)
                    {
                        // the brick doesn't have a name for some reason
                        brick.Name = "";
                    }
                    else
                    {
                        brick.Name = line.Substring(6);
                    }
                }
                else if (line.StartsWith("+ROT"))
                {
                    brick.Rotation = (int.Parse(line.Substring(5), CultureInfo.InvariantCulture) * -1).Mod(360);
                }
                else if (line.StartsWith("+SHAPE"))
                {
                    brick.Shape = (int)BB.GetShape(line.Substring(7));
                }
                else if (line.StartsWith("+NOCOLLISION"))
                {
                    brick.Collision = false;
                }
                else if (line.StartsWith("+MODEL"))
                {
                    brick.Model = line.Substring(7);
                }
                else if (line.StartsWith("+CLICKABLE"))
                {
                    brick.Clickable = true;
                }
                bricks[bricks.Count - 1] = brick; // i guess i gotta do this
            }
        }

        BrickList returnBL = new BrickList();

        returnBL.bricks = bricks.ToArray();
        return(returnBL);
    }
示例#2
0
    private static Map Parse02BRK(string path)
    {
        Map    map   = new Map(); // map that will be outputted
        string name  = Path.GetFileNameWithoutExtension(path);
        string input = File.ReadAllText(path);

        string[] lines = input.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); // split all the lines of the input into separate strings, and remove empty lines

        map.Name = name;

        // the first 7 lines (including whitespace) should be the map info.
        //map.Version = lines[0]; // this is the line that says which version of workshop the map was created with
        map.AmbientColor   = Helper.StringToColor(lines[1], true); // for some reason, the ambient color is in BGR format. why?
        map.BaseplateColor = Helper.StringToColor(lines[2], true); // same as above
        map.SkyColor       = Helper.StringToColor(lines[3]);       // this however, is RGB. wack
        map.BaseplateSize  = int.Parse(lines[4], CultureInfo.InvariantCulture);
        map.SunIntensity   = int.Parse(lines[5], CultureInfo.InvariantCulture);

        int currentID = 0;                                         // this will be incremented when a brick or group is defined
        List <BrickGroup> startedGroups = new List <BrickGroup>(); // when groups are defined, they are added to this list, and removed when they are ended

        // iterate through lines
        for (int i = 6; i < lines.Length; i++)
        {
            string line = lines[i].Trim(); // get the line without any leading or trailing whitepsaces for easy parsing
            if (line[0] != '+')
            {
                // this line is either defining a new brick or something else that isn't a brick property
                if (line.StartsWith(">TEAM"))
                {
                    // this line is clearly defining a team
                    Team t = new Team();
                    t.Name = line.Substring(6);

                    if (lines.Length > i + 1)
                    {
                        string nextLine = lines[i + 1].Trim();
                        if (nextLine.StartsWith("+COLOR"))
                        {
                            t.TeamColor = Helper.StringToColor(nextLine.Substring(7));
                        }
                    }

                    map.Teams.Add(t);
                    i++; // increment i since we handled 2 lines
                }
                else if (line.StartsWith(">SLOT"))
                {
                    // this line is defining an item, but those are history so ignore them
                    continue;
                }
                else if (line.StartsWith(">CAMPOS"))
                {
                    // this line is defining the camera position/rotation. this is created by brickbuilder
                    // TODO
                }
                else if (line.StartsWith(">SUNROT"))
                {
                    // this line is defining the sun rotation. this is created by brickbuilder.
                    // TODO
                }
                else if (line.StartsWith(">GROUP"))
                {
                    // this line is defining a new group. this is created by brickbuilder.
                    BrickGroup newGroup = new BrickGroup();
                    newGroup.Name = line.Substring(7);
                    newGroup.ID   = currentID;

                    if (startedGroups.Count > 0)
                    {
                        startedGroups[startedGroups.Count - 1].Children.Add(currentID); // there is already an opened group, which means this group is a child of that group
                        newGroup.Parent = startedGroups[startedGroups.Count - 1];
                    }

                    startedGroups.Add(newGroup);
                    map.Groups.Add(newGroup);
                    map.MapElements.Add(currentID, newGroup);
                    map.lastID = currentID;
                    currentID++;
                }
                else if (line.StartsWith(">ENDGROUP"))
                {
                    // this line is ending the last group. this is created by brickbuilder.
                    // make sure there even is an opened group
                    if (startedGroups.Count > 0)
                    {
                        startedGroups.RemoveAt(startedGroups.Count - 1); // we have reached the end of that group
                    }
                    else
                    {
                        throw new Exception("Nonexistent group ended!");
                    }
                }
                else if (line[0] != '>')
                {
                    // this line is most likely defining a new brick
                    Brick   brick     = new Brick();
                    float[] brickInfo = Helper.StringToFloatArray(line);
                    brick.Position = new Vector3(brickInfo[0], brickInfo[1], brickInfo[2]);   // first 3 numbers are position
                    brick.Scale    = new Vector3(brickInfo[3], brickInfo[4], brickInfo[5]);   // next 3 numbers are scale
                    brick.ConvertTransformToUnity();
                    brick.BrickColor   = new Color(brickInfo[6], brickInfo[7], brickInfo[8]); // next 3 numbers are color
                    brick.Transparency = brickInfo[9];                                        // last number is transparency
                    brick.ID           = currentID;

                    if (startedGroups.Count > 0)
                    {
                        startedGroups[startedGroups.Count - 1].Children.Add(currentID); // there is already an opened group, which means this group is a child of that group
                        brick.Parent = startedGroups[startedGroups.Count - 1];
                    }

                    map.Bricks.Add(brick); // adds brick to map object
                    map.MapElements.Add(currentID, brick);
                    map.lastID = currentID;
                    currentID++;
                }
            }
            else
            {
                // this line is most likely defining a brick property
                Brick brick = map.Bricks[map.Bricks.Count - 1]; // get the last added brick
                if (line.StartsWith("+NAME"))
                {
                    // this line is defining the brick name
                    if (line.Length == 5)
                    {
                        // the brick doesn't have a name for some reason
                        brick.Name = "";
                    }
                    else
                    {
                        brick.Name = line.Substring(6);
                    }
                }
                else if (line.StartsWith("+ROT"))
                {
                    // this line is defining the brick rotation
                    brick.Rotation = (int.Parse(line.Substring(5), CultureInfo.InvariantCulture) * -1).Mod(360);
                    if (brick.Rotation != 0 && brick.Rotation != 180)
                    {
                        brick.Scale = brick.Scale.SwapXZ();                                               // assuming this is post transform conversion
                    }
                }
                else if (line.StartsWith("+SHAPE"))
                {
                    // this line is defining the brick shape
                    brick.Shape = BB.GetShape(line.Substring(7));
                }
                else if (line.StartsWith("+NOCOLLISION"))
                {
                    // this line is defining whether or not the brick has collision
                    brick.CollisionEnabled = false;
                }
                else if (line.StartsWith("+MODEL"))
                {
                    // this line is defining the custom asset the brick uses
                    brick.Model = line.Substring(7);
                }
                else if (line.StartsWith("+CLICKABLE"))
                {
                    // this line is defining whether or not the brick is clickable
                    // i dont even know if this property is used anymore but eh
                    brick.Clickable = true;
                    if (line.Length > 11)
                    {
                        string dist = lines[i].Substring(11);
                        brick.ClickDistance = float.Parse(dist, CultureInfo.InvariantCulture);
                    }
                }
            }
        }

        return(map);
    }