Skip to content

Commit

Permalink
optifine mod metadata (borrowed from optibabric)
Browse files Browse the repository at this point in the history
  • Loading branch information
tildejustin committed Dec 16, 2023
1 parent 8b71328 commit 0d53f1b
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.modmuss50.optifabric.metadata;

import net.fabricmc.loader.api.metadata.ContactInformation;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

class OptifineContactInformation implements ContactInformation {
static final OptifineContactInformation INSTANCE = new OptifineContactInformation();
private static final Map<String, String> OPTIFINE_CONTACTS = new HashMap<>();

static {
OPTIFINE_CONTACTS.put("email", "optifinex@gmail.com");
OPTIFINE_CONTACTS.put("homepage", "https://optifine.net");
OPTIFINE_CONTACTS.put("issues", "https://github.com/sp614x/optifine/issues");
OPTIFINE_CONTACTS.put("sources", "https://github.com/sp614x/optifine");
}

private OptifineContactInformation() {
}

@Override
public Optional<String> get(String key) {
return Optional.ofNullable(OPTIFINE_CONTACTS.get(key));
}

@Override
public Map<String, String> asMap() {
return OPTIFINE_CONTACTS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package me.modmuss50.optifabric.metadata;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.impl.util.FileSystemUtil;

import java.io.IOException;
import java.nio.file.Path;
import java.util.*;

public class OptifineContainer implements ModContainer {
private final Path ofPath;
private final List<Path> rootPaths;
private final ModMetadata ofMetadata;

public OptifineContainer(Path ofPath, Version version) {
try {
this.ofPath = FileSystemUtil.getJarFileSystem(ofPath, true).get().getRootDirectories().iterator().next();
} catch (IOException e) {
throw new RuntimeException(e);
}
rootPaths = new ArrayList<>();
rootPaths.add(this.ofPath);
this.ofMetadata = new OptifineMetadata(version);
}

@Override
public ModMetadata getMetadata() {
return ofMetadata;
}

@Override
public List<Path> getRootPaths() {
return Collections.unmodifiableList(rootPaths);
}

@Override
public OptifineOrigin getOrigin() {
return this::getRootPaths;
}

@Override
public Optional<ModContainer> getContainingMod() {
return FabricLoader.getInstance().getModContainer("optifabric");
}

@Override
public Collection<ModContainer> getContainedMods() {
return new HashSet<>();
}

@Override
public Path getRootPath() {
return ofPath;
}

@Override
public Path getPath(String file) {
return findPath(file).orElse(null);
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/modmuss50/optifabric/metadata/OptifineIcon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.modmuss50.optifabric.metadata;

import me.modmuss50.optifabric.IOUtils;

import java.io.IOException;

@SuppressWarnings("DataFlowIssue")
public class OptifineIcon {
public static final String DATA;

static {
try {
DATA = new String(IOUtils.toByteArray(OptifineIcon.class.getResourceAsStream("/assets/optifabric/optifine_icon")));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private OptifineIcon() {
throw new UnsupportedOperationException();
}
}
106 changes: 106 additions & 0 deletions src/main/java/me/modmuss50/optifabric/metadata/OptifineMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package me.modmuss50.optifabric.metadata;

import me.modmuss50.optifabric.mod.OptifineVersion;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.metadata.*;

import java.util.*;

public class OptifineMetadata implements ModMetadata {
private final Version version;

OptifineMetadata(Version version) {
this.version = version;
}

@Override
public String getType() {
return "mcp";
}

@Override
public String getId() {
return "optifine";
}

@Override
public Collection<String> getProvides() {
return new ArrayList<>();
}

@Override
public Version getVersion() {
return version;
}

@Override
public ModEnvironment getEnvironment() {
return ModEnvironment.CLIENT;
}

@Override
public Collection<ModDependency> getDependencies() {
return new ArrayList<>();
}

@Override
public String getName() {
return OptifineVersion.version;
}

@Override
public String getDescription() {
return "Chasing the Minecraft Performance. This mod adds support for HD textures and a lot of options for better looks and performance. Doubling the FPS is common.";
}

@Override
public Collection<Person> getAuthors() {
HashSet<Person> authors = new HashSet<>();
authors.add(OptifinePerson.INSTANCE);
return authors;
}

@Override
public Collection<Person> getContributors() {
HashSet<Person> contributors = new HashSet<>();
contributors.add(OptifinePerson.INSTANCE);
return contributors;
}

@Override
public ContactInformation getContact() {
return OptifineContactInformation.INSTANCE;
}

@Override
public Collection<String> getLicense() {
HashSet<String> licenses = new HashSet<>();
licenses.add("All rights reserved");
return licenses;
}

@Override
public Optional<String> getIconPath(int size) {
return Optional.of("assets/optifine/icon.png");
}

@Override
public boolean containsCustomValue(String key) {
return false;
}

@Override
public CustomValue getCustomValue(String key) {
return null;
}

@Override
public Map<String, CustomValue> getCustomValues() {
return new HashMap<>();
}

@Override
public boolean containsCustomElement(String key) {
return false;
}
}
20 changes: 20 additions & 0 deletions src/main/java/me/modmuss50/optifabric/metadata/OptifineOrigin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.modmuss50.optifabric.metadata;

import net.fabricmc.loader.api.metadata.ModOrigin;

public interface OptifineOrigin extends ModOrigin {
@Override
default Kind getKind() {
return Kind.UNKNOWN;
}

@Override
default String getParentModId() {
return "optifabric";
}

@Override
default String getParentSubLocation() {
throw new UnsupportedOperationException("kind " + getKind().name() + " doesn't have a parent sub-location");
}
}
21 changes: 21 additions & 0 deletions src/main/java/me/modmuss50/optifabric/metadata/OptifinePerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.modmuss50.optifabric.metadata;

import net.fabricmc.loader.api.metadata.ContactInformation;
import net.fabricmc.loader.api.metadata.Person;

public class OptifinePerson implements Person {
static final OptifinePerson INSTANCE = new OptifinePerson();

private OptifinePerson() {
}

@Override
public String getName() {
return "sp614x";
}

@Override
public ContactInformation getContact() {
return ContactInformation.EMPTY;
}
}
23 changes: 16 additions & 7 deletions src/main/java/me/modmuss50/optifabric/mod/OptifabricSetup.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package me.modmuss50.optifabric.mod;

import com.chocohead.mm.api.ClassTinkerers;
import me.modmuss50.optifabric.Pair;
import com.chocohead.mm.api.*;
import me.modmuss50.optifabric.*;
import me.modmuss50.optifabric.metadata.*;
import me.modmuss50.optifabric.patcher.ClassCache;
import net.optifine.util.AddOpens;
import net.fabricmc.loader.api.*;
import net.fabricmc.loader.api.metadata.ModMetadata;
import org.spongepowered.asm.mixin.Mixins;
import org.spongepowered.asm.util.JavaVersion;
import net.fabricmc.loader.api.metadata.*;
import net.fabricmc.loader.impl.*;
import net.optifine.util.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.util.*;

import java.io.File;
import java.io.*;
import java.util.*;

@SuppressWarnings("unused")
Expand All @@ -33,6 +35,13 @@ public void run() {
injector.setup();

optifineRuntimeJar = runtime.left();

try {
//noinspection unchecked
((List<ModContainer>) (List<? extends ModContainer>) FabricLoaderImpl.InitHelper.get().getModsInternal()).add(new OptifineContainer(runtime.left().toPath(), Version.parse(OptifineVersion.version)));
} catch (VersionParsingException e) {
throw new RuntimeException(e);
}
} catch (Throwable e) {
if (!Optifabric.hasError()) {
OptifineVersion.jarType = OptifineVersion.JarType.INCOMPATIBLE;
Expand Down
49 changes: 44 additions & 5 deletions src/main/java/me/modmuss50/optifabric/mod/OptifineSetup.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package me.modmuss50.optifabric.mod;

import me.modmuss50.optifabric.*;
import me.modmuss50.optifabric.metadata.*;
import me.modmuss50.optifabric.patcher.ClassCache;
import me.modmuss50.optifabric.patcher.*;
import net.fabricmc.loader.api.*;
import net.fabricmc.loader.impl.launch.*;
import net.fabricmc.loader.impl.lib.tinyremapper.IMappingProvider;
import net.fabricmc.loader.impl.util.mappings.TinyRemapperMappingsHelper;
import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loader.impl.lib.tinyremapper.*;
import net.fabricmc.loader.impl.util.mappings.*;
import org.zeroturnaround.zip.*;

import java.io.File;
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.*;
import java.util.zip.*;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class OptifineSetup {
Expand Down Expand Up @@ -43,6 +46,41 @@ private static Path getLaunchMinecraftJar() {
}
}

private static void addIcon(File zipFile) {
File tempFile;
tempFile = new File(zipFile.getName().replace(".jar", ".tmp"));
tempFile.delete();

boolean renameOk = zipFile.renameTo(tempFile);
if (!renameOk)
throw new RuntimeException("Could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
byte[] buf = new byte[1024];

try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(tempFile.toPath()))) {
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
String name = entry.getName();
out.putNextEntry(new ZipEntry(name));
int len;
while ((len = zin.read(buf)) > 0)
out.write(buf, 0, len);
entry = zin.getNextEntry();
}
}
try (InputStream in = new ByteArrayInputStream(Base64.getDecoder().decode(OptifineIcon.DATA))) {
out.putNextEntry(new ZipEntry("assets/optifine/icon.png"));
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
out.closeEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
tempFile.delete();
}

public Pair<File, ClassCache> getRuntime() throws Throwable {
if (!workingDir.exists()) {
workingDir.mkdirs();
Expand Down Expand Up @@ -118,6 +156,7 @@ public Pair<File, ClassCache> getRuntime() throws Throwable {
RemapUtils.mapJar(lambdaFixJar.toPath(), jarOfTheFree.toPath(), rebuilder, this.getLibs());

this.remapOptifine(lambdaFixJar.toPath(), remappedJar.toPath());
OptifineSetup.addIcon(remappedJar);

classCache = PatchSplitter.generateClassCache(remappedJar, optifinePatches, modHash);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/optifabric/optifine_icon

Large diffs are not rendered by default.

0 comments on commit 0d53f1b

Please sign in to comment.