mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
Compare commits
35
Commits
40ca99f853
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4effbc36f5 | ||
|
|
9fa0176bd2 | ||
|
|
8f314e1f5c | ||
|
|
30fde895b6 | ||
|
|
ed7fa13e7f | ||
|
|
d8a46a1b70 | ||
|
|
09de736269 | ||
|
|
d9075831e3 | ||
|
|
51a232d7ac | ||
|
|
29eb7609e9 | ||
|
|
8183eae9d1 | ||
|
|
6a045fd46f | ||
|
|
524d540c1a | ||
|
|
5f3721c294 | ||
|
|
d241addec7 | ||
|
|
54b476451c | ||
|
|
f6d8c7da2f | ||
|
|
7e27752ffc | ||
|
|
c8e23c5d1c | ||
|
|
a84b04877d | ||
|
|
70d8a854ea | ||
|
|
fdeec2a2f5 | ||
|
|
fea7089ff8 | ||
|
|
0599e48ebe | ||
|
|
0232fb0446 | ||
|
|
5c726f54e4 | ||
|
|
4b55871a8b | ||
|
|
cfb0ba4a48 | ||
|
|
715cb0755f | ||
|
|
b9ac9d5a92 | ||
|
|
b909a01fe0 | ||
|
|
867949967e | ||
|
|
c8fa1a4c64 | ||
|
|
c3dab97779 | ||
|
|
6c19985e52 |
No files matched your search
Generated
+1
-1
@@ -12,7 +12,7 @@
|
|||||||
<component name="FrameworkDetectionExcludesConfiguration">
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
<file type="web" url="file://$PROJECT_DIR$" />
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="zulu-25" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
Generated
+1
@@ -2,6 +2,7 @@
|
|||||||
<configuration default="false" name="Injector" type="Application" factoryName="Application">
|
<configuration default="false" name="Injector" type="Application" factoryName="Application">
|
||||||
<option name="MAIN_CLASS_NAME" value="cn.xiaozhou233.orangex.injector.Main" />
|
<option name="MAIN_CLASS_NAME" value="cn.xiaozhou233.orangex.injector.Main" />
|
||||||
<module name="OrangeX-Client.Injector.main" />
|
<module name="OrangeX-Client.Injector.main" />
|
||||||
|
<option name="VM_PARAMETERS" value="--enable-native-access=ALL-UNNAMED -Dfile.encoding=UTF-8" />
|
||||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/out" />
|
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/out" />
|
||||||
<extension name="coverage">
|
<extension name="coverage">
|
||||||
<pattern>
|
<pattern>
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
|
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
|
||||||
<option name="MAIN_CLASS_NAME" value="Start" />
|
<option name="MAIN_CLASS_NAME" value="Start" />
|
||||||
<module name="OrangeX-Client.Minecraft1_8_8.main" />
|
<module name="OrangeX-Client.Minecraft1_8_8.main" />
|
||||||
<option name="VM_PARAMETERS" value="-Djava.library.path=versions\1.8.8\1.8.8-natives -Dorangex.dev=true" />
|
<option name="VM_PARAMETERS" value="-Djava.library.path=versions\1.8.8\1.8.8-natives -Dorangex.dev=true -Dorangex.debugInput=true" />
|
||||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/Minecraft1.8.8/runtime" />
|
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/Minecraft1.8.8/runtime" />
|
||||||
<method v="2">
|
<method v="2">
|
||||||
<option name="Make" enabled="true" />
|
<option name="Make" enabled="true" />
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
|
|
||||||
|
// Source: https://mvnrepository.com/artifact/com.formdev/flatlaf
|
||||||
|
implementation 'com.formdev:flatlaf:3.8'
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
@@ -24,6 +27,12 @@ tasks.named('jar') {
|
|||||||
manifest {
|
manifest {
|
||||||
attributes 'Main-Class': 'cn.xiaozhou233.orangex.injector.Main'
|
attributes 'Main-Class': 'cn.xiaozhou233.orangex.injector.Main'
|
||||||
}
|
}
|
||||||
|
// Bundle runtime dependencies (FlatLaf) so injector.jar is self-contained.
|
||||||
|
from {
|
||||||
|
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
|
||||||
|
}
|
||||||
|
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||||
|
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType(JavaCompile).configureEach {
|
tasks.withType(JavaCompile).configureEach {
|
||||||
|
|||||||
@@ -0,0 +1,310 @@
|
|||||||
|
package cn.xiaozhou233.orangex.injector;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.juiceagent.injector.Injector;
|
||||||
|
import cn.xiaozhou233.juiceagent.injector.WindowInfo;
|
||||||
|
import com.formdev.flatlaf.util.UIScale;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injector front-end: lists the running Minecraft windows, optionally accepts a
|
||||||
|
* PID typed by hand, and runs {@link Injector#inject(int, String, String)} on a
|
||||||
|
* background thread.
|
||||||
|
*/
|
||||||
|
public class InjectorWindow extends JFrame {
|
||||||
|
|
||||||
|
private static final String[] WINDOW_TITLES = {
|
||||||
|
"Minecraft", "minecraft", "1.8.9", "Badlion", "Lunar"
|
||||||
|
};
|
||||||
|
private static final String[] IGNORED_TITLES = {
|
||||||
|
"Badlion Client", "Badlion Chat", "Home - Lunar Client"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final Color ACCENT = new Color(0xFF8A00);
|
||||||
|
private static final Color SUCCESS = new Color(0x66BB6A);
|
||||||
|
private static final Color ERROR = new Color(0xEF5350);
|
||||||
|
private static final Color MUTED = new Color(0xAAAAAA);
|
||||||
|
|
||||||
|
private final String agentPath;
|
||||||
|
private final String configDir;
|
||||||
|
|
||||||
|
private final DefaultComboBoxModel<ProcessEntry> processModel = new DefaultComboBoxModel<>();
|
||||||
|
private final JComboBox<ProcessEntry> processBox = new JComboBox<>(processModel);
|
||||||
|
private final JButton refreshButton = new JButton("Refresh");
|
||||||
|
private final JCheckBox customPidCheck = new JCheckBox("Use custom PID");
|
||||||
|
private final JTextField pidField = new JTextField();
|
||||||
|
private final JButton injectButton = new JButton("Inject");
|
||||||
|
private final JLabel statusLabel = new JLabel("Ready", SwingConstants.CENTER);
|
||||||
|
|
||||||
|
private boolean busy;
|
||||||
|
|
||||||
|
public InjectorWindow(String agentPath, String configDir) {
|
||||||
|
super("OrangeX Injector");
|
||||||
|
this.agentPath = agentPath;
|
||||||
|
this.configDir = configDir;
|
||||||
|
|
||||||
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
setResizable(false);
|
||||||
|
setContentPane(buildContent());
|
||||||
|
setSize(UIScale.scale(470), UIScale.scale(340));
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
getRootPane().setDefaultButton(injectButton);
|
||||||
|
|
||||||
|
refreshButton.addActionListener(event -> refreshProcesses());
|
||||||
|
customPidCheck.addActionListener(event -> {
|
||||||
|
boolean custom = customPidCheck.isSelected();
|
||||||
|
pidField.setVisible(custom);
|
||||||
|
revalidate();
|
||||||
|
repaint();
|
||||||
|
if (custom) {
|
||||||
|
pidField.requestFocusInWindow();
|
||||||
|
}
|
||||||
|
updateControls();
|
||||||
|
});
|
||||||
|
injectButton.addActionListener(event -> inject());
|
||||||
|
|
||||||
|
refreshProcesses();
|
||||||
|
}
|
||||||
|
|
||||||
|
private JPanel buildContent() {
|
||||||
|
JPanel root = new JPanel(new GridBagLayout());
|
||||||
|
root.setBorder(new EmptyBorder(
|
||||||
|
UIScale.scale(20), UIScale.scale(26),
|
||||||
|
UIScale.scale(16), UIScale.scale(26)));
|
||||||
|
|
||||||
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
|
gbc.gridx = 0;
|
||||||
|
gbc.weightx = 1.0;
|
||||||
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
|
||||||
|
int row = 0;
|
||||||
|
|
||||||
|
JLabel title = new JLabel(
|
||||||
|
"<html><font color='#FF8A00'><b>OrangeX</b></font> Injector</html>",
|
||||||
|
SwingConstants.CENTER);
|
||||||
|
title.setFont(title.getFont().deriveFont(Font.BOLD, UIScale.scale(26f)));
|
||||||
|
gbc.gridy = row++;
|
||||||
|
gbc.insets = UIScale.scale(new Insets(0, 0, 20, 0));
|
||||||
|
root.add(title, gbc);
|
||||||
|
|
||||||
|
processBox.setPreferredSize(new Dimension(UIScale.scale(200), UIScale.scale(34)));
|
||||||
|
JPanel processRow = new JPanel(new BorderLayout(UIScale.scale(8), 0));
|
||||||
|
processRow.add(processBox, BorderLayout.CENTER);
|
||||||
|
processRow.add(refreshButton, BorderLayout.EAST);
|
||||||
|
gbc.gridy = row++;
|
||||||
|
gbc.insets = UIScale.scale(new Insets(0, 0, 10, 0));
|
||||||
|
root.add(processRow, gbc);
|
||||||
|
|
||||||
|
gbc.gridy = row++;
|
||||||
|
gbc.insets = UIScale.scale(new Insets(0, 0, 8, 0));
|
||||||
|
root.add(customPidCheck, gbc);
|
||||||
|
|
||||||
|
pidField.setPreferredSize(new Dimension(0, UIScale.scale(34)));
|
||||||
|
pidField.putClientProperty("JTextField.placeholderText", "Enter a process ID");
|
||||||
|
pidField.setVisible(false);
|
||||||
|
gbc.gridy = row++;
|
||||||
|
gbc.insets = UIScale.scale(new Insets(0, 0, 10, 0));
|
||||||
|
root.add(pidField, gbc);
|
||||||
|
|
||||||
|
injectButton.setPreferredSize(new Dimension(0, UIScale.scale(40)));
|
||||||
|
injectButton.setFont(injectButton.getFont().deriveFont(
|
||||||
|
Font.BOLD, UIScale.scale(15f)));
|
||||||
|
injectButton.setBackground(ACCENT);
|
||||||
|
injectButton.setForeground(Color.WHITE);
|
||||||
|
injectButton.putClientProperty("JButton.buttonType", "roundRect");
|
||||||
|
gbc.gridy = row++;
|
||||||
|
gbc.insets = UIScale.scale(new Insets(10, 0, 12, 0));
|
||||||
|
root.add(injectButton, gbc);
|
||||||
|
|
||||||
|
statusLabel.setFont(statusLabel.getFont().deriveFont(UIScale.scale(12f)));
|
||||||
|
statusLabel.setForeground(MUTED);
|
||||||
|
gbc.gridy = row++;
|
||||||
|
gbc.insets = new Insets(0, 0, 0, 0);
|
||||||
|
root.add(statusLabel, gbc);
|
||||||
|
|
||||||
|
// Keeps the content aligned to the top when the PID field is hidden.
|
||||||
|
gbc.gridy = row;
|
||||||
|
gbc.weighty = 1.0;
|
||||||
|
root.add(Box.createVerticalGlue(), gbc);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rescans the open windows and fills the selection box. Errors are shown
|
||||||
|
* in the status label instead of terminating the window.
|
||||||
|
*/
|
||||||
|
public void refreshProcesses() {
|
||||||
|
processModel.removeAllElements();
|
||||||
|
|
||||||
|
List<ProcessEntry> processes;
|
||||||
|
try {
|
||||||
|
processes = scanProcesses();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throwable.printStackTrace();
|
||||||
|
setStatus("Failed to scan windows: " + throwable.getClass().getSimpleName(), ERROR);
|
||||||
|
updateControls();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ProcessEntry process : processes) {
|
||||||
|
processModel.addElement(process);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (processes.isEmpty()) {
|
||||||
|
setStatus("No Minecraft process found", ERROR);
|
||||||
|
} else {
|
||||||
|
setStatus(processes.size() + " Minecraft process(es) found", MUTED);
|
||||||
|
}
|
||||||
|
updateControls();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ProcessEntry> scanProcesses() {
|
||||||
|
Map<Integer, ProcessEntry> found = new LinkedHashMap<>();
|
||||||
|
for (String title : WINDOW_TITLES) {
|
||||||
|
WindowInfo[] windows = Injector.findWindowsByTitle(title);
|
||||||
|
if (windows == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (WindowInfo window : windows) {
|
||||||
|
if (window == null || window.title == null || found.containsKey(window.pid)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isIgnored(window.title)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
found.put(window.pid, new ProcessEntry(window.pid, window.title));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ArrayList<>(found.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isIgnored(String title) {
|
||||||
|
for (String ignored : IGNORED_TITLES) {
|
||||||
|
if (title.contains(ignored)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void inject() {
|
||||||
|
Integer pid = selectedPid();
|
||||||
|
if (pid == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
busy = true;
|
||||||
|
updateControls();
|
||||||
|
setStatus("Injecting into PID " + pid + "...", MUTED);
|
||||||
|
|
||||||
|
new SwingWorker<Boolean, Void>() {
|
||||||
|
@Override
|
||||||
|
protected Boolean doInBackground() {
|
||||||
|
return Injector.inject(pid, agentPath, configDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void done() {
|
||||||
|
busy = false;
|
||||||
|
updateControls();
|
||||||
|
try {
|
||||||
|
if (Boolean.TRUE.equals(get())) {
|
||||||
|
setStatus("Injection succeeded (PID " + pid + ")", SUCCESS);
|
||||||
|
} else {
|
||||||
|
setStatus("Injection failed (PID " + pid + ")", ERROR);
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
InjectorWindow.this,
|
||||||
|
"Injector.inject returned false for PID " + pid + ".",
|
||||||
|
"Injection failed",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException exception) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
setStatus("Injection interrupted", ERROR);
|
||||||
|
} catch (ExecutionException exception) {
|
||||||
|
Throwable cause = exception.getCause() == null
|
||||||
|
? exception : exception.getCause();
|
||||||
|
cause.printStackTrace();
|
||||||
|
setStatus("Injection failed (PID " + pid + ")", ERROR);
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
InjectorWindow.this,
|
||||||
|
cause.toString(),
|
||||||
|
"Injection failed",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Integer selectedPid() {
|
||||||
|
if (customPidCheck.isSelected()) {
|
||||||
|
String text = pidField.getText().trim();
|
||||||
|
if (text.isEmpty()) {
|
||||||
|
showError("Enter a PID first, or untick \"Use custom PID\".");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int pid = Integer.parseInt(text);
|
||||||
|
if (pid <= 0) {
|
||||||
|
throw new NumberFormatException("PID must be positive");
|
||||||
|
}
|
||||||
|
return pid;
|
||||||
|
} catch (NumberFormatException exception) {
|
||||||
|
showError("PID must be a positive integer.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ProcessEntry selected = (ProcessEntry) processBox.getSelectedItem();
|
||||||
|
if (selected == null) {
|
||||||
|
showError("No Minecraft process selected. Start the game or use a custom PID.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return selected.pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateControls() {
|
||||||
|
boolean custom = customPidCheck.isSelected();
|
||||||
|
boolean hasProcess = processModel.getSize() > 0;
|
||||||
|
|
||||||
|
processBox.setEnabled(!busy && !custom && hasProcess);
|
||||||
|
refreshButton.setEnabled(!busy && !custom);
|
||||||
|
customPidCheck.setEnabled(!busy);
|
||||||
|
pidField.setEnabled(!busy);
|
||||||
|
injectButton.setEnabled(!busy && (custom || hasProcess));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setStatus(String text, Color color) {
|
||||||
|
statusLabel.setText(text);
|
||||||
|
statusLabel.setForeground(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showError(String message) {
|
||||||
|
setStatus(message, ERROR);
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
this, message, "OrangeX Injector", JOptionPane.WARNING_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class ProcessEntry {
|
||||||
|
private final int pid;
|
||||||
|
private final String title;
|
||||||
|
|
||||||
|
private ProcessEntry(int pid, String title) {
|
||||||
|
this.pid = pid;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return title + " (PID: " + pid + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,95 +1,142 @@
|
|||||||
package cn.xiaozhou233.orangex.injector;
|
package cn.xiaozhou233.orangex.injector;
|
||||||
|
|
||||||
import cn.xiaozhou233.juiceagent.injector.Injector;
|
import com.formdev.flatlaf.FlatDarkLaf;
|
||||||
import cn.xiaozhou233.juiceagent.injector.WindowInfo;
|
import com.formdev.flatlaf.FlatLaf;
|
||||||
|
import com.formdev.flatlaf.util.UIScale;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.UIManager;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.GraphicsConfiguration;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Toolkit;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileDescriptor;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.io.PrintStream;
|
||||||
import java.util.Arrays;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static String orangexPath = new File(System.getProperty("user.home") + "/.orangex").getAbsolutePath();
|
public static final String orangexPath =
|
||||||
private static int pid = -1;
|
new File(System.getProperty("user.home"), ".orangex").getAbsolutePath();
|
||||||
|
public static final String agentPath =
|
||||||
|
new File(orangexPath, "libloader.dll").getAbsolutePath();
|
||||||
|
public static final String configDir = orangexPath;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
setupConsole();
|
||||||
|
|
||||||
System.out.println("================");
|
System.out.println("================");
|
||||||
System.out.println("OrangeX Injector v1.0.0");
|
System.out.println("OrangeX Injector v1.0.0");
|
||||||
System.out.println("OrangeX Path: " + orangexPath);
|
System.out.println("OrangeX Path: " + orangexPath);
|
||||||
System.out.println("================");
|
System.out.println("================");
|
||||||
|
|
||||||
// Unzip injection
|
extractInjection();
|
||||||
|
if (!loadNativeLibrary()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
installLookAndFeel();
|
||||||
|
InjectorWindow window = new InjectorWindow(agentPath, configDir);
|
||||||
|
window.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setupConsole() {
|
||||||
|
try {
|
||||||
|
System.setOut(new PrintStream(
|
||||||
|
new FileOutputStream(FileDescriptor.out),
|
||||||
|
true,
|
||||||
|
StandardCharsets.UTF_8.name()
|
||||||
|
));
|
||||||
|
System.setErr(new PrintStream(
|
||||||
|
new FileOutputStream(FileDescriptor.err),
|
||||||
|
true,
|
||||||
|
StandardCharsets.UTF_8.name()
|
||||||
|
));
|
||||||
|
} catch (Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void extractInjection() {
|
||||||
File zipFile = new File("./injection.zip");
|
File zipFile = new File("./injection.zip");
|
||||||
if (!zipFile.exists()) {
|
if (!zipFile.exists()) {
|
||||||
System.err.println("injection.zip not found!");
|
System.err.println("injection.zip not found!");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (InputStream zipStream = new FileInputStream(zipFile)) {
|
try (InputStream zipStream = new FileInputStream(zipFile)) {
|
||||||
FileUtils.unzip(zipStream, new File(orangexPath));
|
FileUtils.unzip(zipStream, new File(orangexPath));
|
||||||
System.out.println("Done!");
|
System.out.println("Extracted injection.zip to " + orangexPath);
|
||||||
} catch (IOException e) {
|
} catch (IOException exception) {
|
||||||
e.printStackTrace();
|
System.err.println("[ERROR] Failed to extract injection.zip: " + exception.getMessage());
|
||||||
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load libinject native library
|
|
||||||
System.load(new File(orangexPath + "/libinject.dll").getAbsolutePath());
|
|
||||||
System.out.println("Loaded libinject.dll");
|
|
||||||
|
|
||||||
// Find Minecraft
|
|
||||||
ArrayList<Integer> pids = new ArrayList<>();
|
|
||||||
ArrayList<String> windowTitles = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
String[] titles = {"Minecraft", "minecraft", "1.8.9", "Badlion", "Lunar"};
|
|
||||||
String[] ignoreTitles = {"Badlion Client", "Badlion Chat", "Home - Lunar Client"};
|
|
||||||
for (String title : titles) {
|
|
||||||
WindowInfo[] list = Injector.findWindowsByTitle(title);
|
|
||||||
if (list == null || list.length == 0) {
|
|
||||||
} else {
|
|
||||||
for (WindowInfo w : list) {
|
|
||||||
if (pids.contains(w.pid))
|
|
||||||
continue;
|
|
||||||
if (Arrays.stream(ignoreTitles).anyMatch(w.title::contains))
|
|
||||||
continue;
|
|
||||||
pids.add(w.pid);
|
|
||||||
windowTitles.add(w.title);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (UnsatisfiedLinkError ule) {
|
|
||||||
throw new RuntimeException("Native method findWindowsByTitle not available", ule);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
System.out.println("[ERROR] Error while calling native findWindowsByTitle: " + ex.getMessage());
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print results
|
|
||||||
System.out.println("=======================");
|
|
||||||
for (int i = 0; i < pids.size(); i++) {
|
|
||||||
System.out.printf("[ %d ] %s (PID: %d)%n", i + 1, windowTitles.get(i), pids.get(i));
|
|
||||||
}
|
|
||||||
System.out.println("=======================");
|
|
||||||
if (pids.isEmpty()) {
|
|
||||||
System.err.println("[ERROR] No Minecraft process found!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Input the sequence number
|
|
||||||
try (Scanner scanner = new Scanner(System.in)) {
|
|
||||||
System.out.printf("[INPUT] Select (1-%d): ", pids.size());
|
|
||||||
int index = scanner.nextInt();
|
|
||||||
if (index < 1 || index > pids.size()) {
|
|
||||||
throw new RuntimeException("Invalid selection: " + index);
|
|
||||||
}
|
|
||||||
pid = pids.get(index - 1);
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
throw e;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Invalid input", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
String agentPath = orangexPath + "/libloader.dll";
|
|
||||||
String configDir = orangexPath;
|
|
||||||
Injector.inject(pid, agentPath, configDir);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private static boolean loadNativeLibrary() {
|
||||||
|
try {
|
||||||
|
System.load(new File(orangexPath, "libinject.dll").getAbsolutePath());
|
||||||
|
System.out.println("Loaded libinject.dll");
|
||||||
|
return true;
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throwable.printStackTrace();
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
null,
|
||||||
|
"Failed to load libinject.dll:\n" + throwable,
|
||||||
|
"OrangeX Injector",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void installLookAndFeel() {
|
||||||
|
try {
|
||||||
|
FlatDarkLaf.setup();
|
||||||
|
UIManager.put("Component.accentColor", new Color(0xFF8A00));
|
||||||
|
UIManager.put("Component.arc", 10);
|
||||||
|
UIManager.put("Button.arc", 12);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
System.out.println("[WARN] FlatLaf is not available; using the default look and feel");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
applyWindowScale();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Large monitors are often driven at 100% OS scaling, which makes Swing
|
||||||
|
* UIs look tiny. Compute the scale the JRE already applies and add a zoom
|
||||||
|
* factor on top until the UI reaches a comfortable physical size.
|
||||||
|
*/
|
||||||
|
private static void applyWindowScale() {
|
||||||
|
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
if (screen.height < 1120) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float target = screen.height >= 1800 ? 2.0f : 1.5f;
|
||||||
|
GraphicsConfiguration configuration = GraphicsEnvironment
|
||||||
|
.getLocalGraphicsEnvironment()
|
||||||
|
.getDefaultScreenDevice()
|
||||||
|
.getDefaultConfiguration();
|
||||||
|
|
||||||
|
double effective = UIScale.getSystemScaleFactor(configuration)
|
||||||
|
* UIScale.getUserScaleFactor();
|
||||||
|
if (effective >= target) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float zoom = (float) (target / effective);
|
||||||
|
if (UIScale.setZoomFactor(zoom)) {
|
||||||
|
FlatLaf.updateUI();
|
||||||
|
System.out.println("[OrangeX] UI scale " + target + "x (zoom " + zoom + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id("com.gradleup.shadow") version "8.3.11"
|
id("com.gradleup.shadow") version "9.6.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'cn.xiaozhou233.orangex.injector'
|
group = 'cn.xiaozhou233.orangex.injector'
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ public class Loader {
|
|||||||
orangexFile = new File(OrangeXPath + "/OrangeX.jar");
|
orangexFile = new File(OrangeXPath + "/OrangeX.jar");
|
||||||
}
|
}
|
||||||
System.out.println("OrangeX Injection: " + orangexFile.getAbsolutePath());
|
System.out.println("OrangeX Injection: " + orangexFile.getAbsolutePath());
|
||||||
|
// javassist's ClassPool cannot always discover this jar through the
|
||||||
|
// game ClassLoader, so expose the path to OrangeX explicitly.
|
||||||
|
System.setProperty("orangex.jar", orangexFile.getAbsolutePath());
|
||||||
addToGameClassLoader(orangexFile, minecraftClassLoader);
|
addToGameClassLoader(orangexFile, minecraftClassLoader);
|
||||||
|
|
||||||
// Start OrangeX
|
// Start OrangeX
|
||||||
|
|||||||
+57
-1
@@ -1,6 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id("com.gradleup.shadow") version "8.3.11"
|
id("com.gradleup.shadow") version "9.6.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'cn.xiaozhou233.orangex'
|
group = 'cn.xiaozhou233.orangex'
|
||||||
@@ -17,13 +17,69 @@ dependencies {
|
|||||||
|
|
||||||
// Source: https://mvnrepository.com/artifact/com.google.code.gson/gson
|
// Source: https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||||
implementation 'com.google.code.gson:gson:2.15.0'
|
implementation 'com.google.code.gson:gson:2.15.0'
|
||||||
|
|
||||||
|
// Source: https://mvnrepository.com/artifact/org.javassist/javassist
|
||||||
|
implementation 'org.javassist:javassist:3.33.0-GA'
|
||||||
|
|
||||||
|
// Source: https://mvnrepository.com/artifact/org.greenrobot/eventbus
|
||||||
|
implementation 'org.greenrobot:eventbus:3.2.0'
|
||||||
|
|
||||||
|
// Legacy OpenGL entry points for the 1.8.9 font backend. This is compile
|
||||||
|
// only: the 1.8.9 runtime provides its own LWJGL 2, and the 26.2 backend
|
||||||
|
// never loads these classes.
|
||||||
|
compileOnly files('../libraries/lwjgl-2.9.4-nightly-20150209.jar')
|
||||||
|
|
||||||
|
compileOnly files("../libraries/minecraft/Minecraft26.2.jar")
|
||||||
|
compileOnly files("../libraries/minecraft/com/mojang/brigadier/1.3.10/brigadier-1.3.10.jar")
|
||||||
|
|
||||||
|
// JuiceAgent is provided at runtime by the agent (see JuiceAgentBridge).
|
||||||
|
// Do NOT bundle it: Fabric's KnotClassLoader would load a second copy of
|
||||||
|
// the API whose native methods are not registered.
|
||||||
|
}
|
||||||
|
|
||||||
|
// The 26.2 input classes import game classes that are records / class-file
|
||||||
|
// version 69, so they cannot be compiled with --release 8. Keep them in a
|
||||||
|
// separate source set compiled by JDK 25, but still emit Java 8 bytecode so a
|
||||||
|
// 1.8.9 JVM can load the rest of the jar.
|
||||||
|
sourceSets {
|
||||||
|
main {
|
||||||
|
java {
|
||||||
|
exclude 'cn/xiaozhou233/orangex/input/v26_2/**'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v26_2 {
|
||||||
|
java {
|
||||||
|
srcDir 'src/main/java'
|
||||||
|
include 'cn/xiaozhou233/orangex/input/v26_2/**'
|
||||||
|
}
|
||||||
|
compileClasspath += sourceSets.main.output
|
||||||
|
compileClasspath += configurations.compileClasspath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named('compileV26_2Java', JavaCompile).configure {
|
||||||
|
javaCompiler = javaToolchains.compilerFor {
|
||||||
|
languageVersion = JavaLanguageVersion.of(25)
|
||||||
|
}
|
||||||
|
// No --release: the JDK 25 platform classpath is needed to resolve records
|
||||||
|
// from Minecraft26.2.jar, while -source/-target 8 keeps the output loadable
|
||||||
|
// by Java 8.
|
||||||
|
options.release = null
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('shadowJar') {
|
tasks.named('shadowJar') {
|
||||||
destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp')
|
destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp')
|
||||||
archiveFileName = "${project.name}.jar"
|
archiveFileName = "${project.name}.jar"
|
||||||
|
dependsOn tasks.named('v26_2Classes')
|
||||||
|
from sourceSets.v26_2.output
|
||||||
}
|
}
|
||||||
|
|
||||||
shadowJar {
|
shadowJar {
|
||||||
relocate 'com.google.gson', 'cn.xiaozhou233.orangex.shaded.gson'
|
relocate 'com.google.gson', 'cn.xiaozhou233.orangex.shaded.gson'
|
||||||
|
// Javassist must live in OrangeX's own namespace: Forge's
|
||||||
|
// LaunchClassLoader delegates the "javassist." package to its parent,
|
||||||
|
// which cannot see the injected jar.
|
||||||
|
relocate 'javassist', 'cn.xiaozhou233.orangex.shaded.javassist'
|
||||||
|
relocate 'org.greenrobot.eventbus', 'cn.xiaozhou233.orangex.shaded.eventbus'
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,54 @@
|
|||||||
package cn.xiaozhou233.orangex;
|
package cn.xiaozhou233.orangex;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.config.ConfigManager;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventKey;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
|
import cn.xiaozhou233.orangex.hook.HookManager;
|
||||||
|
import cn.xiaozhou233.orangex.input.InputController;
|
||||||
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import cn.xiaozhou233.orangex.render.FontDebugOverlay;
|
||||||
|
import cn.xiaozhou233.orangex.render.Render2DManager;
|
||||||
import cn.xiaozhou233.orangex.runtime.RuntimeDetector;
|
import cn.xiaozhou233.orangex.runtime.RuntimeDetector;
|
||||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
import cn.xiaozhou233.orangex.wrapper.MinecraftFactory;
|
import cn.xiaozhou233.orangex.wrapper.MinecraftFactory;
|
||||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Player;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.WorldView;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public class OrangeX {
|
public class OrangeX {
|
||||||
@Getter
|
public static String OrangeXPath = new File(System.getProperty("user.home")+"/.orangex").getAbsolutePath();
|
||||||
private static final OrangeX instance = new OrangeX();
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private static final OrangeX instance = new OrangeX();
|
||||||
|
@Getter
|
||||||
|
private final EventBus eventBus = EventBus.builder()
|
||||||
|
.logNoSubscriberMessages(false)
|
||||||
|
.logSubscriberExceptions(false)
|
||||||
|
.sendNoSubscriberEvent(false)
|
||||||
|
.sendSubscriberExceptionEvent(false)
|
||||||
|
.build();
|
||||||
@Getter
|
@Getter
|
||||||
private RuntimeInfo runtimeInfo;
|
private RuntimeInfo runtimeInfo;
|
||||||
|
@Getter
|
||||||
|
private Render2DManager render2DManager;
|
||||||
|
@Getter
|
||||||
|
private ModuleManager moduleManager = new ModuleManager();
|
||||||
|
@Getter
|
||||||
|
private ConfigManager configManager;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Minecraft minecraft;
|
||||||
|
@Getter
|
||||||
|
private WorldView worldView;
|
||||||
|
@Getter
|
||||||
|
private Player player;
|
||||||
|
|
||||||
public OrangeX() {
|
public OrangeX() {
|
||||||
System.out.println("OrangeX <init> start!");
|
System.out.println("OrangeX <init> start!");
|
||||||
@@ -27,10 +63,52 @@ public class OrangeX {
|
|||||||
MappingManager.init(runtimeInfo);
|
MappingManager.init(runtimeInfo);
|
||||||
|
|
||||||
Minecraft minecraft = MinecraftFactory.create(runtimeInfo, MappingManager.getResolver());
|
Minecraft minecraft = MinecraftFactory.create(runtimeInfo, MappingManager.getResolver());
|
||||||
|
this.minecraft = minecraft;
|
||||||
System.out.println(minecraft.getMinecraft());
|
System.out.println(minecraft.getMinecraft());
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.worldView = MinecraftFactory.createWorld(
|
||||||
|
runtimeInfo, MappingManager.getResolver(), minecraft);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
// The client must keep running even when a version cannot expose
|
||||||
|
// world data; world-dependent modules simply stay inactive.
|
||||||
|
System.err.println("[OrangeX] World wrapper unavailable: "
|
||||||
|
+ throwable.getMessage());
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.player = MinecraftFactory.createPlayer(
|
||||||
|
runtimeInfo, MappingManager.getResolver(), minecraft);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
// Player-dependent modules stay inactive when this fails.
|
||||||
|
System.err.println("[OrangeX] Player wrapper unavailable: "
|
||||||
|
+ throwable.getMessage());
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
render2DManager = new Render2DManager(
|
||||||
|
runtimeInfo, MappingManager.getResolver(), minecraft.getMinecraft());
|
||||||
|
System.out.println("[OrangeX] Font backend: " + render2DManager.getBackendName());
|
||||||
|
|
||||||
|
//eventBus.register(new FontDebugOverlay());
|
||||||
|
eventBus.register(new InputController(minecraft));
|
||||||
|
eventBus.register(this);
|
||||||
|
|
||||||
|
moduleManager.init();
|
||||||
|
|
||||||
|
configManager = new ConfigManager(new File(OrangeXPath, "configs/"), moduleManager);
|
||||||
|
configManager.init();
|
||||||
|
|
||||||
|
HookManager.getINSTANCE().applyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
System.out.println("OrangeX stop!");
|
System.out.println("OrangeX stop!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onKey(EventKey event) {
|
||||||
|
System.out.println("KEY: " + event.getKey());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
private final ConfigManager manager;
|
||||||
|
@Getter
|
||||||
|
private final String name;
|
||||||
|
@Getter
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
public Config(ConfigManager manager, String name, File file) {
|
||||||
|
this.manager = manager;
|
||||||
|
this.name = name;
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean load() {
|
||||||
|
return manager.load(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean save() {
|
||||||
|
return manager.save(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists() {
|
||||||
|
return file.isFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,462 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.config.impl.JsonConfigSerializer;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import cn.xiaozhou233.orangex.value.Value;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ConfigManager {
|
||||||
|
|
||||||
|
private static final Pattern VALID_NAME =
|
||||||
|
Pattern.compile("[a-zA-Z0-9_-]+");
|
||||||
|
|
||||||
|
private static final String EXTENSION = ".json";
|
||||||
|
private static final String TEMP_SUFFIX = ".json.tmp";
|
||||||
|
private static final String BACKUP_SUFFIX = ".json.bak";
|
||||||
|
private static final String CURRENT_FILE = "current.txt";
|
||||||
|
private static final String DEFAULT_CONFIG = "default";
|
||||||
|
|
||||||
|
private final File directory;
|
||||||
|
private final ModuleManager moduleManager;
|
||||||
|
private final ConfigSerializer serializer;
|
||||||
|
|
||||||
|
private final Gson gson = new GsonBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.create();
|
||||||
|
|
||||||
|
private Config currentConfig;
|
||||||
|
private volatile boolean dirty;
|
||||||
|
private boolean initialised;
|
||||||
|
|
||||||
|
public ConfigManager(File directory, ModuleManager moduleManager) {
|
||||||
|
this(directory, moduleManager, new JsonConfigSerializer());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigManager(
|
||||||
|
File directory,
|
||||||
|
ModuleManager moduleManager,
|
||||||
|
ConfigSerializer serializer
|
||||||
|
) {
|
||||||
|
this.directory = directory;
|
||||||
|
this.moduleManager = moduleManager;
|
||||||
|
this.serializer = serializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void init() {
|
||||||
|
ensureDirectory();
|
||||||
|
|
||||||
|
if (!getFile(DEFAULT_CONFIG).isFile()) {
|
||||||
|
create(DEFAULT_CONFIG);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track any later value change so the client can persist settings even
|
||||||
|
// when the user never opens the config screen.
|
||||||
|
Value.setGlobalChangeHook(this::markDirty);
|
||||||
|
|
||||||
|
String name = readCurrentName();
|
||||||
|
if (name == null || getConfig(name) == null) {
|
||||||
|
name = DEFAULT_CONFIG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!load(name)) {
|
||||||
|
// Never leave an unreadable file in place: keep a backup and start
|
||||||
|
// over from the current (default) state.
|
||||||
|
backupUnreadable(name);
|
||||||
|
create(name);
|
||||||
|
if (!load(name)) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Could not restore config '" + name + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initialised) {
|
||||||
|
initialised = true;
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
try {
|
||||||
|
saveIfDirty();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to save config on shutdown: "
|
||||||
|
+ throwable.getMessage());
|
||||||
|
}
|
||||||
|
}, "OrangeX Config Save"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a config file from the current in-memory module state, so a new
|
||||||
|
* preset starts out identical to what the user currently sees.
|
||||||
|
*/
|
||||||
|
public synchronized Config create(String name) {
|
||||||
|
validateName(name);
|
||||||
|
|
||||||
|
File file = getFile(name);
|
||||||
|
|
||||||
|
if (file.exists()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Config already exists: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
Config config = new Config(this, name, file);
|
||||||
|
|
||||||
|
if (!save(name)) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Failed to create config: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean load(String name) {
|
||||||
|
Config config = getConfig(name);
|
||||||
|
|
||||||
|
if (config == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not silently drop unsaved changes when switching presets.
|
||||||
|
if (!isCurrent(name) && dirty && currentConfig != null && !save()) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Refusing to switch config: changes to '"
|
||||||
|
+ currentConfig.getName() + "' could not be saved");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = config.getFile();
|
||||||
|
boolean wasDirty = dirty;
|
||||||
|
|
||||||
|
// Snapshot the live state so a malformed file cannot leave modules
|
||||||
|
// half-restored.
|
||||||
|
JsonObject snapshot = serializer.serialize(moduleManager);
|
||||||
|
|
||||||
|
try (FileReader reader = new FileReader(file)) {
|
||||||
|
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
|
||||||
|
|
||||||
|
int version = json.has("version")
|
||||||
|
? json.get("version").getAsInt()
|
||||||
|
: 1;
|
||||||
|
|
||||||
|
if (version != ConfigSerializer.CURRENT_VERSION) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Unsupported config version: " + version);
|
||||||
|
}
|
||||||
|
|
||||||
|
serializer.deserialize(moduleManager, json);
|
||||||
|
|
||||||
|
currentConfig = config;
|
||||||
|
dirty = false;
|
||||||
|
writeCurrentName(name);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to load config '" + name + "': " + e.getMessage());
|
||||||
|
|
||||||
|
try {
|
||||||
|
serializer.deserialize(moduleManager, snapshot);
|
||||||
|
} catch (Exception rollback) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to restore previous state: "
|
||||||
|
+ rollback.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
dirty = wasDirty;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean save() {
|
||||||
|
if (currentConfig == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return save(currentConfig.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists the current module state under {@code name}.
|
||||||
|
*
|
||||||
|
* @return {@code true} when the file was written.
|
||||||
|
*/
|
||||||
|
public synchronized boolean save(String name) {
|
||||||
|
validateName(name);
|
||||||
|
|
||||||
|
File file = getFile(name);
|
||||||
|
File tempFile = new File(directory, name + TEMP_SUFFIX);
|
||||||
|
|
||||||
|
JsonObject json = serializer.serialize(moduleManager);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ensureDirectory();
|
||||||
|
|
||||||
|
try (FileWriter writer = new FileWriter(tempFile)) {
|
||||||
|
gson.toJson(json, writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.move(
|
||||||
|
tempFile.toPath(),
|
||||||
|
file.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING,
|
||||||
|
StandardCopyOption.ATOMIC_MOVE
|
||||||
|
);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Files.move(
|
||||||
|
tempFile.toPath(),
|
||||||
|
file.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentConfig != null
|
||||||
|
&& currentConfig.getName().equalsIgnoreCase(name)) {
|
||||||
|
dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
tempFile.delete();
|
||||||
|
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to save config '" + name + "': "
|
||||||
|
+ e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves only when values or module states changed since the last load or
|
||||||
|
* save. Used by periodic/GUI/shutdown save hooks.
|
||||||
|
*/
|
||||||
|
public synchronized boolean saveIfDirty() {
|
||||||
|
if (!dirty || currentConfig == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean delete(String name) {
|
||||||
|
Config config = getConfig(name);
|
||||||
|
|
||||||
|
if (config == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deleting the active config would leave the client with nowhere to
|
||||||
|
// save to; require the caller to load another config first.
|
||||||
|
if (isCurrent(name)) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Refusing to delete the active config: " + name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean deleted = config.getFile().delete();
|
||||||
|
|
||||||
|
if (deleted && name.equalsIgnoreCase(readCurrentName())) {
|
||||||
|
clearCurrentName();
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean rename(String oldName, String newName) {
|
||||||
|
validateName(oldName);
|
||||||
|
validateName(newName);
|
||||||
|
|
||||||
|
if (oldName.equalsIgnoreCase(newName)) {
|
||||||
|
// A pure case change cannot be represented reliably on
|
||||||
|
// case-insensitive file systems.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Config oldConfig = getConfig(oldName);
|
||||||
|
|
||||||
|
if (oldConfig == null || getFile(newName).exists()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
File newFile = getFile(newName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.move(
|
||||||
|
oldConfig.getFile().toPath(),
|
||||||
|
newFile.toPath()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isCurrent(oldName)) {
|
||||||
|
currentConfig = new Config(this, newName, newFile);
|
||||||
|
writeCurrentName(newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to rename config: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Config getConfig(String name) {
|
||||||
|
if (name == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = getFile(name);
|
||||||
|
|
||||||
|
if (!file.isFile()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Config(this, name, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Config getCurrentConfig() {
|
||||||
|
return currentConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDirty() {
|
||||||
|
return dirty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void markDirty() {
|
||||||
|
dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized List<Config> getConfigs() {
|
||||||
|
File[] files = directory.listFiles(
|
||||||
|
(dir, name) -> name.toLowerCase(Locale.ROOT).endsWith(EXTENSION)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (files == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Config> configs = new ArrayList<>();
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
String name = file.getName();
|
||||||
|
name = name.substring(0, name.length() - EXTENSION.length());
|
||||||
|
|
||||||
|
if (VALID_NAME.matcher(name).matches()) {
|
||||||
|
configs.add(new Config(this, name, file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
configs.sort(Comparator.comparing(
|
||||||
|
Config::getName, String.CASE_INSENSITIVE_ORDER));
|
||||||
|
|
||||||
|
return configs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getDirectory() {
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCurrent(String name) {
|
||||||
|
return currentConfig != null
|
||||||
|
&& currentConfig.getName().equalsIgnoreCase(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getFile(String name) {
|
||||||
|
validateName(name);
|
||||||
|
return new File(directory, name + EXTENSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateName(String name) {
|
||||||
|
if (name == null || !VALID_NAME.matcher(name).matches()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Invalid config name: " + name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureDirectory() {
|
||||||
|
if (!directory.exists() && !directory.mkdirs()) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Failed to create config directory: " + directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the last config that was successfully loaded, or {@code null}.
|
||||||
|
*/
|
||||||
|
private String readCurrentName() {
|
||||||
|
File file = new File(directory, CURRENT_FILE);
|
||||||
|
|
||||||
|
if (!file.isFile()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
String name = new String(
|
||||||
|
Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)
|
||||||
|
.trim();
|
||||||
|
return VALID_NAME.matcher(name).matches() ? name : null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeCurrentName(String name) {
|
||||||
|
try {
|
||||||
|
Files.write(
|
||||||
|
new File(directory, CURRENT_FILE).toPath(),
|
||||||
|
name.getBytes(StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to remember current config: "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearCurrentName() {
|
||||||
|
File file = new File(directory, CURRENT_FILE);
|
||||||
|
if (file.isFile() && !file.delete()) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to clear current config marker");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backupUnreadable(String name) {
|
||||||
|
File file = getFile(name);
|
||||||
|
|
||||||
|
if (!file.isFile()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File backup = new File(directory, name + BACKUP_SUFFIX);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.move(
|
||||||
|
file.toPath(),
|
||||||
|
backup.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING
|
||||||
|
);
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Backed up unreadable config to " + backup);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to back up config '" + name + "': "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
public interface ConfigSerializer {
|
||||||
|
|
||||||
|
/** On-disk format version shared by the manager and the serializer. */
|
||||||
|
int CURRENT_VERSION = 1;
|
||||||
|
|
||||||
|
JsonObject serialize(ModuleManager moduleManager);
|
||||||
|
|
||||||
|
void deserialize(ModuleManager moduleManager, JsonObject json);
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.config.ConfigSerializer;
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
public class JsonConfigSerializer implements ConfigSerializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject serialize(ModuleManager moduleManager) {
|
||||||
|
JsonObject root = new JsonObject();
|
||||||
|
JsonObject modules = new JsonObject();
|
||||||
|
|
||||||
|
root.addProperty("version", CURRENT_VERSION);
|
||||||
|
|
||||||
|
for (Module module : moduleManager.getAllModules()) {
|
||||||
|
JsonObject moduleJson = new JsonObject();
|
||||||
|
|
||||||
|
moduleJson.addProperty("enabled", module.isEnabled());
|
||||||
|
moduleJson.add("values", module.valuesToJson());
|
||||||
|
|
||||||
|
modules.add(module.getConfigKey(), moduleJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.add("modules", modules);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deserialize(ModuleManager moduleManager, JsonObject json) {
|
||||||
|
if (json == null || !json.has("modules")
|
||||||
|
|| !json.get("modules").isJsonObject()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject modules = json.getAsJsonObject("modules");
|
||||||
|
|
||||||
|
for (Module module : moduleManager.getAllModules()) {
|
||||||
|
JsonElement element = modules.get(module.getConfigKey());
|
||||||
|
|
||||||
|
// Older configs keyed modules by display name.
|
||||||
|
if (element == null) {
|
||||||
|
element = modules.get(module.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element == null || !element.isJsonObject()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject moduleJson = element.getAsJsonObject();
|
||||||
|
|
||||||
|
// Restore values before enabling the module.
|
||||||
|
if (moduleJson.has("values")
|
||||||
|
&& moduleJson.get("values").isJsonObject()) {
|
||||||
|
module.valuesFromJson(moduleJson.getAsJsonObject("values"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (moduleJson.has("enabled")
|
||||||
|
&& moduleJson.get("enabled").isJsonPrimitive()
|
||||||
|
&& moduleJson.get("enabled").getAsJsonPrimitive().isBoolean()) {
|
||||||
|
module.setEnabled(moduleJson.get("enabled").getAsBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event;
|
||||||
|
|
||||||
|
public interface Event {
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.facade;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
import cn.xiaozhou233.orangex.input.KeyCodes;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input entry point shared by injected 1.8.9 code and the 26.2 input screen.
|
||||||
|
*
|
||||||
|
* <p>Every vanilla screen goes through these methods, so each callback first
|
||||||
|
* checks whether the instance is the empty input screen created by OrangeX.
|
||||||
|
* Key events are not handled here: they are posted as
|
||||||
|
* {@link cn.xiaozhou233.orangex.event.impl.EventKey} so features can subscribe
|
||||||
|
* to them, and {@link cn.xiaozhou233.orangex.input.InputController} consumes
|
||||||
|
* the GUI toggle.</p>
|
||||||
|
*/
|
||||||
|
public final class InputEvents {
|
||||||
|
|
||||||
|
private static final boolean DEBUG = Boolean.getBoolean("orangex.debugInput");
|
||||||
|
|
||||||
|
private static boolean failureLogged;
|
||||||
|
|
||||||
|
private static Minecraft minecraft() {
|
||||||
|
return OrangeX.getInstance().getMinecraft();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isOurs(Object screen) {
|
||||||
|
try {
|
||||||
|
Minecraft minecraft = minecraft();
|
||||||
|
return minecraft != null && minecraft.isInputScreenInstance(screen);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
logFailure(throwable);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called from {@code KeyBinding#onTick(int)} for every key press while no
|
||||||
|
* screen is open. The injected code already dropped auto-repeat events.
|
||||||
|
*/
|
||||||
|
public static void onKeyPressed(int keyCode) {
|
||||||
|
if (DEBUG) {
|
||||||
|
System.out.println("[OrangeX] key pressed: " + keyCode);
|
||||||
|
}
|
||||||
|
KeyEvents.fire(keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called from the 26.2 {@code KeyboardHandler#keyPress} hook. Translates
|
||||||
|
* the GLFW key code to the canonical 1.8.9 numbering before posting.
|
||||||
|
*/
|
||||||
|
public static void onGlfwKeyPressed(int glfwKeyCode) {
|
||||||
|
onKeyPressed(KeyCodes.fromGlfw(glfwKeyCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called from {@code GuiScreen#keyTyped(char, int)}. The event is posted to
|
||||||
|
* the EventBus; returning {@code false} keeps vanilla key handling (ESC,
|
||||||
|
* etc.) intact.
|
||||||
|
*/
|
||||||
|
public static boolean onKeyTyped(Object screen, int keyCode) {
|
||||||
|
if (!isOurs(screen)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
KeyEvents.fire(keyCode);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onDrawScreen(Object screen, int mouseX, int mouseY) {
|
||||||
|
if (isOurs(screen)) {
|
||||||
|
ClientInput.onMouseMove(mouseX, mouseY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean onMouseClicked(Object screen, int mouseX,
|
||||||
|
int mouseY, int button) {
|
||||||
|
if (!isOurs(screen)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ClientInput.onMouseButton(mouseX, mouseY, button, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean onMouseReleased(Object screen, int mouseX,
|
||||||
|
int mouseY, int button) {
|
||||||
|
if (!isOurs(screen)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ClientInput.onMouseButton(mouseX, mouseY, button, false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onMouseDragged(Object screen, int mouseX,
|
||||||
|
int mouseY, int button) {
|
||||||
|
if (isOurs(screen)) {
|
||||||
|
ClientInput.onMouseMove(mouseX, mouseY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isOurInputScreen(Object screen) {
|
||||||
|
return isOurs(screen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void logFailure(Throwable throwable) {
|
||||||
|
if (!failureLogged) {
|
||||||
|
failureLogged = true;
|
||||||
|
System.err.println("[OrangeX] Input callback failed");
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputEvents() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.facade;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventKey;
|
||||||
|
import cn.xiaozhou233.orangex.input.KeyCodes;
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entry point for raw key events.
|
||||||
|
*
|
||||||
|
* <p>Injected code and version-specific input code only call this facade;
|
||||||
|
* subscribers receive {@link EventKey} through the shared EventBus. All key
|
||||||
|
* codes are normalised to the 1.8.9 / LWJGL 2 numbering.</p>
|
||||||
|
*/
|
||||||
|
public final class KeyEvents {
|
||||||
|
|
||||||
|
private static final EventBus BUS = OrangeX.getInstance().getEventBus();
|
||||||
|
|
||||||
|
private static boolean failureLogged;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key canonical 1.8.9 (LWJGL 2) key code.
|
||||||
|
*/
|
||||||
|
public static void fire(int key) {
|
||||||
|
if (key == KeyCodes.NONE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BUS.post(new EventKey(key));
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
if (!failureLogged) {
|
||||||
|
failureLogged = true;
|
||||||
|
System.err.println("[OrangeX] Key event dispatch failed");
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private KeyEvents() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.facade;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.render.Render2DManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entry point used by injected code. Always refer to this class by its simple
|
||||||
|
* name inside Javassist snippets (see docs/hooks.md).
|
||||||
|
*/
|
||||||
|
public final class Render2DEvents {
|
||||||
|
|
||||||
|
private static boolean failureLogged;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param inWorld whether the game is currently rendering a loaded level.
|
||||||
|
* Modern versions call the GUI extraction on the title
|
||||||
|
* screen too, so the hook must pass this through.
|
||||||
|
*/
|
||||||
|
public static void fire(Object platformContext, boolean inWorld) {
|
||||||
|
fire(platformContext, inWorld, Float.NaN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param partialTicks the partial tick the game rendered this frame with.
|
||||||
|
* {@link Float#NaN} when the hook cannot supply it.
|
||||||
|
*/
|
||||||
|
public static void fire(Object platformContext, boolean inWorld,
|
||||||
|
float partialTicks) {
|
||||||
|
try {
|
||||||
|
Render2DManager manager = OrangeX.getInstance().getRender2DManager();
|
||||||
|
if (manager != null) {
|
||||||
|
manager.fire(platformContext, inWorld, partialTicks);
|
||||||
|
}
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
if (!failureLogged) {
|
||||||
|
failureLogged = true;
|
||||||
|
System.err.println("[OrangeX] Render2D callback failed");
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Legacy entry point for hooks that only run in world (1.8.9 HUD).
|
||||||
|
*/
|
||||||
|
public static void fire(Object platformContext) {
|
||||||
|
fire(platformContext, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Render2DEvents() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.facade;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPost;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
|
||||||
|
public class TickEvents {
|
||||||
|
private static final EventBus BUS = OrangeX.getInstance().getEventBus();
|
||||||
|
|
||||||
|
public static void firePre() {
|
||||||
|
BUS.post(new EventTickPre());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void firePost() {
|
||||||
|
BUS.post(new EventTickPost());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.Event;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A key press delivered through the EventBus.
|
||||||
|
*
|
||||||
|
* <p>{@link #getKey()} always uses the 1.8.9 / LWJGL 2 numbering, even when
|
||||||
|
* the game runs on 26.2. Modern GLFW key codes are translated before the
|
||||||
|
* event is posted (see {@code KeyCodes#fromGlfw(int)}).</p>
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class EventKey implements Event {
|
||||||
|
private final int key;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired once per frame while the game collects its GUI. Listeners can queue
|
||||||
|
* text through {@link #getRender2D()}.
|
||||||
|
*
|
||||||
|
* <p>{@link #isInWorld()} mirrors the game's own "should render level" flag.
|
||||||
|
* 1.8.9 only runs the HUD hook while a world is loaded, but modern versions
|
||||||
|
* also extract the GUI on the title screen, so listeners must not assume that
|
||||||
|
* receiving this event means the player is in game.</p>
|
||||||
|
*/
|
||||||
|
public class EventRender2D {
|
||||||
|
|
||||||
|
private final Render2D render2D;
|
||||||
|
private final boolean inWorld;
|
||||||
|
private final float partialTicks;
|
||||||
|
|
||||||
|
public EventRender2D(Render2D render2D, boolean inWorld) {
|
||||||
|
this(render2D, inWorld, Float.NaN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventRender2D(Render2D render2D, boolean inWorld, float partialTicks) {
|
||||||
|
this.render2D = render2D;
|
||||||
|
this.inWorld = inWorld;
|
||||||
|
this.partialTicks = partialTicks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Render2D getRender2D() {
|
||||||
|
return render2D;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code true} when the frame is rendered while a level is loaded.
|
||||||
|
*/
|
||||||
|
public boolean isInWorld() {
|
||||||
|
return inWorld;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The partial tick the game used for this frame's world render, i.e. the
|
||||||
|
* interpolation factor between the previous and the current tick.
|
||||||
|
*
|
||||||
|
* <p>Feature code must use this value instead of reading a timer of its own
|
||||||
|
* so projected world positions line up with the rendered world. It is
|
||||||
|
* {@link Float#NaN} when the hook could not supply one.</p>
|
||||||
|
*/
|
||||||
|
public float getPartialTicks() {
|
||||||
|
return partialTicks;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.Event;
|
||||||
|
|
||||||
|
public class EventTickPost implements Event {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package cn.xiaozhou233.orangex.event.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.Event;
|
||||||
|
|
||||||
|
public class EventTickPre implements Event {
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook;
|
||||||
|
|
||||||
|
import javassist.ClassPath;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves game classes from the <strong>loaded</strong> class bytes through
|
||||||
|
* the JuiceAgent.
|
||||||
|
*
|
||||||
|
* <p>Some launchers (for example Lunar's Genesis class loader) serve
|
||||||
|
* MCP-named class resources but the bytes inside still use obfuscated class
|
||||||
|
* names. Javassist then throws errors such as
|
||||||
|
* {@code cannot find net.minecraft.util.IThreadListener: od found in ...}.
|
||||||
|
* The agent can return the current, runtime-named bytecode instead.</p>
|
||||||
|
*/
|
||||||
|
final class AgentClassPath implements ClassPath {
|
||||||
|
|
||||||
|
private final ClassLoader loader;
|
||||||
|
|
||||||
|
AgentClassPath(ClassLoader loader) {
|
||||||
|
this.loader = loader;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openClassfile(String classname) throws NotFoundException {
|
||||||
|
if (isPlatformClass(classname)
|
||||||
|
|| classname.startsWith("cn.xiaozhou233.")
|
||||||
|
|| classname.startsWith("orangexbridge.")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = readBytes(classname);
|
||||||
|
return bytes == null ? null : new ByteArrayInputStream(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readBytes(String classname) {
|
||||||
|
String internalName = classname.replace('.', '/');
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] bytes = JuiceAgentBridge.getClassBytesByName(internalName);
|
||||||
|
if (bytes != null) {
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
// The class may not be loaded yet.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loader == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Class.forName(classname, false, loader);
|
||||||
|
return JuiceAgentBridge.getClassBytesByName(internalName);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isPlatformClass(String classname) {
|
||||||
|
return classname.startsWith("java.")
|
||||||
|
|| classname.startsWith("javax.")
|
||||||
|
|| classname.startsWith("jdk.")
|
||||||
|
|| classname.startsWith("sun.")
|
||||||
|
|| classname.startsWith("com.sun.")
|
||||||
|
|| classname.startsWith("javassist.")
|
||||||
|
|| classname.startsWith("org.greenrobot.")
|
||||||
|
|| classname.startsWith("com.google.gson.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URL find(String classname) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook;
|
||||||
|
|
||||||
|
import javassist.ClassPool;
|
||||||
|
import javassist.CtClass;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
|
public abstract class ClassHook {
|
||||||
|
|
||||||
|
protected abstract String getTargetClass();
|
||||||
|
|
||||||
|
protected abstract void transform(CtClass ctClass) throws Exception;
|
||||||
|
|
||||||
|
public final byte[] transform(ClassLoader loader, byte[] classBytes)
|
||||||
|
throws Exception {
|
||||||
|
|
||||||
|
ClassPool pool = ClassPoolUtils.createPool(loader);
|
||||||
|
CtClass ctClass = pool.makeClass(
|
||||||
|
new ByteArrayInputStream(classBytes),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
transform(ctClass);
|
||||||
|
return ctClass.toBytecode();
|
||||||
|
} finally {
|
||||||
|
ctClass.detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook;
|
||||||
|
|
||||||
|
import javassist.ClassClassPath;
|
||||||
|
import javassist.ClassPool;
|
||||||
|
import javassist.LoaderClassPath;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Javassist setup shared by all hooks.
|
||||||
|
*
|
||||||
|
* <p>A fresh pool is created for each transform instead of using
|
||||||
|
* {@link ClassPool#getDefault()}: on some launchers the current thread has a
|
||||||
|
* {@code null} context class loader, which makes the default pool's system
|
||||||
|
* path throw while resolving imported class names.</p>
|
||||||
|
*/
|
||||||
|
public final class ClassPoolUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set by the Loader before OrangeX loads: the jar that was injected into
|
||||||
|
* the game class loader.
|
||||||
|
*/
|
||||||
|
private static final String ORANGEX_JAR_PROPERTY = "orangex.jar";
|
||||||
|
|
||||||
|
private static boolean bridgeChecked;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a pool that can resolve:
|
||||||
|
* <ul>
|
||||||
|
* <li>OrangeX classes from the injected jar;</li>
|
||||||
|
* <li>game classes from their runtime (post-transform) bytes through
|
||||||
|
* the agent, avoiding launchers whose class-loader resources still
|
||||||
|
* contain obfuscated names;</li>
|
||||||
|
* <li>anything else via the game loader and the system loader.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param loader the game class loader
|
||||||
|
*/
|
||||||
|
public static ClassPool createPool(ClassLoader loader) {
|
||||||
|
ClassPool pool = new ClassPool(false);
|
||||||
|
|
||||||
|
File jar = orangeXJar();
|
||||||
|
if (jar != null) {
|
||||||
|
try {
|
||||||
|
pool.insertClassPath(jar.getAbsolutePath());
|
||||||
|
System.out.println("[OrangeX] javassist classpath: "
|
||||||
|
+ jar.getAbsolutePath());
|
||||||
|
} catch (Throwable t) {
|
||||||
|
System.out.println("[OrangeX] Failed to add javassist classpath: "
|
||||||
|
+ jar.getAbsolutePath() + " (" + t + ")");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("[OrangeX] WARNING: OrangeX.jar not found for javassist; "
|
||||||
|
+ "injected classes may fail to compile");
|
||||||
|
pool.insertClassPath(new ClassClassPath(ClassPoolUtils.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Runtime bytes of already-loaded game classes (MCP-named even when
|
||||||
|
// the launcher's resource stream is still obfuscated).
|
||||||
|
pool.appendClassPath(new AgentClassPath(loader));
|
||||||
|
|
||||||
|
if (loader != null) {
|
||||||
|
pool.appendClassPath(new SafeClassPath(new LoaderClassPath(loader)));
|
||||||
|
}
|
||||||
|
|
||||||
|
ClassLoader system = ClassLoader.getSystemClassLoader();
|
||||||
|
if (system != null) {
|
||||||
|
pool.appendClassPath(new SafeClassPath(new LoaderClassPath(system)));
|
||||||
|
} else {
|
||||||
|
pool.appendClassPath(new ClassClassPath(Object.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bridgeChecked) {
|
||||||
|
bridgeChecked = true;
|
||||||
|
try {
|
||||||
|
pool.get("orangexbridge.Hooks");
|
||||||
|
System.out.println("[OrangeX] javassist bridge OK");
|
||||||
|
} catch (Throwable t) {
|
||||||
|
System.out.println("[OrangeX] javassist bridge FAILED");
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static File orangeXJar() {
|
||||||
|
String path = System.getProperty(ORANGEX_JAR_PROPERTY);
|
||||||
|
if (path == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File jar = new File(path);
|
||||||
|
return jar.isFile() ? jar : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClassPoolUtils() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.hook.impl.*;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public final class HookManager {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private static final HookManager INSTANCE = new HookManager();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Original (pre-injection) bytes of every hook target, keyed by dot name.
|
||||||
|
* Used by 1.8.9 screen generation so it never has to retransform a class
|
||||||
|
* that has already been patched.
|
||||||
|
*/
|
||||||
|
private static final Map<String, byte[]> TARGET_BYTES =
|
||||||
|
new ConcurrentHashMap<String, byte[]>();
|
||||||
|
|
||||||
|
private final List<ClassHook> hooks = new ArrayList<ClassHook>();
|
||||||
|
|
||||||
|
private HookManager() {
|
||||||
|
register(new TickHook());
|
||||||
|
register(new Render2DHook());
|
||||||
|
register(new GuiInputHook());
|
||||||
|
register(new KeyPressHook());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(ClassHook hook) {
|
||||||
|
hooks.add(hook);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyAll() {
|
||||||
|
ClassLoader loader = OrangeX.getInstance().getRuntimeInfo().getMinecraftClassLoader();
|
||||||
|
|
||||||
|
for (ClassHook hook : hooks) {
|
||||||
|
String className = hook.getTargetClass();
|
||||||
|
if (className == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] classBytes = JuiceAgentBridge.getClassBytesByName(
|
||||||
|
className.replace(".", "/"));
|
||||||
|
if (classBytes == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
TARGET_BYTES.put(className, classBytes);
|
||||||
|
|
||||||
|
byte[] bytes = hook.transform(loader, classBytes);
|
||||||
|
JuiceAgentBridge.retransformClassByName(className, bytes);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
System.err.println("[OrangeX] Failed to apply hook: " + className);
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] getTargetBytes(String className) {
|
||||||
|
return TARGET_BYTES.get(className);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reflective access to the JuiceAgent API.
|
||||||
|
*
|
||||||
|
* <p>The agent registers its native methods on the API class loaded by the
|
||||||
|
* system class loader. On Fabric, OrangeX itself is loaded by Knot, so a
|
||||||
|
* direct reference could resolve to a second copy of {@code JuiceAgent} that
|
||||||
|
* has no native implementation and throws {@link UnsatisfiedLinkError}.
|
||||||
|
* Looking the class up through the system class loader always reaches the
|
||||||
|
* registered copy.</p>
|
||||||
|
*/
|
||||||
|
public final class JuiceAgentBridge {
|
||||||
|
|
||||||
|
private static final String API_CLASS = "cn.xiaozhou233.juiceagent.api.JuiceAgent";
|
||||||
|
|
||||||
|
private static Method getClassBytesByName;
|
||||||
|
private static Method retransformClassByName;
|
||||||
|
private static Method defineClass;
|
||||||
|
|
||||||
|
public static byte[] getClassBytesByName(String className) {
|
||||||
|
init();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return (byte[]) getClassBytesByName.invoke(null, className);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"JuiceAgent.getClassBytesByName failed: " + className, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void retransformClassByName(String className, byte[] bytes) {
|
||||||
|
init();
|
||||||
|
|
||||||
|
try {
|
||||||
|
retransformClassByName.invoke(null, className, bytes, bytes.length);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"JuiceAgent.retransformClassByName failed: " + className, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a new class in {@code loader} through the agent.
|
||||||
|
*
|
||||||
|
* <p>{@code ClassLoader#defineClass} is protected and cannot be called
|
||||||
|
* through reflection on modern JDKs; the native agent invokes it through
|
||||||
|
* JNI instead.</p>
|
||||||
|
*/
|
||||||
|
public static Class<?> defineClass(ClassLoader loader, byte[] bytes) {
|
||||||
|
init();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return (Class<?>) defineClass.invoke(null, loader, bytes);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
throw new IllegalStateException("JuiceAgent.defineClass failed", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static synchronized void init() {
|
||||||
|
if (getClassBytesByName != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Class<?> api = Class.forName(API_CLASS, true, ClassLoader.getSystemClassLoader());
|
||||||
|
getClassBytesByName = api.getMethod("getClassBytesByName", String.class);
|
||||||
|
retransformClassByName = api.getMethod(
|
||||||
|
"retransformClassByName", String.class, byte[].class, int.class);
|
||||||
|
defineClass = api.getMethod(
|
||||||
|
"defineClass", ClassLoader.class, byte[].class);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
throw new IllegalStateException("JuiceAgent API is not available", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JuiceAgentBridge() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook;
|
||||||
|
|
||||||
|
import javassist.ClassPath;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps a {@link ClassPath} so launcher-specific exceptions are treated as
|
||||||
|
* "not found" instead of aborting Javassist's class path search.
|
||||||
|
*/
|
||||||
|
final class SafeClassPath implements ClassPath {
|
||||||
|
|
||||||
|
private final ClassPath delegate;
|
||||||
|
|
||||||
|
SafeClassPath(ClassPath delegate) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openClassfile(String classname) {
|
||||||
|
try {
|
||||||
|
return delegate.openClassfile(classname);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URL find(String classname) {
|
||||||
|
try {
|
||||||
|
return delegate.find(classname);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.hook.ClassHook;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forwards 1.8.9 {@code GuiScreen} input to the OrangeX empty input screen.
|
||||||
|
*
|
||||||
|
* <p>1.8.9 has no {@code mouseMoved} callback, so the cursor position is
|
||||||
|
* sampled from {@code drawScreen(int, int, float)} every frame. Clicks are
|
||||||
|
* taken from the screen's normal mouse pipeline.</p>
|
||||||
|
*
|
||||||
|
* <p>26.2 does not need this hook: its input screen overrides the
|
||||||
|
* {@code Screen} callbacks directly.</p>
|
||||||
|
*/
|
||||||
|
public final class GuiInputHook extends ClassHook {
|
||||||
|
|
||||||
|
private static final String GUI_SCREEN = "net/minecraft/client/gui/GuiScreen";
|
||||||
|
|
||||||
|
private static final RuntimeInfo runtimeInfo =
|
||||||
|
OrangeX.getInstance().getRuntimeInfo();
|
||||||
|
private static final MappingResolver resolver = MappingManager.getResolver();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getTargetClass() {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9:
|
||||||
|
return resolver.mapClass(GUI_SCREEN)
|
||||||
|
.getTargetName().replace("/", ".");
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void transform(CtClass ctClass) throws Exception {
|
||||||
|
if (runtimeInfo.getMinecraftVersion() != MinecraftVersion.V1_8_9) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
injectBefore(ctClass, "drawScreen", "(IIF)V",
|
||||||
|
"orangexbridge.Hooks.inputDraw($0, $1, $2);");
|
||||||
|
|
||||||
|
injectBefore(ctClass, "mouseClicked", "(III)V",
|
||||||
|
"if (orangexbridge.Hooks.inputClick($0, $1, $2, $3)) return;");
|
||||||
|
|
||||||
|
injectBefore(ctClass, "mouseReleased", "(III)V",
|
||||||
|
"if (orangexbridge.Hooks.inputRelease($0, $1, $2, $3)) return;");
|
||||||
|
|
||||||
|
injectBefore(ctClass, "mouseClickMove", "(IIIJ)V",
|
||||||
|
"orangexbridge.Hooks.inputDrag($0, $1, $2, $3);");
|
||||||
|
|
||||||
|
// Forward every key typed on our empty screen as an EventKey. The
|
||||||
|
// facade only consumes the toggle key so ESC and other keys keep
|
||||||
|
// their vanilla Screen behaviour.
|
||||||
|
injectBefore(ctClass, "keyTyped", "(CI)V",
|
||||||
|
"if (!orangexbridge.Legacy.isRepeatEvent()"
|
||||||
|
+ " && orangexbridge.Hooks.inputKeyTyped($0, $2)) return;");
|
||||||
|
|
||||||
|
// The empty screen is an overlay: it must not pause singleplayer.
|
||||||
|
injectAfter(ctClass, "doesGuiPauseGame", "()Z",
|
||||||
|
"if (orangexbridge.Hooks.isOurScreen($0)) { $_ = false; }");
|
||||||
|
|
||||||
|
// Wheel scrolling for the ClickGUI. Injecting after vanilla keeps its
|
||||||
|
// mouse-button dispatch intact; the wheel event is still readable.
|
||||||
|
try {
|
||||||
|
injectAfter(ctClass, "handleMouseInput", "()V",
|
||||||
|
"orangexbridge.Legacy.forwardScroll($0);");
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
// Scrolling is optional: never let it disable the input hooks
|
||||||
|
// above on a runtime where this method cannot be resolved.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void injectBefore(CtClass ctClass, String name,
|
||||||
|
String descriptor, String code) throws Exception {
|
||||||
|
CtMethod method = find(ctClass, name, descriptor);
|
||||||
|
method.insertBefore(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void injectAfter(CtClass ctClass, String name,
|
||||||
|
String descriptor, String code) throws Exception {
|
||||||
|
CtMethod method = find(ctClass, name, descriptor);
|
||||||
|
method.insertAfter(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CtMethod find(CtClass ctClass, String name, String descriptor)
|
||||||
|
throws Exception {
|
||||||
|
MappingMethod mapping = resolver.mapMethod(GUI_SCREEN, name, descriptor);
|
||||||
|
return ctClass.getMethod(
|
||||||
|
mapping.getTargetName(), mapping.getTargetDescriptor());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.hook.ClassHook;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key-press hook for both supported versions.
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>1.8.9: {@code KeyBinding#onTick(int)} is called for every key press
|
||||||
|
* while no screen is open. This keeps short taps from being missed
|
||||||
|
* between two 20 Hz client ticks.</li>
|
||||||
|
* <li>26.2: {@code KeyboardHandler#keyPress(long, int, KeyEvent)} is the
|
||||||
|
* GLFW callback and therefore runs for every key press, including while
|
||||||
|
* walking with no screen open. The input screen's own
|
||||||
|
* {@code Screen#keyPressed} is not enough because the game does not
|
||||||
|
* deliver keys to a screen that is not open.</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public final class KeyPressHook extends ClassHook {
|
||||||
|
|
||||||
|
private static final String KEY_BINDING =
|
||||||
|
"net/minecraft/client/settings/KeyBinding";
|
||||||
|
private static final String KEYBOARD_HANDLER =
|
||||||
|
"net/minecraft/client/KeyboardHandler";
|
||||||
|
|
||||||
|
/** GLFW_PRESS. */
|
||||||
|
private static final int GLFW_PRESS = 1;
|
||||||
|
|
||||||
|
private static final RuntimeInfo runtimeInfo =
|
||||||
|
OrangeX.getInstance().getRuntimeInfo();
|
||||||
|
private static final MappingResolver resolver = MappingManager.getResolver();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getTargetClass() {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9:
|
||||||
|
return resolver.mapClass(KEY_BINDING)
|
||||||
|
.getTargetName().replace("/", ".");
|
||||||
|
case V26_2:
|
||||||
|
return resolver.mapClass(KEYBOARD_HANDLER)
|
||||||
|
.getTargetName().replace("/", ".");
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void transform(CtClass ctClass) throws Exception {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9:
|
||||||
|
transformLegacy(ctClass);
|
||||||
|
break;
|
||||||
|
case V26_2:
|
||||||
|
transformModern(ctClass);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void transformLegacy(CtClass ctClass) throws Exception {
|
||||||
|
MappingMethod mapping = resolver.mapMethod(
|
||||||
|
KEY_BINDING, "onTick", "(I)V");
|
||||||
|
CtMethod method = ctClass.getMethod(
|
||||||
|
mapping.getTargetName(), mapping.getTargetDescriptor());
|
||||||
|
// $1 is the key code. Ignore auto-repeat so holding a key does not
|
||||||
|
// flood the EventBus. Every other key is forwarded as an EventKey.
|
||||||
|
method.insertBefore(
|
||||||
|
"if (!orangexbridge.Legacy.isRepeatEvent())"
|
||||||
|
+ " { orangexbridge.Hooks.inputKeyPressed($1); }");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void transformModern(CtClass ctClass) throws Exception {
|
||||||
|
MappingMethod mapping = resolver.mapMethod(
|
||||||
|
KEYBOARD_HANDLER,
|
||||||
|
"keyPress",
|
||||||
|
"(JILnet/minecraft/client/input/KeyEvent;)V");
|
||||||
|
CtMethod method = ctClass.getMethod(
|
||||||
|
mapping.getTargetName(), mapping.getTargetDescriptor());
|
||||||
|
// $2 is the GLFW action (1 = press, 0 = release, 2 = repeat).
|
||||||
|
// $3 is the KeyEvent record; its key() is a GLFW key code, which the
|
||||||
|
// facade normalises to the 1.8.9 numbering.
|
||||||
|
method.insertBefore(
|
||||||
|
"if ($2 == " + GLFW_PRESS + ")"
|
||||||
|
+ " { orangexbridge.Hooks.inputGlfwKeyPressed($3.key()); }");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.hook.ClassHook;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtMethod;
|
||||||
|
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires {@link cn.xiaozhou233.orangex.event.impl.EventRender2D} once per frame.
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>1.8.9: tail of {@code GuiIngame#renderGameOverlay(float)}. This method
|
||||||
|
* is only called while a level is loaded, so the overlay is world-only
|
||||||
|
* by construction.</li>
|
||||||
|
* <li>26.2: tail of {@code Gui#extractRenderState(DeltaTracker, boolean,
|
||||||
|
* boolean)}. The {@code Gui} instance is passed to the facade so the
|
||||||
|
* font backend can reach its {@code GuiRenderState}; the second
|
||||||
|
* argument ({@code shouldRenderLevel}) tells the manager whether a
|
||||||
|
* level is loaded, because this method also runs on the title screen.</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public final class Render2DHook extends ClassHook {
|
||||||
|
|
||||||
|
private static final RuntimeInfo runtimeInfo =
|
||||||
|
OrangeX.getInstance().getRuntimeInfo();
|
||||||
|
private static final MappingResolver resolver = MappingManager.getResolver();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getTargetClass() {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9:
|
||||||
|
if (runtimeInfo.getLoaderType() == LoaderType.FORGE
|
||||||
|
&& classPresent("net.minecraftforge.client.GuiIngameForge")) {
|
||||||
|
// Forge replaces the HUD renderer with its own subclass.
|
||||||
|
return "net.minecraftforge.client.GuiIngameForge";
|
||||||
|
}
|
||||||
|
return resolver.mapClass("net/minecraft/client/gui/GuiIngame")
|
||||||
|
.getTargetName().replace("/", ".");
|
||||||
|
case V26_2:
|
||||||
|
return "net.minecraft.client.gui.Gui";
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean classPresent(String className) {
|
||||||
|
try {
|
||||||
|
Class.forName(className, false, Render2DHook.class.getClassLoader());
|
||||||
|
return true;
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the HUD entry point in a Forge class whose member names depend on
|
||||||
|
* the remapping stage (Notch {@code a}, SRG {@code func_175180_a}, MCP
|
||||||
|
* {@code renderGameOverlay}).
|
||||||
|
*/
|
||||||
|
private static CtMethod findFloatOverlayMethod(CtClass ctClass) {
|
||||||
|
CtMethod fallback = null;
|
||||||
|
for (CtMethod method : ctClass.getDeclaredMethods()) {
|
||||||
|
if (!"(F)V".equals(method.getSignature())
|
||||||
|
|| Modifier.isStatic(method.getModifiers())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String name = method.getName();
|
||||||
|
if ("renderGameOverlay".equals(name)
|
||||||
|
|| "func_175180_a".equals(name)
|
||||||
|
|| "a".equals(name)) {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
if (fallback == null) {
|
||||||
|
fallback = method;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void transform(CtClass ctClass) throws Exception {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9: {
|
||||||
|
String methodName;
|
||||||
|
String methodDescriptor;
|
||||||
|
if (runtimeInfo.getLoaderType() == LoaderType.FORGE
|
||||||
|
&& classPresent("net.minecraftforge.client.GuiIngameForge")) {
|
||||||
|
// Forge's GuiIngameForge is remapped to SRG names at
|
||||||
|
// runtime (func_175180_a), but the universal jar contains
|
||||||
|
// the Notch name (a). Locate the override by its descriptor
|
||||||
|
// instead of hardcoding any of them.
|
||||||
|
CtMethod method = findFloatOverlayMethod(ctClass);
|
||||||
|
if (method == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"No (F)V HUD method found in " + ctClass.getName());
|
||||||
|
}
|
||||||
|
method.insertAfter(
|
||||||
|
"orangexbridge.Hooks.render2d(null, true, $1);");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
MappingMethod target = resolver.mapMethod(
|
||||||
|
"net/minecraft/client/gui/GuiIngame",
|
||||||
|
"renderGameOverlay",
|
||||||
|
"(F)V");
|
||||||
|
methodName = target.getTargetName();
|
||||||
|
methodDescriptor = target.getTargetDescriptor();
|
||||||
|
}
|
||||||
|
CtMethod method = ctClass.getMethod(methodName, methodDescriptor);
|
||||||
|
// $1 is the frame's partial tick: pass it through so features
|
||||||
|
// interpolate world positions exactly like the world render.
|
||||||
|
method.insertAfter(
|
||||||
|
"orangexbridge.Hooks.render2d(null, true, $1);");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case V26_2: {
|
||||||
|
MappingMethod target = resolver.mapMethod(
|
||||||
|
"net/minecraft/client/gui/Gui",
|
||||||
|
"extractRenderState",
|
||||||
|
"(Lnet/minecraft/client/DeltaTracker;ZZ)V");
|
||||||
|
CtMethod method = ctClass.getMethod(
|
||||||
|
target.getTargetName(), target.getTargetDescriptor());
|
||||||
|
// $0 is this, $1 is the DeltaTracker, $2 is shouldRenderLevel:
|
||||||
|
// false on the title screen, true while a level (including its
|
||||||
|
// pause/ClickGUI screens) exists.
|
||||||
|
method.insertAfter("orangexbridge.Hooks.render2d($0, $2);");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package cn.xiaozhou233.orangex.hook.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.hook.ClassHook;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires {@code EventTickPre}/{@code EventTickPost} around
|
||||||
|
* {@code Minecraft#runTick}. The descriptor differs per version.
|
||||||
|
*/
|
||||||
|
public final class TickHook extends ClassHook {
|
||||||
|
|
||||||
|
private static final RuntimeInfo runtimeInfo = OrangeX.getInstance().getRuntimeInfo();
|
||||||
|
private static final MappingResolver resolver = MappingManager.getResolver();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getTargetClass() {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9:
|
||||||
|
return resolver.mapClass("net/minecraft/client/Minecraft")
|
||||||
|
.getTargetName().replace("/", ".");
|
||||||
|
case V26_2:
|
||||||
|
return "net.minecraft.client.Minecraft";
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void transform(CtClass ctClass) throws Exception {
|
||||||
|
String methodName = null;
|
||||||
|
String methodDesc = null;
|
||||||
|
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9: {
|
||||||
|
MappingMethod runTick = resolver.mapMethod(
|
||||||
|
"net/minecraft/client/Minecraft", "runTick", "()V");
|
||||||
|
methodName = runTick.getTargetName();
|
||||||
|
methodDesc = runTick.getTargetDescriptor();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 26.2 is not obfuscated: runTick(Z)V, private.
|
||||||
|
case V26_2: {
|
||||||
|
MappingMethod runTick = resolver.mapMethod(
|
||||||
|
"net/minecraft/client/Minecraft", "runTick", "(Z)V");
|
||||||
|
methodName = runTick.getTargetName();
|
||||||
|
methodDesc = runTick.getTargetDescriptor();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (methodName == null || methodDesc == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CtMethod method = ctClass.getMethod(methodName, methodDesc);
|
||||||
|
|
||||||
|
// Fully qualified bridge name avoids the default-package "cn" class.
|
||||||
|
method.insertBefore("orangexbridge.Hooks.tickPre();");
|
||||||
|
method.insertAfter("orangexbridge.Hooks.tickPost();");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cross-version GUI input state.
|
||||||
|
*
|
||||||
|
* <p>While the empty screen is open the game stops sending mouse and keyboard
|
||||||
|
* input to the player. The screen itself forwards cursor and button events
|
||||||
|
* here, and feature code can poll this class from {@code EventRender2D} to
|
||||||
|
* draw hover effects, cursors, ClickGUI panels, etc.</p>
|
||||||
|
*
|
||||||
|
* <p>Coordinates are Minecraft GUI pixels (the same space as
|
||||||
|
* {@code Render2D}).</p>
|
||||||
|
*/
|
||||||
|
public final class ClientInput {
|
||||||
|
|
||||||
|
private static volatile boolean open;
|
||||||
|
private static volatile double mouseX;
|
||||||
|
private static volatile double mouseY;
|
||||||
|
private static volatile boolean leftDown;
|
||||||
|
private static volatile boolean rightDown;
|
||||||
|
private static volatile boolean middleDown;
|
||||||
|
private static double scrollAmount;
|
||||||
|
|
||||||
|
private static boolean started;
|
||||||
|
private static boolean pendingOpen;
|
||||||
|
private static boolean pendingClose;
|
||||||
|
|
||||||
|
private ClientInput() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called once per client tick by {@link InputController}. The toggle
|
||||||
|
* itself is event-driven on every supported version; this only reconciles
|
||||||
|
* the tracked state with the actual Minecraft screen.
|
||||||
|
*/
|
||||||
|
public static void tick(Minecraft minecraft) {
|
||||||
|
if (!started) {
|
||||||
|
started = true;
|
||||||
|
System.out.println("[OrangeX] Input controller active");
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean screenOpen = minecraft.isInputScreenOpen();
|
||||||
|
|
||||||
|
if (open && !screenOpen) {
|
||||||
|
reset();
|
||||||
|
} else if (!open && screenOpen) {
|
||||||
|
onScreenOpened(minecraft.getGuiMouseX(), minecraft.getGuiMouseY());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies queued open / close requests after the tick's keyboard and mouse
|
||||||
|
* handling.
|
||||||
|
*/
|
||||||
|
public static void applyPending(Minecraft minecraft) {
|
||||||
|
if (pendingOpen) {
|
||||||
|
pendingOpen = false;
|
||||||
|
if (!open && minecraft.isInWorld() && !minecraft.isScreenOpen()) {
|
||||||
|
minecraft.openInputScreen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pendingClose) {
|
||||||
|
pendingClose = false;
|
||||||
|
if (open) {
|
||||||
|
minecraft.closeInputScreen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void requestOpen() {
|
||||||
|
pendingOpen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void requestClose() {
|
||||||
|
pendingClose = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onMouseMove(double x, double y) {
|
||||||
|
mouseX = x;
|
||||||
|
mouseY = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks the GUI as open immediately, without waiting for the next tick.
|
||||||
|
*/
|
||||||
|
public static void onScreenOpened(double x, double y) {
|
||||||
|
open = true;
|
||||||
|
mouseX = x;
|
||||||
|
mouseY = y;
|
||||||
|
System.out.println("[OrangeX] Input screen opened");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onMouseButton(double x, double y, int button, boolean pressed) {
|
||||||
|
mouseX = x;
|
||||||
|
mouseY = y;
|
||||||
|
|
||||||
|
switch (button) {
|
||||||
|
case 0:
|
||||||
|
leftDown = pressed;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
rightDown = pressed;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
middleDown = pressed;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queues a wheel notch for the next frame. Positive is one notch up.
|
||||||
|
*/
|
||||||
|
public static synchronized void onMouseScroll(double amount) {
|
||||||
|
if (amount != 0.0) {
|
||||||
|
scrollAmount += amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onMouseScroll(double x, double y, double amount) {
|
||||||
|
onMouseMove(x, y);
|
||||||
|
onMouseScroll(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the wheel amount accumulated since the previous call.
|
||||||
|
*/
|
||||||
|
public static synchronized double consumeScroll() {
|
||||||
|
double amount = scrollAmount;
|
||||||
|
scrollAmount = 0.0;
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void reset() {
|
||||||
|
if (open) {
|
||||||
|
System.out.println("[OrangeX] Input screen closed");
|
||||||
|
}
|
||||||
|
open = false;
|
||||||
|
leftDown = false;
|
||||||
|
rightDown = false;
|
||||||
|
middleDown = false;
|
||||||
|
scrollAmount = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isOpen() {
|
||||||
|
return open;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getMouseX() {
|
||||||
|
return mouseX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getMouseY() {
|
||||||
|
return mouseY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isLeftDown() {
|
||||||
|
return leftDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isRightDown() {
|
||||||
|
return rightDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isMiddleDown() {
|
||||||
|
return middleDown;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPost;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
import org.greenrobot.eventbus.ThreadMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drives {@link ClientInput} around the game tick.
|
||||||
|
*/
|
||||||
|
public final class InputController {
|
||||||
|
|
||||||
|
private final Minecraft minecraft;
|
||||||
|
private boolean failureLogged;
|
||||||
|
|
||||||
|
public InputController(Minecraft minecraft) {
|
||||||
|
this.minecraft = minecraft;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.POSTING)
|
||||||
|
public void onTick(EventTickPre event) {
|
||||||
|
try {
|
||||||
|
ClientInput.tick(minecraft);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
logFailure(throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.POSTING)
|
||||||
|
public void onTickPost(EventTickPost event) {
|
||||||
|
try {
|
||||||
|
ClientInput.applyPending(minecraft);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
logFailure(throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logFailure(Throwable throwable) {
|
||||||
|
if (!failureLogged) {
|
||||||
|
failureLogged = true;
|
||||||
|
System.err.println("[OrangeX] Input tick failed");
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Canonical key codes used by {@link cn.xiaozhou233.orangex.event.impl.EventKey}.
|
||||||
|
*
|
||||||
|
* <p>All events use the 1.8.9 / LWJGL 2 numbering, regardless of the running
|
||||||
|
* Minecraft version. Modern versions report GLFW key codes, so
|
||||||
|
* {@link #fromGlfw(int)} translates them before the event is posted. Keys that
|
||||||
|
* did not exist in 1.8.9 map to {@link #NONE}.</p>
|
||||||
|
*/
|
||||||
|
public final class KeyCodes {
|
||||||
|
|
||||||
|
/** LWJGL 2 {@code Keyboard.KEY_NONE}. */
|
||||||
|
public static final int NONE = 0;
|
||||||
|
|
||||||
|
/** LWJGL 2 {@code Keyboard.KEY_M}. */
|
||||||
|
public static final int M = 50;
|
||||||
|
|
||||||
|
/** LWJGL 2 {@code Keyboard.KEY_RSHIFT}. */
|
||||||
|
public static final int RSHIFT = 54;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates a GLFW key code (26.x) to the 1.8.9 LWJGL 2 key code used by
|
||||||
|
* {@code EventKey}.
|
||||||
|
*/
|
||||||
|
public static int fromGlfw(int key) {
|
||||||
|
switch (key) {
|
||||||
|
// Printable keys.
|
||||||
|
case 32: return 57; // SPACE
|
||||||
|
case 39: return 40; // APOSTROPHE
|
||||||
|
case 44: return 51; // COMMA
|
||||||
|
case 45: return 12; // MINUS
|
||||||
|
case 46: return 52; // PERIOD
|
||||||
|
case 47: return 53; // SLASH
|
||||||
|
case 48: return 11; // 0
|
||||||
|
case 49: return 2; // 1
|
||||||
|
case 50: return 3; // 2
|
||||||
|
case 51: return 4; // 3
|
||||||
|
case 52: return 5; // 4
|
||||||
|
case 53: return 6; // 5
|
||||||
|
case 54: return 7; // 6
|
||||||
|
case 55: return 8; // 7
|
||||||
|
case 56: return 9; // 8
|
||||||
|
case 57: return 10; // 9
|
||||||
|
case 59: return 39; // SEMICOLON
|
||||||
|
case 61: return 13; // EQUAL
|
||||||
|
case 65: return 30; // A
|
||||||
|
case 66: return 48; // B
|
||||||
|
case 67: return 46; // C
|
||||||
|
case 68: return 32; // D
|
||||||
|
case 69: return 18; // E
|
||||||
|
case 70: return 33; // F
|
||||||
|
case 71: return 34; // G
|
||||||
|
case 72: return 35; // H
|
||||||
|
case 73: return 23; // I
|
||||||
|
case 74: return 36; // J
|
||||||
|
case 75: return 37; // K
|
||||||
|
case 76: return 38; // L
|
||||||
|
case 77: return 50; // M
|
||||||
|
case 78: return 49; // N
|
||||||
|
case 79: return 24; // O
|
||||||
|
case 80: return 25; // P
|
||||||
|
case 81: return 16; // Q
|
||||||
|
case 82: return 19; // R
|
||||||
|
case 83: return 31; // S
|
||||||
|
case 84: return 20; // T
|
||||||
|
case 85: return 22; // U
|
||||||
|
case 86: return 47; // V
|
||||||
|
case 87: return 17; // W
|
||||||
|
case 88: return 45; // X
|
||||||
|
case 89: return 21; // Y
|
||||||
|
case 90: return 44; // Z
|
||||||
|
case 91: return 26; // LEFT_BRACKET
|
||||||
|
case 92: return 43; // BACKSLASH
|
||||||
|
case 93: return 27; // RIGHT_BRACKET
|
||||||
|
case 96: return 41; // GRAVE_ACCENT
|
||||||
|
|
||||||
|
// Function keys.
|
||||||
|
case 256: return 1; // ESCAPE
|
||||||
|
case 257: return 28; // ENTER
|
||||||
|
case 258: return 15; // TAB
|
||||||
|
case 259: return 14; // BACKSPACE
|
||||||
|
case 260: return 210; // INSERT
|
||||||
|
case 261: return 211; // DELETE
|
||||||
|
case 262: return 205; // RIGHT
|
||||||
|
case 263: return 203; // LEFT
|
||||||
|
case 264: return 208; // DOWN
|
||||||
|
case 265: return 200; // UP
|
||||||
|
case 266: return 201; // PAGE_UP
|
||||||
|
case 267: return 209; // PAGE_DOWN
|
||||||
|
case 268: return 199; // HOME
|
||||||
|
case 269: return 207; // END
|
||||||
|
case 280: return 58; // CAPS_LOCK
|
||||||
|
case 281: return 70; // SCROLL_LOCK
|
||||||
|
case 282: return 69; // NUM_LOCK
|
||||||
|
case 283: return 183; // PRINT_SCREEN
|
||||||
|
case 284: return 197; // PAUSE
|
||||||
|
|
||||||
|
case 290: return 59; // F1
|
||||||
|
case 291: return 60; // F2
|
||||||
|
case 292: return 61; // F3
|
||||||
|
case 293: return 62; // F4
|
||||||
|
case 294: return 63; // F5
|
||||||
|
case 295: return 64; // F6
|
||||||
|
case 296: return 65; // F7
|
||||||
|
case 297: return 66; // F8
|
||||||
|
case 298: return 67; // F9
|
||||||
|
case 299: return 68; // F10
|
||||||
|
case 300: return 87; // F11
|
||||||
|
case 301: return 88; // F12
|
||||||
|
case 302: return 100; // F13
|
||||||
|
case 303: return 101; // F14
|
||||||
|
case 304: return 102; // F15
|
||||||
|
case 305: return 103; // F16
|
||||||
|
case 306: return 104; // F17
|
||||||
|
case 307: return 105; // F18
|
||||||
|
case 308: return 113; // F19
|
||||||
|
|
||||||
|
// Keypad.
|
||||||
|
case 320: return 82; // KP_0
|
||||||
|
case 321: return 79; // KP_1
|
||||||
|
case 322: return 80; // KP_2
|
||||||
|
case 323: return 81; // KP_3
|
||||||
|
case 324: return 75; // KP_4
|
||||||
|
case 325: return 76; // KP_5
|
||||||
|
case 326: return 77; // KP_6
|
||||||
|
case 327: return 71; // KP_7
|
||||||
|
case 328: return 72; // KP_8
|
||||||
|
case 329: return 73; // KP_9
|
||||||
|
case 330: return 83; // KP_DECIMAL
|
||||||
|
case 331: return 181; // KP_DIVIDE
|
||||||
|
case 332: return 55; // KP_MULTIPLY
|
||||||
|
case 333: return 74; // KP_SUBTRACT
|
||||||
|
case 334: return 78; // KP_ADD
|
||||||
|
case 335: return 156; // KP_ENTER
|
||||||
|
case 336: return 141; // KP_EQUAL
|
||||||
|
|
||||||
|
// Modifiers.
|
||||||
|
case 340: return 42; // LEFT_SHIFT
|
||||||
|
case 341: return 29; // LEFT_CONTROL
|
||||||
|
case 342: return 56; // LEFT_ALT
|
||||||
|
case 343: return 219; // LEFT_SUPER
|
||||||
|
case 344: return 54; // RIGHT_SHIFT
|
||||||
|
case 345: return 157; // RIGHT_CONTROL
|
||||||
|
case 346: return 184; // RIGHT_ALT
|
||||||
|
case 347: return 220; // RIGHT_SUPER
|
||||||
|
case 348: return 221; // MENU
|
||||||
|
|
||||||
|
default:
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private KeyCodes() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input.internal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Version-specific implementation of the input screen and the cursor helpers.
|
||||||
|
*
|
||||||
|
* <p>Implementations live in version packages and may reference that
|
||||||
|
* version's classes directly. They are loaded lazily through
|
||||||
|
* {@link cn.xiaozhou233.orangex.wrapper.api.Minecraft} so that a 1.8.9 runtime
|
||||||
|
* never loads 26.2 classes and vice versa.</p>
|
||||||
|
*/
|
||||||
|
public interface VersionInput {
|
||||||
|
|
||||||
|
boolean isInWorld();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code true} when our own empty screen is the current Minecraft screen.
|
||||||
|
*/
|
||||||
|
boolean isInputScreenOpen();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code true} when any Minecraft screen is open, including vanilla ones.
|
||||||
|
*/
|
||||||
|
boolean isScreenOpen();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity check used by injected 1.8.9 code, which runs for every
|
||||||
|
* {@code GuiScreen} instance.
|
||||||
|
*/
|
||||||
|
boolean isInputScreenInstance(Object screen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the empty screen. On both versions this releases the mouse and
|
||||||
|
* stops keyboard / mouse input from reaching the player.
|
||||||
|
*/
|
||||||
|
void openInputScreen();
|
||||||
|
|
||||||
|
void closeInputScreen();
|
||||||
|
|
||||||
|
double getGuiMouseX();
|
||||||
|
|
||||||
|
double getGuiMouseY();
|
||||||
|
}
|
||||||
@@ -0,0 +1,328 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input.internal.v1_8_9;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.facade.InputEvents;
|
||||||
|
import cn.xiaozhou233.orangex.hook.ClassPoolUtils;
|
||||||
|
import cn.xiaozhou233.orangex.hook.HookManager;
|
||||||
|
import cn.xiaozhou233.orangex.hook.JuiceAgentBridge;
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
import cn.xiaozhou233.orangex.input.internal.VersionInput;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingClass;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingField;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import javassist.ClassPool;
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtConstructor;
|
||||||
|
import org.lwjgl.input.Keyboard;
|
||||||
|
import org.lwjgl.input.Mouse;
|
||||||
|
import org.lwjgl.opengl.Display;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1.8.9 input screen.
|
||||||
|
*
|
||||||
|
* <p>{@code GuiScreen} is abstract in 1.8.9, so a concrete empty subclass is
|
||||||
|
* generated at runtime and defined into the game class loader. Opening it via
|
||||||
|
* {@code Minecraft#displayGuiScreen} calls {@code setIngameNotInFocus()},
|
||||||
|
* which ungrabs the cursor, and while {@code currentScreen != null} the
|
||||||
|
* player's mouse and keyboard input is not processed.</p>
|
||||||
|
*
|
||||||
|
* <p>This class imports LWJGL 2 directly and is therefore loaded only on
|
||||||
|
* 1.8.9 through a reflective factory in the wrapper.</p>
|
||||||
|
*/
|
||||||
|
public final class V1_8_9Input implements VersionInput {
|
||||||
|
|
||||||
|
private static final String MINECRAFT_CLASS = "net/minecraft/client/Minecraft";
|
||||||
|
private static final String GUI_SCREEN_CLASS = "net/minecraft/client/gui/GuiScreen";
|
||||||
|
private static final String GENERATED_SCREEN =
|
||||||
|
"cn.xiaozhou233.orangex.input.internal.v1_8_9.GeneratedInputScreen";
|
||||||
|
|
||||||
|
private final Object minecraft;
|
||||||
|
private final ClassLoader loader;
|
||||||
|
|
||||||
|
private final Class<?> guiScreenClass;
|
||||||
|
private final Field currentScreenField;
|
||||||
|
private final Field worldField;
|
||||||
|
private final Field screenWidthField;
|
||||||
|
private final Field screenHeightField;
|
||||||
|
private final Method displayGuiScreen;
|
||||||
|
|
||||||
|
private Object shellScreen;
|
||||||
|
private Class<?> inputScreenClass;
|
||||||
|
|
||||||
|
public V1_8_9Input(RuntimeInfo runtimeInfo,
|
||||||
|
MappingResolver resolver,
|
||||||
|
Object minecraft) {
|
||||||
|
this.minecraft = minecraft;
|
||||||
|
|
||||||
|
MappingClass minecraftMapping = resolver.mapClass(MINECRAFT_CLASS);
|
||||||
|
MappingClass guiScreenMapping = resolver.mapClass(GUI_SCREEN_CLASS);
|
||||||
|
|
||||||
|
ClassLoader loader = runtimeInfo.getMinecraftClassLoader();
|
||||||
|
this.loader = loader;
|
||||||
|
Class<?> minecraftClass = loadClass(
|
||||||
|
minecraftMapping.getTargetName(), loader);
|
||||||
|
|
||||||
|
this.guiScreenClass = loadClass(
|
||||||
|
guiScreenMapping.getTargetName(), loader);
|
||||||
|
this.currentScreenField = findField(
|
||||||
|
minecraftClass,
|
||||||
|
minecraftMapping.getField("currentScreen").getTargetName());
|
||||||
|
this.worldField = findField(
|
||||||
|
minecraftClass,
|
||||||
|
minecraftMapping.getField("theWorld").getTargetName());
|
||||||
|
|
||||||
|
this.screenWidthField = findField(
|
||||||
|
guiScreenClass,
|
||||||
|
guiScreenMapping.getField("width").getTargetName());
|
||||||
|
this.screenHeightField = findField(
|
||||||
|
guiScreenClass,
|
||||||
|
guiScreenMapping.getField("height").getTargetName());
|
||||||
|
|
||||||
|
MappingMethod displayMapping = resolver.mapMethod(
|
||||||
|
MINECRAFT_CLASS,
|
||||||
|
"displayGuiScreen",
|
||||||
|
"(Lnet/minecraft/client/gui/GuiScreen;)V");
|
||||||
|
this.displayGuiScreen = findDisplayGuiScreen(
|
||||||
|
minecraftClass, displayMapping.getTargetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class<?> loadClass(String internalName, ClassLoader loader) {
|
||||||
|
try {
|
||||||
|
return Class.forName(
|
||||||
|
internalName.replace('/', '.'), false, loader);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot load " + internalName, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Field findField(Class<?> owner, String name) {
|
||||||
|
for (Class<?> current = owner; current != null; current = current.getSuperclass()) {
|
||||||
|
try {
|
||||||
|
Field field = current.getDeclaredField(name);
|
||||||
|
field.setAccessible(true);
|
||||||
|
return field;
|
||||||
|
} catch (NoSuchFieldException ignored) {
|
||||||
|
// Continue with the superclass.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot find field '" + name + "' on " + owner.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Method findDisplayGuiScreen(Class<?> minecraftClass, String name) {
|
||||||
|
for (Method method : minecraftClass.getDeclaredMethods()) {
|
||||||
|
if (!method.getName().equals(name)
|
||||||
|
|| method.getParameterTypes().length != 1
|
||||||
|
|| Modifier.isStatic(method.getModifiers())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (method.getParameterTypes()[0].isAssignableFrom(guiScreenClass)) {
|
||||||
|
method.setAccessible(true);
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Method method = minecraftClass.getDeclaredMethod(name, guiScreenClass);
|
||||||
|
method.setAccessible(true);
|
||||||
|
return method;
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot find displayGuiScreen on " + minecraftClass.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInWorld() {
|
||||||
|
try {
|
||||||
|
return worldField.get(minecraft) != null;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new IllegalStateException("Cannot read the world field", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInputScreenOpen() {
|
||||||
|
if (shellScreen == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return currentScreenField.get(minecraft) == shellScreen;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new IllegalStateException("Cannot read the current screen", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isScreenOpen() {
|
||||||
|
try {
|
||||||
|
return currentScreenField.get(minecraft) != null;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new IllegalStateException("Cannot read the current screen", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInputScreenInstance(Object screen) {
|
||||||
|
return shellScreen != null && shellScreen == screen;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void openInputScreen() {
|
||||||
|
if (isInputScreenOpen()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Object screen = inputScreenClass().getDeclaredConstructor().newInstance();
|
||||||
|
shellScreen = screen;
|
||||||
|
displayGuiScreen.invoke(minecraft, screen);
|
||||||
|
ClientInput.onScreenOpened(getGuiMouseX(), getGuiMouseY());
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
shellScreen = null;
|
||||||
|
throw new IllegalStateException("Cannot open the 1.8.9 input screen", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code GuiScreen} is abstract in 1.8.9, so generate a concrete empty
|
||||||
|
* subclass at runtime and define it into the game class loader. Doing it
|
||||||
|
* with bytecode avoids hardcoding obfuscated class names.
|
||||||
|
*/
|
||||||
|
private synchronized Class<?> inputScreenClass() {
|
||||||
|
if (inputScreenClass != null) {
|
||||||
|
return inputScreenClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ClassPool pool = ClassPoolUtils.createPool(loader);
|
||||||
|
|
||||||
|
CtClass base;
|
||||||
|
byte[] cachedBase = HookManager.getTargetBytes(
|
||||||
|
guiScreenClass.getName());
|
||||||
|
if (cachedBase != null) {
|
||||||
|
// Prefer the original bytes captured before our hooks were
|
||||||
|
// applied: retransforming GuiScreen again could drop the
|
||||||
|
// injected methods.
|
||||||
|
base = pool.makeClass(new ByteArrayInputStream(cachedBase));
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
base = pool.get(guiScreenClass.getName());
|
||||||
|
} catch (Throwable notFound) {
|
||||||
|
// Some launch environments hide transformed class resources;
|
||||||
|
// fall back to the bytes the agent can read for the loaded class.
|
||||||
|
byte[] baseBytes = JuiceAgentBridge.getClassBytesByName(
|
||||||
|
guiScreenClass.getName().replace('.', '/'));
|
||||||
|
if (baseBytes == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot load " + guiScreenClass.getName(), notFound);
|
||||||
|
}
|
||||||
|
base = pool.makeClass(new ByteArrayInputStream(baseBytes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CtClass generated = pool.makeClass(GENERATED_SCREEN);
|
||||||
|
generated.setModifiers(Modifier.PUBLIC);
|
||||||
|
generated.setSuperclass(base);
|
||||||
|
|
||||||
|
CtConstructor constructor = new CtConstructor(new CtClass[0], generated);
|
||||||
|
constructor.setModifiers(Modifier.PUBLIC);
|
||||||
|
constructor.setBody("super();");
|
||||||
|
generated.addConstructor(constructor);
|
||||||
|
|
||||||
|
byte[] bytes;
|
||||||
|
try {
|
||||||
|
bytes = generated.toBytecode();
|
||||||
|
} finally {
|
||||||
|
generated.detach();
|
||||||
|
base.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> defined = JuiceAgentBridge.defineClass(loader, bytes);
|
||||||
|
if (defined == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"JuiceAgent.defineClass returned null");
|
||||||
|
}
|
||||||
|
inputScreenClass = defined;
|
||||||
|
return inputScreenClass;
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot generate the 1.8.9 input screen", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void closeInputScreen() {
|
||||||
|
if (!isInputScreenOpen()) {
|
||||||
|
shellScreen = null;
|
||||||
|
ClientInput.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
displayGuiScreen.invoke(minecraft, new Object[]{null});
|
||||||
|
shellScreen = null;
|
||||||
|
ClientInput.reset();
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new IllegalStateException("Cannot close the 1.8.9 input screen", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used by injected {@code GuiScreen} code to ignore LWJGL auto-repeat
|
||||||
|
* events.
|
||||||
|
*/
|
||||||
|
public static boolean isRepeatEvent() {
|
||||||
|
return Keyboard.isRepeatEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called after {@code GuiScreen#handleMouseInput} ran. The wheel event is
|
||||||
|
* still readable at this point and must not consume the vanilla mouse
|
||||||
|
* button dispatch.
|
||||||
|
*/
|
||||||
|
public static void forwardScroll(Object screen) {
|
||||||
|
if (!InputEvents.isOurInputScreen(screen)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int wheel = Mouse.getEventDWheel();
|
||||||
|
if (wheel != 0) {
|
||||||
|
ClientInput.onMouseScroll(wheel > 0 ? 1.0 : -1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getGuiMouseX() {
|
||||||
|
Object screen = shellScreen;
|
||||||
|
if (screen == null) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int width = screenWidthField.getInt(screen);
|
||||||
|
return (double) Mouse.getX() * (double) width / (double) Display.getWidth();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new IllegalStateException("Cannot read the GUI width", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getGuiMouseY() {
|
||||||
|
Object screen = shellScreen;
|
||||||
|
if (screen == null) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int height = screenHeightField.getInt(screen);
|
||||||
|
return (double) height
|
||||||
|
- (double) Mouse.getY() * (double) height / (double) Display.getHeight()
|
||||||
|
- 1.0;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new IllegalStateException("Cannot read the GUI height", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input.v26_2;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
import cn.xiaozhou233.orangex.input.internal.VersionInput;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.gui.screens.Screen;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 26.2 input implementation.
|
||||||
|
*
|
||||||
|
* <p>Loaded reflectively only on 26.2; the wrapper keeps 1.8.9 runtimes free
|
||||||
|
* of these references.</p>
|
||||||
|
*/
|
||||||
|
public final class V26_2Input implements VersionInput {
|
||||||
|
|
||||||
|
private final Minecraft minecraft;
|
||||||
|
|
||||||
|
private Screen shellScreen;
|
||||||
|
|
||||||
|
public V26_2Input(RuntimeInfo runtimeInfo, Object minecraft) {
|
||||||
|
this.minecraft = (Minecraft) minecraft;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInWorld() {
|
||||||
|
return minecraft.level != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInputScreenOpen() {
|
||||||
|
return shellScreen != null && minecraft.gui.screen() == shellScreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isScreenOpen() {
|
||||||
|
return minecraft.gui.screen() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInputScreenInstance(Object screen) {
|
||||||
|
return shellScreen != null && shellScreen == screen;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void openInputScreen() {
|
||||||
|
if (isInputScreenOpen()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
shellScreen = new V26_2InputScreen();
|
||||||
|
minecraft.gui.setScreen(shellScreen);
|
||||||
|
ClientInput.onScreenOpened(getGuiMouseX(), getGuiMouseY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void closeInputScreen() {
|
||||||
|
if (!isInputScreenOpen()) {
|
||||||
|
shellScreen = null;
|
||||||
|
ClientInput.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
minecraft.gui.setScreen(null);
|
||||||
|
shellScreen = null;
|
||||||
|
ClientInput.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getGuiMouseX() {
|
||||||
|
return minecraft.mouseHandler.getScaledXPos(minecraft.getWindow());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getGuiMouseY() {
|
||||||
|
return minecraft.mouseHandler.getScaledYPos(minecraft.getWindow());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package cn.xiaozhou233.orangex.input.v26_2;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||||
|
import net.minecraft.client.gui.screens.Screen;
|
||||||
|
import net.minecraft.client.input.MouseButtonEvent;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty input screen for 26.2.
|
||||||
|
*
|
||||||
|
* <p>The client GUI is still rendered by the HUD hook; this screen only exists
|
||||||
|
* to move the game into its "screen open" state. Doing so makes
|
||||||
|
* {@code Gui#setScreen} release the mouse and clear key mappings, and routes
|
||||||
|
* mouse events to this instance instead of the player.</p>
|
||||||
|
*/
|
||||||
|
public final class V26_2InputScreen extends Screen {
|
||||||
|
|
||||||
|
public V26_2InputScreen() {
|
||||||
|
super(Component.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void extractBackground(GuiGraphicsExtractor graphics,
|
||||||
|
int mouseX, int mouseY, float partialTick) {
|
||||||
|
// No blur, panorama, menu texture or dim layer.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void extractRenderState(GuiGraphicsExtractor graphics,
|
||||||
|
int mouseX, int mouseY, float partialTick) {
|
||||||
|
// Drawn by the HUD hook instead.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isPauseScreen() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseMoved(double x, double y) {
|
||||||
|
ClientInput.onMouseMove(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseClicked(MouseButtonEvent event, boolean doubleClick) {
|
||||||
|
ClientInput.onMouseButton(event.x(), event.y(), event.button(), true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseReleased(MouseButtonEvent event) {
|
||||||
|
ClientInput.onMouseButton(event.x(), event.y(), event.button(), false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseDragged(MouseButtonEvent event, double dragX, double dragY) {
|
||||||
|
ClientInput.onMouseMove(event.x(), event.y());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseScrolled(double x, double y,
|
||||||
|
double scrollX, double scrollY) {
|
||||||
|
ClientInput.onMouseScroll(x, y, scrollY);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
package cn.xiaozhou233.orangex.modules;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.config.ConfigManager;
|
||||||
|
import cn.xiaozhou233.orangex.input.KeyCodes;
|
||||||
|
import cn.xiaozhou233.orangex.value.BooleanValue;
|
||||||
|
import cn.xiaozhou233.orangex.value.KeybindValue;
|
||||||
|
import cn.xiaozhou233.orangex.value.ModeValue;
|
||||||
|
import cn.xiaozhou233.orangex.value.Value;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public abstract class Module {
|
||||||
|
protected final Minecraft mc = OrangeX.getInstance().getMinecraft();
|
||||||
|
protected final String name;
|
||||||
|
protected final ModuleCategory category;
|
||||||
|
protected boolean enabled = false;
|
||||||
|
|
||||||
|
private final Map<String, Value<?>> valueMap = new LinkedHashMap<>();
|
||||||
|
private final KeybindValue keybind;
|
||||||
|
|
||||||
|
public Module(String name, ModuleCategory category) {
|
||||||
|
this(name, category, KeyCodes.NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Module(String name, ModuleCategory category, int defaultKey) {
|
||||||
|
this.name = name;
|
||||||
|
this.category = category;
|
||||||
|
this.keybind = new KeybindValue("keybind", defaultKey);
|
||||||
|
valueMap.put(keybind.getName(), keybind);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a value so it can be displayed by configuration UIs and
|
||||||
|
* persisted with the module.
|
||||||
|
*
|
||||||
|
* @return the same value, for fluent field initialisation.
|
||||||
|
*/
|
||||||
|
protected <V extends Value<?>> V addValue(V value) {
|
||||||
|
if (valueMap.containsKey(value.getName())) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Module '" + name + "' already has a value named '" + value.getName() + "'");
|
||||||
|
}
|
||||||
|
valueMap.put(value.getName(), value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BooleanValue addBoolean(String name, boolean defaultValue) {
|
||||||
|
return addValue(new BooleanValue(name, defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected KeybindValue addKeybind(String name, int defaultKey) {
|
||||||
|
return addValue(new KeybindValue(name, defaultKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ModeValue addMode(String name, String defaultMode, String... modes) {
|
||||||
|
return addValue(new ModeValue(name, defaultMode, modes));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stable key used for persistence. Keeping the class name instead of the
|
||||||
|
* display name means renamed modules keep their stored settings.
|
||||||
|
*/
|
||||||
|
public String getConfigKey() {
|
||||||
|
return getClass().getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Value<?> getValue(String name) {
|
||||||
|
return valueMap.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Value<?>> getValues() {
|
||||||
|
return Collections.unmodifiableList(new ArrayList<>(valueMap.values()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Value<?>> getVisibleValues() {
|
||||||
|
List<Value<?>> visible = new ArrayList<>();
|
||||||
|
for (Value<?> value : valueMap.values()) {
|
||||||
|
if (value.isVisible()) {
|
||||||
|
visible.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetValues() {
|
||||||
|
for (Value<?> value : valueMap.values()) {
|
||||||
|
value.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes every value (including the module keybind) for configuration
|
||||||
|
* storage.
|
||||||
|
*/
|
||||||
|
public JsonObject valuesToJson() {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
for (Value<?> value : valueMap.values()) {
|
||||||
|
json.add(value.getName(), value.toJson());
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores previously encoded values. Unknown keys are ignored.
|
||||||
|
*/
|
||||||
|
public void valuesFromJson(JsonObject json) {
|
||||||
|
if (json == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Value<?> value : valueMap.values()) {
|
||||||
|
if (json.has(value.getName())) {
|
||||||
|
value.fromJson(json.get(value.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onEnable() {
|
||||||
|
System.out.println("[OrangeX] Enable " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onDisable() {
|
||||||
|
System.out.println("[OrangeX] Disable " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean setEnabled) {
|
||||||
|
if (this.enabled == setEnabled) return;
|
||||||
|
|
||||||
|
this.enabled = setEnabled;
|
||||||
|
markConfigDirty();
|
||||||
|
|
||||||
|
if (this.enabled) {
|
||||||
|
if (OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||||
|
return;
|
||||||
|
try { OrangeX.getInstance().getEventBus().register(this); } catch (Exception ignored) {}
|
||||||
|
onEnable();
|
||||||
|
} else {
|
||||||
|
if (!OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||||
|
return;
|
||||||
|
try { OrangeX.getInstance().getEventBus().unregister(this); } catch (Exception ignored) {}
|
||||||
|
onDisable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toggle() {
|
||||||
|
setEnabled(!this.enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markConfigDirty() {
|
||||||
|
ConfigManager configManager = OrangeX.getInstance().getConfigManager();
|
||||||
|
if (configManager != null) {
|
||||||
|
configManager.markDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package cn.xiaozhou233.orangex.modules;
|
||||||
|
|
||||||
|
public enum ModuleCategory {
|
||||||
|
RENDER,
|
||||||
|
COMBAT,
|
||||||
|
WORLD,
|
||||||
|
MISC,
|
||||||
|
MOVEMENT,
|
||||||
|
PLAYER,
|
||||||
|
FUN
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package cn.xiaozhou233.orangex.modules;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventKey;
|
||||||
|
import cn.xiaozhou233.orangex.modules.impl.movement.NoJumpDelay;
|
||||||
|
import cn.xiaozhou233.orangex.modules.impl.render.ClickGUI;
|
||||||
|
import cn.xiaozhou233.orangex.modules.impl.render.ESP;
|
||||||
|
import cn.xiaozhou233.orangex.modules.impl.render.HUD;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ModuleManager {
|
||||||
|
private static final List<Module> modules = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
public ModuleManager() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
registerAll();
|
||||||
|
OrangeX.getInstance().getEventBus().register(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerAll() {
|
||||||
|
register(new HUD());
|
||||||
|
register(new ClickGUI());
|
||||||
|
register(new ESP());
|
||||||
|
register(new NoJumpDelay());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(Module module) {
|
||||||
|
modules.add(module);
|
||||||
|
System.out.println("[ModuleManager] Registered module: " + module.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Module getModule(String name) {
|
||||||
|
return modules.stream()
|
||||||
|
.filter(m -> m.getName().equalsIgnoreCase(name))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Module> getModulesByCategory(ModuleCategory category) {
|
||||||
|
return modules.stream()
|
||||||
|
.filter(m -> m.getCategory() == category)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Module> getAllModules() {
|
||||||
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onKey(EventKey event) {
|
||||||
|
for (Module module : modules) {
|
||||||
|
if (module.getKeybind().getValue() == event.getKey()) {
|
||||||
|
module.toggle();
|
||||||
|
System.out.println("[ModuleManager] Toggled module: " + module.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package cn.xiaozhou233.orangex.modules.impl.movement;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleCategory;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Player;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the client-side jump cooldown.
|
||||||
|
*
|
||||||
|
* <p>Vanilla keeps a short timer after every jump before the next one can
|
||||||
|
* start. Clearing it before the player tick lets holding the jump key trigger
|
||||||
|
* a new jump as soon as the player touches the ground again.</p>
|
||||||
|
*/
|
||||||
|
public class NoJumpDelay extends Module {
|
||||||
|
|
||||||
|
private boolean warned;
|
||||||
|
|
||||||
|
public NoJumpDelay() {
|
||||||
|
super("NoJumpDelay", ModuleCategory.MOVEMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onTickPre(EventTickPre event) {
|
||||||
|
Player player = OrangeX.getInstance().getPlayer();
|
||||||
|
if (player == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
player.clearJumpDelay();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
// Do not retry a broken reflection lookup on every single tick.
|
||||||
|
if (!warned) {
|
||||||
|
warned = true;
|
||||||
|
System.err.println("[NoJumpDelay] Failed to clear jump delay: "
|
||||||
|
+ throwable.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package cn.xiaozhou233.orangex.modules.impl.render;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
import cn.xiaozhou233.orangex.input.KeyCodes;
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleCategory;
|
||||||
|
import cn.xiaozhou233.orangex.ui.clickgui.ClickGuiRender;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Host module for the ClickGUI.
|
||||||
|
*
|
||||||
|
* <p>RSHIFT is the default keybind. Enabling the module opens the empty input
|
||||||
|
* screen and disabling it closes the screen again. Draw the actual GUI in
|
||||||
|
* {@link #onRender2D(EventRender2D)}.</p>
|
||||||
|
*/
|
||||||
|
public class ClickGUI extends Module {
|
||||||
|
|
||||||
|
private boolean justEnabled;
|
||||||
|
|
||||||
|
public ClickGUI() {
|
||||||
|
super("ClickGUI", ModuleCategory.RENDER, KeyCodes.RSHIFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onEnable() {
|
||||||
|
super.onEnable();
|
||||||
|
if (!mc.isInWorld() || mc.isScreenOpen()) {
|
||||||
|
// The input screen cannot open right now (title screen, chat,
|
||||||
|
// inventory, ...); do not leave the module in a half-enabled state.
|
||||||
|
setEnabled(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
justEnabled = true;
|
||||||
|
ClickGuiRender.reset();
|
||||||
|
// Defer opening to EventTickPost: opening inside the keyboard callback
|
||||||
|
// would let the same RSHIFT event reach the new screen and close it.
|
||||||
|
ClientInput.requestOpen();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDisable() {
|
||||||
|
justEnabled = false;
|
||||||
|
ClickGuiRender.reset();
|
||||||
|
ClientInput.requestClose();
|
||||||
|
if (OrangeX.getInstance().getConfigManager() != null) {
|
||||||
|
OrangeX.getInstance().getConfigManager().saveIfDirty();
|
||||||
|
}
|
||||||
|
super.onDisable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onTick(EventTickPre event) {
|
||||||
|
if (justEnabled) {
|
||||||
|
justEnabled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!ClientInput.isOpen()) {
|
||||||
|
// The screen was closed by vanilla handling (for example ESC);
|
||||||
|
// keep the module state in sync instead of requiring two RSHIFT
|
||||||
|
// presses to open it again.
|
||||||
|
setEnabled(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onRender2D(EventRender2D event) {
|
||||||
|
if (!ClientInput.isOpen()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ClickGuiRender.render(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,288 @@
|
|||||||
|
package cn.xiaozhou233.orangex.modules.impl.render;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleCategory;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.value.BooleanValue;
|
||||||
|
import cn.xiaozhou233.orangex.value.ModeValue;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.WorldView;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.common.EntityTarget;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.common.ScreenPoint;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.common.Vec3;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.common.WorldSnapshot;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2D ESP: projects entities to the screen and draws a box, name, distance and
|
||||||
|
* health bar around each one.
|
||||||
|
*
|
||||||
|
* <p>All drawing happens in GUI pixel space through {@link Render2D}, so it
|
||||||
|
* works with both the 1.8.9 and the modern font backends. World data comes
|
||||||
|
* from the cross-version {@link WorldView} wrapper, never from game classes
|
||||||
|
* directly. The wrapper reports every entity; the target-type filters below
|
||||||
|
* decide which kinds are drawn.</p>
|
||||||
|
*/
|
||||||
|
public class ESP extends Module {
|
||||||
|
|
||||||
|
private static final float NAME_FONT_SIZE = 9.0F;
|
||||||
|
|
||||||
|
private static final double NAME_GAP = 2.0D;
|
||||||
|
private static final double BOX_PADDING = 0.5D;
|
||||||
|
private static final double CORNER_LENGTH = 4.0D;
|
||||||
|
private static final double HEALTH_BAR_WIDTH = 2.0D;
|
||||||
|
private static final double HEALTH_BAR_GAP = 2.0D;
|
||||||
|
|
||||||
|
private static final double MIN_BOX_HEIGHT = 2.0D;
|
||||||
|
private static final double MIN_BOX_WIDTH = 2.0D;
|
||||||
|
|
||||||
|
private static final int ORANGE = 0xFFFF8A00;
|
||||||
|
private static final int FILL_ALPHA = 32;
|
||||||
|
private static final int HEALTH_BACKGROUND = 0x80000000;
|
||||||
|
|
||||||
|
private final BooleanValue players = addBoolean("Players", true);
|
||||||
|
private final BooleanValue localPlayer = addBoolean("Local Player", false);
|
||||||
|
private final BooleanValue monsters = addBoolean("Monsters", false);
|
||||||
|
private final BooleanValue animals = addBoolean("Animals", false);
|
||||||
|
private final BooleanValue villagers = addBoolean("Villagers", false);
|
||||||
|
private final BooleanValue others = addBoolean("Others", false);
|
||||||
|
private final BooleanValue invisible = addBoolean("Invisible", false);
|
||||||
|
|
||||||
|
private final BooleanValue box = addBoolean("Box", true);
|
||||||
|
private final BooleanValue fill = addBoolean("Fill", true);
|
||||||
|
private final BooleanValue health = addBoolean("Health", true);
|
||||||
|
private final BooleanValue name = addBoolean("Name", true);
|
||||||
|
private final BooleanValue distance = addBoolean("Distance", true);
|
||||||
|
private final ModeValue style = addMode("Style", "Box", "Box", "Corners");
|
||||||
|
private final ModeValue colorMode = addMode("Color", "Orange", "Orange", "Health", "Rainbow");
|
||||||
|
|
||||||
|
public ESP() {
|
||||||
|
super("ESP", ModuleCategory.RENDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onRender2D(EventRender2D event) {
|
||||||
|
if (!event.isInWorld()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
WorldView world = OrangeX.getInstance().getWorldView();
|
||||||
|
if (world == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
WorldSnapshot snapshot;
|
||||||
|
try {
|
||||||
|
snapshot = world.snapshot(event.getPartialTicks());
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
// A broken wrapper must not kill the whole HUD frame.
|
||||||
|
System.err.println("[ESP] Failed to read world snapshot: "
|
||||||
|
+ throwable.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (snapshot == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Render2D graphics = event.getRender2D();
|
||||||
|
int screenWidth = graphics.getWidth();
|
||||||
|
int screenHeight = graphics.getHeight();
|
||||||
|
|
||||||
|
FontRenderer font = graphics.getFont(NAME_FONT_SIZE);
|
||||||
|
double fontHeight = font.getStringHeight();
|
||||||
|
|
||||||
|
List<EntityTarget> targets = snapshot.getTargets();
|
||||||
|
for (int index = 0; index < targets.size(); index++) {
|
||||||
|
EntityTarget target = targets.get(index);
|
||||||
|
if (!isTargetEnabled(target)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
renderTarget(graphics, font, fontHeight, snapshot, target, index,
|
||||||
|
screenWidth, screenHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Target filters live here rather than in the world wrapper so the same
|
||||||
|
* snapshot can feed future features (tracers, nametags, ...) without each
|
||||||
|
* of them re-implementing the rules.
|
||||||
|
*/
|
||||||
|
private boolean isTargetEnabled(EntityTarget target) {
|
||||||
|
// Invisible entities are their own category: they are drawn only when
|
||||||
|
// "Invisible" is enabled, no matter which kind filters are on. Turn
|
||||||
|
// every kind filter off and keep only this one on for an invisible-only
|
||||||
|
// ESP.
|
||||||
|
if (target.isLocalPlayer()) {
|
||||||
|
return localPlayer.isEnabled()
|
||||||
|
&& (!target.isInvisible() || invisible.isEnabled());
|
||||||
|
}
|
||||||
|
if (target.isInvisible()) {
|
||||||
|
return invisible.isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (target.getKind()) {
|
||||||
|
case PLAYER:
|
||||||
|
return players.isEnabled();
|
||||||
|
case MONSTER:
|
||||||
|
return monsters.isEnabled();
|
||||||
|
case ANIMAL:
|
||||||
|
return animals.isEnabled();
|
||||||
|
case VILLAGER:
|
||||||
|
return villagers.isEnabled();
|
||||||
|
case OTHER:
|
||||||
|
default:
|
||||||
|
return others.isEnabled();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderTarget(Render2D graphics,
|
||||||
|
FontRenderer font,
|
||||||
|
double fontHeight,
|
||||||
|
WorldSnapshot snapshot,
|
||||||
|
EntityTarget target,
|
||||||
|
int index,
|
||||||
|
int screenWidth,
|
||||||
|
int screenHeight) {
|
||||||
|
if (target.getHeight() <= 0.0D) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vec3 feet = target.getPosition();
|
||||||
|
Vec3 head = new Vec3(feet.getX(), feet.getY() + target.getHeight(), feet.getZ());
|
||||||
|
|
||||||
|
ScreenPoint feetPoint = snapshot.project(feet, screenWidth, screenHeight);
|
||||||
|
ScreenPoint headPoint = snapshot.project(head, screenWidth, screenHeight);
|
||||||
|
|
||||||
|
if (!feetPoint.isVisible() || !headPoint.isVisible()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double boxHeight = feetPoint.getY() - headPoint.getY();
|
||||||
|
if (boxHeight < MIN_BOX_HEIGHT) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double boxWidth = Math.max(MIN_BOX_WIDTH,
|
||||||
|
boxHeight * (target.getWidth() / target.getHeight()));
|
||||||
|
double centerX = (feetPoint.getX() + headPoint.getX()) * 0.5D;
|
||||||
|
double left = centerX - boxWidth * 0.5D;
|
||||||
|
double top = headPoint.getY();
|
||||||
|
|
||||||
|
int color = color(target, index);
|
||||||
|
|
||||||
|
if (fill.isEnabled()) {
|
||||||
|
graphics.fillRect(left, top, boxWidth, boxHeight,
|
||||||
|
(color & 0x00FFFFFF) | (FILL_ALPHA << 24));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (box.isEnabled()) {
|
||||||
|
if (style.is("Corners")) {
|
||||||
|
drawCorners(graphics, left, top, boxWidth, boxHeight, color);
|
||||||
|
} else {
|
||||||
|
graphics.drawRect(left - BOX_PADDING, top - BOX_PADDING,
|
||||||
|
boxWidth + BOX_PADDING * 2.0D,
|
||||||
|
boxHeight + BOX_PADDING * 2.0D,
|
||||||
|
1.0D, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (health.isEnabled()) {
|
||||||
|
drawHealthBar(graphics, target, left - HEALTH_BAR_GAP, top, boxHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
String label = label(target);
|
||||||
|
if (label == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double textWidth = font.getStringWidth(label);
|
||||||
|
double textY = top - fontHeight - NAME_GAP;
|
||||||
|
font.drawStringWithShadow(label, centerX - textWidth * 0.5D, textY, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawCorners(Render2D graphics, double left, double top,
|
||||||
|
double width, double height, int color) {
|
||||||
|
double length = Math.min(CORNER_LENGTH, Math.min(width, height) * 0.5D);
|
||||||
|
if (length <= 0.0D) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double right = left + width;
|
||||||
|
double bottom = top + height;
|
||||||
|
|
||||||
|
// Top-left
|
||||||
|
graphics.fillRect(left, top, length, 1.0D, color);
|
||||||
|
graphics.fillRect(left, top, 1.0D, length, color);
|
||||||
|
|
||||||
|
// Top-right
|
||||||
|
graphics.fillRect(right - length, top, length, 1.0D, color);
|
||||||
|
graphics.fillRect(right - 1.0D, top, 1.0D, length, color);
|
||||||
|
|
||||||
|
// Bottom-left
|
||||||
|
graphics.fillRect(left, bottom - 1.0D, length, 1.0D, color);
|
||||||
|
graphics.fillRect(left, bottom - length, 1.0D, length, color);
|
||||||
|
|
||||||
|
// Bottom-right
|
||||||
|
graphics.fillRect(right - length, bottom - 1.0D, length, 1.0D, color);
|
||||||
|
graphics.fillRect(right - 1.0D, bottom - length, 1.0D, length, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawHealthBar(Render2D graphics, EntityTarget target,
|
||||||
|
double x, double top, double height) {
|
||||||
|
if (target.getMaxHealth() <= 0.0F) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float fraction = target.getHealth() / target.getMaxHealth();
|
||||||
|
fraction = Math.max(0.0F, Math.min(1.0F, fraction));
|
||||||
|
|
||||||
|
double filled = height * fraction;
|
||||||
|
|
||||||
|
graphics.fillRect(x, top, HEALTH_BAR_WIDTH, height, HEALTH_BACKGROUND);
|
||||||
|
graphics.fillRect(x, top + height - filled, HEALTH_BAR_WIDTH, filled,
|
||||||
|
healthColor(fraction));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String label(EntityTarget target) {
|
||||||
|
String nameText = name.isEnabled() ? target.getName() : null;
|
||||||
|
String distanceText = distance.isEnabled()
|
||||||
|
? String.format(Locale.ROOT, "%.1fm", target.getDistance())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (nameText == null) {
|
||||||
|
return distanceText;
|
||||||
|
}
|
||||||
|
if (distanceText == null) {
|
||||||
|
return nameText;
|
||||||
|
}
|
||||||
|
return nameText + " " + distanceText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int color(EntityTarget target, int index) {
|
||||||
|
if (colorMode.is("Health")) {
|
||||||
|
float fraction = target.getMaxHealth() <= 0.0F
|
||||||
|
? 1.0F
|
||||||
|
: target.getHealth() / target.getMaxHealth();
|
||||||
|
return healthColor(Math.max(0.0F, Math.min(1.0F, fraction)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colorMode.is("Rainbow")) {
|
||||||
|
float hue = ((System.currentTimeMillis() % 4000L) / 4000.0F)
|
||||||
|
+ index * 0.05F;
|
||||||
|
return Color.HSBtoRGB(hue - (float) Math.floor(hue), 0.85F, 1.0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ORANGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int healthColor(float fraction) {
|
||||||
|
return Color.HSBtoRGB(0.33F * fraction, 0.85F, 1.0F);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
|
||||||
|
package cn.xiaozhou233.orangex.modules.impl.render;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleCategory;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class HUD extends Module {
|
||||||
|
|
||||||
|
private static final float RAINBOW_CYCLE = 6000f;
|
||||||
|
|
||||||
|
private static final int LOGO_FONT_SIZE = 22;
|
||||||
|
private static final int MODULE_FONT_SIZE = 10;
|
||||||
|
|
||||||
|
private static final int LOGO_START_X = 5;
|
||||||
|
private static final int LOGO_START_Y = 5;
|
||||||
|
|
||||||
|
private static final int MODULE_MARGIN = 6;
|
||||||
|
private static final int MODULE_SPACING = 4;
|
||||||
|
|
||||||
|
private static final int RAINBOW_OFFSET = 200;
|
||||||
|
|
||||||
|
public HUD() {
|
||||||
|
super("HUD", ModuleCategory.RENDER);
|
||||||
|
setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onRender2D(EventRender2D event) {
|
||||||
|
long currentTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
renderLogo(event, currentTime);
|
||||||
|
renderModuleList(event, currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderLogo(EventRender2D event, long currentTime) {
|
||||||
|
String title = "OrangeX";
|
||||||
|
FontRenderer font = event.getRender2D().getFont(LOGO_FONT_SIZE);
|
||||||
|
|
||||||
|
int x = LOGO_START_X;
|
||||||
|
int y = LOGO_START_Y;
|
||||||
|
|
||||||
|
for (int i = 0; i < title.length(); i++) {
|
||||||
|
String character = String.valueOf(title.charAt(i));
|
||||||
|
|
||||||
|
font.drawString(character, x, y, getRainbowColor(currentTime, i));
|
||||||
|
|
||||||
|
x += (int) font.getStringWidth(character);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderModuleList(EventRender2D event, long currentTime) {
|
||||||
|
FontRenderer font = event.getRender2D().getFont(MODULE_FONT_SIZE);
|
||||||
|
List<Module> modules = OrangeX.getInstance()
|
||||||
|
.getModuleManager()
|
||||||
|
.getAllModules()
|
||||||
|
.stream()
|
||||||
|
.filter(Module::isEnabled)
|
||||||
|
.sorted(Comparator.comparingDouble(
|
||||||
|
module -> -font.getStringWidth(module.getName())
|
||||||
|
))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
int screenWidth = event.getRender2D().getWidth();
|
||||||
|
int y = MODULE_MARGIN;
|
||||||
|
|
||||||
|
for (int i = 0; i < modules.size(); i++) {
|
||||||
|
Module module = modules.get(i);
|
||||||
|
String name = module.getName();
|
||||||
|
|
||||||
|
int textWidth = (int) font.getStringWidth(name);
|
||||||
|
int x = screenWidth - textWidth - MODULE_MARGIN;
|
||||||
|
|
||||||
|
font.drawStringWithShadow(name, x, y, getRainbowColor(currentTime, i));
|
||||||
|
|
||||||
|
y += (int) (font.getStringHeight() + MODULE_SPACING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getRainbowColor(long currentTime, int index) {
|
||||||
|
long offset = (long) index * RAINBOW_OFFSET;
|
||||||
|
|
||||||
|
float hue = ((currentTime + offset) % (long) RAINBOW_CYCLE)
|
||||||
|
/ RAINBOW_CYCLE;
|
||||||
|
|
||||||
|
return Color.HSBtoRGB(hue, 0.9f, 1.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
import org.greenrobot.eventbus.ThreadMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional debug overlay. Enable with {@code -Dorangex.debugFont=true}.
|
||||||
|
*/
|
||||||
|
public final class FontDebugOverlay {
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.POSTING)
|
||||||
|
public void onRender2D(EventRender2D event) {
|
||||||
|
Render2D render2D = event.getRender2D();
|
||||||
|
|
||||||
|
render2D.fillRoundedRect(4, 4, 250, 120, 8,
|
||||||
|
1.0, 0xD0141414, 0xFF303030);
|
||||||
|
render2D.fillGradient(10, 112, 238, 6,
|
||||||
|
0xFF55FFFF, 0xFF55FF55);
|
||||||
|
render2D.drawLine(10, 124, 248, 124, 1.0, 0x80FFFFFF);
|
||||||
|
|
||||||
|
FontRenderer title = render2D.getFont(14.0f, true);
|
||||||
|
FontRenderer body = render2D.getFont(10.0f, false);
|
||||||
|
|
||||||
|
title.drawStringWithShadow("OrangeX", 8, 8, 0xFFFFFFFF);
|
||||||
|
body.drawString("Font backend: " + render2D.getClass().getSimpleName(),
|
||||||
|
8, 26, 0xFF55FFFF);
|
||||||
|
body.drawString("ASCII 0123456789", 8, 40, 0xFFAAAAAA);
|
||||||
|
body.drawString("中文测试:钻石剑 / 迅捷", 8, 54, 0xFFFFAA00);
|
||||||
|
body.drawString("Input: " + (ClientInput.isOpen() ? "OPEN" : "closed")
|
||||||
|
+ " mouse " + (int) ClientInput.getMouseX()
|
||||||
|
+ ", " + (int) ClientInput.getMouseY(),
|
||||||
|
8, 68, 0xFF55FF55);
|
||||||
|
body.drawString("RSHIFT: ClickGUI", 8, 82, 0xFF888888);
|
||||||
|
|
||||||
|
// Large high-contrast marker: if this is invisible too, the problem is
|
||||||
|
// GL state/coordinates rather than the glyph atlas.
|
||||||
|
FontRenderer marker = render2D.getFont(28.0f, true);
|
||||||
|
marker.drawCenteredString("TEST", render2D.getWidth() / 2.0,
|
||||||
|
render2D.getHeight() / 2.0 - 24.0, 0xFFFF3030);
|
||||||
|
|
||||||
|
if (ClientInput.isOpen()) {
|
||||||
|
double mouseX = ClientInput.getMouseX();
|
||||||
|
double mouseY = ClientInput.getMouseY();
|
||||||
|
render2D.drawLine(mouseX - 7, mouseY, mouseX + 7, mouseY, 1.0, 0xFFFF4040);
|
||||||
|
render2D.drawLine(mouseX, mouseY - 7, mouseX, mouseY + 7, 1.0, 0xFFFF4040);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontBackend;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontBackendFactory;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Owns the font cache and turns one platform render callback into an
|
||||||
|
* {@link EventRender2D}.
|
||||||
|
*/
|
||||||
|
public final class Render2DManager {
|
||||||
|
|
||||||
|
private final FontBackend backend;
|
||||||
|
private final FontManager fonts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the overlay is drawn on screens that are not backed by a loaded
|
||||||
|
* level (title screen, world selection, ...). 1.8.9 never renders the HUD
|
||||||
|
* there, and this defaults to the same behaviour on every version.
|
||||||
|
* Enable with {@code -Dorangex.renderInMenus=true}.
|
||||||
|
*/
|
||||||
|
private volatile boolean renderInMenus = Boolean.getBoolean("orangex.renderInMenus");
|
||||||
|
|
||||||
|
public Render2DManager(RuntimeInfo runtimeInfo,
|
||||||
|
MappingResolver mappingResolver,
|
||||||
|
Object minecraft) {
|
||||||
|
this.backend = FontBackendFactory.create(
|
||||||
|
runtimeInfo, mappingResolver, minecraft);
|
||||||
|
this.fonts = new FontManager(backend);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fire(Object platformContext) {
|
||||||
|
fire(platformContext, true, Float.NaN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param inWorld {@code true} when a level is loaded (the game's own
|
||||||
|
* "should render level" flag). When {@code false} the
|
||||||
|
* event is dropped unless {@link #isRenderInMenus()} is on.
|
||||||
|
*/
|
||||||
|
public void fire(Object platformContext, boolean inWorld) {
|
||||||
|
fire(platformContext, inWorld, Float.NaN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param partialTicks the partial tick the game rendered this frame with,
|
||||||
|
* or {@link Float#NaN} when the caller has none.
|
||||||
|
*/
|
||||||
|
public void fire(Object platformContext, boolean inWorld,
|
||||||
|
float partialTicks) {
|
||||||
|
if (!inWorld && !renderInMenus) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EventBus eventBus = OrangeX.getInstance().getEventBus();
|
||||||
|
if (!eventBus.hasSubscriberForEvent(EventRender2D.class)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pin atlases used by this frame and release long-idle ones (e.g. the
|
||||||
|
// previous GUI scale's faces) before they are needed again.
|
||||||
|
fonts.beginFrame();
|
||||||
|
|
||||||
|
Render2D context = backend.createRender2D(fonts, platformContext);
|
||||||
|
try {
|
||||||
|
eventBus.post(new EventRender2D(context, inWorld, partialTicks));
|
||||||
|
} finally {
|
||||||
|
context.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRenderInMenus() {
|
||||||
|
return renderInMenus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRenderInMenus(boolean renderInMenus) {
|
||||||
|
this.renderInMenus = renderInMenus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBackendName() {
|
||||||
|
return backend.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FontManager getFonts() {
|
||||||
|
return fonts;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A font face bound to the current 2D render pass.
|
||||||
|
*
|
||||||
|
* <p>Instances are created by {@link Render2D#getFont(float)} and must only be
|
||||||
|
* used during the frame that produced the {@code Render2D} instance.</p>
|
||||||
|
*/
|
||||||
|
public interface FontRenderer {
|
||||||
|
|
||||||
|
void drawString(String text, double x, double y, int argb);
|
||||||
|
|
||||||
|
void drawStringWithShadow(String text, double x, double y, int argb);
|
||||||
|
|
||||||
|
void drawCenteredString(String text, double centerX, double y, int argb);
|
||||||
|
|
||||||
|
void drawCenteredStringWithShadow(String text, double centerX, double y, int argb);
|
||||||
|
|
||||||
|
double getStringWidth(String text);
|
||||||
|
|
||||||
|
double getStringHeight();
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.api;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontFamily;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The 2D drawing context for one frame.
|
||||||
|
*
|
||||||
|
* <p>Feature code receives this object through
|
||||||
|
* {@code cn.xiaozhou233.orangex.event.impl.EventRender2D}. Coordinates and
|
||||||
|
* sizes are expressed in Minecraft GUI pixels (the same space used by the
|
||||||
|
* vanilla HUD after {@code ScaledResolution}).</p>
|
||||||
|
*/
|
||||||
|
public interface Render2D {
|
||||||
|
|
||||||
|
int getWidth();
|
||||||
|
|
||||||
|
int getHeight();
|
||||||
|
|
||||||
|
double getGuiScale();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Font using the configured default family
|
||||||
|
* ({@code -Dorangex.font.family}, Proxima when unset).
|
||||||
|
*/
|
||||||
|
FontRenderer getFont(float size);
|
||||||
|
|
||||||
|
FontRenderer getFont(float size, boolean bold);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Font using an explicit bundled family, e.g.
|
||||||
|
* {@link FontFamily#MONTSERRAT} or {@link FontFamily#JELLO_LIGHT}.
|
||||||
|
*/
|
||||||
|
FontRenderer getFont(FontFamily family, float size);
|
||||||
|
|
||||||
|
FontRenderer getFont(FontFamily family, float size, boolean bold);
|
||||||
|
|
||||||
|
void fillRect(double x, double y, double width, double height, int argb);
|
||||||
|
|
||||||
|
void fillGradient(double x, double y, double width, double height,
|
||||||
|
int topArgb, int bottomArgb);
|
||||||
|
|
||||||
|
void drawRect(double x, double y, double width, double height,
|
||||||
|
double thickness, int argb);
|
||||||
|
|
||||||
|
void fillRoundedRect(double x, double y, double width, double height,
|
||||||
|
double radius, int argb);
|
||||||
|
|
||||||
|
void fillRoundedRect(double x, double y, double width, double height,
|
||||||
|
double radius, double borderWidth,
|
||||||
|
int argb, int borderArgb);
|
||||||
|
|
||||||
|
void drawLine(double x1, double y1, double x2, double y2,
|
||||||
|
double thickness, int argb);
|
||||||
|
|
||||||
|
void enableScissor(double x, double y, double width, double height);
|
||||||
|
|
||||||
|
void disableScissor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Framework-internal: restores any state the backend changed for this
|
||||||
|
* frame. Feature code should not call it.
|
||||||
|
*/
|
||||||
|
void flush();
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A baked font atlas.
|
||||||
|
*
|
||||||
|
* <p>{@link #getPixels()} contains one 8-bit coverage value per texel. The
|
||||||
|
* backends decide how to upload that data (alpha-only on legacy OpenGL, RGBA
|
||||||
|
* on modern Minecraft).</p>
|
||||||
|
*/
|
||||||
|
public final class FontAtlas {
|
||||||
|
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
private byte[] pixels;
|
||||||
|
private final Map<Integer, Glyph> glyphs;
|
||||||
|
|
||||||
|
/** Distance from the top of a line to the baseline, in atlas pixels. */
|
||||||
|
private final float ascent;
|
||||||
|
/** Recommended line height, in atlas pixels. */
|
||||||
|
private final float lineHeight;
|
||||||
|
|
||||||
|
private final Glyph[] ascii = new Glyph[128];
|
||||||
|
|
||||||
|
public FontAtlas(int width, int height, byte[] pixels,
|
||||||
|
Map<Integer, Glyph> glyphs,
|
||||||
|
float ascent, float lineHeight) {
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.pixels = pixels;
|
||||||
|
this.glyphs = Collections.unmodifiableMap(new HashMap<Integer, Glyph>(glyphs));
|
||||||
|
this.ascent = ascent;
|
||||||
|
this.lineHeight = lineHeight;
|
||||||
|
|
||||||
|
for (Map.Entry<Integer, Glyph> entry : glyphs.entrySet()) {
|
||||||
|
int codePoint = entry.getKey();
|
||||||
|
if (codePoint >= 0 && codePoint < ascii.length) {
|
||||||
|
ascii[codePoint] = entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getPixels() {
|
||||||
|
if (pixels == null) {
|
||||||
|
throw new IllegalStateException("Atlas pixels have already been released");
|
||||||
|
}
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops the CPU-side coverage bitmap after the texture has been uploaded.
|
||||||
|
*/
|
||||||
|
public void releasePixels() {
|
||||||
|
this.pixels = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getAscent() {
|
||||||
|
return ascent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getLineHeight() {
|
||||||
|
return lineHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Glyph getGlyph(int codePoint) {
|
||||||
|
if (codePoint >= 0 && codePoint < ascii.length) {
|
||||||
|
Glyph glyph = ascii[codePoint];
|
||||||
|
if (glyph != null) {
|
||||||
|
return glyph;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return glyphs.get(codePoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Glyph getFallbackGlyph() {
|
||||||
|
Glyph space = getGlyph(' ');
|
||||||
|
if (space != null) {
|
||||||
|
return space;
|
||||||
|
}
|
||||||
|
return getGlyph('?');
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Glyph> getGlyphs() {
|
||||||
|
return glyphs;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One baked atlas and its GPU texture, shared by every {@link FontFace} that
|
||||||
|
* was baked at the same height and resource.
|
||||||
|
*
|
||||||
|
* <p>Logical sizes do not own atlases: {@link FontFace#getDrawScale()} maps
|
||||||
|
* atlas pixels back to GUI pixels, so many sizes reuse a single texture.</p>
|
||||||
|
*/
|
||||||
|
final class FontAtlasEntry {
|
||||||
|
|
||||||
|
final String resource;
|
||||||
|
final int bakeHeight;
|
||||||
|
final FontAtlas atlas;
|
||||||
|
final FontTexture texture;
|
||||||
|
final int texels;
|
||||||
|
long lastUsedNanos;
|
||||||
|
|
||||||
|
FontAtlasEntry(String resource, int bakeHeight, FontAtlas atlas,
|
||||||
|
FontTexture texture, long lastUsedNanos) {
|
||||||
|
this.resource = resource;
|
||||||
|
this.bakeHeight = bakeHeight;
|
||||||
|
this.atlas = atlas;
|
||||||
|
this.texture = texture;
|
||||||
|
this.texels = atlas.getWidth() * atlas.getHeight();
|
||||||
|
this.lastUsedNanos = lastUsedNanos;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A font face bound to a shared {@link FontAtlasEntry} and a logical size.
|
||||||
|
*
|
||||||
|
* <p>The atlas is shared between sizes; {@link #getDrawScale()} converts atlas
|
||||||
|
* pixels back to GUI pixels. Each face owns a small width cache because widths
|
||||||
|
* also depend on the CJK fallback face that the primary face resolved.</p>
|
||||||
|
*/
|
||||||
|
public final class FontFace {
|
||||||
|
|
||||||
|
private static final int MAX_CACHED_WIDTHS = 1024;
|
||||||
|
|
||||||
|
private final FontFamily family;
|
||||||
|
private final FontAtlasEntry entry;
|
||||||
|
private final float logicalSize;
|
||||||
|
private final float drawScale;
|
||||||
|
private final Map<String, Double> widthCache = new HashMap<String, Double>();
|
||||||
|
private long lastUsedNanos;
|
||||||
|
|
||||||
|
FontFace(FontFamily family, FontAtlasEntry entry,
|
||||||
|
float logicalSize, float drawScale, long lastUsedNanos) {
|
||||||
|
this.family = family;
|
||||||
|
this.entry = entry;
|
||||||
|
this.logicalSize = logicalSize;
|
||||||
|
this.drawScale = drawScale;
|
||||||
|
this.lastUsedNanos = lastUsedNanos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FontFamily getFamily() {
|
||||||
|
return family;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FontAtlas getAtlas() {
|
||||||
|
return entry.atlas;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FontTexture getTexture() {
|
||||||
|
return entry.texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getLogicalSize() {
|
||||||
|
return logicalSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getDrawScale() {
|
||||||
|
return drawScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Glyph getGlyph(int codePoint) {
|
||||||
|
return entry.atlas.getGlyph(codePoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Glyph getFallbackGlyph() {
|
||||||
|
return entry.atlas.getFallbackGlyph();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getAscent() {
|
||||||
|
return entry.atlas.getAscent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getLineHeight() {
|
||||||
|
return entry.atlas.getLineHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
FontAtlasEntry getEntry() {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
long getLastUsedNanos() {
|
||||||
|
return lastUsedNanos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void touch(long now) {
|
||||||
|
lastUsedNanos = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared across frames and renderers: {@code getStringWidth} is normally
|
||||||
|
* called every frame for static text.
|
||||||
|
*/
|
||||||
|
Double getCachedWidth(String text) {
|
||||||
|
return widthCache.get(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cacheWidth(String text, double width) {
|
||||||
|
if (widthCache.size() >= MAX_CACHED_WIDTHS) {
|
||||||
|
widthCache.clear();
|
||||||
|
}
|
||||||
|
widthCache.put(text, width);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bundled font families.
|
||||||
|
*
|
||||||
|
* <p>Every family maps a regular face and a bold face. Weights without a
|
||||||
|
* dedicated bold file reuse a heavier (or the same) face, so asking for
|
||||||
|
* {@code bold} text never fails to load.</p>
|
||||||
|
*
|
||||||
|
* <p>Families can be selected globally with
|
||||||
|
* {@code -Dorangex.font.family=<name>}, where {@code <name>} is either the
|
||||||
|
* display name ({@code "Montserrat Light"}), the constant name
|
||||||
|
* ({@code MONTSERRAT_LIGHT}) or the file name ({@code Montserrat-Light.ttf}).
|
||||||
|
* Matching ignores case, spaces and punctuation.</p>
|
||||||
|
*/
|
||||||
|
public enum FontFamily {
|
||||||
|
|
||||||
|
PROXIMA("Proxima", "proxima.ttf", "proximabd.ttf", false),
|
||||||
|
NOTO("Noto Sans", "noto.ttf", "noto.ttf", true),
|
||||||
|
POPPINS("Poppins", "poppins_rg.ttf", "poppins_rg.ttf", false),
|
||||||
|
JELLO("Jello", "JelloRegular.ttf", "JelloMedium.ttf", false),
|
||||||
|
JELLO_LIGHT("Jello Light", "JelloLight.ttf", "JelloMedium.ttf", false),
|
||||||
|
JELLO_MEDIUM("Jello Medium", "JelloMedium.ttf", "JelloMedium.ttf", false),
|
||||||
|
MONTSERRAT("Montserrat", "Montserrat-Regular.ttf", "Montserrat-Bold.ttf", false),
|
||||||
|
MONTSERRAT_LIGHT("Montserrat Light", "Montserrat-Light.ttf", "Montserrat-Bold.ttf", false),
|
||||||
|
MONTSERRAT_MEDIUM("Montserrat Medium", "Montserrat-Medium.ttf", "Montserrat-Bold.ttf", false);
|
||||||
|
|
||||||
|
private static final String FONT_DIRECTORY = "assets/orangex/font/";
|
||||||
|
|
||||||
|
private final String displayName;
|
||||||
|
private final String regularResource;
|
||||||
|
private final String boldResource;
|
||||||
|
private final boolean cjkCapable;
|
||||||
|
|
||||||
|
private final String constantKey;
|
||||||
|
private final String displayKey;
|
||||||
|
private final String fileKey;
|
||||||
|
|
||||||
|
FontFamily(String displayName, String regularFile, String boldFile,
|
||||||
|
boolean cjkCapable) {
|
||||||
|
this.displayName = displayName;
|
||||||
|
this.regularResource = FONT_DIRECTORY + regularFile;
|
||||||
|
this.boldResource = FONT_DIRECTORY + boldFile;
|
||||||
|
this.cjkCapable = cjkCapable;
|
||||||
|
|
||||||
|
this.constantKey = normalize(name());
|
||||||
|
this.displayKey = normalize(displayName);
|
||||||
|
this.fileKey = normalize(stripExtension(regularFile));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Human readable name, suitable for configuration screens.
|
||||||
|
*/
|
||||||
|
public String getDisplayName() {
|
||||||
|
return displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRegularResource() {
|
||||||
|
return regularResource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBoldResource() {
|
||||||
|
return boldResource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getResource(boolean bold) {
|
||||||
|
return bold ? boldResource : regularResource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCjkCapable() {
|
||||||
|
return cjkCapable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a family from its display name, constant name or font file
|
||||||
|
* name. Matching is case-insensitive and ignores spaces/punctuation.
|
||||||
|
*
|
||||||
|
* @return the matching family, or {@code null} when nothing matches.
|
||||||
|
*/
|
||||||
|
public static FontFamily byName(String name) {
|
||||||
|
if (name == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String key = normalize(stripExtension(name));
|
||||||
|
if (key.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (FontFamily family : values()) {
|
||||||
|
if (key.equals(family.constantKey)
|
||||||
|
|| key.equals(family.displayKey)
|
||||||
|
|| key.equals(family.fileKey)) {
|
||||||
|
return family;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String stripExtension(String fileName) {
|
||||||
|
int dot = fileName.lastIndexOf('.');
|
||||||
|
return dot > 0 ? fileName.substring(0, dot) : fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String normalize(String value) {
|
||||||
|
return value.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,381 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontBackend;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and caches {@link FontFace} instances.
|
||||||
|
*
|
||||||
|
* <p>Atlases are cached per (font resource, bake height) and shared between
|
||||||
|
* logical sizes; a face is a lightweight view adding the draw scale. The
|
||||||
|
* atlas is rasterised at {@code size * guiScale} pixels (rounded up to the
|
||||||
|
* family's bake-height bucket) so text stays crisp on high GUI scales, while
|
||||||
|
* {@link FontFace#getDrawScale()} converts atlas pixels back to GUI
|
||||||
|
* pixels.</p>
|
||||||
|
*
|
||||||
|
* <p>Textures are bounded by an idle timeout and a byte budget; least recently
|
||||||
|
* used atlases are closed when either is exceeded, so changing GUI scale or
|
||||||
|
* using many sizes no longer leaks VRAM. Frames should call
|
||||||
|
* {@link #beginFrame()} so atlases used by the current frame are pinned.</p>
|
||||||
|
*/
|
||||||
|
public final class FontManager {
|
||||||
|
|
||||||
|
private static final int MAX_RASTER_HEIGHT = 64;
|
||||||
|
private static final int MAX_CJK_RASTER_HEIGHT = 36;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quantised bake heights for CJK faces. A full Noto atlas is large, so
|
||||||
|
* neighbouring sizes share a bucket instead of each baking their own sheet.
|
||||||
|
*/
|
||||||
|
private static final int[] CJK_BAKE_HEIGHTS =
|
||||||
|
new int[]{12, 16, 20, 24, 28, 32, 36};
|
||||||
|
|
||||||
|
private static final long TRIM_INTERVAL_NANOS = 2_000_000_000L;
|
||||||
|
private static final long DEFAULT_ATLAS_BUDGET_MB = 128L;
|
||||||
|
private static final int DEFAULT_IDLE_SECONDS = 30;
|
||||||
|
|
||||||
|
private static final FontFamily FALLBACK_FAMILY = FontFamily.PROXIMA;
|
||||||
|
|
||||||
|
private final FontBackend backend;
|
||||||
|
private final Map<AtlasKey, FontAtlasEntry> atlases =
|
||||||
|
new HashMap<AtlasKey, FontAtlasEntry>();
|
||||||
|
private final Map<FaceKey, FontFace> faces = new HashMap<FaceKey, FontFace>();
|
||||||
|
private final Map<String, byte[]> fontData = new HashMap<String, byte[]>();
|
||||||
|
|
||||||
|
private final boolean fullGbk;
|
||||||
|
private final boolean debugCache;
|
||||||
|
private final int bytesPerTexel;
|
||||||
|
private final long atlasBudgetBytes;
|
||||||
|
private final long idleNanos;
|
||||||
|
|
||||||
|
private FontFamily defaultFamily;
|
||||||
|
private long frameStartNanos;
|
||||||
|
private long lastTrimNanos;
|
||||||
|
|
||||||
|
public FontManager(FontBackend backend) {
|
||||||
|
this.backend = backend;
|
||||||
|
this.defaultFamily = resolveDefaultFamily();
|
||||||
|
this.fullGbk = !"false".equalsIgnoreCase(
|
||||||
|
System.getProperty("orangex.font.fullGbk", "true"));
|
||||||
|
this.debugCache = Boolean.getBoolean("orangex.font.debugCache");
|
||||||
|
this.bytesPerTexel = Math.max(1, backend.getBytesPerTexel());
|
||||||
|
this.atlasBudgetBytes = Math.max(8L,
|
||||||
|
Long.getLong("orangex.font.atlasBudgetMb", DEFAULT_ATLAS_BUDGET_MB))
|
||||||
|
* 1024L * 1024L;
|
||||||
|
this.idleNanos = Math.max(1,
|
||||||
|
Integer.getInteger("orangex.font.idleSeconds", DEFAULT_IDLE_SECONDS))
|
||||||
|
* 1_000_000_000L;
|
||||||
|
this.frameStartNanos = System.nanoTime();
|
||||||
|
this.lastTrimNanos = this.frameStartNanos;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FontFamily resolveDefaultFamily() {
|
||||||
|
String configured = System.getProperty("orangex.font.family");
|
||||||
|
if (configured == null || configured.trim().isEmpty()) {
|
||||||
|
return FALLBACK_FAMILY;
|
||||||
|
}
|
||||||
|
FontFamily family = FontFamily.byName(configured);
|
||||||
|
if (family == null) {
|
||||||
|
System.out.println("[OrangeX] Unknown font family '" + configured
|
||||||
|
+ "', falling back to " + FALLBACK_FAMILY.getDisplayName());
|
||||||
|
return FALLBACK_FAMILY;
|
||||||
|
}
|
||||||
|
return family;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The family used by {@code Render2D#getFont(size)}. Defaults to
|
||||||
|
* {@link #FALLBACK_FAMILY}, overridable with
|
||||||
|
* {@code -Dorangex.font.family=<name>}.
|
||||||
|
*/
|
||||||
|
public synchronized FontFamily getDefaultFamily() {
|
||||||
|
return defaultFamily;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Switches the family used by the size-only {@code getFont} overloads.
|
||||||
|
* Already created {@link FontFace} instances stay valid.
|
||||||
|
*/
|
||||||
|
public synchronized void setDefaultFamily(FontFamily defaultFamily) {
|
||||||
|
this.defaultFamily = defaultFamily != null ? defaultFamily : FALLBACK_FAMILY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks the start of a rendered frame. Atlases touched after this call are
|
||||||
|
* pinned against budget eviction until the next frame; idle atlases from
|
||||||
|
* earlier frames (for example after a GUI scale change) are released.
|
||||||
|
*/
|
||||||
|
public synchronized void beginFrame() {
|
||||||
|
long now = System.nanoTime();
|
||||||
|
this.frameStartNanos = now;
|
||||||
|
if (now - lastTrimNanos >= TRIM_INTERVAL_NANOS) {
|
||||||
|
lastTrimNanos = now;
|
||||||
|
evictIdle(now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized FontFace getFace(FontFamily family, float logicalSize,
|
||||||
|
boolean bold, double guiScale) {
|
||||||
|
if (family == null) {
|
||||||
|
family = defaultFamily;
|
||||||
|
}
|
||||||
|
if (logicalSize <= 0.0f) {
|
||||||
|
throw new IllegalArgumentException("logicalSize must be positive");
|
||||||
|
}
|
||||||
|
|
||||||
|
int rasterHeight = (int) Math.round(logicalSize * Math.max(1.0, guiScale));
|
||||||
|
int maxRasterHeight = family.isCjkCapable()
|
||||||
|
? MAX_CJK_RASTER_HEIGHT
|
||||||
|
: MAX_RASTER_HEIGHT;
|
||||||
|
if (rasterHeight < 1) {
|
||||||
|
rasterHeight = 1;
|
||||||
|
} else if (rasterHeight > maxRasterHeight) {
|
||||||
|
rasterHeight = maxRasterHeight;
|
||||||
|
}
|
||||||
|
int bakeHeight = bakeHeight(family, rasterHeight);
|
||||||
|
|
||||||
|
long now = System.nanoTime();
|
||||||
|
String resource = family.getResource(bold);
|
||||||
|
AtlasKey atlasKey = new AtlasKey(resource, bakeHeight, family.isCjkCapable());
|
||||||
|
FontAtlasEntry entry = atlases.get(atlasKey);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = bake(atlasKey, now);
|
||||||
|
atlases.put(atlasKey, entry);
|
||||||
|
enforceBudget();
|
||||||
|
}
|
||||||
|
entry.lastUsedNanos = now;
|
||||||
|
|
||||||
|
FaceKey faceKey = new FaceKey(atlasKey, logicalSize);
|
||||||
|
FontFace face = faces.get(faceKey);
|
||||||
|
if (face == null) {
|
||||||
|
float drawScale = logicalSize / (float) bakeHeight;
|
||||||
|
face = new FontFace(family, entry, logicalSize, drawScale, now);
|
||||||
|
faces.put(faceKey, face);
|
||||||
|
} else {
|
||||||
|
face.touch(now);
|
||||||
|
}
|
||||||
|
return face;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int bakeHeight(FontFamily family, int rasterHeight) {
|
||||||
|
if (!family.isCjkCapable()) {
|
||||||
|
return rasterHeight;
|
||||||
|
}
|
||||||
|
for (int height : CJK_BAKE_HEIGHTS) {
|
||||||
|
if (rasterHeight <= height) {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CJK_BAKE_HEIGHTS[CJK_BAKE_HEIGHTS.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
private FontAtlasEntry bake(AtlasKey key, long now) {
|
||||||
|
byte[] data = loadFont(key.resource);
|
||||||
|
FontAtlas atlas = StbFontBaker.bake(
|
||||||
|
data, key.bakeHeight, key.cjk, fullGbk);
|
||||||
|
FontTexture texture = backend.createTexture(atlas);
|
||||||
|
atlas.releasePixels();
|
||||||
|
FontAtlasEntry entry = new FontAtlasEntry(
|
||||||
|
key.resource, key.bakeHeight, atlas, texture, now);
|
||||||
|
if (debugCache) {
|
||||||
|
System.out.println("[OrangeX] Baked " + key.resource
|
||||||
|
+ " @" + key.bakeHeight + "px -> "
|
||||||
|
+ atlas.getWidth() + "x" + atlas.getHeight()
|
||||||
|
+ " (" + (entry.texels * (long) bytesPerTexel / (1024 * 1024))
|
||||||
|
+ " MB)");
|
||||||
|
}
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops atlases and faces that have not been used for {@code idleSeconds}
|
||||||
|
* (for example the previous GUI scale's faces).
|
||||||
|
*/
|
||||||
|
private void evictIdle(long now) {
|
||||||
|
Iterator<Map.Entry<AtlasKey, FontAtlasEntry>> iterator =
|
||||||
|
atlases.entrySet().iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Map.Entry<AtlasKey, FontAtlasEntry> next = iterator.next();
|
||||||
|
if (now - next.getValue().lastUsedNanos < idleNanos) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
iterator.remove();
|
||||||
|
close(next.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator<Map.Entry<FaceKey, FontFace>> facesIterator =
|
||||||
|
faces.entrySet().iterator();
|
||||||
|
while (facesIterator.hasNext()) {
|
||||||
|
if (now - facesIterator.next().getValue().getLastUsedNanos()
|
||||||
|
>= idleNanos) {
|
||||||
|
facesIterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evicts least recently used atlases until the texture budget is met.
|
||||||
|
* Atlases already used in the current frame are pinned, so a working set
|
||||||
|
* larger than the budget never causes per-frame re-baking.
|
||||||
|
*/
|
||||||
|
private void enforceBudget() {
|
||||||
|
long totalBytes = totalBytes();
|
||||||
|
while (totalBytes > atlasBudgetBytes) {
|
||||||
|
AtlasKey victimKey = null;
|
||||||
|
FontAtlasEntry victim = null;
|
||||||
|
for (Map.Entry<AtlasKey, FontAtlasEntry> candidate : atlases.entrySet()) {
|
||||||
|
FontAtlasEntry entry = candidate.getValue();
|
||||||
|
if (entry.lastUsedNanos >= frameStartNanos) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (victim == null || entry.lastUsedNanos < victim.lastUsedNanos) {
|
||||||
|
victim = entry;
|
||||||
|
victimKey = candidate.getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (victim == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
atlases.remove(victimKey);
|
||||||
|
totalBytes -= (long) victim.texels * bytesPerTexel;
|
||||||
|
close(victim);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long totalBytes() {
|
||||||
|
long total = 0L;
|
||||||
|
for (FontAtlasEntry entry : atlases.values()) {
|
||||||
|
total += (long) entry.texels * bytesPerTexel;
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void close(FontAtlasEntry entry) {
|
||||||
|
entry.texture.close();
|
||||||
|
Iterator<Map.Entry<FaceKey, FontFace>> iterator =
|
||||||
|
faces.entrySet().iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (iterator.next().getValue().getEntry() == entry) {
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (debugCache) {
|
||||||
|
System.out.println("[OrangeX] Released " + entry.resource
|
||||||
|
+ " @" + entry.bakeHeight + "px");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] loadFont(String resource) {
|
||||||
|
byte[] cached = fontData.get(resource);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream stream = FontManager.class.getClassLoader()
|
||||||
|
.getResourceAsStream(resource);
|
||||||
|
if (stream == null) {
|
||||||
|
throw new IllegalStateException("Missing font resource: " + resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream output = new ByteArrayOutputStream(64 * 1024);
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int read;
|
||||||
|
while ((read = stream.read(buffer)) != -1) {
|
||||||
|
output.write(buffer, 0, read);
|
||||||
|
}
|
||||||
|
byte[] data = output.toByteArray();
|
||||||
|
fontData.put(resource, data);
|
||||||
|
return data;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Cannot read font resource: " + resource, e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
stream.close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
// Ignore close failures for a classpath resource.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBackendName() {
|
||||||
|
return backend.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void clear() {
|
||||||
|
for (FontAtlasEntry entry : atlases.values()) {
|
||||||
|
entry.texture.close();
|
||||||
|
}
|
||||||
|
atlases.clear();
|
||||||
|
faces.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class AtlasKey {
|
||||||
|
private final String resource;
|
||||||
|
private final int bakeHeight;
|
||||||
|
private final boolean cjk;
|
||||||
|
|
||||||
|
private AtlasKey(String resource, int bakeHeight, boolean cjk) {
|
||||||
|
this.resource = resource;
|
||||||
|
this.bakeHeight = bakeHeight;
|
||||||
|
this.cjk = cjk;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (this == other) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(other instanceof AtlasKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
AtlasKey key = (AtlasKey) other;
|
||||||
|
return bakeHeight == key.bakeHeight
|
||||||
|
&& cjk == key.cjk
|
||||||
|
&& resource.equals(key.resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = resource.hashCode();
|
||||||
|
result = 31 * result + bakeHeight;
|
||||||
|
result = 31 * result + (cjk ? 1 : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class FaceKey {
|
||||||
|
private final AtlasKey atlasKey;
|
||||||
|
private final float logicalSize;
|
||||||
|
|
||||||
|
private FaceKey(AtlasKey atlasKey, float logicalSize) {
|
||||||
|
this.atlasKey = atlasKey;
|
||||||
|
this.logicalSize = logicalSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (this == other) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(other instanceof FaceKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
FaceKey key = (FaceKey) other;
|
||||||
|
return Float.compare(logicalSize, key.logicalSize) == 0
|
||||||
|
&& atlasKey.equals(key.atlasKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 31 * atlasKey.hashCode() + Float.floatToIntBits(logicalSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One glyph inside a {@link FontAtlas}.
|
||||||
|
*
|
||||||
|
* <p>All offsets are expressed in atlas pixels relative to the pen position on
|
||||||
|
* the baseline. UVs are normalised to {@code [0, 1]}.</p>
|
||||||
|
*/
|
||||||
|
public final class Glyph {
|
||||||
|
|
||||||
|
private final float x0;
|
||||||
|
private final float y0;
|
||||||
|
private final float x1;
|
||||||
|
private final float y1;
|
||||||
|
|
||||||
|
private final float u0;
|
||||||
|
private final float v0;
|
||||||
|
private final float u1;
|
||||||
|
private final float v1;
|
||||||
|
|
||||||
|
private final float advance;
|
||||||
|
|
||||||
|
public Glyph(float x0, float y0, float x1, float y1,
|
||||||
|
float u0, float v0, float u1, float v1,
|
||||||
|
float advance) {
|
||||||
|
this.x0 = x0;
|
||||||
|
this.y0 = y0;
|
||||||
|
this.x1 = x1;
|
||||||
|
this.y1 = y1;
|
||||||
|
this.u0 = u0;
|
||||||
|
this.v0 = v0;
|
||||||
|
this.u1 = u1;
|
||||||
|
this.v1 = v1;
|
||||||
|
this.advance = advance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getX0() {
|
||||||
|
return x0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getY0() {
|
||||||
|
return y0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getX1() {
|
||||||
|
return x1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getY1() {
|
||||||
|
return y1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getU0() {
|
||||||
|
return u0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getV0() {
|
||||||
|
return v0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getU1() {
|
||||||
|
return u1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getV1() {
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getAdvance() {
|
||||||
|
return advance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBlank() {
|
||||||
|
return x0 == x1 || y0 == y1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,288 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbRasterizer;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypeFontInfo;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypePackContext;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypePackRange;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypePackedChar;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bakes a TrueType/OpenType font into a {@link FontAtlas}.
|
||||||
|
*
|
||||||
|
* <p>This is a thin adapter around the ported stb_truetype code in
|
||||||
|
* {@code render.font.stb}. The original Vape renderer used a single range and
|
||||||
|
* one fixed 4096x4096 sheet; here the character set is split into a Latin range
|
||||||
|
* and an optional CJK range so oversampling/atlas pressure can be controlled
|
||||||
|
* separately.</p>
|
||||||
|
*
|
||||||
|
* <p>Atlas size is chosen automatically from the requested glyph set (a fixed
|
||||||
|
* 4096x4096 sheet wastes 4x-16x the memory for Latin-only faces). The grown
|
||||||
|
* sizes are retried on overflow, so the estimate only affects bake time, never
|
||||||
|
* correctness. {@code -Dorangex.font.atlasSize=<n>} pins an exact size for
|
||||||
|
* compatibility with the old behaviour.</p>
|
||||||
|
*/
|
||||||
|
public final class StbFontBaker {
|
||||||
|
|
||||||
|
private static final int MIN_ATLAS_SIZE = 512;
|
||||||
|
private static final int MAX_ATLAS_SIZE = 8192;
|
||||||
|
private static final int LATIN_OVERSAMPLE = 2;
|
||||||
|
private static final int CJK_OVERSAMPLE = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fraction of the sheet expected to be occupied by glyph boxes. The
|
||||||
|
* per-glyph areas below are calibrated against stb's real packing output
|
||||||
|
* for the bundled fonts; overflow still doubles and retries, so the
|
||||||
|
* estimate only trades a possible extra bake against wasted VRAM.
|
||||||
|
*/
|
||||||
|
private static final double PACK_FILL = 0.92;
|
||||||
|
private static final double LATIN_AREA_FACTOR = 0.55;
|
||||||
|
private static final double CJK_AREA_FACTOR = 0.36;
|
||||||
|
|
||||||
|
/** CJK glyph sets never fit a 512 sheet in practice; skip the retry. */
|
||||||
|
private static final int MIN_CJK_ATLAS_SIZE = 1024;
|
||||||
|
|
||||||
|
private StbFontBaker() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FontAtlas bake(byte[] fontData, int pixelHeight,
|
||||||
|
boolean includeCjk, boolean fullGbk) {
|
||||||
|
if (fontData == null || fontData.length == 0) {
|
||||||
|
throw new IllegalArgumentException("fontData is empty");
|
||||||
|
}
|
||||||
|
if (pixelHeight < 1) {
|
||||||
|
throw new IllegalArgumentException("pixelHeight must be positive");
|
||||||
|
}
|
||||||
|
|
||||||
|
StbTrueTypeFontInfo info = new StbTrueTypeFontInfo();
|
||||||
|
if (StbRasterizer.z(info, fontData, 0) == 0) {
|
||||||
|
throw new IllegalArgumentException("Not a valid TrueType/OpenType font");
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] ascent = new int[1];
|
||||||
|
int[] descent = new int[1];
|
||||||
|
int[] lineGap = new int[1];
|
||||||
|
StbRasterizer.x(info, ascent, descent, lineGap);
|
||||||
|
|
||||||
|
float fontScale = StbRasterizer.V(info, pixelHeight);
|
||||||
|
float ascentPx = ascent[0] * fontScale;
|
||||||
|
float lineHeightPx = (ascent[0] - descent[0] + lineGap[0]) * fontScale;
|
||||||
|
|
||||||
|
int[] latin = latinCodepoints();
|
||||||
|
int[] cjk = includeCjk ? cjkCodepoints(fullGbk) : new int[0];
|
||||||
|
|
||||||
|
Integer pinned = Integer.getInteger("orangex.font.atlasSize");
|
||||||
|
int atlasSize = pinned != null
|
||||||
|
? clampAtlasSize(pinned)
|
||||||
|
: estimateAtlasSize(latin.length, cjk.length, pixelHeight);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Result result = bakeAt(fontData, pixelHeight, latin, cjk,
|
||||||
|
atlasSize, ascentPx, lineHeightPx);
|
||||||
|
if (!result.overflow) {
|
||||||
|
return result.atlas;
|
||||||
|
}
|
||||||
|
if (pinned != null || atlasSize >= MAX_ATLAS_SIZE) {
|
||||||
|
// Keep drawing with the glyphs that did fit rather than
|
||||||
|
// failing the whole frame.
|
||||||
|
System.out.println("[OrangeX] Font atlas overflow at "
|
||||||
|
+ atlasSize + "x" + atlasSize
|
||||||
|
+ "; some glyphs are missing. Raise "
|
||||||
|
+ "-Dorangex.font.atlasSize.");
|
||||||
|
return result.atlas;
|
||||||
|
}
|
||||||
|
atlasSize = Math.min(MAX_ATLAS_SIZE, atlasSize * 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Result bakeAt(byte[] fontData, int pixelHeight,
|
||||||
|
int[] latin, int[] cjk, int atlasSize,
|
||||||
|
float ascentPx, float lineHeightPx) {
|
||||||
|
byte[] pixels = new byte[atlasSize * atlasSize];
|
||||||
|
|
||||||
|
StbTrueTypePackContext packContext = new StbTrueTypePackContext();
|
||||||
|
if (StbRasterizer.C(packContext, pixels, atlasSize, atlasSize, 0, 1) == 0) {
|
||||||
|
throw new IllegalStateException("Unable to initialise the stb pack context");
|
||||||
|
}
|
||||||
|
|
||||||
|
// stb packs every range with the context-wide oversampling, so the
|
||||||
|
// ranges are packed in two passes to give Latin 2x2 and CJK 1x1 (the
|
||||||
|
// latter alone would otherwise quadruple CJK atlas memory).
|
||||||
|
StbTrueTypePackRange latinRange = createRange(
|
||||||
|
latin, pixelHeight, LATIN_OVERSAMPLE);
|
||||||
|
StbTrueTypePackRange cjkRange = cjk.length > 0
|
||||||
|
? createRange(cjk, pixelHeight, CJK_OVERSAMPLE)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
StbRasterizer.n(packContext, LATIN_OVERSAMPLE, LATIN_OVERSAMPLE);
|
||||||
|
boolean packed = StbRasterizer.c(packContext, fontData, 0,
|
||||||
|
new StbTrueTypePackRange[]{latinRange}, 1) != 0;
|
||||||
|
|
||||||
|
if (cjkRange != null) {
|
||||||
|
StbRasterizer.n(packContext, CJK_OVERSAMPLE, CJK_OVERSAMPLE);
|
||||||
|
packed &= StbRasterizer.c(packContext, fontData, 0,
|
||||||
|
new StbTrueTypePackRange[]{cjkRange}, 1) != 0;
|
||||||
|
}
|
||||||
|
StbRasterizer.S(packContext);
|
||||||
|
|
||||||
|
Map<Integer, Glyph> glyphs = new HashMap<Integer, Glyph>();
|
||||||
|
collectGlyphs(glyphs, latinRange, latin, atlasSize);
|
||||||
|
if (cjkRange != null) {
|
||||||
|
collectGlyphs(glyphs, cjkRange, cjk, atlasSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Missing glyph: '?' or space. The renderer falls back to it for
|
||||||
|
// unsupported code points.
|
||||||
|
if (!glyphs.containsKey((int) '?')) {
|
||||||
|
Glyph space = glyphs.get((int) ' ');
|
||||||
|
if (space != null) {
|
||||||
|
glyphs.put((int) '?', space);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FontAtlas atlas = new FontAtlas(atlasSize, atlasSize, pixels, glyphs,
|
||||||
|
ascentPx, lineHeightPx);
|
||||||
|
return new Result(atlas, !packed);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void collectGlyphs(Map<Integer, Glyph> out,
|
||||||
|
StbTrueTypePackRange range,
|
||||||
|
int[] codepoints,
|
||||||
|
int atlasSize) {
|
||||||
|
float inverse = 1.0f / (float) atlasSize;
|
||||||
|
for (int i = 0; i < codepoints.length; i++) {
|
||||||
|
StbTrueTypePackedChar packedChar = range.M[i];
|
||||||
|
if (packedChar.A == 0.0f && packedChar.q == 0 && packedChar.p == 0) {
|
||||||
|
// Glyph did not fit or has no advance; keep spaces (A > 0).
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
out.put(codepoints[i], new Glyph(
|
||||||
|
packedChar.s,
|
||||||
|
packedChar.G,
|
||||||
|
packedChar.t,
|
||||||
|
packedChar.R,
|
||||||
|
packedChar.b * inverse,
|
||||||
|
packedChar.k * inverse,
|
||||||
|
packedChar.q * inverse,
|
||||||
|
packedChar.p * inverse,
|
||||||
|
packedChar.A
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int estimateAtlasSize(int latinCount, int cjkCount,
|
||||||
|
int pixelHeight) {
|
||||||
|
double latinCell = LATIN_OVERSAMPLE * (double) pixelHeight;
|
||||||
|
double cjkCell = CJK_OVERSAMPLE * (double) pixelHeight;
|
||||||
|
double area = latinCount * latinCell * latinCell * LATIN_AREA_FACTOR
|
||||||
|
+ cjkCount * cjkCell * cjkCell * CJK_AREA_FACTOR;
|
||||||
|
int side = (int) Math.ceil(Math.sqrt(area / PACK_FILL));
|
||||||
|
if (cjkCount > 0 && side < MIN_CJK_ATLAS_SIZE) {
|
||||||
|
side = MIN_CJK_ATLAS_SIZE;
|
||||||
|
}
|
||||||
|
return nextPowerOfTwo(clampAtlasSize(side));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int nextPowerOfTwo(int value) {
|
||||||
|
int result = MIN_ATLAS_SIZE;
|
||||||
|
while (result < value && result < MAX_ATLAS_SIZE) {
|
||||||
|
result <<= 1;
|
||||||
|
}
|
||||||
|
return Math.min(result, MAX_ATLAS_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int clampAtlasSize(int size) {
|
||||||
|
if (size < MIN_ATLAS_SIZE) {
|
||||||
|
return MIN_ATLAS_SIZE;
|
||||||
|
}
|
||||||
|
if (size > MAX_ATLAS_SIZE) {
|
||||||
|
return MAX_ATLAS_SIZE;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StbTrueTypePackRange createRange(int[] codepoints, int pixelHeight,
|
||||||
|
int oversample) {
|
||||||
|
StbTrueTypePackedChar[] packedChars = new StbTrueTypePackedChar[codepoints.length];
|
||||||
|
for (int i = 0; i < packedChars.length; i++) {
|
||||||
|
packedChars[i] = new StbTrueTypePackedChar();
|
||||||
|
}
|
||||||
|
|
||||||
|
StbTrueTypePackRange range = new StbTrueTypePackRange();
|
||||||
|
range.E = pixelHeight;
|
||||||
|
range.R = 0;
|
||||||
|
range.N = codepoints;
|
||||||
|
range.b = codepoints.length;
|
||||||
|
range.M = packedChars;
|
||||||
|
range.i = (byte) oversample;
|
||||||
|
range.y = (byte) oversample;
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int[] latinCodepoints() {
|
||||||
|
Set<Integer> codepoints = new LinkedHashSet<Integer>();
|
||||||
|
for (int c = 32; c <= 126; c++) {
|
||||||
|
codepoints.add(c);
|
||||||
|
}
|
||||||
|
for (int c = 160; c <= 255; c++) {
|
||||||
|
codepoints.add(c);
|
||||||
|
}
|
||||||
|
return toArray(codepoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes GBK double-byte ranges. Level 1 (common simplified Chinese) is
|
||||||
|
* used by default; {@code fullGbk} widens it to the whole GBK range that
|
||||||
|
* Vape covered.
|
||||||
|
*/
|
||||||
|
private static int[] cjkCodepoints(boolean fullGbk) {
|
||||||
|
Set<Integer> codepoints = new LinkedHashSet<Integer>();
|
||||||
|
int highStart = fullGbk ? 0xA1 : 0xB0;
|
||||||
|
int highEnd = fullGbk ? 0xF7 : 0xD7;
|
||||||
|
Charset gbk;
|
||||||
|
try {
|
||||||
|
gbk = Charset.forName("GBK");
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return new int[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int high = highStart; high <= highEnd; high++) {
|
||||||
|
for (int low = 0xA1; low <= 0xFE; low++) {
|
||||||
|
String decoded = new String(
|
||||||
|
new byte[]{(byte) high, (byte) low}, gbk);
|
||||||
|
if (decoded.length() != 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
char c = decoded.charAt(0);
|
||||||
|
if (c >= '\u4e00' && c <= '\u9fff') {
|
||||||
|
codepoints.add((int) c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toArray(codepoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int[] toArray(Set<Integer> values) {
|
||||||
|
int[] result = new int[values.size()];
|
||||||
|
int index = 0;
|
||||||
|
for (Integer value : values) {
|
||||||
|
result[index++] = value;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class Result {
|
||||||
|
private final FontAtlas atlas;
|
||||||
|
private final boolean overflow;
|
||||||
|
|
||||||
|
private Result(FontAtlas atlas, boolean overflow) {
|
||||||
|
this.atlas = atlas;
|
||||||
|
this.overflow = overflow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,231 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.GlyphDrawer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws UTF-16 text with a primary face and an optional CJK fallback face.
|
||||||
|
*
|
||||||
|
* <p>Supported formatting: Minecraft {@code §} colour codes ({@code 0-9},
|
||||||
|
* {@code a-f}, {@code r}). Shadow text is drawn as MC does: a dimmed copy
|
||||||
|
* offset by one GUI pixel.</p>
|
||||||
|
*/
|
||||||
|
public final class TrueTypeFontRenderer implements FontRenderer {
|
||||||
|
|
||||||
|
private static final int[] COLORS = new int[]{
|
||||||
|
0xFF000000, 0xFF0000AA, 0xFF00AA00, 0xFF00AAAA,
|
||||||
|
0xFFAA0000, 0xFFAA00AA, 0xFFFFAA00, 0xFFAAAAAA,
|
||||||
|
0xFF555555, 0xFF5555FF, 0xFF55FF55, 0xFF55FFFF,
|
||||||
|
0xFFFF5555, 0xFFFF55FF, 0xFFFFFF55, 0xFFFFFFFF
|
||||||
|
};
|
||||||
|
|
||||||
|
private final FontManager manager;
|
||||||
|
private final FontFace primary;
|
||||||
|
private final GlyphDrawer drawer;
|
||||||
|
private final boolean bold;
|
||||||
|
private final double guiScale;
|
||||||
|
|
||||||
|
private FontFace fallback;
|
||||||
|
|
||||||
|
public TrueTypeFontRenderer(FontManager manager, FontFace primary,
|
||||||
|
boolean bold, double guiScale,
|
||||||
|
GlyphDrawer drawer) {
|
||||||
|
this.manager = manager;
|
||||||
|
this.primary = primary;
|
||||||
|
this.bold = bold;
|
||||||
|
this.guiScale = guiScale;
|
||||||
|
this.drawer = drawer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawString(String text, double x, double y, int argb) {
|
||||||
|
draw(text, x, y, argb, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawStringWithShadow(String text, double x, double y, int argb) {
|
||||||
|
draw(text, x, y, argb, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawCenteredString(String text, double centerX, double y, int argb) {
|
||||||
|
drawString(text, centerX - getStringWidth(text) / 2.0, y, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawCenteredStringWithShadow(String text, double centerX,
|
||||||
|
double y, int argb) {
|
||||||
|
drawStringWithShadow(text, centerX - getStringWidth(text) / 2.0, y, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getStringWidth(String text) {
|
||||||
|
if (text == null || text.isEmpty()) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
Double cached = primary.getCachedWidth(text);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
double width = getUncachedWidth(text);
|
||||||
|
primary.cacheWidth(text, width);
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getStringHeight() {
|
||||||
|
float height = primary.getLineHeight() * primary.getDrawScale();
|
||||||
|
return height > 0.0f ? height : primary.getLogicalSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void draw(String text, double x, double y, int argb, boolean shadow) {
|
||||||
|
if (text == null || text.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int color = normalizeAlpha(argb);
|
||||||
|
if (shadow) {
|
||||||
|
drawPass(text, x + 1.0, y + 1.0, color, true);
|
||||||
|
}
|
||||||
|
drawPass(text, x, y, color, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawPass(String text, double x, double y,
|
||||||
|
int baseColor, boolean shadow) {
|
||||||
|
int color = baseColor;
|
||||||
|
double penX = x;
|
||||||
|
|
||||||
|
for (int i = 0; i < text.length(); ) {
|
||||||
|
char ch = text.charAt(i);
|
||||||
|
|
||||||
|
if (ch == '\u00a7' && i + 1 < text.length()) {
|
||||||
|
char code = text.charAt(i + 1);
|
||||||
|
color = colorFor(code, baseColor);
|
||||||
|
i += 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int codePoint;
|
||||||
|
if (Character.isHighSurrogate(ch) && i + 1 < text.length()) {
|
||||||
|
codePoint = Character.toCodePoint(ch, text.charAt(i + 1));
|
||||||
|
i += 2;
|
||||||
|
} else {
|
||||||
|
codePoint = ch;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResolvedGlyph resolved = resolve(codePoint);
|
||||||
|
if (resolved == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
float scale = resolved.face.getDrawScale();
|
||||||
|
double baseline = y + resolved.face.getAscent() * scale;
|
||||||
|
int drawColor = shadow ? shadowColor(color) : color;
|
||||||
|
|
||||||
|
if (!resolved.glyph.isBlank()) {
|
||||||
|
drawer.drawGlyph(resolved.face.getTexture(), resolved.glyph,
|
||||||
|
penX, baseline, scale, drawColor);
|
||||||
|
}
|
||||||
|
penX += resolved.glyph.getAdvance() * scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private double getUncachedWidth(String text) {
|
||||||
|
double width = 0.0;
|
||||||
|
for (int i = 0; i < text.length(); ) {
|
||||||
|
char ch = text.charAt(i);
|
||||||
|
|
||||||
|
if (ch == '\u00a7' && i + 1 < text.length()) {
|
||||||
|
i += 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int codePoint;
|
||||||
|
if (Character.isHighSurrogate(ch) && i + 1 < text.length()) {
|
||||||
|
codePoint = Character.toCodePoint(ch, text.charAt(i + 1));
|
||||||
|
i += 2;
|
||||||
|
} else {
|
||||||
|
codePoint = ch;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResolvedGlyph resolved = resolve(codePoint);
|
||||||
|
if (resolved == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
width += resolved.glyph.getAdvance() * resolved.face.getDrawScale();
|
||||||
|
}
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResolvedGlyph resolve(int codePoint) {
|
||||||
|
Glyph glyph = primary.getGlyph(codePoint);
|
||||||
|
if (glyph != null) {
|
||||||
|
return new ResolvedGlyph(primary, glyph);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ASCII/Latin faces do not contain CJK. Lazy-load Noto only when a
|
||||||
|
// missing code point is actually encountered.
|
||||||
|
if (!primary.getFamily().isCjkCapable()) {
|
||||||
|
if (fallback == null) {
|
||||||
|
fallback = manager.getFace(FontFamily.NOTO,
|
||||||
|
primary.getLogicalSize(), bold, guiScale);
|
||||||
|
}
|
||||||
|
glyph = fallback.getGlyph(codePoint);
|
||||||
|
if (glyph != null) {
|
||||||
|
return new ResolvedGlyph(fallback, glyph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glyph = primary.getFallbackGlyph();
|
||||||
|
return glyph != null ? new ResolvedGlyph(primary, glyph) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int normalizeAlpha(int argb) {
|
||||||
|
if ((argb >>> 24) == 0) {
|
||||||
|
return 0xFF000000 | (argb & 0x00FFFFFF);
|
||||||
|
}
|
||||||
|
return argb;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int colorFor(char code, int baseColor) {
|
||||||
|
if (code == 'r' || code == 'R') {
|
||||||
|
return baseColor;
|
||||||
|
}
|
||||||
|
if (code >= '0' && code <= '9') {
|
||||||
|
return applyAlpha(COLORS[code - '0'], baseColor);
|
||||||
|
}
|
||||||
|
if (code >= 'a' && code <= 'f') {
|
||||||
|
return applyAlpha(COLORS[10 + code - 'a'], baseColor);
|
||||||
|
}
|
||||||
|
if (code >= 'A' && code <= 'F') {
|
||||||
|
return applyAlpha(COLORS[10 + code - 'A'], baseColor);
|
||||||
|
}
|
||||||
|
return baseColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int applyAlpha(int rgb, int baseColor) {
|
||||||
|
return (baseColor & 0xFF000000) | (rgb & 0x00FFFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int shadowColor(int color) {
|
||||||
|
int alpha = color & 0xFF000000;
|
||||||
|
int red = (color >> 16) & 0xFF;
|
||||||
|
int green = (color >> 8) & 0xFF;
|
||||||
|
int blue = color & 0xFF;
|
||||||
|
return alpha
|
||||||
|
| ((red / 4) << 16)
|
||||||
|
| ((green / 4) << 8)
|
||||||
|
| (blue / 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class ResolvedGlyph {
|
||||||
|
private final FontFace face;
|
||||||
|
private final Glyph glyph;
|
||||||
|
|
||||||
|
private ResolvedGlyph(FontFace face, Glyph glyph) {
|
||||||
|
this.face = face;
|
||||||
|
this.glyph = glyph;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Recovered from the sample class that collided with Javassist's
|
||||||
|
* javassist.bytecode.analysis.Frame.
|
||||||
|
*/
|
||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
/** A lightweight cursor over a byte-array slice used by the STB parser. */
|
||||||
|
public final class StbByteBufferView {
|
||||||
|
public byte[] f;
|
||||||
|
public int y;
|
||||||
|
public int C;
|
||||||
|
public int h;
|
||||||
|
|
||||||
|
public StbByteBufferView() {
|
||||||
|
this(null, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StbByteBufferView(byte[] data, int offset, int length) {
|
||||||
|
this.f = data;
|
||||||
|
this.C = offset;
|
||||||
|
this.h = 0;
|
||||||
|
this.y = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StbByteBufferView k() {
|
||||||
|
StbByteBufferView copy = new StbByteBufferView();
|
||||||
|
copy.f = this.f;
|
||||||
|
copy.C = this.C;
|
||||||
|
copy.h = this.h;
|
||||||
|
copy.y = this.y;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeActiveEdge {
|
||||||
|
public float j;
|
||||||
|
public float e;
|
||||||
|
public float N;
|
||||||
|
public float m;
|
||||||
|
public StbTrueTypeActiveEdge l;
|
||||||
|
public float T;
|
||||||
|
public float L;
|
||||||
|
}
|
||||||
|
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeAlignedQuad {
|
||||||
|
public float K;
|
||||||
|
public float G;
|
||||||
|
public float C;
|
||||||
|
public float x;
|
||||||
|
public float v;
|
||||||
|
public float M;
|
||||||
|
public float O;
|
||||||
|
public float S;
|
||||||
|
}
|
||||||
|
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeBakedChar {
|
||||||
|
public int V;
|
||||||
|
public float k;
|
||||||
|
public float l;
|
||||||
|
public int D;
|
||||||
|
public float B;
|
||||||
|
public int U;
|
||||||
|
public int Q;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeBitmap {
|
||||||
|
public byte[] E;
|
||||||
|
public int c;
|
||||||
|
public int K;
|
||||||
|
public int h;
|
||||||
|
public int A;
|
||||||
|
}
|
||||||
|
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypeVertex;
|
||||||
|
|
||||||
|
public class StbTrueTypeCharStringContext {
|
||||||
|
public int Z;
|
||||||
|
public int c;
|
||||||
|
public int q;
|
||||||
|
public float p;
|
||||||
|
public int v;
|
||||||
|
public float o;
|
||||||
|
public float s;
|
||||||
|
public float C;
|
||||||
|
public int J;
|
||||||
|
public int y;
|
||||||
|
public int f;
|
||||||
|
public StbTrueTypeVertex[] V;
|
||||||
|
|
||||||
|
public StbTrueTypeCharStringContext(int n) {
|
||||||
|
this.J = n;
|
||||||
|
this.v = 0;
|
||||||
|
this.s = 0.0f;
|
||||||
|
this.p = 0.0f;
|
||||||
|
this.C = 0.0f;
|
||||||
|
this.o = 0.0f;
|
||||||
|
this.y = 0;
|
||||||
|
this.c = 0;
|
||||||
|
this.Z = 0;
|
||||||
|
this.f = 0;
|
||||||
|
this.V = null;
|
||||||
|
this.q = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeEdge {
|
||||||
|
public float u;
|
||||||
|
public float d;
|
||||||
|
public int W;
|
||||||
|
public float X;
|
||||||
|
public float i;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeFontInfo {
|
||||||
|
public int h;
|
||||||
|
public int G;
|
||||||
|
public StbByteBufferView H;
|
||||||
|
public int Y;
|
||||||
|
public int L;
|
||||||
|
public int Q;
|
||||||
|
public int U;
|
||||||
|
public StbByteBufferView g = new StbByteBufferView();
|
||||||
|
public int c;
|
||||||
|
public StbByteBufferView j;
|
||||||
|
public byte[] S;
|
||||||
|
public StbByteBufferView z = new StbByteBufferView();
|
||||||
|
public StbByteBufferView J;
|
||||||
|
public int d;
|
||||||
|
public int T;
|
||||||
|
public int w;
|
||||||
|
public StbByteBufferView N;
|
||||||
|
public int v;
|
||||||
|
public int p;
|
||||||
|
|
||||||
|
public StbTrueTypeFontInfo() {
|
||||||
|
this.j = new StbByteBufferView();
|
||||||
|
this.J = new StbByteBufferView();
|
||||||
|
this.H = new StbByteBufferView();
|
||||||
|
this.N = new StbByteBufferView();
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeGlyphPackNode {
|
||||||
|
public int z;
|
||||||
|
}
|
||||||
|
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeKerningEntry {
|
||||||
|
public int y;
|
||||||
|
public int q;
|
||||||
|
public int o;
|
||||||
|
}
|
||||||
|
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypePackAtlasState {
|
||||||
|
public int F;
|
||||||
|
public int a;
|
||||||
|
public int n;
|
||||||
|
public int f;
|
||||||
|
public int t;
|
||||||
|
}
|
||||||
|
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypeGlyphPackNode;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypePackAtlasState;
|
||||||
|
|
||||||
|
public class StbTrueTypePackContext {
|
||||||
|
public int d;
|
||||||
|
public StbTrueTypeGlyphPackNode[] j;
|
||||||
|
public int m;
|
||||||
|
public int h;
|
||||||
|
public int G;
|
||||||
|
public int b;
|
||||||
|
public StbTrueTypePackAtlasState J;
|
||||||
|
public int c;
|
||||||
|
public int w;
|
||||||
|
public byte[] X;
|
||||||
|
}
|
||||||
|
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.stb.StbTrueTypePackedChar;
|
||||||
|
|
||||||
|
public class StbTrueTypePackRange {
|
||||||
|
public int y;
|
||||||
|
public int R;
|
||||||
|
public int[] N;
|
||||||
|
public StbTrueTypePackedChar[] M;
|
||||||
|
public int i;
|
||||||
|
public float E;
|
||||||
|
private static String[] j;
|
||||||
|
public int b;
|
||||||
|
|
||||||
|
public static void t(String[] stringArray) {
|
||||||
|
j = stringArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] x() {
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
if (StbTrueTypePackRange.x() != null) {
|
||||||
|
StbTrueTypePackRange.t(new String[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypePackRectangle {
|
||||||
|
public int e;
|
||||||
|
public int J;
|
||||||
|
public int P;
|
||||||
|
public int M;
|
||||||
|
public int N;
|
||||||
|
public int q;
|
||||||
|
}
|
||||||
|
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypePackedChar {
|
||||||
|
public int p;
|
||||||
|
public int q;
|
||||||
|
public float R;
|
||||||
|
public int k;
|
||||||
|
public float A;
|
||||||
|
public int b;
|
||||||
|
public float G;
|
||||||
|
public float t;
|
||||||
|
public float s;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypePoint {
|
||||||
|
public float E;
|
||||||
|
public float M;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.font.stb;
|
||||||
|
|
||||||
|
public class StbTrueTypeVertex {
|
||||||
|
public short h;
|
||||||
|
public int O;
|
||||||
|
public short H;
|
||||||
|
public short a;
|
||||||
|
public short D;
|
||||||
|
public short s;
|
||||||
|
public int v;
|
||||||
|
public short f;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontAtlas;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Version-specific GPU operations for the font renderer.
|
||||||
|
*/
|
||||||
|
public interface FontBackend {
|
||||||
|
|
||||||
|
FontTexture createTexture(FontAtlas atlas);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bytes per atlas texel as uploaded by {@link #createTexture}, used to
|
||||||
|
* enforce the texture memory budget. RGBA backends return 4; legacy
|
||||||
|
* alpha backends can override this.
|
||||||
|
*/
|
||||||
|
default int getBytesPerTexel() {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
Render2D createRender2D(FontManager fonts, Object platformContext);
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.v1_8_9.V1_8_9FontBackend;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.v26_2.V26_2FontBackend;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selects the font backend for the detected Minecraft version.
|
||||||
|
*/
|
||||||
|
public final class FontBackendFactory {
|
||||||
|
|
||||||
|
public static FontBackend create(RuntimeInfo runtimeInfo,
|
||||||
|
MappingResolver mappingResolver,
|
||||||
|
Object minecraft) {
|
||||||
|
switch (runtimeInfo.getMinecraftVersion()) {
|
||||||
|
case V1_8_9:
|
||||||
|
return new V1_8_9FontBackend(runtimeInfo, mappingResolver, minecraft);
|
||||||
|
case V26_2:
|
||||||
|
return new V26_2FontBackend(runtimeInfo);
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"No font backend for " + runtimeInfo.getMinecraftVersion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private FontBackendFactory() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opaque GPU texture owned by one {@link cn.xiaozhou233.orangex.render.font.FontAtlas}.
|
||||||
|
* The concrete type depends on the Minecraft version.
|
||||||
|
*/
|
||||||
|
public interface FontTexture {
|
||||||
|
|
||||||
|
int getWidth();
|
||||||
|
|
||||||
|
int getHeight();
|
||||||
|
|
||||||
|
void close();
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.Glyph;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws one glyph for the current frame. A drawer is created per frame because
|
||||||
|
* modern Minecraft renders the GUI through a deferred render-state object.
|
||||||
|
*/
|
||||||
|
public interface GlyphDrawer {
|
||||||
|
|
||||||
|
void drawGlyph(FontTexture texture, Glyph glyph,
|
||||||
|
double x, double y, double scale, int argb);
|
||||||
|
}
|
||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontAtlas;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontBackend;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
import org.lwjgl.opengl.GL12;
|
||||||
|
import org.lwjgl.opengl.GL13;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Legacy OpenGL backend used by Minecraft 1.8.9.
|
||||||
|
*
|
||||||
|
* <p>The atlas is uploaded as {@code GL_ALPHA}; the fixed-function pipeline
|
||||||
|
* modulates the current colour with the glyph coverage, which is exactly what
|
||||||
|
* the old Minecraft font renderer does.</p>
|
||||||
|
*/
|
||||||
|
public final class V1_8_9FontBackend implements FontBackend {
|
||||||
|
|
||||||
|
private final RuntimeInfo runtimeInfo;
|
||||||
|
private final MappingResolver resolver;
|
||||||
|
private final Object minecraft;
|
||||||
|
private final V1_8_9RenderState state = new V1_8_9RenderState();
|
||||||
|
private final V1_8_9GlyphDrawer drawer = new V1_8_9GlyphDrawer(state);
|
||||||
|
|
||||||
|
public V1_8_9FontBackend(RuntimeInfo runtimeInfo,
|
||||||
|
MappingResolver resolver,
|
||||||
|
Object minecraft) {
|
||||||
|
this.runtimeInfo = runtimeInfo;
|
||||||
|
this.resolver = resolver;
|
||||||
|
this.minecraft = minecraft;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontTexture createTexture(FontAtlas atlas) {
|
||||||
|
int previousTexture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||||
|
int previousAlignment = GL11.glGetInteger(GL11.GL_UNPACK_ALIGNMENT);
|
||||||
|
int previousActiveTexture = GL11.glGetInteger(GL13.GL_ACTIVE_TEXTURE);
|
||||||
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||||
|
int textureId = GL11.glGenTextures();
|
||||||
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
|
||||||
|
|
||||||
|
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
|
||||||
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
|
||||||
|
GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
||||||
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
|
||||||
|
GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
||||||
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
|
||||||
|
GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
|
||||||
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
|
||||||
|
GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
|
||||||
|
|
||||||
|
// Two bytes per texel instead of four. GL_ALPHA would be even smaller
|
||||||
|
// but samples RGB as 0, which GL_MODULATE turns into black text;
|
||||||
|
// LUMINANCE_ALPHA keeps RGB = coverage and therefore behaves exactly
|
||||||
|
// like the RGBA upload.
|
||||||
|
ByteBuffer buffer = BufferUtils.createByteBuffer(
|
||||||
|
atlas.getWidth() * atlas.getHeight() * 2);
|
||||||
|
byte[] coverage = atlas.getPixels();
|
||||||
|
for (int i = 0; i < coverage.length; i++) {
|
||||||
|
buffer.put(coverage[i]);
|
||||||
|
buffer.put(coverage[i]);
|
||||||
|
}
|
||||||
|
buffer.flip();
|
||||||
|
|
||||||
|
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_LUMINANCE_ALPHA,
|
||||||
|
atlas.getWidth(), atlas.getHeight(), 0,
|
||||||
|
GL11.GL_LUMINANCE_ALPHA, GL11.GL_UNSIGNED_BYTE, buffer);
|
||||||
|
|
||||||
|
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, previousAlignment);
|
||||||
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, previousTexture);
|
||||||
|
GL13.glActiveTexture(previousActiveTexture);
|
||||||
|
return new V1_8_9FontTexture(textureId, atlas.getWidth(), atlas.getHeight());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBytesPerTexel() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Render2D createRender2D(FontManager fonts, Object platformContext) {
|
||||||
|
return new V1_8_9Render2D(fonts, runtimeInfo, resolver, minecraft, drawer, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "1.8.9/GL11";
|
||||||
|
}
|
||||||
|
}
|
||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
|
public final class V1_8_9FontTexture implements FontTexture {
|
||||||
|
|
||||||
|
private final int textureId;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
V1_8_9FontTexture(int textureId, int width, int height) {
|
||||||
|
this.textureId = textureId;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getTextureId() {
|
||||||
|
return textureId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
GL11.glDeleteTextures(textureId);
|
||||||
|
}
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.Glyph;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.GlyphDrawer;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Immediate-mode glyph drawer for the legacy (1.8.9) OpenGL pipeline.
|
||||||
|
*/
|
||||||
|
final class V1_8_9GlyphDrawer implements GlyphDrawer {
|
||||||
|
|
||||||
|
private final V1_8_9RenderState state;
|
||||||
|
private int boundTexture = -1;
|
||||||
|
|
||||||
|
V1_8_9GlyphDrawer(V1_8_9RenderState state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawGlyph(FontTexture texture, Glyph glyph,
|
||||||
|
double penX, double baseline, double scale, int argb) {
|
||||||
|
if (!(texture instanceof V1_8_9FontTexture)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.begin();
|
||||||
|
state.enableTexture();
|
||||||
|
|
||||||
|
int textureId = ((V1_8_9FontTexture) texture).getTextureId();
|
||||||
|
if (textureId != boundTexture) {
|
||||||
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
|
||||||
|
boundTexture = textureId;
|
||||||
|
}
|
||||||
|
state.setGlyphColor(argb);
|
||||||
|
|
||||||
|
double left = penX + glyph.getX0() * scale;
|
||||||
|
double right = penX + glyph.getX1() * scale;
|
||||||
|
double top = baseline + glyph.getY0() * scale;
|
||||||
|
double bottom = baseline + glyph.getY1() * scale;
|
||||||
|
|
||||||
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
|
GL11.glTexCoord2f(glyph.getU0(), glyph.getV0());
|
||||||
|
GL11.glVertex2d(left, top);
|
||||||
|
GL11.glTexCoord2f(glyph.getU0(), glyph.getV1());
|
||||||
|
GL11.glVertex2d(left, bottom);
|
||||||
|
GL11.glTexCoord2f(glyph.getU1(), glyph.getV1());
|
||||||
|
GL11.glVertex2d(right, bottom);
|
||||||
|
GL11.glTexCoord2f(glyph.getU1(), glyph.getV0());
|
||||||
|
GL11.glVertex2d(right, top);
|
||||||
|
GL11.glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
boundTexture = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
+326
@@ -0,0 +1,326 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingClass;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontFace;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontFamily;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.TrueTypeFontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1.8.9 2D context. Coordinates are the vanilla {@code ScaledResolution}
|
||||||
|
* pixels, which matches the ortho set up by {@code GuiIngame#renderGameOverlay}.
|
||||||
|
*/
|
||||||
|
final class V1_8_9Render2D implements Render2D {
|
||||||
|
|
||||||
|
private static final String SCALED_RESOLUTION =
|
||||||
|
"net/minecraft/client/gui/ScaledResolution";
|
||||||
|
private static final int ARC_SEGMENTS = 8;
|
||||||
|
|
||||||
|
private final FontManager fonts;
|
||||||
|
private final double guiScale;
|
||||||
|
private final V1_8_9GlyphDrawer drawer;
|
||||||
|
private final V1_8_9RenderState state;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
private final List<int[]> scissorStack = new ArrayList<int[]>();
|
||||||
|
private final IntBuffer scissorBuffer = BufferUtils.createIntBuffer(16);
|
||||||
|
|
||||||
|
V1_8_9Render2D(FontManager fonts, RuntimeInfo runtimeInfo,
|
||||||
|
MappingResolver resolver, Object minecraft,
|
||||||
|
V1_8_9GlyphDrawer drawer, V1_8_9RenderState state) {
|
||||||
|
this.fonts = fonts;
|
||||||
|
this.drawer = drawer;
|
||||||
|
this.state = state;
|
||||||
|
|
||||||
|
int resolvedWidth = 854;
|
||||||
|
int resolvedHeight = 480;
|
||||||
|
int resolvedScale = 1;
|
||||||
|
|
||||||
|
try {
|
||||||
|
MappingClass mappingClass = resolver.mapClass(SCALED_RESOLUTION);
|
||||||
|
Class<?> scaledClass = Class.forName(
|
||||||
|
mappingClass.getTargetName().replace('/', '.'),
|
||||||
|
false, runtimeInfo.getMinecraftClassLoader());
|
||||||
|
Constructor<?> constructor = scaledClass.getDeclaredConstructor(
|
||||||
|
minecraft.getClass());
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
Object scaled = constructor.newInstance(minecraft);
|
||||||
|
|
||||||
|
Method widthMethod = method(scaledClass, resolver, "getScaledWidth", "()I");
|
||||||
|
Method heightMethod = method(scaledClass, resolver, "getScaledHeight", "()I");
|
||||||
|
Method scaleMethod = method(scaledClass, resolver, "getScaleFactor", "()I");
|
||||||
|
|
||||||
|
resolvedWidth = (Integer) widthMethod.invoke(scaled);
|
||||||
|
resolvedHeight = (Integer) heightMethod.invoke(scaled);
|
||||||
|
resolvedScale = (Integer) scaleMethod.invoke(scaled);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
System.err.println("[OrangeX] Failed to resolve ScaledResolution for 1.8.9: "
|
||||||
|
+ throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.width = resolvedWidth;
|
||||||
|
this.height = resolvedHeight;
|
||||||
|
this.guiScale = Math.max(1, resolvedScale);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Method method(Class<?> owner, MappingResolver resolver,
|
||||||
|
String name, String descriptor) throws Exception {
|
||||||
|
MappingMethod mapping = resolver.mapMethod(
|
||||||
|
SCALED_RESOLUTION, name, descriptor);
|
||||||
|
Method method = owner.getDeclaredMethod(mapping.getTargetName());
|
||||||
|
method.setAccessible(true);
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getGuiScale() {
|
||||||
|
return guiScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(float size) {
|
||||||
|
return getFont(size, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(float size, boolean bold) {
|
||||||
|
return getFont(fonts.getDefaultFamily(), size, bold);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(FontFamily family, float size) {
|
||||||
|
return getFont(family, size, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(FontFamily family, float size, boolean bold) {
|
||||||
|
FontFace face = fonts.getFace(family, size, bold, guiScale);
|
||||||
|
return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillRect(double x, double y, double width, double height, int argb) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.begin();
|
||||||
|
state.disableTexture();
|
||||||
|
state.setColor(argb);
|
||||||
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
|
GL11.glVertex2d(x, y);
|
||||||
|
GL11.glVertex2d(x + width, y);
|
||||||
|
GL11.glVertex2d(x + width, y + height);
|
||||||
|
GL11.glVertex2d(x, y + height);
|
||||||
|
GL11.glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillGradient(double x, double y, double width, double height,
|
||||||
|
int topArgb, int bottomArgb) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.begin();
|
||||||
|
state.disableTexture();
|
||||||
|
GL11.glShadeModel(GL11.GL_SMOOTH);
|
||||||
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
|
|
||||||
|
state.setColor(topArgb);
|
||||||
|
GL11.glVertex2d(x, y);
|
||||||
|
GL11.glVertex2d(x + width, y);
|
||||||
|
state.setColor(bottomArgb);
|
||||||
|
GL11.glVertex2d(x + width, y + height);
|
||||||
|
GL11.glVertex2d(x, y + height);
|
||||||
|
GL11.glEnd();
|
||||||
|
GL11.glShadeModel(GL11.GL_FLAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawRect(double x, double y, double width, double height,
|
||||||
|
double thickness, int argb) {
|
||||||
|
if (thickness <= 0.0 || width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fillRect(x, y, width, thickness, argb);
|
||||||
|
fillRect(x, y + height - thickness, width, thickness, argb);
|
||||||
|
fillRect(x, y + thickness, thickness, height - thickness * 2.0, argb);
|
||||||
|
fillRect(x + width - thickness, y + thickness,
|
||||||
|
thickness, height - thickness * 2.0, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillRoundedRect(double x, double y, double width, double height,
|
||||||
|
double radius, int argb) {
|
||||||
|
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillRoundedRect(double x, double y, double width, double height,
|
||||||
|
double radius, double borderWidth,
|
||||||
|
int argb, int borderArgb) {
|
||||||
|
if (borderWidth <= 0.0) {
|
||||||
|
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fillRoundedRectInternal(x, y, width, height, radius, borderArgb);
|
||||||
|
double inner = Math.max(0.0, borderWidth);
|
||||||
|
double innerWidth = width - inner * 2.0;
|
||||||
|
double innerHeight = height - inner * 2.0;
|
||||||
|
if (innerWidth > 0.0 && innerHeight > 0.0) {
|
||||||
|
fillRoundedRectInternal(x + inner, y + inner,
|
||||||
|
innerWidth, innerHeight,
|
||||||
|
Math.max(0.0, radius - inner), argb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillRoundedRectInternal(double x, double y, double width,
|
||||||
|
double height, double radius, int argb) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double r = Math.min(radius, Math.min(width, height) / 2.0);
|
||||||
|
if (r <= 0.0) {
|
||||||
|
fillRect(x, y, width, height, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<double[]> points = roundedRectPerimeter(x, y, width, height, r);
|
||||||
|
if (points.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.begin();
|
||||||
|
state.disableTexture();
|
||||||
|
state.setColor(argb);
|
||||||
|
GL11.glBegin(GL11.GL_TRIANGLE_FAN);
|
||||||
|
GL11.glVertex2d(x + width / 2.0, y + height / 2.0);
|
||||||
|
for (double[] point : points) {
|
||||||
|
GL11.glVertex2d(point[0], point[1]);
|
||||||
|
}
|
||||||
|
double[] first = points.get(0);
|
||||||
|
GL11.glVertex2d(first[0], first[1]);
|
||||||
|
GL11.glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawLine(double x1, double y1, double x2, double y2,
|
||||||
|
double thickness, int argb) {
|
||||||
|
if (thickness <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double dx = x2 - x1;
|
||||||
|
double dy = y2 - y1;
|
||||||
|
if (Math.abs(dy) < 0.01) {
|
||||||
|
fillRect(Math.min(x1, x2), y1 - thickness / 2.0,
|
||||||
|
Math.abs(dx), thickness, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Math.abs(dx) < 0.01) {
|
||||||
|
fillRect(x1 - thickness / 2.0, Math.min(y1, y2),
|
||||||
|
thickness, Math.abs(dy), argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.begin();
|
||||||
|
state.disableTexture();
|
||||||
|
state.setColor(argb);
|
||||||
|
GL11.glLineWidth((float) thickness);
|
||||||
|
GL11.glBegin(GL11.GL_LINES);
|
||||||
|
GL11.glVertex2d(x1, y1);
|
||||||
|
GL11.glVertex2d(x2, y2);
|
||||||
|
GL11.glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enableScissor(double x, double y, double width, double height) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.begin();
|
||||||
|
|
||||||
|
scissorBuffer.clear();
|
||||||
|
GL11.glGetInteger(GL11.GL_SCISSOR_BOX, scissorBuffer);
|
||||||
|
scissorStack.add(new int[]{
|
||||||
|
scissorBuffer.get(0), scissorBuffer.get(1),
|
||||||
|
scissorBuffer.get(2), scissorBuffer.get(3)
|
||||||
|
});
|
||||||
|
|
||||||
|
int scale = (int) Math.round(guiScale);
|
||||||
|
int sx = (int) Math.round(x * scale);
|
||||||
|
int sy = (int) Math.round((this.height - y - height) * scale);
|
||||||
|
int sw = (int) Math.round(width * scale);
|
||||||
|
int sh = (int) Math.round(height * scale);
|
||||||
|
|
||||||
|
GL11.glEnable(GL11.GL_SCISSOR_TEST);
|
||||||
|
GL11.glScissor(sx, sy, Math.max(0, sw), Math.max(0, sh));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disableScissor() {
|
||||||
|
if (scissorStack.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int[] box = scissorStack.remove(scissorStack.size() - 1);
|
||||||
|
if (scissorStack.isEmpty()) {
|
||||||
|
GL11.glDisable(GL11.GL_SCISSOR_TEST);
|
||||||
|
} else {
|
||||||
|
GL11.glScissor(box[0], box[1], box[2], box[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<double[]> roundedRectPerimeter(double x, double y,
|
||||||
|
double width, double height,
|
||||||
|
double radius) {
|
||||||
|
List<double[]> points = new ArrayList<double[]>();
|
||||||
|
double right = x + width;
|
||||||
|
double bottom = y + height;
|
||||||
|
|
||||||
|
addArc(points, x + radius, y + radius, radius, 180.0, 270.0);
|
||||||
|
addArc(points, right - radius, y + radius, radius, 270.0, 360.0);
|
||||||
|
addArc(points, right - radius, bottom - radius, radius, 0.0, 90.0);
|
||||||
|
addArc(points, x + radius, bottom - radius, radius, 90.0, 180.0);
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addArc(List<double[]> points, double centerX,
|
||||||
|
double centerY, double radius,
|
||||||
|
double startDegrees, double endDegrees) {
|
||||||
|
for (int i = 0; i <= ARC_SEGMENTS; i++) {
|
||||||
|
double angle = Math.toRadians(startDegrees
|
||||||
|
+ (endDegrees - startDegrees) * i / ARC_SEGMENTS);
|
||||||
|
points.add(new double[]{
|
||||||
|
centerX + Math.cos(angle) * radius,
|
||||||
|
centerY + Math.sin(angle) * radius
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush() {
|
||||||
|
state.end();
|
||||||
|
drawer.reset();
|
||||||
|
scissorStack.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
+179
@@ -0,0 +1,179 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||||
|
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
import org.lwjgl.opengl.GL13;
|
||||||
|
|
||||||
|
import java.nio.FloatBuffer;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared fixed-function state for one 2D frame.
|
||||||
|
*
|
||||||
|
* <p>Shapes and glyphs both use this object so that interleaved draw calls do
|
||||||
|
* not save/restore each other's GL state. {@link #begin()} is idempotent and
|
||||||
|
* {@link #end()} restores the state that existed before the first draw.</p>
|
||||||
|
*/
|
||||||
|
final class V1_8_9RenderState {
|
||||||
|
|
||||||
|
private final FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(16);
|
||||||
|
private final IntBuffer scissorBuffer = BufferUtils.createIntBuffer(16);
|
||||||
|
|
||||||
|
private boolean active;
|
||||||
|
private boolean premultipliedBlend;
|
||||||
|
private boolean previousBlend;
|
||||||
|
private boolean previousTexture2D;
|
||||||
|
private boolean previousAlphaTest;
|
||||||
|
private boolean previousDepthTest;
|
||||||
|
private boolean previousCullFace;
|
||||||
|
private boolean previousLighting;
|
||||||
|
private boolean previousFog;
|
||||||
|
private boolean previousScissorTest;
|
||||||
|
private int previousTexture;
|
||||||
|
private int previousTextureEnvMode;
|
||||||
|
private int previousActiveTexture;
|
||||||
|
private int previousShadeModel;
|
||||||
|
private int previousBlendSrc;
|
||||||
|
private int previousBlendDst;
|
||||||
|
private float previousLineWidth;
|
||||||
|
private int previousScissorX;
|
||||||
|
private int previousScissorY;
|
||||||
|
private int previousScissorWidth;
|
||||||
|
private int previousScissorHeight;
|
||||||
|
|
||||||
|
void begin() {
|
||||||
|
if (active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
active = true;
|
||||||
|
|
||||||
|
previousBlend = GL11.glIsEnabled(GL11.GL_BLEND);
|
||||||
|
previousTexture2D = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
|
||||||
|
previousAlphaTest = GL11.glIsEnabled(GL11.GL_ALPHA_TEST);
|
||||||
|
previousDepthTest = GL11.glIsEnabled(GL11.GL_DEPTH_TEST);
|
||||||
|
previousCullFace = GL11.glIsEnabled(GL11.GL_CULL_FACE);
|
||||||
|
previousLighting = GL11.glIsEnabled(GL11.GL_LIGHTING);
|
||||||
|
previousFog = GL11.glIsEnabled(GL11.GL_FOG);
|
||||||
|
previousScissorTest = GL11.glIsEnabled(GL11.GL_SCISSOR_TEST);
|
||||||
|
previousTextureEnvMode = GL11.glGetTexEnvi(
|
||||||
|
GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE);
|
||||||
|
previousShadeModel = GL11.glGetInteger(GL11.GL_SHADE_MODEL);
|
||||||
|
previousBlendSrc = GL11.glGetInteger(GL11.GL_BLEND_SRC);
|
||||||
|
previousBlendDst = GL11.glGetInteger(GL11.GL_BLEND_DST);
|
||||||
|
previousLineWidth = GL11.glGetFloat(GL11.GL_LINE_WIDTH);
|
||||||
|
previousActiveTexture = GL11.glGetInteger(GL13.GL_ACTIVE_TEXTURE);
|
||||||
|
|
||||||
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||||
|
previousTexture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||||
|
|
||||||
|
scissorBuffer.clear();
|
||||||
|
GL11.glGetInteger(GL11.GL_SCISSOR_BOX, scissorBuffer);
|
||||||
|
previousScissorX = scissorBuffer.get(0);
|
||||||
|
previousScissorY = scissorBuffer.get(1);
|
||||||
|
previousScissorWidth = scissorBuffer.get(2);
|
||||||
|
previousScissorHeight = scissorBuffer.get(3);
|
||||||
|
|
||||||
|
colorBuffer.clear();
|
||||||
|
GL11.glGetFloat(GL11.GL_CURRENT_COLOR, colorBuffer);
|
||||||
|
|
||||||
|
GL11.glEnable(GL11.GL_BLEND);
|
||||||
|
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
premultipliedBlend = false;
|
||||||
|
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||||
|
GL11.glDisable(GL11.GL_ALPHA_TEST);
|
||||||
|
GL11.glDisable(GL11.GL_DEPTH_TEST);
|
||||||
|
GL11.glDisable(GL11.GL_CULL_FACE);
|
||||||
|
GL11.glDisable(GL11.GL_LIGHTING);
|
||||||
|
GL11.glDisable(GL11.GL_FOG);
|
||||||
|
GL11.glDisable(GL11.GL_SCISSOR_TEST);
|
||||||
|
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||||
|
GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void end() {
|
||||||
|
if (!active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
active = false;
|
||||||
|
|
||||||
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, previousTexture);
|
||||||
|
GL11.glColor4f(colorBuffer.get(0), colorBuffer.get(1),
|
||||||
|
colorBuffer.get(2), colorBuffer.get(3));
|
||||||
|
setEnabled(GL11.GL_BLEND, previousBlend);
|
||||||
|
setEnabled(GL11.GL_TEXTURE_2D, previousTexture2D);
|
||||||
|
setEnabled(GL11.GL_ALPHA_TEST, previousAlphaTest);
|
||||||
|
setEnabled(GL11.GL_DEPTH_TEST, previousDepthTest);
|
||||||
|
setEnabled(GL11.GL_CULL_FACE, previousCullFace);
|
||||||
|
setEnabled(GL11.GL_LIGHTING, previousLighting);
|
||||||
|
setEnabled(GL11.GL_FOG, previousFog);
|
||||||
|
setEnabled(GL11.GL_SCISSOR_TEST, previousScissorTest);
|
||||||
|
GL11.glScissor(previousScissorX, previousScissorY,
|
||||||
|
previousScissorWidth, previousScissorHeight);
|
||||||
|
GL11.glBlendFunc(previousBlendSrc, previousBlendDst);
|
||||||
|
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||||
|
GL11.GL_TEXTURE_ENV_MODE, previousTextureEnvMode);
|
||||||
|
GL11.glShadeModel(previousShadeModel);
|
||||||
|
GL11.glLineWidth(previousLineWidth);
|
||||||
|
GL13.glActiveTexture(previousActiveTexture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void enableTexture() {
|
||||||
|
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
|
||||||
|
void disableTexture() {
|
||||||
|
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setColor(int argb) {
|
||||||
|
useStandardBlend();
|
||||||
|
GL11.glColor4f(
|
||||||
|
((argb >> 16) & 0xFF) / 255.0f,
|
||||||
|
((argb >> 8) & 0xFF) / 255.0f,
|
||||||
|
(argb & 0xFF) / 255.0f,
|
||||||
|
((argb >>> 24) & 0xFF) / 255.0f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Colours for {@code LUMINANCE_ALPHA} glyph atlases.
|
||||||
|
*
|
||||||
|
* <p>That atlas format stores the coverage in both luminance and alpha, so
|
||||||
|
* {@code GL_MODULATE} produces {@code Cf * coverage} twice. Premultiplying
|
||||||
|
* the vertex colour and blending with {@code (ONE, ONE_MINUS_SRC_ALPHA)}
|
||||||
|
* keeps the result identical to the old RGBA upload while halving the
|
||||||
|
* texture size.</p>
|
||||||
|
*/
|
||||||
|
void setGlyphColor(int argb) {
|
||||||
|
usePremultipliedBlend();
|
||||||
|
float alpha = ((argb >>> 24) & 0xFF) / 255.0f;
|
||||||
|
GL11.glColor4f(
|
||||||
|
((argb >> 16) & 0xFF) / 255.0f * alpha,
|
||||||
|
((argb >> 8) & 0xFF) / 255.0f * alpha,
|
||||||
|
(argb & 0xFF) / 255.0f * alpha,
|
||||||
|
alpha
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void useStandardBlend() {
|
||||||
|
if (premultipliedBlend) {
|
||||||
|
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
premultipliedBlend = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void usePremultipliedBlend() {
|
||||||
|
if (!premultipliedBlend) {
|
||||||
|
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
premultipliedBlend = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setEnabled(int capability, boolean enabled) {
|
||||||
|
if (enabled) {
|
||||||
|
GL11.glEnable(capability);
|
||||||
|
} else {
|
||||||
|
GL11.glDisable(capability);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontAtlas;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontBackend;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 26.x backend.
|
||||||
|
*
|
||||||
|
* <p>The atlas is expanded to RGBA (white RGB + coverage alpha) and wrapped in
|
||||||
|
* a {@code DynamicTexture}. Glyph drawing is deferred through
|
||||||
|
* {@code GuiRenderState}, so no raw OpenGL calls are made here.</p>
|
||||||
|
*/
|
||||||
|
public final class V26_2FontBackend implements FontBackend {
|
||||||
|
|
||||||
|
private final ClassLoader loader;
|
||||||
|
private final V26_2ShapeCache shapes;
|
||||||
|
|
||||||
|
public V26_2FontBackend(RuntimeInfo runtimeInfo) {
|
||||||
|
this.loader = runtimeInfo.getMinecraftClassLoader();
|
||||||
|
this.shapes = new V26_2ShapeCache(loader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontTexture createTexture(FontAtlas atlas) {
|
||||||
|
int width = atlas.getWidth();
|
||||||
|
int height = atlas.getHeight();
|
||||||
|
byte[] coverage = atlas.getPixels();
|
||||||
|
|
||||||
|
V26_2GpuTexture texture = V26_2TextureHelper.createFromCoverage(
|
||||||
|
loader, width, height, coverage);
|
||||||
|
return new V26_2FontTexture(texture.texture, texture.textureView,
|
||||||
|
texture.sampler, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Render2D createRender2D(FontManager fonts, Object platformContext) {
|
||||||
|
return new V26_2Render2D(fonts, platformContext, loader, shapes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "26.2/GuiRenderState";
|
||||||
|
}
|
||||||
|
}
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps a 26.x {@code DynamicTexture}. All fields are kept as {@link Object}
|
||||||
|
* because OrangeX is compiled against Java 8 without Mojang/LWJGL3 on the
|
||||||
|
* compile classpath.
|
||||||
|
*/
|
||||||
|
public final class V26_2FontTexture implements FontTexture {
|
||||||
|
|
||||||
|
private final Object texture;
|
||||||
|
private final Object textureView;
|
||||||
|
private final Object sampler;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
V26_2FontTexture(Object texture, Object textureView, Object sampler,
|
||||||
|
int width, int height) {
|
||||||
|
this.texture = texture;
|
||||||
|
this.textureView = textureView;
|
||||||
|
this.sampler = sampler;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object getTexture() {
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object getTextureView() {
|
||||||
|
return textureView;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object getSampler() {
|
||||||
|
return sampler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
texture.getClass().getMethod("close").invoke(texture);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
System.err.println("[OrangeX] Failed to close a 26.2 font texture: "
|
||||||
|
+ throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+88
@@ -0,0 +1,88 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.font.Glyph;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||||
|
import cn.xiaozhou233.orangex.render.internal.GlyphDrawer;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds one deferred blit per glyph to the 26.x {@code GuiRenderState}.
|
||||||
|
*/
|
||||||
|
final class V26_2GlyphDrawer implements GlyphDrawer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum distance (in logical pixels) that still counts as "sitting on the
|
||||||
|
* baseline" rather than being a descender or a raised glyph.
|
||||||
|
*
|
||||||
|
* <p>The atlas is packed with 2x oversampling, so a normal glyph's baked
|
||||||
|
* bottom is 0.25/0.75px away from the baseline. 26.2 blits use integer
|
||||||
|
* coordinates, and rounding those offsets per glyph made some letters drop
|
||||||
|
* onto the next pixel while their neighbours stayed put. Snapping
|
||||||
|
* near-baseline bottoms to one shared value keeps a line visually level.</p>
|
||||||
|
*/
|
||||||
|
private static final double BASELINE_SNAP = 0.9;
|
||||||
|
|
||||||
|
private final Object extractor;
|
||||||
|
private final Method innerBlit;
|
||||||
|
private final Object guiPipeline;
|
||||||
|
|
||||||
|
V26_2GlyphDrawer(Object extractor, Method innerBlit, Object guiPipeline) {
|
||||||
|
this.extractor = extractor;
|
||||||
|
this.innerBlit = innerBlit;
|
||||||
|
this.guiPipeline = guiPipeline;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawGlyph(FontTexture texture, Glyph glyph,
|
||||||
|
double penX, double baseline, double scale, int argb) {
|
||||||
|
if (!(texture instanceof V26_2FontTexture)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snap both horizontal edges the same way (downwards). Rounding each
|
||||||
|
// edge independently made neighbouring glyphs drift apart or together
|
||||||
|
// by one pixel, which is most visible around narrow letters such as I.
|
||||||
|
int x0 = (int) Math.floor(penX + glyph.getX0() * scale + 0.0001);
|
||||||
|
int x1 = (int) Math.floor(penX + glyph.getX1() * scale + 0.0001);
|
||||||
|
|
||||||
|
// Anchor both edges to one rounded baseline so every glyph in a line
|
||||||
|
// shares the same pixel origin.
|
||||||
|
int base = (int) Math.round(baseline);
|
||||||
|
// Round the top down: curved letters (C, G, o...) overshoot the flat
|
||||||
|
// cap/x-height by half a pixel in the atlas. Rounding split them onto
|
||||||
|
// different rows, which made "CLICK GUI" look uneven.
|
||||||
|
int y0 = base + (int) Math.floor(glyph.getY0() * scale + 0.0001);
|
||||||
|
double bottomOffset = glyph.getY1() * scale;
|
||||||
|
int y1;
|
||||||
|
if (bottomOffset >= 0.0 && bottomOffset < BASELINE_SNAP) {
|
||||||
|
// Normal letter/punctuation: sit exactly on the baseline.
|
||||||
|
y1 = base;
|
||||||
|
} else {
|
||||||
|
// Descenders (g, p, y...) and raised glyphs keep their offset.
|
||||||
|
y1 = base + (int) Math.round(bottomOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Thin strokes (l, I, | ...) can be narrower than one GUI pixel at high
|
||||||
|
// GUI scales. Keep them visible with at least one pixel.
|
||||||
|
if (x1 <= x0) {
|
||||||
|
x1 = x0 + 1;
|
||||||
|
}
|
||||||
|
if (y1 <= y0) {
|
||||||
|
y1 = y0 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
V26_2FontTexture fontTexture = (V26_2FontTexture) texture;
|
||||||
|
try {
|
||||||
|
innerBlit.invoke(extractor, guiPipeline,
|
||||||
|
fontTexture.getTextureView(), fontTexture.getSampler(),
|
||||||
|
x0, y0, x1, y1,
|
||||||
|
glyph.getU0(), glyph.getU1(),
|
||||||
|
glyph.getV0(), glyph.getV1(),
|
||||||
|
argb);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Failed to submit a GUI glyph", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reflective holder for a 26.x GPU texture and its sampler.
|
||||||
|
*/
|
||||||
|
final class V26_2GpuTexture {
|
||||||
|
|
||||||
|
final Object texture;
|
||||||
|
final Object textureView;
|
||||||
|
final Object sampler;
|
||||||
|
final int width;
|
||||||
|
final int height;
|
||||||
|
|
||||||
|
V26_2GpuTexture(Object texture, Object textureView, Object sampler,
|
||||||
|
int width, int height) {
|
||||||
|
this.texture = texture;
|
||||||
|
this.textureView = textureView;
|
||||||
|
this.sampler = sampler;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
+341
@@ -0,0 +1,341 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontFace;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontFamily;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.TrueTypeFontRenderer;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 26.x 2D context.
|
||||||
|
*
|
||||||
|
* <p>26.x no longer exposes {@code GuiGraphics}; the GUI is collected into a
|
||||||
|
* {@code GuiRenderState} by {@code Gui#extractRenderState}. The render hook
|
||||||
|
* passes that {@code Gui} instance in, and this class creates a private
|
||||||
|
* {@code GuiGraphicsExtractor} over the same render state to queue glyph
|
||||||
|
* blits.</p>
|
||||||
|
*/
|
||||||
|
final class V26_2Render2D implements Render2D {
|
||||||
|
|
||||||
|
private final FontManager fonts;
|
||||||
|
private final V26_2GlyphDrawer drawer;
|
||||||
|
private final Object extractor;
|
||||||
|
private final Method innerBlit;
|
||||||
|
private final Object guiPipeline;
|
||||||
|
private final Method fillMethod;
|
||||||
|
private final Method fillGradientMethod;
|
||||||
|
private final Method enableScissorMethod;
|
||||||
|
private final Method disableScissorMethod;
|
||||||
|
private final V26_2ShapeCache shapes;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
private final double guiScale;
|
||||||
|
|
||||||
|
V26_2Render2D(FontManager fonts, Object gui, ClassLoader loader,
|
||||||
|
V26_2ShapeCache shapes) {
|
||||||
|
this.fonts = fonts;
|
||||||
|
this.shapes = shapes;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Field minecraftField = field(gui.getClass(), "minecraft");
|
||||||
|
Object minecraft = minecraftField.get(gui);
|
||||||
|
|
||||||
|
Field renderStateField = field(gui.getClass(), "guiRenderState");
|
||||||
|
Object guiRenderState = renderStateField.get(gui);
|
||||||
|
|
||||||
|
Class<?> renderStateClass = Class.forName(
|
||||||
|
"net.minecraft.client.renderer.state.gui.GuiRenderState",
|
||||||
|
false, loader);
|
||||||
|
Class<?> extractorClass = Class.forName(
|
||||||
|
"net.minecraft.client.gui.GuiGraphicsExtractor",
|
||||||
|
false, loader);
|
||||||
|
|
||||||
|
Constructor<?> extractorConstructor = extractorClass.getDeclaredConstructor(
|
||||||
|
minecraft.getClass(), renderStateClass, int.class, int.class);
|
||||||
|
extractorConstructor.setAccessible(true);
|
||||||
|
Object extractor = extractorConstructor.newInstance(
|
||||||
|
minecraft, guiRenderState, 0, 0);
|
||||||
|
|
||||||
|
Object window = minecraft.getClass().getMethod("getWindow").invoke(minecraft);
|
||||||
|
this.width = (Integer) window.getClass().getMethod("getGuiScaledWidth")
|
||||||
|
.invoke(window);
|
||||||
|
this.height = (Integer) window.getClass().getMethod("getGuiScaledHeight")
|
||||||
|
.invoke(window);
|
||||||
|
this.guiScale = Math.max(1, (Integer) window.getClass()
|
||||||
|
.getMethod("getGuiScale").invoke(window));
|
||||||
|
|
||||||
|
Class<?> pipelineClass = Class.forName(
|
||||||
|
"com.mojang.blaze3d.pipeline.RenderPipeline", false, loader);
|
||||||
|
Class<?> textureViewClass = Class.forName(
|
||||||
|
"com.mojang.blaze3d.textures.GpuTextureView", false, loader);
|
||||||
|
Class<?> samplerClass = Class.forName(
|
||||||
|
"com.mojang.blaze3d.textures.GpuSampler", false, loader);
|
||||||
|
|
||||||
|
Object guiPipeline = Class.forName(
|
||||||
|
"net.minecraft.client.renderer.RenderPipelines", false, loader)
|
||||||
|
.getField("GUI_TEXTURED").get(null);
|
||||||
|
|
||||||
|
Method innerBlit = extractorClass.getDeclaredMethod("innerBlit",
|
||||||
|
pipelineClass, textureViewClass, samplerClass,
|
||||||
|
int.class, int.class, int.class, int.class,
|
||||||
|
float.class, float.class, float.class, float.class,
|
||||||
|
int.class);
|
||||||
|
innerBlit.setAccessible(true);
|
||||||
|
|
||||||
|
this.extractor = extractor;
|
||||||
|
this.innerBlit = innerBlit;
|
||||||
|
this.guiPipeline = guiPipeline;
|
||||||
|
this.fillMethod = extractorClass.getMethod("fill",
|
||||||
|
int.class, int.class, int.class, int.class, int.class);
|
||||||
|
this.fillGradientMethod = extractorClass.getMethod("fillGradient",
|
||||||
|
int.class, int.class, int.class, int.class,
|
||||||
|
int.class, int.class);
|
||||||
|
this.enableScissorMethod = extractorClass.getMethod("enableScissor",
|
||||||
|
int.class, int.class, int.class, int.class);
|
||||||
|
this.disableScissorMethod = extractorClass.getMethod("disableScissor");
|
||||||
|
this.drawer = new V26_2GlyphDrawer(extractor, innerBlit, guiPipeline);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot initialise the 26.2 GUI font context", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Field field(Class<?> owner, String name) throws Exception {
|
||||||
|
for (Class<?> current = owner; current != null; current = current.getSuperclass()) {
|
||||||
|
try {
|
||||||
|
Field field = current.getDeclaredField(name);
|
||||||
|
field.setAccessible(true);
|
||||||
|
return field;
|
||||||
|
} catch (NoSuchFieldException ignored) {
|
||||||
|
// Continue with the superclass.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new NoSuchFieldException(name + " on " + owner.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getGuiScale() {
|
||||||
|
return guiScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(float size) {
|
||||||
|
return getFont(size, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(float size, boolean bold) {
|
||||||
|
return getFont(fonts.getDefaultFamily(), size, bold);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(FontFamily family, float size) {
|
||||||
|
return getFont(family, size, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FontRenderer getFont(FontFamily family, float size, boolean bold) {
|
||||||
|
FontFace face = fonts.getFace(family, size, bold, guiScale);
|
||||||
|
return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillRect(double x, double y, double width, double height, int argb) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int x0 = (int) Math.round(x);
|
||||||
|
int y0 = (int) Math.round(y);
|
||||||
|
int x1 = (int) Math.round(x + width);
|
||||||
|
int y1 = (int) Math.round(y + height);
|
||||||
|
invokeFill(x0, y0, x1, y1, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillGradient(double x, double y, double width, double height,
|
||||||
|
int topArgb, int bottomArgb) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
fillGradientMethod.invoke(extractor,
|
||||||
|
(int) Math.round(x), (int) Math.round(y),
|
||||||
|
(int) Math.round(x + width), (int) Math.round(y + height),
|
||||||
|
topArgb, bottomArgb);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Failed to draw a GUI gradient", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawRect(double x, double y, double width, double height,
|
||||||
|
double thickness, int argb) {
|
||||||
|
if (thickness <= 0.0 || width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fillRect(x, y, width, thickness, argb);
|
||||||
|
fillRect(x, y + height - thickness, width, thickness, argb);
|
||||||
|
fillRect(x, y + thickness, thickness, height - thickness * 2.0, argb);
|
||||||
|
fillRect(x + width - thickness, y + thickness,
|
||||||
|
thickness, height - thickness * 2.0, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillRoundedRect(double x, double y, double width, double height,
|
||||||
|
double radius, int argb) {
|
||||||
|
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillRoundedRect(double x, double y, double width, double height,
|
||||||
|
double radius, double borderWidth,
|
||||||
|
int argb, int borderArgb) {
|
||||||
|
if (borderWidth <= 0.0) {
|
||||||
|
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fillRoundedRectInternal(x, y, width, height, radius, borderArgb);
|
||||||
|
double inset = Math.max(0.0, borderWidth);
|
||||||
|
double innerWidth = width - inset * 2.0;
|
||||||
|
double innerHeight = height - inset * 2.0;
|
||||||
|
if (innerWidth > 0.0 && innerHeight > 0.0) {
|
||||||
|
fillRoundedRectInternal(x + inset, y + inset,
|
||||||
|
innerWidth, innerHeight,
|
||||||
|
Math.max(0.0, radius - inset), argb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillRoundedRectInternal(double x, double y, double width,
|
||||||
|
double height, double radius, int argb) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double clamped = Math.min(radius, Math.min(width, height) / 2.0);
|
||||||
|
if (clamped <= 0.5) {
|
||||||
|
fillRect(x, y, width, height, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int x0 = (int) Math.round(x);
|
||||||
|
int y0 = (int) Math.round(y);
|
||||||
|
int x1 = (int) Math.round(x + width);
|
||||||
|
int y1 = (int) Math.round(y + height);
|
||||||
|
int corner = Math.max(1, (int) Math.round(clamped));
|
||||||
|
corner = Math.min(corner, Math.min((x1 - x0) / 2, (y1 - y0) / 2));
|
||||||
|
if (corner <= 0) {
|
||||||
|
fillRect(x, y, width, height, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Straight strips.
|
||||||
|
invokeFill(x0 + corner, y0, x1 - corner, y0 + corner, argb);
|
||||||
|
invokeFill(x0 + corner, y1 - corner, x1 - corner, y1, argb);
|
||||||
|
invokeFill(x0 + corner, y0 + corner, x1 - corner, y1 - corner, argb);
|
||||||
|
invokeFill(x0, y0 + corner, x0 + corner, y1 - corner, argb);
|
||||||
|
invokeFill(x1 - corner, y0 + corner, x1, y1 - corner, argb);
|
||||||
|
|
||||||
|
int radiusPixels = Math.max(2, (int) Math.round(clamped * guiScale));
|
||||||
|
V26_2GpuTexture texture = shapes.corner(radiusPixels);
|
||||||
|
|
||||||
|
// Antialiased quarter-circle corners. Flipping U/V mirrors the same
|
||||||
|
// texture for the other three corners.
|
||||||
|
invokeBlit(texture, x0, y0, x0 + corner, y0 + corner,
|
||||||
|
0.0f, 0.0f, 1.0f, 1.0f, argb);
|
||||||
|
invokeBlit(texture, x1 - corner, y0, x1, y0 + corner,
|
||||||
|
1.0f, 0.0f, 0.0f, 1.0f, argb);
|
||||||
|
invokeBlit(texture, x0, y1 - corner, x0 + corner, y1,
|
||||||
|
0.0f, 1.0f, 1.0f, 0.0f, argb);
|
||||||
|
invokeBlit(texture, x1 - corner, y1 - corner, x1, y1,
|
||||||
|
1.0f, 1.0f, 0.0f, 0.0f, argb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawLine(double x1, double y1, double x2, double y2,
|
||||||
|
double thickness, int argb) {
|
||||||
|
if (thickness <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double dx = x2 - x1;
|
||||||
|
double dy = y2 - y1;
|
||||||
|
if (Math.abs(dy) < 0.01) {
|
||||||
|
fillRect(Math.min(x1, x2), y1 - thickness / 2.0,
|
||||||
|
Math.abs(dx), thickness, argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Math.abs(dx) < 0.01) {
|
||||||
|
fillRect(x1 - thickness / 2.0, Math.min(y1, y2),
|
||||||
|
thickness, Math.abs(dy), argb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int steps = (int) Math.ceil(Math.max(Math.abs(dx), Math.abs(dy)));
|
||||||
|
for (int i = 0; i <= steps; i++) {
|
||||||
|
double t = steps == 0 ? 0.0 : (double) i / (double) steps;
|
||||||
|
fillRect(x1 + dx * t - thickness / 2.0,
|
||||||
|
y1 + dy * t - thickness / 2.0,
|
||||||
|
thickness, thickness, argb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enableScissor(double x, double y, double width, double height) {
|
||||||
|
if (width <= 0.0 || height <= 0.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
enableScissorMethod.invoke(extractor,
|
||||||
|
(int) Math.round(x), (int) Math.round(y),
|
||||||
|
(int) Math.round(x + width), (int) Math.round(y + height));
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Failed to enable a GUI scissor", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disableScissor() {
|
||||||
|
try {
|
||||||
|
disableScissorMethod.invoke(extractor);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Failed to disable a GUI scissor", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void invokeFill(int x0, int y0, int x1, int y1, int argb) {
|
||||||
|
try {
|
||||||
|
fillMethod.invoke(extractor, x0, y0, x1, y1, argb);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Failed to draw a GUI rectangle", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void invokeBlit(V26_2GpuTexture texture, int x0, int y0, int x1, int y1,
|
||||||
|
float u0, float v0, float u1, float v1, int argb) {
|
||||||
|
try {
|
||||||
|
innerBlit.invoke(extractor, guiPipeline,
|
||||||
|
texture.textureView, texture.sampler,
|
||||||
|
x0, y0, x1, y1, u0, u1, v0, v1, argb);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Failed to draw a GUI corner", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush() {
|
||||||
|
// Everything was submitted to the GuiRenderState; nothing to restore.
|
||||||
|
}
|
||||||
|
}
|
||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Caches white quarter-circle corner textures used to assemble rounded
|
||||||
|
* rectangles through the 26.x GUI blit pipeline.
|
||||||
|
*/
|
||||||
|
final class V26_2ShapeCache {
|
||||||
|
|
||||||
|
private final ClassLoader loader;
|
||||||
|
private final Map<Integer, V26_2GpuTexture> corners =
|
||||||
|
new HashMap<Integer, V26_2GpuTexture>();
|
||||||
|
|
||||||
|
V26_2ShapeCache(ClassLoader loader) {
|
||||||
|
this.loader = loader;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized V26_2GpuTexture corner(int radiusPixels) {
|
||||||
|
int radius = Math.max(2, radiusPixels);
|
||||||
|
V26_2GpuTexture cached = corners.get(radius);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
int size = radius;
|
||||||
|
byte[] rgba = new byte[size * size * 4];
|
||||||
|
for (int y = 0; y < size; y++) {
|
||||||
|
for (int x = 0; x < size; x++) {
|
||||||
|
double dx = radius - (x + 0.5);
|
||||||
|
double dy = radius - (y + 0.5);
|
||||||
|
double distance = Math.sqrt(dx * dx + dy * dy);
|
||||||
|
float coverage = (float) clamp(radius - distance + 0.5, 0.0, 1.0);
|
||||||
|
|
||||||
|
int offset = (y * size + x) * 4;
|
||||||
|
rgba[offset] = (byte) 0xFF;
|
||||||
|
rgba[offset + 1] = (byte) 0xFF;
|
||||||
|
rgba[offset + 2] = (byte) 0xFF;
|
||||||
|
rgba[offset + 3] = (byte) Math.round(coverage * 255.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
V26_2GpuTexture texture = V26_2TextureHelper.create(
|
||||||
|
loader, size, size, rgba);
|
||||||
|
corners.put(radius, texture);
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double clamp(double value, double min, double max) {
|
||||||
|
return Math.max(min, Math.min(max, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@code DynamicTexture} without linking against Mojang's classes.
|
||||||
|
*
|
||||||
|
* <p>Two input formats are supported so callers cannot mix them up: raw RGBA
|
||||||
|
* (shapes, sprites) and one-byte coverage/font atlases, which are expanded to
|
||||||
|
* white RGB + coverage alpha directly inside the {@code NativeImage} buffer.</p>
|
||||||
|
*/
|
||||||
|
final class V26_2TextureHelper {
|
||||||
|
|
||||||
|
static V26_2GpuTexture create(ClassLoader loader, int width, int height,
|
||||||
|
final byte[] rgba) {
|
||||||
|
return create(loader, width, height, new PixelWriter() {
|
||||||
|
@Override
|
||||||
|
public void write(ByteBuffer buffer) {
|
||||||
|
buffer.put(rgba);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static V26_2GpuTexture createFromCoverage(ClassLoader loader, int width,
|
||||||
|
int height, final byte[] coverage) {
|
||||||
|
return create(loader, width, height, new PixelWriter() {
|
||||||
|
@Override
|
||||||
|
public void write(ByteBuffer buffer) {
|
||||||
|
// Expand straight into the NativeImage buffer: avoids a second
|
||||||
|
// full-size RGBA array on top of the atlas and the image.
|
||||||
|
for (byte alpha : coverage) {
|
||||||
|
buffer.put((byte) 0xFF);
|
||||||
|
buffer.put((byte) 0xFF);
|
||||||
|
buffer.put((byte) 0xFF);
|
||||||
|
buffer.put(alpha);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static V26_2GpuTexture create(ClassLoader loader, int width,
|
||||||
|
int height, PixelWriter writer) {
|
||||||
|
try {
|
||||||
|
Class<?> nativeImageClass = Class.forName(
|
||||||
|
"com.mojang.blaze3d.platform.NativeImage", false, loader);
|
||||||
|
Constructor<?> nativeImageConstructor = nativeImageClass
|
||||||
|
.getDeclaredConstructor(int.class, int.class, boolean.class);
|
||||||
|
Object image = nativeImageConstructor.newInstance(width, height, false);
|
||||||
|
|
||||||
|
ByteBuffer buffer = (ByteBuffer) nativeImageClass
|
||||||
|
.getMethod("getPixelBytes").invoke(image);
|
||||||
|
writer.write(buffer);
|
||||||
|
|
||||||
|
Class<?> dynamicTextureClass = Class.forName(
|
||||||
|
"net.minecraft.client.renderer.texture.DynamicTexture", false, loader);
|
||||||
|
Constructor<?> dynamicTextureConstructor = dynamicTextureClass
|
||||||
|
.getDeclaredConstructor(Supplier.class, nativeImageClass);
|
||||||
|
Object texture = dynamicTextureConstructor.newInstance(
|
||||||
|
new Supplier<String>() {
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
return "OrangeX texture";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
image);
|
||||||
|
|
||||||
|
Object textureView = dynamicTextureClass.getMethod("getTextureView")
|
||||||
|
.invoke(texture);
|
||||||
|
Object sampler = dynamicTextureClass.getMethod("getSampler")
|
||||||
|
.invoke(texture);
|
||||||
|
return new V26_2GpuTexture(texture, textureView, sampler, width, height);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new IllegalStateException("Cannot create a 26.2 texture", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface PixelWriter {
|
||||||
|
void write(ByteBuffer buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private V26_2TextureHelper() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
import java.util.IdentityHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Smooths hover and toggle values for ClickGUI elements.
|
||||||
|
*
|
||||||
|
* <p>Keys are compared by identity so equal-but-distinct modules or values do
|
||||||
|
* not share an animation.</p>
|
||||||
|
*/
|
||||||
|
public final class ClickGuiAnimator {
|
||||||
|
|
||||||
|
private static final float HOVER_SPEED = 14.0f;
|
||||||
|
private static final float STATE_SPEED = 16.0f;
|
||||||
|
|
||||||
|
private final Map<Object, Float> hoverAnimations =
|
||||||
|
new IdentityHashMap<Object, Float>();
|
||||||
|
private final Map<Object, Float> stateAnimations =
|
||||||
|
new IdentityHashMap<Object, Float>();
|
||||||
|
|
||||||
|
public float hover(Object key, boolean hovered, float delta) {
|
||||||
|
if (!hoverAnimations.containsKey(key)) {
|
||||||
|
hoverAnimations.put(key, 0.0f);
|
||||||
|
}
|
||||||
|
return animate(hoverAnimations, key, hovered ? 1.0f : 0.0f,
|
||||||
|
delta, HOVER_SPEED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float state(Object key, boolean state, float delta) {
|
||||||
|
return animate(stateAnimations, key, state ? 1.0f : 0.0f,
|
||||||
|
delta, STATE_SPEED);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float animate(Map<Object, Float> animations, Object key,
|
||||||
|
float target, float delta, float speed) {
|
||||||
|
Float stored = animations.get(key);
|
||||||
|
float current = stored == null ? target : stored.floatValue();
|
||||||
|
|
||||||
|
if (Math.abs(target - current) < 0.001f) {
|
||||||
|
animations.put(key, target);
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
float factor = 1.0f - (float) Math.exp(-speed * delta);
|
||||||
|
float next = current + (target - current) * factor;
|
||||||
|
if (Math.abs(target - next) < 0.002f) {
|
||||||
|
next = target;
|
||||||
|
}
|
||||||
|
animations.put(key, next);
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Immutable geometry for one ClickGUI frame.
|
||||||
|
*
|
||||||
|
* <p>The panel keeps its own bounds and category-row height; this class turns
|
||||||
|
* them into the positions and sizes used by the individual renderers.</p>
|
||||||
|
*/
|
||||||
|
public final class ClickGuiLayout {
|
||||||
|
|
||||||
|
private final float x;
|
||||||
|
private final float y;
|
||||||
|
private final float width;
|
||||||
|
private final float height;
|
||||||
|
private final float categoryRowHeight;
|
||||||
|
private final int screenWidth;
|
||||||
|
private final int screenHeight;
|
||||||
|
|
||||||
|
public ClickGuiLayout(float x, float y, float width, float height,
|
||||||
|
float categoryRowHeight, int screenWidth,
|
||||||
|
int screenHeight) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.categoryRowHeight = categoryRowHeight;
|
||||||
|
this.screenWidth = screenWidth;
|
||||||
|
this.screenHeight = screenHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float x() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float y() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float width() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float height() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float categoryRowHeight() {
|
||||||
|
return categoryRowHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int screenWidth() {
|
||||||
|
return screenWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int screenHeight() {
|
||||||
|
return screenHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float contentX() {
|
||||||
|
return x + ClickGuiStyle.SIDEBAR_CARD_X
|
||||||
|
+ ClickGuiStyle.SIDEBAR_WIDTH + 14.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float contentWidth() {
|
||||||
|
return x + width - 12.0f - contentX();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float listTop() {
|
||||||
|
return y + ClickGuiStyle.HEADER_HEIGHT + 38.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float listBottom() {
|
||||||
|
return y + height - 8.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float categoryRowX() {
|
||||||
|
return x + ClickGuiStyle.SIDEBAR_CARD_X + 6.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float categoryRowWidth() {
|
||||||
|
return ClickGuiStyle.SIDEBAR_WIDTH - 12.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float categoryRowTop() {
|
||||||
|
return y + ClickGuiStyle.SIDEBAR_CARD_Y + 9.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleCategory;
|
||||||
|
import cn.xiaozhou233.orangex.value.KeybindValue;
|
||||||
|
import cn.xiaozhou233.orangex.value.Value;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module and category helpers shared by the ClickGUI renderers.
|
||||||
|
*/
|
||||||
|
public final class ClickGuiModules {
|
||||||
|
|
||||||
|
private ClickGuiModules() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Value<?>> settingsOf(Module module) {
|
||||||
|
List<Value<?>> settings = new ArrayList<Value<?>>();
|
||||||
|
for (Value<?> value : module.getVisibleValues()) {
|
||||||
|
// The module keybind is shown on the collapsed row instead.
|
||||||
|
if (!(value instanceof KeybindValue)) {
|
||||||
|
settings.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocation-free variant of {@link #settingsOf(Module)} for rows that only
|
||||||
|
* need to know whether a settings page can be opened.
|
||||||
|
*/
|
||||||
|
public static boolean hasSettings(Module module) {
|
||||||
|
for (Value<?> value : module.getVisibleValues()) {
|
||||||
|
if (!(value instanceof KeybindValue)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String displayName(ModuleCategory category) {
|
||||||
|
String name = category.name();
|
||||||
|
return name.substring(0, 1) + name.substring(1).toLowerCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders and drives the ClickGUI while the empty input screen is open.
|
||||||
|
*
|
||||||
|
* <p>The input screen only records the raw mouse state in
|
||||||
|
* {@link ClientInput}; this class turns button transitions into component
|
||||||
|
* callbacks and then renders {@link MainPanel}.</p>
|
||||||
|
*/
|
||||||
|
public final class ClickGuiRender {
|
||||||
|
|
||||||
|
private static final MainPanel MAIN_PANEL = new MainPanel();
|
||||||
|
|
||||||
|
private static boolean lastLeftDown;
|
||||||
|
private static boolean lastRightDown;
|
||||||
|
|
||||||
|
private ClickGuiRender() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void render(EventRender2D event) {
|
||||||
|
if (!ClientInput.isOpen()) {
|
||||||
|
reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float mouseX = (float) ClientInput.getMouseX();
|
||||||
|
float mouseY = (float) ClientInput.getMouseY();
|
||||||
|
boolean leftDown = ClientInput.isLeftDown();
|
||||||
|
boolean rightDown = ClientInput.isRightDown();
|
||||||
|
|
||||||
|
if (leftDown && !lastLeftDown) {
|
||||||
|
MAIN_PANEL.mouseClicked(mouseX, mouseY, 0);
|
||||||
|
}
|
||||||
|
if (rightDown && !lastRightDown) {
|
||||||
|
MAIN_PANEL.mouseClicked(mouseX, mouseY, 1);
|
||||||
|
}
|
||||||
|
if (leftDown && lastLeftDown) {
|
||||||
|
MAIN_PANEL.mouseDragged(mouseX, mouseY, 0);
|
||||||
|
}
|
||||||
|
if (!leftDown && lastLeftDown) {
|
||||||
|
MAIN_PANEL.mouseReleased(mouseX, mouseY, 0);
|
||||||
|
}
|
||||||
|
if (!rightDown && lastRightDown) {
|
||||||
|
MAIN_PANEL.mouseReleased(mouseX, mouseY, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
MAIN_PANEL.render(event);
|
||||||
|
|
||||||
|
lastLeftDown = leftDown;
|
||||||
|
lastRightDown = rightDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops transient input state. Called when the GUI closes or reopens so a
|
||||||
|
* button held across screens does not produce a stale drag.
|
||||||
|
*/
|
||||||
|
public static void reset() {
|
||||||
|
MAIN_PANEL.resetInteraction();
|
||||||
|
lastLeftDown = false;
|
||||||
|
lastRightDown = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.render.font.FontFamily;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared colours, metrics and drawing primitives for the ClickGUI.
|
||||||
|
*
|
||||||
|
* <p>All colours are ARGB and all coordinates are Minecraft GUI pixels.</p>
|
||||||
|
*/
|
||||||
|
public final class ClickGuiStyle {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Font family used only by the ClickGUI. Other screens keep using the
|
||||||
|
* globally configured default family.
|
||||||
|
*/
|
||||||
|
public static final FontFamily FONT_FAMILY = FontFamily.MONTSERRAT;
|
||||||
|
|
||||||
|
public static final float DESIGN_WIDTH = 448f;
|
||||||
|
public static final float DESIGN_HEIGHT = 306f;
|
||||||
|
public static final float MARGIN = 12f;
|
||||||
|
public static final float PANEL_RADIUS = 9f;
|
||||||
|
|
||||||
|
public static final float HEADER_HEIGHT = 46f;
|
||||||
|
|
||||||
|
public static final float SIDEBAR_WIDTH = 112f;
|
||||||
|
public static final float SIDEBAR_CARD_X = 10f;
|
||||||
|
public static final float SIDEBAR_CARD_Y = HEADER_HEIGHT + 8f;
|
||||||
|
public static final float CATEGORY_HEIGHT = 26f;
|
||||||
|
public static final float CATEGORY_GAP = 2f;
|
||||||
|
|
||||||
|
public static final float MODULE_HEIGHT = 27f;
|
||||||
|
public static final float MODULE_GAP = 3f;
|
||||||
|
|
||||||
|
public static final float TOGGLE_WIDTH = 26f;
|
||||||
|
public static final float TOGGLE_HEIGHT = 12f;
|
||||||
|
public static final float SMALL_TOGGLE_WIDTH = 20f;
|
||||||
|
public static final float SMALL_TOGGLE_HEIGHT = 9f;
|
||||||
|
|
||||||
|
public static final int COLOR_PANEL = 0xF218181D;
|
||||||
|
public static final int COLOR_BORDER = 0xFF2C2C37;
|
||||||
|
public static final int COLOR_TEXT = 0xFFEDEDF2;
|
||||||
|
public static final int COLOR_TEXT_DIM = 0xFF9A9AA8;
|
||||||
|
public static final int COLOR_TEXT_MUTED = 0xFF61616F;
|
||||||
|
public static final int COLOR_ACCENT = 0xFFFF8A3D;
|
||||||
|
public static final int COLOR_ACCENT_RGB = 0x00FF8A3D;
|
||||||
|
public static final int COLOR_DIVIDER = 0xFF272730;
|
||||||
|
public static final int COLOR_SIDEBAR_BG = 0xFF15151A;
|
||||||
|
public static final int COLOR_SIDEBAR_BORDER = 0xFF24242C;
|
||||||
|
public static final int COLOR_SWITCH_OFF = 0xFF34343F;
|
||||||
|
public static final int COLOR_KNOB_OFF = 0xFF9A9AA8;
|
||||||
|
public static final int COLOR_SCROLLBAR = 0x40FFFFFF;
|
||||||
|
|
||||||
|
private ClickGuiStyle() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FontRenderer font(Render2D graphics, float size) {
|
||||||
|
return graphics.getFont(FONT_FAMILY, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FontRenderer font(Render2D graphics, float size, boolean bold) {
|
||||||
|
return graphics.getFont(FONT_FAMILY, size, bold);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawSwitch(Render2D graphics, float switchX, float switchY,
|
||||||
|
float switchWidth, float switchHeight,
|
||||||
|
float progress) {
|
||||||
|
int track = lerpColor(COLOR_SWITCH_OFF, COLOR_ACCENT, progress);
|
||||||
|
graphics.fillRoundedRect(switchX, switchY, switchWidth, switchHeight,
|
||||||
|
switchHeight / 2.0f, track);
|
||||||
|
|
||||||
|
float knobSize = switchHeight - 4.0f;
|
||||||
|
float knobX = switchX + 2.0f
|
||||||
|
+ (switchWidth - knobSize - 4.0f) * progress;
|
||||||
|
int knob = lerpColor(COLOR_KNOB_OFF, 0xFFFFFFFF, progress);
|
||||||
|
graphics.fillRoundedRect(knobX, switchY + 2.0f, knobSize, knobSize,
|
||||||
|
knobSize / 2.0f, knob);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double centeredY(FontRenderer font, float top, float height) {
|
||||||
|
return top + (height - font.getStringHeight()) / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws a thin scrollbar for a scrollable list. Nothing is drawn while the
|
||||||
|
* content fits, so short lists look unchanged.
|
||||||
|
*/
|
||||||
|
public static void drawScrollbar(Render2D graphics, double x, double top,
|
||||||
|
double viewHeight, double contentHeight,
|
||||||
|
double scroll) {
|
||||||
|
if (contentHeight <= viewHeight + 0.5) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double thumbHeight = Math.max(18.0,
|
||||||
|
viewHeight * (viewHeight / contentHeight));
|
||||||
|
double maxScroll = contentHeight - viewHeight;
|
||||||
|
double progress = maxScroll <= 0.0 ? 0.0
|
||||||
|
: clamp((float) (scroll / maxScroll), 0.0f, 1.0f);
|
||||||
|
double thumbY = top + (viewHeight - thumbHeight) * progress;
|
||||||
|
|
||||||
|
graphics.fillRoundedRect(x, thumbY, 2.0, thumbHeight, 1.0,
|
||||||
|
COLOR_SCROLLBAR);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isInside(double mouseX, double mouseY,
|
||||||
|
double x, double y,
|
||||||
|
double width, double height) {
|
||||||
|
return mouseX >= x && mouseX <= x + width
|
||||||
|
&& mouseY >= y && mouseY <= y + height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float clamp(float value, float min, float max) {
|
||||||
|
return Math.max(min, Math.min(max, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int argb(int alpha, int rgb) {
|
||||||
|
int clamped = Math.max(0, Math.min(255, alpha));
|
||||||
|
return (clamped << 24) | (rgb & 0x00FFFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int lerpColor(int from, int to, float progress) {
|
||||||
|
float t = clamp(progress, 0.0f, 1.0f);
|
||||||
|
int a = lerpChannel(from >>> 24, to >>> 24, t);
|
||||||
|
int r = lerpChannel(from >> 16, to >> 16, t);
|
||||||
|
int g = lerpChannel(from >> 8, to >> 8, t);
|
||||||
|
int b = lerpChannel(from, to, t);
|
||||||
|
return (a << 24) | (r << 16) | (g << 8) | b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int lerpChannel(int from, int to, float t) {
|
||||||
|
int fromChannel = from & 0xFF;
|
||||||
|
int toChannel = to & 0xFF;
|
||||||
|
return Math.round(fromChannel + (toChannel - fromChannel) * t);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Row geometry for the module list.
|
||||||
|
*
|
||||||
|
* <p>Module rows are evenly sized, so a row's position is just its index times
|
||||||
|
* the row stride minus the scroll offset. The renderer and the input code share
|
||||||
|
* this class so both always agree on where a row is.</p>
|
||||||
|
*/
|
||||||
|
public final class ContentList {
|
||||||
|
|
||||||
|
private final ClickGuiLayout layout;
|
||||||
|
private final List<Module> modules;
|
||||||
|
|
||||||
|
private float scroll;
|
||||||
|
|
||||||
|
public ContentList(ClickGuiLayout layout, List<Module> modules,
|
||||||
|
float scroll) {
|
||||||
|
this.layout = layout;
|
||||||
|
this.modules = modules;
|
||||||
|
this.scroll = scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Module> modules() {
|
||||||
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float scroll() {
|
||||||
|
return scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scrollBy(float amount) {
|
||||||
|
scroll += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clamps the scroll offset to the current content, so switching category
|
||||||
|
* never leaves the list overscrolled.
|
||||||
|
*/
|
||||||
|
public void clampScroll() {
|
||||||
|
scroll = ClickGuiStyle.clamp(scroll, 0.0f, maxScroll());
|
||||||
|
}
|
||||||
|
|
||||||
|
public float rowStride() {
|
||||||
|
return ClickGuiStyle.MODULE_HEIGHT + ClickGuiStyle.MODULE_GAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float rowTop(int index) {
|
||||||
|
return layout.listTop() + index * rowStride() - scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float contentHeight() {
|
||||||
|
return modules.isEmpty() ? 0.0f
|
||||||
|
: modules.size() * rowStride() - ClickGuiStyle.MODULE_GAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float maxScroll() {
|
||||||
|
return Math.max(0.0f,
|
||||||
|
contentHeight() - (layout.listBottom() - layout.listTop()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the index of the module row under {@code mouseY}, or {@code -1}.
|
||||||
|
*/
|
||||||
|
public int moduleIndexAt(float mouseY) {
|
||||||
|
for (int i = 0; i < modules.size(); i++) {
|
||||||
|
float top = rowTop(i);
|
||||||
|
if (mouseY >= top && mouseY <= top + ClickGuiStyle.MODULE_HEIGHT) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleCategory;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.FontRenderer;
|
||||||
|
import cn.xiaozhou233.orangex.render.api.Render2D;
|
||||||
|
import cn.xiaozhou233.orangex.value.KeybindValue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the module list for the selected category.
|
||||||
|
*
|
||||||
|
* <p>Right-clicking a module opens its settings page in this same content area
|
||||||
|
* (see {@link SettingsSection}). The list scrolls with the wheel and shows a
|
||||||
|
* scrollbar when it does not fit.</p>
|
||||||
|
*/
|
||||||
|
public final class ContentRenderer {
|
||||||
|
|
||||||
|
private ContentRenderer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void draw(Render2D graphics, ClickGuiLayout layout,
|
||||||
|
ContentList rows, ModuleCategory category,
|
||||||
|
ClickGuiAnimator animator, float delta,
|
||||||
|
double mouseX, double mouseY) {
|
||||||
|
float y = layout.y();
|
||||||
|
float contentX = layout.contentX();
|
||||||
|
float contentWidth = layout.contentWidth();
|
||||||
|
List<Module> modules = rows.modules();
|
||||||
|
|
||||||
|
FontRenderer title = ClickGuiStyle.font(graphics, 13.0f, true);
|
||||||
|
title.drawString(ClickGuiModules.displayName(category), contentX,
|
||||||
|
y + ClickGuiStyle.HEADER_HEIGHT + 10,
|
||||||
|
ClickGuiStyle.COLOR_TEXT);
|
||||||
|
|
||||||
|
FontRenderer meta = ClickGuiStyle.font(graphics, 8.0f);
|
||||||
|
String metaText = modules.size()
|
||||||
|
+ (modules.size() == 1 ? " MODULE" : " MODULES");
|
||||||
|
meta.drawString(metaText,
|
||||||
|
contentX + contentWidth - meta.getStringWidth(metaText),
|
||||||
|
y + ClickGuiStyle.HEADER_HEIGHT + 14,
|
||||||
|
ClickGuiStyle.COLOR_TEXT_MUTED);
|
||||||
|
|
||||||
|
graphics.fillRect(contentX, y + ClickGuiStyle.HEADER_HEIGHT + 30,
|
||||||
|
contentWidth, 1, ClickGuiStyle.COLOR_DIVIDER);
|
||||||
|
|
||||||
|
float listTop = layout.listTop();
|
||||||
|
float listBottom = layout.listBottom();
|
||||||
|
|
||||||
|
graphics.enableScissor(contentX - 2, listTop - 2,
|
||||||
|
contentWidth + 4, listBottom - listTop + 4);
|
||||||
|
|
||||||
|
if (modules.isEmpty()) {
|
||||||
|
FontRenderer empty = ClickGuiStyle.font(graphics, 9.0f);
|
||||||
|
String text = "No modules here yet";
|
||||||
|
empty.drawString(text,
|
||||||
|
contentX + (contentWidth - empty.getStringWidth(text)) / 2.0,
|
||||||
|
listTop + 14, ClickGuiStyle.COLOR_TEXT_MUTED);
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < modules.size(); i++) {
|
||||||
|
Module module = modules.get(i);
|
||||||
|
float rowY = rows.rowTop(i);
|
||||||
|
|
||||||
|
if (rowY > listBottom
|
||||||
|
|| rowY + ClickGuiStyle.MODULE_HEIGHT < listTop) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hovered = mouseY >= listTop && mouseY <= listBottom
|
||||||
|
&& ClickGuiStyle.isInside(mouseX, mouseY, contentX,
|
||||||
|
rowY, contentWidth, ClickGuiStyle.MODULE_HEIGHT);
|
||||||
|
float hover = animator.hover(module, hovered, delta);
|
||||||
|
|
||||||
|
drawModuleRow(graphics, layout, module, rowY, hover, animator,
|
||||||
|
delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
graphics.disableScissor();
|
||||||
|
|
||||||
|
ClickGuiStyle.drawScrollbar(graphics, contentX + contentWidth - 3,
|
||||||
|
listTop, listBottom - listTop, rows.contentHeight(),
|
||||||
|
rows.scroll());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void drawModuleRow(Render2D graphics, ClickGuiLayout layout,
|
||||||
|
Module module, float rowY, float hover,
|
||||||
|
ClickGuiAnimator animator, float delta) {
|
||||||
|
float contentX = layout.contentX();
|
||||||
|
float contentWidth = layout.contentWidth();
|
||||||
|
float rowRight = contentX + contentWidth;
|
||||||
|
float moduleHeight = ClickGuiStyle.MODULE_HEIGHT;
|
||||||
|
|
||||||
|
int background = module.isEnabled()
|
||||||
|
? ClickGuiStyle.argb((int) (14.0f + 14.0f * hover),
|
||||||
|
ClickGuiStyle.COLOR_ACCENT_RGB)
|
||||||
|
: ClickGuiStyle.argb((int) (6.0f + 10.0f * hover),
|
||||||
|
0x00FFFFFF);
|
||||||
|
graphics.fillRoundedRect(contentX, rowY, contentWidth, moduleHeight,
|
||||||
|
5, background);
|
||||||
|
|
||||||
|
if (module.isEnabled()) {
|
||||||
|
graphics.fillRoundedRect(contentX + 2, rowY + 8, 2.0f,
|
||||||
|
moduleHeight - 16, 1.0f, ClickGuiStyle.COLOR_ACCENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
FontRenderer name = ClickGuiStyle.font(graphics, 10.0f,
|
||||||
|
module.isEnabled());
|
||||||
|
name.drawString(module.getName(), contentX + 10,
|
||||||
|
ClickGuiStyle.centeredY(name, rowY, moduleHeight),
|
||||||
|
module.isEnabled() ? ClickGuiStyle.COLOR_TEXT
|
||||||
|
: ClickGuiStyle.COLOR_TEXT_DIM);
|
||||||
|
|
||||||
|
KeybindValue keybind = module.getKeybind();
|
||||||
|
if (keybind.isBound()) {
|
||||||
|
FontRenderer keyFont = ClickGuiStyle.font(graphics, 8.0f);
|
||||||
|
String key = KeyNames.keyName(keybind.getKey());
|
||||||
|
keyFont.drawString(key,
|
||||||
|
rowRight - ClickGuiStyle.TOGGLE_WIDTH - 18
|
||||||
|
- keyFont.getStringWidth(key),
|
||||||
|
ClickGuiStyle.centeredY(keyFont, rowY, moduleHeight),
|
||||||
|
ClickGuiStyle.COLOR_TEXT_MUTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
float toggleX = rowRight - ClickGuiStyle.TOGGLE_WIDTH - 10;
|
||||||
|
float toggleY = rowY + (moduleHeight - ClickGuiStyle.TOGGLE_HEIGHT)
|
||||||
|
/ 2.0f;
|
||||||
|
ClickGuiStyle.drawSwitch(graphics, toggleX, toggleY,
|
||||||
|
ClickGuiStyle.TOGGLE_WIDTH, ClickGuiStyle.TOGGLE_HEIGHT,
|
||||||
|
animator.state(module, module.isEnabled(), delta));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loaded 100 of 163 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user