1- using System ;
1+ using Microsoft . Win32 ;
2+ using System ;
23using System . Collections . Generic ;
34using System . Diagnostics ;
45using System . IO ;
56using System . Linq ;
7+ using System . Reflection ;
8+ using System . Runtime . ExceptionServices ;
69using System . Text ;
710using System . Threading . Tasks ;
811using SystemPlus ;
@@ -12,16 +15,67 @@ namespace BuildPlate_Editor
1215 class Program
1316 {
1417 public static Window Window ;
18+ public static string baseDir ;
1519
20+ // todo: check json is buildplate
1621 static void Main ( string [ ] args )
1722 {
18- if ( ! File . Exists ( Environment . CurrentDirectory + "/texturesPath.txt" ) ) {
23+ // Get base path (.exe location)
24+ string myExecutable = Assembly . GetEntryAssembly ( ) . Location ;
25+
26+ if ( args != null && args . Length > 0 && args [ 0 ] == "setJsonDefault" ) {
27+ Util . SetAssociationWithExtension ( ".json" , "Json" , myExecutable , "BuildPlate" ) ;
28+ Console . WriteLine ( "Set .json as default, To Apply: Select .json file, click \" Open with\" , \" Choose another app\" , " +
29+ "Select BuildPlate_Editor, Check \" Always use this app...\" " ) ;
30+ Console . WriteLine ( "Press any key to continue..." ) ;
31+ Console . ReadKey ( true ) ;
32+ return ;
33+ }
34+
35+ baseDir = Path . GetDirectoryName ( myExecutable ) + "\\ " ;
36+ Console . WriteLine ( $ "Base directory: { baseDir } ") ;
37+
38+ AppDomain . CurrentDomain . UnhandledException += CurrentDomain_UnhandledException ;
39+
40+ if ( File . Exists ( baseDir + "askedForDefault" ) || args != null && args . Length > 0 )
41+ goto skipAks ;
42+
43+ Console . Write ( "Would you like to set this as default for .json files? (Y/N):" ) ;
44+ char typed = Console . ReadKey ( ) . KeyChar ;
45+ Console . WriteLine ( ) ;
46+ if ( typed == 'y' || typed == 'Y' ) {
47+ // Make this app default for .json files
48+ try {
49+ // Launch again with admin, so it can edit registry
50+ using ( Process configTool = new Process ( ) ) {
51+ configTool . StartInfo . FileName = myExecutable ;
52+ configTool . StartInfo . Arguments = "setJsonDefault" ;
53+ configTool . StartInfo . Verb = "runas" ;
54+ configTool . Start ( ) ;
55+ configTool . WaitForExit ( ) ;
56+ }
57+ Console . WriteLine ( "Set this as default for .json" ) ;
58+ } catch ( Exception ex ) {
59+ Console . WriteLine ( $ "Failed to edit registry: { ex } ") ;
60+ Console . WriteLine ( "Press any key to continue..." ) ;
61+ Console . ReadKey ( true ) ;
62+ goto skipAks ;
63+ }
64+ }
65+ else
66+ Console . WriteLine ( "Ok :(, I won't aks again" ) ;
67+
68+ File . WriteAllBytes ( baseDir + "askedForDefault" , new byte [ 0 ] ) ;
69+
70+ skipAks :
71+
72+ if ( ! File . Exists ( baseDir + "texturesPath.txt" ) ) {
1973 Console . WriteLine ( "texturesPath.txt doen't exist" ) ;
2074 Console . WriteLine ( $ "Extract mce resource pack 2 times and set path to {{path}}/textures/blocks/") ;
2175 Console . ReadKey ( true ) ;
2276 return ;
2377 }
24- string texturesPath = File . ReadAllText ( Environment . CurrentDirectory + "/ texturesPath.txt" ) ;
78+ string texturesPath = File . ReadAllText ( baseDir + "texturesPath.txt" ) ;
2579 if ( texturesPath == string . Empty || ! Directory . Exists ( texturesPath ) ) {
2680 Console . WriteLine ( $ "path inside texturesPath.txt ({ texturesPath } ) doesn't exist") ;
2781 Console . WriteLine ( $ "Extract mce resource pack 2 times and set path to {{path}}/textures/blocks/") ;
@@ -33,16 +87,23 @@ static void Main(string[] args)
3387 texturesPath += '/' ;
3488 World . textureBasePath = texturesPath ;
3589#if DEBUG
36- World . targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\b22b4ada-49e5-41c9-8bf0-76e36c5ec7b2.json" ;
90+ //World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\7cd6d53b-1715-4b22-9a99-d6d43edd61df.json";
91+ World . targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\00d1fa99-7acf-449d-bb4f-8d11127bd6e3.json" ;
3792#else
3893 Console . Write ( "Build plate to edit (.json): " ) ;
39- string buildPlate = Console . ReadLine ( ) ;
40- if ( ! File . Exists ( buildPlate ) ) {
41- Console . WriteLine ( $ "build plate \" { buildPlate } \" doesn't exist") ;
42- Console . ReadKey ( true ) ;
43- return ;
94+ if ( args != null && args . Length > 0 && File . Exists ( args [ 0 ] ) ) {
95+ World . targetFilePath = args [ 0 ] ;
96+ Console . WriteLine ( args [ 0 ] ) ;
97+ }
98+ else {
99+ string buildPlate = Console . ReadLine ( ) ;
100+ if ( ! File . Exists ( buildPlate ) ) {
101+ Console . WriteLine ( $ "build plate \" { buildPlate } \" doesn't exist") ;
102+ Console . ReadKey ( true ) ;
103+ return ;
104+ }
105+ World . targetFilePath = buildPlate ;
44106 }
45- World . targetFilePath = buildPlate ;
46107#endif
47108 Window = new Window ( ) ;
48109 string version ;
@@ -56,6 +117,16 @@ static void Main(string[] args)
56117 Window . Run ( 60d ) ;
57118 }
58119
120+ private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
121+ {
122+ Exception ex = ( Exception ) e . ExceptionObject ;
123+ Console . WriteLine ( $ "CurrentDomain_UnhandledException: { ex . Message } ") ;
124+ Console . WriteLine ( $ "Source: { ex . Source } ") ;
125+ Console . WriteLine ( $ "Stack trace: { ex . StackTrace } ") ;
126+ Console . WriteLine ( "Press any key to exit..." ) ;
127+ Console . ReadKey ( true ) ;
128+ }
129+
59130 private static void LowVersion ( )
60131 {
61132 Console . WriteLine ( "OpenGL version is low. The editor might not work correctly" ) ;
0 commit comments