Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
package net.ornithemc.osl.resource.loader.impl;

import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import net.fabricmc.loader.api.FabricLoader;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.function.IOSupplier;
import net.ornithemc.osl.resource.loader.api.resource.ResourceType;
import net.ornithemc.osl.resource.loader.api.resource.pack.ResourceConsumer;
import net.ornithemc.osl.resource.loader.impl.resource.pack.ModContainerResourcePack;
import net.ornithemc.osl.resource.loader.impl.resource.pack.ResourcePacks;
import net.ornithemc.osl.text.api.TextComponents;
import net.ornithemc.osl.resource.loader.impl.resource.pack.ModResourcePack;

public class DefaultTexturePack extends ModContainerResourcePack {
public class DefaultTexturePack extends TexturePack {

public DefaultTexturePack() {
super(FabricLoader.getInstance().getModContainer("minecraft").get(), ".", "Default");
}

@Override
protected IOSupplier<InputStream> generateMetadataFile() {
return ResourcePacks.generateMetadataFile(
TextComponents.literal("The default look of Minecraft")
);
}

@Override
protected IOSupplier<InputStream> generateIconFile() {
return ModContainerResourcePack.getIcon(this.mod);
}

@Override
protected Map<ResourceType, Set<String>> findNamespaces() {
return Collections.emptyMap();
}

@Override
public boolean hasResource(ResourceType type, NamespacedIdentifier location) {
return false;
}

@Override
public IOSupplier<InputStream> getResource(ResourceType type, NamespacedIdentifier location) {
return null;
super(new ModResourcePack(FabricLoader.getInstance().getModContainer("minecraft").get(), ".", "Default"));
}

@Override
public void findResources(ResourceType type, String namespace, String directory, ResourceConsumer consumer) {
void initMetadata() {
this.descriptionLine1 = "The default look of Minecraft";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package net.ornithemc.osl.resource.loader.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.function.IOSupplier;
import net.ornithemc.osl.resource.loader.api.resource.ResourceType;
import net.ornithemc.osl.resource.loader.api.resource.pack.AbstractResourcePack;
import net.ornithemc.osl.resource.loader.api.resource.pack.ResourceConsumer;
import net.ornithemc.osl.resource.loader.api.resource.pack.ResourcePack;
import net.ornithemc.osl.resource.loader.impl.resource.pack.ResourcePacks;
import net.ornithemc.osl.text.api.TextComponents;

public class TexturePack extends AbstractResourcePack {

public static final String METADATA_FILE = "pack.txt";

private final ResourcePack delegate;
private final IOSupplier<InputStream> metadata;

public String descriptionLine1 = "";
public String descriptionLine2 = "";

public TexturePack(ResourcePack delegate) {
this.delegate = delegate;
this.metadata = this.createMetadata();
}

private IOSupplier<InputStream> createMetadata() {
this.initMetadata();

String description = "";
if (this.descriptionLine1 != null) {
description += this.descriptionLine1;
if (!this.descriptionLine2.isEmpty()) {
description += "\n" + this.descriptionLine2;
}
}

return ResourcePacks.generateMetadataFile(
TextComponents.literal(description)
);
}

void initMetadata() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(this.getResource(METADATA_FILE)))) {
this.descriptionLine1 = this.trimDescriptionLine(br.readLine());
this.descriptionLine2 = this.trimDescriptionLine(br.readLine());
} catch (IOException e) {
ResourceLoader.LOGGER.warn("Unable to read metadata from texture pack " + this.getName(), e);
}
}

String trimDescriptionLine(String line) {
if (line.length() > 34) {
line = line.substring(0, 34);
}

return line;
}

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

@Override
public boolean hasResource(String path) {
if (ResourcePack.METADATA_FILE.equals(path)) {
return true;
}

return this.delegate.hasResource(path);
}

@Override
public InputStream getResource(String path) throws IOException {
if (ResourcePack.METADATA_FILE.equals(path)) {
return this.metadata.get();
}

return this.delegate.getResource(path);
}

@Override
protected Map<ResourceType, Set<String>> findNamespaces() {
return Collections.emptyMap();
}

@Override
public boolean hasResource(ResourceType type, NamespacedIdentifier location) {
return false;
}

@Override
public IOSupplier<InputStream> getResource(ResourceType type, NamespacedIdentifier location) {
return null;
}

@Override
public void findResources(ResourceType type, String namespace, String directory, ResourceConsumer consumer) {
}

@Override
public void open() {
this.delegate.open();
}

@Override
public void close() {
this.delegate.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.ornithemc.osl.resource.loader.impl;

import java.nio.file.Files;
import java.nio.file.Path;

import net.ornithemc.osl.resource.loader.api.resource.pack.ResourcePack;
import net.ornithemc.osl.resource.loader.impl.resource.repository.ResourcePackDetector;

public class TexturePackDetector extends ResourcePackDetector {

@Override
protected boolean isValidDirectory(Path file) {
return Files.exists(file.resolve(TexturePack.METADATA_FILE));
}

@Override
protected ResourcePack createDirectoryPack(Path file) {
return new TexturePack(super.createDirectoryPack(file));
}

@Override
protected ResourcePack createZippedPack(Path file) {
return new TexturePack(super.createZippedPack(file));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.ornithemc.osl.resource.loader.impl.access;

public interface GameOptionsAccess {

String osl$resource_loader$getSkin();

void osl$resource_loader$setSkin(String skin);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.ornithemc.osl.resource.loader.impl.mixin.client;

import java.io.PrintWriter;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.llamalad7.mixinextras.sugar.Local;

import net.minecraft.client.options.GameOptions;

import net.ornithemc.osl.resource.loader.impl.access.GameOptionsAccess;

@Mixin(GameOptions.class)
public class GameOptionsMixin implements GameOptionsAccess {

private static final String SKIN_OPTION = "skin";
private static final String SKIN_DEFAULT_VALUE = "Default";

@Unique
private String skin;

@Inject(
method = "<init>*",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/options/GameOptions;load()V"
)
)
private void osl$resource_loader$initSkinOption(CallbackInfo ci) {
this.skin = SKIN_DEFAULT_VALUE;
}

@Inject(
method = "load",
at = @At(
value = "INVOKE",
target = "Ljava/lang/String;split(Ljava/lang/String;)[Ljava/lang/String;",
shift = Shift.BY,
by = 2
)
)
private void osl$resource_loader$loadSkinOption(CallbackInfo ci, @Local String[] option) {
if (option[0].equals(SKIN_OPTION)) {
this.skin = option[1];
}
}

@Inject(
method = "save",
at = @At(
value = "INVOKE",
target = "Ljava/io/PrintWriter;println(Ljava/lang/String;)V",
ordinal = 10, // after fancyGraphics option
shift = Shift.AFTER
)
)
private void osl$resource_loader$skinSkinOption(CallbackInfo ci, @Local PrintWriter writer) {
writer.println(String.format("%s:%s", SKIN_OPTION, this.skin));
}

@Override
public String osl$resource_loader$getSkin() {
return this.skin;
}

@Override
public void osl$resource_loader$setSkin(String skin) {
this.skin = skin;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
package net.ornithemc.osl.resource.loader.impl.mixin.client;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.Minecraft;
import net.minecraft.client.options.GameOptions;
import net.minecraft.client.render.texture.TextureManager;
import net.minecraft.client.render.world.WorldRenderer;

import net.ornithemc.osl.resource.loader.api.client.MinecraftExtension;
import net.ornithemc.osl.resource.loader.api.resource.pack.ResourcePack;
import net.ornithemc.osl.resource.loader.api.resource.reload.ResourceReloader;
import net.ornithemc.osl.resource.loader.api.resource.repository.ResourcePackSummary;
import net.ornithemc.osl.resource.loader.impl.ClientResourcePacks;
import net.ornithemc.osl.resource.loader.impl.ResourceLoader;
import net.ornithemc.osl.resource.loader.impl.TexturePack;
import net.ornithemc.osl.resource.loader.impl.TexturePackDetector;
import net.ornithemc.osl.resource.loader.impl.access.GameOptionsAccess;
import net.ornithemc.osl.resource.loader.impl.resource.manager.SimpleReloadableResourceManager;
import net.ornithemc.osl.resource.loader.impl.resource.repository.BundledModResourcePacks;
import net.ornithemc.osl.resource.loader.impl.resource.repository.DirectoryPackSource;
import net.ornithemc.osl.resource.loader.impl.resource.repository.SimpleResourcePackRepository;

@Mixin(Minecraft.class)
public class MinecraftMixin implements MinecraftExtension {

@Shadow
private File gameDir;
@Shadow
private GameOptions options;
@Shadow
private TextureManager textureManager;
@Shadow
Expand All @@ -41,17 +56,29 @@ public class MinecraftMixin implements MinecraftExtension {
)
)
private void osl$resource_loader$initPackRepository(CallbackInfo ci) {
Path dir = this.gameDir.toPath();

this.packRepository = SimpleResourcePackRepository.client();

this.packRepository.reset();
this.packRepository.setCallbacks(null, null);

this.packRepository.addSource(new ClientResourcePacks());
this.packRepository.addSource(new BundledModResourcePacks());
// this.packRepository.addSource(new DirectoryResourcePackSource()); // TODO
this.packRepository.addSource(new DirectoryPackSource(dir.resolve("texturepacks"), new TexturePackDetector()));

this.packRepository.init();
this.packRepository.reload();

String skin = ((GameOptionsAccess) this.options).osl$resource_loader$getSkin();

for (ResourcePackSummary summary : this.packRepository.getAvailablePacks()) {
ResourcePack pack = summary.open();

if (pack instanceof TexturePack && pack.getName().equals(skin)) {
this.packRepository.setSelectedPacks(Collections.singleton(summary.getId()));
}
}
}

@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"mixins": [
],
"client": [
"client.GameOptionsMixin",
"client.MinecraftMixin",
"client.TextureManagerMixin"
],
"server": [
],
"injectors": {
"defaultRequire": 1
"defaultRequire": 1,
"maxShiftBy": 2
}
}
Loading
Loading