From b8b2368ef5624df34cffbc3513a844c0cefbaf7e Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 3 Dec 2023 18:15:05 +0800 Subject: [PATCH] fix issues - fix lifetime issue about python c callback function - fix type hint in PyBMap - fix formatter clamp issue in CKContext --- BMapBindings/PyBMap/PyBMap/bmap.py | 2 +- BMapBindings/PyBMap/PyBMap/bmap_wrapper.py | 9 ++++----- CodeGen/BMapBindings/snippets/header.py | 2 +- LibCmo/CK2/CKContext.cpp | 3 ++- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/BMapBindings/PyBMap/PyBMap/bmap.py b/BMapBindings/PyBMap/PyBMap/bmap.py index 8dcab34..6a1bc44 100644 --- a/BMapBindings/PyBMap/PyBMap/bmap.py +++ b/BMapBindings/PyBMap/PyBMap/bmap.py @@ -78,7 +78,7 @@ elif sys.platform.startswith('darwin'): else: _g_BMapLibName = "BMap.bin" -_g_BMapModule: ctypes.CDLL = None +_g_BMapModule: ctypes.CDLL | None = None try: _g_BMapModule = ctypes.cdll.LoadLibrary( os.path.join(os.path.dirname(__file__), _g_BMapLibName) diff --git a/BMapBindings/PyBMap/PyBMap/bmap_wrapper.py b/BMapBindings/PyBMap/PyBMap/bmap_wrapper.py index 9a3168e..f4bf6b3 100644 --- a/BMapBindings/PyBMap/PyBMap/bmap_wrapper.py +++ b/BMapBindings/PyBMap/PyBMap/bmap_wrapper.py @@ -7,7 +7,7 @@ g_InvalidPtr: bmap.bm_void_p = bmap.bm_void_p(0) g_InvalidCKID: int = 0 g_BMapEncoding: str = "utf-8" -def python_callback(strl: bytes): +def _python_callback(strl: bytes): """ The Python type callback for BMFile. Simply add a prefix when output. @@ -17,6 +17,7 @@ def python_callback(strl: bytes): # i think Python do a auto convertion here. if strl is not None: print(f'[PyBMap] {strl.decode(g_BMapEncoding)}') +_g_RawCallback: bmap.bm_callback = bmap.bm_callback(_python_callback) class _AbstractPointer(): __mRawPointer: int @@ -594,7 +595,6 @@ class BMFileReader(_AbstractPointer): 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)) 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_)) encodings: ctypes.Array = (bmap.bm_CKSTRING * len(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() # exec bmap.BMFile_Load( - file_name, temp_folder, texture_folder, raw_callback, + file_name, temp_folder, texture_folder, _g_RawCallback, encoding_count, encodings, ctypes.byref(out_file) ) @@ -693,7 +693,6 @@ class BMFileWriter(_AbstractPointer): # create param 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)) - raw_callback: bmap.bm_callback = bmap.bm_callback(python_callback) encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_)) encodings: ctypes.Array = (bmap.bm_CKSTRING * len(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() # exec bmap.BMFile_Create( - temp_folder, texture_folder, raw_callback, + temp_folder, texture_folder, _g_RawCallback, encoding_count, encodings, ctypes.byref(out_file) ) diff --git a/CodeGen/BMapBindings/snippets/header.py b/CodeGen/BMapBindings/snippets/header.py index 8a7e521..32cf17b 100644 --- a/CodeGen/BMapBindings/snippets/header.py +++ b/CodeGen/BMapBindings/snippets/header.py @@ -78,7 +78,7 @@ elif sys.platform.startswith('darwin'): else: _g_BMapLibName = "BMap.bin" -_g_BMapModule: ctypes.CDLL = None +_g_BMapModule: ctypes.CDLL | None = None try: _g_BMapModule = ctypes.cdll.LoadLibrary( os.path.join(os.path.dirname(__file__), _g_BMapLibName) diff --git a/LibCmo/CK2/CKContext.cpp b/LibCmo/CK2/CKContext.cpp index 7857dc2..921ca01 100644 --- a/LibCmo/CK2/CKContext.cpp +++ b/LibCmo/CK2/CKContext.cpp @@ -264,7 +264,8 @@ namespace LibCmo::CK2 { XContainer::XString result; int count = std::vsnprintf(nullptr, 0, fmt, argptr); 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;