|
| 1 | +-- Field data management module |
| 2 | +-- Handles initialization and manipulation of field data structures |
| 3 | + |
| 4 | +require('common'); |
| 5 | +local field_utils = require('fields.utils'); |
| 6 | + |
| 7 | +local field_data = {}; |
| 8 | + |
| 9 | +-- Field data initialization |
| 10 | +function field_data.init_field_data(field) |
| 11 | + if field_utils.is_char_array(field) then |
| 12 | + return T { '' }; |
| 13 | + elseif field_utils.is_numeric_array(field) then |
| 14 | + local array = T {}; |
| 15 | + for i = 1, field.array_size do array[i] = 0; end |
| 16 | + return array; |
| 17 | + else |
| 18 | + return T { 0 }; |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +function field_data.init_nested_struct_data(nested_fields) |
| 23 | + local nested_data = T {}; |
| 24 | + for _, nested_field in ipairs(nested_fields) do |
| 25 | + if not nested_field.name:find('padding') then |
| 26 | + -- Initialize nested field data exactly like regular fields |
| 27 | + if nested_field.array_size then |
| 28 | + if nested_field.base_type == 'uint8_t' or nested_field.base_type == 'char' then |
| 29 | + nested_data[nested_field.name] = T { '' }; |
| 30 | + else |
| 31 | + local array = T {}; |
| 32 | + for i = 1, nested_field.array_size do array[i] = 0; end |
| 33 | + nested_data[nested_field.name] = array; |
| 34 | + end |
| 35 | + else |
| 36 | + nested_data[nested_field.name] = T { 0 }; |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + return nested_data; |
| 41 | +end |
| 42 | + |
| 43 | +function field_data.init_struct_data(struct_name, get_struct_fields_func) |
| 44 | + local struct_info = get_struct_fields_func(struct_name); |
| 45 | + if not struct_info then return false; end |
| 46 | + |
| 47 | + local struct_data = T {}; |
| 48 | + |
| 49 | + struct_info.fields:each(function(field) |
| 50 | + if field_utils.is_nested_struct(field) then |
| 51 | + -- Handle nested struct or array of structs |
| 52 | + local array_count = field.array_size or 1; |
| 53 | + local nested_array = T {}; |
| 54 | + for i = 1, array_count do |
| 55 | + nested_array[i] = field_data.init_nested_struct_data(field.nested_fields); |
| 56 | + end |
| 57 | + struct_data[field.name] = nested_array; |
| 58 | + else |
| 59 | + -- Handle regular fields |
| 60 | + struct_data[field.name] = field_data.init_field_data(field); |
| 61 | + end |
| 62 | + end); |
| 63 | + |
| 64 | + -- Set packet ID if field exists |
| 65 | + if struct_data.id then |
| 66 | + struct_data.id[1] = struct_info.packet_id; |
| 67 | + end |
| 68 | + |
| 69 | + return struct_data; |
| 70 | +end |
| 71 | + |
| 72 | +-- Helper value getters for specific field types |
| 73 | +function field_data.get_helper_value(field_name) |
| 74 | + local target = AshitaCore:GetMemoryManager():GetTarget(); |
| 75 | + local party = AshitaCore:GetMemoryManager():GetParty(); |
| 76 | + |
| 77 | + if field_name:match('UniqueNo') then |
| 78 | + if target and target:GetTargetIndex(0) > 0 then |
| 79 | + return target:GetServerId(0); |
| 80 | + else |
| 81 | + return party:GetMemberServerId(0); |
| 82 | + end |
| 83 | + elseif field_name:match('ActIndex') then |
| 84 | + if target and target:GetTargetIndex(0) > 0 then |
| 85 | + return target:GetTargetIndex(0); |
| 86 | + else |
| 87 | + return party:GetMemberTargetIndex(0); |
| 88 | + end |
| 89 | + end |
| 90 | + return nil; |
| 91 | +end |
| 92 | + |
| 93 | +return field_data; |
0 commit comments