Ejemplo n.º 1
0
        public void OnHostEvent(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState is Exception)
            {
                Exception ex = e.UserState as Exception;
                PopupString(ex.ToString() + "\r\n" + ex.StackTrace.ToString());
            }
            if (e.UserState is string)
            {
                /*
                 * string line = (string)e.UserState;
                 *
                 * textHistory.AppendText(line + "\r\n");
                 * textHistory.ScrollToEnd();
                 */
            }
            else if (e.UserState is EvalBundle)
            {
                EvalBundle b = (EvalBundle)e.UserState;
                ProcessEvals(b);
            }
            else if (e.UserState is DictBundle)
            {
                DictBundle b = (DictBundle)e.UserState;

                if (b.path == "download-result")
                {
                    OfferDownloads(b);
                    if (accessLibraryDlg != null)
                    {
                        accessLibraryDlg.Consider(b);
                    }
                }
                else
                {
                    if (b.path == "_gameaid/_filenames")
                    {
                        filenameDict = b.dict;
                    }

                    partyInfo1.Consider(b);
                    partyInfo2.Consider(b);
                    vs1.Consider(b);
                    readyRolls.Consider(b);

                    if (openSaveRollsDlg != null)
                    {
                        openSaveRollsDlg.Consider(b);
                    }

                    map1.Consider(b);
                    map2.Consider(b);

                    foreach (var sq in Squad.Children)
                    {
                        var sheet = sq as VirtualSheet;
                        if (sheet != null)
                        {
                            sheet.Consider(b);
                        }
                    }
                }
            }
            else if (e.UserState is DownloadFile)
            {
                DownloadFile file = e.UserState as DownloadFile;

                // Configure open file dialog box
                var dlg = new System.Windows.Forms.SaveFileDialog();
                dlg.Title           = "Save Download";
                dlg.FileName        = file.name;
                dlg.DefaultExt      = ".xlsx";                           // Default file extension
                dlg.Filter          = "Character Sheets (.xlsx)|*.xlsx"; // Filter files by extension
                dlg.OverwritePrompt = true;
                dlg.CheckPathExists = true;

                // Show open file dialog box
                System.Windows.Forms.DialogResult result = dlg.ShowDialog();

                // Process open file dialog box results
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        System.IO.File.WriteAllBytes(dlg.FileName, file.bytes);
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
            else if (e.UserState is AudioReport)
            {
                AudioReport audio = e.UserState as AudioReport;

                string url = "";

                switch (audio.desc)
                {
                case "speak":
                    if (reader == null)
                    {
                        try
                        {
                            // tolerate reader acquisition failure more gracefully

                            reader = new SpeechSynthesizer();

                            // var l = new List<VoiceInfo>();
                            //
                            // foreach (InstalledVoice voice in reader.GetInstalledVoices())
                            // {
                            //     VoiceInfo info = voice.VoiceInfo;
                            //     string AudioFormats = "";
                            //     foreach (SpeechAudioFormatInfo fmt in info.SupportedAudioFormats)
                            //     {
                            //         AudioFormats += String.Format("{0}\n",
                            //         fmt.EncodingFormat.ToString());
                            //     }
                            //
                            //     l.Add(info);
                            //
                            // }

                            reader.SelectVoice("Microsoft Zira Desktop");
                        }
                        catch
                        {
                        }
                    }

                    if (reader != null)
                    {
                        reader.SpeakAsync(audio.text);
                    }
                    break;

                case "ownage":
                    switch (audio.killcount)
                    {
                    case 1:
                        url = "http://myserver.com/uploads/killshot/1.mp3";
                        break;

                    case 2:
                        url = "http://myserver.com/uploads/killshot/4.mp3";
                        break;

                    case 3:
                        url = "http://myserver.com/uploads/killshot/6.mp3";
                        break;

                    case 4:
                        url = "http://myserver.com/uploads/killshot/7.mp3";
                        break;

                    case 5:
                        url = "http://myserver.com/uploads/killshot/10.mp3";
                        break;

                    case 6:
                        url = "http://myserver.com/uploads/killshot/9.mp3";
                        break;

                    case 7:
                    default:
                        url = "http://myserver.com/uploads/killshot/14.mp3";
                        break;
                    }
                    break;

                case "fumble":
                    url = "http://myserver.com/uploads/killshot/0.mp3";
                    break;

                default:
                    url = "http://myserver.com/uploads/misc/" + audio.desc;
                    break;
                }

                if (url != "")
                {
                    var player = new System.Windows.Media.MediaPlayer();
                    player.Open(new Uri(url));
                    player.MediaOpened += new EventHandler((object s, EventArgs a) => { player.Play(); });
                    player.MediaEnded  += new EventHandler((object s, EventArgs a) => { player.Close(); });
                }
            }
            else if (e.UserState is HoursReport)
            {
                HoursReport h = e.UserState as HoursReport;
                vs1.SetRemainingHours(h.hours);
            }
        }
Ejemplo n.º 2
0
        public void HandleHost(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker b = (BackgroundWorker)sender;
            DictBundle       pending = null;
            string           a1, a2, a3, a4;

            for (;;)
            {
                try
                {
                    hostClient = new System.Net.Sockets.TcpClient(server, 6668);

                    NetworkStream      stream      = hostClient.GetStream();
                    HybridStreamReader r           = new HybridStreamReader(stream);
                    HybridStreamReader savedStream = null;

                    SendHost("nick " + nick);
                    SendHost("dir /");
                    SendHost("dir _maps/default");

                    string line;

                    for (; ;)
                    {
                        if ((line = r.ReadLine()) == null)
                        {
                            if (savedStream != null)
                            {
                                r           = savedStream;
                                savedStream = null;
                                continue;
                            }

                            break;
                        }

                        if (line.StartsWith("hours "))
                        {
                            int hours;
                            Parse2(line, out a1, out a2);
                            Int32.TryParse(a2, out hours);

                            var h = new HoursReport();
                            h.hours = hours;
                            b.ReportProgress(1, h);
                        }
                        else if (line.StartsWith("audio "))
                        {
                            int count;
                            Parse3(line, out a1, out a2, out a3);

                            var audio = new AudioReport();
                            audio.desc = a2;
                            if (a2 == "ownage")
                            {
                                Int32.TryParse(a3, out count);
                                audio.killcount = count;
                            }
                            else
                            {
                                audio.text = a3;
                            }

                            b.ReportProgress(1, audio);
                        }
                        else if (line.StartsWith("download "))
                        {
                            int len;
                            Parse3(line, out a1, out a2, out a3);
                            Int32.TryParse(a3, out len);
                            var bytes = new byte[len];
                            r.Read(bytes);

                            var download = new DownloadFile();
                            download.name  = a2;
                            download.bytes = bytes;
                            b.ReportProgress(1, download);
                        }
                        else if (line.StartsWith("compressed "))
                        {
                            int clen, len;
                            Parse3(line, out a1, out a2, out a3);
                            Int32.TryParse(a2, out clen);
                            Int32.TryParse(a3, out len);

                            // switch to the saved stream
                            savedStream = r;
                            r           = UncompressBytes(r, clen);

                            if (r == null)
                            {
                                r = savedStream;
                            }
                            continue;
                        }
                        else if (line.StartsWith("begin eval "))
                        {
                            pending = new EvalBundle();
                            Parse3(line, out a1, out a2, out a3);
                            pending.path = a3;
                        }
                        else if (line.StartsWith("begin dir "))
                        {
                            pending = new DictBundle();
                            Parse3(line, out a1, out a2, out a3);
                            pending.path = a3;
                        }
                        else if (pending != null && line.StartsWith("v "))
                        {
                            Parse4(line, out a1, out a2, out a3, out a4);
                            if (a1 != "v" || a2 != pending.path)
                            {
                                continue;
                            }

                            if (!pending.dict.ContainsKey(a3))
                            {
                                pending.dict.Add(a3, a4);
                            }
                        }
                        else if (line.StartsWith("end dir "))
                        {
                            b.ReportProgress(1, pending);
                            pending = null;
                        }
                        else if (line.StartsWith("end eval "))
                        {
                            b.ReportProgress(1, pending);
                            pending = null;
                        }
                        else if (pending == null)
                        {
                            b.ReportProgress(1, line);
                        }
                    }
                }
                catch (Exception ex)
                {
                    b.ReportProgress(0, ex);
                    System.Threading.Thread.Sleep(5000);
                }
            }
        }