Skip to content

Commit cd670e7

Browse files
authored
Merge pull request #18 from Flow-Launcher/fix_load_uwp
Fix UWP apps loading
2 parents b10414f + 374e1f6 commit cd670e7

File tree

4 files changed

+78
-48
lines changed

4 files changed

+78
-48
lines changed

Flow.Launcher.Test/Flow.Launcher.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41+
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Program\Flow.Launcher.Plugin.Program.csproj" />
4142
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Url\Flow.Launcher.Plugin.Url.csproj" />
4243
<ProjectReference Include="..\Flow.Launcher.Core\Flow.Launcher.Core.csproj" />
4344
<ProjectReference Include="..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Flow.Launcher.Plugin.Program.Programs;
2+
using NUnit.Framework;
3+
using System;
4+
using Windows.ApplicationModel;
5+
6+
namespace Flow.Launcher.Test.Plugins
7+
{
8+
[TestFixture]
9+
public class ProgramTest
10+
{
11+
[TestCase("Microsoft.WindowsCamera", "ms-resource:LensSDK/Resources/AppTitle", "ms-resource://Microsoft.WindowsCamera/LensSDK/Resources/AppTitle")]
12+
[TestCase("microsoft.windowscommunicationsapps", "ms-resource://microsoft.windowscommunicationsapps/hxoutlookintl/AppManifest_MailDesktop_DisplayName",
13+
"ms-resource://microsoft.windowscommunicationsapps/hxoutlookintl/AppManifest_MailDesktop_DisplayName")]
14+
[TestCase("windows.immersivecontrolpanel", "ms-resource:DisplayName", "ms-resource://windows.immersivecontrolpanel/Resources/DisplayName")]
15+
[TestCase("Microsoft.MSPaint", "ms-resource:AppName", "ms-resource://Microsoft.MSPaint/Resources/AppName")]
16+
public void WhenGivenPriReferenceValueShouldReturnCorrectFormat(string packageName, string rawPriReferenceValue, string expectedFormat)
17+
{
18+
// Arrange
19+
var app = new UWP.Application();
20+
21+
// Act
22+
var result = app.FormattedPriReferenceValue(packageName, rawPriReferenceValue);
23+
24+
// Assert
25+
Assert.IsTrue(result == expectedFormat,
26+
$"Expected Pri reference format: {expectedFormat}{Environment.NewLine} " +
27+
$"Actual: {result}{Environment.NewLine}");
28+
}
29+
}
30+
}

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ public class Application : IProgram
263263
public string LogoPath { get; set; }
264264
public UWP Package { get; set; }
265265

266+
public Application(){}
267+
266268
private int Score(string query)
267269
{
268270
var displayNameMatch = StringMatcher.FuzzySearch(query, DisplayName);
@@ -363,73 +365,70 @@ public Application(IAppxManifestApplication manifestApp, UWP package)
363365
BackgroundColor = manifestApp.GetStringValue("BackgroundColor");
364366
Package = package;
365367

366-
DisplayName = ResourceFromPri(package.FullName, DisplayName);
367-
Description = ResourceFromPri(package.FullName, Description);
368+
DisplayName = ResourceFromPri(package.FullName, package.Name, DisplayName);
369+
Description = ResourceFromPri(package.FullName, package.Name, Description);
368370
LogoUri = LogoUriFromManifest(manifestApp);
369371
LogoPath = LogoPathFromUri(LogoUri);
370372

371373
Enabled = true;
372374
}
373375

374-
internal string ResourceFromPri(string packageFullName, string resourceReference)
376+
internal string ResourceFromPri(string packageFullName, string packageName, string rawReferenceValue)
375377
{
376-
const string prefix = "ms-resource:";
377-
if (!string.IsNullOrWhiteSpace(resourceReference) && resourceReference.StartsWith(prefix))
378-
{
379-
// magic comes from @talynone
380-
// https://github.com/talynone/Wox.Plugin.WindowsUniversalAppLauncher/blob/master/StoreAppLauncher/Helpers/NativeApiHelper.cs#L139-L153
381-
string key = resourceReference.Substring(prefix.Length);
382-
string parsed;
383-
if (key.StartsWith("//"))
384-
{
385-
parsed = prefix + key;
386-
}
387-
else if (key.StartsWith("/"))
388-
{
389-
parsed = prefix + "//" + key;
390-
}
391-
else
392-
{
393-
parsed = prefix + "///resources/" + key;
394-
}
378+
if (string.IsNullOrWhiteSpace(rawReferenceValue) || !rawReferenceValue.StartsWith("ms-resource:"))
379+
return rawReferenceValue;
395380

396-
var outBuffer = new StringBuilder(128);
397-
string source = $"@{{{packageFullName}? {parsed}}}";
398-
var capacity = (uint)outBuffer.Capacity;
399-
var hResult = SHLoadIndirectString(source, outBuffer, capacity, IntPtr.Zero);
400-
if (hResult == Hresult.Ok)
381+
var formattedPriReference = FormattedPriReferenceValue(packageName, rawReferenceValue);
382+
383+
var outBuffer = new StringBuilder(128);
384+
string source = $"@{{{packageFullName}? {formattedPriReference}}}";
385+
var capacity = (uint)outBuffer.Capacity;
386+
var hResult = SHLoadIndirectString(source, outBuffer, capacity, IntPtr.Zero);
387+
if (hResult == Hresult.Ok)
388+
{
389+
var loaded = outBuffer.ToString();
390+
if (!string.IsNullOrEmpty(loaded))
401391
{
402-
var loaded = outBuffer.ToString();
403-
if (!string.IsNullOrEmpty(loaded))
404-
{
405-
return loaded;
406-
}
407-
else
408-
{
409-
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Can't load null or empty result "
410-
+ $"pri {source} in uwp location {Package.Location}", new NullReferenceException());
411-
return string.Empty;
412-
}
392+
return loaded;
413393
}
414394
else
415395
{
416-
// https://github.com/Wox-launcher/Wox/issues/964
417-
// known hresult 2147942522:
418-
// 'Microsoft Corporation' violates pattern constraint of '\bms-resource:.{1,256}'.
419-
// for
420-
// Microsoft.MicrosoftOfficeHub_17.7608.23501.0_x64__8wekyb3d8bbwe: ms-resource://Microsoft.MicrosoftOfficeHub/officehubintl/AppManifest_GetOffice_Description
421-
// Microsoft.BingFoodAndDrink_3.0.4.336_x64__8wekyb3d8bbwe: ms-resource:AppDescription
422-
var e = Marshal.GetExceptionForHR((int)hResult);
423-
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Load pri failed {source} with HResult {hResult} and location {Package.Location}", e);
396+
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Can't load null or empty result "
397+
+ $"pri {source} in uwp location {Package.Location}", new NullReferenceException());
424398
return string.Empty;
425399
}
426400
}
427401
else
428402
{
429-
return resourceReference;
403+
var e = Marshal.GetExceptionForHR((int)hResult);
404+
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Load pri failed {source} with HResult {hResult} and location {Package.Location}", e);
405+
return string.Empty;
430406
}
431407
}
432408

409+
public string FormattedPriReferenceValue(string packageName, string rawPriReferenceValue)
410+
{
411+
const string prefix = "ms-resource:";
412+
413+
if (string.IsNullOrWhiteSpace(rawPriReferenceValue) || !rawPriReferenceValue.StartsWith(prefix))
414+
return rawPriReferenceValue;
415+
416+
string key = rawPriReferenceValue.Substring(prefix.Length);
417+
if (key.StartsWith("//"))
418+
return $"{prefix}{key}";
419+
420+
if (!key.StartsWith("/"))
421+
{
422+
key = $"/{key}";
423+
}
424+
425+
if (!key.ToLower().Contains("resources"))
426+
{
427+
key = $"/Resources{key}";
428+
}
429+
430+
return $"{prefix}//{packageName}{key}";
431+
}
433432

434433
internal string LogoUriFromManifest(IAppxManifestApplication app)
435434
{

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private static ParallelQuery<Win32> UnregisteredPrograms(List<Settings.ProgramSo
342342
var paths = listToAdd.Distinct().ToArray();
343343

344344
var programs1 = paths.AsParallel().Where(p => Extension(p) == ExeExtension).Select(ExeProgram);
345-
var programs2 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(ExeProgram);
345+
var programs2 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
346346
var programs3 = from p in paths.AsParallel()
347347
let e = Extension(p)
348348
where e != ShortcutExtension && e != ExeExtension

0 commit comments

Comments
 (0)