nmeier / simscript

Flight sim input/output automation via python scripts
MIT License
10 stars 3 forks source link

FalconSharedMemoryAreaString #4

Closed dglava closed 3 weeks ago

dglava commented 2 years ago

I've also tried to access this part of the shared memory. I ran into an issue where I'm totally lost: std::string strData in line 785 inside the FlightData.h. A few lines below seems to be a guide on how to parse and reconstruct the actual data from it, but it's way over my head.

Do you have an idea how to work this out and perhaps include it in modules/falcon.py?

nmeier commented 2 years ago

Have you tried something like this?

class FalconSharedMemoryAreaString(ctypes.Structure):
    _fields_ = (
        ("versionNum", ctypes.wintypes.UINT),
        ("NoOfStrings", ctypes.wintypes.UINT),
        ("dataSize", ctypes.wintypes.UINT),
        ("data", ctypes.wintypes.CHAR)
)

class FalconSharedMemoryAreaStringStruct(ctypes.Structure):
    _fields_ = (
        ("strId", ctypes.wintypes.UINT),
        ("strLength", ctypes.wintypes.UINT),
        ("strData", ctypes.wintypes.CHAR)
)

for access you'll have to use cast I think

   stringStruct = cast(_pString.data, POINTER(FalconSharedMemoryAreaStringStruct))
   stringStruct.strData

then loop over the structs

   stringStruct = cast(_pString.data + stringStruct.strLength+1, POINTER(FalconSharedMemoryAreaStringStruct))
   stringStruct.strData

I'm away from my computer for a while, I'm just guessing and would have to test this out.

I would probably add a new function to falcon.py, that returns a list of custom list of a customer python class instances with all the complexity resolved if that makes sense,.

dglava commented 2 years ago

Oh, this is way too advanced for my limited skills. I'm hoping someone else might eventually need those and implement it.

dglava commented 1 year ago

This is how far I have gotten, utilizing the help of ChatGPT:

import mmap
import ctypes
import ctypes.wintypes

class StringStruct(ctypes.Structure):
    _fields_ = (
        ("strId", ctypes.wintypes.UINT),
        ("strLength", ctypes.wintypes.UINT),
        ("strData", ctypes.c_char_p)
    )

class StringData(ctypes.Structure):
    _fields_ = (
        ("VersionNum", ctypes.wintypes.UINT),
        ("NoOfStrings", ctypes.wintypes.UINT),
        ("dataSize", ctypes.wintypes.UINT),
        ("data", ctypes.POINTER(StringStruct))
    )

buff = mmap.mmap(-1, ctypes.sizeof(StringData), "FalconSharedMemoryAreaString")
data = StringData.from_buffer(buff)

string_struct_array = ctypes.cast(data.data, ctypes.POINTER(StringStruct * data.NoOfStrings))

for i in range(data.NoOfStrings):
    print(ctypes.string_at(string_struct_array.contents[i].strData, string_struct_array.contents[i].strLength))

But string_struct_array[0].strId or string_struct_array[0].strData isn't outputting anything. What did I do wrong?

dglava commented 3 weeks ago

With the help of various AI programming assistants, I have managed to cobble something together. It also reads the FalconSharedMemoryAreaString part: https://github.com/dglava/falcon-memreader