import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:path/path.dart' as p; import 'package:shared_preferences/shared_preferences.dart'; /// 获取本机机器码(注册表 MachineGuid,失败则退化为稳定哈希) Future getMachineCode() async { try { final result = Process.runSync('reg', [ 'query', r'HKLM\SOFTWARE\Microsoft\Cryptography', '/v', 'MachineGuid' ]); final out = result.stdout.toString(); final m = RegExp(r'MachineGuid\s+REG_SZ\s+(\S+)').firstMatch(out); if (m != null) return m.group(1)!; } catch (_) {} // 备选:主机名+用户名 final fallback = '${Platform.localHostname}-${Platform.environment['USERNAME'] ?? 'pc'}'; return fallback; } /// 本地持久化:下载/安装路径 class AppPrefs { static Future getDownloadPath() async { final sp = await SharedPreferences.getInstance(); return sp.getString('download_path') ?? ''; } static Future getInstallPath() async { final sp = await SharedPreferences.getInstance(); return sp.getString('install_path') ?? ''; } static Future setPaths(String dl, String ins) async { final sp = await SharedPreferences.getInstance(); await sp.setString('download_path', dl); await sp.setString('install_path', ins); } } /// 简单格式化字节数 String formatBytes(double bytes) { if (bytes >= 1024 * 1024 * 1024) { return '${(bytes / 1024 / 1024 / 1024).toStringAsFixed(2)} GB'; } if (bytes >= 1024 * 1024) { return '${(bytes / 1024 / 1024).toStringAsFixed(2)} MB'; } if (bytes >= 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB'; return '${bytes.toStringAsFixed(0)} B'; } /// 速度格式化(按比特展示:下载弹窗显示 Mbps / bps) String formatSpeed(double bytesPerSec) { if (bytesPerSec <= 0) return '0.00 bps'; final mbps = bytesPerSec * 8 / 1000000; if (mbps < 1) { return '${(bytesPerSec * 8 / 1000).toStringAsFixed(2)} Kbps'; } return '${mbps.toStringAsFixed(2)} Mbps'; } /// 秒 -> HH:MM:SS String formatDuration(int seconds) { final h = seconds ~/ 3600; final m = (seconds % 3600) ~/ 60; final s = seconds % 60; String two(int n) => n.toString().padLeft(2, '0'); return '${two(h)}:${two(m)}:${two(s)}'; } /// 文件名安全化(去除 Windows 非法字符) String safeFileName(String s) => s.replaceAll(RegExp(r'[\\/:*?"<>|]'), '_'); /// 游戏安装目录(与下载安装逻辑保持一致) Future gameInstallDir(String title) async { final ins = await AppPrefs.getInstallPath(); final base = ins.isEmpty ? 'C:\\99GameBox\\99GameI' : ins; return '$base\\${safeFileName(title)}'; } /// 游戏是否已安装(目录存在且能找到启动程序) Future isGameInstalled(String title, String launchExe) async { final dir = Directory(await gameInstallDir(title)); if (!await dir.exists()) return false; if (launchExe.isNotEmpty) { if (await File(p.join(dir.path, launchExe)).exists()) return true; } try { return dir .listSync(recursive: false) .whereType() .any((f) => f.path.toLowerCase().endsWith('.exe')); } catch (_) { return false; } } /// 启动已安装的游戏(优先管理端设置的启动程序,否则取目录中第一个exe) Future launchInstalledGame(String title, String launchExe) async { final dirPath = await gameInstallDir(title); final dir = Directory(dirPath); if (!await dir.exists()) return false; if (launchExe.isNotEmpty) { final exe = File(p.join(dirPath, launchExe)); if (await exe.exists()) { Process.run(exe.path, [], workingDirectory: p.dirname(exe.path)); return true; } } try { final exes = dir .listSync(recursive: false) .whereType() .where((f) => f.path.toLowerCase().endsWith('.exe')) .toList(); if (exes.isNotEmpty) { Process.run(exes.first.path, [], workingDirectory: dirPath); return true; } } catch (_) {} return false; } void debugLog(String msg) { if (kDebugMode) print('[gamebox] $msg'); }