Add basis for a warning screen, modularise updater and make it work when Launcher2 is called Launcher.exe.

This commit is contained in:
UnknownShadow200 2015-11-09 12:57:56 +11:00
parent a3807afd53
commit 1b5d98f413
7 changed files with 165 additions and 64 deletions

View File

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

View File

@ -101,6 +101,7 @@
<Compile Include="2D\Screens\Menu\TexturePackScreen.cs" />
<Compile Include="2D\Screens\HudScreen.cs" />
<Compile Include="2D\Screens\Screen.cs" />
<Compile Include="2D\Screens\WarningScreen.cs" />
<Compile Include="2D\Texture.cs" />
<Compile Include="2D\Widgets\BlockHotbarWidget.cs" />
<Compile Include="2D\Widgets\Chat\ChatTextWidget.cs" />

View File

@ -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();

View File

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

View File

@ -74,6 +74,8 @@
<Compile Include="Patcher\ZipWriter.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Updater\Scripts.cs" />
<Compile Include="Updater\Patcher.cs" />
<Compile Include="Utils\Client.cs" />
<Compile Include="Utils\ClientStartData.cs" />
<Compile Include="Utils\JSON.cs" />
@ -99,6 +101,7 @@
<Folder Include="Gui\TableWidget" />
<Folder Include="Gui\Screens" />
<Folder Include="Gui\Widgets" />
<Folder Include="Updater" />
<Folder Include="Utils" />
<Folder Include="Patcher" />
<Folder Include="WebService" />

View File

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

View File

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