Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit 9d8c24c

Browse files
author
Flash
committed
Different files for prefs and dump items now
1 parent 5ed70f9 commit 9d8c24c

File tree

6 files changed

+202
-162
lines changed

6 files changed

+202
-162
lines changed

AutoUpdater/Configs/Config.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace AutoUpdater.Configs;
44

5-
public class Config
5+
public class Preferences
66
{
77
public string? WorkingDirectory;
88
public string? PdbFile;
@@ -11,7 +11,16 @@ public class Config
1111
public string? PdbUtil;
1212
public string? DemangleUtil;
1313
public string? OutputFile = "output.txt";
14+
}
15+
16+
public class UpdateConfig
17+
{
18+
public string? Author;
19+
public List<UpdateItem> Items = new();
20+
}
1421

15-
public List<UpdateItem> UpdateItems = new();
16-
17-
}
22+
public class Config
23+
{
24+
public Preferences? Preferences = new();
25+
public UpdateConfig? SDK = new();
26+
}

AutoUpdater/Configs/ConfigParser.cs

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@ namespace AutoUpdater.Configs;
66

77
public 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
}

AutoUpdater/Program.cs

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,101 +7,111 @@ namespace AutoUpdater;
77
internal static class Program
88
{
99
public static Config Config = new();
10-
10+
1111
public static Dictionary<string, PublicSymbol> PublicSymbolsDict = new();
1212
public static List<Section> Sections = new();
1313

1414
private static async Task Main()
1515
{
16-
while (true)
17-
{
18-
Console.Title = "Auto Updater";
19-
Console.ForegroundColor = ConsoleColor.DarkCyan;
16+
Console.Title = "Auto Updater";
17+
Console.ForegroundColor = ConsoleColor.DarkCyan;
2018

21-
Misc.DrawASCII();
19+
Misc.DrawASCII();
2220

23-
if (!ConfigParser.IsConfigValid())
21+
if (!ConfigParser.ArePreferencesValid())
22+
{
23+
// Delete it if it exists
24+
if (File.Exists(Environment.CurrentDirectory + "\\preferences.json"))
2425
{
25-
// Delete it if it exists
26-
if (File.Exists(Environment.CurrentDirectory + "\\config.json"))
27-
{
28-
File.Delete(Environment.CurrentDirectory + "\\config.json");
29-
}
30-
31-
Console.WriteLine("Creating config...");
32-
ConfigParser.SetupConfig();
26+
File.Delete(Environment.CurrentDirectory + "\\preferences.json");
3327
}
34-
else
28+
Console.WriteLine("Configuration file not found. Creating");
29+
30+
31+
ConfigParser.SetupConfig();
32+
return;
33+
}
34+
35+
if (!ConfigParser.IsDumpValid())
36+
{
37+
if (File.Exists(Environment.CurrentDirectory + "\\dump.json"))
3538
{
36-
Console.WriteLine("Loading config...");
37-
Config = ConfigParser.LoadConfig();
39+
File.Delete(Environment.CurrentDirectory + "\\dump.json");
3840
}
41+
Console.WriteLine("Configuration file not found. Creating");
42+
43+
ConfigParser.SetupConfig();
44+
return;
45+
}
3946

40-
Console.WriteLine("Download latest version? (Y/n)");
41-
var input = Console.ReadLine();
47+
48+
Console.WriteLine("Loading config...");
49+
Config = ConfigParser.LoadConfigs();
50+
51+
Console.WriteLine("Download latest version? (Y/n)");
52+
var input = Console.ReadLine();
4253

43-
if (!input!.ToLower().Contains('n'))
44-
{
45-
// Try to print out the version
46-
try
47-
{
48-
var folderName = await Misc.DownloadLatest();
49-
Console.WriteLine($"Version: {folderName}");
50-
51-
Config.PdbFile = $@"{Config.WorkingDirectory}\{folderName}\bedrock_server.pdb";
52-
Config.ExeFile = $@"{Config.WorkingDirectory}\{folderName}\bedrock_server.exe";
53-
54-
ConfigParser.SaveConfig();
55-
}
56-
catch (Exception)
57-
{
58-
Console.WriteLine("Failed to get version.");
59-
}
60-
}
61-
else
54+
if (!input!.ToLower().Contains('n'))
55+
{
56+
// Try to print out the version
57+
try
6258
{
63-
Console.Write("Enter a folder name (Must be in the working directory):");
64-
var folderName = Console.ReadLine();
59+
var folderName = await Misc.DownloadLatest();
60+
Console.WriteLine($"Version: {folderName}");
6561

66-
Config.PdbFile = $@"{Config.WorkingDirectory}\{folderName}\bedrock_server.pdb";
67-
Config.ExeFile = $@"{Config.WorkingDirectory}\{folderName}\bedrock_server.exe";
62+
Config.Preferences.PdbFile = $@"{Config.Preferences.WorkingDirectory}\{folderName}\bedrock_server.pdb";
63+
Config.Preferences.ExeFile = $@"{Config.Preferences.WorkingDirectory}\{folderName}\bedrock_server.exe";
6864

6965
ConfigParser.SaveConfig();
7066
}
67+
catch (Exception)
68+
{
69+
Console.WriteLine("Failed to get version.");
70+
}
71+
}
72+
else
73+
{
74+
Console.Write("Enter a folder name (Must be in the working directory):");
75+
var folderName = Console.ReadLine();
7176

72-
Console.Clear();
73-
Misc.DrawASCII();
77+
Config.Preferences.PdbFile = $@"{Config.Preferences.WorkingDirectory}\{folderName}\bedrock_server.pdb";
78+
Config.Preferences.ExeFile = $@"{Config.Preferences.WorkingDirectory}\{folderName}\bedrock_server.exe";
7479

75-
PublicSymbolsDict = LLVM.DumpPublics();
76-
Sections = LLVM.DumpSections();
80+
ConfigParser.SaveConfig();
81+
}
7782

78-
var offsets = Update.UpdateFromConfig(Config);
83+
Console.Clear();
84+
Misc.DrawASCII();
7985

86+
PublicSymbolsDict = LLVM.DumpPublics();
87+
Sections = LLVM.DumpSections();
8088

81-
Console.Clear();
82-
Misc.DrawASCII();
89+
var offsets = Update.UpdateFromConfig(Config);
8390

84-
Console.WriteLine("==========================================");
8591

86-
Console.WriteLine("Offsets:");
87-
foreach (var offset in offsets)
88-
{
89-
Console.WriteLine($"{offset.Key}: 0x{offset.Value:X}");
90-
}
92+
Console.Clear();
93+
Misc.DrawASCII();
9194

92-
Console.WriteLine("==========================================");
95+
Console.WriteLine("==========================================");
9396

94-
// Export to file
95-
Console.WriteLine("Exporting to file...");
96-
97-
if (Config.OutputFile != null)
98-
await File.WriteAllTextAsync(Config.OutputFile,
99-
string.Join("\n", offsets.Select(x => $"{x.Key}=0x{x.Value:X}")));
97+
Console.WriteLine("Offsets:");
98+
foreach (var offset in offsets)
99+
{
100+
Console.WriteLine($"{offset.Key}: 0x{offset.Value:X}");
101+
}
100102

101-
Console.WriteLine("Done. Press enter to start again.");
103+
Console.WriteLine("==========================================");
102104

103-
Console.ReadLine();
104-
Console.Clear();
105-
}
105+
// Export to file
106+
Console.WriteLine("Exporting to file...");
107+
108+
if (Config.Preferences.OutputFile != null)
109+
await File.WriteAllTextAsync(Config.Preferences.OutputFile,
110+
string.Join("\n", offsets.Select(x => $"{x.Key}=0x{x.Value:X}")));
111+
112+
Console.WriteLine("Done. Press enter to start again.");
113+
114+
Console.ReadLine();
115+
Console.Clear();
106116
}
107117
}

0 commit comments

Comments
 (0)