From 1b5d98f413589f871d315a3f836c08ed7be899e7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 9 Nov 2015 12:57:56 +1100 Subject: [PATCH] Add basis for a warning screen, modularise updater and make it work when Launcher2 is called Launcher.exe. --- ClassicalSharp/2D/Screens/WarningScreen.cs | 54 +++++++++++++++++++ ClassicalSharp/ClassicalSharp.csproj | 1 + ClassicalSharp/Game/Game.cs | 7 ++- Launcher2/Gui/Screens/UpdatesScreen.cs | 62 +--------------------- Launcher2/Launcher2.csproj | 3 ++ Launcher2/Updater/Patcher.cs | 44 +++++++++++++++ Launcher2/Updater/Scripts.cs | 58 ++++++++++++++++++++ 7 files changed, 165 insertions(+), 64 deletions(-) create mode 100644 ClassicalSharp/2D/Screens/WarningScreen.cs create mode 100644 Launcher2/Updater/Patcher.cs create mode 100644 Launcher2/Updater/Scripts.cs diff --git a/ClassicalSharp/2D/Screens/WarningScreen.cs b/ClassicalSharp/2D/Screens/WarningScreen.cs new file mode 100644 index 000000000..44c21bc72 --- /dev/null +++ b/ClassicalSharp/2D/Screens/WarningScreen.cs @@ -0,0 +1,54 @@ +using System; +using System.Drawing; + +namespace ClassicalSharp { + + // TODO: get and set activescreen. + public sealed class WarningScreen : MenuScreen { + + public WarningScreen( Game game ) : base( game ) { + } + + public override void Init() { + titleFont = new Font( "Arial", 16, FontStyle.Bold ); + regularFont = new Font( "Arial", 14, FontStyle.Regular ); + + buttons = new ButtonWidget[] { + ButtonWidget.Create( game, -60, 30, 60, 20, "Yes", Anchor.Centre, + Anchor.Centre, titleFont, OnYesClick ), + ButtonWidget.Create( game, 60, 30, 60, 20, "No", Anchor.Centre, + Anchor.Centre, titleFont, OnNoClick ), + }; + labels = new TextWidget[] { + TextWidget.Create( game, 0, -120, "Do you want to XYZ?", + Anchor.Centre, Anchor.Centre, titleFont ), + TextWidget.Create( game, 0, -70, "Warning text here", + Anchor.Centre, Anchor.Centre, regularFont ), + }; + } + TextWidget[] labels; + + void OnYesClick( Game g, Widget w ) { + game.SetNewScreen( null ); + } + + void OnNoClick( Game g, Widget w ) { + game.SetNewScreen( null ); + } + + public override void Render( double delta ) { + RenderMenuBounds(); + graphicsApi.Texturing = true; + RenderMenuButtons( delta ); + for( int i = 0; i < labels.Length; i++ ) + labels[i].Render( delta ); + graphicsApi.Texturing = false; + } + + public override void OnResize( int oldWidth, int oldHeight, int width, int height ) { + base.OnResize( oldWidth, oldHeight, width, height ); + for( int i = 0; i < labels.Length; i++ ) + labels[i].OnResize( oldWidth, oldHeight, width, height ); + } + } +} \ No newline at end of file diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 352f4c3e7..7b0a09164 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -101,6 +101,7 @@ + diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index bfb7eb7d0..af3eea120 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -314,9 +314,8 @@ namespace ClassicalSharp { base.OnResize( sender, e ); Graphics.OnWindowResize( this ); UpdateProjection(); - if( activeScreen != null ) { + if( activeScreen != null ) activeScreen.OnResize( width, height, Width, Height ); - } hudScreen.OnResize( width, height, Width, Height ); width = Width; height = Height; @@ -387,10 +386,10 @@ namespace ClassicalSharp { ParticleManager.Dispose(); Players.Dispose(); AsyncDownloader.Dispose(); + Chat.Dispose(); - if( activeScreen != null ) { + if( activeScreen != null ) activeScreen.Dispose(); - } Graphics.DeleteIb( defaultIb ); Graphics.Dispose(); Drawer2D.DisposeInstance(); diff --git a/Launcher2/Gui/Screens/UpdatesScreen.cs b/Launcher2/Gui/Screens/UpdatesScreen.cs index 84aa1b62e..ed47f9d58 100644 --- a/Launcher2/Gui/Screens/UpdatesScreen.cs +++ b/Launcher2/Gui/Screens/UpdatesScreen.cs @@ -1,11 +1,9 @@ using System; -using System.Diagnostics; using System.Drawing; using System.Globalization; using System.IO; -using System.Net; using ClassicalSharp; -using ClassicalSharp.TexturePack; +using Launcher2.Updater; namespace Launcher2 { @@ -97,38 +95,7 @@ namespace Launcher2 { void UpdateBuild( DateTime last, bool valid, string dir ) { if( last == DateTime.MinValue || !valid ) return; - - using( WebClient client = new WebClient() ) { - byte[] zipData = client.DownloadData( UpdateCheckTask.UpdatesUri + dir ); - MakeUpdatesFolder( zipData ); - } - - if( !OpenTK.Configuration.RunningOnWindows ) - return; // TODO: bash script for OSX and linux - if( !File.Exists( "update.bat" ) ) - File.WriteAllText( "update.bat", batch ); - - ProcessStartInfo info = new ProcessStartInfo( "cmd.exe", "/c update.bat" ); - info.CreateNoWindow = false; - info.UseShellExecute = false; - Process p = Process.Start( info ); - Process.GetCurrentProcess().Kill(); - } - - void MakeUpdatesFolder( byte[] zipData ) { - using( MemoryStream stream = new MemoryStream( zipData ) ) { - ZipReader reader = new ZipReader(); - Directory.CreateDirectory( "CS_Update" ); - - reader.ShouldProcessZipEntry = (f) => true; - reader.ProcessZipEntry = ProcessZipEntry; - reader.Extract( stream ); - } - } - - void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) { - string path = Path.Combine( "CS_Update", Path.GetFileName( filename ) ); - File.WriteAllBytes( path, data ); + Patcher.Update( dir ); } public override void Dispose() { @@ -138,30 +105,5 @@ namespace Launcher2 { titleFont.Dispose(); infoFont.Dispose(); } - - const string batch = - @" -@echo off -echo Waiting for launcher to exit.. -echo 5.. -sleep 1 -echo 4.. -sleep 1 -echo 3.. -sleep 1 -echo 2.. -sleep 1 -echo 1.. -sleep 1 - -set root=%CD% -echo Extracting files from CS_Update folder - -for /f ""tokens=*"" %%f in ('dir /b ""%root%\CS_Update""') do move ""%root%\CS_Update\%%f"" ""%root%\%%f"" -rmdir ""%root%\CS_Update"" - -echo Starting launcher again -start Launcher2.exe -exit"; } } diff --git a/Launcher2/Launcher2.csproj b/Launcher2/Launcher2.csproj index ccb09b833..da5bbf8fc 100644 --- a/Launcher2/Launcher2.csproj +++ b/Launcher2/Launcher2.csproj @@ -74,6 +74,8 @@ + + @@ -99,6 +101,7 @@ + diff --git a/Launcher2/Updater/Patcher.cs b/Launcher2/Updater/Patcher.cs new file mode 100644 index 000000000..dcdfdec34 --- /dev/null +++ b/Launcher2/Updater/Patcher.cs @@ -0,0 +1,44 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Net; +using ClassicalSharp.TexturePack; + +namespace Launcher2.Updater { + + public static class Patcher { + + public static void Update( string dir ) { + using( WebClient client = new WebClient() ) { + byte[] zipData = client.DownloadData( UpdateCheckTask.UpdatesUri + dir ); + MakeUpdatesFolder( zipData ); + } + + if( !OpenTK.Configuration.RunningOnWindows ) + return; // TODO: bash script for OSX and linux + File.WriteAllText( "update.bat", Scripts.BatchFile ); + + ProcessStartInfo info = new ProcessStartInfo( "cmd.exe", "/c update.bat" ); + info.CreateNoWindow = false; + info.UseShellExecute = false; + Process p = Process.Start( info ); + Process.GetCurrentProcess().Kill(); + } + + static void MakeUpdatesFolder( byte[] zipData ) { + using( MemoryStream stream = new MemoryStream( zipData ) ) { + ZipReader reader = new ZipReader(); + Directory.CreateDirectory( "CS_Update" ); + + reader.ShouldProcessZipEntry = (f) => true; + reader.ProcessZipEntry = ProcessZipEntry; + reader.Extract( stream ); + } + } + + static void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) { + string path = Path.Combine( "CS_Update", Path.GetFileName( filename ) ); + File.WriteAllBytes( path, data ); + } + } +} diff --git a/Launcher2/Updater/Scripts.cs b/Launcher2/Updater/Scripts.cs new file mode 100644 index 000000000..eb203c132 --- /dev/null +++ b/Launcher2/Updater/Scripts.cs @@ -0,0 +1,58 @@ +using System; + +namespace Launcher2.Updater { + + public static class Scripts { + + public const string BatchFile = + @"@echo off +echo Waiting for launcher to exit.. +echo 5.. +sleep 1 +echo 4.. +sleep 1 +echo 3.. +sleep 1 +echo 2.. +sleep 1 +echo 1.. +sleep 1 + +set root=%CD% +echo Extracting files from CS_Update folder + +for /f ""tokens=*"" %%f in ('dir /b ""%root%\CS_Update""') do move ""%root%\CS_Update\%%f"" ""%root%\%%f"" +rmdir ""%root%\CS_Update"" + +echo Starting launcher again +if exist Launcher2.exe ( +start Launcher2.exe +) else ( +start Launcher.exe +) +exit"; + + public const string BashFile = + @"#!/bin/bash +echo ""Waiting for launcher to exit.."" +echo 5.. +sleep 1 +echo 4.. +sleep 1 +echo 3.. +sleep 1 +echo 2.. +sleep 1 +echo 1.. +sleep 1 + +echo Starting launcher again +if [ -f ""Launcher2.exe"" ]; +then + exec mono ""Launcher2.exe"" +else + exec mono ""Launcher.exe"" +fi +"; + } +}