diff --git a/scripts/ApplyTypes.java b/scripts/ApplyTypes.java index a8ec3d4..4c6fc7e 100644 --- a/scripts/ApplyTypes.java +++ b/scripts/ApplyTypes.java @@ -34,7 +34,9 @@ public class ApplyTypes extends GhidraScript { void addFields(StructureDataType s, String cls, int depth) { if (depth > 8) return; if (base.containsKey(cls)) addFields(s, base.get(cls), depth + 1); - for (String[] f : fields.getOrDefault(cls, List.of())) { + List own = new ArrayList<>(fields.getOrDefault(cls, List.of())); + own.sort(Comparator.comparingInt(f -> Integer.decode(f[1]))); // in offset order, so an embedded struct beats views into it + for (String[] f : own) { java.util.regex.Matcher arr = java.util.regex.Pattern.compile("\\[(\\w+)\\]\\s*$").matcher(f[2]); // "int _trinketsID[2]" String decl = arr.find() ? f[2].substring(0, arr.start()) : f[2]; DataType dt = prim(decl); @@ -43,6 +45,8 @@ public class ApplyTypes extends GhidraScript { int off = Integer.decode(f[1]); String nm = decl.trim().replaceAll(".*\\s", "").replaceAll("[^\\w]", ""); try { + DataTypeComponent in = off < s.getLength() ? s.getComponentContaining(off) : null; + if (in != null && in.getOffset() != off && in.getDataType() != DataType.DEFAULT) continue; // inside a placed member (Entity 0xdc is _sprite._offset): keep the member if (off + dt.getLength() > s.getLength()) s.growStructure(off + dt.getLength() - s.getLength()); s.replaceAtOffset(off, dt, dt.getLength(), nm, null); } catch (Exception e) { /* overlapping/odd field: skip */ }