Add option to fetch resources. (thanks Jonty800)

This commit is contained in:
UnknownShadow200 2014-12-26 09:34:40 +11:00
parent 8ebaefc62b
commit f920e05495
3 changed files with 178 additions and 116 deletions

View File

@ -80,6 +80,7 @@
<Compile Include="MinecraftSession.cs" /> <Compile Include="MinecraftSession.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceFetcher.cs" />
<Compile Include="ServerListEntry.cs" /> <Compile Include="ServerListEntry.cs" />
<Compile Include="WebUtility.cs" /> <Compile Include="WebUtility.cs" />
</ItemGroup> </ItemGroup>

View File

@ -24,6 +24,20 @@ namespace Launcher {
tabMC.TabPages.Remove( tabMCServer ); tabMC.TabPages.Remove( tabMCServer );
tabCC.TabPages.Remove( tabCCServers ); tabCC.TabPages.Remove( tabCCServers );
tabCC.TabPages.Remove( tabCCServer ); 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, T2>( T1 arg1, T2 arg2 ); delegate void Action<T1, T2>( T1 arg1, T2 arg2 );

View File

@ -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" );
}
}
}