Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyncBromaScript member fixes #727

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2c56f33
first moves
hiimjustin000 Aug 24, 2024
256a63b
i think i fixed padding but structures are still broken
hiimjustin000 Aug 24, 2024
4464c38
okay
hiimjustin000 Aug 24, 2024
d8518fa
fix structures
hiimjustin000 Aug 26, 2024
1333a06
!!!
hiimjustin000 Aug 26, 2024
dbd134f
unused data type
hiimjustin000 Aug 26, 2024
02af3c2
okay let's try arrays
hiimjustin000 Aug 27, 2024
bb38a26
Merge branch 'main' of https://github.com/geode-sdk/bindings into poi…
hiimjustin000 Aug 27, 2024
db82a95
oh my goodness gracious
hiimjustin000 Aug 28, 2024
a28f8dc
I think I should do this but IDK
hiimjustin000 Aug 29, 2024
61e63d0
Merge branch 'main' of https://github.com/geode-sdk/bindings into poi…
hiimjustin000 Aug 29, 2024
732b091
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Aug 30, 2024
0011d1a
final commit maybe (fix non-windows)
hiimjustin000 Aug 30, 2024
bf53b07
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Aug 30, 2024
925b22f
a
hiimjustin000 Aug 30, 2024
4c8d118
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Sep 2, 2024
959457b
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Sep 6, 2024
8469d70
gjbgl fix (increase depth limit)
hiimjustin000 Sep 6, 2024
b431476
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Sep 13, 2024
db2d9a7
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Sep 21, 2024
0eedc32
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Oct 1, 2024
fc05804
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Oct 1, 2024
e82ad75
Merge branch 'geode-sdk:main' into pointer-fix
hiimjustin000 Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/ghidra/Broma.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private Member(Broma broma, Class parent, Platform platform, Matcher matcher) {
var addrMatcher = broma.forkMatcher(Regexes.GRAB_PLATFORM_ADDRESS, matcher, "platforms", false);
while (addrMatcher.find()) {
mutPaddings.put(
Platform.fromShortName(addrMatcher.group("platform")),
Platform.fromShortName(addrMatcher.group("platform"), platform),
Integer.parseInt(addrMatcher.group("addr"), 16)
);
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/ghidra/PaddingInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public PaddingInfo(Map<Platform, Integer> platforms, int offset) {
this.offset = offset;
}

public PaddingInfo(String comment) {
public PaddingInfo(String comment, Platform curPlatform) {
if (comment == null || comment.isEmpty()) {
this.platforms = Map.of();
this.offset = -1;
Expand All @@ -35,7 +35,7 @@ public PaddingInfo(String comment) {
offset = addr;
}
else {
this.platforms.put(Platform.fromShortName(matcher.group("platform")), addr);
this.platforms.put(Platform.fromShortName(matcher.group("platform"), curPlatform), addr);
}
}
this.offset = offset;
Expand Down
9 changes: 6 additions & 3 deletions scripts/ghidra/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ private Platform(String longName, String shortName, boolean singleBinary, String
this.groupName = groupName;
}

public static Platform fromShortName(String shortName) {
public static Platform fromShortName(String shortName, Platform curPlatform) {
for (var v : Platform.values()) {
if (v.respondsToName(shortName)) {
if (v.respondsToName(shortName, curPlatform)) {
return v;
}
}
Expand All @@ -55,8 +55,11 @@ public String getGroupName() {
public boolean hasSingleBinary() {
return this.singleBinary;
}
public boolean respondsToName(String shortName) {
public boolean respondsToName(String shortName, Platform curPlatform) {
if (this.shortName.equals(shortName)) {
if ((this == Platform.WINDOWS32 || this == Platform.WINDOWS64) && (curPlatform == Platform.WINDOWS32 || curPlatform == Platform.WINDOWS64)) {
return this == curPlatform;
}
return true;
}
if (this.groupName != null && this.groupName.equals(shortName)) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/ghidra/Regexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static final Pattern grabMemberOrPad(String memberName) {
public static final Pattern GRAB_CLASS = grabClass("(?:\\w+::)*\\w+");
public static final Pattern GRAB_TYPE = generateRecursiveRegex(
"(?<lconst>\\bconst\\s+)?(?<sign>\\b(?:signed|unsigned)\\s+)?(?<name>(?:\\w+::)*\\w+)(?<template><(?:{0})(?:\\s*,\\s*(?:{0}))*>)?(?<rconst>\\s+const\\b)?(?<ptr>\\s*\\*+)?(?<ref>\\s*&+)?",
2,
3,
"__depth_limit",
Pattern.DOTALL
);
Expand Down
91 changes: 77 additions & 14 deletions scripts/ghidra/ScriptWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

import ghidra.app.script.GhidraScript;
import ghidra.program.model.data.ArrayDataType;
Expand All @@ -24,14 +25,13 @@
import ghidra.program.model.data.FunctionDefinitionDataType;
import ghidra.program.model.data.IntegerDataType;
import ghidra.program.model.data.LongDataType;
import ghidra.program.model.data.LongLongDataType;
import ghidra.program.model.data.UnsignedLongLongDataType;
import ghidra.program.model.data.ParameterDefinition;
import ghidra.program.model.data.ParameterDefinitionImpl;
import ghidra.program.model.data.Pointer;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.data.ShortDataType;
import ghidra.program.model.data.StructureDataType;
import ghidra.program.model.data.Undefined1DataType;
import ghidra.program.model.data.UnionDataType;
import ghidra.program.model.data.UnsignedCharDataType;
import ghidra.program.model.data.UnsignedIntegerDataType;
Expand Down Expand Up @@ -142,6 +142,38 @@ Namespace addOrGetNamespace(String string) throws Exception {
return ret;
}

DataType getArrayType(String name) {
var arrayMatch = Pattern.compile("^array<(.+), (\\d+)>$").matcher(name);
if (!arrayMatch.matches()) {
printfmt("Array type {0} doesn't match the expected format", name);
return new ArrayDataType(new PointerDataType(VoidDataType.dataType), 0);
}
var type = arrayMatch.group(1);
var size = Integer.parseInt(arrayMatch.group(2));
DataType inner = null;
if (type.matches("bool|char|short|int|long|float|double|void|uchar|ushort|uint|ulong")) {
switch (type) {
case "bool": inner = BooleanDataType.dataType; break;
case "char": inner = CharDataType.dataType; break;
case "short": inner = ShortDataType.dataType; break;
case "int": inner = IntegerDataType.dataType; break;
case "long": inner = LongDataType.dataType; break;
case "float": inner = FloatDataType.dataType; break;
case "double": inner = DoubleDataType.dataType; break;
case "void": inner = VoidDataType.dataType; break;
case "uchar": inner = UnsignedCharDataType.dataType; break;
case "ushort": inner = UnsignedShortDataType.dataType; break;
case "uint": inner = UnsignedIntegerDataType.dataType; break;
case "ulong": inner = UnsignedLongDataType.dataType; break;
}
}
else if (type.startsWith("std::array")) {
inner = this.getArrayType(type.substring(5));
}

return new ArrayDataType(inner != null ? inner : new PointerDataType(VoidDataType.dataType), inner != null ? size : 0);
}

DataType addOrGetType(Broma.Type type) throws Exception {
final var manager = wrapped.getCurrentProgram().getDataTypeManager();

Expand All @@ -164,6 +196,10 @@ DataType addOrGetType(Broma.Type type) throws Exception {
else if (type.name.value.startsWith("gd::")) {
result = this.updateTypeDatabaseWithSTL(type.name.value.substring(4));
}
// Array types
else if (type.name.value.startsWith("std::array")) {
result = this.getArrayType(type.name.value.substring(5) + type.template.get().value);
}
// Broma-specific type
else if (type.name.value.equals("TodoReturn")) {
var path = new CategoryPath("/ClassDataTypes/broma/TodoReturn");
Expand Down Expand Up @@ -326,8 +362,9 @@ void updateTypeDatabase() throws Exception {
cat = this.createCategoryAll(category.extend("gd", "string"));
var string = new StructureDataType(cat, cat.getName(), 0x0);
string.add(stringDataUnion, 0x10, "data", "String data with SSO");
string.add(LongLongDataType.dataType, 0x8, "length", "The length of the string without the terminating null byte");
string.add(LongLongDataType.dataType, 0x8, "capacity", "The capacity of the string buffer");
string.add(UnsignedLongLongDataType.dataType, 0x8, "length", "The length of the string without the terminating null byte");
string.add(UnsignedLongLongDataType.dataType, 0x8, "capacity", "The capacity of the string buffer");
string.setPackingEnabled(true);

manager.addDataType(string, DataTypeConflictHandler.REPLACE_HANDLER);

Expand All @@ -337,6 +374,7 @@ void updateTypeDatabase() throws Exception {
var point = new StructureDataType(cat, cat.getName(), 0x0);
point.add(FloatDataType.dataType, 0x4, "x", "X position of the point");
point.add(FloatDataType.dataType, 0x4, "y", "Y position of the point");
point.setPackingEnabled(true);
manager.addDataType(point, DataTypeConflictHandler.REPLACE_HANDLER);

// cocos2d::CCSize
Expand All @@ -345,6 +383,7 @@ void updateTypeDatabase() throws Exception {
var size = new StructureDataType(cat, cat.getName(), 0x0);
size.add(FloatDataType.dataType, 0x4, "width", "Width of the size");
size.add(FloatDataType.dataType, 0x4, "height", "Height of the size");
size.setPackingEnabled(true);
manager.addDataType(size, DataTypeConflictHandler.REPLACE_HANDLER);

// cocos2d::CCRect
Expand All @@ -355,6 +394,7 @@ void updateTypeDatabase() throws Exception {
rect.add(FloatDataType.dataType, 0x4, "y", "Y position of the rect");
rect.add(FloatDataType.dataType, 0x4, "width", "Width of the rect");
rect.add(FloatDataType.dataType, 0x4, "height", "Height of the rect");
rect.setPackingEnabled(true);
manager.addDataType(rect, DataTypeConflictHandler.REPLACE_HANDLER);

// cocos2d::ccColor3B
Expand All @@ -364,6 +404,7 @@ void updateTypeDatabase() throws Exception {
color3B.add(ByteDataType.dataType, 0x1, "r", "Red component");
color3B.add(ByteDataType.dataType, 0x1, "g", "Green component");
color3B.add(ByteDataType.dataType, 0x1, "b", "Blue component");
color3B.setPackingEnabled(true);
manager.addDataType(color3B, DataTypeConflictHandler.REPLACE_HANDLER);

// cocos2d::ccColor4B
Expand All @@ -374,6 +415,7 @@ void updateTypeDatabase() throws Exception {
color4B.add(ByteDataType.dataType, 0x1, "g", "Green component");
color4B.add(ByteDataType.dataType, 0x1, "b", "Blue component");
color4B.add(ByteDataType.dataType, 0x1, "a", "Alpha component");
color4B.setPackingEnabled(true);
manager.addDataType(color4B, DataTypeConflictHandler.REPLACE_HANDLER);

// cocos2d::ccHSVValue
Expand All @@ -385,8 +427,7 @@ void updateTypeDatabase() throws Exception {
ccHSVValue.add(FloatDataType.dataType, 0x4, "v", "Lightness");
ccHSVValue.add(ByteDataType.dataType, 0x1, "saturationChecked", "");
ccHSVValue.add(ByteDataType.dataType, 0x1, "brightnessChecked", "");
ccHSVValue.add(Undefined1DataType.dataType);
ccHSVValue.add(Undefined1DataType.dataType);
ccHSVValue.setPackingEnabled(true);
manager.addDataType(ccHSVValue, DataTypeConflictHandler.REPLACE_HANDLER);

// cocos2d::SEL_MenuHandler
Expand Down Expand Up @@ -421,6 +462,7 @@ void updateTypeDatabase() throws Exception {
case 'V': ty.add(IntegerDataType.dataType, 0x4, "value", "The original value of the protected value"); break;
}
}
ty.setPackingEnabled(true);
manager.addDataType(ty, DataTypeConflictHandler.REPLACE_HANDLER);
}
}
Expand All @@ -440,26 +482,47 @@ DataType updateTypeDatabaseWithSTL(String templated) throws Exception {
point.add(PointerDataType.dataType, 0x8, "start", "Pointer to the first element in the vector");
point.add(PointerDataType.dataType, 0x8, "last", "Pointer to one past the last element in the vector");
point.add(PointerDataType.dataType, 0x8, "capacity", "Pointer to the end of the current vector allocation");
point.setPackingEnabled(true);
return manager.addDataType(point, DataTypeConflictHandler.REPLACE_HANDLER);
}
else if (templated.startsWith("unordered_map")) {
var point = new StructureDataType(cat, cat.getName(), 0x40);
// todo: idk the structure...
var point = new StructureDataType(cat, cat.getName(), 0x0);
point.add(FloatDataType.dataType, 0x4, "traits", "");
point.add(PointerDataType.dataType, 0x8, "listptr", "");
point.add(UnsignedLongLongDataType.dataType, 0x8, "listlen", "");
point.add(PointerDataType.dataType, 0x8, "vstart", "");
point.add(PointerDataType.dataType, 0x8, "vlast", "");
point.add(PointerDataType.dataType, 0x8, "vcapacity", "");
point.add(UnsignedLongLongDataType.dataType, 0x8, "mask", "");
point.add(UnsignedLongLongDataType.dataType, 0x8, "max", "");
point.setPackingEnabled(true);
return manager.addDataType(point, DataTypeConflictHandler.REPLACE_HANDLER);
}
else if (templated.startsWith("map")) {
var point = new StructureDataType(cat, cat.getName(), 0x10);
// todo: idk the structure...
var point = new StructureDataType(cat, cat.getName(), 0x0);
point.add(PointerDataType.dataType, 0x8, "ptr", "The pointer to the main node of the map");
point.add(UnsignedLongLongDataType.dataType, 0x8, "length", "The length of the map");
point.setPackingEnabled(true);
return manager.addDataType(point, DataTypeConflictHandler.REPLACE_HANDLER);
}
else if (templated.startsWith("unordered_set")) {
var point = new StructureDataType(cat, cat.getName(), 0x40);
// todo: idk the structure...
var point = new StructureDataType(cat, cat.getName(), 0x0);
point.add(FloatDataType.dataType, 0x4, "traits", "");
point.add(PointerDataType.dataType, 0x8, "listptr", "");
point.add(UnsignedLongLongDataType.dataType, 0x8, "listlen", "");
point.add(PointerDataType.dataType, 0x8, "vstart", "");
point.add(PointerDataType.dataType, 0x8, "vlast", "");
point.add(PointerDataType.dataType, 0x8, "vcapacity", "");
point.add(UnsignedLongLongDataType.dataType, 0x8, "mask", "");
point.add(UnsignedLongLongDataType.dataType, 0x8, "max", "");
point.setPackingEnabled(true);
return manager.addDataType(point, DataTypeConflictHandler.REPLACE_HANDLER);
}
else if (templated.startsWith("set")) {
var point = new StructureDataType(cat, cat.getName(), 0x10);
// todo: idk the structure...
var point = new StructureDataType(cat, cat.getName(), 0x0);
point.add(PointerDataType.dataType, 0x8, "ptr", "The pointer to the main node of the set");
point.add(UnsignedLongLongDataType.dataType, 0x8, "length", "The length of the set");
point.setPackingEnabled(true);
return manager.addDataType(point, DataTypeConflictHandler.REPLACE_HANDLER);
}
else {
Expand Down
18 changes: 10 additions & 8 deletions scripts/ghidra/SyncBromaScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,10 @@ private void handleImportMembers() throws Exception {
int length;
if (mem.name.isPresent()) {
final var memType = wrapper.addOrGetType(mem.type.get());
length = memType.getLength();
offset += offset % memType.getAlignment();
boolean isPointer = memType instanceof PointerDataType;
length = isPointer ? manager.getDataOrganization().getPointerSize() : memType.getLength();
int alignment = isPointer ? length : memType.getAlignment();
offset = (offset + alignment - 1) / alignment * alignment;
}
else {
if (mem.paddings.containsKey(args.platform)) {
Expand Down Expand Up @@ -727,16 +729,16 @@ private void handleImportMembers() throws Exception {
);
}
}
for (int i = memType.getLength(); i > 0; i -= 1) {
for (int i = length; i > 0; i -= 1) {
classDataMembers.clearAtOffset(offset + i - 1);
}
classDataMembers.replaceAtOffset(
offset,
memType, memType.getLength(),
memType, length,
mem.name.get().value,
mem.getComment().orElse(null)
);
offset += memType.getLength();
offset += length;
}
else {
if (mem.paddings.containsKey(args.platform)) {
Expand Down Expand Up @@ -814,7 +816,7 @@ private void handleExportMembers(Category category) throws Exception {

// Handle exporting paddings
if (mem.getDataType() instanceof Undefined) {
var pad = new PaddingInfo(mem.getComment());
var pad = new PaddingInfo(mem.getComment(), args.platform);

// If this padding is different between different platforms,
// skip any members inside that padding region
Expand All @@ -833,7 +835,7 @@ private void handleExportMembers(Category category) throws Exception {
int lastPaddingIndex = i;
int originalPadRegionEndIndex = type.getNumComponents() - 1;
for (var j = i; j < type.getNumComponents(); j += 1) {
var opad = new PaddingInfo(type.getComponent(j).getComment());
var opad = new PaddingInfo(type.getComponent(j).getComment(), args.platform);
if (opad.offset == pad.offset) {
lastPaddingIndex = j;
}
Expand Down Expand Up @@ -886,7 +888,7 @@ private void handleExportMembers(Category category) throws Exception {
else {
var length = 0;
for (var j = i; j < type.getNumComponents(); j += 1) {
var opad = new PaddingInfo(type.getComponent(j).getComment());
var opad = new PaddingInfo(type.getComponent(j).getComment(), args.platform);
if (opad.offset != pad.offset) {
break;
}
Expand Down
Loading