示例#1
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);
        }
示例#2
0
        public void ShowSongWindow()
        {
            if (GetOpenWindows().Count != 0)
            {
                return;
            }
            game.Track.Stop();
            var wc = new WindowControl(this, "Song Sync", false)
            {
                DeleteOnClose = true
            };

            wc.MakeModal(true);

            wc.MinimumSize = new System.Drawing.Point(220, 240);

            wc.Width  = 220;
            wc.Height = 240;
            wc.SetPosition((int)(Width / 2) - (wc.Width / 2), (int)(Height / 2) - (wc.Height / 2));
            //wc.DisableResizing();
            var enablesongcb = new LabeledCheckBox(wc);

            enablesongcb.CheckChanged += (o, e) => { game.EnableSong = enablesongcb.IsChecked; };
            enablesongcb.IsChecked     = game.EnableSong;
            enablesongcb.Text          = "Enable Song";
            enablesongcb.Dock          = Pos.Top;
            var gb = new GroupBox(wc);

            gb.Text = "Song Selection";
            gb.Dock = Pos.Fill;
            var Songs = new TreeControl(gb);

            Songs.Height = 100;
            Songs.Margin = new Margin()
            {
                Bottom = 10
            };
            Songs.Dock = Pos.Fill;
            Songs.Name = "songtv";

            var filedir = Program.CurrentDirectory + "Songs";

            if (Directory.Exists(filedir))
            {
                var      songfiles          = Directory.GetFiles(filedir, "*.*");
                var      supportedfiles     = new List <string>();
                string[] supportedfiletypes = new string[]
                {
                    ".mp3", ".wav", ".wave", ".ogg", ".wma", ".m4a", ".aac"
                };

                foreach (var file in songfiles)
                {
                    var lower = file.ToLower(Program.Culture);
                    foreach (var type in supportedfiletypes)
                    {
                        if (lower.EndsWith(type, StringComparison.OrdinalIgnoreCase))
                        {
                            supportedfiles.Add(file);
                            break;
                        }
                    }
                }

                foreach (var sf in supportedfiles)
                {
                    var name     = Path.GetFileName(sf);
                    var nodename = name.ToLower().Contains(".ogg") ? name : "[convert] " + name;
                    var node     = Songs.AddNode(nodename);
                    node.UserData = name;
                    if (name == game.CurrentSong?.Location)
                    {
                        node.IsSelected = true;
                    }
                }
            }
            Songs.SelectionChanged += (snd, ev) =>
            {
                var tc   = (TreeControl)wc.FindChildByName("songtv", true);
                var list = (List <TreeNode>)tc.SelectedChildren;
                if (list.Count == 1)
                {
                    game.CurrentSong.Location = (string)list[0].UserData;
                }
            };
            wc.IsHiddenChanged += (o, e) =>
            {
                if (!wc.IsHidden)
                {
                    return;
                }
                if (game.EnableSong)
                {
                    var fn = Program.CurrentDirectory + "Songs" +
                             Path.DirectorySeparatorChar +
                             game.CurrentSong.Location;
                    if (File.Exists(fn))
                    {
                        game.Loading = true;
                        AudioPlayback.LoadFile(ref fn);
                        game.Loading = false;
                    }
                }
                try
                {
                    var svolume =
                        (HorizontalSlider)wc.FindChildByName("volume", true);
                    Settings.Default.Volume = svolume.Value;
                    Settings.Default.Save();
                }
                catch
                {
                    // ignored
                }
            };
            var container = new ControlBase(gb);

            container.Dock   = Pos.Bottom;
            container.Height = 20;
            var offset = new NumericUpDown(container);

            offset.ValueChanged += (snd, ev) => { game.CurrentSong.Offset = offset.Value; };
            offset.Min           = 0;
            offset.Max           = 10000;
            offset.Value         = game.CurrentSong.Offset;
            offset.Dock          = Pos.Right;
            var label = new Label(container);

            label.Dock = Pos.Left;
            label.Text = "Offset (secs)";
            gb.Height  = 150;
            var container2 = new ControlBase(wc);

            container2.Dock   = Pos.Bottom;
            container2.Height = 40;
            label             = new Label(container2);
            label.Margin      = new Margin()
            {
                Top = 13
            };
            label.Dock = Pos.Left;
            label.Text = "Volume:";
            var volume = new HorizontalSlider(container2);

            volume.Min = 0;
            volume.Max = 100;
            if (Settings.Default.Volume > 100)
            {
                Settings.Default.Volume = 100;
            }
            if (Settings.Default.Volume < 0)
            {
                Settings.Default.Volume = 0;
            }
            volume.Value                = Settings.Default.Volume;
            volume.Name                 = "volume";
            volume.SnapToNotches        = false;
            volume.KeyboardInputEnabled = false;
            volume.Width                = 150;
            volume.Dock                 = Pos.Right;
            wc.Show();
            game.Cursor = MouseCursor.Default;
        }