Skip to content

Commit

Permalink
Merge branch 'dev' into gfni
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Jan 18, 2024
2 parents 497d1ff + 17a17ed commit 2bb135c
Show file tree
Hide file tree
Showing 29 changed files with 770 additions and 191 deletions.
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ sourceSets {
runtimeClasspath += api.output
}
}

// only includes /src/desktop/java/net/caffeinemc/mods/sodium/desktop/LaunchWarn.java
// this source set is should be compiled with Java 8
launchWarn {
java {
srcDir "src/desktop/java"
include "**/LaunchWarn.java"
}
}
}

tasks.named('compileLaunchWarnJava') {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

tasks.register('apiJar', Jar) {
Expand All @@ -66,6 +80,8 @@ build.dependsOn remapApiJar
jar {
from sourceSets.api.output.classesDirs
from sourceSets.api.output.resourcesDir
from sourceSets.launchWarn.output.classesDirs // add launch warn to the main jar
manifest.attributes["Main-Class"] = "${project.main_class}"
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ fabric_version=0.91.1+1.20.3
mod_version=0.5.5
maven_group=me.jellysquid.mods
archives_base_name=sodium-fabric
main_class=net.caffeinemc.mods.sodium.desktop.LaunchWarn
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.caffeinemc.mods.sodium.desktop;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.net.URI;

/**
* Taken from
* https://github.com/IrisShaders/Iris/blob/6c880cd377d97ffd5de648ba4dfac7ea88897b4f/src/main/java/net/coderbot/iris/LaunchWarn.java
* and modified to fit Sodium. See Iris' license for more information.
*/
public class LaunchWarn {
public static void main(String[] args) {
String message = "You have tried to launch Sodium (a Minecraft mod) directly, but it is not an executable program or mod installer. You must install Fabric Loader for Minecraft, and place this file in your mods directory instead.\nIf this is your first time installing mods for Fabric Loader, click \"Help\" for a guide on how to do this.";
String fallback = "You have tried to launch Sodium (a Minecraft mod) directly, but it is not an executable program or mod installer. You must install Fabric Loader for Minecraft, and place this file in your mods directory instead.\nIf this is your first time installing mods for Fabric Loader, open \"https://github.com/CaffeineMC/sodium-fabric/wiki/Installation\" for a guide on how to do this.";
if (GraphicsEnvironment.isHeadless()) {
System.err.println(fallback);
} else {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ReflectiveOperationException | UnsupportedLookAndFeelException ignored) {
// Ignored
}

if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
int option = JOptionPane.showOptionDialog(null, message, "Sodium", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, new Object[] { "Help", "Cancel" }, JOptionPane.YES_OPTION);

if (option == JOptionPane.YES_OPTION) {
try {
Desktop.getDesktop().browse(URI.create("https://github.com/CaffeineMC/sodium-fabric/wiki/Installation"));
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// Fallback for Linux, etc users with no "default" browser
JOptionPane.showMessageDialog(null, fallback);
}
}

System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package me.jellysquid.mods.sodium.client.compatibility.checks;

import me.jellysquid.mods.sodium.client.platform.MessageBox;
import me.jellysquid.mods.sodium.client.platform.windows.api.Kernel32;
import me.jellysquid.mods.sodium.client.platform.windows.api.version.Version;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.Window;
import net.minecraft.util.WinNativeModuleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;

/**
* Utility class for determining whether the current process has been injected into or otherwise modified. This should
Expand Down Expand Up @@ -33,11 +41,120 @@ public static void checkModules() {

// RivaTuner hooks the wglCreateContext function, and leaves itself behind as a loaded module
if (Configuration.WIN32_RTSS_HOOKS && isModuleLoaded(modules, RTSS_HOOKS_MODULE_NAMES)) {
checkRTSSModules();
}
}

private static void checkRTSSModules() {
LOGGER.warn("RivaTuner Statistics Server (RTSS) has injected into the process! Attempting to apply workarounds for compatibility...");

String version = null;

try {
version = findRTSSModuleVersion();
} catch (Throwable t) {
LOGGER.warn("Exception thrown while reading file version", t);
}

if (version == null) {
LOGGER.warn("Could not determine version of RivaTuner Statistics Server");
} else {
LOGGER.info("Detected RivaTuner Statistics Server version: {}", version);
}

if (version == null || !isRTSSCompatible(version)) {
Window window = MinecraftClient.getInstance().getWindow();
MessageBox.showMessageBox(window, MessageBox.IconType.ERROR, "Sodium Renderer",
"You appear to be using an older version of RivaTuner Statistics Server (RTSS) which is not compatible with Sodium. " +
"You must either update to a newer version (7.3.4 and later) or close the RivaTuner Statistics Server application.\n\n" +
"For more information on how to solve this problem, click the 'Help' button.",
"https://github.com/CaffeineMC/sodium-fabric/wiki/Known-Issues#rtss-incompatible");

throw new RuntimeException("RivaTuner Statistics Server (RTSS) is not compatible with Sodium, " +
"see here for more details: https://github.com/CaffeineMC/sodium-fabric/wiki/Known-Issues#rtss-incompatible");
}
}

private static final Pattern RTSS_VERSION_PATTERN = Pattern.compile("^(?<x>\\d*), (?<y>\\d*), (?<z>\\d*), (?<w>\\d*)$");

private static boolean isRTSSCompatible(String version) {
var matcher = RTSS_VERSION_PATTERN.matcher(version);

if (!matcher.matches()) {
return false;
}

try {
int x = Integer.parseInt(matcher.group("x"));
int y = Integer.parseInt(matcher.group("y"));
int z = Integer.parseInt(matcher.group("z"));

// >=7.3.4
return x > 7 || (x == 7 && y > 3) || (x == 7 && y == 3 && z >= 4);
} catch (NumberFormatException e) {
LOGGER.warn("Invalid version string: {}", version);
}

return false;
}

private static String findRTSSModuleVersion() {
long module;

try {
module = Kernel32.getModuleHandleByNames(RTSS_HOOKS_MODULE_NAMES);
} catch (Throwable t) {
LOGGER.warn("Failed to locate module", t);
return null;
}

String moduleFileName;

try {
moduleFileName = Kernel32.getModuleFileName(module);
} catch (Throwable t) {
LOGGER.warn("Failed to get path of module", t);
return null;
}

var modulePath = Path.of(moduleFileName);
var moduleDirectory = modulePath.getParent();

LOGGER.info("Searching directory: {}", moduleDirectory);

var executablePath = moduleDirectory.resolve("RTSS.exe");

if (!Files.exists(executablePath)) {
LOGGER.warn("Could not find executable: {}", executablePath);
return null;
}

LOGGER.info("Parsing file: {}", executablePath);

var version = Version.getModuleFileVersion(executablePath.toAbsolutePath().toString());

if (version == null) {
LOGGER.warn("Couldn't find version structure");
return null;
}

var translation = version.queryEnglishTranslation();

if (translation == null) {
LOGGER.warn("Couldn't find suitable translation");
return null;
}

var fileVersion = version.queryValue("FileVersion", translation);

if (fileVersion == null) {
LOGGER.warn("Couldn't query file version");
return null;
}

return fileVersion;
}

private static boolean isModuleLoaded(List<WinNativeModuleUtil.NativeModule> modules, String[] names) {
for (var name : names) {
for (var module : modules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import me.jellysquid.mods.sodium.client.gui.console.Console;
import me.jellysquid.mods.sodium.client.gui.console.message.MessageLevel;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -44,10 +47,25 @@ public static void checkIfCoreShaderLoaded(ResourceManager manager) {
// Omit 'vanilla' and 'fabric' resource packs
if (!resourcePack.getName().equals("vanilla") && !resourcePack.getName().equals("fabric")) {
var resourcePackName = resourcePack.getName();
var ignoredShaders = determineIgnoredShaders(resourcePack);

resourcePack.findResources(ResourceType.CLIENT_RESOURCES, Identifier.DEFAULT_NAMESPACE, "shaders", (path, ignored) -> {
// Trim full shader file path to only contain the filename
var shaderName = path.getPath().substring(path.getPath().lastIndexOf('/') + 1);

// Check if the pack has already acknowledged the warnings in this file,
// in this case we report a different info log about the situation
if (ignoredShaders.contains(shaderName)) {
if (VSH_FSH_BLACKLIST.contains(shaderName)) {
LOGGER.info("Resource pack '{}' replaces core shader '{}' but indicates it can be ignored", resourcePackName, shaderName);
}

if (GLSL_BLACKLIST.contains(shaderName)) {
LOGGER.info("Resource pack '{}' replaces shader '{}' but indicates it can be ignored", resourcePackName, shaderName);
}
return;
}

if (VSH_FSH_BLACKLIST.contains(shaderName)) {

if (!detectedResourcePacks.containsKey(resourcePackName)) {
Expand All @@ -56,7 +74,7 @@ public static void checkIfCoreShaderLoaded(ResourceManager manager) {
detectedResourcePacks.replace(resourcePackName, MessageLevel.SEVERE);
}

LOGGER.error("Resource pack '" + resourcePackName + "' replaces core shader '" + shaderName + "'");
LOGGER.error("Resource pack '{}' replaces core shader '{}'", resourcePackName, shaderName);
}

if (GLSL_BLACKLIST.contains(shaderName)) {
Expand All @@ -65,7 +83,7 @@ public static void checkIfCoreShaderLoaded(ResourceManager manager) {
detectedResourcePacks.put(resourcePackName, MessageLevel.WARN);
}

LOGGER.warn("Resource pack '" + resourcePackName + "' replaces shader '" + shaderName + "'");
LOGGER.error("Resource pack '{}' replaces shader '{}'", resourcePackName, shaderName);

}
});
Expand Down Expand Up @@ -103,6 +121,27 @@ public static void checkIfCoreShaderLoaded(ResourceManager manager) {
}
}

/**
* Looks at a resource pack's metadata to find a list of shaders that can be gracefully
* ignored. This offers resource packs the ability to acknowledge they are shipping shaders
* which will not work with Sodium, but that Sodium can ignore.
*
* @param resourcePack The resource pack to fetch the ignored shaders of
* @return A list of shaders to ignore, this is the filename only without the path
*/
private static List<String> determineIgnoredShaders(ResourcePack resourcePack) {
var ignoredShaders = new ArrayList<String>();
try {
var meta = resourcePack.parseMetadata(SodiumResourcePackMetadata.SERIALIZER);
if (meta != null) {
ignoredShaders.addAll(meta.ignoredShaders());
}
} catch (IOException x) {
LOGGER.error("Failed to load pack.mcmeta file for resource pack '{}'", resourcePack.getName());
}
return ignoredShaders;
}

private static void showConsoleMessage(MutableText message, MessageLevel messageLevel) {
Console.instance().logMessage(messageLevel, message, 20.0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.jellysquid.mods.sodium.client.compatibility.checks;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.resource.metadata.ResourceMetadataSerializer;

import java.util.List;

/**
* Reads additional metadata for Sodium from a resource pack's `pack.mcmeta` file. This allows the
* resource pack author to specify which shaders from their pack are not usable with Sodium, but that
* the author is aware of and is fine with being ignored.
*/
public record SodiumResourcePackMetadata(List<String> ignoredShaders) {
public static final Codec<SodiumResourcePackMetadata> CODEC = RecordCodecBuilder.create((instance) ->
instance.group(Codec.STRING.listOf().fieldOf("ignored_shaders")
.forGetter(SodiumResourcePackMetadata::ignoredShaders))
.apply(instance, SodiumResourcePackMetadata::new)
);
public static final ResourceMetadataSerializer<SodiumResourcePackMetadata> SERIALIZER =
ResourceMetadataSerializer.fromCodec("sodium", CODEC);
}
Loading

0 comments on commit 2bb135c

Please sign in to comment.