Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 1bafda1

Browse files
Impl Forge's selective resource reloader (#107)
* Implement ISelectiveResourceReloadListener * Add other ResourceTypes, add readme, remove patchwork-fml dependency * Apply suggestions from code review Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com> * Made lambda injection working by disabling remap * Fix json indentation * Update patchwork-resource/src/main/java/net/patchworkmc/mixin/resource/MixinLanguageOptionsScreen.java Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com> Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com>
1 parent 943ee78 commit 1bafda1

24 files changed

+806
-0
lines changed

patchwork-resource/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
archivesBaseName = "patchwork-resource"
2+
version = getSubprojectVersion(project, "0.1.0")

patchwork-resource/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
This module contains Forge's "Selective Resource Reload Listener" (`ISelectiveResourceReloadListener`), which is used by Forge mods and Forge itself.
2+
3+
4+
The ISelectiveResourceReloadListener is designed to reload only specific resources to save time.
5+
However, Forge's vanilla patches are not finished yet; vanilla in-game reloading still reloads everything.
6+
For example, when you switch the Gui language, it also reloads sounds and models which is considered to be surplus.
7+
`ISelectiveResourceReloadListener` of Forge mods and Forge itself handles selective resource reloading correctly.
8+
9+
__This feature is toggleable in Forge, which indicates that Forge mods and Forge itself should be able to reload everything flawlessly in both cases.__
10+
11+
In Patchwork, we don't change the vanilla behavior and all reloads are not selective, at least for now, or until Forge implements this properly.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.resource;
21+
22+
/**
23+
* Represents a generic type of reloadable resource. Used for resource reload filtering.
24+
*/
25+
public interface IResourceType {
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.resource;
21+
22+
import java.util.function.Predicate;
23+
24+
import net.minecraft.resource.ResourceManager;
25+
import net.minecraft.resource.SynchronousResourceReloadListener;
26+
27+
public interface ISelectiveResourceReloadListener extends SynchronousResourceReloadListener {
28+
@Override
29+
default void apply(ResourceManager resourceManager) {
30+
// For compatibility, call the selective version from the non-selective function
31+
onResourceManagerReload(resourceManager, SelectiveReloadStateHandler.INSTANCE.get());
32+
}
33+
34+
/**
35+
* A version of onResourceManager that selectively chooses
36+
* {@link net.minecraftforge.resource.IResourceType}s to reload. When using
37+
* this, the given predicate should be called to ensure the relevant resources
38+
* should be reloaded at this time.
39+
*
40+
* @param resourceManager the resource manager being reloaded
41+
* @param resourcePredicate predicate to test whether any given resource type
42+
* should be reloaded
43+
*/
44+
void onResourceManagerReload(ResourceManager resourceManager, Predicate<IResourceType> resourcePredicate);
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.resource;
21+
22+
import java.util.Set;
23+
import java.util.function.Predicate;
24+
25+
import com.google.common.collect.Sets;
26+
27+
import net.fabricmc.api.EnvType;
28+
import net.fabricmc.api.Environment;
29+
30+
@Environment(EnvType.CLIENT)
31+
public final class ReloadRequirements {
32+
/**
33+
* Creates a reload predicate accepting all resource types.
34+
*
35+
* @return a predicate accepting all types
36+
*/
37+
public static Predicate<IResourceType> all() {
38+
return type -> true;
39+
}
40+
41+
/**
42+
* Creates an inclusive reload predicate. Only given resource types will be
43+
* loaded along with this.
44+
*
45+
* @param inclusion the set of resource types to be included in the reload
46+
* @return an inclusion predicate based on the given types
47+
*/
48+
public static Predicate<IResourceType> include(IResourceType... inclusion) {
49+
Set<IResourceType> inclusionSet = Sets.newHashSet(inclusion);
50+
return inclusionSet::contains;
51+
}
52+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.resource;
21+
22+
import java.util.function.Predicate;
23+
24+
import javax.annotation.Nullable;
25+
26+
import net.minecraft.resource.SynchronousResourceReloadListener;
27+
//import net.minecraftforge.common.ForgeConfig;
28+
import net.patchworkmc.impl.resource.TypedResourceLoader;
29+
30+
/**
31+
* Handles reload parameters for selective loaders.
32+
*/
33+
public enum SelectiveReloadStateHandler {
34+
INSTANCE;
35+
36+
@Nullable
37+
private Predicate<IResourceType> currentPredicate = null;
38+
39+
/**
40+
* Pushes a resource type predicate for the current reload. Should only be
41+
* called when initiating a resource reload. If a reload is already in progress
42+
* when this is called, an exception will be thrown.
43+
*
44+
* @param resourcePredicate the resource requirement predicate for the current
45+
* reload
46+
*/
47+
public void beginReload(Predicate<IResourceType> resourcePredicate) {
48+
if (this.currentPredicate != null) {
49+
throw new IllegalStateException("Recursive resource reloading detected");
50+
}
51+
52+
this.currentPredicate = resourcePredicate;
53+
}
54+
55+
/**
56+
* Gets the current reload resource predicate for the initiated reload.
57+
*
58+
* @return the active reload resource predicate, or an accepting one if none in
59+
* progress
60+
*/
61+
public Predicate<IResourceType> get() {
62+
if (this.currentPredicate == null) { // || !ForgeConfig.CLIENT.selectiveResourceReloadEnabled.get()) {
63+
return ReloadRequirements.all();
64+
}
65+
66+
return this.currentPredicate;
67+
}
68+
69+
/**
70+
* Finishes the current reload and deletes the previously added reload
71+
* predicate.
72+
*/
73+
public void endReload() {
74+
this.currentPredicate = null;
75+
}
76+
77+
// Helpder function for testing if vanilla listeners should reload.
78+
public boolean test(SynchronousResourceReloadListener listener) {
79+
IResourceType type = listener instanceof TypedResourceLoader
80+
? ((TypedResourceLoader) listener).getResourceType() : null;
81+
return type == null || get() == null || get().test(type);
82+
}
83+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.resource;
21+
22+
/**
23+
* An enum of all {@link IResourceType}s used by the Vanilla game. These should
24+
* be used if handling vanilla-related resources.
25+
*/
26+
public enum VanillaResourceType implements IResourceType {
27+
/**
28+
* Used when block and item models are reloaded and rebaked. This also includes
29+
* the texture-stitching from that phase.
30+
*/
31+
MODELS,
32+
33+
/**
34+
* Used when textures from the
35+
* {@link net.minecraft.client.renderer.texture.TextureManager} are reloaded.
36+
* Does not effect block or item textures on the texture atlas.
37+
*/
38+
TEXTURES,
39+
40+
/**
41+
* Used when all game sounds are reloaded.
42+
*/
43+
SOUNDS,
44+
45+
/**
46+
* Used when the current language is reloaded.
47+
*/
48+
LANGUAGES,
49+
50+
/**
51+
* Used when all shaders are reloaded.
52+
*/
53+
SHADERS,
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.impl.resource;
21+
22+
import java.util.concurrent.CompletableFuture;
23+
24+
import javax.annotation.Nullable;
25+
26+
import net.minecraftforge.resource.IResourceType;
27+
import net.minecraftforge.resource.ReloadRequirements;
28+
import net.minecraftforge.resource.SelectiveReloadStateHandler;
29+
import net.minecraftforge.resource.VanillaResourceType;
30+
31+
import net.minecraft.client.MinecraftClient;
32+
33+
public interface TypedResourceLoader {
34+
default IResourceType getResourceType() {
35+
return null;
36+
}
37+
38+
/**
39+
* Called by vanilla hooks and ForgeHooksClient.
40+
*/
41+
static CompletableFuture<Void> patchwork$refreshResources(MinecraftClient mc, VanillaResourceType... types) {
42+
SelectiveReloadStateHandler.INSTANCE.beginReload(ReloadRequirements.include(types));
43+
CompletableFuture<Void> ret = mc.reloadResources();
44+
SelectiveReloadStateHandler.INSTANCE.endReload();
45+
return ret;
46+
}
47+
48+
@Nullable
49+
static IResourceType patchwork$getResourceType(Object obj) {
50+
return obj instanceof TypedResourceLoader ? ((TypedResourceLoader) obj).getResourceType() : null;
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.resource;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import net.minecraftforge.resource.IResourceType;
24+
import net.minecraftforge.resource.VanillaResourceType;
25+
26+
import net.minecraft.client.render.model.BakedModelManager;
27+
28+
import net.patchworkmc.impl.resource.TypedResourceLoader;
29+
30+
@Mixin(BakedModelManager.class)
31+
public abstract class MixinBakedModelManager implements TypedResourceLoader {
32+
@Override
33+
public IResourceType getResourceType() {
34+
return VanillaResourceType.MODELS;
35+
}
36+
}

0 commit comments

Comments
 (0)