added files.
This commit is contained in:
commit
e908ab2355
353 files changed
+130515
No files matched your search
@@ -0,0 +1,67 @@
|
||||
package dev.slinky.client.gui.profile;
|
||||
|
||||
import dev.slinky.client.render.DrawPlan;
|
||||
import dev.slinky.client.render.RenderMath;
|
||||
import dev.slinky.client.render.font.CanvasFonts;
|
||||
import dev.slinky.inject.Platform;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class ProfileIcons {
|
||||
private static final byte[] COVERAGE = coverage();
|
||||
private static final int TEXTURE = CanvasFonts.allocateTextureIdentity();
|
||||
|
||||
private static byte[] coverage() {
|
||||
byte[] result = new byte[6400];
|
||||
|
||||
try (InputStream input = Platform.resource("/assets/slinkysemantic/profile-icon-coverage.bin")) {
|
||||
if (input == null) {
|
||||
throw new IOException("Profile icon coverage is missing");
|
||||
} else {
|
||||
int position = 0;
|
||||
|
||||
while (position < result.length) {
|
||||
int count = input.read(result, position, result.length - position);
|
||||
if (count < 0) {
|
||||
throw new IOException("Incomplete profile icon atlas");
|
||||
}
|
||||
|
||||
position += count;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
} catch (IOException var15) {
|
||||
throw new IllegalStateException("Cannot load profile icons", var15);
|
||||
}
|
||||
}
|
||||
|
||||
public static void draw(DrawPlan plan, RenderMath.Rect button, int action, boolean enabled, boolean hover) {
|
||||
float u0 = (float)action / 4.0F;
|
||||
float u1 = (float)(action + 1) / 4.0F;
|
||||
float x = button.x;
|
||||
float y = button.y;
|
||||
float right = x + button.width;
|
||||
float bottom = y + button.height;
|
||||
int color = action == 3 ? (!enabled ? -8901331 : (hover ? -1089697 : -2275513)) : (!enabled ? -11119018 : (hover ? -2368549 : -6710887));
|
||||
plan.alphaSprite(
|
||||
TEXTURE,
|
||||
160,
|
||||
40,
|
||||
COVERAGE,
|
||||
Arrays.asList(
|
||||
new float[]{x, y, u0, 0.0F},
|
||||
new float[]{right, bottom, u1, 1.0F},
|
||||
new float[]{right, y, u1, 0.0F},
|
||||
new float[]{x, y, u0, 0.0F},
|
||||
new float[]{x, bottom, u0, 1.0F},
|
||||
new float[]{right, bottom, u1, 1.0F}
|
||||
),
|
||||
color
|
||||
);
|
||||
}
|
||||
|
||||
private ProfileIcons() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package dev.slinky.client.gui.profile;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import dev.slinky.client.numeric.FixedWidth;
|
||||
import dev.slinky.inject.Platform;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public final class ProfileMath {
|
||||
private static final Map<Integer, byte[]> LABELS = labels();
|
||||
private static final ProfileMath.Formatter JAVA_FORMAT = (pattern, value, capacity) -> {
|
||||
String format = new String(pattern, StandardCharsets.UTF_8).replace("%lld", "%d");
|
||||
byte[] result = String.format(Locale.ROOT, format, value).getBytes(StandardCharsets.UTF_8);
|
||||
return Arrays.copyOf(result, Math.min(result.length, capacity - 1));
|
||||
};
|
||||
|
||||
private ProfileMath() {
|
||||
}
|
||||
|
||||
private static Map<Integer, byte[]> labels() {
|
||||
Map<Integer, byte[]> result = new HashMap<>();
|
||||
InputStream stream = Platform.resource("/assets/slinkysemantic/ProfileLabels.json");
|
||||
if (stream == null) {
|
||||
throw new IllegalStateException("Missing immutable profile labels");
|
||||
} else {
|
||||
try (Reader reader = new InputStreamReader(stream, "UTF-8")) {
|
||||
for (JsonElement element : new JsonParser().parse(reader).getAsJsonArray()) {
|
||||
JsonObject row = element.getAsJsonObject();
|
||||
String hex = row.get("bytes").getAsString();
|
||||
byte[] bytes = new byte[hex.length() / 2];
|
||||
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
|
||||
}
|
||||
|
||||
result.put(row.get("rva").getAsInt(), bytes);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (IOException var20) {
|
||||
throw new ExceptionInInitializerError(var20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static long ageSeconds(long nowFileTime, long modifiedFileTime) {
|
||||
return Long.compareUnsigned(modifiedFileTime - 1L, nowFileTime) >= 0
|
||||
? -1L
|
||||
: FixedWidth.unsignedMultiplyHigh(nowFileTime - modifiedFileTime, -2972493582642298179L) >>> 23;
|
||||
}
|
||||
|
||||
public static long days(long ageSeconds) {
|
||||
long high = FixedWidth.signedMultiplyHigh(ageSeconds, 1749024623285053783L);
|
||||
return (high >> 13) + (high >>> 63);
|
||||
}
|
||||
|
||||
public static byte[] dateBytes(long ageSeconds, ProfileMath.Formatter formatter) {
|
||||
byte[] buffer = new byte[32];
|
||||
if (ageSeconds == -1L) {
|
||||
System.arraycopy(LABELS.get(1310590), 0, buffer, 0, 16);
|
||||
byte[] end = new byte[]{116, 105, 111, 110, 32, 100, 97, 116};
|
||||
System.arraycopy(end, 0, buffer, 16, end.length);
|
||||
} else {
|
||||
long day = days(ageSeconds);
|
||||
if (day == 1L) {
|
||||
System.arraycopy(LABELS.get(1310641), 0, buffer, 0, 16);
|
||||
buffer[16] = 103;
|
||||
buffer[17] = 111;
|
||||
buffer[18] = 46;
|
||||
} else if (day == 0L) {
|
||||
System.arraycopy(LABELS.get(1310665), 0, buffer, 0, 16);
|
||||
} else {
|
||||
byte[] text = formatter.format(cString(LABELS.get(1310617)), day, 25);
|
||||
System.arraycopy(text, 0, buffer, 0, Math.min(text.length, 24));
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static String date(long ageSeconds) {
|
||||
return new String(cString(dateBytes(ageSeconds, JAVA_FORMAT)), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte[] cString(byte[] text) {
|
||||
int length = 0;
|
||||
|
||||
while (length < text.length && text[length] != 0) {
|
||||
length++;
|
||||
}
|
||||
|
||||
return Arrays.copyOf(text, length);
|
||||
}
|
||||
|
||||
public static int compareNames(String left, String right) {
|
||||
byte[] a = left.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] b = right.getBytes(StandardCharsets.UTF_8);
|
||||
int i = 0;
|
||||
|
||||
while (true) {
|
||||
int x = i < a.length ? a[i] & 255 : 0;
|
||||
int y = i < b.length ? b[i] & 255 : 0;
|
||||
if (x != y) {
|
||||
return x - y;
|
||||
}
|
||||
|
||||
if (x == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Formatter {
|
||||
byte[] format(byte[] var1, long var2, int var4);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
package dev.slinky.client.gui.profile;
|
||||
|
||||
import dev.slinky.client.render.RenderConstants;
|
||||
import dev.slinky.config.ConfigStore;
|
||||
import dev.slinky.config.ConfigurationFiles;
|
||||
import dev.slinky.config.ModuleConfiguration;
|
||||
import dev.slinky.module.ModuleManager;
|
||||
import java.awt.Desktop;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.DirectoryIteratorException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
public final class ProfileServices {
|
||||
private ProfileServices() {
|
||||
}
|
||||
|
||||
public interface DirectoryScanner {
|
||||
void scan(Path var1, LongSupplier var2, ProfileServices.RecordReceiver var3) throws IOException;
|
||||
}
|
||||
|
||||
public static final class Failure extends IOException {
|
||||
public final int code;
|
||||
|
||||
public Failure(int code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class FilesHost implements ProfileServices.Host {
|
||||
private static final long FILETIME_EPOCH = 116444736000000000L;
|
||||
private final ModuleManager manager;
|
||||
private final Path directory;
|
||||
private final ConfigStore configuration;
|
||||
private final LongSupplier fileTime;
|
||||
private final ProfileServices.DirectoryScanner scanner;
|
||||
private List<ProfileServices.Record> rows = Collections.emptyList();
|
||||
|
||||
public FilesHost(ModuleManager manager, Path directory, ConfigStore configuration) {
|
||||
this(manager, directory, configuration, () -> System.currentTimeMillis() * 10000L + 116444736000000000L);
|
||||
}
|
||||
|
||||
public FilesHost(ModuleManager manager, Path directory, ConfigStore configuration, LongSupplier fileTime) {
|
||||
this(manager, directory, configuration, fileTime, ProfileServices.FilesHost::scanDirectory);
|
||||
}
|
||||
|
||||
public FilesHost(ModuleManager manager, Path directory, ConfigStore configuration, LongSupplier fileTime, ProfileServices.DirectoryScanner scanner) {
|
||||
this.manager = Objects.requireNonNull(manager);
|
||||
this.directory = directory.toAbsolutePath().normalize();
|
||||
this.configuration = Objects.requireNonNull(configuration);
|
||||
this.fileTime = Objects.requireNonNull(fileTime);
|
||||
this.scanner = Objects.requireNonNull(scanner);
|
||||
}
|
||||
|
||||
private Path path(String name) throws IOException {
|
||||
if (name != null && !name.isEmpty()) {
|
||||
try {
|
||||
Path target = this.directory.resolve(name).normalize();
|
||||
if (!this.directory.equals(target.getParent())) {
|
||||
throw new ProfileServices.Failure(1012, "Invalid profile name");
|
||||
} else {
|
||||
return target;
|
||||
}
|
||||
} catch (InvalidPathException var3) {
|
||||
throw new ProfileServices.Failure(1006, "Invalid profile filename");
|
||||
}
|
||||
} else {
|
||||
throw new ProfileServices.Failure(1012, "A profile name is required");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<ProfileServices.Record> records() throws IOException {
|
||||
Map<String, Integer> retained = new TreeMap<>(ProfileMath::compareNames);
|
||||
|
||||
for (ModuleConfiguration.ProfileBinding binding : this.configuration.profileBindings()) {
|
||||
if (binding.binding != 0 && !retained.containsKey(binding.name)) {
|
||||
retained.put(binding.name, binding.binding);
|
||||
}
|
||||
}
|
||||
|
||||
for (ProfileServices.Record row : this.rows) {
|
||||
if (row.binding != 0) {
|
||||
retained.put(row.name, row.binding);
|
||||
}
|
||||
}
|
||||
|
||||
List<ProfileServices.Record> next = new ArrayList<>();
|
||||
this.rows = next;
|
||||
|
||||
try {
|
||||
ensure(this.directory.getParent());
|
||||
ensure(this.directory);
|
||||
this.scanner.scan(this.directory, this.fileTime, (name, age) -> {
|
||||
if (name.getBytes(StandardCharsets.UTF_8).length >= 260) {
|
||||
throw new ProfileServices.Failure(1006, "Profile filename exceeds the narrow host buffer");
|
||||
} else {
|
||||
Integer bindingx = retained.get(name);
|
||||
next.add(new ProfileServices.Record(name, age, bindingx == null ? 0 : bindingx));
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
next.sort((left, right) -> {
|
||||
int order = Long.compare(left.ageSeconds, right.ageSeconds);
|
||||
return order != 0 ? order : ProfileMath.compareNames(left.name, right.name);
|
||||
});
|
||||
this.saveBindings();
|
||||
}
|
||||
|
||||
return this.snapshot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<ProfileServices.Record> snapshot() {
|
||||
return Collections.unmodifiableList(new ArrayList<>(this.rows));
|
||||
}
|
||||
|
||||
private static void ensure(Path directory) throws IOException {
|
||||
ConfigurationFiles.WidePath path = new ConfigurationFiles.WidePath();
|
||||
path.text = ConfigurationFiles.wide(directory.toString());
|
||||
|
||||
try (ConfigurationFiles.NioHost host = new ConfigurationFiles.NioHost(null)) {
|
||||
int code = ConfigurationFiles.ensureDirectory(path, host);
|
||||
if (code != 0) {
|
||||
throw new ProfileServices.Failure(code, "Could not create profile directory");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void scanDirectory(Path directory, LongSupplier fileTime, ProfileServices.RecordReceiver receiver) throws IOException {
|
||||
DirectoryStream<Path> files;
|
||||
try {
|
||||
files = Files.newDirectoryStream(directory);
|
||||
} catch (NoSuchFileException var22) {
|
||||
return;
|
||||
} catch (IOException var23) {
|
||||
throw new ProfileServices.Failure(1013, "Could not begin profile enumeration");
|
||||
}
|
||||
|
||||
try {
|
||||
DirectoryStream<Path> opened = files;
|
||||
Throwable var5 = null;
|
||||
|
||||
try {
|
||||
long now = fileTime.getAsLong();
|
||||
|
||||
for (Path file : opened) {
|
||||
if (!Files.isDirectory(file)) {
|
||||
long modified = Files.getLastModifiedTime(file).toMillis() * 10000L + 116444736000000000L;
|
||||
receiver.record(file.getFileName().toString(), ProfileMath.ageSeconds(now, modified));
|
||||
}
|
||||
}
|
||||
} catch (Throwable var24) {
|
||||
var5 = var24;
|
||||
throw var24;
|
||||
} finally {
|
||||
if (files != null) {
|
||||
if (var5 != null) {
|
||||
try {
|
||||
opened.close();
|
||||
} catch (Throwable var21) {
|
||||
var5.addSuppressed(var21);
|
||||
}
|
||||
} else {
|
||||
files.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (DirectoryIteratorException var26) {
|
||||
throw new ProfileServices.Failure(1015, "Could not continue profile enumeration");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(String name) throws IOException {
|
||||
if (name == null) {
|
||||
ModuleConfiguration.resetProfile(this.manager);
|
||||
} else {
|
||||
ensure(this.directory.getParent());
|
||||
ensure(this.directory);
|
||||
Path target = this.path(name);
|
||||
if (!Files.isRegularFile(target)) {
|
||||
throw new ProfileServices.Failure(1008, "Profile does not exist");
|
||||
}
|
||||
|
||||
try {
|
||||
new ConfigStore(target).loadProfile(this.manager);
|
||||
} catch (ConfigurationFiles.Failure var4) {
|
||||
throw new ProfileServices.Failure(var4.code, var4.getMessage());
|
||||
} catch (IOException var5) {
|
||||
throw new ProfileServices.Failure(5001, var5.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
this.manager.configurationSession().profileApplied(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(String name, boolean overwrite) throws IOException {
|
||||
Path target = this.path(name);
|
||||
|
||||
try {
|
||||
new ConfigStore(target).saveProfile(this.manager, overwrite);
|
||||
} catch (ConfigurationFiles.Failure var5) {
|
||||
throw new ProfileServices.Failure(var5.code, var5.getMessage());
|
||||
}
|
||||
|
||||
this.manager.configurationSession().profileApplied(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String name) throws IOException {
|
||||
if (name == null) {
|
||||
throw new ProfileServices.Failure(6004, RenderConstants.text(1313637));
|
||||
} else {
|
||||
try {
|
||||
Files.delete(this.path(name));
|
||||
} catch (IOException var3) {
|
||||
throw new ProfileServices.Failure(1011, "Could not delete profile");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void explore() throws IOException {
|
||||
if (!Desktop.isDesktopSupported()) {
|
||||
throw new ProfileServices.Failure(4000, "Directory opener unavailable");
|
||||
} else {
|
||||
Desktop.getDesktop().open(this.directory.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveConfiguration() throws IOException {
|
||||
try {
|
||||
this.configuration.save(this.manager);
|
||||
} catch (ConfigurationFiles.Failure var2) {
|
||||
throw new ProfileServices.Failure(var2.code, var2.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void bind(String name, int key) {
|
||||
if (key >= 0 && key <= 103) {
|
||||
ProfileServices.Record found = null;
|
||||
|
||||
for (ProfileServices.Record row : this.rows) {
|
||||
if (ProfileMath.compareNames(row.name, name) == 0) {
|
||||
found = row;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == null) {
|
||||
throw new IllegalArgumentException("Profile is absent from the current directory records");
|
||||
} else {
|
||||
found.binding = key;
|
||||
this.saveBindings();
|
||||
this.manager.persist();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Binding ID outside the recovered key table");
|
||||
}
|
||||
}
|
||||
|
||||
private void saveBindings() {
|
||||
List<ModuleConfiguration.ProfileBinding> bindings = new ArrayList<>();
|
||||
|
||||
for (ProfileServices.Record row : this.rows) {
|
||||
if (row.binding != 0) {
|
||||
bindings.add(new ModuleConfiguration.ProfileBinding(row.name, row.binding));
|
||||
}
|
||||
}
|
||||
|
||||
this.configuration.profileBindings(bindings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String current() {
|
||||
return this.manager.configurationSession().currentProfile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pending() {
|
||||
return this.manager.configurationSession().pending();
|
||||
}
|
||||
}
|
||||
|
||||
public interface Host {
|
||||
List<ProfileServices.Record> records() throws IOException;
|
||||
|
||||
default List<ProfileServices.Record> snapshot() {
|
||||
throw new UnsupportedOperationException("Profile snapshot service is required after refresh failure");
|
||||
}
|
||||
|
||||
void load(String var1) throws IOException;
|
||||
|
||||
void save(String var1, boolean var2) throws IOException;
|
||||
|
||||
void delete(String var1) throws IOException;
|
||||
|
||||
void explore() throws IOException;
|
||||
|
||||
void saveConfiguration() throws IOException;
|
||||
|
||||
void bind(String var1, int var2);
|
||||
|
||||
String current();
|
||||
|
||||
boolean pending();
|
||||
}
|
||||
|
||||
public static final class Record {
|
||||
public final String name;
|
||||
public final String date;
|
||||
public final long ageSeconds;
|
||||
public int binding;
|
||||
|
||||
public Record(String name, long ageSeconds, int binding) {
|
||||
this.name = name;
|
||||
this.ageSeconds = ageSeconds;
|
||||
this.date = ProfileMath.date(ageSeconds);
|
||||
this.binding = binding;
|
||||
}
|
||||
}
|
||||
|
||||
public interface RecordReceiver {
|
||||
void record(String var1, long var2) throws IOException;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user