Skip to content

Commit 2c35e18

Browse files
authored
Misc. cleanup and minor optimisations (#3)
- Reduced the number of allocating lambdas and object allocations - Avoided double `DownloadAssets#getAssetDest()` calls on cache miss - Drastically simplified `JarVersionInfo#hello` - Use `MethodHandles#publicLookup` - Requires the main method to be public, in a public class and in a package that is unconditionally open (if in a module), but faster to obtain than a `MethodHandles#lookup`
1 parent 70bbc65 commit 2c35e18

File tree

3 files changed

+24
-78
lines changed

3 files changed

+24
-78
lines changed

src/main/java/net/minecraftforge/launcher/DownloadAssets.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraftforge.util.logging.Log;
1313

1414
import java.io.File;
15+
import java.util.Map;
1516

1617
/** Handles downloading assets for Minecraft. */
1718
final class DownloadAssets {
@@ -29,35 +30,34 @@ static void download(String repo, File assetsDir, MinecraftVersion versionJson)
2930
if (!objectsDir.exists() && !objectsDir.mkdirs())
3031
throw new IllegalStateException("Failed to create objects directory: " + objectsDir);
3132

32-
index.objects.forEach((name, asset) -> {
33-
File file = new File(objectsDir, getAssetDest(asset.hash));
33+
for (Map.Entry<String, AssetsIndex.Asset> entry : index.objects.entrySet()) {
34+
String name = entry.getKey();
35+
AssetsIndex.Asset asset = entry.getValue();
36+
String assetDest = getAssetDest(asset.hash);
37+
File file = new File(objectsDir, assetDest);
3438
if (file.exists()) {
3539
Log.debug("Considering existing file with size " + file.length() + " and hash " + asset.hash + " for " + name);
3640
if (file.length() == asset.size && HashFunction.SHA1.sneakyHash(file).equals(asset.hash)) {
3741
Log.debug("Hash and size check succeeded. Skipping.");
38-
return;
42+
continue;
3943
}
4044
}
4145

4246
// We need to download assets? Release the log so the consumer is aware.
4347
Log.release();
4448
try {
4549
Log.info("Downloading missing asset: " + name);
46-
DownloadUtils.downloadFile(file, getAssetDownloadUrl(repo, asset.hash));
50+
DownloadUtils.downloadFile(file, repo + assetDest);
4751
} catch (Exception e) {
4852
throw new IllegalStateException("Failed to download " + name, e);
4953
}
50-
});
54+
}
5155
}
5256

5357
private static String getAssetDest(String hash) {
5458
return hash.substring(0, 2) + "/" + hash;
5559
}
5660

57-
private static String getAssetDownloadUrl(String repo, String hash) {
58-
return repo + getAssetDest(hash);
59-
}
60-
6161
private static File downloadIndex(MinecraftVersion versionJson, File assetsDir) {
6262
File index = new File(assetsDir, "indexes/" + versionJson.assetIndex.id + ".json");
6363
if (index.exists() && HashFunction.SHA1.sneakyHash(index).equals(versionJson.assetIndex.sha1)) {

src/main/java/net/minecraftforge/launcher/JarVersionInfo.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/main/java/net/minecraftforge/launcher/Main.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.invoke.MethodHandle;
2121
import java.lang.invoke.MethodHandles;
2222
import java.lang.invoke.MethodType;
23-
import java.util.Arrays;
2423
import java.util.zip.ZipEntry;
2524
import java.util.zip.ZipFile;
2625

@@ -82,7 +81,8 @@ private static Launcher run(String[] args) throws Throwable {
8281
.accepts("main", "The main class to run")
8382
.withRequiredArg().ofType(String.class);
8483

85-
JarVersionInfo.of(Main.class).hello(Log::info, false, true);
84+
Package pkg = Main.class.getPackage();
85+
Log.info(pkg.getImplementationTitle() + " " + pkg.getImplementationVersion());
8686

8787
SplitArgs split = new SplitArgs(args);
8888

@@ -118,15 +118,17 @@ private static Launcher run(String[] args) throws Throwable {
118118
MethodHandle mainMethod;
119119
try {
120120
Class<?> main = Class.forName(mainClass);
121-
mainMethod = MethodHandles.lookup().findStatic(main, "main", MethodType.methodType(void.class, String[].class));
121+
mainMethod = MethodHandles.publicLookup().findStatic(main, "main", MethodType.methodType(void.class, String[].class));
122122
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
123123
throw new IllegalStateException("Could not find main class!", e);
124124
}
125125

126126
Log.info("Sanitizing Minecraft arguments");
127-
Arrays.asList(split.mc).replaceAll(s -> s
128-
.replace("{asset_index}", versionJson.assetIndex.id)
129-
.replace("{assets_root}", assets.getAbsolutePath()));
127+
for (int i = 0; i < split.mc.length; i++) {
128+
split.mc[i] = split.mc[i]
129+
.replace("{asset_index}", versionJson.assetIndex.id)
130+
.replace("{assets_root}", assets.getAbsolutePath());
131+
}
130132

131133
return new Launcher(mainClass, mainMethod, split.mc);
132134
}
@@ -155,7 +157,13 @@ private static final class SplitArgs {
155157

156158
private SplitArgs(String[] args) {
157159
// we're looking for the first "--"
158-
int splitIdx = Arrays.asList(args).indexOf("--");
160+
int splitIdx = -1;
161+
for (int i = 0; i < args.length; i++) {
162+
if (args[i].equals("--")) {
163+
splitIdx = i;
164+
break;
165+
}
166+
}
159167

160168
if (splitIdx < 0) {
161169
this.sl = args;

0 commit comments

Comments
 (0)