From 623334f8637a8e3d7f7994b38d15b3b86bf26193 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Thu, 24 Oct 2024 16:39:11 +0800 Subject: [PATCH] feat: update BMapSharp. - update the function calling in BMapSharp.BMFileReader. (hope JIT can optimize my bad code served for beauty) - remove outdated content in COMPILE.md --- .../BMapSharp/BMapSharp/BMapWrapper.cs | 101 +++++++----------- COMPILE.md | 14 +-- 2 files changed, 42 insertions(+), 73 deletions(-) diff --git a/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs b/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs index a354e8f..75d7920 100644 --- a/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs +++ b/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs @@ -150,7 +150,7 @@ namespace BMapSharp.BMapWrapper { } public class BMObject : AbstractCKObject { - internal BMObject(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) {} + internal BMObject(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) { } public string GetName() { BMapException.ThrowIfFailed(BMap.BMObject_GetName( @@ -166,30 +166,30 @@ namespace BMapSharp.BMapWrapper { } public class BMTexture : BMObject { - internal BMTexture(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) {} + internal BMTexture(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) { } } public class BMMaterial : BMObject { - internal BMMaterial(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) {} + internal BMMaterial(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) { } } public class BMMesh : BMObject { - internal BMMesh(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) {} + internal BMMesh(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) { } } public class BM3dObject : BMObject { - internal BM3dObject(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) {} + internal BM3dObject(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) { } } public class BMGroup : BMObject { - internal BMGroup(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) {} + internal BMGroup(nint raw_pointer, uint ckid) : base(raw_pointer, ckid) { } } public sealed class BMFileReader : AbstractPointer { private static IntPtr AllocateHandle(string file_name, string temp_folder, string texture_folder, string[] encodings) { BMapException.ThrowIfFailed(BMap.BMFile_Load( - file_name, temp_folder, texture_folder, - Utils.BMapSharpCallback, + file_name, temp_folder, texture_folder, + Utils.BMapSharpCallback, (uint)encodings.Length, encodings, out IntPtr out_file )); @@ -199,67 +199,44 @@ namespace BMapSharp.BMapWrapper { return BMap.BMFile_Free(this.handle); } public BMFileReader(string file_name, string temp_folder, string texture_folder, string[] encodings) - : base(AllocateHandle(file_name, temp_folder, texture_folder, encodings)) {} + : base(AllocateHandle(file_name, temp_folder, texture_folder, encodings)) { } - public uint GetTextureCount() { - BMapException.ThrowIfFailed(BMap.BMFile_GetTextureCount(this.getPointer(), out uint out_count)); + + private delegate bool FctProtoGetCount(nint bmf, out uint cnt); + private delegate bool FctProtoGetObject(nint bmf, uint idx, out uint id); + private delegate T FctProtoCreateInstance(nint bmf, uint id); + private uint GetCKObjectCount(FctProtoGetCount fct_cnt) { + BMapException.ThrowIfFailed(fct_cnt(this.getPointer(), out uint out_count)); return out_count; } - public IEnumerable GetTextures() { - uint count = GetTextureCount(); + private IEnumerable GetCKObjects(FctProtoGetCount fct_cnt, FctProtoGetObject fct_obj, FctProtoCreateInstance fct_crt) { + uint count = GetCKObjectCount(fct_cnt); for (uint i = 0; i < count; ++i) { - BMapException.ThrowIfFailed(BMap.BMFile_GetTexture(this.getPointer(), i, out uint out_id)); - yield return new BMTexture(this.getPointer(), out_id); + BMapException.ThrowIfFailed(fct_obj(this.getPointer(), i, out uint out_id)); + yield return fct_crt(this.getPointer(), out_id); } } - public uint GetMaterialCount() { - BMapException.ThrowIfFailed(BMap.BMFile_GetMaterialCount(this.getPointer(), out uint out_count)); - return out_count; - } - public IEnumerable GetMaterials() { - uint count = GetMaterialCount(); - for (uint i = 0; i < count; ++i) { - BMapException.ThrowIfFailed(BMap.BMFile_GetMaterial(this.getPointer(), i, out uint out_id)); - yield return new BMMaterial(this.getPointer(), out_id); - } - } - - public uint GetMeshCount() { - BMapException.ThrowIfFailed(BMap.BMFile_GetMeshCount(this.getPointer(), out uint out_count)); - return out_count; - } - public IEnumerable GetMeshes() { - uint count = GetMeshCount(); - for (uint i = 0; i < count; ++i) { - BMapException.ThrowIfFailed(BMap.BMFile_GetMesh(this.getPointer(), i, out uint out_id)); - yield return new BMMesh(this.getPointer(), out_id); - } - } - - public uint Get3dObjectCount() { - BMapException.ThrowIfFailed(BMap.BMFile_Get3dObjectCount(this.getPointer(), out uint out_count)); - return out_count; - } - public IEnumerable Get3dObjects() { - uint count = Get3dObjectCount(); - for (uint i = 0; i < count; ++i) { - BMapException.ThrowIfFailed(BMap.BMFile_Get3dObject(this.getPointer(), i, out uint out_id)); - yield return new BM3dObject(this.getPointer(), out_id); - } - } - - public uint GetGroupCount() { - BMapException.ThrowIfFailed(BMap.BMFile_GetGroupCount(this.getPointer(), out uint out_count)); - return out_count; - } - public IEnumerable GetGroups() { - uint count = GetGroupCount(); - for (uint i = 0; i < count; ++i) { - BMapException.ThrowIfFailed(BMap.BMFile_GetGroup(this.getPointer(), i, out uint out_id)); - yield return new BMGroup(this.getPointer(), out_id); - } - } + public uint GetTextureCount() => + GetCKObjectCount(BMap.BMFile_GetTextureCount); + public IEnumerable GetTextures() => + GetCKObjects(BMap.BMFile_GetTextureCount, BMap.BMFile_GetTexture, (bmf, id) => new BMTexture(bmf, id)); + public uint GetMaterialCount() => + GetCKObjectCount(BMap.BMFile_GetMaterialCount); + public IEnumerable GetMaterials() => + GetCKObjects(BMap.BMFile_GetMaterialCount, BMap.BMFile_GetMaterial, (bmf, id) => new BMMaterial(bmf, id)); + public uint GetMeshCount() => + GetCKObjectCount(BMap.BMFile_GetMeshCount); + public IEnumerable GetMeshes() => + GetCKObjects(BMap.BMFile_GetMeshCount, BMap.BMFile_GetMesh, (bmf, id) => new BMMesh(bmf, id)); + public uint Get3dObjectCount() => + GetCKObjectCount(BMap.BMFile_Get3dObjectCount); + public IEnumerable Get3dObjects() => + GetCKObjects(BMap.BMFile_Get3dObjectCount, BMap.BMFile_Get3dObject, (bmf, id) => new BM3dObject(bmf, id)); + public uint GetGroupCount() => + GetCKObjectCount(BMap.BMFile_GetGroupCount); + public IEnumerable GetGroups() => + GetCKObjects(BMap.BMFile_GetGroupCount, BMap.BMFile_GetGroup, (bmf, id) => new BMGroup(bmf, id)); } diff --git a/COMPILE.md b/COMPILE.md index 0c73aaa..bb98a0a 100644 --- a/COMPILE.md +++ b/COMPILE.md @@ -42,7 +42,7 @@ First, create subdirectory `Bin/build` and `Bin/install`. Then enter subdirectory `Bin/build` and use following command to configure CMake: -- Windows: `cmake -DNEMO_BUILD_UNVIRT=ON -DNEMO_BUILD_BMAP=ON -DNEMO_BUILD_DOC=OFF -DSTB_IMAGE_PATH= -DYYCC_PATH= -DZLIB_HEADER_PATH= -DZLIB_BINARY_PATH= ../..` +- Windows (MSVC): `cmake -DNEMO_BUILD_UNVIRT=ON -DNEMO_BUILD_BMAP=ON -DNEMO_BUILD_DOC=OFF -DSTB_IMAGE_PATH= -DYYCC_PATH= -DZLIB_HEADER_PATH= -DZLIB_BINARY_PATH= ../..` - non-Windows: `cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DNEMO_BUILD_UNVIRT=ON -DNEMO_BUILD_BMAP=ON -DNEMO_BUILD_DOC=OFF -DSTB_IMAGE_PATH= -DYYCC_PATH= ../..` The arguments in command should be replaced by: @@ -62,8 +62,8 @@ The switches in command can be switched as you wish: Execute following command to build libcmo21. -* Windows:`cmake --build . --config RelWithDebInfo` -* non-Windows:`cmake --build .` +* Windows: `cmake --build . --config RelWithDebInfo` +* non-Windows: `cmake --build .` ### Build Type @@ -76,11 +76,3 @@ Currently the CMake install script still has some bugs and can not work as expec ## Note You may face issue when compiling this program on Linux or macOS because I develop this project on Windows frequently. The compatibility with Linux will only be checked just before releasing. And I don't have any Apple device to check the compatibility with macOS. So, for Linux issue, please report it directly and I will try to fix. For macOS bug, PR is welcomed. - - - -It can be compiled on Windows via sln file. You should set up `LibRef.props` when using sln file to build this project on Windows. -You also can use CMake file to compile this project on Linux or anything else platform. However CMake may not be updated in time because I develop this project on Windows frequently. -You may need use this command to configure CMake: `cmake .. -DSTB_IMAGE_PATH="/path/to/stb-image" -DCMAKE_BUILD_TYPE=Release` - -