-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathHelper.cpp
More file actions
102 lines (85 loc) · 2.82 KB
/
Copy pathHelper.cpp
File metadata and controls
102 lines (85 loc) · 2.82 KB
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
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#include "pch.h"
#include "frameworkudk\ResourceManager.h"
bool IsResourceNotFound(HRESULT hr)
{
switch (hr)
{
case HRESULT_FROM_WIN32(ERROR_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_MRM_MAP_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_MRM_NAMED_RESOURCE_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_MRM_NO_CANDIDATE):
case HRESULT_FROM_WIN32(ERROR_MRM_NO_MATCH_OR_DEFAULT_CANDIDATE):
return true;
}
return false;
}
HRESULT CheckFile(LPCWSTR filename)
{
DWORD attributes = GetFileAttributes(filename);
if (attributes == INVALID_FILE_ATTRIBUTES)
{
return HRESULT_FROM_WIN32(GetLastError());
}
if (attributes & FILE_ATTRIBUTE_DIRECTORY)
{
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
}
return S_OK;
}
HRESULT GetDefaultPriFileForCurentModule(bool isPackaged, winrt::hstring& filePath)
{
PWSTR path = nullptr;
HRESULT hr = MrmGetFilePathFromName(isPackaged ? L"resources.pri" : nullptr, &path);
if (FAILED(hr))
{
return hr;
}
string_resoure_ptr pathContainter(path);
filePath = pathContainter.get();
return S_OK;
}
HRESULT GetDefaultPriFileForCurrentPackage(winrt::hstring& filePath)
{
wchar_t* resourceFile = nullptr;
HRESULT hr = ResourceManager_GetDefaultResourcePathForPackageFullName(nullptr, &resourceFile);
if (FAILED(hr))
{
return hr;
}
std::unique_ptr<wchar_t, decltype(&LocalFree)> resourceFilePtr(resourceFile, LocalFree);
hr = CheckFile(resourceFile);
if (FAILED(hr))
{
return hr;
}
filePath = resourceFile;
return S_OK;
}
HRESULT GetDefaultPriFile(winrt::hstring& filePath)
{
HRESULT hr = GetDefaultPriFileForCurrentPackage(filePath);
if (SUCCEEDED(hr))
{
return S_OK;
}
// GetDefaultPriFileForCurrentPackage will not handle the new case where
// resources.pri is in the parent folder.
bool isPackaged = (hr != HRESULT_FROM_WIN32(APPMODEL_ERROR_NO_PACKAGE));
hr = GetDefaultPriFileForCurentModule(isPackaged, filePath);
// Sparse-packaged apps have identity but deploy PRI files as loose files; fall back to unpackaged discovery which also searches for "[modulename].pri".
if (isPackaged && IsResourceNotFound(hr))
{
HRESULT hrFallback = GetDefaultPriFileForCurentModule(false, filePath);
if (SUCCEEDED(hrFallback))
{
hr = hrFallback;
}
// If the fallback also fails, preserve the original HRESULT (not the fallback's)
// for backward compatibility so callers checking for specific errors (e.g., ERROR_FILE_NOT_FOUND) are not broken.
}
return hr;
}