|
1 | | -using Microsoft.Win32; |
2 | | -using System; |
3 | | -using System.Collections.Generic; |
4 | | -using System.Diagnostics; |
5 | | -using System.IO; |
6 | | - |
7 | | -namespace mscript { |
8 | | - class Program { |
9 | | - static void Main(string[] args) { |
10 | | - try { |
11 | | - if (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun) { |
12 | | - Console.WriteLine("Finishing installation..."); |
13 | | - SetAddRemoveProgramsIcon(); |
14 | | - Console.WriteLine("Installation complete. Press enter to exit."); |
15 | | - Console.ReadLine(); |
16 | | - return; |
17 | | - } |
18 | | - } catch(System.Deployment.Application.InvalidDeploymentException e) { |
19 | | - // Being run directly from the executable, so this is not the first installed run anyways. |
20 | | - } |
21 | | - |
22 | | - string[] activationData = null; |
23 | | - if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null) { |
24 | | - activationData = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData; |
25 | | - } |
26 | | - string startFile = null; |
27 | | - if (activationData != null) { |
28 | | - // We were launched directly if this is null, otherwise |
29 | | - // activationData[0] has the name of the file we were launched from |
30 | | - startFile = activationData[0]; |
31 | | - } |
32 | | - |
33 | | - string jarLocation = Registry.CurrentUser.OpenSubKey("Software\\MethodScript").GetValue("JarLocation").ToString(); |
34 | | - |
35 | | - List<string> command = new List<string>(); |
36 | | - if (startFile == null) { |
37 | | - // start interpreter |
38 | | - command.Add("-jar"); |
39 | | - command.Add(jarLocation); |
40 | | - command.Add("interpreter"); |
41 | | - // Don't think these are needed here |
42 | | - //command.Add("--location-----"); |
43 | | - //command.Add(AppDomain.CurrentDomain.BaseDirectory); |
44 | | - } else { |
45 | | - // launched from a *.ms file |
46 | | - if(args.Length > 0 && args[0] == "--") { |
47 | | - // Script passthrough to java -jar CH.jar <arguments> |
48 | | - command.Add("-jar"); |
49 | | - command.Add(jarLocation); |
50 | | - command.AddRange(args); |
51 | | - } else if (System.Environment.GetEnvironmentVariable("DEBUG_MSCRIPT") != null && System.Environment.GetEnvironmentVariable("DEBUG_MSCRIPT") == "1") { |
52 | | - // java debug mode |
53 | | - command.Add("-Xrs"); |
54 | | - command.Add("-Xdebug"); |
55 | | - command.Add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9001"); |
56 | | - command.Add("-jar"); |
57 | | - command.Add(jarLocation); |
58 | | - command.Add("cmdline"); |
59 | | - command.Add(startFile); |
60 | | - command.AddRange(args); |
61 | | - } else { |
62 | | - // Normal launch with script |
63 | | - command.Add("-Xrs"); |
64 | | - command.Add("-jar"); |
65 | | - command.Add(jarLocation); |
66 | | - command.Add("cmdline"); |
67 | | - command.Add(startFile); |
68 | | - command.AddRange(args); |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - |
73 | | - ProcessStartInfo start = new ProcessStartInfo(); |
74 | | - start.Arguments = JoinAndEscape(command); |
75 | | - start.FileName = "java.exe"; |
76 | | - start.UseShellExecute = false; |
77 | | - |
78 | | - //Console.WriteLine(start.Arguments); |
79 | | - |
80 | | - int exitCode; |
81 | | - |
82 | | - using (Process proc = Process.Start(start)) { |
83 | | - proc.WaitForExit(); |
84 | | - exitCode = proc.ExitCode; |
85 | | - } |
86 | | - |
87 | | - //Console.WriteLine("command: " + string.Join(" ", command)); |
88 | | - //Console.ReadLine(); |
89 | | - return; |
90 | | - } |
91 | | - |
92 | | - private static string JoinAndEscape(List<string> args) { |
93 | | - string s = ""; |
94 | | - foreach(string arg in args) { |
95 | | - string arg2 = arg.Replace("\\", "\\\\"); |
96 | | - arg2 = arg2.Replace("\"", "\\\""); |
97 | | - arg2 = "\"" + arg2 + "\""; |
98 | | - s += arg2; |
99 | | - s += " "; |
100 | | - } |
101 | | - return s; |
102 | | - } |
103 | | - |
104 | | - private static void SetAddRemoveProgramsIcon() { |
105 | | - try { |
106 | | - string assemblyDescription = "MethodScript"; |
107 | | - |
108 | | - //the icon is included in this program |
109 | | - string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "commandhelper_icon.ico"); |
110 | | - //Console.WriteLine("iconSourcePath: " + iconSourcePath); |
111 | | - //Console.WriteLine("assemblyDescription: " + assemblyDescription); |
112 | | - |
113 | | - if (!File.Exists(iconSourcePath)) |
114 | | - return; |
115 | | - |
116 | | - RegistryKey myUninstallKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"); |
117 | | - string[] mySubKeyNames = myUninstallKey.GetSubKeyNames(); |
118 | | - for (int i = 0; i < mySubKeyNames.Length; i++) { |
119 | | - RegistryKey myKey = myUninstallKey.OpenSubKey(mySubKeyNames[i], true); |
120 | | - object myValue = myKey.GetValue("DisplayName"); |
121 | | - if (myValue != null && myValue.ToString() == assemblyDescription) { |
122 | | - //Console.WriteLine("Setting iconSourcePath in " + myKey.Name); |
123 | | - myKey.SetValue("DisplayIcon", iconSourcePath); |
124 | | - break; |
125 | | - } |
126 | | - } |
127 | | - } catch (Exception ex) { |
128 | | - //log an error |
129 | | - } |
130 | | - } |
131 | | - } |
132 | | -} |
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | + |
| 7 | +namespace mscript { |
| 8 | + class Program { |
| 9 | + static void Main(string[] args) { |
| 10 | + try { |
| 11 | + if (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun) { |
| 12 | + Console.WriteLine("Finishing installation..."); |
| 13 | + SetAddRemoveProgramsIcon(); |
| 14 | + Console.WriteLine("Installation complete. Press enter to exit."); |
| 15 | + Console.ReadLine(); |
| 16 | + return; |
| 17 | + } |
| 18 | + } catch(System.Deployment.Application.InvalidDeploymentException e) { |
| 19 | + // Being run directly from the executable, so this is not the first installed run anyways. |
| 20 | + } |
| 21 | + |
| 22 | + string[] activationData = null; |
| 23 | + if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null) { |
| 24 | + activationData = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData; |
| 25 | + } |
| 26 | + string startFile = null; |
| 27 | + if (activationData != null) { |
| 28 | + // We were launched directly if this is null, otherwise |
| 29 | + // activationData[0] has the name of the file we were launched from |
| 30 | + startFile = activationData[0]; |
| 31 | + } |
| 32 | + |
| 33 | + string jarLocation = Registry.CurrentUser.OpenSubKey("Software\\MethodScript").GetValue("JarLocation").ToString(); |
| 34 | + |
| 35 | + List<string> command = new List<string>(); |
| 36 | + if (startFile == null) { |
| 37 | + // start interpreter |
| 38 | + command.Add("-jar"); |
| 39 | + command.Add(jarLocation); |
| 40 | + command.Add("interpreter"); |
| 41 | + // Don't think these are needed here |
| 42 | + //command.Add("--location-----"); |
| 43 | + //command.Add(AppDomain.CurrentDomain.BaseDirectory); |
| 44 | + } else { |
| 45 | + // launched from a *.ms file |
| 46 | + if(args.Length > 0 && args[0] == "--") { |
| 47 | + // Script passthrough to java -jar CH.jar <arguments> |
| 48 | + command.Add("-jar"); |
| 49 | + command.Add(jarLocation); |
| 50 | + command.AddRange(args); |
| 51 | + } else if (System.Environment.GetEnvironmentVariable("DEBUG_MSCRIPT") != null && System.Environment.GetEnvironmentVariable("DEBUG_MSCRIPT") == "1") { |
| 52 | + // java debug mode |
| 53 | + command.Add("-Xrs"); |
| 54 | + command.Add("-Xdebug"); |
| 55 | + command.Add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9001"); |
| 56 | + command.Add("-jar"); |
| 57 | + command.Add(jarLocation); |
| 58 | + command.Add("cmdline"); |
| 59 | + command.Add(startFile); |
| 60 | + command.AddRange(args); |
| 61 | + } else { |
| 62 | + // Normal launch with script |
| 63 | + command.Add("-Xrs"); |
| 64 | + command.Add("-jar"); |
| 65 | + command.Add(jarLocation); |
| 66 | + command.Add("cmdline"); |
| 67 | + command.Add(startFile); |
| 68 | + command.AddRange(args); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + ProcessStartInfo start = new ProcessStartInfo(); |
| 74 | + start.Arguments = JoinAndEscape(command); |
| 75 | + start.FileName = "java.exe"; |
| 76 | + start.UseShellExecute = false; |
| 77 | + |
| 78 | + //Console.WriteLine(start.Arguments); |
| 79 | + |
| 80 | + int exitCode; |
| 81 | + |
| 82 | + using (Process proc = Process.Start(start)) { |
| 83 | + proc.WaitForExit(); |
| 84 | + exitCode = proc.ExitCode; |
| 85 | + } |
| 86 | + |
| 87 | + //Console.WriteLine("command: " + string.Join(" ", command)); |
| 88 | + //Console.ReadLine(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + private static string JoinAndEscape(List<string> args) { |
| 93 | + string s = ""; |
| 94 | + foreach(string arg in args) { |
| 95 | + string arg2 = arg.Replace("\\", "\\\\"); |
| 96 | + arg2 = arg2.Replace("\"", "\\\""); |
| 97 | + arg2 = "\"" + arg2 + "\""; |
| 98 | + s += arg2; |
| 99 | + s += " "; |
| 100 | + } |
| 101 | + return s; |
| 102 | + } |
| 103 | + |
| 104 | + private static void SetAddRemoveProgramsIcon() { |
| 105 | + try { |
| 106 | + string assemblyDescription = "MethodScript"; |
| 107 | + |
| 108 | + //the icon is included in this program |
| 109 | + string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "commandhelper_icon.ico"); |
| 110 | + //Console.WriteLine("iconSourcePath: " + iconSourcePath); |
| 111 | + //Console.WriteLine("assemblyDescription: " + assemblyDescription); |
| 112 | + |
| 113 | + if (!File.Exists(iconSourcePath)) |
| 114 | + return; |
| 115 | + |
| 116 | + RegistryKey myUninstallKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"); |
| 117 | + string[] mySubKeyNames = myUninstallKey.GetSubKeyNames(); |
| 118 | + for (int i = 0; i < mySubKeyNames.Length; i++) { |
| 119 | + RegistryKey myKey = myUninstallKey.OpenSubKey(mySubKeyNames[i], true); |
| 120 | + object myValue = myKey.GetValue("DisplayName"); |
| 121 | + if (myValue != null && myValue.ToString() == assemblyDescription) { |
| 122 | + //Console.WriteLine("Setting iconSourcePath in " + myKey.Name); |
| 123 | + myKey.SetValue("DisplayIcon", iconSourcePath); |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + } catch (Exception ex) { |
| 128 | + //log an error |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments