Fix FileNotFoundException *.hmc when launching.

This commit is contained in:
huanghongxun
2015-06-26 18:55:36 +08:00
parent bee81fd6d9
commit 0df7479e64
12 changed files with 175 additions and 65 deletions

View File

@@ -90,7 +90,7 @@ task proguard(type: proguard.gradle.ProGuardTask, dependsOn: jar) {
configuration 'proguard.pro'
}
/*
task makeExecutable(dependsOn: jar) {
ext {
def re = jar.classifier
@@ -111,7 +111,7 @@ task makeExecutable(dependsOn: jar) {
fos.write(bytes, 0, read);
is.close()
fos.close()
}
}*/
launch4j {
launch4jCmd = 'D:\\Develop\\Java\\Launch4j\\launch4j.exe'
@@ -135,5 +135,5 @@ processResources {
}
}
//build.dependsOn proguard
build.dependsOn makeExecutable
build.dependsOn proguard
//build.dependsOn makeExecutable

View File

@@ -36,6 +36,7 @@ import org.jackhuang.hellominecraft.utils.JdkVersion;
import org.jackhuang.hellominecraft.utils.LauncherPrintStream;
import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.OS;
import org.jackhuang.hellominecraft.utils.Platform;
import org.jackhuang.hellominecraft.utils.Utils;
/**
@@ -105,7 +106,7 @@ public final class Launcher {
return;
}
if (!JdkVersion.isJava64Bit() && OS.is64Bit())
if (!JdkVersion.isJava64Bit() && OS.getPlatform() == Platform.BIT_32)
MessageBox.Show(C.i18n("advice.os64butjdk32"));
Method minecraftMain;

View File

@@ -26,11 +26,12 @@ import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.launcher.Launcher;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider;
import org.jackhuang.hellominecraft.launcher.utils.settings.Profile;
import org.jackhuang.hellominecraft.utils.FileUtils;
import org.jackhuang.hellominecraft.launcher.utils.settings.Settings;
import org.jackhuang.hellominecraft.utils.JdkVersion;
import org.jackhuang.hellominecraft.utils.MathUtils;
import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.OS;
import org.jackhuang.hellominecraft.utils.Platform;
import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.utils.Utils;
@@ -62,24 +63,16 @@ public abstract class IMinecraftLoader {
res.addAll(Arrays.asList(v.getWrapperLauncher().split(" ")));
String str = v.getJavaDir();
JdkVersion jv = null;
File f = new File(str + ".hmc");
try {
String s = FileUtils.readFileToString(f);
String[] strs = s.split("\n");
if (str.length() >= 2)
jv = new JdkVersion(strs[0], MathUtils.parseInt(strs[1], -1));
else
throw new IllegalStateException("The format of file: " + f + " is wrong: " + s);
} catch (IOException | IllegalStateException e) {
try {
jv = JdkVersion.getJavaVersionFromExecutable(str);
jv.write(f);
if (!f.exists())
HMCLog.warn("Failed to load version from file " + f, e);
} catch (Exception ex) {
HMCLog.warn("Failed to read JDKVersion.", ex);
}
JdkVersion jv = new JdkVersion(str);
if(Settings.getInstance().getJava().contains(jv))
jv = Settings.getInstance().getJava().get(Settings.getInstance().getJava().indexOf(jv));
else try {
jv = JdkVersion.getJavaVersionFromExecutable(str);
Settings.getInstance().getJava().add(jv);
Settings.save();
} catch (IOException ex) {
HMCLog.warn("Failed to get java version", ex);
jv = null;
}
res.add(str);
@@ -95,12 +88,12 @@ public abstract class IMinecraftLoader {
res.add("-Xmn128m");
}
if (jv != null && jv.is64Bit == 0 && OS.is64Bit())
if (jv != null && jv.platform == Platform.BIT_32 && OS.getPlatform() == Platform.BIT_64)
MessageBox.Show(C.i18n("advice.os64butjdk32"));
if (!StrUtils.isBlank(v.getMaxMemory())) {
int mem = MathUtils.parseMemory(v.getMaxMemory(), 2147483647);
if (jv != null && jv.is64Bit == 0 && mem > 1024)
if (jv != null && jv.platform == Platform.BIT_32 && mem > 1024)
MessageBox.Show(C.i18n("launch.too_big_memory_alloc_64bit"));
else {
long a = OS.getTotalPhysicalMemory() / 1024 / 1024;
@@ -114,10 +107,10 @@ public abstract class IMinecraftLoader {
}
if (!StrUtils.isBlank(v.getPermSize()) && !v.isNoJVMArgs())
if (jv != null && jv.ver != null && (jv.ver.startsWith("1.8") || jv.ver.startsWith("1.9"))); else res.add("-XX:MaxPermSize=" + v.getPermSize() + "m");
if (jv != null && jv.ver != null && (jv.ver.startsWith("1.8") || jv.ver.startsWith("1.9")));
else res.add("-XX:MaxPermSize=" + v.getPermSize() + "m");
if (!v.isNoJVMArgs())
appendJVMArgs(res);
if (!v.isNoJVMArgs()) appendJVMArgs(res);
HMCLog.log("On making java.library.path.");

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
*/
package org.jackhuang.hellominecraft.launcher.utils;
import java.io.File;
import java.io.IOException;
import org.jackhuang.hellominecraft.utils.FileUtils;
/**
*
* @author hyh
*/
public class BaseLauncherProfile {
public static String profile = "{\"selectedProfile\": \"(Default)\",\"profiles\": {\"(Default)\": {\"name\": \"(Default)\"}},\"clientToken\": \"88888888-8888-8888-8888-888888888888\"}";
public static void tryWriteProfile(File gameDir) throws IOException {
File file = new File(gameDir, "launcher_profiles.json");
if(!file.exists())
FileUtils.writeStringToFile(file, profile);
}
}

View File

@@ -18,9 +18,12 @@ package org.jackhuang.hellominecraft.launcher.utils.settings;
import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import org.jackhuang.hellominecraft.utils.JdkVersion;
import org.jackhuang.hellominecraft.utils.OS;
/**
@@ -41,6 +44,12 @@ public final class Config {
private boolean enableShadow;
@SerializedName("theme")
private int theme;
private List<JdkVersion> java;
public List<JdkVersion> getJava() {
return java == null ? java = new ArrayList<>() : java;
}
public int getTheme() {
return theme;

View File

@@ -16,6 +16,8 @@
*/
package org.jackhuang.hellominecraft.launcher.utils.settings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import java.io.File;
import java.io.IOException;
@@ -25,10 +27,12 @@ import java.util.Objects;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.launcher.Main;
import org.jackhuang.hellominecraft.utils.EnumAdapter;
import org.jackhuang.hellominecraft.utils.tinystream.CollectionUtils;
import org.jackhuang.hellominecraft.utils.FileUtils;
import org.jackhuang.hellominecraft.utils.IOUtils;
import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.Platform;
import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.utils.UpdateChecker;
import org.jackhuang.hellominecraft.utils.VersionNumber;
@@ -38,7 +42,9 @@ import org.jackhuang.hellominecraft.utils.VersionNumber;
* @author hyh
*/
public final class Settings {
public static final File settingsFile = new File(IOUtils.currentDir(), "hmcl.json");
public static final Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Platform.class, new EnumAdapter<>(Platform.values())).create();
private static boolean isFirstLoad;
private static final Config settings;
@@ -55,34 +61,34 @@ public final class Settings {
static {
settings = initSettings();
isFirstLoad = StrUtils.isBlank(settings.getUsername());
if(!getVersions().containsKey("Default"))
getVersions().put("Default", new Profile());
if (!getVersions().containsKey("Default"))
getVersions().put("Default", new Profile());
UPDATE_CHECKER = new UpdateChecker(new VersionNumber(Main.firstVer, Main.secondVer, Main.thirdVer),
"hmcl", settings.isCheckUpdate(), () -> Main.invokeUpdate());
}
private static Config initSettings() {
Config c = new Config();
if (settingsFile.exists()) {
if (settingsFile.exists())
try {
String str = FileUtils.readFileToString(settingsFile);
if (str == null || str.trim().equals("")) {
HMCLog.log("Settings file is empty, use the default settings.");
} else {
Config d = C.gsonPrettyPrinting.fromJson(str, Config.class);
if(d != null) c = d;
if (str == null || str.trim().equals(""))
HMCLog.log("Settings file is empty, use the default settings.");
else {
Config d = gson.fromJson(str, Config.class);
if (d != null) c = d;
}
HMCLog.log("Initialized settings.");
} catch (IOException | JsonSyntaxException e) {
HMCLog.warn("Something happened wrongly when load settings.", e);
HMCLog.warn("Something happened wrongly when load settings.", e);
if (MessageBox.Show(C.i18n("settings.failed_load"), MessageBox.YES_NO_OPTION) == MessageBox.NO_OPTION) {
HMCLog.err("Cancelled loading settings.");
HMCLog.err("Cancelled loading settings.");
System.exit(1);
}
}
} else {
HMCLog.log("No settings file here, may be first loading.");
else {
HMCLog.log("No settings file here, may be first loading.");
isFirstLoad = true;
}
return c;
@@ -90,7 +96,7 @@ public final class Settings {
public static void save() {
try {
FileUtils.write(settingsFile, C.gsonPrettyPrinting.toJson(settings));
FileUtils.write(settingsFile, gson.toJson(settings));
} catch (IOException ex) {
HMCLog.err("Failed to save config", ex);
}
@@ -99,20 +105,20 @@ public final class Settings {
public static Profile getVersion(String name) {
return getVersions().get(name);
}
public static Map<String, Profile> getVersions() {
return settings.getConfigurations();
return settings.getConfigurations();
}
public static void setVersion(Profile ver) {
Objects.requireNonNull(ver);
getVersions().put(ver.getName(), ver);
}
public static Collection<Profile> getProfiles() {
return CollectionUtils.sortOut(getVersions().values(), (t) -> t != null && t.getName() != null);
return CollectionUtils.sortOut(getVersions().values(), (t) -> t != null && t.getName() != null);
}
public static Profile getOneProfile() {
return settings.getConfigurations().firstEntry().getValue();
}

View File

@@ -32,7 +32,7 @@ import org.jackhuang.hellominecraft.launcher.launch.GameLauncher.DownloadLibrary
import org.jackhuang.hellominecraft.launcher.launch.IMinecraftLoader;
import org.jackhuang.hellominecraft.launcher.launch.IMinecraftProvider;
import org.jackhuang.hellominecraft.launcher.launch.MinecraftLoader;
import org.jackhuang.hellominecraft.utils.BaseLauncherProfile;
import org.jackhuang.hellominecraft.launcher.utils.BaseLauncherProfile;
import org.jackhuang.hellominecraft.utils.FileUtils;
import org.jackhuang.hellominecraft.launcher.utils.MCUtils;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider;