Skip to content

Commit 10f6f40

Browse files
committed
Fix FoxLoaderUpdater update + add drag & drop FoxLoader updating.
1 parent 2b39eff commit 10f6f40

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

loader/src/main/java/com/fox2code/foxloader/loader/ModLoaderInit.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.fox2code.foxloader.loader.java.JavaModInfo;
3434
import com.fox2code.foxloader.patching.PreLoader;
3535
import com.fox2code.foxloader.patching.mixin.MixinModLoader;
36+
import com.fox2code.foxloader.updater.FoxLoaderUpdater;
3637
import com.fox2code.foxloader.utils.Platform;
3738
import com.google.gson.Gson;
3839
import com.google.gson.GsonBuilder;
@@ -66,6 +67,7 @@ public final class ModLoaderInit {
6667
static final LinkedHashMap<String, ModContainer> modContainers = new LinkedHashMap<>();
6768
private static final Collection<ModContainer> modContainnersCollection =
6869
Collections.unmodifiableCollection(modContainers.values());
70+
private static boolean inPreBootup = true;
6971
static boolean isClientDevModeImpl = true;
7072

7173
static {
@@ -190,7 +192,7 @@ private static ArrayList<File> collectFilesToLoad() {
190192
}
191193

192194
private static Collection<LoadingPlugin> loadModContainersWithLoaders() throws Exception {
193-
if (!modContainers.isEmpty()) {
195+
if (!modContainers.isEmpty() || !inPreBootup) {
194196
throw new IllegalStateException("Mods container were already loaded.");
195197
}
196198
if (!mods.exists() && !mods.mkdirs()) {
@@ -227,6 +229,14 @@ private static Collection<LoadingPlugin> loadModContainersWithLoaders() throws E
227229
Iterator<File> fileIterator = files.iterator();
228230
while (fileIterator.hasNext()) {
229231
File file = fileIterator.next();
232+
if (FoxLoaderUpdater.updateFoxLoaderFromMod(file)) {
233+
if (file.exists() && !file.delete()) {
234+
file.deleteOnExit();
235+
}
236+
System.exit(0);
237+
// We should always be able to exit...
238+
throw new Error("System.exit(0); // Failed");
239+
}
230240
if (file.isFile() && file.length() > 0) {
231241
gatherJarInJarInfos(jarInJarInfos, file);
232242
}
@@ -277,6 +287,7 @@ private static Collection<LoadingPlugin> loadModContainersWithLoaders() throws E
277287
jarInJarIterator.remove();
278288
}
279289
}
290+
inPreBootup = false;
280291
// Construct loading plugins
281292
for (EarlyModRegistryInfo earlyModRegistryInfo : earlyModRegistryInfos.values()) {
282293
// Load dependencies bundles of potential loading plugins early.
@@ -523,5 +534,9 @@ public static void markAddMixin(ModContainer modContainer) {
523534
modContainer.markAddMixin();
524535
}
525536
}
537+
538+
public static boolean isInPreBootupStage() {
539+
return inPreBootup && !FoxLauncher.getFoxClassLoader().isAllowLoadingGame();
540+
}
526541
}
527542
}

loader/src/main/java/com/fox2code/foxloader/loader/java/JavaLoadingPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public ModInfo getReIndevModInfo() throws IOException {
7171
public @Nullable ModInfo getModInfo(@NotNull File mod, @Nullable String jarPath) throws Exception {
7272
if (mod.getName().endsWith(".jar") && (jarPath == null || jarPath.endsWith(".jar"))) {
7373
JavaModInfo javaModInfo = new JavaModInfo(mod, jarPath);
74-
if (javaModInfo.id != null && !javaModInfo.id.isEmpty() &&
74+
if (!javaModInfo.id.isEmpty() &&
7575
javaModInfo.forFoxLoaderVersion != null &&
7676
(javaModInfo.forFoxLoaderVersion.equals("*") ||
7777
javaModInfo.forFoxLoaderVersion.startsWith(

loader/src/main/java/com/fox2code/foxloader/updater/FoxLoaderUpdater.java

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.util.Arrays;
4444
import java.util.Map;
4545
import java.util.Scanner;
46+
import java.util.jar.Attributes;
47+
import java.util.jar.JarFile;
4648

4749
public final class FoxLoaderUpdater extends MavenUpdater {
4850
private static final String ALL_LOADERS_URL = "https://cdn.fox2code.com/maven/foxloader-version-map.json";
@@ -75,19 +77,72 @@ protected String findLatestVersion() throws IOException {
7577

7678
@Override
7779
protected void doUpdate() throws IOException {
80+
updateFoxLoaderImpl(this.latestMavenVersion, this.getUrlForLatestJar(), null);
81+
}
82+
83+
public static boolean updateFoxLoaderFromMod(File file) throws IOException {
84+
if (!ModLoaderInit.Internal.isInPreBootupStage()) {
85+
throw new IllegalStateException("Cannot be called at runtime");
86+
}
87+
String fileName = file.getName();
88+
if (!((fileName.startsWith("foxloader-") ||
89+
fileName.startsWith("loader-")) &&
90+
fileName.endsWith(".jar"))) {
91+
return false;
92+
}
93+
String foxLoaderVersion, mainClass;
94+
try (JarFile jarFile = new JarFile(file)) {
95+
Attributes attributes = jarFile.getManifest().getMainAttributes();
96+
foxLoaderVersion = attributes.getValue("FoxLoader-Version");
97+
mainClass = attributes.getValue("Main-Class");
98+
} catch (IOException e) {
99+
return false;
100+
}
101+
// Allow FoxLoader when it goes through the installer, but not when it is from a server only jar.
102+
if (foxLoaderVersion == null || foxLoaderVersion.isEmpty() ||
103+
mainClass == null || !mainClass.startsWith("com.fox2code.foxloader.")) {
104+
if (mainClass != null && mainClass.startsWith("com.fox2code.foxloader.launcher.")) {
105+
throw new IOException("Cannot upgrade from a server only jar. (File: " + fileName + ")");
106+
}
107+
return false;
108+
}
109+
ModLoaderInit.getModLoaderLogger().info("Found FoxLoader " +
110+
foxLoaderVersion + " in the mods folder: " + fileName);
111+
if (FoxLauncher.DEV_MODE || FoxLauncher.DEVELOPING_FOXLOADER) {
112+
ModLoaderInit.getModLoaderLogger().info(
113+
"Cannot update in a development environment, shutting down...");
114+
return true;
115+
}
116+
if (foxLoaderVersion.equals(BuildConfig.FOXLOADER_VERSION)) {
117+
ModLoaderInit.getModLoaderLogger().info(
118+
"Cannot update to the same version as itself, shutting down...");
119+
return true;
120+
}
121+
ModLoaderInit.getModLoaderLogger().info("Updating to FoxLoader " + foxLoaderVersion);
122+
updateFoxLoaderImpl(foxLoaderVersion, null, file);
123+
ModLoaderInit.getModLoaderLogger().info("Update completed, shutting down...");
124+
return true;
125+
}
126+
127+
private static void updateFoxLoaderImpl(String updateVersion, String remoteFoxLoaderJar, File localFoxLoaderJar) throws IOException {
128+
if (remoteFoxLoaderJar == null && localFoxLoaderJar == null) {
129+
throw new IOException("Both remoteFoxLoaderJar and localFoxLoaderJar are null...");
130+
}
78131
File dest = null;
79132
String[] args;
80133
LauncherType launcherType = FoxLauncher.getLauncherType();
81134
ModLoaderInit.getModLoaderLogger().info(
82-
"Updating to " + this.latestMavenVersion + " from " + launcherType + " launcher");
135+
"Updating to " + updateVersion + " from " + launcherType + " launcher");
83136
switch (launcherType) {
84-
default:
85-
return;
86137
case MMC_LIKE:
87138
File libraries = ModLoaderInit.getModContainer("foxloader").getModInfo().file.getParentFile();
88-
dest = new File(libraries, "foxloader-" + this.latestMavenVersion + ".jar");
139+
dest = new File(libraries, "foxloader-" + updateVersion + ".jar");
140+
// fall-through
89141
case VANILLA_LIKE:
90142
args = new String[]{null, "-jar", null, "--update", launcherType.name()};
143+
break;
144+
default:
145+
return;
91146
}
92147
if (dest == null) {
93148
File updateTmp = new File(ModLoader.getConfigFolder(), "update-tmp");
@@ -96,18 +151,21 @@ protected void doUpdate() throws IOException {
96151
.warning("Unable to create update tmp folder.");
97152
return;
98153
}
99-
dest = new File(updateTmp, "foxloader-" + this.latestMavenVersion + ".jar");
154+
dest = new File(updateTmp, "foxloader-" + updateVersion + ".jar");
100155
}
101-
if (BuildConfig.FOXLOADER_VERSION.equals(this.latestMavenVersion) &&
156+
if (BuildConfig.FOXLOADER_VERSION.equals(updateVersion) &&
102157
FoxLauncher.getLauncherType() != LauncherType.BIN) {
103158
// Can happen if wrongly installed
104159
if (!dest.equals(FoxLauncher.foxLoaderFile)) {
105160
Files.copy(FoxLauncher.foxLoaderFile.toPath(), dest.toPath(),
106161
StandardCopyOption.REPLACE_EXISTING);
107162
}
163+
} else if (localFoxLoaderJar != null && localFoxLoaderJar.exists()) {
164+
Files.copy(localFoxLoaderJar.toPath(), dest.toPath(),
165+
StandardCopyOption.REPLACE_EXISTING);
108166
} else {
109167
try (FileOutputStream fileOutputStream = new FileOutputStream(dest)) {
110-
NetUtils.downloadTo(this.getUrlForLatestJar(), fileOutputStream);
168+
NetUtils.downloadTo(remoteFoxLoaderJar, fileOutputStream);
111169
}
112170
}
113171
args[0] = Platform.getPlatform().javaBin.getPath();

loader/src/main/java/com/fox2code/foxloader/updater/MavenUpdater.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected MavenUpdater(ModContainer modContainer, String repository, String depe
5252
int i = dependencyBase.indexOf(':');
5353
int i2 = dependencyBase.indexOf(':', i + 1);
5454
if (i2 == -1) i2 = dependencyBase.length();
55+
if (!repository.endsWith("/")) repository += "/";
5556
this.mavenUrlRoot = repository + // "https://www.jitpack.io/"
5657
dependencyBase.substring(0, i2).replace('.', '/').replace(':', '/');
5758
this.mavenArtifactId = dependencyBase.substring(i + 1, i2).replace(':', '-');

0 commit comments

Comments
 (0)