From f920e05495a000b08f95e18320ac4efbaf230fbf Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 26 Dec 2014 09:34:40 +1100 Subject: [PATCH] Add option to fetch resources. (thanks Jonty800) --- Launcher/Launcher.csproj | 233 ++++++++++++++++++------------------ Launcher/MainForm.cs | 14 +++ Launcher/ResourceFetcher.cs | 47 ++++++++ 3 files changed, 178 insertions(+), 116 deletions(-) create mode 100644 Launcher/ResourceFetcher.cs diff --git a/Launcher/Launcher.csproj b/Launcher/Launcher.csproj index 9551ef6cc..48e6ce6bb 100644 --- a/Launcher/Launcher.csproj +++ b/Launcher/Launcher.csproj @@ -1,117 +1,118 @@ - - - - {23B9BDA8-4330-46AB-9012-08D87430391A} - Debug - AnyCPU - WinExe - Launcher - Launcher - v2.0 - Properties - - - - - - - 3.5 - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - False - False - False - False - 4 - false - - - AnyCPU - False - Auto - 4194304 - 4096 - - - ..\output\debug\ - true - Full - False - True - DEBUG;TRACE - - - ..\output\release\ - false - None - True - False - TRACE - - - - - - - - - - - - - Form - - - MainForm.cs - - - - - - - - - - - - - MainForm.cs - - - - - False - Microsoft .NET Framework 4 %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 3.1 - true - - - + + + + {23B9BDA8-4330-46AB-9012-08D87430391A} + Debug + AnyCPU + WinExe + Launcher + Launcher + v2.0 + Properties + + + + + + + 3.5 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + False + False + False + False + 4 + false + + + AnyCPU + False + Auto + 4194304 + 4096 + + + ..\output\debug\ + true + Full + False + True + DEBUG;TRACE + + + ..\output\release\ + false + None + True + False + TRACE + + + + + + + + + + + + + Form + + + MainForm.cs + + + + + + + + + + + + + + MainForm.cs + + + + + False + Microsoft .NET Framework 4 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + \ No newline at end of file diff --git a/Launcher/MainForm.cs b/Launcher/MainForm.cs index 0f7fa74c5..9f9b62cad 100644 --- a/Launcher/MainForm.cs +++ b/Launcher/MainForm.cs @@ -24,6 +24,20 @@ namespace Launcher { tabMC.TabPages.Remove( tabMCServer ); tabCC.TabPages.Remove( tabCCServers ); tabCC.TabPages.Remove( tabCCServer ); + Shown += DisplayResourcesDialog; + } + + void DisplayResourcesDialog( object sender, EventArgs e ) { + // TODO: async fetching + ResourceFetcher fetcher = new ResourceFetcher(); + if( !fetcher.CheckAllResourcesExist() ) { + DialogResult result = MessageBox.Show( + "Some required resources weren't found. Would you like to download them now?", "Missing resources", + MessageBoxButtons.OKCancel ); + if( result == DialogResult.OK ) { + fetcher.Run(); + } + } } delegate void Action( T1 arg1, T2 arg2 ); diff --git a/Launcher/ResourceFetcher.cs b/Launcher/ResourceFetcher.cs new file mode 100644 index 000000000..491085566 --- /dev/null +++ b/Launcher/ResourceFetcher.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; +using System.Net; +using System.Windows.Forms; + +namespace Launcher { + + public class ResourceFetcher { + + const string terrainUri = "https://raw.githubusercontent.com/andrewphorn/ClassiCube-Client/master/src/main/resources/terrain.png"; + const string cloudsUri = "https://raw.githubusercontent.com/andrewphorn/ClassiCube-Client/master/src/main/resources/clouds.png"; + const string charUri = "https://raw.githubusercontent.com/andrewphorn/ClassiCube-Client/master/src/main/resources/char.png"; + + public void Run() { + using( WebClient client = new WebClient() ) { + client.Proxy = null; + if( !DownloadData( terrainUri, client, "terrain.png" ) ) return; + if( !DownloadData( cloudsUri, client, "clouds.png" ) ) return; + if( !DownloadData( charUri, client, "char.png" ) ) return; + } + } + + static bool DownloadData( string uri, WebClient client, string output ) { + if( File.Exists( output ) ) return true; + + byte[] data = null; + try { + data = client.DownloadData( uri ); + } catch( WebException ) { + MessageBox.Show( "Unable to download " + output, "Failed to download resource", MessageBoxButtons.OK, MessageBoxIcon.Error ); + return false; + } + + try { + File.WriteAllBytes( output, data ); + } catch( IOException ) { + MessageBox.Show( "Unable to save " + output, "Failed to save resource", MessageBoxButtons.OK, MessageBoxIcon.Error ); + return false; + } + return true; + } + + public bool CheckAllResourcesExist() { + return File.Exists( "terrain.png" ) && File.Exists( "clouds.png" ) && File.Exists( "char.png" ); + } + } +}