Use absolute instead of relative paths, launching ClassicalSharp from terminal works properly now.

This commit is contained in:
UnknownShadow200 2015-12-27 22:51:26 +11:00
parent ac1934b37f
commit f03148555e
22 changed files with 111 additions and 64 deletions

View file

@ -150,6 +150,7 @@ namespace ClassicalSharp {
string textPath;
void SaveMap( string path ) {
path = Path.Combine( Program.AppDirectory, path );
try {
using( FileStream fs = new FileStream( path, FileMode.CreateNew, FileAccess.Write ) ) {
IMapFileFormat map = new MapCw();

View file

@ -7,16 +7,16 @@ namespace ClassicalSharp {
public LoadLevelScreen( Game game ) : base( game ) {
titleText = "Select a level";
string directory = Environment.CurrentDirectory;
string[] cwFiles = Directory.GetFiles( directory, "*.cw", SearchOption.AllDirectories );
string[] datFiles = Directory.GetFiles( directory, "*.dat", SearchOption.AllDirectories );
string dir = Program.AppDirectory;
string[] cwFiles = Directory.GetFiles( dir, "*.cw", SearchOption.AllDirectories );
string[] datFiles = Directory.GetFiles( dir, "*.dat", SearchOption.AllDirectories );
files = new string[cwFiles.Length + datFiles.Length];
Array.Copy( cwFiles, 0, files, 0, cwFiles.Length );
Array.Copy( datFiles, 0, files, cwFiles.Length, datFiles.Length );
for( int i = 0; i < files.Length; i++ ) {
string absolutePath = files[i];
files[i] = absolutePath.Substring( directory.Length + 1 );
files[i] = absolutePath.Substring( dir.Length );
}
Array.Sort( files );
}
@ -29,6 +29,7 @@ namespace ClassicalSharp {
protected override void TextButtonClick( Game game, Widget widget ) {
string path = ((ButtonWidget)widget).Text;
path = Path.Combine( Program.AppDirectory, path );
if( File.Exists( path ) )
LoadMap( path );
}
@ -41,7 +42,7 @@ namespace ClassicalSharp {
mapFile = new MapFcm3();
} else if( path.EndsWith( ".cw" ) ) {
mapFile = new MapCw();
}
}
try {
using( FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) ) {

View file

@ -83,6 +83,7 @@ namespace ClassicalSharp {
MakeDescWidget( "Please enter a filename" );
return;
}
text = Path.Combine( Program.AppDirectory, text );
text = Path.ChangeExtension( text, ".cw" );
if( File.Exists( text ) ) {
@ -117,6 +118,7 @@ namespace ClassicalSharp {
string textPath;
void SaveMap( string path ) {
path = Path.Combine( Program.AppDirectory, path );
try {
if( File.Exists( path ) )
File.Delete( path );

View file

@ -9,12 +9,12 @@ namespace ClassicalSharp {
public TexturePackScreen( Game game ) : base( game ) {
titleText = "Select a texture pack zip";
string directory = Environment.CurrentDirectory;
files = Directory.GetFiles( directory, "*.zip", SearchOption.AllDirectories );
string dir = Program.AppDirectory;
files = Directory.GetFiles( dir, "*.zip", SearchOption.AllDirectories );
for( int i = 0; i < files.Length; i++ ) {
string absolutePath = files[i];
files[i] = absolutePath.Substring( directory.Length + 1 );
files[i] = absolutePath.Substring( dir.Length );
}
Array.Sort( files );
}
@ -26,13 +26,15 @@ namespace ClassicalSharp {
}
protected override void TextButtonClick( Game game, Widget widget ) {
string path = ((ButtonWidget)widget).Text;
if( File.Exists( path ) ) {
game.DefaultTexturePack = path;
TexturePackExtractor extractor = new TexturePackExtractor();
extractor.Extract( path, game );
Recreate();
}
string file = ((ButtonWidget)widget).Text;
string path = Path.Combine( Program.AppDirectory, file );
if( !File.Exists( path ) )
return;
game.DefaultTexturePack = file;
TexturePackExtractor extractor = new TexturePackExtractor();
extractor.Extract( path, game );
Recreate();
}
}
}

View file

@ -28,7 +28,8 @@ namespace ClassicalSharp.Audio {
}
void InitMusic() {
musicFiles = Directory.GetFiles( "audio", "*.ogg" );
string path = Path.Combine( Program.AppDirectory, "audio" );
musicFiles = Directory.GetFiles( path, "*.ogg" );
disposingMusic = false;
musicThread = MakeThread( DoMusicThread, ref musicOut,
"ClassicalSharp.DoMusic" );
@ -39,8 +40,10 @@ namespace ClassicalSharp.Audio {
Random rnd = new Random();
while( !disposingMusic ) {
string file = musicFiles[rnd.Next( 0, musicFiles.Length )];
Console.WriteLine( "playing music file: " + file );
using( FileStream fs = File.OpenRead( file ) ) {
string path = Path.Combine( Program.AppDirectory, file );
Utils.LogDebug( "playing music file: " + file );
using( FileStream fs = File.OpenRead( path ) ) {
OggContainer container = new OggContainer( fs );
musicOut.PlayStreaming( container );
}

View file

@ -19,8 +19,9 @@ namespace ClassicalSharp.Audio {
}
public void Init( string group ) {
string binPath = Path.Combine( "audio", group + ".bin" );
string txtPath = Path.Combine( "audio", group + ".txt" );
string basePath = Path.Combine( Program.AppDirectory, "audio" );
string binPath = Path.Combine( basePath, group + ".bin" );
string txtPath = Path.Combine( basePath, group + ".txt" );
Data = File.ReadAllBytes( binPath );
ReadMetadata( txtPath );

View file

@ -89,15 +89,16 @@ namespace ClassicalSharp {
}
void OpenChatFile( DateTime now ) {
if( !Directory.Exists( "logs" ) )
Directory.CreateDirectory( "logs" );
string basePath = Path.Combine( Program.AppDirectory, "logs" );
if( !Directory.Exists( basePath ) )
Directory.CreateDirectory( basePath );
string date = now.ToString( "yyyy-MM-dd" );
// Ensure multiple instances do not end up overwriting each other's log entries.
for( int i = 0; i < 20; i++ ) {
string id = i == 0 ? "" : " _" + i;
string fileName = "chat-" + date + id + ".log";
string path = Path.Combine( "logs", fileName );
string path = Path.Combine( basePath, fileName );
FileStream stream = null;
try {

View file

@ -155,7 +155,10 @@ namespace ClassicalSharp {
/// <remarks> If the custom default texture pack specified by the user could not be found,
/// this method returns "default.zip". </remarks>
public string DefaultTexturePack {
get { return File.Exists( defTexturePack ) ? defTexturePack : "default.zip"; }
get {
string path = Path.Combine( Program.AppDirectory, defTexturePack );
return File.Exists( path ) ? defTexturePack : "default.zip";
}
set {
defTexturePack = value;
Options.Set( OptionsKey.DefaultTexturePack, value );

View file

@ -133,10 +133,14 @@ namespace ClassicalSharp {
}
void LoadIcon() {
if( File.Exists( "Launcher2.exe" ) ) {
Icon = Icon.ExtractAssociatedIcon( "Launcher2.exe" );
} else if( File.Exists( "Launcher.exe" ) ) {
Icon = Icon.ExtractAssociatedIcon( "Launcher.exe" );
string launcherPath = Path.Combine( Program.AppDirectory, "Launcher2.exe" );
if( File.Exists( launcherPath ) ) {
Icon = Icon.ExtractAssociatedIcon( launcherPath );
return;
}
launcherPath = Path.Combine( Program.AppDirectory, "Launcher.exe" );
if( File.Exists( launcherPath ) ) {
Icon = Icon.ExtractAssociatedIcon( launcherPath );
}
}
@ -261,13 +265,13 @@ namespace ClassicalSharp {
}
void TakeScreenshot() {
if( !Directory.Exists( "screenshots" ) ) {
Directory.CreateDirectory( "screenshots" );
}
string path = Path.Combine( Program.AppDirectory, "screenshots" );
if( !Directory.Exists( path ) )
Directory.CreateDirectory( path );
string timestamp = DateTime.Now.ToString( "dd-MM-yyyy-HH-mm-ss" );
string file = "screenshot_" + timestamp + ".png";
string path = Path.Combine( "screenshots", file );
path = Path.Combine( "screenshots", file );
Graphics.TakeScreenshot( path, ClientSize );
Chat.Add( "&eTaken screenshot as: " + file );
screenshotRequested = false;

View file

@ -9,12 +9,16 @@ namespace ClassicalSharp {
public const string AppName = "ClassicalSharp 0.98.2";
public static string AppDirectory;
[STAThread]
static void Main( string[] args ) {
ErrorHandler.InstallHandler( "client.log" );
AppDirectory = AppDomain.CurrentDomain.BaseDirectory;
string logPath = Path.Combine( AppDirectory, "client.log" );
ErrorHandler.InstallHandler( logPath );
Utils.LogDebug( "Starting " + AppName + ".." );
if( !File.Exists( "default.zip" ) ) {
if( !File.Exists( Path.Combine( AppDirectory, "default.zip" ) ) ) {
Utils.LogDebug( "default.zip not found. Cannot start." );
return;
}

View file

@ -20,7 +20,8 @@ namespace ClassicalSharp {
public bool Load() {
try {
using( Stream fs = File.OpenRead( Path.Combine( folder, file ) ) )
string path = Path.Combine( Program.AppDirectory, folder );
using( Stream fs = File.OpenRead( Path.Combine( path, file ) ) )
using( StreamReader reader = new StreamReader( fs, false ) )
{
string line;
@ -40,10 +41,11 @@ namespace ClassicalSharp {
public bool Save() {
try {
if( !Directory.Exists( folder ) )
Directory.CreateDirectory( folder );
string path = Path.Combine( Program.AppDirectory, folder );
if( !Directory.Exists( path ) )
Directory.CreateDirectory( path );
using( Stream fs = File.Create( Path.Combine( folder, file ) ) )
using( Stream fs = File.Create( Path.Combine( path, file ) ) )
using( StreamWriter writer = new StreamWriter( fs ) )
{
foreach( string value in acceptedUrls )

View file

@ -59,8 +59,9 @@ namespace ClassicalSharp.TexturePack {
public static void AddToCache( string url, Bitmap bmp ) {
string path = MakePath( url );
try {
if( !Directory.Exists( Folder ) )
Directory.CreateDirectory( Folder );
string basePath = Path.Combine( Program.AppDirectory, Folder );
if( !Directory.Exists( basePath ) )
Directory.CreateDirectory( basePath );
bmp.Save( path, ImageFormat.Png );
} catch( IOException ex ) {
@ -72,8 +73,9 @@ namespace ClassicalSharp.TexturePack {
public static void AddToCache( string url, byte[] data ) {
string path = MakePath( url );
try {
if( !Directory.Exists( Folder ) )
Directory.CreateDirectory( Folder );
string basePath = Path.Combine( Program.AppDirectory, Folder );
if( !Directory.Exists( basePath ) )
Directory.CreateDirectory( basePath );
File.WriteAllBytes( path, data );
} catch( IOException ex ) {
@ -86,7 +88,8 @@ namespace ClassicalSharp.TexturePack {
static string MakePath( string url ) {
byte[] utf8 = Encoding.UTF8.GetBytes( url );
uint crc32 = CRC32( utf8 );
return Path.Combine( Folder, crc32.ToString() );
string basePath = Path.Combine( Program.AppDirectory, Folder );
return Path.Combine( basePath, crc32.ToString() );
}
static uint CRC32( byte[] data ) {

View file

@ -11,6 +11,7 @@ namespace ClassicalSharp.TexturePack {
Game game;
public void Extract( string path, Game game ) {
path = Path.Combine( Program.AppDirectory, path );
using( Stream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) )
Extract( fs, game );
}

View file

@ -50,6 +50,7 @@ namespace ClassicalSharp {
public static Dictionary<string, string> OptionsSet = new Dictionary<string, string>();
public static bool HasChanged;
const string OptionsFile = "options.txt";
public static string Get( string key ) {
string value;
@ -116,11 +117,14 @@ namespace ClassicalSharp {
HasChanged = true;
}
public const string OptionsFile = "options.txt";
public static bool Load() {
// i.e. when running from the launcher
if( Program.AppDirectory == null )
Program.AppDirectory = AppDomain.CurrentDomain.BaseDirectory;
try {
using( Stream fs = File.OpenRead( OptionsFile ) )
string path = Path.Combine( Program.AppDirectory, OptionsFile );
using( Stream fs = File.OpenRead( path ) )
using( StreamReader reader = new StreamReader( fs, false ) )
LoadFrom( reader );
return true;
@ -151,7 +155,8 @@ namespace ClassicalSharp {
public static bool Save() {
try {
using( Stream fs = File.Create( OptionsFile ) )
string path = Path.Combine( Program.AppDirectory, OptionsFile );
using( Stream fs = File.Create( path ) )
using( StreamWriter writer = new StreamWriter( fs ) )
SaveTo( writer );
return true;

View file

@ -80,9 +80,10 @@ namespace Launcher2 {
void Draw() {
widgetIndex = 0;
string exePath = Path.Combine( Program.AppDirectory, "ClassicalSharp.exe" );
MakeLabelAt( "Your build:", titleFont, Anchor.Centre, Anchor.Centre, -55, -120 );
string yourBuild = File.GetLastWriteTime( "ClassicalSharp.exe" ).ToString( dateFormat );
string yourBuild = File.GetLastWriteTime( exePath ).ToString( dateFormat );
MakeLabelAt( yourBuild, infoFont, Anchor.Centre, Anchor.Centre, 100, -120 );
MakeLabelAt( "Latest stable:", titleFont, Anchor.Centre, Anchor.Centre, -70, -80 );

View file

@ -12,9 +12,10 @@ namespace Launcher2 {
Options.Load();
LauncherSkin.LoadFromOptions();
string texPack = Options.Get( OptionsKey.DefaultTexturePack ) ?? "default.zip";
texPack = Path.Combine( Program.AppDirectory, texPack );
if( !File.Exists( texPack ) )
texPack = "default.zip";
texPack = Path.Combine( Program.AppDirectory, "default.zip" );
if( !File.Exists( texPack ) )
return;

View file

@ -116,7 +116,7 @@ namespace Launcher2 {
return true;
}
public void Run() {
public void Run() {
Window = new NativeWindow( 640, 480, Program.AppName, 0,
GraphicsMode.Default, DisplayDevice.Default );
Window.Visible = true;

View file

@ -10,8 +10,9 @@ namespace Launcher2 {
internal AsyncDownloader downloader;
SoundPatcher digPatcher, stepPatcher;
public ResourceFetcher() {
digPath = Path.Combine( "audio", "dig" );
stepPath = Path.Combine( "audio", "step" );
string basePath = Path.Combine( Program.AppDirectory, "audio" );
digPath = Path.Combine( basePath, "dig" );
stepPath = Path.Combine( basePath, "step" );
}
const string jarClassicUri = "http://s3.amazonaws.com/Minecraft.Download/versions/c0.30_01c/c0.30_01c.jar";
@ -105,12 +106,14 @@ namespace Launcher2 {
}
public void CheckResourceExistence() {
if( !Directory.Exists( "audio" ) )
Directory.CreateDirectory( "audio" );
string audioPath = Path.Combine( Program.AppDirectory, "audio" );
if( !Directory.Exists( audioPath ) )
Directory.CreateDirectory( audioPath );
AllResourcesExist = File.Exists( digPath + ".bin" )
&& File.Exists( stepPath + ".bin" );
defaultZipExists = File.Exists( "default.zip" );
string zipPath = Path.Combine( Program.AppDirectory, "default.zip" );
defaultZipExists = File.Exists( zipPath );
if( !defaultZipExists ) {
// classic.jar + 1.6.2.jar + terrain-patch.png + gui.png
DownloadSize += (291 + 4621 + 7 + 21) / 1024f;
@ -119,7 +122,7 @@ namespace Launcher2 {
}
for( int i = 0; i < musicFiles.Length; i++ ) {
string file = Path.Combine( "audio", musicFiles[i] + ".ogg" );
string file = Path.Combine( audioPath, musicFiles[i] + ".ogg" );
musicExists[i] = File.Exists( file );
if( !musicExists[i] ) {
DownloadSize += musicSizes[i] / 1024f;
@ -154,7 +157,8 @@ namespace Launcher2 {
return false;
if( data == null ) continue;
string path = Path.Combine( "audio", name + ".ogg" );
string path = Path.Combine( Program.AppDirectory, "audio" );
path = Path.Combine( path, name + ".ogg" );
File.WriteAllBytes( path, data );
}
return true;

View file

@ -21,10 +21,11 @@ namespace Launcher2 {
reader = new ZipReader();
reader.ShouldProcessZipEntry = ShouldProcessZipEntry_Classic;
reader.ProcessZipEntry = ProcessZipEntry_Classic;
string path = Path.Combine( Program.AppDirectory, "default.zip" );
using( Stream srcClassic = new MemoryStream( jarClassic ),
srcModern = new MemoryStream( jar162 ),
dst = new FileStream( "default.zip", FileMode.Create, FileAccess.Write ) ) {
dst = new FileStream( path, FileMode.Create, FileAccess.Write ) ) {
writer = new ZipWriter( dst );
reader.Extract( srcClassic );

View file

@ -1,4 +1,5 @@
using System;
using System.IO;
using ClassicalSharp;
namespace Launcher2 {
@ -7,9 +8,13 @@ namespace Launcher2 {
public const string AppName = "ClassicalSharp Launcher 0.98.2";
public static string AppDirectory;
[STAThread]
static void Main( string[] args ) {
ErrorHandler.InstallHandler( "launcher.log" );
AppDirectory = AppDomain.CurrentDomain.BaseDirectory;
string logPath = Path.Combine( AppDirectory, "launcher.log" );
ErrorHandler.InstallHandler( logPath );
LauncherWindow window = new LauncherWindow();
window.Run();
}

View file

@ -22,11 +22,12 @@ namespace Launcher2.Updater {
static void LaunchUpdateScript() {
ProcessStartInfo info;
if( OpenTK.Configuration.RunningOnWindows ) {
File.WriteAllText( "update.bat", Scripts.BatchFile );
string path = Path.Combine( Program.AppDirectory, "update.bat" );
File.WriteAllText( path, Scripts.BatchFile );
info = new ProcessStartInfo( "cmd.exe", "/c update.bat" );
} else {
File.WriteAllText( "update.sh", Scripts.BashFile.Replace( "\r\n", "\n" ) );
string path = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "update.sh" );
string path = Path.Combine( Program.AppDirectory, "update.sh" );
File.WriteAllText( path, Scripts.BashFile.Replace( "\r\n", "\n" ) );
const int flags = 0x7;// read | write | executable
int code = chmod( path, (flags << 6) | (flags << 3) | 4 );
if( code != 0 )
@ -55,7 +56,8 @@ namespace Launcher2.Updater {
}
static void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) {
string path = Path.Combine( "CS_Update", Path.GetFileName( filename ) );
string path = Path.Combine( Program.AppDirectory, "CS_Update" );
path = Path.Combine( path, Path.GetFileName( filename ) );
File.WriteAllBytes( path, data );
}
}

View file

@ -22,7 +22,7 @@ namespace Launcher2 {
static bool StartImpl( ClientStartData data, bool classicubeSkins,
string args, ref bool shouldExit ) {
Process process = null;
string path = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "ClassicalSharp.exe" );
string path = Path.Combine( Program.AppDirectory, "ClassicalSharp.exe" );
if( !File.Exists( path ) )
return false;