Skip to content

Commit

Permalink
Prevent mods from shadowing default resources (Fixes #66) (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbot16 authored and modmuss50 committed Jul 16, 2019
1 parent 4229e3b commit 7495ea2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public <T extends ResourcePackContainer> void registerContainer(Map<String, T> m
}

T var3 = ResourcePackContainer.of("fabric/" + ((ModResourcePack) pack).getFabricModMetadata().getId(),
false, () -> pack, factory, ResourcePackContainer.InsertionPosition.BOTTOM);
false, () -> pack, factory, ResourcePackContainer.InsertionPosition.TOP);

if (var3 != null) {
map.put(var3.getName(), var3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.resources;

import net.minecraft.resource.DefaultResourcePack;
import net.minecraft.resource.DirectoryResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;

@Mixin(DefaultResourcePack.class)
public class MixinDefaultResourcePack {

@Inject(method = "findInputStream", at = @At("HEAD"), cancellable = true)
protected void onFindInputStream(ResourceType resourceType, Identifier identifier, CallbackInfoReturnable<InputStream> callback) {
if(DefaultResourcePack.RESOURCE_PATH != null) {
// Fall through to Vanilla logic, they have a special case here.
return;
}

String path = resourceType.getName() + "/" + identifier.getNamespace() + "/" + identifier.getPath();
URL found = null;

try {
Enumeration<URL> candidates = DefaultResourcePack.class.getClassLoader().getResources(path);

// Get the last element
while(candidates.hasMoreElements()) {
found = candidates.nextElement();
}

if(found == null || !DirectoryResourcePack.isValidPath(new File(found.getFile()), "/" + path)) {
// Mimics vanilla behavior

callback.setReturnValue(null);
return;
}
} catch (IOException var6) {
// Default path
}

try {
if(found != null) {
callback.setReturnValue(found.openStream());
}
} catch (Exception e) {
// Default path
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"mixins": [
"MixinKeyedResourceReloadListener$Server",
"MixinMinecraftServer",
"MixinReloadableResourceManagerImpl"
"MixinReloadableResourceManagerImpl",
"MixinDefaultResourcePack"
],
"client": [
"MixinKeyedResourceReloadListener$Client",
Expand Down

0 comments on commit 7495ea2

Please sign in to comment.