905086
2026-11-09 cbb26873e88a7a13e868448c1cc578ce70b8c6ae
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
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
 
/// 获取本机机器码(注册表 MachineGuid,失败则退化为稳定哈希)
Future<String> 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<String> getDownloadPath() async {
    final sp = await SharedPreferences.getInstance();
    return sp.getString('download_path') ?? '';
  }
 
  static Future<String> getInstallPath() async {
    final sp = await SharedPreferences.getInstance();
    return sp.getString('install_path') ?? '';
  }
 
  static Future<void> setPaths(String dl, String ins) async {
    final sp = await SharedPreferences.getInstance();
    await sp.setString('download_path', dl);
    await sp.setString('install_path', ins);
  }
 
  /// 保存登录账号密码(登录成功后调用,用于下次自动填充)
  static Future<void> saveLoginCredentials(String phone, String password) async {
    final sp = await SharedPreferences.getInstance();
    await sp.setString('saved_phone', phone);
    await sp.setString('saved_password', password);
  }
 
  /// 读取上次登录成功的账号密码
  static Future<(String, String)> getLoginCredentials() async {
    final sp = await SharedPreferences.getInstance();
    return (sp.getString('saved_phone') ?? '', sp.getString('saved_password') ?? '');
  }
}
 
/// 大小展示:统一 GB 单位(纯数字自动追加 GB,兼容旧的 "82 GB" 格式)
String sizeWithGb(String s) {
  if (s.isEmpty) return '';
  final t = s.trim();
  if (RegExp(r'[a-zA-Z]').hasMatch(t)) return t; // 已带单位(GB/MB等)
  return '$t GB';
}
 
/// 解析 GB 数值("232.6"、"232.6 GB" 均可;解析失败返回 0)
double parseGb(String s) {
  if (s.isEmpty) return 0;
  final m = RegExp(r'([\d.]+)').firstMatch(s);
  if (m == null) return 0;
  return double.tryParse(m.group(1)!) ?? 0;
}
 
/// 获取磁盘剩余空间(字节)。path 形如 "D:\xxx";返回 -1 表示无法获取(如 UNC 路径)
/// 注意:内部调用 PowerShell(启动较慢),请异步调用避免阻塞 UI
Future<double> getDiskFreeBytes(String path) async {
  try {
    if (path.length < 3 || path[1] != ':') return -1;
    final drive = path[0].toUpperCase();
    final r = await Process.run('powershell', [
      '-NoProfile',
      '-Command',
      '(Get-PSDrive -Name $drive).Free'
    ]);
    final v = double.tryParse(r.stdout.toString().trim());
    return v ?? -1;
  } catch (_) {
    return -1;
  }
}
 
/// 简单格式化字节数
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<String> gameInstallDir(String title) async {
  final ins = await AppPrefs.getInstallPath();
  final base = ins.isEmpty ? 'C:\\99GameBox\\99GameI' : ins;
  return '$base\\${safeFileName(title)}';
}
 
/// 游戏是否已安装(目录存在且能找到启动程序)
Future<bool> 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<File>()
        .any((f) => f.path.toLowerCase().endsWith('.exe'));
  } catch (_) {
    return false;
  }
}
 
/// 启动已安装的游戏(优先管理端设置的启动程序,否则取目录中第一个exe)
Future<bool> 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<File>()
        .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');
}