diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Updater')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs | 118 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Updater/Images/warning.png | bin | 8947 -> 0 bytes | |||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml | 35 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs | 174 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Updater/Tango.PPC.Updater.csproj | 4 |
5 files changed, 32 insertions, 299 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs b/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs deleted file mode 100644 index 3a43be6f9..000000000 --- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Security.Principal; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.PPC.Updater -{ - public static class IdentityUtils - { - [DllImport("advapi32.dll", SetLastError = true)] - static extern bool GetTokenInformation(IntPtr tokenHandle, TokenInformationClass tokenInformationClass, IntPtr tokenInformation, int tokenInformationLength, out int returnLength); - - /// <summary> - /// Passed to <see cref="GetTokenInformation"/> to specify what - /// information about the token to return. - /// </summary> - enum TokenInformationClass - { - TokenUser = 1, - TokenGroups, - TokenPrivileges, - TokenOwner, - TokenPrimaryGroup, - TokenDefaultDacl, - TokenSource, - TokenType, - TokenImpersonationLevel, - TokenStatistics, - TokenRestrictedSids, - TokenSessionId, - TokenGroupsAndPrivileges, - TokenSessionReference, - TokenSandBoxInert, - TokenAuditPolicy, - TokenOrigin, - TokenElevationType, - TokenLinkedToken, - TokenElevation, - TokenHasRestrictions, - TokenAccessInformation, - TokenVirtualizationAllowed, - TokenVirtualizationEnabled, - TokenIntegrityLevel, - TokenUiAccess, - TokenMandatoryPolicy, - TokenLogonSid, - MaxTokenInfoClass - } - - /// <summary> - /// The elevation type for a user token. - /// </summary> - enum TokenElevationType - { - TokenElevationTypeDefault = 1, - TokenElevationTypeFull, - TokenElevationTypeLimited - } - - public static bool IsElevated() - { - var identity = WindowsIdentity.GetCurrent(); - if (identity == null) throw new InvalidOperationException("Couldn't get the current user identity"); - var principal = new WindowsPrincipal(identity); - - // Check if this user has the Administrator role. If they do, return immediately. - // If UAC is on, and the process is not elevated, then this will actually return false. - //if (principal.IsInRole(WindowsBuiltInRole.Administrator)) return true; - - //// If we're not running in Vista onwards, we don't have to worry about checking for UAC. - //if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6) - //{ - // // Operating system does not support UAC; skipping elevation check. - // return false; - //} - - int tokenInfLength = Marshal.SizeOf(typeof(int)); - IntPtr tokenInformation = Marshal.AllocHGlobal(tokenInfLength); - - try - { - var token = identity.Token; - var result = GetTokenInformation(token, TokenInformationClass.TokenElevationType, tokenInformation, tokenInfLength, out tokenInfLength); - - if (!result) - { - var exception = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); - throw new InvalidOperationException("Couldn't get token information", exception); - } - - var elevationType = (TokenElevationType)Marshal.ReadInt32(tokenInformation); - - switch (elevationType) - { - case TokenElevationType.TokenElevationTypeDefault: - // TokenElevationTypeDefault - User is not using a split token, so they cannot elevate. - return false; - case TokenElevationType.TokenElevationTypeFull: - // TokenElevationTypeFull - User has a split token, and the process is running elevated. Assuming they're an administrator. - return true; - case TokenElevationType.TokenElevationTypeLimited: - // TokenElevationTypeLimited - User has a split token, but the process is not running elevated. Assuming they're an administrator. - return false; - default: - // Unknown token elevation type. - return false; - } - } - finally - { - if (tokenInformation != IntPtr.Zero) Marshal.FreeHGlobal(tokenInformation); - } - } - } -} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/Images/warning.png b/Software/Visual_Studio/PPC/Tango.PPC.Updater/Images/warning.png Binary files differdeleted file mode 100644 index 26ccc3ecb..000000000 --- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/Images/warning.png +++ /dev/null diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml b/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml index 351ca0838..a5e63477f 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml +++ b/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml @@ -6,43 +6,12 @@ xmlns:local="clr-namespace:Tango.PPC.Updater" mc:Ignorable="d" WindowStyle="None" ResizeMode="NoResize" Width="800" Height="1280"> - - <Window.Resources> - <SolidColorBrush x:Key="Foreground" Color="#1c63ea" /> - - <Style TargetType="Button" x:Key="Button"> - <Setter Property="Foreground" Value="{StaticResource Foreground}"></Setter> - <Setter Property="FontSize" Value="18"></Setter> - <Setter Property="FontWeight" Value="SemiBold"></Setter> - <Setter Property="Margin" Value="20 0"></Setter> - <Setter Property="Template"> - <Setter.Value> - <ControlTemplate TargetType="Button"> - <Border BorderBrush="{StaticResource Foreground}" BorderThickness="1" CornerRadius="25" Background="Transparent"> - <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}" /> - </Border> - </ControlTemplate> - </Setter.Value> - </Setter> - </Style> - </Window.Resources> <Grid> - <StackPanel x:Name="stackProgress" HorizontalAlignment="Center" VerticalAlignment="Center"> + <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Image Source="/Images/package.png" Width="200" /> <TextBlock x:Name="txtStatus" Margin="0 100 0 0" FontSize="16" Text="Updating Tango..." HorizontalAlignment="Center"></TextBlock> - <ProgressBar x:Name="prog" Margin="0 30 0 0" Width="500" Height="10" Maximum="100" Value="0" BorderThickness="0" Foreground="{StaticResource Foreground}"></ProgressBar> - </StackPanel> - - <StackPanel x:Name="stackFailed" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0 460 0 0" Visibility="Collapsed"> - <Image Source="/Images/warning.png" Width="200" RenderOptions.BitmapScalingMode="Fant" /> - <TextBlock Margin="0 100 0 0" FontSize="16" Text="Update Failed" Foreground="#FF0072" FontWeight="SemiBold" HorizontalAlignment="Center"></TextBlock> - <TextBlock x:Name="txtError" Margin="0 20 0 0" FontSize="16" Foreground="Gray" Text="Unexpected error" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Height="100"></TextBlock> - - <UniformGrid Width="600" Height="55" Columns="2" Margin="0 200 0 0"> - <Button x:Name="btnAbort" Content="ABORT" Style="{StaticResource Button}"></Button> - <Button x:Name="btnRetry" Content="RETRY" Style="{StaticResource Button}"></Button> - </UniformGrid> + <ProgressBar x:Name="prog" Margin="0 30 0 0" Width="500" Height="10" Maximum="100" Value="0" BorderThickness="0" Foreground="#1c63ea"></ProgressBar> </StackPanel> </Grid> </Window> diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs index 8f521c85a..ae4f22420 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; -using System.Security.Principal; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -27,17 +26,16 @@ namespace Tango.PPC.Updater private String _sourceFolder = AppDomain.CurrentDomain.BaseDirectory; private String _msProcessName = "Tango.PPC.UI"; private String _appPath; - private bool EMULATE_EXCEPTION = false; public MainWindow() { //Launch debugger.. -#if DEBUG - if (!Debugger.IsAttached) - { - Debugger.Launch(); - } -#endif + //#if DEBUG + // if (!Debugger.IsAttached) + // { + // Debugger.Launch(); + // } + //#endif InitializeComponent(); @@ -58,16 +56,8 @@ namespace Tango.PPC.Updater Width = touch_screen.Bounds.Width; Height = touch_screen.Bounds.Height; } - else - { - Top = 0; - Left = 0; - } ContentRendered += MainWindow_ContentRendered; - - btnRetry.Click += BtnRetry_Click; - btnAbort.Click += BtnAbort_Click; } private void MainWindow_ContentRendered(object sender, EventArgs e) @@ -79,32 +69,7 @@ namespace Tango.PPC.Updater { try { - ShowProgress(); - - if (!IdentityUtils.IsElevated()) - { - ShowError("The updater utility is not running under elevated permissions and cannot perform.\nThe process will restart."); - var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; - ProcessStartInfo startInfo = new ProcessStartInfo(exeName); - startInfo.Arguments = String.Join(" ", App.StartupArgs); - startInfo.Verb = "runas"; - Process.Start(startInfo); - Environment.Exit(0); - return; - } - } - catch { } - - try - { Init(); - - if (EMULATE_EXCEPTION) - { - EMULATE_EXCEPTION = false; - throw new InvalidOperationException("This is an emulated error."); - } - EnsureTangoIsDown(); RemoveOldDLLFiles(); ReplaceFiles(); @@ -113,31 +78,16 @@ namespace Tango.PPC.Updater DoEvents(); Thread.Sleep(1000); StartTango(true); - Exit(); } catch (Exception ex) { - ShowFailed(ex); + ShowError($"Update failed.\n{ex.Message}"); + StartTango(false); } - } - - private void Exit() - { - try + finally { - foreach (var file in Directory.GetFiles(_sourceFolder, "*.*", SearchOption.AllDirectories)) - { - try - { - File.Delete(file); - } - catch { } - } + Environment.Exit(0); } - catch { } - - - Environment.Exit(0); } private void Init() @@ -145,11 +95,6 @@ namespace Tango.PPC.Updater try { _appPath = String.Join(" ", App.StartupArgs); - - if (!_appPath.EndsWith("\\")) - { - _appPath += "\\"; - } } catch { @@ -173,18 +118,12 @@ namespace Tango.PPC.Updater { p.StartInfo.Arguments = "-update_ok"; } - else - { - p.StartInfo.Arguments = "-update_failed"; - } p.Start(); } private void ReplaceFiles() { - txtStatus.Text = "Updating files..."; - int maxProgress = Directory.GetFiles(_sourceFolder, "*.*", SearchOption.AllDirectories).Length; int progress = 0; @@ -204,6 +143,7 @@ namespace Tango.PPC.Updater { try { + txtStatus.Text = "Copying file " + Path.GetFileName(newPath); DoEvents(); File.Copy(newPath, newPath.Replace(_sourceFolder, _appPath), true); @@ -211,6 +151,8 @@ namespace Tango.PPC.Updater prog.Maximum = maxProgress; prog.Value = progress++; DoEvents(); + + Thread.Sleep(10); } catch (Exception ex) { @@ -243,49 +185,37 @@ namespace Tango.PPC.Updater { Process appProcess = null; - for (int i = 0; i < 20; i++) + int tries = 0; + + do { - try - { - appProcess = Process.GetProcessesByName(_msProcessName).FirstOrDefault(); - } - catch - { - Thread.Sleep(1000); - continue; - } + appProcess = Process.GetProcessesByName(_msProcessName).FirstOrDefault(); + Process p = new Process(); + p.StartInfo.CreateNoWindow = true; + p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + p.StartInfo.FileName = "wmic"; + p.StartInfo.Arguments = String.Format("process where name='{0}' delete", _msProcessName); if (appProcess != null) { - try - { - appProcess.Kill(); - } - catch { } + tries++; + appProcess.Kill(); try { - Process p = new Process(); - p.StartInfo.CreateNoWindow = true; - p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - p.StartInfo.FileName = "wmic"; - p.StartInfo.Arguments = String.Format("process where name='{0}' delete", _msProcessName); p.Start(); } catch { } + + Thread.Sleep(1000); } - else + + if (tries > 10) { - break; + throw new IOException("The main Tango process seems to in a frozen state. Please restart your computer and try again."); } - Thread.Sleep(1000); - } - - if (appProcess != null) - { - throw new IOException("The main Tango process seems to in a frozen state. Please restart your computer and try again."); - } + } while (appProcess != null); } /// <summary> @@ -300,49 +230,5 @@ namespace Tango.PPC.Updater { MessageBox.Show(error, "Tango Update", MessageBoxButton.OK, MessageBoxImage.Error); } - - private bool ShowErrorRetry(String error) - { - var result = MessageBox.Show(error + "\n" + "Press yes to retry.", "Tango Update", MessageBoxButton.YesNo, MessageBoxImage.Error); - return result == MessageBoxResult.Yes; - } - - private void ShowProgress() - { - stackProgress.Visibility = Visibility.Visible; - stackFailed.Visibility = Visibility.Collapsed; - } - - private void ShowFailed(Exception ex) - { - try - { - using (EventLog eventLog = new EventLog("Application")) - { - eventLog.Source = "PPC Updater"; - eventLog.WriteEntry($"PPC Updater Failed\n{ex.ToString()}", EventLogEntryType.Error, 101, 1); - } - } - catch { } - - stackProgress.Visibility = Visibility.Collapsed; - stackFailed.Visibility = Visibility.Visible; - txtError.Text = ex.Message; - } - - private void BtnAbort_Click(object sender, RoutedEventArgs e) - { - ShowProgress(); - txtStatus.Text = "Update failed. Restoring previous application state..."; - DoEvents(); - Thread.Sleep(1000); - StartTango(false); - Exit(); - } - - private void BtnRetry_Click(object sender, RoutedEventArgs e) - { - Update(); - } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/Tango.PPC.Updater.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Updater/Tango.PPC.Updater.csproj index 8f8430102..3f232c4d2 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/Tango.PPC.Updater.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Updater/Tango.PPC.Updater.csproj @@ -67,7 +67,6 @@ <DependentUpon>App.xaml</DependentUpon> <SubType>Code</SubType> </Compile> - <Compile Include="IdentityUtils.cs" /> <Compile Include="MainWindow.xaml.cs"> <DependentUpon>MainWindow.xaml</DependentUpon> <SubType>Code</SubType> @@ -103,9 +102,6 @@ <ItemGroup> <Resource Include="Images\package.png" /> </ItemGroup> - <ItemGroup> - <Resource Include="Images\warning.png" /> - </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <ProjectExtensions> <VisualStudio> |
