Skip to content

Commit

Permalink
Avoid repeated creation of useless heavy objects
Browse files Browse the repository at this point in the history
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
  • Loading branch information
TheSilkMiner committed May 21, 2022
1 parent e9c1844 commit 3aa5713
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.FileSystem;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand All @@ -28,7 +25,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

final class RuntimePack implements PackResources {
final class RuntimePack {
private static final class ResourceGatherer extends SimpleFileVisitor<Path> {
private final Path root;
private final Consumer<Path> consumer;
Expand Down Expand Up @@ -75,18 +72,15 @@ private static JsonObject validateMeta(final JsonObject object) {
throw new IllegalArgumentException("Invalid metadata sections " + invalid + ": not a JsonObject");
}

@Nullable
@Override
public InputStream getRootResource(final String s) throws IOException {
InputStream rootResource(final String s) throws IOException {
Objects.requireNonNull(s);
if (s.contains("/") || s.contains("\\")) {
throw new IllegalStateException("Root resources can only be file names");
}
return this.resource(s);
}

@Override
public InputStream getResource(final PackType packType, final ResourceLocation resourceLocation) throws IOException {
InputStream resource(final PackType packType, final ResourceLocation resourceLocation) throws IOException {
Objects.requireNonNull(packType);
Objects.requireNonNull(resourceLocation);
if (packType != this.type || !this.targetNamespace.equals(resourceLocation.getNamespace())) {
Expand All @@ -95,8 +89,7 @@ public InputStream getResource(final PackType packType, final ResourceLocation r
return this.resource(resourceLocation.getPath());
}

@Override
public Collection<ResourceLocation> getResources(final PackType packType, final String s, final String s1, final int i, final Predicate<String> predicate) {
Collection<ResourceLocation> resources(final PackType packType, final String s, final String s1, final int i, final Predicate<String> predicate) {
Objects.requireNonNull(packType);
Objects.requireNonNull(s);
Objects.requireNonNull(s1);
Expand All @@ -114,35 +107,25 @@ public Collection<ResourceLocation> getResources(final PackType packType, final
return resources;
}

@Override
public boolean hasResource(final PackType packType, final ResourceLocation resourceLocation) {
boolean knowsResource(final PackType packType, final ResourceLocation resourceLocation) {
Objects.requireNonNull(packType);
Objects.requireNonNull(resourceLocation);
return packType == this.type && this.targetNamespace.equals(resourceLocation.getNamespace()) && Files.exists(this.pathOf(resourceLocation.getPath()));
}

@Override
public Set<String> getNamespaces(final PackType packType) {
Set<String> namespaces(final PackType packType) {
return this.type == packType? Set.of(this.targetNamespace) : Collections.emptySet();
}

@Nullable
@Override
public <T> T getMetadataSection(final MetadataSectionSerializer<T> metadataSectionSerializer) {
<T> T metadataSection(final MetadataSectionSerializer<T> metadataSectionSerializer) {
final String name = metadataSectionSerializer.getMetadataSectionName();
return this.metadata.has(name)? metadataSectionSerializer.fromJson(this.metadata.getAsJsonObject(name)) : null;
}

@Override
public String getName() {
String name() {
return this.name;
}

@Override
public void close() {
// Do not close file system: it gets managed automatically
}

private InputStream resource(final String resource) throws IOException {
final Path path = this.pathOf(resource);
return Files.exists(path)? Files.newInputStream(path, StandardOpenOption.READ) : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.blamejared.contenttweaker.core.resource;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

final class RuntimePackResources implements PackResources {
private final RuntimePack pack;

RuntimePackResources(final RuntimePack pack) {
this.pack = Objects.requireNonNull(pack);
}

@Nullable
@Override
public InputStream getRootResource(final String s) throws IOException {
return this.pack.rootResource(s);
}

@Override
public InputStream getResource(final PackType packType, final ResourceLocation resourceLocation) throws IOException {
return this.pack.resource(packType, resourceLocation);
}

@Override
public Collection<ResourceLocation> getResources(final PackType packType, final String s, final String s1, final int i, final Predicate<String> predicate) {
return this.pack.resources(packType, s, s1, i, predicate);
}

@Override
public boolean hasResource(final PackType packType, final ResourceLocation resourceLocation) {
return this.pack.knowsResource(packType, resourceLocation);
}

@Override
public Set<String> getNamespaces(final PackType packType) {
return this.pack.namespaces(packType);
}

@Nullable
@Override
public <T> T getMetadataSection(final MetadataSectionSerializer<T> metadataSectionSerializer) throws IOException {
return this.pack.metadataSection(metadataSectionSerializer);
}

@Override
public String getName() {
return this.pack.name();
}

@Override
public void close() {
// Do not close resources: it gets managed automatically on shutdown
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

public final class RuntimeRepositorySource implements RepositorySource {
private static final int CLIENT_FORMAT = 8;
private static final int SERVER_FORMAT = 8;

private final PackType type;

private RuntimeRepositorySource(final PackType type) {
Expand All @@ -39,15 +37,16 @@ public void loadPacks(final Consumer<Pack> consumer, final Pack.PackConstructor

private Pack pack(final PackType type, final String id, final RuntimeFragment fragment, final Pack.PackConstructor constructor) {
final String packId = ContentTweakerConstants.rl("runtime/" + fragment.fsId().replace(':', '/')).toString();
final Pack pack = Pack.create(packId, true, () -> this.createPack(type, id, packId, fragment), constructor, Pack.Position.TOP, this::decorateSource);
final Pack pack = Pack.create(packId, true, this.createPack(type, id, packId, fragment), constructor, Pack.Position.TOP, this::decorateSource);
if (pack == null) {
throw new IllegalStateException("An error occurred while generating runtime " + ContentTweakerConstants.MOD_NAME + " pack '" + packId + "'");
}
return pack;
}

private PackResources createPack(final PackType type, final String target, final String packId, final RuntimeFragment fragment) {
return new RuntimePack(packId, target, type, this.makeMetadata(type, packId, target), fragment::fs);
private Supplier<PackResources> createPack(final PackType type, final String target, final String packId, final RuntimeFragment fragment) {
final RuntimePack pack = new RuntimePack(packId, target, type, this.makeMetadata(type, packId, target), fragment::fs);
return () -> new RuntimePackResources(pack);
}

private JsonObject makeMetadata(final PackType type, final String packId, final String target) {
Expand Down

0 comments on commit 3aa5713

Please sign in to comment.