lib3ddevil1/bindings/py3devil1pld.py

67 lines
2.0 KiB
Python
Raw Permalink Normal View History

import ctypes
import os, sys
# This is the folder containing the whole library.
sys.path.append(
os.path.abspath(
os.path.join(
os.path.dirname(__file__), "../../")))
from lib3ddevil1.bindings import libc
del os, sys
2018-05-16 01:12:16 +05:30
#--------------------------------------+
# Basic Struct
#--------------------------------------+
class PldHeader(ctypes.Structure):
2018-05-16 01:12:16 +05:30
_pack_ = 1
_fields_ = [
("numOffset", ctypes.c_int),
("offsets", ctypes.POINTER(ctypes.c_int))
]
class Devil1PLD_FN(ctypes.Structure):
_fields_ = [
("getheader" , ctypes.CFUNCTYPE(
ctypes.c_bool,
ctypes.POINTER(PldHeader),
2018-05-16 01:12:16 +05:30
ctypes.c_char_p)),
("sizeofsector", ctypes.CFUNCTYPE(
ctypes.c_int,
ctypes.POINTER(PldHeader),
2018-05-16 01:12:16 +05:30
ctypes.c_int)),
("printheader" , ctypes.CFUNCTYPE(None,
ctypes.POINTER(PldHeader)))
2018-05-16 01:12:16 +05:30
]
devil1pld = Devil1PLD_FN.in_dll(libc, "DEVIL1PLD")
del libc
2018-05-16 01:12:16 +05:30
#--------------------------------------+
# Pythonic Object
#--------------------------------------+
class pyPldHeader:
2018-05-16 01:12:16 +05:30
def __init__(self, filedata = None):
# Store C Struct in order to call C functions
self.cstruct = PldHeader()
2018-05-16 01:12:16 +05:30
if filedata:
2018-05-18 00:05:19 +05:30
if not devil1pld.getheader(ctypes.byref(self.cstruct), filedata):
raise RuntimeError("failed to get .pld header")
2018-05-16 01:12:16 +05:30
self.eof = len(filedata)
def show(self):
devil1pld.printheader(ctypes.byref(self.cstruct))
return
def getnumoffsets(self):
return self.cstruct.numOffsets
# return pythonic list of offsets
def getoffsets(self):
return self.cstruct.offsets[:self.cstruct.numOffset]
def sizeofsector(self, i):
ptr = ctypes.byref(self.cstruct)
return devil1pld.sizeofsector(ptr, i, self.eof)