mirror of
https://github.com/chylex/Minecraft-Window-Title.git
synced 2025-12-18 16:58:54 +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">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="gradleJvm" value="openjdk-21" />
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
|
|||||||
@@ -1,25 +1,26 @@
|
|||||||
package chylex.customwindowtitle.fabric;
|
package chylex.customwindowtitle.fabric;
|
||||||
|
|
||||||
|
import chylex.customwindowtitle.IconChanger;
|
||||||
import chylex.customwindowtitle.TitleConfig;
|
import chylex.customwindowtitle.TitleConfig;
|
||||||
import chylex.customwindowtitle.TitleParser;
|
import chylex.customwindowtitle.TitleParser;
|
||||||
import chylex.customwindowtitle.data.CommonTokenData;
|
import chylex.customwindowtitle.data.CommonTokenData;
|
||||||
|
import chylex.customwindowtitle.mixin.DisableVanillaTitle;
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
|
||||||
public class CustomWindowTitle implements ClientModInitializer {
|
public class CustomWindowTitle implements ClientModInitializer {
|
||||||
private final TitleConfig config;
|
private final TitleConfig config;
|
||||||
|
|
||||||
public CustomWindowTitle() {
|
public CustomWindowTitle() {
|
||||||
config = TitleConfig.read(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
|
config = TitleConfig.load(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
CommonTokenData.register(new TokenProvider());
|
CommonTokenData.register(new TokenProvider());
|
||||||
Minecraft.getInstance().execute(this::updateTitle);
|
Minecraft.getInstance().execute(this::updateTitle);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateTitle() {
|
private void updateTitle() {
|
||||||
Minecraft.getInstance().getWindow().setTitle(TitleParser.parse(config.getTitle()));
|
Minecraft.getInstance().getWindow().setTitle(TitleParser.parse(config.getTitle()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ public class CustomWindowTitle {
|
|||||||
|
|
||||||
private final TitleConfig config;
|
private final TitleConfig config;
|
||||||
|
|
||||||
public CustomWindowTitle(IEventBus eventBus) {
|
public CustomWindowTitle(final IEventBus eventBus) {
|
||||||
config = TitleConfig.read(FMLPaths.CONFIGDIR.get().toString());
|
config = TitleConfig.load(FMLPaths.CONFIGDIR.get().toString());
|
||||||
eventBus.addListener(this::onClientSetup);
|
eventBus.addListener(this::onClientSetup);
|
||||||
CommonTokenData.register(new TokenProvider());
|
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,86 +1,119 @@
|
|||||||
package chylex.customwindowtitle;
|
package chylex.customwindowtitle;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public final class TitleConfig {
|
public final class TitleConfig {
|
||||||
private static final ImmutableMap<String, String> DEFAULTS = ImmutableMap.<String, String>builder()
|
private static final Map<String, String> DEFAULTS;
|
||||||
.put("title", "Minecraft {mcversion}")
|
private static volatile TitleConfig instance = null;
|
||||||
.build();
|
|
||||||
|
static {
|
||||||
private static final ImmutableSet<String> IGNORED_KEYS = ImmutableSet.of(
|
final Map<String, String> defaults = new LinkedHashMap<>();
|
||||||
"icon16",
|
defaults.put("title", "Minecraft {mcversion}");
|
||||||
"icon32"
|
defaults.put("squareIcon", "");
|
||||||
);
|
DEFAULTS = Collections.unmodifiableMap(defaults);
|
||||||
|
|
||||||
public static TitleConfig read(final String folder) {
|
|
||||||
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 -> {
|
|
||||||
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)) {
|
|
||||||
throw new RuntimeException("CustomWindowTitle configuration has an invalid key: " + key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (final IOException e) {
|
|
||||||
throw new RuntimeException("CustomWindowTitle configuration error", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TitleConfig(config.get("title"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 -> {
|
||||||
|
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 {
|
||||||
|
throw new RuntimeException("CustomWindowTitle configuration has an invalid key: " + key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (final IOException e) {
|
||||||
|
throw new RuntimeException("CustomWindowTitle configuration error", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
private static String parseTrimmedValue(String value) {
|
||||||
if (value.isEmpty()) {
|
if (value.isEmpty()) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
final char surrounding = value.charAt(0);
|
final char surrounding = value.charAt(0);
|
||||||
final int length = value.length();
|
final int length = value.length();
|
||||||
|
|
||||||
if (value.charAt(length - 1) == surrounding) {
|
if (value.charAt(length - 1) == surrounding) {
|
||||||
value = value.substring(1, length - 1);
|
value = value.substring(1, length - 1);
|
||||||
|
|
||||||
if (surrounding == '"') {
|
if (surrounding == '"') {
|
||||||
value = value.replace("\\\"", "\"").replace("\\\\", "\\");
|
value = value.replace("\\\"", "\"").replace("\\\\", "\\");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String title;
|
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.title = title;
|
||||||
|
this.icon = icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasIcon() {
|
||||||
|
return icon != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path getIconPath() {
|
||||||
|
return icon;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package chylex.customwindowtitle.mixin;
|
package chylex.customwindowtitle.mixin;
|
||||||
|
|
||||||
|
import chylex.customwindowtitle.IconChanger;
|
||||||
|
import chylex.customwindowtitle.TitleConfig;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
@@ -8,6 +10,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|||||||
|
|
||||||
@Mixin(Minecraft.class)
|
@Mixin(Minecraft.class)
|
||||||
public final class DisableVanillaTitle {
|
public final class DisableVanillaTitle {
|
||||||
|
|
||||||
@Inject(method = "updateTitle()V", at = @At("HEAD"), cancellable = true)
|
@Inject(method = "updateTitle()V", at = @At("HEAD"), cancellable = true)
|
||||||
private void updateTitle(final CallbackInfo info) {
|
private void updateTitle(final CallbackInfo info) {
|
||||||
info.cancel();
|
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",
|
"refmap": "customwindowtitle.refmap.json",
|
||||||
"compatibilityLevel": "JAVA_17",
|
"compatibilityLevel": "JAVA_17",
|
||||||
"client": [
|
"client": [
|
||||||
"DisableVanillaTitle"
|
"DisableVanillaTitle",
|
||||||
|
"InitializeCustomIcon"
|
||||||
],
|
],
|
||||||
"injectors": {
|
"injectors": {
|
||||||
"defaultRequire": 1
|
"defaultRequire": 1
|
||||||
|
|||||||
Reference in New Issue
Block a user