|
| 1 | +-- Finds a primitive variable in memory |
| 2 | + |
| 3 | +--[====[ |
| 4 | +
|
| 5 | +devel/find-primitive |
| 6 | +==================== |
| 7 | +
|
| 8 | +Finds a primitive variable in DF's data section, relying on the user to change |
| 9 | +its value. This is similar to `devel/find-offsets`, but useful for new variables |
| 10 | +whose locations are unknown (i.e. they could be part of an existing global). |
| 11 | +
|
| 12 | +Usage:: |
| 13 | +
|
| 14 | + devel/find-primitive data-type val1 val2 [val3...] |
| 15 | +
|
| 16 | +where ``data-type`` is a primitive type (int32_t, uint8_t, long, etc.) and each |
| 17 | +``val`` is a valid value for that type. |
| 18 | +
|
| 19 | +Use ``devel/find-primitive help`` for a list of valid data types. |
| 20 | +
|
| 21 | +]====] |
| 22 | + |
| 23 | +ms = require('memscan') |
| 24 | + |
| 25 | +searcher = ms.DiffSearcher.new(ms.get_data_segment()) |
| 26 | +values = {...} |
| 27 | +data_type = table.remove(values, 1) |
| 28 | + |
| 29 | +function is_valid_data_type(t) |
| 30 | + return getmetatable(searcher.area[t]) == ms.CheckedArray |
| 31 | +end |
| 32 | + |
| 33 | +if data_type == 'help' or not is_valid_data_type(data_type) then |
| 34 | + print('Valid data types:') |
| 35 | + for t in pairs(searcher.area) do |
| 36 | + if is_valid_data_type(t) then |
| 37 | + print(' ' .. t) |
| 38 | + end |
| 39 | + end |
| 40 | + if data_type ~= 'help' then |
| 41 | + qerror('Invalid data type') |
| 42 | + end |
| 43 | + return |
| 44 | +end |
| 45 | + |
| 46 | +addr = searcher:find_menu_cursor( |
| 47 | + 'Adjust the value of the variable so that it matches the message given, then press enter.', |
| 48 | + data_type, |
| 49 | + values |
| 50 | +) |
| 51 | +if addr then |
| 52 | + print(('0x%x'):format(addr)) |
| 53 | +else |
| 54 | + qerror('Unable to find variable') |
| 55 | +end |
0 commit comments