From 3f637f4aef7eaa18e66b16d56119685140695f2f Mon Sep 17 00:00:00 2001 From: xkeyC <3334969096@qq.com> Date: Thu, 23 Nov 2023 22:30:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Affinity=20=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/common/helper/system_helper.dart | 23 ++++++++++++++++++++--- lib/ui/home/home_ui_model.dart | 5 +++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/common/helper/system_helper.dart b/lib/common/helper/system_helper.dart index 094b18e..ff13e5e 100644 --- a/lib/common/helper/system_helper.dart +++ b/lib/common/helper/system_helper.dart @@ -218,9 +218,26 @@ foreach ($adapter in $adapterMemory) { return int.tryParse(cpuNumberResult.stdout.toString().trim()) ?? 0; } - static Future getCpuAffinity(int eCoreCount) async { + static Future getCpuAffinity(int eCoreCount) async { final cpuNumber = await getNumberOfLogicalProcessors(); - if (cpuNumber == 0) return null; - return (1 << cpuNumber) - (1 << eCoreCount); + if (cpuNumber == 0 || eCoreCount == 0 || eCoreCount > cpuNumber) { + return null; + } + + StringBuffer sb = StringBuffer(); + for (var i = 0; i < cpuNumber; i++) { + if (i < eCoreCount) { + sb.write("0"); + } else { + sb.write("1"); + } + } + final binaryString = sb.toString(); + int hexDigits = (binaryString.length / 4).ceil(); + dPrint("Affinity sb ==== $sb"); + return int.parse(binaryString, radix: 2) + .toRadixString(16) + .padLeft(hexDigits, '0') + .toUpperCase(); } } diff --git a/lib/ui/home/home_ui_model.dart b/lib/ui/home/home_ui_model.dart index 1f7b4fe..c36d1db 100644 --- a/lib/ui/home/home_ui_model.dart +++ b/lib/ui/home/home_ui_model.dart @@ -527,7 +527,7 @@ class HomeUIModel extends BaseUIModel { } doLaunchGame(String launchExe, List args, String installPath, - int? processorAffinity) async { + String? processorAffinity) async { _isGameRunning[installPath] = true; notifyListeners(); try { @@ -537,13 +537,14 @@ class HomeUIModel extends BaseUIModel { dPrint('stdout: ${result.stdout}'); dPrint('stderr: ${result.stderr}'); } else { + dPrint("set Affinity === $processorAffinity "); ProcessResult result = await Process.run("cmd.exe", [ '/C', 'Start', '"StarCitizen"', '/High', '/Affinity', - '$processorAffinity', + processorAffinity, launchExe, ...args ]);