Separate ResourceFetcher into ResourceFetcher and ResourceChecker.

This commit is contained in:
UnknownShadow200 2016-05-15 16:29:07 +10:00
parent f05fba36de
commit a9172905cd
4 changed files with 137 additions and 105 deletions

View file

@ -400,6 +400,7 @@ namespace ClassicalSharp.Network {
env.SetWeatherSpeed( value / 256f ); break;
case 7:
Utils.Clamp( ref value, byte.MinValue, byte.MaxValue );
Console.WriteLine( value );
env.SetWeatherFade( value / 128f ); break;
}
}

View file

@ -91,6 +91,7 @@
<Compile Include="LauncherWindow.cs" />
<Compile Include="Patcher\Animations.cs" />
<Compile Include="Patcher\BinUnpacker.cs" />
<Compile Include="Patcher\ResourceChecker.cs" />
<Compile Include="Patcher\ResourceFetcher.cs" />
<Compile Include="Patcher\ResourcePatcher.cs" />
<Compile Include="Patcher\SoundPatcher.cs" />

View file

@ -0,0 +1,95 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.IO;
using ClassicalSharp.TexturePack;
namespace Launcher {
public sealed class ResourceChecker {
public void CheckResourceExistence() {
string audioPath = Path.Combine( Program.AppDirectory, "audio" );
if( !Directory.Exists( audioPath ) )
Directory.CreateDirectory( audioPath );
AllResourcesExist = CheckSoundsExist();
string texDir = Path.Combine( Program.AppDirectory, "texpacks" );
string zipPath = Path.Combine( texDir, "default.zip" );
defaultZipExists = File.Exists( zipPath );
if( defaultZipExists )
CheckClassicGuiPng( zipPath );
if( !defaultZipExists ) {
// classic.jar + 1.6.2.jar + terrain-patch.png + gui.png
DownloadSize += (291 + 4621 + 7 + 21) / 1024f;
ResourcesCount += 4;
AllResourcesExist = false;
}
for( int i = 0; i < musicFiles.Length; i++ ) {
string file = Path.Combine( audioPath, musicFiles[i] + ".ogg" );
musicExists[i] = File.Exists( file );
if( !musicExists[i] ) {
DownloadSize += musicSizes[i] / 1024f;
ResourcesCount++;
AllResourcesExist = false;
}
}
ResourcesCount += digSounds.Length;
DownloadSize += 173 / 1024f;
ResourcesCount += stepSounds.Length;
DownloadSize += 244 / 1024f;
}
public bool AllResourcesExist;
public float DownloadSize;
public int ResourcesCount;
internal bool[] musicExists = new bool[7];
internal bool classicGuiPngExists, defaultZipExists;
void CheckClassicGuiPng( string path ) {
ZipReader reader = new ZipReader();
reader.ShouldProcessZipEntry = ShouldProcessZipEntry;
reader.ProcessZipEntry = ProcessZipEntry;
using( Stream src = new FileStream( path, FileMode.Open, FileAccess.Read ) )
reader.Extract( src );
if( !classicGuiPngExists )
defaultZipExists = false;
}
bool ShouldProcessZipEntry( string filename ) {
if( filename == "gui_classic.png" )
classicGuiPngExists = true;
return false;
}
void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) { }
bool CheckSoundsExist() {
string path = Path.Combine( Program.AppDirectory, "audio" );
for( int i = 0; i < digSounds.Length; i++ ) {
string file = "dig_" + digSounds[i].Substring( 1 ) + ".wav";
if( !File.Exists( Path.Combine( path, file ) ) ) return false;
}
for( int i = 0; i < stepSounds.Length; i++ ) {
string file = "step_" + stepSounds[i].Substring( 1 ) + ".wav";
if( !File.Exists( Path.Combine( path, file ) ) ) return false;
}
return true;
}
internal static string[] digSounds = new [] { "Acloth1", "Acloth2", "Acloth3", "Acloth4", "Bglass1",
"Bglass2", "Bglass3", "Agrass1", "Agrass2", "Agrass3", "Agrass4", "Agravel1", "Agravel2",
"Agravel3", "Agravel4", "Asand1", "Asand2", "Asand3", "Asand4", "Asnow1", "Asnow2", "Asnow3",
"Asnow4", "Astone1", "Astone2", "Astone3", "Astone4", "Awood1", "Awood2", "Awood3", "Awood4" };
internal static string[] stepSounds = new [] { "Acloth1", "Acloth2", "Acloth3", "Acloth4", "Bgrass1",
"Bgrass2", "Bgrass3", "Bgrass4", "Agravel1", "Agravel2", "Agravel3", "Agravel4", "Asand1",
"Asand2", "Asand3", "Asand4", "Asnow1", "Asnow2", "Asnow3", "Asnow4", "Astone1", "Astone2",
"Astone3", "Astone4", "Awood1", "Awood2", "Awood3", "Awood4" };
internal static string[] musicFiles = new [] { "calm1", "calm2", "calm3", "hal1", "hal2", "hal3", "hal4" };
static int[] musicSizes = new [] { 2472, 1931, 2181, 1926, 1714, 1879, 2499 };
}
}

View file

@ -26,9 +26,9 @@ namespace Launcher {
public void DownloadItems( AsyncDownloader downloader, Action<string> setStatus ) {
this.downloader = downloader;
DownloadMusicFiles();
digPatcher = new SoundPatcher( digSounds, "dig_", "step_cloth1" );
digPatcher = new SoundPatcher( ResourceChecker.digSounds, "dig_", "step_cloth1" );
digPatcher.FetchFiles( digSoundsUri, altDigSoundsUri, this );
stepPatcher = new SoundPatcher( stepSounds, "step_", "classic jar" );
stepPatcher = new SoundPatcher( ResourceChecker.stepSounds, "step_", "classic jar" );
stepPatcher.FetchFiles( stepSoundsUri, altStepSoundsUri, this );
if( !defaultZipExists ) {
downloader.DownloadData( jarClassicUri, false, "classic_jar" );
@ -39,15 +39,26 @@ namespace Launcher {
SetFirstStatus( setStatus );
}
void DownloadMusicFiles() {
string[] files = ResourceChecker.musicFiles;
for( int i = 0; i < files.Length; i++ ) {
if( musicExists[i] ) continue;
string baseUri = i < 3 ? musicUri : newMusicUri;
string url = baseUri + files[i] + ".ogg";
downloader.DownloadData( url, false, files[i] );
}
}
void SetFirstStatus( Action<string> setStatus ) {
for( int i = 0; i < musicExists.Length; i++ ) {
if( musicExists[i] ) continue;
setStatus( MakeNext( musicFiles[i] ) );
setStatus( MakeNext( ResourceChecker.musicFiles[i] ) );
return;
}
setStatus( MakeNext( "dig_cloth1" ) );
}
internal byte[] jarClassic, jar162, pngTerrainPatch, pngGuiPatch;
public bool Check( Action<string> setStatus ) {
if( Done ) return true;
@ -59,17 +70,17 @@ namespace Launcher {
if( !stepPatcher.CheckDownloaded( this, setStatus ) )
return false;
if( !DownloadItem( "classic_jar", "classic jar",
"1.6.2 jar", ref jarClassic, setStatus ) )
if( !Download( "classic_jar", "classic jar",
"1.6.2 jar", ref jarClassic, setStatus ) )
return false;
if( !DownloadItem( "162_jar", "1.6.2 jar",
"terrain patch", ref jar162, setStatus ) )
if( !Download( "162_jar", "1.6.2 jar",
"terrain patch", ref jar162, setStatus ) )
return false;
if( !DownloadItem( "terrain_patch", "terrain.png patch",
"gui", ref pngTerrainPatch, setStatus ) )
if( !Download( "terrain_patch", "terrain.png patch",
"gui", ref pngTerrainPatch, setStatus ) )
return false;
if( !DownloadItem( "gui_patch", "gui.png patch",
null, ref pngGuiPatch, setStatus ) )
if( !Download( "gui_patch", "gui.png patch",
null, ref pngGuiPatch, setStatus ) )
return false;
bool done = !defaultZipExists ? pngGuiPatch != null :
@ -81,8 +92,8 @@ namespace Launcher {
return true;
}
bool DownloadItem( string identifier, string name, string next,
ref byte[] data, Action<string> setStatus ) {
bool Download( string identifier, string name, string next,
ref byte[] data, Action<string> setStatus ) {
DownloadedItem item;
if( downloader.TryGetItem( identifier, out item ) ) {
Console.WriteLine( "got resource " + identifier );
@ -101,42 +112,6 @@ namespace Launcher {
return true;
}
public void CheckResourceExistence() {
string audioPath = Path.Combine( Program.AppDirectory, "audio" );
if( !Directory.Exists( audioPath ) )
Directory.CreateDirectory( audioPath );
AllResourcesExist = CheckSoundsExist();
string texDir = Path.Combine( Program.AppDirectory, "texpacks" );
string zipPath = Path.Combine( texDir, "default.zip" );
defaultZipExists = File.Exists( zipPath );
if( defaultZipExists )
CheckClassicGuiPng( zipPath );
if( !defaultZipExists ) {
// classic.jar + 1.6.2.jar + terrain-patch.png + gui.png
DownloadSize += (291 + 4621 + 7 + 21) / 1024f;
ResourcesCount += 4;
AllResourcesExist = false;
}
for( int i = 0; i < musicFiles.Length; i++ ) {
string file = Path.Combine( audioPath, musicFiles[i] + ".ogg" );
musicExists[i] = File.Exists( file );
if( !musicExists[i] ) {
DownloadSize += musicSizes[i] / 1024f;
ResourcesCount++;
AllResourcesExist = false;
}
}
ResourcesCount += digSounds.Length;
DownloadSize += 173 / 1024f;
ResourcesCount += stepSounds.Length;
DownloadSize += 244 / 1024f;
}
public bool AllResourcesExist;
public float DownloadSize;
public int ResourcesCount, CurrentResource;
const string lineFormat = "&eFetching {0}.. ({1}/{2})";
public string MakeNext( string next ) {
CurrentResource++;
@ -144,32 +119,13 @@ namespace Launcher {
CurrentResource, ResourcesCount );
}
bool classicGuiPngExists = false;
void CheckClassicGuiPng( string path ) {
ZipReader reader = new ZipReader();
reader.ShouldProcessZipEntry = ShouldProcessZipEntry;
reader.ProcessZipEntry = ProcessZipEntry;
using( Stream src = new FileStream( path, FileMode.Open, FileAccess.Read ) )
reader.Extract( src );
if( !classicGuiPngExists )
defaultZipExists = false;
}
bool ShouldProcessZipEntry( string filename ) {
if( filename == "gui_classic.png" )
classicGuiPngExists = true;
return false;
}
void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) { }
bool CheckMusicFiles( Action<string> setStatus ) {
for( int i = 0; i < musicFiles.Length; i++ ) {
string next = i < musicFiles.Length - 1 ? musicFiles[i + 1] : "dig_cloth1";
string name = musicFiles[i];
string[] files = ResourceChecker.musicFiles;
for( int i = 0; i < files.Length; i++ ) {
string next = i < files.Length - 1 ? files[i + 1] : "dig_cloth1";
string name = files[i];
byte[] data = null;
if( !DownloadItem( name, name, next, ref data, setStatus ) )
if( !Download( name, name, next, ref data, setStatus ) )
return false;
if( data == null ) continue;
@ -180,41 +136,20 @@ namespace Launcher {
return true;
}
void DownloadMusicFiles() {
for( int i = 0; i < musicFiles.Length; i++ ) {
if( musicExists[i] ) continue;
string baseUri = i < 3 ? musicUri : newMusicUri;
string url = baseUri + musicFiles[i] + ".ogg";
downloader.DownloadData( url, false, musicFiles[i] );
}
}
bool CheckSoundsExist() {
string path = Path.Combine( Program.AppDirectory, "audio" );
for( int i = 0; i < digSounds.Length; i++ ) {
string file = "dig_" + digSounds[i].Substring( 1 ) + ".wav";
if( !File.Exists( Path.Combine( path, file ) ) ) return false;
}
public void CheckResourceExistence() {
ResourceChecker checker = new ResourceChecker();
checker.CheckResourceExistence();
for( int i = 0; i < stepSounds.Length; i++ ) {
string file = "step_" + stepSounds[i].Substring( 1 ) + ".wav";
if( !File.Exists( Path.Combine( path, file ) ) ) return false;
}
return true;
AllResourcesExist = checker.AllResourcesExist;
DownloadSize = checker.DownloadSize;
ResourcesCount = checker.ResourcesCount;
musicExists = checker.musicExists;
defaultZipExists = checker.defaultZipExists;
}
string[] digSounds = new [] { "Acloth1", "Acloth2", "Acloth3", "Acloth4", "Bglass1",
"Bglass2", "Bglass3", "Agrass1", "Agrass2", "Agrass3", "Agrass4", "Agravel1", "Agravel2",
"Agravel3", "Agravel4", "Asand1", "Asand2", "Asand3", "Asand4", "Asnow1", "Asnow2", "Asnow3",
"Asnow4", "Astone1", "Astone2", "Astone3", "Astone4", "Awood1", "Awood2", "Awood3", "Awood4" };
string[] stepSounds = new [] { "Acloth1", "Acloth2", "Acloth3", "Acloth4", "Bgrass1",
"Bgrass2", "Bgrass3", "Bgrass4", "Agravel1", "Agravel2", "Agravel3", "Agravel4", "Asand1",
"Asand2", "Asand3", "Asand4", "Asnow1", "Asnow2", "Asnow3", "Asnow4", "Astone1", "Astone2",
"Astone3", "Astone4", "Awood1", "Awood2", "Awood3", "Awood4" };
string[] musicFiles = new [] { "calm1", "calm2", "calm3", "hal1", "hal2", "hal3", "hal4" };
int[] musicSizes = new [] { 2472, 1931, 2181, 1926, 1714, 1879, 2499 };
public bool AllResourcesExist;
public float DownloadSize;
public int ResourcesCount, CurrentResource;
bool[] musicExists = new bool[7];
internal bool defaultZipExists;
}