wangxichen 4 дней назад
Родитель
Сommit
783abe866b
1 измененных файлов с 9 добавлено и 2 удалено
  1. 9 2
      src/com/mes/util/ClientRuntimeAgent.java

+ 9 - 2
src/com/mes/util/ClientRuntimeAgent.java

@@ -144,7 +144,7 @@ public final class ClientRuntimeAgent {
         File download = new File(jar.getParentFile(), jar.getName() + ".download");
         download(data.getString("path"), download);
         if (download.length() == 0) throw new IOException("empty download");
-        Files.write(Paths.get("config", "client-version.txt"), String.valueOf(target).getBytes(StandardCharsets.UTF_8));
+        // 版本号不在这里写,改由 VBScript 在移动文件成功后写入,避免 VBS 失败时版本号已变
 
         File vbs = new File(jar.getParentFile(), "client-update-" + System.currentTimeMillis() + ".vbs");
         String jarPath = jar.getAbsolutePath();
@@ -152,13 +152,20 @@ public final class ClientRuntimeAgent {
         String downloadPath = download.getAbsolutePath();
         String libGlob = new File(jar.getParentFile(), "lib").getAbsolutePath() + File.separator + "*";
         String workDir = jar.getParentFile().getAbsolutePath();
+        String verFile = new File(new File(workDir, "config"), "client-version.txt").getAbsolutePath();
         try (Writer w = new OutputStreamWriter(new FileOutputStream(vbs), StandardCharsets.UTF_8)) {
             w.write(
                 "Set sh = CreateObject(\"WScript.Shell\")\r\n" +
                 "Set fso = CreateObject(\"Scripting.FileSystemObject\")\r\n" +
                 "WScript.Sleep 2000\r\n" +
-                "If fso.FileExists(\"" + jarPath + "\") Then fso.CopyFile \"" + jarPath + "\", \"" + bakPath + "\", True\r\n" +
+                "If fso.FileExists(\"" + jarPath + "\") Then\r\n" +
+                "  If fso.FileExists(\"" + bakPath + "\") Then fso.DeleteFile \"" + bakPath + "\"\r\n" +
+                "  fso.MoveFile \"" + jarPath + "\", \"" + bakPath + "\"\r\n" +
+                "End If\r\n" +
                 "fso.MoveFile \"" + downloadPath + "\", \"" + jarPath + "\"\r\n" +
+                "Set f = fso.CreateTextFile(\"" + verFile + "\", True)\r\n" +
+                "f.Write \"" + target + "\"\r\n" +
+                "f.Close\r\n" +
                 "sh.CurrentDirectory = \"" + workDir + "\"\r\n" +
                 "sh.Run \"javaw -cp \"\"" + jarPath + ";" + libGlob + "\"\" " + mainClass + "\", 0, False\r\n" +
                 "fso.DeleteFile WScript.ScriptFullName\r\n"