2019-09-08 21:39:04 +02:00
using System ;
2019-09-09 16:53:11 +02:00
using System.Diagnostics ;
using System.IO ;
using System.Net ;
using System.Reflection ;
using System.Runtime.InteropServices ;
using System.Security.AccessControl ;
using System.Security.Cryptography ;
using System.Security.Principal ;
using System.Threading ;
2019-09-08 21:39:04 +02:00
using System.Windows.Forms ;
2019-09-09 16:53:11 +02:00
using System.Xml.Linq ;
2019-09-09 17:50:33 +02:00
using System.Drawing ;
2019-09-08 21:39:04 +02:00
namespace UpTool2
{
static class Program
{
2019-09-09 17:50:33 +02:00
public static Form splash ;
2019-09-08 21:39:04 +02:00
[STAThread]
static void Main ( )
{
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2019-09-09 17:50:33 +02:00
splash = new Form
{
StartPosition = FormStartPosition . CenterScreen ,
FormBorderStyle = FormBorderStyle . None ,
ControlBox = false ,
MaximizeBox = false ,
MinimizeBox = false ,
ShowIcon = false ,
ShowInTaskbar = false ,
Size = new Size ( 700 , 400 ) ,
ForeColor = Color . Green ,
TopMost = true
} ;
splash . MaximumSize = splash . Size ;
splash . MinimumSize = splash . Size ;
Label splashL = new Label
{
AutoSize = false ,
Dock = DockStyle . Fill ,
TextAlign = ContentAlignment . MiddleCenter ,
Text = "Loading" ,
Font = new Font ( FontFamily . GenericSansSerif , 40 )
} ;
splash . Controls . Add ( splashL ) ;
splash . Show ( ) ;
splash . BringToFront ( ) ;
Thread . Sleep ( 1000 ) ;
2019-09-09 16:53:11 +02:00
string appGuid = ( ( GuidAttribute ) Assembly . GetExecutingAssembly ( ) . GetCustomAttributes ( typeof ( GuidAttribute ) , false ) . GetValue ( 0 ) ) . Value . ToString ( ) ;
string mutexId = string . Format ( "Global\\{{{0}}}" , appGuid ) ;
bool createdNew ;
var allowEveryoneRule = new MutexAccessRule ( new SecurityIdentifier ( WellKnownSidType . WorldSid , null ) , MutexRights . FullControl , AccessControlType . Allow ) ;
var securitySettings = new MutexSecurity ( ) ;
securitySettings . AddAccessRule ( allowEveryoneRule ) ;
using ( var mutex = new Mutex ( false , mutexId , out createdNew , securitySettings ) )
{
var hasHandle = false ;
2019-09-09 17:05:53 +02:00
#if ! DEBUG
2019-09-09 16:53:11 +02:00
try
{
2019-09-09 17:05:53 +02:00
#endif
2019-09-09 16:53:11 +02:00
try
{
hasHandle = mutex . WaitOne ( 5000 , false ) ;
if ( hasHandle = = false )
throw new TimeoutException ( "Timeout waiting for exclusive access" ) ;
}
catch ( AbandonedMutexException )
{
#if DEBUG
Console . WriteLine ( "Mutex abandoned" ) ;
#endif
hasHandle = true ;
}
string dir = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) + @"\UpTool2" ;
2019-10-12 10:38:38 +02:00
if ( ! Directory . Exists ( dir + @"\Apps" ) )
Directory . CreateDirectory ( dir + @"\Apps" ) ;
2019-09-09 16:53:11 +02:00
string xml = dir + @"\info.xml" ;
if ( ! File . Exists ( xml ) )
2019-10-21 15:18:08 +02:00
new XElement ( "meta" , new XElement ( "Version" , 0 ) , new XElement ( "Repos" , new XElement ( "Repo" , new XElement ( "Name" , "UpTool2 official Repo" ) , new XElement ( "Link" , "https://github.com/CreepyCrafter24/UpTool2/releases/download/Repo/Repo.xml" ) ) ) , new XElement ( "LocalRepo" ) ) . Save ( xml ) ;
2019-09-09 17:05:53 +02:00
XElement meta = XDocument . Load ( "https://github.com/CreepyCrafter24/UpTool2/releases/download/Repo/Meta.xml" ) . Element ( "meta" ) ;
2019-09-09 16:53:11 +02:00
int version = int . Parse ( meta . Element ( "Version" ) . Value ) ;
if ( int . Parse ( XDocument . Load ( xml ) . Element ( "meta" ) . Element ( "Version" ) . Value ) < version )
{
2019-10-20 18:31:32 +02:00
if ( new DownloadDialog ( meta . Element ( "File" ) . Value , dir + @"\update.exe" ) . ShowDialog ( ) ! = DialogResult . OK )
throw new Exception ( "Failed to update" ) ;
using ( SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider ( ) )
2019-09-09 16:53:11 +02:00
{
2019-10-20 18:31:32 +02:00
string pkghash = BitConverter . ToString ( sha256 . ComputeHash ( File . ReadAllBytes ( dir + @"\update.exe" ) ) ) . Replace ( "-" , string . Empty ) . ToUpper ( ) ;
if ( pkghash ! = meta . Element ( "Hash" ) . Value . ToUpper ( ) )
throw new Exception ( "The hash is not equal to the one stored in the repo:\r\nPackage: " + pkghash + "\r\nOnline: " + meta . Element ( "Hash" ) . Value . ToUpper ( ) ) ;
2019-09-09 16:53:11 +02:00
}
new XElement ( "meta" , new XElement ( "Version" , version ) ) . Save ( xml ) ;
2019-09-09 17:50:33 +02:00
splash . Hide ( ) ;
2019-10-20 19:20:42 +02:00
Process . Start ( new ProcessStartInfo { FileName = "cmd.exe" , Arguments = "/C timeout /t 2 & copy /b/v/y \"" + dir + @"\update.exe" + "\" \"" + Application . ExecutablePath + "\" & \"" + Application . ExecutablePath + "\"" , CreateNoWindow = true , WindowStyle = ProcessWindowStyle . Hidden } ) ;
2019-09-09 16:53:11 +02:00
}
else
Application . Run ( new MainForm ( ) ) ;
2019-09-09 17:05:53 +02:00
#if ! DEBUG
2019-09-09 16:53:11 +02:00
}
catch ( Exception e1 )
{
MessageBox . Show ( e1 . ToString ( ) ) ;
}
finally
{
if ( hasHandle )
mutex . ReleaseMutex ( ) ;
}
2019-09-09 17:05:53 +02:00
#endif
2019-09-09 16:53:11 +02:00
}
2019-09-08 21:39:04 +02:00
}
}
}