import 'dart:io'; import 'package:path/path.dart' as p; /// Resolves relocated bundled tools while preserving valid custom executables. class ToolPaths { ToolPaths({String? base, this.projectRoot, String? searchPath}) : base = base ?? p.dirname(Platform.resolvedExecutable), searchPath = searchPath ?? Platform.environment['PATH'] ?? ''; final String base, searchPath; final String? projectRoot; static bool managed(String value) => RegExp(r'/programs/(?:ttstd|tongtongstudio)/ads-(?:flutter|avalonia)/') .hasMatch(value.replaceAll(r'\', '/').toLowerCase()); String resolve(String name, {String configured = '', bool required = false}) { final exe = Platform.isWindows ? '$name.exe' : name; final selected = configured.trim().replaceAll(RegExp(r'^"|"$'), ''); if (selected.isNotEmpty && !managed(selected) && File(selected).existsSync()) { return p.absolute(selected); } final candidates = [ p.join(base, exe), p.join(base, 'tools', 'scrcpy', exe), if (projectRoot != null) p.join(projectRoot!, 'bin', exe), if (projectRoot != null) p.join(projectRoot!, 'bin', 'tools', 'scrcpy', exe), for (final folder in searchPath.split(Platform.isWindows ? ';' : ':')) if (folder.trim().isNotEmpty) p.join(folder.trim().replaceAll('"', ''), exe), ]; for (final candidate in candidates) { if (File(candidate).existsSync()) return p.absolute(candidate); } if (required) { throw FileSystemException( '找不到 $exe。请重新安装完整安装包,或在连接配置中选择有效的工具文件。当前程序目录:$base', selected.isEmpty ? exe : selected, ); } return selected.isNotEmpty ? selected : exe; } }