4 using System.Collections.Generic;
5 using System.Diagnostics;
9 using System.Threading;
10 using System.Windows.Forms;
11 using SiliconStudio.Assets;
12 using SiliconStudio.Core.Windows;
14 namespace SiliconStudio.LauncherApp
18 private const string LaunchAppRestartOption =
"/LaunchAppRestart";
21 private int previousComplete;
23 private bool isPostDownloading;
24 bool relaunchThisProcess =
false;
27 [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
28 private static void Main()
31 Environment.SetEnvironmentVariable(
"SiliconStudioParadoxDir", Path.GetDirectoryName(typeof(
Program).Assembly.Location));
37 private void RunSafe(Action action)
45 ShowUnhandledException(exception);
49 private void ShowUnhandledException(
Exception exception)
51 var message = AppHelper.BuildErrorToClipboard(exception, string.Format(
"LauncherApp Version: {0}\n", LauncherApp.Version));
52 MessageBox.Show(GetCurrentWindow(),
"An unhandled exception has occured (copied to the clipboard) : \n\n" + message,
"Launcher error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
55 private void Run(
string[] args)
57 if (args.Length > 0 && args[0] == LaunchAppRestartOption)
59 Console.WriteLine(
"Restart, wait for 200ms");
61 args = args.Skip(1).ToArray();
66 launcherApp.DialogAvailable += launcherApp_DialogAvailable;
67 launcherApp.UnhandledException += (sender, exception) => ShowUnhandledException(exception);
69 var evt =
new ManualResetEvent(
false);
71 var splashThread =
new Thread(
75 splashscreen.Initialize(launcherApp);
78 launcherApp.MainWindowHandle = splashscreen.Handle;
81 Application.Run(splashscreen);
88 launcherApp.ProgressAvailable += launcherApp_ProgressAvailable;
89 launcherApp.LogAvailable += launcherApp_LogAvailable;
90 var runningForm = splashscreen;
91 launcherApp.Running += (sender, eventArgs) => SafeWindowClose(runningForm);
93 var result = launcherApp.Run(args);
98 runningForm.ExitOnUserClose =
false;
99 SafeWindowClose(runningForm);
103 if (launcherApp.IsDownloading)
105 isPostDownloading =
true;
106 launcherApp.DownloadFinished += launcherApp_DownloadFinished;
109 splashscreen.Initialize(launcherApp,
"Downloading new version");
111 Application.Run(splashscreen);
118 if (relaunchThisProcess)
120 var newArgs =
new List<string>() { LaunchAppRestartOption };
121 newArgs.AddRange(args);
122 var startInfo =
new ProcessStartInfo(typeof(
Program).Assembly.Location)
124 Arguments = string.Join(
" ", newArgs),
125 WorkingDirectory = Environment.CurrentDirectory,
126 UseShellExecute =
true
128 Process.Start(startInfo);
132 static int GetLauncherAppProcessCount()
134 return Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length;
137 private static void SafeWindowClose(
Form form)
141 form.InvokeSafe(() =>
143 if (!form.IsDisposed)
151 void launcherApp_DownloadFinished(
object sender,
EventArgs e)
153 if (splashscreen != null)
156 var launcherCount = GetLauncherAppProcessCount();
157 if (launcherApp.IsNewPackageAvailable && isPostDownloading && launcherCount == 1)
159 var result = MessageBox.Show(GetCurrentWindow(),
"Do you want to install and run the new version now?",
"Launcher", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
160 relaunchThisProcess = result == DialogResult.Yes;
163 var form = splashscreen;
164 splashscreen.InvokeSafe(form.Close);
170 e.Result = MessageBox.Show(GetCurrentWindow(), e.Text, e.Caption, e.Buttons, e.Icon, e.DefaultButton, e.Options);
173 void launcherApp_ProgressAvailable(
object sender, NuGet.ProgressEventArgs e)
175 if (previousComplete != e.PercentComplete)
177 Console.WriteLine(
"Download {0} {1}",
e.Operation, e.PercentComplete);
179 previousComplete = e.PercentComplete;
182 static void launcherApp_LogAvailable(
object sender, NugetLogEventArgs e)
184 Console.WriteLine(e);
187 IWin32Window GetCurrentWindow()
190 if (System.Diagnostics.Debugger.IsAttached)
196 var windowHandler = Process.GetCurrentProcess().MainWindowHandle;
197 var wnd = windowHandler != IntPtr.Zero ? NativeWindow.FromHandle(windowHandler) : null;
199 return splashscreen != null && !splashscreen.IsDisposed ? (IWin32Window)splashscreen : wnd;
static string[] GetCommandLineArgs()
Operation
Enumeration of the different operations in the new Assimp's material stack.