Example #1
0
        private static void RespCallback(IAsyncResult asynchronousResult)
        {
            RequestState     requestState     = (RequestState)asynchronousResult.AsyncState;
            UpdateDownloader updateDownloader = requestState.UpdateDownloader;

            updateDownloader.versionInfoReceived.Set();

            try
            {
                WebResponse  response      = requestState.Request.EndGetResponse(asynchronousResult);
                StreamReader sr            = new StreamReader(response.GetResponseStream());
                string       versionString = sr.ReadLine();
                Version      version       = new Version(versionString.Trim());
                updateDownloader.latestVersion = version;
            }
            catch (WebException ex)
            {
                Graphics.Application.Log("Failed to find latest available version: " + ex.Message);
            }
            catch (Exception ex)
            {
                Graphics.Application.Log("Couldn't parse latest available version: " + ex.Message);
            }
        }
        static void Main(string[] args)
        {
            Application.UnhandledApplicationException += new EventHandler<UnhandledExceptionEventArgs>(Application_UnhandledApplicationException);
            Application.UnhandledThreadException += new EventHandler<System.Threading.ThreadExceptionEventArgs>(Application_UnhandledThreadException);

            DefaultSettings = LoadSettings("DefaultSettings") ?? new Settings();

            if (DefaultSettings.UseAppDataWorkingDirectory)
            {
                Application.ApplicationDataFolder =
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/" +
                        DefaultSettings.ApplicationDataFolder + "/";
            }

            if (!Directory.Exists(Application.ApplicationDataFolder + "Logs"))
                Directory.CreateDirectory(Application.ApplicationDataFolder + "Logs");
            Application.LogInit();

            String errorMessage;
            if (!CheckValidFrameworkSetup(out errorMessage))
            {
                if (System.Windows.Forms.MessageBox.Show(
                    String.Format(Locale.Resource.ErrorWrongFrameworkVersion, errorMessage),
                    Locale.Resource.ErrorWrongFrameworkVersionTitle,
                    System.Windows.Forms.MessageBoxButtons.YesNo,
                    System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No)
                    return;
                else
                    Application.Log("User ignored missing framework warning");
            }

            Settings = LoadSettings(Application.ApplicationDataFolder + "Settings");

            Graphics.Renderer.Results result;
            if (Settings == null)
            {
                Settings = (Settings)DefaultSettings.Clone();

                result = Graphics.GraphicsDevice.SettingsUtilities.Initialize(Settings.GraphicsDeviceSettings.DeviceMode);
                Settings.InitilizeToDefaults();
            }
            else
            {
                result = Graphics.GraphicsDevice.SettingsUtilities.Initialize(Settings.GraphicsDeviceSettings.DeviceMode);
                Application.Log("Defualt video quality: " + Enum.GetValues(typeof(VideoQualities)).GetValue(3 - Graphics.GraphicsDevice.SettingsUtilities.FindRecommendedVideoSettings()));
            }

            Settings.WindowedFullscreenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;

            foreach (var v in args)
            {
                var ss = v.Split('=');
                var p = Settings.GetType().GetProperty(ss[0].Substring(1));
                if (p != null)
                {
                    object value = null;
                    if (p.PropertyType == typeof(string))
                    {
                        value = ss[1];
                        if (ss[1][0] == '\'' && ss[1][ss[1].Length - 1] == '\'')
                            value = ss[1].Substring(1, ss[1].Length - 2);
                    }
                    else value = System.Convert.ChangeType(ss[1], p.PropertyType);
                    Application.Log("Command line argument: " + p.Name + "=" + value);
                    p.SetValue(Settings, value, null);
                }
            }

            Application.Log("Application start up screen resolution: " + Settings.WindowedFullscreenSize.Width + " x " + Settings.WindowedFullscreenSize.Height);

            SetActiveCulture(Program.Settings.ActiveLanguage);

#if !DEBUG
            if(!System.Diagnostics.Debugger.IsAttached)
                Application.Log("Installation directory file hashes:", GetApplicationFileInfoString());
#endif

            if (result == Graphics.Renderer.Results.VideoCardNotSupported)
            {
                System.Windows.Forms.MessageBox.Show("Your graphics card is not supported. Game is shutting down.");
                Process.GetCurrentProcess().Kill();
            }
            else if(result == Graphics.Renderer.Results.VideoCardNotRecommended)
            {
                System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(Locale.Resource.ErrorBadVideoCard, Locale.Resource.GenWarning, System.Windows.Forms.MessageBoxButtons.OKCancel);
                if (dialogResult == System.Windows.Forms.DialogResult.Cancel)
                    Process.GetCurrentProcess().Kill();
            }


            FeedbackCommon.SendableBase.SendablePosted += new Action<FeedbackCommon.SendableBase>(FeedbackSendablePosted);
            FeedbackCommon.SendableBase.SendablePostingError += new Action<FeedbackCommon.SendableBase, Exception>(FeedbackSendablePostingError);

            Graphics.Content.Font.DefaultEncoding = Settings.DefaultEncoding;

            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
            //Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
            //System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);
#if TIME_LIMITED
#warning TIME LIMITED RELEASE
            if (DateTime.Now > new DateTime(2011, 5, 2))
            {
                System.Windows.Forms.MessageBox.Show("This version has expired. Please visit http://deadmeetslead.com to purchase the full game.");
                return;
            }
#endif
            UpdateDownloader.ClearOldInstallers();
            UpdateDownloader updateDownloader = new UpdateDownloader(Directory.GetCurrentDirectory());
            if (Settings.CheckForUpdate)
            {
                Application.Log("Start looking for latest version");
                System.Threading.ThreadPool.QueueUserWorkItem((w) =>
                {
                    updateDownloader.SendLatestVersionRequest();
                });
                Application.Log("End looking for latest version");
                //if (UpdateDownloader.TryToFindUpdate(Directory.GetCurrentDirectory()))
                //    return;     // installer launched
            }

            using (Window = new System.Windows.Forms.Form
            {
                BackColor = Color.Black,
                Icon = new Icon(global::Common.FileSystem.Instance.OpenRead(Program.DataPath + "/Interface/Common/DesktopIcon1.ico")),
                Text = Locale.Resource.DeadMeetsLead
            })
            {
                Application.Init(Window);

                ProcessCrashStorageDirectory();     // resend any failed send-attempts

                Application.Log("Client assembly version: " + typeof(Program).Assembly.GetName());
                Application.Log("ApplicationData folder: " + Application.ApplicationDataFolder);

                if (Settings.ChallengeMapMode)
                    Application.Log("Mode: Challenger");
                else
                    Application.Log("Mode: Normal");

                System.Windows.Forms.Cursor.Hide();

                Window.Width = Settings.WindowSize.Width;
                Window.Height = Settings.WindowSize.Height;
                Window.WindowState = Settings.WindowState;
                Window.FormBorderStyle = (Settings.WindowMode == WindowMode.Windowed) ? System.Windows.Forms.FormBorderStyle.Sizable : System.Windows.Forms.FormBorderStyle.None;

                Window.Controls.Add(Instance = new Program
                {
                    Dock = System.Windows.Forms.DockStyle.Fill,
                    WindowMode = Settings.WindowMode,
                    GraphicsDevice = new GraphicsDevice9() { Settings = Settings.GraphicsDeviceSettings },
                    updateDownloader = updateDownloader
                });

                Instance.args = args;
                UpdateWindowMode();
                // Program runs here
                System.Windows.Forms.Application.Run(Window);
                // After program close
                Instance.SaveControls();
                SaveSettings();
                if (updateMode)
                    updateDownloader.ShowDialog();      // Download update
            }
        }