From 1bb4b9d99bf441197883a6c20244d6cb0e0903cb Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 30 Dec 2015 13:54:33 +1100 Subject: [PATCH] Move texture packs and map files into their own sub-directories. --- .../2D/Screens/Menu/LoadLevelScreen.cs | 6 ++-- .../2D/Screens/Menu/SaveLevelScreen.cs | 1 + .../2D/Screens/Menu/TexturePackScreen.cs | 4 +-- ClassicalSharp/Game/Game.cs | 12 +++---- ClassicalSharp/Program.cs | 33 ++++++++++++++++++- .../TexturePack/TexturePackExtractor.cs | 2 ++ ClassicalSharp/Utils/Options.cs | 7 +++- Launcher2/LauncherWindow.Background.cs | 5 +-- Launcher2/Patcher/ResourceFetcher.cs | 3 +- Launcher2/Patcher/ResourcePatcher.cs | 3 +- 10 files changed, 59 insertions(+), 17 deletions(-) diff --git a/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs b/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs index 052e99d9b..fc4d9515e 100644 --- a/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs @@ -7,9 +7,9 @@ namespace ClassicalSharp { public LoadLevelScreen( Game game ) : base( game ) { titleText = "Select a level"; - string dir = Program.AppDirectory; - string[] cwFiles = Directory.GetFiles( dir, "*.cw", SearchOption.AllDirectories ); - string[] datFiles = Directory.GetFiles( dir, "*.dat", SearchOption.AllDirectories ); + string dir = Path.Combine( Program.AppDirectory, "maps" ); + string[] cwFiles = Directory.GetFiles( dir, "*.cw" ); + string[] datFiles = Directory.GetFiles( dir, "*.dat" ); files = new string[cwFiles.Length + datFiles.Length]; Array.Copy( cwFiles, 0, files, 0, cwFiles.Length ); Array.Copy( datFiles, 0, files, cwFiles.Length, datFiles.Length ); diff --git a/ClassicalSharp/2D/Screens/Menu/SaveLevelScreen.cs b/ClassicalSharp/2D/Screens/Menu/SaveLevelScreen.cs index 7fc63d9f2..ec607ef0c 100644 --- a/ClassicalSharp/2D/Screens/Menu/SaveLevelScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/SaveLevelScreen.cs @@ -118,6 +118,7 @@ namespace ClassicalSharp { string textPath; void SaveMap( string path ) { + path = Path.Combine( "maps", path ); path = Path.Combine( Program.AppDirectory, path ); try { if( File.Exists( path ) ) diff --git a/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs b/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs index 23c14721f..c217d63de 100644 --- a/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs @@ -9,8 +9,8 @@ namespace ClassicalSharp { public TexturePackScreen( Game game ) : base( game ) { titleText = "Select a texture pack zip"; - string dir = Program.AppDirectory; - files = Directory.GetFiles( dir, "*.zip", SearchOption.AllDirectories ); + string dir = Path.Combine( Program.AppDirectory, TexturePackExtractor.Dir ); + files = Directory.GetFiles( dir, "*.zip" ); for( int i = 0; i < files.Length; i++ ) { string absolutePath = files[i]; diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 0bf7d48ff..71c3616cb 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -19,7 +19,7 @@ using OpenTK.Input; namespace ClassicalSharp { public partial class Game : GameWindow { - + void LoadAtlas( Bitmap bmp ) { TerrainAtlas1D.Dispose(); TerrainAtlas.Dispose(); @@ -70,7 +70,7 @@ namespace ClassicalSharp { Animations = new Animations( this ); defTexturePack = Options.Get( OptionsKey.DefaultTexturePack ) ?? "default.zip"; TexturePackExtractor extractor = new TexturePackExtractor(); - extractor.Extract( "default.zip", this ); + extractor.Extract( "default.zip", this ); // in case the user's default texture pack doesn't have all required textures if( defTexturePack != "default.zip" ) extractor.Extract( DefaultTexturePack, this ); @@ -137,7 +137,7 @@ namespace ClassicalSharp { if( File.Exists( launcherPath ) ) { Icon = Icon.ExtractAssociatedIcon( launcherPath ); return; - } + } launcherPath = Path.Combine( Program.AppDirectory, "Launcher.exe" ); if( File.Exists( launcherPath ) ) { Icon = Icon.ExtractAssociatedIcon( launcherPath ); @@ -225,7 +225,7 @@ namespace ClassicalSharp { const double ticksPeriod = 1.0 / ticksFrequency; const double imageCheckPeriod = 30.0; const double cameraPeriod = 1.0 / 120; - double ticksAccumulator, imageCheckAccumulator, + double ticksAccumulator, imageCheckAccumulator, cameraAccumulator; void CheckScheduledTasks( double time ) { @@ -384,7 +384,7 @@ namespace ClassicalSharp { } void PerformFpsElapsed( double elapsedMs ) { - limitAcc += elapsedMs; + limitAcc += elapsedMs; if( limitAcc >= limitMilliseconds ) { // going slower than limit? limitAcc -= limitMilliseconds; } else { // going faster than limit @@ -441,7 +441,7 @@ namespace ClassicalSharp { if( block == 0 ) return false; if( !BlockInfo.IsLiquid[block] ) return true; - return !LiquidsBreakable ? false : + return !LiquidsBreakable ? false : Inventory.CanPlace[block] && Inventory.CanDelete[block]; } diff --git a/ClassicalSharp/Program.cs b/ClassicalSharp/Program.cs index e0917ed26..9c1b7bf7b 100644 --- a/ClassicalSharp/Program.cs +++ b/ClassicalSharp/Program.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Net; +using ClassicalSharp.TexturePack; using OpenTK; namespace ClassicalSharp { @@ -16,9 +17,11 @@ namespace ClassicalSharp { AppDirectory = AppDomain.CurrentDomain.BaseDirectory; string logPath = Path.Combine( AppDirectory, "client.log" ); ErrorHandler.InstallHandler( logPath ); + CleanupMainDirectory(); Utils.LogDebug( "Starting " + AppName + ".." ); - if( !File.Exists( Path.Combine( AppDirectory, "default.zip" ) ) ) { + string path = Path.Combine( Program.AppDirectory, TexturePackExtractor.Dir ); + if( !File.Exists( Path.Combine( path, "default.zip" ) ) ) { Utils.LogDebug( "default.zip not found. Cannot start." ); return; } @@ -76,5 +79,33 @@ namespace ClassicalSharp { game.Run(); } } + + internal static void CleanupMainDirectory() { + string mapPath = Path.Combine( Program.AppDirectory, "maps" ); + if( !Directory.Exists( mapPath ) ) + Directory.CreateDirectory( mapPath ); + string texPath = Path.Combine( Program.AppDirectory, TexturePackExtractor.Dir ); + if( !Directory.Exists( texPath ) ) + Directory.CreateDirectory( texPath ); + CopyFiles( "*.cw", mapPath ); + CopyFiles( "*.dat", mapPath ); + CopyFiles( "*.zip", texPath ); + } + + static void CopyFiles( string filter, string folder ) { + string[] files = Directory.GetFiles( AppDirectory, filter ); + for( int i = 0; i < files.Length; i++ ) { + string name = Path.GetFileName( files[i] ); + string dst = Path.Combine( folder, name ); + if( File.Exists( dst ) ) + continue; + + try { + File.Copy( files[i], dst ); + File.Delete( files[i] ); + } catch( IOException ex ) { + } + } + } } } \ No newline at end of file diff --git a/ClassicalSharp/TexturePack/TexturePackExtractor.cs b/ClassicalSharp/TexturePack/TexturePackExtractor.cs index 37e1271f6..30d48da3a 100644 --- a/ClassicalSharp/TexturePack/TexturePackExtractor.cs +++ b/ClassicalSharp/TexturePack/TexturePackExtractor.cs @@ -9,8 +9,10 @@ namespace ClassicalSharp.TexturePack { /// Extracts resources from a .zip texture pack. public sealed class TexturePackExtractor { + public const string Dir = "texpacks"; Game game; public void Extract( string path, Game game ) { + path = Path.Combine( Dir, path ); path = Path.Combine( Program.AppDirectory, path ); using( Stream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) ) Extract( fs, game ); diff --git a/ClassicalSharp/Utils/Options.cs b/ClassicalSharp/Utils/Options.cs index 99ee2230c..33338a590 100644 --- a/ClassicalSharp/Utils/Options.cs +++ b/ClassicalSharp/Utils/Options.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using ClassicalSharp.TexturePack; using OpenTK; using OpenTK.Input; @@ -118,9 +119,13 @@ namespace ClassicalSharp { } public static bool Load() { - // i.e. when running from the launcher + // Both of these are from when running from the launcher if( Program.AppDirectory == null ) Program.AppDirectory = AppDomain.CurrentDomain.BaseDirectory; + string defZip = Path.Combine( Program.AppDirectory, "default.zip" ); + string texDir = Path.Combine( Program.AppDirectory, TexturePackExtractor.Dir ); + if( File.Exists( defZip ) || !Directory.Exists( texDir ) ) + Program.CleanupMainDirectory(); try { string path = Path.Combine( Program.AppDirectory, OptionsFile ); diff --git a/Launcher2/LauncherWindow.Background.cs b/Launcher2/LauncherWindow.Background.cs index 20b6f2719..4d7e60389 100644 --- a/Launcher2/LauncherWindow.Background.cs +++ b/Launcher2/LauncherWindow.Background.cs @@ -11,11 +11,12 @@ namespace Launcher2 { internal void TryLoadTexturePack() { Options.Load(); LauncherSkin.LoadFromOptions(); + string texDir = Path.Combine( Program.AppDirectory, "texpacks" ); string texPack = Options.Get( OptionsKey.DefaultTexturePack ) ?? "default.zip"; - texPack = Path.Combine( Program.AppDirectory, texPack ); + texPack = Path.Combine( texDir, texPack ); if( !File.Exists( texPack ) ) - texPack = Path.Combine( Program.AppDirectory, "default.zip" ); + texPack = Path.Combine( texDir, "default.zip" ); if( !File.Exists( texPack ) ) return; diff --git a/Launcher2/Patcher/ResourceFetcher.cs b/Launcher2/Patcher/ResourceFetcher.cs index d88efb4e0..88f747be6 100644 --- a/Launcher2/Patcher/ResourceFetcher.cs +++ b/Launcher2/Patcher/ResourceFetcher.cs @@ -112,7 +112,8 @@ namespace Launcher2 { AllResourcesExist = File.Exists( digPath + ".bin" ) && File.Exists( stepPath + ".bin" ); - string zipPath = Path.Combine( Program.AppDirectory, "default.zip" ); + string texDir = Path.Combine( Program.AppDirectory, "texpacks" ); + string zipPath = Path.Combine( texDir, "default.zip" ); defaultZipExists = File.Exists( zipPath ); if( !defaultZipExists ) { // classic.jar + 1.6.2.jar + terrain-patch.png + gui.png diff --git a/Launcher2/Patcher/ResourcePatcher.cs b/Launcher2/Patcher/ResourcePatcher.cs index 1a9967a83..2656414df 100644 --- a/Launcher2/Patcher/ResourcePatcher.cs +++ b/Launcher2/Patcher/ResourcePatcher.cs @@ -21,7 +21,8 @@ namespace Launcher2 { reader = new ZipReader(); reader.ShouldProcessZipEntry = ShouldProcessZipEntry_Classic; reader.ProcessZipEntry = ProcessZipEntry_Classic; - string path = Path.Combine( Program.AppDirectory, "default.zip" ); + string texDir = Path.Combine( Program.AppDirectory, "texpacks" ); + string path = Path.Combine( texDir, "default.zip" ); using( Stream srcClassic = new MemoryStream( jarClassic ), srcModern = new MemoryStream( jar162 ),