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

Commit 3b64e46

Browse files
committed
Automatic .json default program setting
Checks if json is in buildplate format
1 parent c97ce88 commit 3b64e46

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

Program.cs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ class Program
1717
public static Window Window;
1818
public static string baseDir;
1919

20-
// todo: check json is buildplate
2120
static void Main(string[] args)
2221
{
2322
// Get base path (.exe location)
2423
string myExecutable = Assembly.GetEntryAssembly().Location;
2524

2625
if (args != null && args.Length > 0 && args[0] == "setJsonDefault") {
2726
Util.SetAssociationWithExtension(".json", "Json", myExecutable, "BuildPlate");
27+
Util.SetAssociationWithExtension(".plate", "Plate", myExecutable, "BuildPlate");
28+
Util.SetAssociationWithExtension(".plate64", "Plate64", myExecutable, "BuildPlate");
2829
Console.WriteLine("Set .json as default, To Apply: Select .json file, click \"Open with\", \"Choose another app\", " +
2930
"Select BuildPlate_Editor, Check \"Always use this app...\"");
31+
Console.WriteLine("You can also do this for .plate and .plate64");
3032
Console.WriteLine("Press any key to continue...");
3133
Console.ReadKey(true);
3234
return;
@@ -91,20 +93,41 @@ static void Main(string[] args)
9193
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\00d1fa99-7acf-449d-bb4f-8d11127bd6e3.json";
9294
#else
9395
Console.Write("Build plate to edit (.json): ");
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;
96+
// json - api, plate - data only, plate64 - base64 encoded data
97+
if (args != null && args.Length > 0 && File.Exists(string.Join(" ", args)) && string.Join(" ", args).Split('.').Length > 1) {
98+
string truePath = new FileInfo(string.Join(" ", args)).FullName;
99+
Console.WriteLine(truePath);
100+
string extension = truePath.Split('.').Last();
101+
if (extension == "json" || extension == "plate" || extension == "plate64") {
102+
World.targetFilePath = truePath;
103+
goto check;
104104
}
105-
World.targetFilePath = buildPlate;
106105
}
106+
107+
string buildPlate = Console.ReadLine();
108+
if (!File.Exists(buildPlate)) {
109+
Console.WriteLine($"build plate \"{buildPlate}\" doesn't exist");
110+
Console.ReadKey(true);
111+
return;
112+
} else if (Path.GetExtension(buildPlate) == ".json" || Path.GetExtension(buildPlate) == ".plate"
113+
|| Path.GetExtension(buildPlate) == ".plate64") {
114+
Console.WriteLine($"\"{Path.GetExtension(buildPlate)}\" isn't valid buildplate extension, valid: .json, .plate, .plate64");
115+
Console.ReadKey(true);
116+
return;
117+
}
118+
World.targetFilePath = buildPlate;
107119
#endif
120+
check:
121+
// check json is buildplate
122+
try {
123+
BuildPlate.Load(World.targetFilePath);
124+
} catch {
125+
Console.WriteLine($"Couldn't parse \"{World.targetFilePath}\", Make sure it's valid build plate file.");
126+
Console.WriteLine("Press any key to exit...");
127+
Console.ReadKey(true);
128+
return;
129+
}
130+
108131
Window = new Window();
109132
string version;
110133
try { version = OpenGLHelper.GetVersion(); } catch { version = "Failed to get version"; }

Util.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,17 @@ public static T[] Cloned<T>(this T[] array)
145145
// needs admin
146146
public static void SetAssociationWithExtension(string Extension, string key, string OpenWith, string FileDescription)
147147
{
148-
/*Key: HKLM\SOFTWARE\Classes\.foo
149-
Value: <default> = “Foo.Document”
150-
151-
Key: HKLM\SOFTWARE\Classes\Foo.Document
152-
Value: <default> = “Foo Document”
153-
154-
Key: HKLM\SOFTWARE\Classes\Foo.Document\shell\open\command
155-
Value: <default> = “[blah.exe]” “%1″*/
156148
RegistryKey key1 = Registry.LocalMachine.CreateSubKey($@"SOFTWARE\Classes\{Extension}");
157149
key1.SetValue("", $"{Extension.Replace(".", "")}.Document");
158150
key1.Flush();
159151
RegistryKey key2 = Registry.LocalMachine.CreateSubKey($@"SOFTWARE\Classes\{key}.Document");
160152
key2.SetValue("", FileDescription);
161153
key2.Flush();
162154
RegistryKey key3 = Registry.LocalMachine.CreateSubKey($@"SOFTWARE\Classes\{key}.Document\Shell\Open\Command");
163-
key3.SetValue("", $"{OpenWith} %1");
155+
key3.SetValue("", $"\"{OpenWith}\" %1");
164156
key3.Flush();
165157
RegistryKey key4 = Registry.LocalMachine.CreateSubKey($@"SOFTWARE\Classes\{key}.Document\Shell\Edit\Command");
166-
key4.SetValue("", $"{OpenWith} %1");
158+
key4.SetValue("", $"\"{OpenWith}\" %1");
167159
key4.Flush();
168160
}
169161
}

0 commit comments

Comments
 (0)