My aim is not to write the exact code you want to end up using. My only intent is to show how to read an array of structs that have their addresses specified by a previously read array of offsets. That is only the part that you are writing fake code for presumably because you are unsure how ImHex can do that operation. My idea is that once that is understood, the other details for your particular pattern should no be that complicated.
\nHere is the problem. Youwant to perform this operation
\nSectionData data_sections[i] @ header.sections[ i].data_offset;
for all values of i between 0 and header.num_sections as if it was being done in a loop. As explained, loops cannot be used inside structs and you need to create an array of structs to accomplish the equivalent work done in the loop the ImHex way.
\nWhen I say array of structs I don't mean an array of SectionData structs. If you make an array of them you will only be able to place the first one and the rest will simply follow each other.
\nTo achieve the placing of each array element at the corresponding offset found in the array of offsets a totally new structure, call it whatever you want but I'll use DataStruct, must be created. Inside it you place one of the section array elements using the function shown above to get the corresponding offset. As the array is being created, the array_index() function returns the value of the current array index and since the two indices in the loop were the same hare you just use it directly. This way we place one structure at one address. How and where you get the offsets is not important for the explanation. Using parent is cleaner and preferable but when starting to write patterns there is a tendency to assign data to global variables because it makes things easier. Easier means less powerful too. Again this is not important for the current discussion. So this is what i would think is the explanation you seek.
struct DataStruct {\n SectionData data_sections @ header.sections[ std::core::array_index()].data_offset;\n};\nThis is not optional. You can't place the entire array at sets of addresses directly. You have to do it one at a time. You wrap it up by creating an array of Datastructs. The array obviously has to be the same size as the array of offsets. \n```cpp\n// Inside some struct that you need to place\nDataStruct ds[header.num_sections];\n...
You don't have to necessarily see the DataStruct variable in your pattern. There are a number of ways like [[inline]] that can let you see exactly what you want in the pattern data window.
\nTo be honest, this idea of using offsets to place structs is almost never needed. All input files can be read from the beginning to the end once you learn whats located where. Also it is preferable to end up with 1 pattern that is placed at zero and reads the entire file. This makes modifying the pattern a lot easier and makes the code look cleaner. If you browse through the pattern library provided by imhex you will see a majority of patterns follow those principles as close as possible. At the very least that was my experience when I did that.
","upvoteCount":0,"url":"https://github.com/WerWolv/ImHex/discussions/1896#discussioncomment-10594507"}}}-
Currently dealing with a file format where I want to look recursively at structs and create custom logic based off that. For example, say TL:DR: I want to essentially do
|
Beta Was this translation helpful? Give feedback.
-
Loops cannot be used inside of structs. Instead you want to create an array of structs which will function as the loop. The function std::core::array_index() tends to be very useful for this kind of setup because it gives you access inside the struct you are making an array of to the current index being processed so you can use it to index placed arrays that maybe located at the parent struct using the parent keyword. If still unsure I can try to post some code if you can give a minimal example description of what you are trying to do. |
Beta Was this translation helpful? Give feedback.
My aim is not to write the exact code you want to end up using. My only intent is to show how to read an array of structs that have their addresses specified by a previously read array of offsets. That is only the part that you are writing fake code for presumably because you are unsure how ImHex can do that operation. My idea is that once that is understood, the other details for your particular pattern should no be that complicated.
Here is the problem. Youwant to perform this operation
for all values of i between 0 and header.num_sections as if it was being done in a loop. As explained, loops cannot be used inside structs …