mirror of
https://github.com/chylex/Minecraft-Window-Title.git
synced 2025-12-18 07:58:56 +01:00
Compare commits
3 Commits
master
...
6854f05c05
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6854f05c05 | ||
|
|
1a9d5e65df | ||
|
|
c62d8658c4 |
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@@ -5,6 +5,7 @@
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="openjdk-21" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package chylex.customwindowtitle.fabric;
|
||||
|
||||
import chylex.customwindowtitle.IconChanger;
|
||||
import chylex.customwindowtitle.TitleConfig;
|
||||
import chylex.customwindowtitle.TitleParser;
|
||||
import chylex.customwindowtitle.data.CommonTokenData;
|
||||
import chylex.customwindowtitle.mixin.DisableVanillaTitle;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
public class CustomWindowTitle implements ClientModInitializer {
|
||||
private final TitleConfig config;
|
||||
|
||||
public CustomWindowTitle() {
|
||||
config = TitleConfig.read(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
|
||||
config = TitleConfig.load(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,8 +15,8 @@ public class CustomWindowTitle {
|
||||
|
||||
private final TitleConfig config;
|
||||
|
||||
public CustomWindowTitle(IEventBus eventBus) {
|
||||
config = TitleConfig.read(FMLPaths.CONFIGDIR.get().toString());
|
||||
public CustomWindowTitle(final IEventBus eventBus) {
|
||||
config = TitleConfig.load(FMLPaths.CONFIGDIR.get().toString());
|
||||
eventBus.addListener(this::onClientSetup);
|
||||
CommonTokenData.register(new TokenProvider());
|
||||
}
|
||||
|
||||
65
src/main/java/chylex/customwindowtitle/IconChanger.java
Normal file
65
src/main/java/chylex/customwindowtitle/IconChanger.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package chylex.customwindowtitle;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.lwjgl.glfw.GLFWImage;
|
||||
import org.lwjgl.stb.STBImage;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public final class IconChanger {
|
||||
|
||||
public static void setIcon(final Path iconPath) {
|
||||
final long windowHandle = Minecraft.getInstance().getWindow().getWindow();
|
||||
setWindowIcon(windowHandle, iconPath);
|
||||
}
|
||||
|
||||
private static void setWindowIcon(final long windowHandle, final Path iconPath) {
|
||||
try (final MemoryStack stack = MemoryStack.stackPush()) {
|
||||
final IntBuffer w = stack.mallocInt(1);
|
||||
final IntBuffer h = stack.mallocInt(1);
|
||||
final IntBuffer channels = stack.mallocInt(1);
|
||||
|
||||
final ByteBuffer icon = loadIcon(iconPath, w, h, channels);
|
||||
if (icon != null) {
|
||||
final GLFWImage glfwImage1 = GLFWImage.malloc();
|
||||
glfwImage1.set(w.get(0), h.get(0), icon);
|
||||
final GLFWImage glfwImage2 = GLFWImage.malloc();
|
||||
glfwImage2.set(w.get(0), h.get(0), icon);
|
||||
|
||||
final GLFWImage.Buffer icons = GLFWImage.malloc(2);
|
||||
icons.put(0, glfwImage1);
|
||||
icons.put(1, glfwImage2);
|
||||
|
||||
org.lwjgl.glfw.GLFW.glfwSetWindowIcon(windowHandle, icons);
|
||||
|
||||
icons.free();
|
||||
glfwImage1.free();
|
||||
glfwImage2.free();
|
||||
} else {
|
||||
System.err.println("Failed to load icon: " + iconPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ByteBuffer loadIcon(final Path path, final IntBuffer w, final IntBuffer h, final IntBuffer channels) {
|
||||
try (final InputStream inputStream = Files.newInputStream(path)) {
|
||||
final byte[] iconBytes = inputStream.readAllBytes();
|
||||
final ByteBuffer buffer = ByteBuffer.allocateDirect(iconBytes.length).put(iconBytes).flip();
|
||||
final ByteBuffer icon = STBImage.stbi_load_from_memory(buffer, w, h, channels, 4);
|
||||
if (icon == null) {
|
||||
System.err.println("Failed to load image from memory for: " + path + " - " + STBImage.stbi_failure_reason());
|
||||
}
|
||||
return icon;
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,60 @@
|
||||
package chylex.customwindowtitle;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class TitleConfig {
|
||||
private static final ImmutableMap<String, String> DEFAULTS = ImmutableMap.<String, String>builder()
|
||||
.put("title", "Minecraft {mcversion}")
|
||||
.build();
|
||||
private static final Map<String, String> DEFAULTS;
|
||||
private static volatile TitleConfig instance = null;
|
||||
|
||||
private static final ImmutableSet<String> IGNORED_KEYS = ImmutableSet.of(
|
||||
"icon16",
|
||||
"icon32"
|
||||
);
|
||||
static {
|
||||
final Map<String, String> defaults = new LinkedHashMap<>();
|
||||
defaults.put("title", "Minecraft {mcversion}");
|
||||
defaults.put("squareIcon", "");
|
||||
DEFAULTS = Collections.unmodifiableMap(defaults);
|
||||
}
|
||||
|
||||
public static TitleConfig read(final String folder) {
|
||||
public static TitleConfig getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static TitleConfig load(final String folder) {
|
||||
if (instance != null)
|
||||
throw new IllegalStateException("TitleConfig has already been loaded and cannot be loaded again.");
|
||||
|
||||
if (instance == null) {
|
||||
synchronized (TitleConfig.class) {
|
||||
if (instance == null) {
|
||||
final Path configFile = Paths.get(folder, "customwindowtitle-client.toml");
|
||||
final Map<String, String> config = new LinkedHashMap<>(DEFAULTS);
|
||||
|
||||
try {
|
||||
if (!Files.exists(configFile)) {
|
||||
Files.write(configFile, config.entrySet().stream().map(entry -> String.format("%s = '%s'", entry.getKey(), entry.getValue())).collect(Collectors.toList()), StandardCharsets.UTF_8);
|
||||
}
|
||||
else {
|
||||
Files.readAllLines(configFile, StandardCharsets.UTF_8).stream().map(String::trim).filter(line -> !line.isEmpty()).forEach(line -> {
|
||||
Files.write(configFile, config.entrySet().stream()
|
||||
.map(entry -> String.format("%s = '%s'", entry.getKey(), entry.getValue()))
|
||||
.collect(Collectors.toList()), StandardCharsets.UTF_8);
|
||||
} else {
|
||||
Files.readAllLines(configFile, StandardCharsets.UTF_8).stream()
|
||||
.map(String::trim)
|
||||
.filter(line -> !line.isEmpty())
|
||||
.forEach(line -> {
|
||||
final String[] split = line.split("=", 2);
|
||||
|
||||
if (split.length != 2) {
|
||||
throw new RuntimeException("CustomWindowTitle configuration has an invalid line: " + line);
|
||||
}
|
||||
|
||||
final String key = split[0].trim();
|
||||
final String value = parseTrimmedValue(split[1].trim());
|
||||
|
||||
if (config.containsKey(key)) {
|
||||
config.put(key, value);
|
||||
}
|
||||
else if (!IGNORED_KEYS.contains(key)) {
|
||||
} else {
|
||||
throw new RuntimeException("CustomWindowTitle configuration has an invalid key: " + key);
|
||||
}
|
||||
});
|
||||
@@ -52,7 +63,17 @@ public final class TitleConfig {
|
||||
throw new RuntimeException("CustomWindowTitle configuration error", e);
|
||||
}
|
||||
|
||||
return new TitleConfig(config.get("title"));
|
||||
final String iconPath = config.get("squareIcon");
|
||||
final Path pathIcon = iconPath.isEmpty() ? null : Paths.get(folder, iconPath);
|
||||
if (pathIcon != null && Files.notExists(pathIcon)) {
|
||||
throw new RuntimeException("CustomWindowTitle icon not found: " + pathIcon);
|
||||
}
|
||||
|
||||
instance = new TitleConfig(config.get("title"), pathIcon);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static String parseTrimmedValue(String value) {
|
||||
@@ -75,12 +96,24 @@ public final class TitleConfig {
|
||||
}
|
||||
|
||||
private final String title;
|
||||
private final Path icon;
|
||||
|
||||
private TitleConfig(final String title) {
|
||||
private TitleConfig(final String title, final Path icon) {
|
||||
this.title = title;
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public boolean hasIcon() {
|
||||
return icon != null;
|
||||
}
|
||||
|
||||
public Path getIconPath() {
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package chylex.customwindowtitle.mixin;
|
||||
|
||||
import chylex.customwindowtitle.IconChanger;
|
||||
import chylex.customwindowtitle.TitleConfig;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
@@ -8,6 +10,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(Minecraft.class)
|
||||
public final class DisableVanillaTitle {
|
||||
|
||||
@Inject(method = "updateTitle()V", at = @At("HEAD"), cancellable = true)
|
||||
private void updateTitle(final CallbackInfo info) {
|
||||
info.cancel();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package chylex.customwindowtitle.mixin;
|
||||
|
||||
import chylex.customwindowtitle.IconChanger;
|
||||
import chylex.customwindowtitle.TitleConfig;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(Minecraft.class)
|
||||
public final class InitializeCustomIcon {
|
||||
@Inject(method = "onResourceLoadFinished", at = @At("HEAD"))
|
||||
private void onFinishedLoading(final CallbackInfo callbackInfo) {
|
||||
final TitleConfig config = TitleConfig.getInstance();
|
||||
if (config != null && config.hasIcon()) {
|
||||
IconChanger.setIcon(config.getIconPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,8 @@
|
||||
"refmap": "customwindowtitle.refmap.json",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"client": [
|
||||
"DisableVanillaTitle"
|
||||
"DisableVanillaTitle",
|
||||
"InitializeCustomIcon"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
||||
Reference in New Issue
Block a user