Skip to content

Commit

Permalink
Add API version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
LunNova committed Feb 7, 2016
1 parent ab77443 commit 5fa3c49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/main/java/me/nallar/modpatcher/ModPatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* This behaviour can be disabled by creating the file "libs/modpatcher/NEVER_UPDATE.txt"
*/
public class ModPatcher {
private static final int API_VERSION = 0;
private static final Logger log = LogManager.getLogger("ModPatcher");
private static final String mcVersion = "@MC_VERSION@";
private static final Path neverUpdatePath = Paths.get("./libs/ModPatcher/NEVER_UPDATE.txt").toAbsolutePath();
Expand Down Expand Up @@ -194,11 +195,9 @@ private static void addToCurrentClassLoader() {
} catch (Exception e) {
throw new Error(e);
}

ModPatcherLoadHook.loadHook(requiredVersion, getModPatcherRelease());
}

private static boolean neverUpdate() {
static boolean neverUpdate() {
return "true".equals(System.getProperty("modPatcher.neverUpdate")) || Files.exists(neverUpdatePath);
}

Expand Down Expand Up @@ -227,9 +226,16 @@ private static void download() {
}

private static void checkClassLoading() {
checkClassLoading(true);
}

private static void checkClassLoading(boolean load) {
try {
ModPatcherLoadHook.class.getName();
ModPatcherLoadHook.loadHook(requiredVersion, getModPatcherRelease(), API_VERSION);
} catch (NoClassDefFoundError e) {
if (!load)
throw e;

loadModPatcher();
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/me/nallar/modpatcher/ModPatcherLoadHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
import me.nallar.javatransformer.api.JavaTransformer;

class ModPatcherLoadHook {
private static final int API_VERSION = 0; //Keep in sync with version in ModPatcher.java
private static final String VERSION = "@VERSION@".replace("-SNAPSHOT", "");

static void loadHook(ModPatcher.Version requiredVersion, String modPatcherRelease) {
static void loadHook(ModPatcher.Version requiredVersion, String modPatcherRelease, int apiVersion) {
if (ModPatcherLoadHook.class.getClassLoader().getClass().getName().contains("LaunchClassLoader")) {
throw new Error("ModPatcher should not be loaded under LaunchClassLoader");
}

if (API_VERSION != apiVersion) {
PatcherLog.warn("API version mismatch. Expected " + API_VERSION + ", got " + apiVersion);
PatcherLog.warn("API was loaded from: " + JavaTransformer.pathFromClass(ModPatcher.class));
}

ModPatcher.Version current = ModPatcher.Version.of(VERSION);

if (current.compareTo(requiredVersion) < 0) {
JavaTransformer.pathFromClass(ModPatcherTransformer.class).toFile().deleteOnExit();
throw new RuntimeException("ModPatcher outdated. Have version: " + VERSION + ", requested version: " + requiredVersion + "\nWill auto-update on next start.");
String autoUpdate = "\nWill auto-update on next start.";

if (ModPatcher.neverUpdate())
autoUpdate = "";
else
JavaTransformer.pathFromClass(ModPatcherTransformer.class).toFile().deleteOnExit();

throw new RuntimeException("ModPatcher outdated. Have version: " + VERSION + ", requested version: " + requiredVersion + autoUpdate);
}
}
}

0 comments on commit 5fa3c49

Please sign in to comment.