fix issues

- fix lifetime issue about python c callback function
- fix type hint in PyBMap
- fix formatter clamp issue in CKContext
This commit is contained in:
yyc12345 2023-12-03 18:15:05 +08:00
parent a70e32a306
commit b8b2368ef5
4 changed files with 8 additions and 8 deletions

View File

@ -78,7 +78,7 @@ elif sys.platform.startswith('darwin'):
else: else:
_g_BMapLibName = "BMap.bin" _g_BMapLibName = "BMap.bin"
_g_BMapModule: ctypes.CDLL = None _g_BMapModule: ctypes.CDLL | None = None
try: try:
_g_BMapModule = ctypes.cdll.LoadLibrary( _g_BMapModule = ctypes.cdll.LoadLibrary(
os.path.join(os.path.dirname(__file__), _g_BMapLibName) os.path.join(os.path.dirname(__file__), _g_BMapLibName)

View File

@ -7,7 +7,7 @@ g_InvalidPtr: bmap.bm_void_p = bmap.bm_void_p(0)
g_InvalidCKID: int = 0 g_InvalidCKID: int = 0
g_BMapEncoding: str = "utf-8" g_BMapEncoding: str = "utf-8"
def python_callback(strl: bytes): def _python_callback(strl: bytes):
""" """
The Python type callback for BMFile. The Python type callback for BMFile.
Simply add a prefix when output. Simply add a prefix when output.
@ -17,6 +17,7 @@ def python_callback(strl: bytes):
# i think Python do a auto convertion here. # i think Python do a auto convertion here.
if strl is not None: if strl is not None:
print(f'[PyBMap] {strl.decode(g_BMapEncoding)}') print(f'[PyBMap] {strl.decode(g_BMapEncoding)}')
_g_RawCallback: bmap.bm_callback = bmap.bm_callback(_python_callback)
class _AbstractPointer(): class _AbstractPointer():
__mRawPointer: int __mRawPointer: int
@ -594,7 +595,6 @@ class BMFileReader(_AbstractPointer):
file_name: bmap.bm_CKSTRING = bmap.bm_CKSTRING(file_name_.encode(g_BMapEncoding)) file_name: bmap.bm_CKSTRING = bmap.bm_CKSTRING(file_name_.encode(g_BMapEncoding))
temp_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(temp_folder_.encode(g_BMapEncoding)) temp_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(temp_folder_.encode(g_BMapEncoding))
texture_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(texture_folder_.encode(g_BMapEncoding)) texture_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(texture_folder_.encode(g_BMapEncoding))
raw_callback: bmap.bm_callback = bmap.bm_callback(python_callback)
encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_)) encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_))
encodings: ctypes.Array = (bmap.bm_CKSTRING * len(encodings_))( encodings: ctypes.Array = (bmap.bm_CKSTRING * len(encodings_))(
*(strl.encode(g_BMapEncoding) for strl in encodings_) *(strl.encode(g_BMapEncoding) for strl in encodings_)
@ -602,7 +602,7 @@ class BMFileReader(_AbstractPointer):
out_file: bmap.bm_void_p = bmap.bm_void_p() out_file: bmap.bm_void_p = bmap.bm_void_p()
# exec # exec
bmap.BMFile_Load( bmap.BMFile_Load(
file_name, temp_folder, texture_folder, raw_callback, file_name, temp_folder, texture_folder, _g_RawCallback,
encoding_count, encodings, encoding_count, encodings,
ctypes.byref(out_file) ctypes.byref(out_file)
) )
@ -693,7 +693,6 @@ class BMFileWriter(_AbstractPointer):
# create param # create param
temp_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(temp_folder_.encode(g_BMapEncoding)) temp_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(temp_folder_.encode(g_BMapEncoding))
texture_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(texture_folder_.encode(g_BMapEncoding)) texture_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(texture_folder_.encode(g_BMapEncoding))
raw_callback: bmap.bm_callback = bmap.bm_callback(python_callback)
encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_)) encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_))
encodings: ctypes.Array = (bmap.bm_CKSTRING * len(encodings_))( encodings: ctypes.Array = (bmap.bm_CKSTRING * len(encodings_))(
*(strl.encode(g_BMapEncoding) for strl in encodings_) *(strl.encode(g_BMapEncoding) for strl in encodings_)
@ -701,7 +700,7 @@ class BMFileWriter(_AbstractPointer):
out_file: bmap.bm_void_p = bmap.bm_void_p() out_file: bmap.bm_void_p = bmap.bm_void_p()
# exec # exec
bmap.BMFile_Create( bmap.BMFile_Create(
temp_folder, texture_folder, raw_callback, temp_folder, texture_folder, _g_RawCallback,
encoding_count, encodings, encoding_count, encodings,
ctypes.byref(out_file) ctypes.byref(out_file)
) )

View File

@ -78,7 +78,7 @@ elif sys.platform.startswith('darwin'):
else: else:
_g_BMapLibName = "BMap.bin" _g_BMapLibName = "BMap.bin"
_g_BMapModule: ctypes.CDLL = None _g_BMapModule: ctypes.CDLL | None = None
try: try:
_g_BMapModule = ctypes.cdll.LoadLibrary( _g_BMapModule = ctypes.cdll.LoadLibrary(
os.path.join(os.path.dirname(__file__), _g_BMapLibName) os.path.join(os.path.dirname(__file__), _g_BMapLibName)

View File

@ -264,7 +264,8 @@ namespace LibCmo::CK2 {
XContainer::XString result; XContainer::XString result;
int count = std::vsnprintf(nullptr, 0, fmt, argptr); int count = std::vsnprintf(nullptr, 0, fmt, argptr);
result.resize(count); result.resize(count);
int write_result = std::vsnprintf(result.data(), count, fmt, argptr); // count + 1 for NUL terminator. but we don't need allocate space for it (resize with count). let it write into the reserved tail of std::string.
int write_result = std::vsnprintf(result.data(), count + 1, fmt, argptr);
if (write_result < 0 || write_result > count) return; if (write_result < 0 || write_result > count) return;