public void StartMission(string missionName)
        {
            var down = Program.Downloader.GetResource(DownloadType.MISSION, missionName);
            if (down == null)
            {
                //okay Mission exist, but lets check for dependency!
                down = Program.Downloader.GetDependenciesOnly(missionName);
            }

            var engine = Program.Downloader.GetResource(DownloadType.ENGINE, Program.TasClient.ServerWelcome.Engine ?? GlobalConst.DefaultEngineOverride);

            var metaWait = new EventWaitHandle(false, EventResetMode.ManualReset);
            Mod modInfo = null;
            Program.MetaData.GetModAsync(missionName,
                mod =>
                {
                    if (!mod.IsMission)
                    {
                        Program.MainWindow.InvokeFunc(() => { WarningBar.DisplayWarning(string.Format("{0} is not a valid mission", missionName)); });
                    }

                    else modInfo = mod;

                    metaWait.Set();
                },
                error =>
                {
                    Program.MainWindow.InvokeFunc(() =>
                    {
                        WarningBar.DisplayWarning(string.Format("Download of metadata failed: {0}", error.Message));
                        //container.btnStop.Enabled = true;
                    });
                    metaWait.Set();
                });

            var downloads = new List<Download>() { down, engine }.Where(x => x != null).ToList();
            if (downloads.Count > 0)
            {
                var dd = new WaitDownloadDialog(downloads);
                if (dd.ShowDialog(Program.MainWindow) == DialogResult.Cancel)
                {
                    Program.MainWindow.InvokeFunc(() => Program.NotifySection.RemoveBar(this));
                    return;
                }
            }
            metaWait.WaitOne();

            var spring = new Spring(Program.SpringPaths);
            spring.RunLocalScriptGame(modInfo.MissionScript, Program.TasClient.ServerWelcome.Engine ?? GlobalConst.DefaultEngineOverride);
            var cs = GlobalConst.GetContentService();
            cs.NotifyMissionRun(Program.Conf.LobbyPlayerName, missionName);
            spring.SpringExited += (o, args) => RecordMissionResult(spring, modInfo);
            Program.MainWindow.InvokeFunc(() => Program.NotifySection.RemoveBar(this));
        }
        public void StartReplay(string demoUrl, string modName, string mapName, string engineVersion)
        {
            var downloads = new List<Download>();
            downloads.Add(Program.Downloader.GetResource(DownloadType.ENGINE, engineVersion));
            downloads.Add(Program.Downloader.GetResource(DownloadType.MOD, modName));
            downloads.Add(Program.Downloader.GetResource(DownloadType.MAP, mapName));
            downloads.Add(Program.Downloader.GetResource(DownloadType.DEMO, demoUrl));
            downloads = downloads.Where(x => x != null).ToList();
            if (downloads.Count > 0)
            {
                var dd = new WaitDownloadDialog(downloads);
                if (dd.ShowDialog(Program.MainWindow) == DialogResult.Cancel)
                {
                    Program.NotifySection.RemoveBar(this);
                    return;
                }
            }

            var path = Utils.MakePath(Program.SpringPaths.WritableDirectory, "demos", new Uri(demoUrl).Segments.Last());
            try
            {
                var optirun = Environment.GetEnvironmentVariable("OPTIRUN"); //get OPTIRUN filename from OS
                var springFilename = Program.SpringPaths.GetSpringExecutablePath(engineVersion); //use springMT or standard

                Program.SpringPaths.SetDefaultEnvVars(null, engineVersion);
                if (string.IsNullOrEmpty(optirun))
                {
                    Process.Start(springFilename, string.Format("\"{0}\" --config \"{1}\"", path, Program.SpringPaths.GetSpringConfigPath()));
                }
                else
                {
                    Process.Start(optirun,
                        string.Format("\"{0}\" \"{1}\" --config \"{2}\"", springFilename, path, Program.SpringPaths.GetSpringConfigPath()));
                }

                Program.MainWindow.InvokeFunc(() => Program.NotifySection.RemoveBar(this));
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error starting replay: {0}", ex);
                Program.MainWindow.InvokeFunc(() =>
                {
                    WarningBar.DisplayWarning(string.Format("Error starting replay {0}", demoUrl));
                });
            }
        }
예제 #3
0
        /// <summary>
        /// singleton, dont use, internal for designer
        /// </summary>
        internal BattleBar()
        {
            InitializeComponent();

            Program.ToolTip.SetText(btnLeave, "Leave this battle");

            picoChat.ChatBackgroundColor = TextColor.background; //same color as Program.Conf.BgColor
            picoChat.IRCForeColor = 14; //mirc grey. Unknown use
            picoChat.DefaultTooltip = "Last lines from room chat, click to enter full screen chat";

            gameBox.BackColor = Color.Transparent;
            btnStart.Image = Buttons.fight.GetResizedWithCache(38, 38);
            btnStart.ImageAlign = ContentAlignment.MiddleCenter;
            btnStart.TextImageRelation = TextImageRelation.ImageAboveText;
            btnStart.Text = "Play";

            btnLeave.Image = Buttons.exit.GetResizedWithCache(38, 38);
            btnLeave.ImageAlign = ContentAlignment.MiddleCenter;
            btnLeave.TextImageRelation = TextImageRelation.ImageAboveText;
            btnLeave.Text = "Leave";

            Program.ToolTip?.SetText(btnStart, "Start battle");
            Program.ToolTip?.SetText(btnLeave, "Quit battle");

            picoChat.Visible = false;


            btnStart.Click += btnStart_Click;
            btnLeave.Click += BtnLeaveClick;

            client = Program.TasClient;
            spring = new Spring(Program.SpringPaths);


            try
            {
                // silly way to create speech and voice engines on runtime - needed due to mono crash
                speech = Activator.CreateInstance(Type.GetType("ZeroKLobby.ChatToSpeech"), spring);
            }
            catch (Exception ex)
            {
                Trace.TraceWarning("Failed to init VoiceCommands:{0}", ex.Message);
            }

            spring.SpringExited += (s, e) =>
                {
                    client.ChangeMyUserStatus(isInGame: false);

                    if (e.IsCrash)
                    {
                        Program.MainWindow.InvokeFunc(() =>
                            {
                                var defaultButton = MessageBoxDefaultButton.Button2;
                                var icon = MessageBoxIcon.None;
                                if (
                                    MessageBox.Show(this, "Do you want me to set Low details?\n(will effect: lups.cfg and springsettings.cfg)\n\nIf you wish to file a bug report, please include a copy of infolog.txt in your game data folder (accessible through Settings).\nUpload it to a text sharing site such as pastebin.com.",
                                                    "Spring engine has crashed, update your video and audio drivers please!",
                                                    MessageBoxButtons.YesNo,
                                                    icon,
                                                    defaultButton) == DialogResult.Yes)
                                {
                                    Program.Conf.UseSafeMode = true;
                                    Program.EngineConfigurator.Configure(true, 0);
                                }
                            });
                    }
                };

            spring.SpringStarted += (s, e) =>
            {
                Program.MainWindow.SwitchMusicOnOff(false);
                client.ChangeMyUserStatus(isInGame: true);
            };

            client.Rang += (s, e) =>
                {
                    if (e.Place == SayPlace.Channel)
                        MainWindow.Instance.NotifyUser($"chat/{e.Target}", $"Attention needed in {e.Target} channel", true, true);
                    else 
                    {
                        MainWindow.Instance.NotifyUser("chat/battle", "Someone demands your attention in battle room!", true, true);
                        AutoRespond();
                    }
                };



            client.BattleJoined += (s, e) =>
                {
                    if (!isVisible) ManualBattleStarted();
                    if (IsHostGameRunning()) btnStart.Text = "Rejoin";
                    else btnStart.Text = "Start";


                    //client.ChangeMyUserStatus(false, false);
                    var battle = client.MyBattle;
                    lastBattleFounder = battle.FounderName;

                    //Title = string.Format("Joined battle room hosted by {0}", battle.Founder.Name);
                    //TitleTooltip = "Use button on the left side to start a game";
                    radioPlay.Visible = true;
                    radioSpec.Visible = true;
                    btnStart.Visible = true;

                    Program.Downloader.GetResource(DownloadType.MAP, battle.MapName);
                    Program.Downloader.GetResource(DownloadType.MOD, battle.ModName);
                    Program.Downloader.GetResource(DownloadType.ENGINE, battle.EngineVersion);

                    if (gameBox.Image != null) gameBox.Image.Dispose();
                    CreateBattleIcon(Program.BattleIconManager.GetBattleIcon(battle.BattleID));

                    RefreshTooltip();

                    if (Program.TasClient.MyBattle != null) NavigationControl.Instance.Path = "chat/battle";

                    client.ChangeMyBattleStatus(desiredSpectatorState, HasAllResources() ? SyncStatuses.Synced : SyncStatuses.Unsynced, 0);
                };


            client.MyBattleMapChanged += (s, e) =>
                {
                    if (client.MyBattle != null && !Program.SpringScanner.HasResource(client.MyBattle.MapName))
                    {
                        client.ChangeMyBattleStatus(syncStatus: SyncStatuses.Unsynced);
                        Program.Downloader.GetResource(DownloadType.MAP, client.MyBattle.MapName);
                    }
                    RefreshTooltip();
                };

            client.MyBattleHostExited += (s, e) => { btnStart.Text = "Start"; };

            client.ConnectSpringReceived += (s, e) =>
            {
                try
                {
                    btnStart.Text = "Rejoin";

                    List<Download> downloads = new List<Download>();
                    downloads.Add(Program.Downloader.GetResource(DownloadType.ENGINE, e.Engine));
                    downloads.Add(Program.Downloader.GetResource(DownloadType.MOD, e.Game));
                    downloads.Add(Program.Downloader.GetResource(DownloadType.MAP, e.Map));
                    downloads = downloads.Where(x => x != null).ToList();
                    if (downloads.Count > 0)
                    {
                        var dd = new WaitDownloadDialog(downloads);
                        if (dd.ShowDialog(Program.MainWindow) == DialogResult.Cancel) return;
                    }
                    
                    if (spring.IsRunning) spring.ExitGame();
                    spring.ConnectGame(e.Ip, e.Port, client.UserName, e.ScriptPassword, e.Engine);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Error starting spring: " + ex.Message);
                }
                RefreshTooltip();
            };

            client.BattleMyUserStatusChanged += (s, e) =>
                {
                    if (client.MyBattleStatus != null)
                    {
                        //btnStart.Enabled = client.MyBattleStatus.SyncStatus == SyncStatuses.Synced;

                        if (client.MyBattleStatus.IsSpectator && radioPlay.Checked) ChangeGuiSpectatorWithoutEvent(false); // i was spectated
                        if (!client.MyBattleStatus.IsSpectator && radioSpec.Checked) ChangeGuiSpectatorWithoutEvent(true); //i was unspectated
                    }
                };

            client.BattleClosed += (s, e) =>
                {
                    btnStart.Text = "Start";
                    if (gameBox.Image != null) gameBox.Image.Dispose();
                    gameBox.Image = null;
                    RefreshTooltip();
                    Stop();
                };

            client.MyBattleRemoved += (s, e) =>
                {
                    var t = new Timer();
                    var tryCount = 0;
                    t.Interval = 1000;
                    t.Tick += (s2, e2) =>
                        {
                            tryCount++;
                            if (tryCount > 15)
                            {
                                t.Stop();
                                t.Dispose();
                            }
                            else if (client.IsLoggedIn && client.MyBattle == null)
                            {
                                var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.FounderName == lastBattleFounder && !x.IsPassworded);
                                if (bat != null)
                                {
                                    ActionHandler.JoinBattle(bat.BattleID, null);
                                    t.Stop();
                                    t.Dispose();
                                }
                            }
                        };
                    t.Start();
                };

            client.ConnectionLost += (s, e) =>
                {
                    if (gameBox.Image != null) gameBox.Image.Dispose();
                    gameBox.Image = null;
                    RefreshTooltip();
                    Stop();
                };



            timer.Tick += (s, e) =>
                {
                    if (client.IsLoggedIn)
                    {
                        if (WindowsApi.IdleTime.TotalMinutes > Program.Conf.IdleTime)
                        {
                            if (!client.MyUser.IsAway) client.ChangeMyUserStatus(isAway: true);
                        }
                        else
                        {
                            if (client.MyUser.IsAway) client.ChangeMyUserStatus(isAway: false);
                        }
                        CheckMyBattle();
                    }
                };


            Program.MainWindow.navigationControl.PageChanged += s =>
            {
                if (s != "chat/battle")
                {
                    picoChat.Visible = true;
                }
                else picoChat.Visible = false;
            };

            timer.Interval = 1000;
            timer.Start();

            Program.BattleIconManager.BattleChanged += BattleIconManager_BattleChanged;

            //picoChat.Font = new Font(Program.Conf.ChatFont.FontFamily, Program.Conf.ChatFont.Size*0.8f);
            picoChat.ShowHistory = false;
            picoChat.ShowJoinLeave = false;
            //picoChat.HideScroll = true;

            BattleChatControl.BattleLine += (s, e) => picoChat.AddLine(e.Data);

            picoChat.MouseClick += (s, e) => NavigationControl.Instance.Path = "chat/battle";
        }
        public void StartScriptMission(string missionName)
        {
            var serv = GlobalConst.GetContentService();
            var profile = serv.GetScriptMissionData(missionName);
            var downloads = new List<Download>();
            downloads.Add(Program.Downloader.GetResource(DownloadType.RAPID, profile.ModName));
            downloads.Add(Program.Downloader.GetResource(DownloadType.MAP, profile.MapName));
            downloads.Add(Program.Downloader.GetResource(DownloadType.ENGINE, Program.TasClient.ServerWelcome.Engine ?? GlobalConst.DefaultEngineOverride));
            if (profile.ManualDependencies != null) foreach (var entry in profile.ManualDependencies) if (!string.IsNullOrEmpty(entry)) downloads.Add(Program.Downloader.GetResource(DownloadType.NOTKNOWN, entry));

            downloads = downloads.Where(x => x != null).ToList();

            if (downloads.Count > 0)
            {
                var dd = new WaitDownloadDialog(downloads);
                if (dd.ShowDialog(Program.MainWindow) == DialogResult.Cancel) return;
            }

            var spring = new Spring(Program.SpringPaths);
            var name = Program.Conf.LobbyPlayerName;
            if (string.IsNullOrEmpty(name)) name = "Player";

            spring.RunLocalScriptGame(
                profile.StartScript.Replace("%MOD%", profile.ModName).Replace("%MAP%", profile.MapName).Replace("%NAME%", name),
                Program.TasClient.ServerWelcome.Engine ?? GlobalConst.DefaultEngineOverride);
            serv.NotifyMissionRun(name, profile.Name);
        }