Skip to content

Commit af6a50c

Browse files
committed
feat: use cmdline args as the args of BMap bindings.
- update testbench of PyBMap and BMapSharp. use command line arguments as the arguments of testbench, instead of hardcoded variables in code.
1 parent 0bf0519 commit af6a50c

File tree

5 files changed

+103
-11
lines changed

5 files changed

+103
-11
lines changed

BMapBindings/BMapSharp/BMapSharpTestbench/BMapSharpTestbench.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<ProjectReference Include="..\BMapSharp\BMapSharp.csproj" />
55
</ItemGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
9+
</ItemGroup>
10+
711
<PropertyGroup>
812
<OutputType>Exe</OutputType>
913
<TargetFramework>net8.0</TargetFramework>

BMapBindings/BMapSharp/BMapSharpTestbench/Program.cs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
using System.Text;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.CommandLine;
67

78
namespace BMapSharpTestbench {
89
internal class Program {
910

1011
static void Main(string[] args) {
12+
// Parse arguments
13+
var resolved_args = ResolveArguments(args);
14+
if (resolved_args is null) {
15+
// just silent quit
16+
Environment.Exit(0);
17+
}
18+
1119
// Check environment
1220
Console.OutputEncoding = Encoding.UTF8;
1321
if (!BMapSharp.BMapWrapper.Utils.IsBMapAvailable()) {
@@ -21,10 +29,10 @@ static void Main(string[] args) {
2129
Console.ReadKey(true);
2230

2331
// Start testbench
24-
string file_name = "LightCameraTest.nmo";
25-
string temp_folder = "Temp";
26-
string texture_folder = "F:\\Ballance\\Ballance\\Textures";
27-
string[] encodings = ["cp1252", "gb2312"];
32+
string file_name = resolved_args.mFileName; // "LightCameraTest.nmo";
33+
string temp_folder = resolved_args.mTempFolder; // "Temp";
34+
string texture_folder = resolved_args.mTextureFolder; // "F:\\Ballance\\Ballance\\Textures";
35+
string[] encodings = resolved_args.mEncodings; // ["cp1252", "gb2312"];
2836

2937
using (var reader = new BMapSharp.BMapWrapper.BMFileReader(file_name, temp_folder, texture_folder, encodings)) {
3038
TestCommon(reader);
@@ -36,6 +44,53 @@ static void Main(string[] args) {
3644

3745
}
3846

47+
class BMapSharpArguments {
48+
public string mFileName;
49+
public string mTempFolder;
50+
public string mTextureFolder;
51+
public string[] mEncodings;
52+
}
53+
54+
static BMapSharpArguments ResolveArguments(string[] args) {
55+
// define arguments
56+
var fileNameOpt = new Option<string>
57+
("--file-path", "The path to input Virtools file.");
58+
fileNameOpt.IsRequired = true;
59+
var tempFolderOpt = new Option<string>
60+
("--temp-dir", "The temp folder used by BMap.");
61+
tempFolderOpt.IsRequired = true;
62+
var textureFolderOpt = new Option<string>
63+
("--texture-dir", "The texture folder containing Ballance texture resources.");
64+
textureFolderOpt.IsRequired = true;
65+
var encodingsOpt = new Option<IEnumerable<string>>
66+
("--encodings", "The encodings used to parse the names stroed in input Virtools file.");
67+
encodingsOpt.IsRequired = true;
68+
encodingsOpt.Arity = ArgumentArity.OneOrMore;
69+
encodingsOpt.AllowMultipleArgumentsPerToken = true;
70+
71+
// init root command
72+
var rootCommand = new RootCommand("The testbench of BMapSharp.");
73+
rootCommand.Add(fileNameOpt);
74+
rootCommand.Add(tempFolderOpt);
75+
rootCommand.Add(textureFolderOpt);
76+
rootCommand.Add(encodingsOpt);
77+
78+
// init result container
79+
BMapSharpArguments ret = new BMapSharpArguments();
80+
// set handler
81+
rootCommand.SetHandler((context) => {
82+
ret.mFileName = context.ParseResult.GetValueForOption(fileNameOpt);
83+
ret.mTempFolder = context.ParseResult.GetValueForOption(tempFolderOpt);
84+
ret.mTextureFolder = context.ParseResult.GetValueForOption(textureFolderOpt);
85+
ret.mEncodings = context.ParseResult.GetValueForOption(encodingsOpt).ToArray();
86+
context.ExitCode = 61;
87+
});
88+
89+
// execute root command and return value.
90+
if (rootCommand.Invoke(args) != 61) return null;
91+
return ret;
92+
}
93+
3994
static void TestCommon(BMapSharp.BMapWrapper.BMFileReader reader) {
4095
// Console.WriteLine("===== Groups =====");
4196
// foreach (var gp in reader.GetGroups()) {
@@ -137,7 +192,7 @@ static void TestIEquatable(BMapSharp.BMapWrapper.BMFileReader reader) {
137192
);
138193
return;
139194
}
140-
195+
141196
// Prepare test variables
142197
var all_3dobjects = new List<BM3dObject>(reader.Get3dObjects());
143198
var first_3dobj = all_3dobjects[0];

BMapBindings/BMapSharp/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ The core of BMapSharp project is placed within `BMapSharp` subdirectory. This di
55
The native BMap library should be placed together with managed `BMapSharp` dynamic library. I use gitignore file to filter all native binary so you need put them manually. The native BMap library must be named as `BMap.dll` (in Windows), `BMap.so` (in Linux or BSD), or `BMap.dylib` (in macOS). If you still can not load BMap or your system is not listed above, you should name it as `BMap.bin`.
66

77
The most content of `VirtoolsTypes.cs` is generated by EnumsMigration, and the most content of `BMap.cs` is generated by BMapBindings. You should watch these file changes if corresponding C++ code or structures are changed.
8+
9+
Since BMap 0.3.0, testbench use command line arguments, instead of hardcode variables in code, as the arguments of BMap. It is convenient that debug BMapSharp without any modification of source code. For a brief instruction, you may need to launch BMapSharpTestbench in following command (just an example. you can modify it as you wished): `dotnet run -- --file-path "LightCameraTest.nmo" --temp-dir "Temp" --texture-dir "F:/Ballance/Ballance/Textures" --encodings cp1252 gb2312`.

BMapBindings/PyBMap/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ The real scripts are placed in sub PyBMap folder. This folder is served for test
55
The native BMap library should be placed in sub PyBMap folder, and I have used gitignore file to filter them. The native BMap library must be named as `BMap.dll` (in Windows), `BMap.so` (in Linux or BSD), or `BMap.dylib` (in macOS). If you still can not load BMap or your system is not listed above, you should name it as `BMap.bin`.
66

77
Please note the most content of `virtools_types.py` are generated by EnumsMigration sub-project. Additionally the most content of `bmap.py` is generated by BMapBindings. So if some structs are updated, do not forget checking these files.
8+
9+
Since BMap 0.3.0, testbench use command line arguments, instead of hardcode variables in code, as the arguments of BMap. It is convenient that debug BMapSharp without any modification of source code. For a brief instruction, you may need to launch BMapSharpTestbench in following command (just an example. you can modify it as you wished): `py testbench.py --file-path "LightCameraTest.nmo" --temp-dir "Temp" --texture-dir "F:/Ballance/Ballance/Textures" --encodings cp1252 gb2312`.

BMapBindings/PyBMap/testbench.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import os
2+
import argparse
23
import PyBMap.bmap_wrapper as bmap
34

4-
def main() -> None:
5+
def main(file_name: str, temp_folder: str, texture_folder: str, encodings: tuple[str, ...]) -> None:
56
input(f'Python PID is {os.getpid()}. Waiting for debugger, press any key to continue...')
67

7-
file_name: str = 'LightCameraTest.nmo'
8-
temp_folder: str = 'Temp'
9-
texture_folder: str = 'F:\\Ballance\\Ballance\\Textures'
10-
encodings: tuple[str, ...] = ('cp1252', )
8+
# file_name: str = 'LightCameraTest.nmo'
9+
# temp_folder: str = 'Temp'
10+
# texture_folder: str = 'F:\\Ballance\\Ballance\\Textures'
11+
# encodings: tuple[str, ...] = ('cp1252', )
1112
with bmap.BMFileReader(file_name, temp_folder, texture_folder, encodings) as reader:
1213
test_common(reader)
1314
test_equatable(reader)
@@ -146,4 +147,32 @@ def test_equatable(reader: bmap.BMFileReader):
146147
assert second_3dobj in test_dict
147148

148149
if __name__ == '__main__':
149-
main()
150+
# parse argument
151+
parser = argparse.ArgumentParser(
152+
prog='PyBMap Testbench',
153+
description='The testbench of PyBMap.'
154+
)
155+
parser.add_argument(
156+
'--file-path',
157+
action='store', dest='file_path', required=True,
158+
help='The path to input Virtools file.'
159+
)
160+
parser.add_argument(
161+
'--temp-dir',
162+
action='store', dest='temp_dir', required=True,
163+
help='The temp folder used by BMap.'
164+
)
165+
parser.add_argument(
166+
'--texture-dir',
167+
action='store', dest='texture_dir', required=True,
168+
help='The texture folder containing Ballance texture resources.'
169+
)
170+
parser.add_argument(
171+
'--encodings',
172+
action='extend', nargs='+', dest='encodings', required=True,
173+
help='The encodings used to parse the names stroed in input Virtools file.'
174+
)
175+
args = parser.parse_args()
176+
177+
# run main function
178+
main(args.file_path, args.temp_dir, args.texture_dir, tuple(args.encodings))

0 commit comments

Comments
 (0)