From 43afe7bf70b24d5d7bbf9312ee85b1932108a065 Mon Sep 17 00:00:00 2001 From: surkeh Date: Wed, 16 May 2018 22:10:46 -0700 Subject: [PATCH] Cleanup: #!s, prints, exceptions, more consistent naming --- Makefile | 2 +- bindings/__init__.py | 6 +----- bindings/{main.py => __test__.py} | 27 ++++++++++++++------------- bindings/py3devil1geo.py | 11 +++++------ bindings/py3devil1pld.py | 14 +++++++------- bindings/py3devil1tex.py | 11 +++++------ 6 files changed, 33 insertions(+), 38 deletions(-) rename bindings/{main.py => __test__.py} (86%) mode change 100644 => 100755 diff --git a/Makefile b/Makefile index ee67998..9b199fb 100644 --- a/Makefile +++ b/Makefile @@ -35,4 +35,4 @@ devil1geo.o: src/devil1geo.c $(CC) -c $^ $(CFLAGS) clean: - rm *.o *.gch $(TARGET) $(PEX) $(TEX) $(MEX) + rm *.o $(TARGET) $(PEX) $(TEX) $(MEX) diff --git a/bindings/__init__.py b/bindings/__init__.py index 7262a68..dbbc6dc 100644 --- a/bindings/__init__.py +++ b/bindings/__init__.py @@ -3,11 +3,7 @@ import sys, ctypes sharedlib = './lib3ddevil1.so' libc = ctypes.cdll.LoadLibrary(sharedlib) if not libc: - print("Couldn't load %s" % sharedlib) - sys.exit() - -# Observe how many times the .so is loaded. -print("\nlib3ddevil1 loaded.") + raise SystemExit("Couldn't load %s" % sharedlib) # Don't need these anymore del sys, sharedlib diff --git a/bindings/main.py b/bindings/__test__.py old mode 100644 new mode 100755 similarity index 86% rename from bindings/main.py rename to bindings/__test__.py index 8659ea2..3953c65 --- a/bindings/main.py +++ b/bindings/__test__.py @@ -1,6 +1,7 @@ -from py3devil1pld import PldHeader -from py3devil1tex import TEXturePack, TEXtureBatchDescriptor, TEXtureBatch -from py3devil1geo import GEOHeader, MEShHeader, MEsh +#!/usr/bin/python3 +from py3devil1pld import pyPldHeader +from py3devil1tex import pyTexturePack, pyTextureBatchDescriptor, pyTextureBatch +from py3devil1geo import pyGeoHeader, pyMeshHeader, pyMesh #print(libc) @@ -54,7 +55,7 @@ if __name__ == "__main__": print("OK") pldfn = Devil1PLD_FN.in_dll(libc, "DEVIL1PLD") - pldh = PldHeader() + pldh = pyPldHeader() pldtest(pldfn, pldh) texfn = Devil1TEX_FN.in_dll(libc, "DEVIL1TEX") @@ -68,29 +69,29 @@ if __name__ == "__main__": def mainx(): with open("pl01.pld", "rb") as f: data = f.read() - pld = PldHeader(data) + pld = pyPldHeader(data) pld.show() - pld2 = PldHeader() + pld2 = pyPldHeader() pld2.show() with open("pl01.pld_1.txp", "rb") as f: data = f.read() - txp = TEXturePack(data) + txp = pyTexturePack(data) txp.show() print(txp.getbatchno()) print(txp.getfirstbatchoffset()) - tbd = TEXtureBatchDescriptor(1, data) + tbd = pyTextureBatchDescriptor(1, data) tbd.show() print(tbd.gettexturesize()) - #tx = TEXtures(0, tbd.gettexno(), data) - tx = TEXtureBatch(0, data) + #tx = pyTextures(0, tbd.gettexno(), data) + tx = pyTextureBatch(0, data) ts = tx.gettextures() for i in range(0, 10): print(ts[0].data[i]) with open("pl00.pld_0", "rb") as f: data = f.read() - gh = GEOHeader(data) + gh = pyGeoHeader(data) gh.show() print("-------------") print(gh.getmeshno()) @@ -99,9 +100,9 @@ if __name__ == "__main__": print(gh.getunknownd()) print(gh.getpadding()) print(gh.getunknownoffset()) - mh = MEShHeader(3, data) + mh = pyMeshHeader(3, data) mh.show() - m = MEsh(0, data) + m = pyMesh(0, data) m.show() # p = m.getpositions() # print("positions:") diff --git a/bindings/py3devil1geo.py b/bindings/py3devil1geo.py index 7d5ca1b..7388053 100644 --- a/bindings/py3devil1geo.py +++ b/bindings/py3devil1geo.py @@ -1,6 +1,5 @@ -#!/usr/bin/python3 import ctypes -from .__init__ import libc +from __init__ import libc #--------------------------------------+ # Basic Struct @@ -141,7 +140,7 @@ del libc # Pythonic Object #--------------------------------------+ -class GEOHeader: +class pyGeoHeader: def __init__(self, filedata): self.cstruct = ctypes.pointer(Header()) ptrofptr = ctypes.byref(self.cstruct) @@ -171,7 +170,7 @@ class GEOHeader: return hex(self.cstruct.contents.unknownOffset) -class MEShHeader: +class pyMeshHeader: def __init__(self, i, filedata): self.cstruct = ctypes.pointer(MeshHeader()) ptrofptr = ctypes.byref(self.cstruct) @@ -198,11 +197,11 @@ class MEShHeader: def getflags(self): return self.cstruct.contents.flags -class MEsh: +class pyMesh: def __init__(self, i, filedata): self.cstruct = Mesh() if filedata: - mh = MEShHeader(i, filedata) + mh = pyMeshHeader(i, filedata) # allocate memory for the size of batch * number of batches memsize = ctypes.sizeof(Batch) * mh.getbatchno() self.cstruct.b = ctypes.cast( diff --git a/bindings/py3devil1pld.py b/bindings/py3devil1pld.py index 8bc6c5d..958f8bd 100644 --- a/bindings/py3devil1pld.py +++ b/bindings/py3devil1pld.py @@ -1,12 +1,12 @@ #!/usr/bin/python3 import ctypes -from .__init__ import libc +from __init__ import libc #--------------------------------------+ # Basic Struct #--------------------------------------+ -class _PldHeader_(ctypes.Structure): +class PldHeader(ctypes.Structure): _pack_ = 1 _fields_ = [ ("numOffset", ctypes.c_int), @@ -17,14 +17,14 @@ class Devil1PLD_FN(ctypes.Structure): _fields_ = [ ("getheader" , ctypes.CFUNCTYPE( ctypes.c_bool, - ctypes.POINTER(_PldHeader_), + ctypes.POINTER(PldHeader), ctypes.c_char_p)), ("sizeofsector", ctypes.CFUNCTYPE( ctypes.c_int, - ctypes.POINTER(_PldHeader_), + ctypes.POINTER(PldHeader), ctypes.c_int)), ("printheader" , ctypes.CFUNCTYPE(None, - ctypes.POINTER(_PldHeader_))) + ctypes.POINTER(PldHeader))) ] devil1pld = Devil1PLD_FN.in_dll(libc, "DEVIL1PLD") @@ -34,10 +34,10 @@ del libc # Pythonic Object #--------------------------------------+ -class PldHeader: +class pyPldHeader: def __init__(self, filedata = None): # Store C Struct in order to call C functions - self.cstruct = _PldHeader_() + self.cstruct = PldHeader() if filedata: devil1pld.getheader(ctypes.byref(self.cstruct), filedata) self.eof = len(filedata) diff --git a/bindings/py3devil1tex.py b/bindings/py3devil1tex.py index 7ad661a..32aeb7f 100644 --- a/bindings/py3devil1tex.py +++ b/bindings/py3devil1tex.py @@ -1,6 +1,5 @@ -#!/usr/bin/python3 import ctypes -from .__init__ import libc +from __init__ import libc #--------------------------------------+ # Basic Struct @@ -80,7 +79,7 @@ del libc # Pythonic Object #--------------------------------------+ -class TEXturePack: +class pyTexturePack: def __init__(self, filedata): self.cstruct = ctypes.pointer(TexturePack()) devil1tex.getheader(ctypes.byref(self.cstruct), filedata) @@ -95,7 +94,7 @@ class TEXturePack: def getfirstbatchoffset(self): return self.cstruct.contents.firstBatchOffset -class TEXtureBatchDescriptor: +class pyTextureBatchDescriptor: def __init__(self, i, filedata): self.cstruct = ctypes.pointer(TextureBatchDescriptor()) ptrofptr = ctypes.byref(self.cstruct) @@ -118,12 +117,12 @@ class TEXtureBatchDescriptor: def gettexturesize(self): return self.cstruct.contents.textureSize -class TEXtureBatch: +class pyTextureBatch: def __init__(self, i, filedata): self.cstruct = TextureBatch() if filedata: self.cstruct.batch = None - tbd = TEXtureBatchDescriptor(i, filedata) + tbd = pyTextureBatchDescriptor(i, filedata) self.amount = tbd.gettexno() memsize = self.amount * tbd.gettexturesize() self.cstruct.batch = ctypes.cast(