Exemple #1
0
        public static Track LoadTrack(sol_track trackdata)
        {
            var ret = new Track {
                Name = trackdata.name
            };
            var buffer     = (List <Amf0Object>)trackdata.get_property("data");
            var addedlines = new Dictionary <int, StandardLine>();
            var version    = trackdata.data.First(x => x.name == "version").data as string;

            if (version == "6.1")
            {
                ret.SetVersion(6.1m);
            }
            else
            {
                ret.SetVersion(6.2m);
            }
            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
            }
            var extensions = new List <Extensionentry>();

            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.SetExtension(Convert.ToInt32(line[4].data, CultureInfo.InvariantCulture));
                    if (line[6].data != null)
                    {
                        var prev = Convert.ToInt32(line[6].data, CultureInfo.InvariantCulture);
                        extensions.Add(new Extensionentry {
                                Line = l, Linkid = prev, Next = false
                            });
                    }
                    if (line[7].data != null)
                    {
                        var next = Convert.ToInt32(line[7].data, CultureInfo.InvariantCulture);
                        extensions.Add(new Extensionentry {
                                Line = l, Linkid = next, Next = true
                            });
                    }
                    if (!addedlines.ContainsKey(l.ID))
                    {
                        ret.AddLines(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.SetExtension(Convert.ToInt32(line[4].data, CultureInfo.InvariantCulture));
                    if (line[6].data != null)
                    {
                        var prev = Convert.ToInt32(line[6].data, CultureInfo.InvariantCulture);
                        extensions.Add(new Extensionentry {
                                Line = l, Linkid = prev, Next = false
                            });
                    }
                    if (line[7].data != null)
                    {
                        var next = Convert.ToInt32(line[7].data, CultureInfo.InvariantCulture);
                        extensions.Add(new Extensionentry {
                                Line = l, Linkid = next, Next = true
                            });
                    }
                    if (!addedlines.ContainsKey(l.ID))
                    {
                        ret.AddLines(l);
                        addedlines[l.ID] = l;
                    }
                }
                break;

                case 2:
                    ret.AddLines(
                        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 Exception("Unknown line type");
                }
            }
            foreach (var v in extensions)
            {
                if (v.Next)
                {
                    StandardLine sl;
                    if (addedlines.TryGetValue(v.Linkid, out sl))
                    {
                        v.Line.Next = sl;
                        sl.Prev     = v.Line;
                    }
                }
                else //prev
                {
                    StandardLine sl;
                    if (addedlines.TryGetValue(v.Linkid, out sl))
                    {
                        v.Line.Prev = sl;
                        sl.Next     = v.Line;
                    }
                }
            }
            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 = ret.Lines[conv].Position.X
                });
                startline.Add(new Amf0Object {
                    data = ret.Lines[conv].Position.Y - 50 * 0.5
                });
            }
            ret.Start.X = Convert.ToDouble(startline[0].data, CultureInfo.InvariantCulture);
            ret.Start.Y = Convert.ToDouble(startline[1].data, CultureInfo.InvariantCulture);
            ret.ResetUndo();
            ret.ResetChanges();
            return(ret);
        }
Exemple #2
0
        public static Track LoadTrackTRK(string track, string savename)
        {
            var ret = new Track();

            ret.Name = track;
            var addedlines = new Dictionary <int, StandardLine>();
            var extensions = new List <Extensionentry>();
            var location   = Program.CurrentDirectory + "Tracks" + Path.DirectorySeparatorChar + track;

            if (savename != null)
            {
                location += Path.DirectorySeparatorChar + savename + ".trk";
            }
            else
            {
                location += ".trk";
            }
            using (var file =
                       File.Open(location, FileMode.Open))
            {
                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 Exception("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 Exception("Unsupported feature");
                        }
                    }
                    if (supports61)
                    {
                        ret.SetVersion(6.1m);
                    }
                    else
                    {
                        ret.SetVersion(6.2m);
                    }
                    if (songinfo)
                    {
                        var song = br.ReadString();
                        try
                        {
                            var strings = song.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            var fn      = Program.CurrentDirectory + "Songs" +
                                          Path.DirectorySeparatorChar +
                                          strings[0];
                            if (File.Exists(fn))
                            {
                                if (AudioPlayback.LoadFile(ref fn))
                                {
                                    game.CurrentSong = new Song(Path.GetFileName(fn), float.Parse(strings[1]));
                                    game.EnableSong  = true;
                                }
                                else
                                {
                                    Program.NonFatalError("An unknown error occured trying to load the song file");
                                }
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                    ret.Start = new Vector2d(br.ReadDouble(), br.ReadDouble());
                    var lines = br.ReadInt32();
                    for (var i = 0; i < lines; i++)
                    {
                        Line        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();
                                nxtID = br.ReadInt32();
                            }
                        }
                        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.SetExtension(lim);
                            l = bl;
                            if (prvID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = bl, Linkid = prvID, Next = false
                                });
                            }
                            if (nxtID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = bl, Linkid = nxtID, Next = true
                                });
                            }
                            bl.Trigger = tr;
                            break;

                        case LineType.Red:
                            var rl = new RedLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                            rl.ID = ID;
                            rl.SetExtension(lim);
                            if (redmultipier)
                            {
                                rl.Multiplier = multiplier;
                            }
                            l = rl;
                            if (prvID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = rl, Linkid = prvID, Next = false
                                });
                            }
                            if (nxtID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = rl, Linkid = nxtID, Next = true
                                });
                            }
                            rl.Trigger = tr;
                            break;

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

                            break;

                        default:
                            throw new Exception("Invalid line type");
                        }
                        if (l is StandardLine)
                        {
                            if (!addedlines.ContainsKey(l.ID))
                            {
                                addedlines[ID] = (StandardLine)l;
                                ret.AddLines(l);
                            }
                        }
                        else
                        {
                            ret.AddLines(l);
                        }
                    }
                }
            }
            foreach (var v in extensions)
            {
                if (v.Next)
                {
                    StandardLine sl;
                    if (addedlines.TryGetValue(v.Linkid, out sl))
                    {
                        //if (sl.Extension == StandardLine.ExtensionDirection.Right || sl.Extension == StandardLine.ExtensionDirection.Both)
                        {
                            v.Line.Next = sl;
                            sl.Prev     = v.Line;
                        }
                    }
                }
                else //prev
                {
                    StandardLine sl;
                    if (addedlines.TryGetValue(v.Linkid, out sl))
                    {
                        //if (sl.Extension == StandardLine.ExtensionDirection.Left || sl.Extension == StandardLine.ExtensionDirection.Both)
                        {
                            v.Line.Prev = sl;
                            sl.Next     = v.Line;
                        }
                    }
                }
            }
            ret.ResetUndo();
            ret.ResetChanges();
            return(ret);
        }