Skip to content

Commit

Permalink
Core Editor: Add basic decoding for KJPXPath
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 3, 2024
1 parent c85e235 commit db351e4
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.shade.decima.ui.data.handlers;

import com.shade.decima.model.rtti.RTTIType;
import com.shade.decima.model.rtti.objects.RTTIObject;
import com.shade.decima.model.util.hash.CRC32C;
import com.shade.decima.ui.data.registry.ValueHandlerRegistration;
import com.shade.decima.ui.data.registry.ValueHandlerRegistration.Selector;
import com.shade.decima.ui.data.registry.ValueHandlerRegistration.Type;
import com.shade.platform.model.util.IOUtils;
import com.shade.platform.ui.controls.TextAttributes;
import com.shade.util.NotNull;

import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@ValueHandlerRegistration(id = "KJPXPath", name = "KJP XPath", value = {
@Selector(type = @Type(name = "KJPXPath"))
})
public class KJPXPathValueHandler extends ObjectValueHandler {
private static final Map<Integer, String> LOOKUP;

static {
final String[] knownNames = {
"continue",
"new_game",
"exit_game",
"game_over",
"update_info",
"can_restart",
"difficulty_select_title",
"very_easy",
"easy",
"normal",
"hard",
"very_hard",
"month",
"date",
"visible",
"text()",
"bool()",
"!bool()",
"int()",
"float()",
"object()",
"isNull()",
"!isNull()",
"count()",
"index()",
"index1()",
"exist()",
"!exist()",
"isEmpty()",
"!isEmpty()",
};

LOOKUP = Stream.of(knownNames).collect(Collectors.toMap(
name -> CRC32C.calculate(name.getBytes(StandardCharsets.UTF_8)),
Function.identity()
));
}

@NotNull
@Override
public Decorator getDecorator(@NotNull RTTIType<?> type) {
return (value, component) -> component.append(getText(type, value), TextAttributes.REGULAR_ATTRIBUTES);
}

@NotNull
@Override
public String getText(@NotNull RTTIType<?> type, @NotNull Object value) {
final RTTIObject object = (RTTIObject) value;
final StringJoiner buffer = new StringJoiner("/");
final int length = (int) object.i64("Union7");

for (int i = 0; i < length; i++) {
buffer.add(getElementName(object.i64("Union%d".formatted(i))));
}

return buffer.toString();
}

@NotNull
private static String getElementName(long element) {
final String name = LOOKUP.get((int) element);

if (name != null) {
return name;
} else {
return IOUtils.toHexDigits((int) element, ByteOrder.BIG_ENDIAN);
}
}
}

0 comments on commit db351e4

Please sign in to comment.