Refactor AI handling and improve code organization

- Removed ARM64 architecture check in `CheckAiAvailability`
  to simplify AI availability logic.
- Centralized JSON serialization settings in
  `AiAvailabilityCacheService.cs` using `SerializerOptions`.
- Refactored JSON serialization to use the new options for
  consistency and maintainability.
- Moved `AiCapabilityCache` to its own file, `AiCapabilityCache.cs`,
  with a copyright header for better organization.
- Updated `ImageResizer.exe` path in `main.cpp` to reflect its
  new location in the `WinUI3Apps` folder.
- Added `ai_detection.h` to `runner.vcxproj` and its filter file
  to support new AI detection functionality.
This commit is contained in:
Yu Leng
2025-11-28 17:06:45 +08:00
parent 4ab5d4eaa5
commit adc5a43fd4
6 changed files with 34 additions and 28 deletions

View File

@@ -156,12 +156,6 @@ namespace ImageResizer
{
try
{
// First check if the platform is ARM64 - AI Super Resolution only works on ARM64
if (RuntimeInformation.ProcessArchitecture != Architecture.Arm64)
{
return AiAvailabilityState.NotSupported;
}
// Check Windows AI service model ready state
// it's so slow, why?
var readyState = Services.WinAiSuperResolutionService.GetModelReadyState();

View File

@@ -22,6 +22,11 @@ namespace ImageResizer.Services
private const string CacheFileName = "ai_capabilities.json";
private const int CacheVersion = 1;
private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
};
private static string CachePath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Microsoft",
@@ -82,10 +87,7 @@ namespace ImageResizer.Services
Directory.CreateDirectory(dir);
}
var json = JsonSerializer.Serialize(cache, new JsonSerializerOptions
{
WriteIndented = true,
});
var json = JsonSerializer.Serialize(cache, SerializerOptions);
File.WriteAllText(CachePath, json);
Logger.LogInfo($"AI cache saved: {state}");
@@ -120,20 +122,4 @@ namespace ImageResizer.Services
return true;
}
}
/// <summary>
/// Data model for AI capability cache file.
/// </summary>
internal sealed class AiCapabilityCache
{
public int Version { get; set; }
public int State { get; set; }
public string WindowsBuild { get; set; }
public string Architecture { get; set; }
public string Timestamp { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ImageResizer.Services
{
/// <summary>
/// Data model for AI capability cache file.
/// </summary>
internal sealed class AiCapabilityCache
{
public int Version { get; set; }
public int State { get; set; }
public string WindowsBuild { get; set; }
public string Architecture { get; set; }
public string Timestamp { get; set; }
}
}

View File

@@ -105,9 +105,9 @@ void DetectAiCapabilitiesAsync(bool skipSettingsCheck)
}
}
// Get ImageResizer.exe path
// Get ImageResizer.exe path (located in WinUI3Apps folder)
std::wstring imageResizerPath = get_module_folderpath();
imageResizerPath += L"\\modules\\ImageResizer\\ImageResizer.exe";
imageResizerPath += L"\\WinUI3Apps\\PowerToys.ImageResizer.exe";
if (!std::filesystem::exists(imageResizerPath))
{

View File

@@ -77,6 +77,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="ActionRunnerUtils.h" />
<ClInclude Include="ai_detection.h" />
<ClInclude Include="auto_start_helper.h" />
<ClInclude Include="bug_report.h" />
<ClInclude Include="centralized_hotkeys.h" />

View File

@@ -81,6 +81,9 @@
<ClInclude Include="ActionRunnerUtils.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="ai_detection.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>Utils</Filter>
</ClInclude>