@@ -6,16 +6,30 @@ namespace AutoUpdater.Configs;
66
77public static class ConfigParser
88{
9- public static Config LoadConfig ( )
9+ public static Config LoadConfigs ( )
1010 {
11+ // Check is valid
12+ if ( ! IsDumpValid ( ) || ! ArePreferencesValid ( ) )
13+ {
14+ throw new Exception ( "Config is not valid" ) ;
15+ }
16+
17+ Config config = new ( ) ;
18+
19+
20+
1121 // Load json file
12- var json = File . ReadAllText ( Environment . CurrentDirectory + "\\ config .json" ) ;
22+ var prefJson = File . ReadAllText ( Environment . CurrentDirectory + "\\ preferences .json" ) ;
1323 // Deserialize json
14- var config = JsonConvert . DeserializeObject < Config > ( json ) ;
15- // Return config
24+ config . Preferences = JsonConvert . DeserializeObject < Preferences > ( prefJson ) ;
25+
26+ // Load json file
27+ var dumpJson = File . ReadAllText ( Environment . CurrentDirectory + "\\ dump.json" ) ;
28+ // Deserialize json
29+ config . SDK = JsonConvert . DeserializeObject < UpdateConfig > ( dumpJson ) ;
1630
1731 // Make sure to convert all the resolvers to the correct type
18- foreach ( var updateItem in config ! . UpdateItems )
32+ foreach ( var updateItem in config ! . SDK . Items )
1933 {
2034 updateItem . Resolver = updateItem . Resolver . Type switch
2135 {
@@ -31,83 +45,82 @@ public static Config LoadConfig()
3145 public static void SaveConfig ( )
3246 {
3347 // Serialize config
34- var json = JsonConvert . SerializeObject ( Program . Config , Formatting . Indented ) ;
48+ var prefJson = JsonConvert . SerializeObject ( Program . Config . Preferences , Formatting . Indented ) ;
3549 // Write to file
36- File . WriteAllText ( Environment . CurrentDirectory + "\\ config.json" , json ) ;
50+ File . WriteAllText ( Environment . CurrentDirectory + "\\ preferences.json" , prefJson ) ;
51+
52+ // Serialize config
53+ var dumpJson = JsonConvert . SerializeObject ( Program . Config . SDK , Formatting . Indented ) ;
54+ // Write to file
55+ File . WriteAllText ( Environment . CurrentDirectory + "\\ dump.json" , dumpJson ) ;
3756 }
3857
39- public static bool IsConfigValid ( )
58+ public static bool ArePreferencesValid ( )
4059 {
4160 // Make sure the config file exists
42- if ( ! File . Exists ( Environment . CurrentDirectory + "\\ config .json" ) )
43- {
44- return false ;
45- }
46-
61+ return File . Exists ( Environment . CurrentDirectory + " \\ dump.json" ) && File . Exists ( Environment . CurrentDirectory + "\\ preferences .json" ) ;
62+ }
63+
64+ public static bool IsDumpValid ( )
65+ {
4766 // Make sure its not empty
48- if ( new FileInfo ( Environment . CurrentDirectory + "\\ config.json" ) . Length == 0 )
49- {
50- return false ;
51- }
52-
53- // Make sure the config is valid
54- try
55- {
56- LoadConfig ( ) ;
57- }
58- catch ( Exception )
59- {
60- return false ;
61- }
62-
63- return true ;
67+ return new FileInfo ( Environment . CurrentDirectory + "\\ dump.json" ) . Length != 0 && new FileInfo ( Environment . CurrentDirectory + "\\ preferences.json" ) . Length != 0 ;
6468 }
6569
6670 public static void SetupConfig ( )
6771 {
68- Console . Write ( "Enter the directory of LLVM's installation: " ) ;
69- Program . Config . LlvmInstallDirectory = Console . ReadLine ( ) ;
70-
71- Program . Config . PdbUtil = Program . Config . LlvmInstallDirectory + "\\ llvm-pdbutil.exe" ;
72- Program . Config . DemangleUtil = Program . Config . LlvmInstallDirectory + "\\ llvm-undname.exe" ;
7372
74- if ( ! File . Exists ( Program . Config . PdbUtil ) )
73+ if ( ! File . Exists ( "preferences.json" ) || File . ReadAllText ( "preferences.json" ) == "" )
7574 {
76- Console . WriteLine ( "llvm-pdbutil.exe not found. Please enter the directory of llvm-pdbutil.exe:" ) ;
77- Program . Config . PdbUtil = Console . ReadLine ( ) ;
78- }
75+ Console . Write ( "Enter the directory of LLVM's installation: " ) ;
76+ Program . Config . Preferences ! . LlvmInstallDirectory = Console . ReadLine ( ) ;
7977
80- if ( ! File . Exists ( Program . Config . DemangleUtil ) )
81- {
82- Console . WriteLine ( "llvm-undname.exe not found. Please enter the directory of llvm-undname.exe:" ) ;
83- Console . WriteLine ( "This file is usually installed when using MSYS2. If you do not have this file, you can install MSYS2 from https://www.msys2.org/" ) ;
84- Program . Config . DemangleUtil = Console . ReadLine ( ) ;
85- }
78+ Program . Config . Preferences . PdbUtil = Program . Config . Preferences . LlvmInstallDirectory + "\\ llvm-pdbutil.exe" ;
79+ Program . Config . Preferences . DemangleUtil = Program . Config . Preferences . LlvmInstallDirectory + "\\ llvm-undname.exe" ;
8680
87- Console . Write ( "Enter the directory you wish to download the server to: " ) ;
88- Program . Config . WorkingDirectory = Console . ReadLine ( ) ;
81+ if ( ! File . Exists ( Program . Config . Preferences . PdbUtil ) )
82+ {
83+ Console . WriteLine ( "llvm-pdbutil.exe not found. Please enter the directory of llvm-pdbutil.exe:" ) ;
84+ Program . Config . Preferences . PdbUtil = Console . ReadLine ( ) ;
85+ }
8986
90- // Create a new update item for an example
91- UpdateItem example = new ( )
92- {
93- Name = "Example Offset" ,
94- Function = "?getSupplies@Player@@QEBAAEBVPlayerInventory@@XZ" ,
95- Resolver = new OffsetResolver ( "3" )
96- } ;
87+ if ( ! File . Exists ( Program . Config . Preferences . DemangleUtil ) )
88+ {
89+ Console . WriteLine ( "llvm-undname.exe not found. Please enter the directory of llvm-undname.exe:" ) ;
90+ Console . WriteLine ( "This file is usually installed when using MSYS2. If you do not have this file, you can install MSYS2 from https://www.msys2.org/" ) ;
91+ Program . Config . Preferences . DemangleUtil = Console . ReadLine ( ) ;
92+ }
9793
98- // Add the example update item to the config
99- Program . Config . UpdateItems . Add ( example ) ;
94+ Console . Write ( "Enter the directory you wish to download the server to: " ) ;
95+ Program . Config . Preferences . WorkingDirectory = Console . ReadLine ( ) ;
96+ }
10097
101- // Add pattern resolver example
102- UpdateItem example2 = new ( )
98+ if ( ! File . Exists ( "dump.json" ) || File . ReadAllText ( "dump.json" ) == "" )
10399 {
104- Name = "Example Pattern" ,
105- Function = "?getSupplies@Player@@QEBAAEBVPlayerInventory@@XZ" ,
106- Resolver = new PatternResolver ( "? ? ? ? C3" )
107- } ;
100+
101+
102+ // Create a new update item for an example
103+ UpdateItem example = new ( )
104+ {
105+ Name = "Example Offset" ,
106+ Function = "?getSupplies@Player@@QEBAAEBVPlayerInventory@@XZ" ,
107+ Resolver = new OffsetResolver ( "3" )
108+ } ;
108109
109- // Add the example update item to the config
110- Program . Config . UpdateItems . Add ( example2 ) ;
110+ // Add the example update item to the config
111+ Program . Config . SDK . Items . Add ( example ) ;
112+
113+ // Add pattern resolver example
114+ UpdateItem example2 = new ( )
115+ {
116+ Name = "Example Pattern" ,
117+ Function = "?getSupplies@Player@@QEBAAEBVPlayerInventory@@XZ" ,
118+ Resolver = new PatternResolver ( "? ? ? ? C3" )
119+ } ;
120+
121+ // Add the example update item to the config
122+ Program . Config . SDK . Items . Add ( example2 ) ;
123+ }
111124
112125 SaveConfig ( ) ;
113126 }
0 commit comments