Esempio n. 1
0
        private static void AddLine(Track track, line_json line)
        {
            switch (line.type)
            {
            case 0:
            {
                var add = new StandardLine(
                    new Vector2d(line.x1, line.y1),
                    new Vector2d(line.x2, line.y2),
                    Convert.ToBoolean(line.flipped));
                add.ID        = line.id;
                add.Extension = (StandardLine.Ext)line.extended;
                if (Convert.ToBoolean(line.leftExtended))
                {
                    add.Extension |= StandardLine.Ext.Left;
                }
                if (Convert.ToBoolean(line.rightExtended))
                {
                    add.Extension |= StandardLine.Ext.Right;
                }
                track.AddLine(add);
                break;
            }

            case 1:
            {
                var add = new RedLine(
                    new Vector2d(line.x1, line.y1),
                    new Vector2d(line.x2, line.y2),
                    Convert.ToBoolean(line.flipped));
                add.ID        = line.id;
                add.Extension = (StandardLine.Ext)line.extended;
                if (Convert.ToBoolean(line.leftExtended))
                {
                    add.Extension |= StandardLine.Ext.Left;
                }
                if (Convert.ToBoolean(line.rightExtended))
                {
                    add.Extension |= StandardLine.Ext.Right;
                }
                track.AddLine(add);
                break;
            }

            case 2:
            {
                var add = new SceneryLine(
                    new Vector2d(line.x1, line.y1),
                    new Vector2d(line.x2, line.y2));
                add.ID = line.id;
                track.AddLine(add);
                break;
            }

            default:
                throw new TrackIO.TrackLoadException(
                          "Unknown line type");
            }
        }
Esempio n. 2
0
        public static Track LoadTrack(string trackfile, string trackname)
        {
            var ret = new Track();

            ret.Filename = trackfile;
            ret.Name     = trackname;
            ret.Remount  = false;
            var addedlines = new Dictionary <int, StandardLine>();
            var location   = trackfile;
            var bytes      = File.ReadAllBytes(location);

            using (var file =
                       new MemoryStream(bytes))
            {
                var br    = new BinaryReader(file);
                int magic = br.ReadInt32();
                if (magic != ('T' | 'R' << 8 | 'K' << 16 | 0xF2 << 24))
                {
                    throw new TrackIO.TrackLoadException("File was read as .trk but it is not valid");
                }
                byte     version  = br.ReadByte();
                string[] features = ReadString(br).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (version != 1)
                {
                    throw new TrackIO.TrackLoadException("Unsupported version");
                }
                bool redmultipier     = false;
                bool scenerywidth     = false;
                bool supports61       = false;
                bool songinfo         = false;
                bool ignorabletrigger = false;
                for (int i = 0; i < features.Length; i++)
                {
                    switch (features[i])
                    {
                    case TrackFeatures.redmultiplier:
                        redmultipier = true;
                        break;

                    case TrackFeatures.scenerywidth:
                        scenerywidth = true;
                        break;

                    case TrackFeatures.six_one:
                        supports61 = true;
                        break;

                    case TrackFeatures.songinfo:
                        songinfo = true;
                        break;

                    case TrackFeatures.ignorable_trigger:
                        ignorabletrigger = true;
                        break;

                    case TrackFeatures.zerostart:
                        ret.ZeroStart = true;
                        break;

                    case TrackFeatures.remount:
                        ret.Remount = true;
                        break;

                    case TrackFeatures.frictionless:
                        ret.frictionless = true;
                        break;

                    default:
                        throw new TrackIO.TrackLoadException("Unsupported feature");
                    }
                }
                if (supports61)
                {
                    ret.SetVersion(61);
                }
                else
                {
                    ret.SetVersion(62);
                }
                if (songinfo)
                {
                    var song = br.ReadString();
                    try
                    {
                        var strings = song.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        var fn      = Program.UserDirectory + "Songs" +
                                      Path.DirectorySeparatorChar +
                                      strings[0];
                        if (File.Exists(fn))
                        {
                            if (AudioService.LoadFile(ref fn))
                            {
                                ret.Song = new Song(Path.GetFileName(fn), float.Parse(strings[1], Program.Culture));
                            }
                            else
                            {
                                Program.NonFatalError("An unknown error occured trying to load the song file");
                            }
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
                ret.StartOffset = new Vector2d(br.ReadDouble(), br.ReadDouble());
                var lines = br.ReadInt32();
                List <LineTrigger> linetriggers = new List <LineTrigger>();
                for (var i = 0; i < lines; i++)
                {
                    GameLine    l;
                    byte        ltype      = br.ReadByte();
                    var         lt         = (LineType)(ltype & 0x1F);//we get 5 bits
                    var         inv        = (ltype >> 7) != 0;
                    var         lim        = (ltype >> 5) & 0x3;
                    var         ID         = -1;
                    var         prvID      = -1;
                    var         nxtID      = -1;
                    var         multiplier = 1;
                    var         linewidth  = 1f;
                    LineTrigger tr         = null;
                    if (redmultipier)
                    {
                        if (lt == LineType.Red)
                        {
                            multiplier = br.ReadByte();
                        }
                    }
                    if (lt == LineType.Blue || lt == LineType.Red)
                    {
                        if (ignorabletrigger)
                        {
                            tr = new LineTrigger();
                            bool zoomtrigger = br.ReadBoolean();
                            if (zoomtrigger)
                            {
                                tr.ZoomTrigger = true;
                                var target = br.ReadSingle();
                                var frames = br.ReadInt16();
                                tr.ZoomFrames = frames;
                                tr.ZoomTarget = target;
                            }
                            else
                            {
                                tr = null;
                            }
                        }
                        ID = br.ReadInt32();
                        if (lim != 0)
                        {
                            prvID = br.ReadInt32(); //ignored
                            nxtID = br.ReadInt32(); //ignored
                        }
                    }
                    if (lt == LineType.Scenery)
                    {
                        if (scenerywidth)
                        {
                            float b = br.ReadByte();
                            linewidth = b / 10f;
                        }
                    }
                    var x1 = br.ReadDouble();
                    var y1 = br.ReadDouble();
                    var x2 = br.ReadDouble();
                    var y2 = br.ReadDouble();

                    if (tr != null)
                    {
                        tr.LineID = ID;
                        linetriggers.Add(tr);
                    }
                    switch (lt)
                    {
                    case LineType.Blue:
                        var bl = new StandardLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                        bl.ID        = ID;
                        bl.Extension = (StandardLine.Ext)lim;
                        l            = bl;
                        break;

                    case LineType.Red:
                        var rl = new RedLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                        rl.ID        = ID;
                        rl.Extension = (StandardLine.Ext)lim;
                        if (redmultipier)
                        {
                            rl.Multiplier = multiplier;
                        }
                        l = rl;
                        break;

                    case LineType.Scenery:
                        l = new SceneryLine(new Vector2d(x1, y1), new Vector2d(x2, y2))
                        {
                            Width = linewidth
                        };

                        break;

                    default:
                        throw new TrackIO.TrackLoadException("Invalid line type at ID " + ID);
                    }
                    if (l is StandardLine)
                    {
                        if (!addedlines.ContainsKey(l.ID))
                        {
                            addedlines[ID] = (StandardLine)l;
                            ret.AddLine(l);
                        }
                    }
                    else
                    {
                        ret.AddLine(l);
                    }
                }
                ret.Triggers = TriggerConverter.ConvertTriggers(linetriggers, ret);
                if (br.BaseStream.Position != br.BaseStream.Length)
                {
                    var meta = br.ReadInt32();
                    if (meta == ('M' | 'E' << 8 | 'T' << 16 | 'A' << 24))
                    {
                        ParseMetadata(ret, br);
                    }
                    else
                    {
                        throw new TrackIO.TrackLoadException("Expected metadata tag but got " + meta.ToString("X8"));
                    }
                }
            }
            return(ret);
        }
        public static Track LoadTrack(string trackfile, string trackname)
        {
            var ret = new Track();

            ret.Filename = trackfile;
            ret.Name     = trackname;
            var addedlines = new Dictionary <int, StandardLine>();
            var location   = trackfile;
            var bytes      = File.ReadAllBytes(location);

            using (var file =
                       new MemoryStream(bytes))
            {
                var br    = new BinaryReader(file);
                int magic = br.ReadInt32();
                if (magic == ('T' | 'R' << 8 | 'K' << 16 | 0xF2 << 24))
                {
                    byte     version  = br.ReadByte();
                    string[] features = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt16())).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (version != 1)
                    {
                        throw new TrackIO.TrackLoadException("Unsupported version");
                    }
                    bool redmultipier     = false;
                    bool scenerywidth     = false;
                    bool supports61       = false;
                    bool songinfo         = false;
                    bool ignorabletrigger = false;
                    for (int i = 0; i < features.Length; i++)
                    {
                        switch (features[i])
                        {
                        case "REDMULTIPLIER":
                            redmultipier = true;
                            break;

                        case "SCENERYWIDTH":
                            scenerywidth = true;
                            break;

                        case "6.1":
                            supports61 = true;
                            break;

                        case "SONGINFO":
                            songinfo = true;
                            break;

                        case "IGNORABLE_TRIGGER":
                            ignorabletrigger = true;
                            break;

                        case "ZEROSTART":
                            ret.ZeroStart = true;
                            break;

                        default:
                            throw new TrackIO.TrackLoadException("Unsupported feature");
                        }
                    }
                    if (supports61)
                    {
                        ret.SetVersion(61);
                    }
                    else
                    {
                        ret.SetVersion(62);
                    }
                    if (songinfo)
                    {
                        var song = br.ReadString();
                        try
                        {
                            var strings = song.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            var fn      = Program.UserDirectory + "Songs" +
                                          Path.DirectorySeparatorChar +
                                          strings[0];
                            if (File.Exists(fn))
                            {
                                if (AudioService.LoadFile(ref fn))
                                {
                                    Settings.Local.CurrentSong = new Song(Path.GetFileName(fn), float.Parse(strings[1]));
                                    Settings.Local.EnableSong  = true;
                                }
                                else
                                {
                                    Program.NonFatalError("An unknown error occured trying to load the song file");
                                }
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                    ret.StartOffset = new Vector2d(br.ReadDouble(), br.ReadDouble());
                    var lines = br.ReadInt32();
                    for (var i = 0; i < lines; i++)
                    {
                        GameLine    l;
                        byte        ltype      = br.ReadByte();
                        var         lt         = (LineType)(ltype & 0x1F);//we get 5 bits
                        var         inv        = (ltype >> 7) != 0;
                        var         lim        = (ltype >> 5) & 0x3;
                        var         ID         = -1;
                        var         prvID      = -1;
                        var         nxtID      = -1;
                        var         multiplier = 1;
                        var         linewidth  = 1f;
                        LineTrigger tr         = ignorabletrigger ? new LineTrigger() : null;
                        if (redmultipier)
                        {
                            if (lt == LineType.Red)
                            {
                                multiplier = br.ReadByte();
                            }
                        }
                        if (lt == LineType.Blue || lt == LineType.Red)
                        {
                            if (ignorabletrigger)
                            {
                                bool zoomtrigger = br.ReadBoolean();
                                if (zoomtrigger)
                                {
                                    tr.Zoomtrigger = true;
                                    var target = br.ReadSingle();
                                    var frames = br.ReadInt16();
                                    tr.ZoomFrames = frames;
                                    tr.ZoomTarget = target;
                                }
                                else
                                {
                                    tr = null;
                                }
                            }
                            ID = br.ReadInt32();
                            if (lim != 0)
                            {
                                prvID = br.ReadInt32(); //ignored
                                nxtID = br.ReadInt32(); //ignored
                            }
                        }
                        if (lt == LineType.Scenery)
                        {
                            if (scenerywidth)
                            {
                                float b = br.ReadByte();
                                linewidth = b / 10f;
                            }
                        }
                        var x1 = br.ReadDouble();
                        var y1 = br.ReadDouble();
                        var x2 = br.ReadDouble();
                        var y2 = br.ReadDouble();
                        switch (lt)
                        {
                        case LineType.Blue:
                            var bl = new StandardLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                            bl.ID        = ID;
                            bl.Extension = (StandardLine.Ext)lim;
                            l            = bl;
                            bl.Trigger   = tr;
                            break;

                        case LineType.Red:
                            var rl = new RedLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                            rl.ID        = ID;
                            rl.Extension = (StandardLine.Ext)lim;
                            if (redmultipier)
                            {
                                rl.Multiplier = multiplier;
                            }
                            l          = rl;
                            rl.Trigger = tr;
                            break;

                        case LineType.Scenery:
                            l = new SceneryLine(new Vector2d(x1, y1), new Vector2d(x2, y2))
                            {
                                Width = linewidth
                            };

                            break;

                        default:
                            throw new TrackIO.TrackLoadException("Invalid line type at ID " + ID);
                        }
                        if (l is StandardLine)
                        {
                            if (!addedlines.ContainsKey(l.ID))
                            {
                                addedlines[ID] = (StandardLine)l;
                                ret.AddLine(l);
                            }
                        }
                        else
                        {
                            ret.AddLine(l);
                        }
                    }
                }
            }
            return(ret);
        }
Esempio n. 4
0
        public static Track LoadTrack(sol_track trackdata)
        {
            var ret = new Track {
                Name = trackdata.name, Filename = trackdata.filename
            };
            var             buffer     = (List <Amf0Object>)trackdata.get_property("data");
            List <GameLine> lineslist  = new List <GameLine>(buffer.Count);
            var             addedlines = new Dictionary <int, StandardLine>(buffer.Count);
            var             version    = trackdata.data.First(x => x.name == "version").data as string;

            if (version == "6.1")
            {
                ret.SetVersion(61);
            }
            else
            {
                ret.SetVersion(62);
            }
            try
            {
                var options = (List <Amf0Object>)trackdata.get_property("trackData");
                if (options.Count >= 2)
                {
                    try
                    {
                        ret.ZeroStart = (bool)options.Find(x => x.name == "2").get_property("5");
                    }
                    catch
                    {
                        //ignored
                    }
                }
            }
            catch
            {
                //ignored
            }
            for (var i = buffer.Count - 1; i >= 0; --i)
            {
                var line = (List <Amf0Object>)buffer[i].data;
                var type = Convert.ToInt32(line[9].data, CultureInfo.InvariantCulture);
                switch (type)
                {
                case 0:
                {
                    var l =
                        new StandardLine(
                            new Vector2d(Convert.ToDouble(line[0].data, CultureInfo.InvariantCulture),
                                         Convert.ToDouble(line[1].data, CultureInfo.InvariantCulture)),
                            new Vector2d(Convert.ToDouble(line[2].data, CultureInfo.InvariantCulture),
                                         Convert.ToDouble(line[3].data, CultureInfo.InvariantCulture)),
                            Convert.ToBoolean(line[5].data, CultureInfo.InvariantCulture))
                    {
                        ID = Convert.ToInt32(line[8].data, CultureInfo.InvariantCulture)
                    };
                    l.Extension = (StandardLine.Ext)(
                        Convert.ToInt32(
                            line[4].data,
                            CultureInfo.InvariantCulture));
                    if (line[6].data != null)
                    {
                        var prev = Convert.ToInt32(line[6].data, CultureInfo.InvariantCulture);
                    }
                    if (line[7].data != null)
                    {
                        var next = Convert.ToInt32(line[7].data, CultureInfo.InvariantCulture);
                    }
                    if (!addedlines.ContainsKey(l.ID))
                    {
                        lineslist.Add(l);
                        addedlines[l.ID] = l;
                    }
                }
                break;

                case 1:
                {
                    var l =
                        new RedLine(
                            new Vector2d(Convert.ToDouble(line[0].data, CultureInfo.InvariantCulture),
                                         Convert.ToDouble(line[1].data, CultureInfo.InvariantCulture)),
                            new Vector2d(Convert.ToDouble(line[2].data, CultureInfo.InvariantCulture),
                                         Convert.ToDouble(line[3].data, CultureInfo.InvariantCulture)),
                            Convert.ToBoolean(line[5].data, CultureInfo.InvariantCulture))
                    {
                        ID = Convert.ToInt32(line[8].data, CultureInfo.InvariantCulture)
                    };
                    l.Extension = (StandardLine.Ext)(
                        Convert.ToInt32(
                            line[4].data,
                            CultureInfo.InvariantCulture));
                    if (line[6].data != null)
                    {
                        var prev = Convert.ToInt32(line[6].data, CultureInfo.InvariantCulture);
                    }
                    if (line[7].data != null)
                    {
                        var next = Convert.ToInt32(line[7].data, CultureInfo.InvariantCulture);
                    }
                    if (!addedlines.ContainsKey(l.ID))
                    {
                        lineslist.Add(l);
                        addedlines[l.ID] = l;
                    }
                }
                break;

                case 2:
                    lineslist.Add(
                        new SceneryLine(
                            new Vector2d(Convert.ToDouble(line[0].data, CultureInfo.InvariantCulture),
                                         Convert.ToDouble(line[1].data, CultureInfo.InvariantCulture)),
                            new Vector2d(Convert.ToDouble(line[2].data, CultureInfo.InvariantCulture),
                                         Convert.ToDouble(line[3].data, CultureInfo.InvariantCulture))));
                    break;

                default:
                    throw new TrackIO.TrackLoadException("Unknown line type");
                }
            }
            var startlineprop = trackdata.get_property("startLine");
            var startline     = startlineprop as List <Amf0Object>;

            if (startline == null && startlineprop is double)
            {
                var conv = Convert.ToInt32(startlineprop, CultureInfo.InvariantCulture);
                if (conv >= ret.Lines.Count || conv < 0)
                {
                    startline = new List <Amf0Object>();
                    startline.Add(new Amf0Object {
                        data = 100
                    });
                    startline.Add(new Amf0Object {
                        data = 100
                    });
                }
            }
            else if (startlineprop is double)
            {
                var conv = Convert.ToInt32(startlineprop, CultureInfo.InvariantCulture);
                startline = new List <Amf0Object>();
                startline.Add(new Amf0Object {
                    data = lineslist[conv].Position.X
                });
                startline.Add(new Amf0Object {
                    data = lineslist[conv].Position.Y - 50 * 0.5
                });
            }
            ret.StartOffset = new Vector2d(
                Convert.ToDouble(startline[0].data, CultureInfo.InvariantCulture),
                Convert.ToDouble(startline[1].data, CultureInfo.InvariantCulture));
            foreach (var line in lineslist)
            {
                ret.AddLine(line);
            }
            return(ret);
        }