1717
1818namespace BTDToolbox_Updater
1919{
20- public partial class Form1 : Form
20+ public partial class Main : Form
2121 {
22+ /// <summary>
23+ /// This project is used to check text files on github for updates, and if there is an update, it will download
24+ /// and install it.
25+ ///
26+ /// You will need to configure the "For easy reuse" variables
27+ ///
28+ /// For the program to work correctly, you need to set the "url" variable to the url of the RAW text file
29+ /// on github, which you can view by going to your text file on github, then pressing the "RAW" button.
30+ /// The program will then attempt to read that text file, and store its result in the "downloadedString" variable.
31+ /// Some configuration will likely be necessary, as it needs to remove everything in "downloadedString" until the
32+ /// only thing left is the URL of the most updated version of your project. This link needs to be the url of the
33+ /// actual file itself, not a main webpage or release page, but the actual download link for it. Some configuration
34+ /// may also be necessary with the console messages, as some of them are specifically for this project.
35+ ///
36+ ///
37+ /// In short, this project will read a text file off of github and process it down to a url, then download the file
38+ /// located at that url, extract it, delete all files (except for the exe) related to this updater, then launch the
39+ /// updated program.
40+ ///
41+ /// </summary>
2242
43+
44+
45+ //================================================================================
46+ //For easy reuse of project, change these variables
47+ string filename = "BTD Toolbox" ; //This will be used in all of the console messages
48+ string program_ProcessName = "BTDToolbox" ; //Used to check if the program is already running. Must set this
49+ string exeName = "BTDToolbox.exe" ; //Used to restart program after update. Do not put slashes in front of it, just name
50+ string this_exeName = "BTDToolbox_Updater" ; //The name of the exe that is this tool. Used to prevent deleting the Updater.exe
51+ string updateZip_Name = "BTDToolbox_Updater.zip" ; //The name of the zip file that is created from the downloaded update
52+ string url = "https://raw.githubusercontent.com/TDToolbox/BTDToolbox-2019_LiveFIles/master/Version" ; //URL of github config file
53+ string [ ] ignoreFiles = new string [ ] { "BTDToolbox_Updater" , "Backups" , "DotNetZip" , ".json" } ; //list of files to ignore during deletion, based on the full path name
54+ string [ ] deleteFiles = new string [ ] { "BTDToolbox_Updater.zip" , "Update" } ; //list of files to delete AFTER the update has finished. This is case specific
55+ //================================================================================
56+
57+
58+ //project varibales
2359 Thread bgThread ;
2460 WebClient client ;
2561
26- string toolboxRelease ;
27- public string lastMessage ;
62+ // string variables
63+ string releaseVersion ;
2864 string downloadedString = "" ;
29- string url = "https://raw.githubusercontent.com/TDToolbox/BTDToolbox-2019_LiveFIles/master/Version" ;
3065
66+ //extraction variables
3167 int totalFiles ;
3268 int filesTransfered ;
69+
70+ //other variables
71+ string lastMessage ;
3372 bool exit = false ;
73+ bool deleteProjects = false ;
74+
3475
35- public Form1 ( )
76+ public Main ( )
3677 {
3778 InitializeComponent ( ) ;
3879 client = new WebClient ( ) ;
@@ -42,10 +83,11 @@ public Form1()
4283 private void Form1_Shown ( object sender , EventArgs e )
4384 {
4485 printToConsole ( "Program Initialized..." ) ;
45- printToConsole ( "Welcome to BTD Toolbox 2019 auto-updater" ) ;
86+ printToConsole ( "Welcome to " + filename + " auto-updater") ;
4687 printToConsole ( "Getting download link for latest version..." ) ;
47-
48- CheckURL ( ) ;
88+ Thread bg = new Thread ( load ) ;
89+ bg . Start ( ) ;
90+
4991 }
5092 private void Form1_FormClosing ( object sender , FormClosingEventArgs e )
5193 {
@@ -55,6 +97,11 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
5597
5698
5799 //Core functions
100+ private void load ( ) //this pauses the updater for 1.5 seconds so user can see console
101+ {
102+ Thread . Sleep ( 1500 ) ;
103+ CheckURL ( ) ;
104+ }
58105 private void printToConsole ( string message )
59106 {
60107 if ( message != lastMessage )
@@ -72,62 +119,71 @@ private void printToConsole(string message)
72119 }
73120 private bool isToolboxRunning ( )
74121 {
75- if ( Process . GetProcessesByName ( "BTDToolbox" ) . Length > 0 )
122+ if ( Process . GetProcessesByName ( program_ProcessName ) . Length > 0 )
76123 return true ;
77124 else
78125 return false ;
79126 }
80- private void DeleteDirectory ( string path , bool deleteProjs )
127+ private void DeleteDirectory ( string path )
81128 {
82129 if ( Directory . Exists ( path ) )
83130 {
84131 printToConsole ( "Deleting directory.." ) ;
85132 var directory = new DirectoryInfo ( path ) { Attributes = FileAttributes . Normal } ;
86133
134+ foreach ( var info in directory . GetFileSystemInfos ( "*" , SearchOption . AllDirectories ) )
135+ {
136+ info . Attributes = FileAttributes . Normal ;
137+ }
138+
87139 foreach ( FileInfo file in directory . GetFiles ( ) )
88140 {
89- if ( ( deleteProjs == true ) )
141+ bool skip = false ;
142+ foreach ( string ignore in ignoreFiles )
90143 {
91- if ( ! file . ToString ( ) . Contains ( "BTDToolbox_Updater" ) && ( ! file . ToString ( ) . Contains ( "Backups" ) ) && ( ! file . ToString ( ) . Contains ( ".dll" ) ) && ( ! file . ToString ( ) . Contains ( ".json" ) ) )
92- file . Delete ( ) ;
144+ if ( file . ToString ( ) . Contains ( ignore ) )
145+ {
146+ skip = true ;
147+ }
93148 }
94- else
149+ if ( skip == false )
95150 {
96- if ( ! file . ToString ( ) . Contains ( "BTDToolbox_Updater" ) && ( ! file . ToString ( ) . Contains ( "Backups" ) ) && ( ! file . ToString ( ) . Contains ( "proj" ) ) && ( ! file . ToString ( ) . Contains ( ".dll" ) ) && ( ! file . ToString ( ) . Contains ( ".json" ) ) )
151+ if ( file . Exists )
97152 file . Delete ( ) ;
98153 }
99-
100-
101154 }
102155 foreach ( DirectoryInfo dir in directory . GetDirectories ( ) )
103156 {
104-
105- if ( ( deleteProjs == true ) )
157+ bool skip = false ;
158+ foreach ( string ignore in ignoreFiles )
106159 {
107- if ( ( dir . ToString ( ) != Environment . CurrentDirectory ) && ( ! dir . ToString ( ) . Contains ( "Backups" ) ) )
108- dir . Delete ( true ) ;
160+ if ( dir . FullName . ToString ( ) . Contains ( ignore ) )
161+ {
162+ skip = true ;
163+ }
109164 }
110- else
165+ if ( skip == false )
111166 {
112- if ( ( dir . ToString ( ) != Environment . CurrentDirectory ) && ( ! dir . ToString ( ) . Contains ( "proj" ) && ( ! dir . ToString ( ) . Contains ( "Backups" ) ) ) )
167+ if ( dir . Exists )
113168 dir . Delete ( true ) ;
114169 }
115170 }
116171 printToConsole ( "Directory deleted." ) ;
117172 }
118173 else
119174 {
120- printToConsole ( "Directory not found. Unable to delete directory at:\r \n " + path ) ;
175+ printToConsole ( "Directory not found. Unable to delete directory at: " + path ) ;
121176 }
122177 }
123178 private void CheckForExit ( )
124179 {
125180 if ( exit )
126181 {
127- if ( File . Exists ( Environment . CurrentDirectory + "\\ BTDToolbox_Updater.zip" ) )
128- File . Delete ( Environment . CurrentDirectory + "\\ BTDToolbox_Updater.zip" ) ;
129- if ( File . Exists ( Environment . CurrentDirectory + "\\ Update" ) )
130- File . Delete ( Environment . CurrentDirectory + "\\ Update" ) ;
182+ foreach ( string delete in deleteFiles )
183+ {
184+ if ( File . Exists ( Environment . CurrentDirectory + "\\ " + delete ) )
185+ File . Delete ( Environment . CurrentDirectory + "\\ " + delete ) ;
186+ }
131187 Environment . Exit ( 0 ) ;
132188 }
133189 }
@@ -142,11 +198,9 @@ public void CheckURL()
142198 private void GetURL ( string url )
143199 {
144200 bool success = false ;
145-
146201 try
147202 {
148203 downloadedString = client . DownloadString ( url ) ;
149- //MessageBox.Show(downloadedString);
150204 success = true ;
151205 }
152206 catch ( Exception )
@@ -185,7 +239,7 @@ private void GetURL(string url)
185239 }
186240 else
187241 {
188- printToConsole ( "ERROR! The program was unable to determine the latest version of BTD Toolbox . You can continue to use toolbox like normal, or try reopening the program to check again..." ) ;
242+ printToConsole ( "ERROR! The program was unable to determine the latest version of " + filename + " . You can continue to use it like normal, or try reopening the program to check again...") ;
189243 for ( int i = 0 ; i <= 120 ; i ++ )
190244 {
191245 if ( ! exit )
@@ -205,25 +259,25 @@ private void GetURL(string url)
205259 private void start ( )
206260 {
207261 string [ ] split = downloadedString . Split ( '\n ' ) ;
208- toolboxRelease = split [ 0 ] . Replace ( "toolbox2019: " , "" ) ;
262+ releaseVersion = split [ 0 ] . Replace ( "toolbox2019: " , "" ) ;
209263 printToConsole ( "Download link aquired!" ) ;
210264
211265 CheckForExit ( ) ;
212- printToConsole ( "Checking if Toolbox is running.." ) ;
266+ printToConsole ( "Checking if " + filename + " is running..") ;
213267 if ( isToolboxRunning ( ) == false )
214268 {
215- printToConsole ( "Toolbox is not running...") ;
269+ printToConsole ( filename + " is not running...") ;
216270 BeginUpdate ( ) ;
217271 }
218272 else
219273 {
220274 try
221275 {
222- foreach ( Process proc in Process . GetProcessesByName ( "BTDToolbox" ) )
276+ foreach ( Process proc in Process . GetProcessesByName ( program_ProcessName ) )
223277 {
224- printToConsole ( "Toolbox is running, terminating toolbox ...") ;
278+ printToConsole ( filename + " is running, terminating " + filename + " ...") ;
225279 proc . Kill ( ) ;
226- printToConsole ( "Toolbox terminated...") ;
280+ printToConsole ( filename + " terminated...") ;
227281 }
228282 BeginUpdate ( ) ;
229283 }
@@ -236,28 +290,27 @@ private void start()
236290 private void BeginUpdate ( )
237291 {
238292 printToConsole ( "Beginning update..." ) ;
239- printToConsole ( "The update will delete the old toolbox files...\n >> Do you want to delete all of the projects as well?" ) ;
240- DialogResult result = MessageBox . Show ( "The update will delete the old toolbox files...\n \n Do you want to delete all of the projects as well?" , "Delete project files?" , MessageBoxButtons . YesNo ) ;
241- if ( result == DialogResult . Yes )
242- {
243- if ( ! exit )
244- DeleteDirectory ( Environment . CurrentDirectory , true ) ;
245- }
246- else
293+ printToConsole ( "This tool will replace all of the old " + filename + " files...\n \n >> Do you want it to delete all of the projects as well ?" ) ;
294+ DialogResult result = MessageBox . Show ( "This tool will replace all of the old " + filename + " files... Do you want it to delete all of the projects as well?" , "Delete project files?" , MessageBoxButtons . YesNo ) ;
295+ if ( result == DialogResult . No )
247296 {
248- if ( ! exit )
249- DeleteDirectory ( Environment . CurrentDirectory , false ) ;
297+ Array . Resize ( ref ignoreFiles , ignoreFiles . Length + 1 ) ;
298+ ignoreFiles [ ignoreFiles . Length - 1 ] = "proj" ;
250299 }
251- CheckForExit ( ) ;
300+ DeleteDirectory ( Environment . CurrentDirectory ) ;
252301 DownloadUpdate ( ) ;
253302 }
254303 private void DownloadUpdate ( )
255304 {
256305 printToConsole ( "Downloading update..." ) ;
257306
258- client . DownloadFile ( toolboxRelease , "Update" ) ; //, Environment.CurrentDirectory);//
307+ client . DownloadFile ( releaseVersion , "Update" ) ; //, Environment.CurrentDirectory);//
259308 printToConsole ( "Update successfully downloaded!" ) ;
260- File . Move ( "Update" , "BTDToolbox.zip" ) ;
309+ if ( File . Exists ( updateZip_Name ) )
310+ {
311+ File . Delete ( updateZip_Name ) ;
312+ }
313+ File . Move ( "Update" , updateZip_Name ) ;
261314 CheckForExit ( ) ;
262315 extract ( ) ;
263316 }
@@ -267,7 +320,7 @@ private void DownloadUpdate()
267320 private void extract ( )
268321 {
269322 printToConsole ( "Extracting update files...." ) ;
270- string zipPath = Environment . CurrentDirectory + "\\ BTDToolbox.zip" ;
323+ string zipPath = Environment . CurrentDirectory + "\\ " + updateZip_Name ;
271324 string extractedFilePath = Environment . CurrentDirectory ;
272325 ZipFile archive = new ZipFile ( zipPath ) ;
273326
@@ -282,9 +335,9 @@ private void extract()
282335 archive . Dispose ( ) ;
283336 printToConsole ( "Update files successfully extracted!!!\n >> Deleting installation files..." ) ;
284337 File . Delete ( zipPath ) ;
285- printToConsole ( "Installation files deleted. Restarting Toolbox ..." ) ;
338+ printToConsole ( "Installation files deleted. Restarting" + filename + " ...") ;
286339
287- Process . Start ( Environment . CurrentDirectory + "\\ BTDToolbox.exe" ) ;
340+ Process . Start ( Environment . CurrentDirectory + "\\ " + exeName ) ;
288341 exit = true ;
289342 CheckForExit ( ) ;
290343 }
0 commit comments