Skip to content

Commit 6c91ef1

Browse files
committed
feature: use own simple client resource loader
prevents server resources from being loaded through mods easily
1 parent 3d7827a commit 6c91ef1

File tree

7 files changed

+127
-55
lines changed

7 files changed

+127
-55
lines changed

Diff for: build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ plugins {
33
id 'maven-publish'
44
}
55

6-
sourceCompatibility = JavaVersion.VERSION_1_8
7-
targetCompatibility = JavaVersion.VERSION_1_8
8-
96
version = project.mod_version
107
group = project.maven_group
118

@@ -31,7 +28,7 @@ dependencies {
3128
// Fabric API
3229
//include(modImplementation "net.fabricmc.fabric-api:fabric-key-binding-api-v1:1.0.36+fb8d95da77")
3330
include(modImplementation "com.github.tildejustin:key-binding-api:a41fc49671")
34-
include(modImplementation "net.fabricmc.fabric-api:fabric-resource-loader-v0:0.2.7+c668f41583")
31+
//include(modImplementation "net.fabricmc.fabric-api:fabric-resource-loader-v0:0.2.7+c668f41583")
3532
}
3633

3734
processResources {
@@ -51,6 +48,9 @@ tasks.withType(JavaCompile).configureEach {
5148
}
5249

5350
java {
51+
sourceCompatibility = JavaVersion.VERSION_1_8
52+
targetCompatibility = JavaVersion.VERSION_1_8
53+
5454
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
5555
// if it is present.
5656
// If you remove this line, sources will not be generated.

Diff for: gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ org.gradle.parallel=true
66
# check these on https://fabricmc.net/develop
77
minecraft_version=1.16.1
88
yarn_mappings=1.16.1.build.17
9-
loader_version=0.15.7
9+
loader_version=0.16.0
1010

1111
# Mod Properties
1212
mod_version=1.0
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package org.mcsr.speedrunapi.mixin;
2-
3-
import net.minecraft.client.MinecraftClient;
4-
import org.mcsr.speedrunapi.config.SpeedrunConfigAPI;
5-
import org.spongepowered.asm.mixin.Mixin;
6-
import org.spongepowered.asm.mixin.injection.At;
7-
import org.spongepowered.asm.mixin.injection.Inject;
8-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9-
10-
@Mixin(MinecraftClient.class)
11-
public abstract class MinecraftClientMixin {
12-
13-
@Inject(method = "<init>", at = @At("TAIL"))
14-
private void onPostLaunchEntrypoint(CallbackInfo ci) {
15-
SpeedrunConfigAPI.onPostLaunch();
16-
}
17-
}
1+
package org.mcsr.speedrunapi.mixin.entrypoint;
2+
3+
import net.minecraft.client.MinecraftClient;
4+
import org.mcsr.speedrunapi.config.SpeedrunConfigAPI;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9+
10+
@Mixin(MinecraftClient.class)
11+
public abstract class MinecraftClientMixin {
12+
13+
@Inject(method = "<init>", at = @At("TAIL"))
14+
private void onPostLaunchEntrypoint(CallbackInfo ci) {
15+
SpeedrunConfigAPI.onPostLaunch();
16+
}
17+
}

Diff for: src/main/java/org/mcsr/speedrunapi/mixin/MinecraftDedicatedServerMixin.java renamed to src/main/java/org/mcsr/speedrunapi/mixin/entrypoint/MinecraftDedicatedServerMixin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.mcsr.speedrunapi.mixin;
1+
package org.mcsr.speedrunapi.mixin.entrypoint;
22

33
import net.minecraft.server.dedicated.MinecraftDedicatedServer;
44
import org.mcsr.speedrunapi.config.SpeedrunConfigAPI;
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
package org.mcsr.speedrunapi.mixin;
2-
3-
import net.minecraft.client.gui.screen.Screen;
4-
import net.minecraft.client.gui.screen.options.OptionsScreen;
5-
import net.minecraft.text.Text;
6-
import net.minecraft.text.TranslatableText;
7-
import net.minecraft.util.Identifier;
8-
import org.mcsr.speedrunapi.config.screen.SpeedrunModConfigsScreen;
9-
import org.mcsr.speedrunapi.config.screen.widgets.IconButtonWidget;
10-
import org.spongepowered.asm.mixin.Mixin;
11-
import org.spongepowered.asm.mixin.injection.At;
12-
import org.spongepowered.asm.mixin.injection.Inject;
13-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
14-
15-
@Mixin(OptionsScreen.class)
16-
public abstract class OptionsScreenMixin extends Screen {
17-
18-
protected OptionsScreenMixin(Text title) {
19-
super(title);
20-
}
21-
22-
@Inject(method = "init", at = @At("TAIL"))
23-
private void addSpeedrunConfigButton(CallbackInfo ci) {
24-
this.addButton(new IconButtonWidget(new Identifier("textures/item/writable_book.png"), this.width / 2 + 160, this.height / 6 - 12, new TranslatableText("speedrunapi.gui.config.button"), button -> {
25-
assert this.client != null;
26-
this.client.openScreen(new SpeedrunModConfigsScreen(this));
27-
}));
28-
}
29-
}
1+
package org.mcsr.speedrunapi.mixin.gui;
2+
3+
import net.minecraft.client.gui.screen.Screen;
4+
import net.minecraft.client.gui.screen.options.OptionsScreen;
5+
import net.minecraft.text.Text;
6+
import net.minecraft.text.TranslatableText;
7+
import net.minecraft.util.Identifier;
8+
import org.mcsr.speedrunapi.config.screen.SpeedrunModConfigsScreen;
9+
import org.mcsr.speedrunapi.config.screen.widgets.IconButtonWidget;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Inject;
13+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
14+
15+
@Mixin(OptionsScreen.class)
16+
public abstract class OptionsScreenMixin extends Screen {
17+
18+
protected OptionsScreenMixin(Text title) {
19+
super(title);
20+
}
21+
22+
@Inject(method = "init", at = @At("TAIL"))
23+
private void addSpeedrunConfigButton(CallbackInfo ci) {
24+
this.addButton(new IconButtonWidget(new Identifier("textures/item/writable_book.png"), this.width / 2 + 160, this.height / 6 - 12, new TranslatableText("speedrunapi.gui.config.button"), button -> {
25+
assert this.client != null;
26+
this.client.openScreen(new SpeedrunModConfigsScreen(this));
27+
}));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.mcsr.speedrunapi.mixin.resourceloader;
2+
3+
import net.fabricmc.loader.api.FabricLoader;
4+
import net.fabricmc.loader.api.ModContainer;
5+
import net.minecraft.client.resource.DefaultClientResourcePack;
6+
import net.minecraft.resource.ResourceType;
7+
import net.minecraft.util.Identifier;
8+
import org.mcsr.speedrunapi.SpeedrunAPI;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Unique;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Inject;
13+
import org.spongepowered.asm.mixin.injection.ModifyArg;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
15+
16+
import java.io.InputStream;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
@Mixin(DefaultClientResourcePack.class)
21+
public abstract class DefaultResourcePackMixin {
22+
23+
@Unique
24+
private static final boolean HAS_FABRIC_RESOURCE_LOADER = FabricLoader.getInstance().isModLoaded("fabric-resource-loader-v0");
25+
26+
@ModifyArg(
27+
method = "<init>",
28+
at = @At(
29+
value = "INVOKE",
30+
target = "Lnet/minecraft/resource/DefaultResourcePack;<init>([Ljava/lang/String;)V"
31+
)
32+
)
33+
private static String[] addModNamespaces(String[] namespaces) {
34+
if (HAS_FABRIC_RESOURCE_LOADER) {
35+
SpeedrunAPI.LOGGER.info("Disabling SpeedrunAPI resource loader in favor of fabric-resource-loader.");
36+
return namespaces;
37+
}
38+
39+
List<ModContainer> mods = new ArrayList<>(FabricLoader.getInstance().getAllMods());
40+
mods.removeIf(mod -> mod.getMetadata().getType().equals("builtin"));
41+
42+
String[] allNamespaces = new String[mods.size() + namespaces.length];
43+
int i;
44+
for (i = 0; i < namespaces.length; i++) {
45+
allNamespaces[i] = namespaces[i];
46+
}
47+
for (ModContainer mod : mods) {
48+
allNamespaces[i++] = mod.getMetadata().getId();
49+
}
50+
return allNamespaces;
51+
}
52+
53+
@Inject(method = "findInputStream", at = @At("HEAD"), cancellable = true)
54+
private void loadModResources(ResourceType type, Identifier id, CallbackInfoReturnable<InputStream> cir) {
55+
if (HAS_FABRIC_RESOURCE_LOADER) {
56+
return;
57+
}
58+
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
59+
if (mod.getMetadata().getType().equals("builtin")) {
60+
continue;
61+
}
62+
mod.findPath(type.getDirectory() + "/" + id.getNamespace() + "/" + id.getPath()).ifPresent(path -> {
63+
try {
64+
cir.setReturnValue(path.toUri().toURL().openStream());
65+
} catch (Exception e) {
66+
SpeedrunAPI.LOGGER.warn("Failed to load resource '{}' from mod '{}'.", id, mod.getMetadata().getId(), e);
67+
}
68+
});
69+
}
70+
}
71+
}

Diff for: src/main/resources/speedrunapi.mixins.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"defaultRequire": 1
77
},
88
"client": [
9-
"MinecraftClientMixin",
10-
"OptionsScreenMixin"
9+
"entrypoint.MinecraftClientMixin",
10+
"gui.OptionsScreenMixin",
11+
"resourceloader.DefaultResourcePackMixin"
1112
],
1213
"server": [
13-
"MinecraftDedicatedServerMixin"
14+
"entrypoint.MinecraftDedicatedServerMixin"
1415
],
1516
"plugin": "org.mcsr.speedrunapi.mixin_plugin.SpeedrunMixinConfigPlugin"
1617
}

0 commit comments

Comments
 (0)