diff --git a/scripts/ApplyTypes.java b/scripts/ApplyTypes.java index 3fba1e9..8e48561 100644 --- a/scripts/ApplyTypes.java +++ b/scripts/ApplyTypes.java @@ -36,13 +36,17 @@ public class ApplyTypes extends GhidraScript { if (base.containsKey(cls)) addFields(s, base.get(cls), depth + 1); 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) { + for (int i = 0; i < own.size(); i++) { + String[] f = own.get(i); 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); - if (dt == null) continue; - if (arr.find(0)) dt = new ArrayDataType(dt, Integer.decode(arr.group(1)), dt.getLength()); int off = Integer.decode(f[1]); + int room = i + 1 < own.size() ? Integer.decode(own.get(i + 1)[1]) - off : 4; + if (dt == null && !arr.find(0) && room > 0) // unknown type (std_map, DeathmatchManager): name its first dword + dt = room >= 4 ? Undefined4DataType.dataType : new ArrayDataType(ByteDataType.dataType, room, 1); + else if (dt != null && arr.find(0)) dt = new ArrayDataType(dt, Integer.decode(arr.group(1)), dt.getLength()); + if (dt == null) continue; String nm = decl.trim().replaceAll(".*\\s", "").replaceAll("[^\\w]", ""); try { DataTypeComponent in = off < s.getLength() ? s.getComponentContaining(off) : null; @@ -111,14 +115,8 @@ public class ApplyTypes extends GhidraScript { // methods whose ECX offsets fit their class (scripts/this_types.py) become __thiscall, typing `this` int fitted = 0; if (getScriptArgs().length > 1) for (String l : Files.readAllLines(Paths.get(getScriptArgs()[1]))) { - String[] c = l.split("\t"); - Function f = getFunctionAt(toAddr(c[0])); + Function f = getFunctionAt(toAddr(l.split("\t")[0])); if (f == null || "__thiscall".equals(f.getCallingConventionName())) continue; - if (f.getName().startsWith("FUN_")) { // unnamed, placed by its ECX offsets: move it into the class - Namespace cn = st.getNamespace(c[1], global); - if (cn == null) continue; - try { f.setParentNamespace(cn); } catch (Exception e) { continue; } - } Namespace ns = f.getParentNamespace(); if (!(ns instanceof GhidraClass)) try { st.convertNamespaceToClass(ns); } catch (Exception e) { continue; } f.setCallingConvention("__thiscall");