From 262ce70998fa52dec3120589f51c47e96fb96df4 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Tue, 03 Nov 2026 17:04:19 +0800
Subject: [PATCH] feat: 完成游戏盒子前后端基础功能开发
---
fluttergamebox/lib/utils/util.dart | 65 ++++++++++++++++++++++++++++++++
1 files changed, 65 insertions(+), 0 deletions(-)
diff --git a/fluttergamebox/lib/utils/util.dart b/fluttergamebox/lib/utils/util.dart
new file mode 100644
index 0000000..d6d07f4
--- /dev/null
+++ b/fluttergamebox/lib/utils/util.dart
@@ -0,0 +1,65 @@
+import 'dart:io';
+import 'package:flutter/foundation.dart';
+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);
+ }
+}
+
+/// 简单格式化字节数
+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';
+}
+
+/// 秒 -> 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)}';
+}
+
+void debugLog(String msg) {
+ if (kDebugMode) print('[gamebox] $msg');
+}
--
Gitblit v1.10.0