mirror of
https://github.com/chylex/Minecraft-Window-Title.git
synced 2025-09-16 02:32:17 +02:00
Compare commits
2 Commits
master
...
15b922a6f2
Author | SHA1 | Date | |
---|---|---|---|
![]() |
15b922a6f2 | ||
![]() |
c62d8658c4 |
@@ -1,25 +1,27 @@
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
CommonTokenData.register(new TokenProvider());
|
||||
Minecraft.getInstance().execute(this::updateTitle);
|
||||
}
|
||||
|
||||
|
||||
private void updateTitle() {
|
||||
Minecraft.getInstance().getWindow().setTitle(TitleParser.parse(config.getTitle()));
|
||||
}
|
||||
|
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 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,115 @@
|
||||
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 ImmutableSet<String> IGNORED_KEYS = ImmutableSet.of(
|
||||
"icon16",
|
||||
"icon32"
|
||||
);
|
||||
|
||||
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"));
|
||||
private static final Map<String, String> DEFAULTS;
|
||||
private static TitleConfig instance;
|
||||
|
||||
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) {
|
||||
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) {
|
||||
if (value.isEmpty()) {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
final char surrounding = value.charAt(0);
|
||||
final int length = value.length();
|
||||
|
||||
|
||||
if (value.charAt(length - 1) == surrounding) {
|
||||
value = value.substring(1, length - 1);
|
||||
|
||||
|
||||
if (surrounding == '"') {
|
||||
value = value.replace("\\\"", "\"").replace("\\\\", "\\");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
private final String title;
|
||||
|
||||
private TitleConfig(final String title) {
|
||||
private final Path icon;
|
||||
|
||||
private TitleConfig(final String title, final Path icon) {
|
||||
this.title = title;
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
|
||||
public static TitleConfig getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
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,8 +10,17 @@ 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();
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user