| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- using System.Security.Cryptography;
- using System.Windows.Forms;
- namespace MesClientLauncher
- {
- static class Program
- {
- private const string ResourceName = "EmbeddedJar.jar";
- private const string JarFileName = "mesclient-sd.jar";
- [STAThread]
- static void Main()
- {
- try
- {
- string exeDir = AppDomain.CurrentDomain.BaseDirectory;
- string cacheDir = Path.Combine(
- Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
- "mesclient-banshou");
- Directory.CreateDirectory(cacheDir);
- string jarPath = Path.Combine(cacheDir, JarFileName);
- ExtractEmbeddedJarIfNeeded(jarPath);
- string javaw = FindJavaw();
- if (string.IsNullOrEmpty(javaw))
- {
- MessageBox.Show("未找到 Java 8 (javaw.exe)。\n请安装 JDK/JRE 1.8,或设置 JAVA_HOME。",
- "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- ProcessStartInfo psi = new ProcessStartInfo();
- psi.FileName = javaw;
- psi.Arguments = "-Dfile.encoding=UTF-8 -jar \"" + jarPath + "\"";
- psi.WorkingDirectory = exeDir;
- psi.UseShellExecute = false;
- Process.Start(psi);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString(), "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- static void ExtractEmbeddedJarIfNeeded(string jarPath)
- {
- Assembly asm = Assembly.GetExecutingAssembly();
- using (Stream src = asm.GetManifestResourceStream(ResourceName))
- {
- if (src == null)
- {
- throw new InvalidOperationException("EXE 内未嵌入 jar 资源: " + ResourceName);
- }
- bool needWrite = true;
- if (File.Exists(jarPath))
- {
- FileInfo fi = new FileInfo(jarPath);
- if (fi.Length == src.Length)
- {
- string diskHash = HashFile(jarPath);
- string embHash = HashStream(src);
- src.Position = 0;
- needWrite = !string.Equals(diskHash, embHash, StringComparison.OrdinalIgnoreCase);
- }
- }
- if (!needWrite) return;
- string tmp = jarPath + ".tmp";
- using (FileStream fs = new FileStream(tmp, FileMode.Create, FileAccess.Write))
- {
- byte[] buffer = new byte[1024 * 256];
- int read;
- while ((read = src.Read(buffer, 0, buffer.Length)) > 0)
- {
- fs.Write(buffer, 0, read);
- }
- }
- if (File.Exists(jarPath)) File.Delete(jarPath);
- File.Move(tmp, jarPath);
- }
- }
- static string HashFile(string path)
- {
- using (FileStream fs = File.OpenRead(path))
- {
- return HashStream(fs);
- }
- }
- static string HashStream(Stream stream)
- {
- using (SHA1 sha = SHA1.Create())
- {
- byte[] hash = sha.ComputeHash(stream);
- return BitConverter.ToString(hash).Replace("-", "");
- }
- }
- static string FindJavaw()
- {
- string[] candidates = new string[]
- {
- Environment.GetEnvironmentVariable("JAVA_HOME"),
- @"C:\Program Files\Java\jdk1.8.0_503",
- @"C:\Program Files\Java\jre1.8.0_503"
- };
- foreach (string home in candidates)
- {
- if (string.IsNullOrEmpty(home)) continue;
- string p1 = Path.Combine(home, "bin", "javaw.exe");
- if (File.Exists(p1)) return p1;
- string p2 = Path.Combine(home, "jre", "bin", "javaw.exe");
- if (File.Exists(p2)) return p2;
- }
- string javaRoot = @"C:\Program Files\Java";
- if (Directory.Exists(javaRoot))
- {
- string[] dirs = Directory.GetDirectories(javaRoot);
- Array.Sort(dirs);
- Array.Reverse(dirs);
- foreach (string dir in dirs)
- {
- string name = Path.GetFileName(dir).ToLowerInvariant();
- if (!(name.StartsWith("jdk1.8") || name.StartsWith("jre1.8"))) continue;
- string p = Path.Combine(dir, "bin", "javaw.exe");
- if (File.Exists(p)) return p;
- }
- }
- try
- {
- ProcessStartInfo wherePsi = new ProcessStartInfo();
- wherePsi.FileName = "where.exe";
- wherePsi.Arguments = "javaw";
- wherePsi.RedirectStandardOutput = true;
- wherePsi.UseShellExecute = false;
- wherePsi.CreateNoWindow = true;
- using (Process p = Process.Start(wherePsi))
- {
- string output = p.StandardOutput.ReadToEnd();
- p.WaitForExit();
- foreach (string line in output.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
- {
- if (File.Exists(line.Trim())) return line.Trim();
- }
- }
- }
- catch { }
- return null;
- }
- }
- }
|