MesClientLauncher.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Security.Cryptography;
  6. using System.Windows.Forms;
  7. namespace MesClientLauncher
  8. {
  9. static class Program
  10. {
  11. private const string ResourceName = "EmbeddedJar.jar";
  12. private const string JarFileName = "mesclient-sd.jar";
  13. [STAThread]
  14. static void Main()
  15. {
  16. try
  17. {
  18. string exeDir = AppDomain.CurrentDomain.BaseDirectory;
  19. string cacheDir = Path.Combine(
  20. Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
  21. "mesclient-banshou");
  22. Directory.CreateDirectory(cacheDir);
  23. string jarPath = Path.Combine(cacheDir, JarFileName);
  24. ExtractEmbeddedJarIfNeeded(jarPath);
  25. string javaw = FindJavaw();
  26. if (string.IsNullOrEmpty(javaw))
  27. {
  28. MessageBox.Show("未找到 Java 8 (javaw.exe)。\n请安装 JDK/JRE 1.8,或设置 JAVA_HOME。",
  29. "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  30. return;
  31. }
  32. ProcessStartInfo psi = new ProcessStartInfo();
  33. psi.FileName = javaw;
  34. psi.Arguments = "-Dfile.encoding=UTF-8 -jar \"" + jarPath + "\"";
  35. psi.WorkingDirectory = exeDir;
  36. psi.UseShellExecute = false;
  37. Process.Start(psi);
  38. }
  39. catch (Exception ex)
  40. {
  41. MessageBox.Show(ex.ToString(), "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  42. }
  43. }
  44. static void ExtractEmbeddedJarIfNeeded(string jarPath)
  45. {
  46. Assembly asm = Assembly.GetExecutingAssembly();
  47. using (Stream src = asm.GetManifestResourceStream(ResourceName))
  48. {
  49. if (src == null)
  50. {
  51. throw new InvalidOperationException("EXE 内未嵌入 jar 资源: " + ResourceName);
  52. }
  53. bool needWrite = true;
  54. if (File.Exists(jarPath))
  55. {
  56. FileInfo fi = new FileInfo(jarPath);
  57. if (fi.Length == src.Length)
  58. {
  59. string diskHash = HashFile(jarPath);
  60. string embHash = HashStream(src);
  61. src.Position = 0;
  62. needWrite = !string.Equals(diskHash, embHash, StringComparison.OrdinalIgnoreCase);
  63. }
  64. }
  65. if (!needWrite) return;
  66. string tmp = jarPath + ".tmp";
  67. using (FileStream fs = new FileStream(tmp, FileMode.Create, FileAccess.Write))
  68. {
  69. byte[] buffer = new byte[1024 * 256];
  70. int read;
  71. while ((read = src.Read(buffer, 0, buffer.Length)) > 0)
  72. {
  73. fs.Write(buffer, 0, read);
  74. }
  75. }
  76. if (File.Exists(jarPath)) File.Delete(jarPath);
  77. File.Move(tmp, jarPath);
  78. }
  79. }
  80. static string HashFile(string path)
  81. {
  82. using (FileStream fs = File.OpenRead(path))
  83. {
  84. return HashStream(fs);
  85. }
  86. }
  87. static string HashStream(Stream stream)
  88. {
  89. using (SHA1 sha = SHA1.Create())
  90. {
  91. byte[] hash = sha.ComputeHash(stream);
  92. return BitConverter.ToString(hash).Replace("-", "");
  93. }
  94. }
  95. static string FindJavaw()
  96. {
  97. string[] candidates = new string[]
  98. {
  99. Environment.GetEnvironmentVariable("JAVA_HOME"),
  100. @"C:\Program Files\Java\jdk1.8.0_503",
  101. @"C:\Program Files\Java\jre1.8.0_503"
  102. };
  103. foreach (string home in candidates)
  104. {
  105. if (string.IsNullOrEmpty(home)) continue;
  106. string p1 = Path.Combine(home, "bin", "javaw.exe");
  107. if (File.Exists(p1)) return p1;
  108. string p2 = Path.Combine(home, "jre", "bin", "javaw.exe");
  109. if (File.Exists(p2)) return p2;
  110. }
  111. string javaRoot = @"C:\Program Files\Java";
  112. if (Directory.Exists(javaRoot))
  113. {
  114. string[] dirs = Directory.GetDirectories(javaRoot);
  115. Array.Sort(dirs);
  116. Array.Reverse(dirs);
  117. foreach (string dir in dirs)
  118. {
  119. string name = Path.GetFileName(dir).ToLowerInvariant();
  120. if (!(name.StartsWith("jdk1.8") || name.StartsWith("jre1.8"))) continue;
  121. string p = Path.Combine(dir, "bin", "javaw.exe");
  122. if (File.Exists(p)) return p;
  123. }
  124. }
  125. try
  126. {
  127. ProcessStartInfo wherePsi = new ProcessStartInfo();
  128. wherePsi.FileName = "where.exe";
  129. wherePsi.Arguments = "javaw";
  130. wherePsi.RedirectStandardOutput = true;
  131. wherePsi.UseShellExecute = false;
  132. wherePsi.CreateNoWindow = true;
  133. using (Process p = Process.Start(wherePsi))
  134. {
  135. string output = p.StandardOutput.ReadToEnd();
  136. p.WaitForExit();
  137. foreach (string line in output.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
  138. {
  139. if (File.Exists(line.Trim())) return line.Trim();
  140. }
  141. }
  142. }
  143. catch { }
  144. return null;
  145. }
  146. }
  147. }