-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
1291 lines (1125 loc) · 42.2 KB
/
mcp_server.py
File metadata and controls
1291 lines (1125 loc) · 42.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ValveResourceFormat MCP Server
使用官方 MCP SDK 重构的服务器,提供与 Valve Source 2 资源格式交互的工具。
"""
import asyncio
import os
import sys
import json
import subprocess
import concurrent.futures
from pathlib import Path
from typing import Optional, Any
try:
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
except ImportError:
print(json.dumps({
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32000,
"message": "MCP SDK not found. Please install: pip install mcp"
}
}), file=sys.stderr)
sys.exit(1)
# 全局配置
VRF_CLI_PATH: Optional[str] = None
_thread_count: int = 1
_executor: Optional[Any] = None
_executor_shutdown: bool = False
def get_cli_path() -> str:
"""获取 CLI 路径,从环境变量读取"""
global VRF_CLI_PATH
if VRF_CLI_PATH:
return VRF_CLI_PATH
env_path = os.environ.get("VRF_CLI_PATH")
if env_path and Path(env_path).exists():
VRF_CLI_PATH = env_path
return env_path
error_msg = (
f"VRF CLI not found. VRF_CLI_PATH='{env_path or '<not set>'}'. "
"Please set VRF_CLI_PATH environment variable to point to Source2Viewer-CLI.exe"
)
raise FileNotFoundError(error_msg)
def run_cli(args: list[str], timeout: int = 60) -> tuple[int, str, str]:
"""执行 VRF CLI 命令"""
cli_path = get_cli_path()
cmd = [cli_path] + args
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
encoding='utf-8',
errors='replace',
timeout=timeout
)
return result.returncode, result.stdout, result.stderr
except subprocess.TimeoutExpired:
return -1, "", f"Command timed out after {timeout} seconds"
except Exception as e:
return -1, "", str(e) if str(e) else "Unknown error occurred"
def parse_vpk_list(output: str) -> list[dict[str, Any]]:
"""解析 VPK 列表输出"""
files = []
for line in output.strip().split('\n'):
line = line.strip()
if not line:
continue
# 格式: "path/to/file.ext CRC:xxxxxxxxxx size:xxxxx"
parts = line.split(' CRC:')
if parts:
path = parts[0]
size = 0
crc = "0000000000"
if len(parts) > 1:
crc_part = parts[1]
crc_size_parts = crc_part.split(' size:')
if crc_size_parts:
crc = crc_size_parts[0]
if len(crc_size_parts) > 1:
try:
size = int(crc_size_parts[1])
except ValueError:
pass
files.append({
"path": path,
"crc": crc,
"size": size
})
return files
# ============================================================
# 工具定义
# ============================================================
def create_tools() -> list[Tool]:
"""创建 MCP 工具列表"""
return [
Tool(
name="get_file_info",
description="获取文件的基本信息(大小、类型、扩展名)",
inputSchema={
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "文件路径"
}
},
"required": ["file_path"]
}
),
Tool(
name="list_vpk_contents",
description="列出 VPK 归档中的所有文件,支持按扩展名或路径过滤",
inputSchema={
"type": "object",
"properties": {
"vpk_path": {
"type": "string",
"description": "VPK 文件路径"
},
"extension_filter": {
"type": "string",
"description": "逗号分隔的扩展名列表(如 'vmdl,vmat')"
},
"path_filter": {
"type": "string",
"description": "路径前缀过滤(如 'models/')"
}
},
"required": ["vpk_path"]
}
),
Tool(
name="inspect_file",
description="检查 Source 2 资源文件的结构、块和数据。VPK 内部文件请使用 'vpk_path::internal_path' 格式",
inputSchema={
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "资源文件路径,可以是文件系统路径或 'vpk_path::internal_path' 格式"
}
},
"required": ["file_path"]
}
),
Tool(
name="decompile_resource",
description="将 Source 2 资源文件反编译为可读的原始格式",
inputSchema={
"type": "object",
"properties": {
"input_path": {
"type": "string",
"description": "要反编译的资源文件路径"
},
"output_path": {
"type": "string",
"description": "可选的输出路径"
}
},
"required": ["input_path"]
}
),
Tool(
name="export_gltf",
description="将 3D 模型(.vmdl)导出为 glTF/glb 格式以便在其他工具中查看",
inputSchema={
"type": "object",
"properties": {
"model_path": {
"type": "string",
"description": "模型文件路径(.vmdl)或 VPK 内部路径"
},
"output_path": {
"type": "string",
"description": "输出 glTF/glb 文件路径"
},
"vpk_path": {
"type": "string",
"description": "如果 model_path 是内部路径,需要指定此 VPK 路径"
},
"include_animations": {
"type": "boolean",
"description": "是否在导出中包含动画",
"default": True
},
"include_materials": {
"type": "boolean",
"description": "是否在导出中包含材质",
"default": True
}
},
"required": ["model_path", "output_path"]
}
),
Tool(
name="export_gltf_advanced",
description="高级 glTF 导出,支持动画/网格过滤和 VPK 支持",
inputSchema={
"type": "object",
"properties": {
"model_path": {
"type": "string",
"description": "模型文件路径(.vmdl)或 VPK 内部路径"
},
"output_path": {
"type": "string",
"description": "输出 glTF/glb 文件路径"
},
"format": {
"type": "string",
"description": "导出格式,gltf 或 glb",
"default": "glb"
},
"vpk_path": {
"type": "string",
"description": "如果 model_path 是内部路径,则指定 VPK 路径"
},
"include_animations": {
"type": "boolean",
"description": "是否包含动画",
"default": True
},
"include_materials": {
"type": "boolean",
"description": "是否包含材质",
"default": True
},
"animation_list": {
"type": "string",
"description": "逗号分隔的要包含的动画名称列表"
},
"mesh_list": {
"type": "string",
"description": "逗号分隔的要包含的网格名称列表"
},
"textures_adapt": {
"type": "boolean",
"description": "对纹理执行 glTF 规范适配",
"default": False
},
"export_extras": {
"type": "boolean",
"description": "将额外的网格属性导出到 glTF extras",
"default": False
}
},
"required": ["model_path", "output_path"]
}
),
Tool(
name="extract_texture",
description="将纹理(.vtex)提取为图像文件(PNG/TGA)",
inputSchema={
"type": "object",
"properties": {
"texture_path": {
"type": "string",
"description": "纹理文件路径(.vtex)"
},
"output_path": {
"type": "string",
"description": "输出图像文件路径"
},
"decode_flags": {
"type": "string",
"description": "解码标志:'none'、'auto' 或 'focused'",
"default": "auto"
}
},
"required": ["texture_path", "output_path"]
}
),
Tool(
name="list_directory_resources",
description="列出目录中的所有 Source 2 资源文件,支持可选过滤",
inputSchema={
"type": "object",
"properties": {
"directory": {
"type": "string",
"description": "要扫描的目录路径"
},
"extension_filter": {
"type": "string",
"description": "逗号分隔的要包含的扩展名列表"
},
"recursive": {
"type": "boolean",
"description": "是否递归扫描子目录",
"default": False
}
},
"required": ["directory"]
}
),
Tool(
name="verify_vpk",
description="验证 VPK 归档的完整性和签名",
inputSchema={
"type": "object",
"properties": {
"vpk_path": {
"type": "string",
"description": "VPK 文件路径"
}
},
"required": ["vpk_path"]
}
),
Tool(
name="decompile_vpk",
description="将 VPK 归档中的所有资源反编译到指定输出目录",
inputSchema={
"type": "object",
"properties": {
"vpk_path": {
"type": "string",
"description": "VPK 文件路径"
},
"output_path": {
"type": "string",
"description": "反编译文件的输出目录"
},
"extension_filter": {
"type": "string",
"description": "逗号分隔的扩展名过滤器"
},
"path_filter": {
"type": "string",
"description": "路径前缀过滤器"
},
"recursive": {
"type": "boolean",
"description": "是否递归到嵌套的 VPK",
"default": False
}
},
"required": ["vpk_path", "output_path"]
}
),
Tool(
name="collect_stats",
description="收集资源文件的统计信息。使用 'steam' 作为输入来扫描所有 Steam 库",
inputSchema={
"type": "object",
"properties": {
"input_path": {
"type": "string",
"description": "文件/文件夹/VPK 路径,或 'steam' 扫描所有 Steam 库"
},
"include_files": {
"type": "boolean",
"description": "打印每个统计的示例文件名",
"default": False
},
"unique_deps": {
"type": "boolean",
"description": "收集所有唯一依赖项",
"default": False
},
"particles": {
"type": "boolean",
"description": "收集粒子算子、渲染器、发射器、初始化器",
"default": False
},
"vbib": {
"type": "boolean",
"description": "收集顶点属性统计",
"default": False
},
"with_loader": {
"type": "boolean",
"description": "使用 GameFileLoader 加载依赖项",
"default": False
},
"gltf_test": {
"type": "boolean",
"description": "测试每个支持文件的 glTF 导出代码路径",
"default": False
}
},
"required": ["input_path"]
}
),
Tool(
name="vpk_dir",
description="显示 VPK 归档的详细目录信息,包括文件偏移、CRC、元数据大小等",
inputSchema={
"type": "object",
"properties": {
"vpk_path": {
"type": "string",
"description": "VPK 文件路径"
}
},
"required": ["vpk_path"]
}
),
Tool(
name="inspect_block",
description="检查 Source 2 资源文件的结构、块和数据。支持 -a 打印所有块,-b 指定特定块",
inputSchema={
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "资源文件路径,支持 'vpk_path::internal_path' 格式"
},
"print_all": {
"type": "boolean",
"description": "打印每个资源块的全部内容 (-a)",
"default": False
},
"block_name": {
"type": "string",
"description": "只打印指定块,如 DATA, RERL, REDI, NTRO (-b)"
}
},
"required": ["file_path"]
}
),
Tool(
name="set_threads",
description="设置处理文件时的线程数,用于加速批量处理",
inputSchema={
"type": "object",
"properties": {
"thread_count": {
"type": "integer",
"description": "线程数量,1 表示单线程,大于 1 表示并发处理",
"default": 1
}
}
}
),
Tool(
name="vpk_cache",
description="使用 VPK 缓存清单跟踪更新,只写入变更的文件",
inputSchema={
"type": "object",
"properties": {
"vpk_path": {
"type": "string",
"description": "VPK 文件路径"
},
"output_path": {
"type": "string",
"description": "输出目录"
},
"use_cache": {
"type": "boolean",
"description": "是否使用缓存",
"default": True
}
},
"required": ["vpk_path", "output_path"]
}
),
Tool(
name="gltf_export",
description="将 3D 模型(.vmdl)导出为 glTF 或 glb 格式",
inputSchema={
"type": "object",
"properties": {
"model_path": {
"type": "string",
"description": "模型文件路径(.vmdl),支持 VPK 内部路径"
},
"output_path": {
"type": "string",
"description": "输出 glTF/glb 文件路径"
},
"format": {
"type": "string",
"description": "导出格式,gltf 或 glb",
"default": "glb"
},
"include_animations": {
"type": "boolean",
"description": "是否包含动画",
"default": True
},
"include_materials": {
"type": "boolean",
"description": "是否包含材质",
"default": True
},
"animation_list": {
"type": "string",
"description": "逗号分隔的要包含的动画名称列表"
},
"mesh_list": {
"type": "string",
"description": "逗号分隔的要包含的网格名称列表"
},
"textures_adapt": {
"type": "boolean",
"description": "对纹理执行 glTF 规范适配",
"default": False
},
"export_extras": {
"type": "boolean",
"description": "将额外的网格属性导出到 glTF extras",
"default": False
},
"vpk_path": {
"type": "string",
"description": "如果 model_path 是内部路径,需要指定此 VPK 路径"
}
},
"required": ["model_path", "output_path"]
}
),
Tool(
name="dump_unknown_keys",
description="收集统计信息时保存所有未知实体键哈希到 unknown_keys.txt",
inputSchema={
"type": "object",
"properties": {
"input_path": {
"type": "string",
"description": "文件/文件夹/VPK 路径,或 'steam' 扫描所有 Steam 库"
},
"include_files": {
"type": "boolean",
"description": "是否打印示例文件名",
"default": False
}
},
"required": ["input_path"]
}
),
Tool(
name="tools_asset_info",
description="获取工具资源信息,支持简短模式只打印路径",
inputSchema={
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "资源文件路径,支持 'vpk_path::internal_path' 格式"
},
"short": {
"type": "boolean",
"description": "简短模式,只打印文件路径",
"default": False
}
},
"required": ["file_path"]
}
),
]
# ============================================================
# 工具实现
# ============================================================
async def handle_get_file_info(args: dict) -> dict:
"""获取文件信息"""
file_path = args.get("file_path")
if not file_path:
return {"success": False, "error": "file_path 是必填参数"}
path = Path(file_path)
if not path.exists():
return {"success": False, "error": "文件不存在", "file": file_path}
size = path.stat().st_size
return {
"success": True,
"file": file_path,
"name": path.name,
"extension": path.suffix.lower(),
"size": size,
"size_formatted": _format_size(size)
}
async def handle_list_vpk_contents(args: dict) -> dict:
"""列出 VPK 内容"""
vpk_path = args.get("vpk_path")
if not vpk_path:
return {"success": False, "error": "vpk_path 是必填参数"}
cli_args = ["-i", vpk_path, "--vpk_list"]
extension_filter = args.get("extension_filter")
if extension_filter:
cli_args.extend(["--vpk_extensions", extension_filter])
path_filter = args.get("path_filter")
if path_filter:
cli_args.extend(["--vpk_filepath", path_filter])
returncode, stdout, stderr = await run_cli_async(cli_args)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法列出 VPK 内容",
"vpk": vpk_path
}
files = parse_vpk_list(stdout)
# 应用扩展名过滤(如果 CLI 没有支持)
if extension_filter and not path_filter:
exts = [f".{e.strip('.')}" for e in extension_filter.split(",")]
files = [f for f in files if any(f["path"].endswith(e) for e in exts)]
return {
"success": True,
"vpk": vpk_path,
"files": [f["path"] for f in files],
"file_count": len(files),
"details": files
}
async def handle_inspect_file(args: dict) -> dict:
"""检查资源文件(inspect_block 的便捷封装)"""
file_path = args.get("file_path")
if not file_path:
return {"success": False, "error": "file_path 是必填参数"}
# 调用 inspect_block 并启用 print_all
return await handle_inspect_block({
"file_path": file_path,
"print_all": True
})
async def handle_decompile_resource(args: dict) -> dict:
"""反编译资源"""
input_path = args.get("input_path")
if not input_path:
return {"success": False, "error": "input_path 是必填参数"}
output_path = args.get("output_path")
# 处理 VPK 内部路径格式
if "::" in input_path:
parts = input_path.split("::", 1)
vpk_path = parts[0]
internal_path = parts[1] if len(parts) > 1 else ""
cli_args = ["-i", vpk_path, "--vpk_filepath", internal_path, "--decompile"]
else:
cli_args = ["-i", input_path, "--decompile"]
if output_path:
cli_args.extend(["-o", output_path])
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=120)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法反编译文件",
"input": input_path
}
return {
"success": True,
"input": input_path,
"output": output_path or stdout,
"output_path": output_path or ""
}
async def handle_export_gltf(args: dict) -> dict:
"""导出 glTF(gltf_export 的便捷封装,固定输出 glb 格式)"""
model_path = args.get("model_path")
output_path = args.get("output_path")
if not model_path:
return {"success": False, "error": "model_path 是必填参数"}
if not output_path:
return {"success": False, "error": "output_path 是必填参数"}
include_animations = args.get("include_animations", True)
include_materials = args.get("include_materials", True)
# 调用 gltf_export,固定 format 为 glb
return await handle_gltf_export({
"model_path": model_path,
"output_path": output_path,
"format": "glb",
"include_animations": include_animations,
"include_materials": include_materials,
"vpk_path": args.get("vpk_path")
})
async def handle_export_gltf_advanced(args: dict) -> dict:
"""高级 glTF 导出(gltf_export 的封装)"""
model_path = args.get("model_path")
output_path = args.get("output_path")
if not model_path:
return {"success": False, "error": "model_path 是必填参数"}
if not output_path:
return {"success": False, "error": "output_path 是必填参数"}
# 调用 gltf_export,传递所有参数
return await handle_gltf_export({
"model_path": model_path,
"output_path": output_path,
"format": args.get("format", "glb"),
"include_animations": args.get("include_animations", True),
"include_materials": args.get("include_materials", True),
"animation_list": args.get("animation_list"),
"mesh_list": args.get("mesh_list"),
"textures_adapt": args.get("textures_adapt", False),
"export_extras": args.get("export_extras", False),
"vpk_path": args.get("vpk_path"),
})
async def handle_extract_texture(args: dict) -> dict:
"""提取纹理"""
texture_path = args.get("texture_path")
output_path = args.get("output_path")
if not texture_path:
return {"success": False, "error": "texture_path 是必填参数"}
if not output_path:
return {"success": False, "error": "output_path 是必填参数"}
decode_flags = args.get("decode_flags", "auto")
# 处理 VPK 内部路径格式
if "::" in texture_path:
parts = texture_path.split("::", 1)
vpk_path = parts[0]
internal_path = parts[1] if len(parts) > 1 else ""
cli_args = [
"-i", vpk_path,
"--vpk_filepath", internal_path,
"--decompile",
"--texture_decode_flags", decode_flags,
"-o", output_path
]
else:
cli_args = [
"-i", texture_path,
"--decompile",
"--texture_decode_flags", decode_flags,
"-o", output_path
]
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=120)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法提取纹理",
"input": texture_path
}
return {
"success": True,
"input": texture_path,
"output": output_path
}
async def handle_list_directory_resources(args: dict) -> dict:
"""列出目录中的资源"""
directory = args.get("directory")
if not directory:
return {"success": False, "error": "directory 是必填参数"}
extension_filter = args.get("extension_filter")
recursive = args.get("recursive", False)
path = Path(directory)
if not path.exists() or not path.is_dir():
return {"success": False, "error": "目录不存在", "directory": directory}
# 默认 Source 2 扩展名
if extension_filter:
extensions = [f".{ext.strip('.')}" for ext in extension_filter.split(",")]
else:
extensions = [
".vmdl", ".vmat", ".vtex", ".vani", ".vsndevts",
".vpcf", ".vmap", ".vrad", ".vrml", ".vrml_c",
".vbsp", ".vcd", ".vpk"
]
files = []
pattern = "**/*" if recursive else "*"
for ext in extensions:
for f in path.glob(f"{pattern}{ext}"):
if f.is_file():
files.append(str(f))
# 也查找编译版本
compiled_extensions = [f".{ext.rstrip('_c')}_c" for ext in extensions]
for ext in compiled_extensions:
for f in path.glob(f"{pattern}{ext}"):
if f.is_file():
files.append(str(f))
unique_files = sorted(set(files))
return {
"success": True,
"directory": directory,
"files": [str(Path(f).relative_to(path)) for f in unique_files],
"file_count": len(unique_files)
}
async def handle_verify_vpk(args: dict) -> dict:
"""验证 VPK"""
vpk_path = args.get("vpk_path")
if not vpk_path:
return {"success": False, "error": "vpk_path 是必填参数"}
cli_args = ["-i", vpk_path, "--vpk_verify"]
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=120)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法验证 VPK",
"vpk": vpk_path
}
return {
"success": True,
"vpk": vpk_path,
"output": stdout
}
async def handle_decompile_vpk(args: dict) -> dict:
"""批量反编译 VPK"""
vpk_path = args.get("vpk_path")
output_path = args.get("output_path")
if not vpk_path:
return {"success": False, "error": "vpk_path 是必填参数"}
if not output_path:
return {"success": False, "error": "output_path 是必填参数"}
extension_filter = args.get("extension_filter")
path_filter = args.get("path_filter")
recursive = args.get("recursive", False)
cli_args = ["-i", vpk_path, "-o", output_path, "-d"]
if extension_filter:
cli_args.extend(["--vpk_extensions", extension_filter])
if path_filter:
cli_args.extend(["--vpk_filepath", path_filter])
if recursive:
cli_args.append("--recursive_vpk")
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=600)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法反编译 VPK",
"vpk": vpk_path
}
return {
"success": True,
"vpk": vpk_path,
"output_path": output_path,
"output": stdout
}
async def handle_collect_stats(args: dict) -> dict:
"""收集统计信息"""
input_path = args.get("input_path")
if not input_path:
return {"success": False, "error": "input_path 是必填参数"}
include_files = args.get("include_files", False)
unique_deps = args.get("unique_deps", False)
particles = args.get("particles", False)
vbib = args.get("vbib", False)
with_loader = args.get("with_loader", False)
gltf_test = args.get("gltf_test", False)
cli_args = ["-i", input_path, "--stats"]
if include_files:
cli_args.append("--stats_print_files")
if unique_deps:
cli_args.append("--stats_unique_deps")
if particles:
cli_args.append("--stats_particles")
if vbib:
cli_args.append("--stats_vbib")
if with_loader:
cli_args.append("--stats_with_loader")
if gltf_test:
cli_args.append("--gltf_test")
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=600)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法收集统计",
"input": input_path
}
return {
"success": True,
"input": input_path,
"output": stdout
}
async def handle_vpk_dir(args: dict) -> dict:
"""显示 VPK 详细目录信息"""
vpk_path = args.get("vpk_path")
if not vpk_path:
return {"success": False, "error": "vpk_path 是必填参数"}
cli_args = ["-i", vpk_path, "--vpk_dir"]
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=120)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法获取 VPK 目录",
"vpk": vpk_path
}
return {
"success": True,
"vpk": vpk_path,
"output": stdout
}
async def handle_inspect_block(args: dict) -> dict:
"""检查资源文件块"""
file_path = args.get("file_path")
if not file_path:
return {"success": False, "error": "file_path 是必填参数"}
print_all = args.get("print_all", False)
block_name = args.get("block_name")
# 处理 VPK 内部路径格式
if "::" in file_path:
parts = file_path.split("::", 1)
vpk_path = parts[0]
internal_path = parts[1] if len(parts) > 1 else ""
cli_args = ["-i", vpk_path, "--vpk_filepath", internal_path]
else:
cli_args = ["-i", file_path]
if print_all:
cli_args.append("-a")
elif block_name:
cli_args.extend(["-b", block_name])
else:
# 无参数时默认打印所有块
cli_args.append("-a")
returncode, stdout, stderr = await run_cli_async(cli_args, timeout=120)
if returncode != 0:
return {
"success": False,
"error": stderr or "无法检查文件",
"file": file_path