Skip to content

Commit 7284f17

Browse files
lostindarkzufuliu
authored andcommitted
Use %LOCALAPPDATA% to store INI file for WinGet or Chocolatey installation, PR #1069.
1 parent 6dbfaea commit 7284f17

File tree

2 files changed

+80
-16
lines changed

2 files changed

+80
-16
lines changed

matepath/src/matepath.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,6 +3048,8 @@ enum class IniFindStatus {
30483048
Found = 1,
30493049
Relative = 2,
30503050
Specified = 4,
3051+
Program = 8,
3052+
AppData = 16,
30513053
};
30523054

30533055
constexpr IniFindStatus operator|(IniFindStatus status, IniFindStatus value) noexcept {
@@ -3057,12 +3059,18 @@ constexpr IniFindStatus operator|(IniFindStatus status, IniFindStatus value) noe
30573059
struct IniFinder {
30583060
LPCWSTR tchModule;
30593061
size_t nameIndex;
3062+
bool portable = true;
30603063
LPWSTR folder[3]{};
30613064
const KNOWNFOLDERID* rfidList[3]{};
30623065

30633066
IniFinder() noexcept {
30643067
tchModule = szExeRealPath;
30653068
nameIndex = PathFindFileName(tchModule) - tchModule;
3069+
if (StrStrI(tchModule, L"WinGet") != nullptr || StrStrI(tchModule, L"Chocolatey") != nullptr) {
3070+
// %LOCALAPPDATA%\Microsoft\WinGet\Packages
3071+
// ChocolateyInstall
3072+
portable = false;
3073+
}
30663074
}
30673075
~IniFinder() {
30683076
for (UINT i = 0; i < COUNTOF(folder); i++) {
@@ -3080,11 +3088,13 @@ IniFindStatus IniFinder::CheckIniFile(LPWSTR lpszFile) noexcept {
30803088
if (PathIsRelative(lpszFile)) {
30813089
WCHAR tchBuild[MAX_PATH];
30823090
// program directory
3083-
lstrcpy(tchBuild, tchModule);
3084-
lstrcpy(&tchBuild[nameIndex], lpszFile);
3085-
if (PathIsFile(tchBuild)) {
3086-
lstrcpy(lpszFile, tchBuild);
3087-
return IniFindStatus::Found;
3091+
if (portable) {
3092+
lstrcpy(tchBuild, tchModule);
3093+
lstrcpy(&tchBuild[nameIndex], lpszFile);
3094+
if (PathIsFile(tchBuild)) {
3095+
lstrcpy(lpszFile, tchBuild);
3096+
return IniFindStatus::Found | IniFindStatus::Program;
3097+
}
30883098
}
30893099

30903100
if (rfidList[0] == nullptr) {
@@ -3108,10 +3118,18 @@ IniFindStatus IniFinder::CheckIniFile(LPWSTR lpszFile) noexcept {
31083118
PathAppend(tchBuild, lpszFile);
31093119
if (PathIsFile(tchBuild)) {
31103120
lstrcpy(lpszFile, tchBuild);
3111-
return IniFindStatus::Found;
3121+
return IniFindStatus::Found | IniFindStatus::AppData;
31123122
}
31133123
}
31143124
}
3125+
if (!portable) {
3126+
lstrcpy(tchBuild, tchModule);
3127+
lstrcpy(&tchBuild[nameIndex], lpszFile);
3128+
if (PathIsFile(tchBuild)) {
3129+
lstrcpy(lpszFile, tchBuild);
3130+
return IniFindStatus::Found | IniFindStatus::Program;
3131+
}
3132+
}
31153133
return IniFindStatus::Relative;
31163134
}
31173135
if (PathIsFile(lpszFile)) {
@@ -3150,6 +3168,15 @@ void FindIniFile() noexcept {
31503168

31513169
if (FlagSet(status, IniFindStatus::Found)) {
31523170
WCHAR tchTmp[MAX_PATH];
3171+
if (FlagSet(status, IniFindStatus::Program) && !finder.portable && finder.folder[0] != nullptr) {
3172+
// copy ini to %LOCALAPPDATA%\Notepad4
3173+
PathCombine(tchTmp, finder.folder[0], WC_NOTEPAD4);
3174+
SHCreateDirectoryEx(nullptr, tchTmp, nullptr);
3175+
PathAppend(tchTmp, &tchTest[finder.nameIndex]);
3176+
CopyFile(tchTest, tchTmp, TRUE);
3177+
// always use %LOCALAPPDATA% for non-portable installation
3178+
lstrcpy(tchTest, tchTmp);
3179+
}
31533180
// check ini redirection
31543181
if (GetPrivateProfileString(INI_SECTION_NAME_MATEPATH, L"matepath.ini", L"", tchTmp, COUNTOF(tchTmp), tchTest)) {
31553182
if (!ExpandEnvironmentStringsEx(tchTmp, tchTest)) {
@@ -3171,8 +3198,13 @@ void FindIniFile() noexcept {
31713198
relative = FlagSet(status, IniFindStatus::Relative);
31723199
}
31733200
if (relative) {
3174-
lstrcpy(tchTest, finder.tchModule);
3175-
lstrcpy(&tchTest[finder.nameIndex], pszFile);
3201+
if (!finder.portable && finder.folder[0] != nullptr) {
3202+
PathCombine(tchTest, finder.folder[0], WC_NOTEPAD4);
3203+
PathAppend(tchTest, pszFile);
3204+
} else {
3205+
lstrcpy(tchTest, finder.tchModule);
3206+
lstrcpy(&tchTest[finder.nameIndex], pszFile);
3207+
}
31763208
PathRenameExtension(tchTest, L".ini");
31773209
pszFile = tchTest;
31783210
if (PathIsFile(tchTest)) {

src/Notepad4.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6404,6 +6404,8 @@ enum class IniFindStatus {
64046404
Found = 1,
64056405
Relative = 2,
64066406
Specified = 4,
6407+
Program = 8,
6408+
AppData = 16,
64076409
};
64086410

64096411
constexpr IniFindStatus operator|(IniFindStatus status, IniFindStatus value) noexcept {
@@ -6413,12 +6415,18 @@ constexpr IniFindStatus operator|(IniFindStatus status, IniFindStatus value) noe
64136415
struct IniFinder {
64146416
LPCWSTR tchModule;
64156417
size_t nameIndex;
6418+
bool portable = true;
64166419
LPWSTR folder[3]{};
64176420
const KNOWNFOLDERID* rfidList[3]{};
64186421

64196422
IniFinder() noexcept {
64206423
tchModule = szExeRealPath;
64216424
nameIndex = PathFindFileName(tchModule) - tchModule;
6425+
if (StrStrI(tchModule, L"WinGet") != nullptr || StrStrI(tchModule, L"Chocolatey") != nullptr) {
6426+
// %LOCALAPPDATA%\Microsoft\WinGet\Packages
6427+
// ChocolateyInstall
6428+
portable = false;
6429+
}
64226430
}
64236431
~IniFinder() {
64246432
for (UINT i = 0; i < COUNTOF(folder); i++) {
@@ -6436,11 +6444,13 @@ IniFindStatus IniFinder::CheckIniFile(LPWSTR lpszFile) noexcept {
64366444
if (PathIsRelative(lpszFile)) {
64376445
WCHAR tchBuild[MAX_PATH];
64386446
// program directory
6439-
lstrcpy(tchBuild, tchModule);
6440-
lstrcpy(&tchBuild[nameIndex], lpszFile);
6441-
if (PathIsFile(tchBuild)) {
6442-
lstrcpy(lpszFile, tchBuild);
6443-
return IniFindStatus::Found;
6447+
if (portable) {
6448+
lstrcpy(tchBuild, tchModule);
6449+
lstrcpy(&tchBuild[nameIndex], lpszFile);
6450+
if (PathIsFile(tchBuild)) {
6451+
lstrcpy(lpszFile, tchBuild);
6452+
return IniFindStatus::Found | IniFindStatus::Program;
6453+
}
64446454
}
64456455

64466456
if (rfidList[0] == nullptr) {
@@ -6464,10 +6474,18 @@ IniFindStatus IniFinder::CheckIniFile(LPWSTR lpszFile) noexcept {
64646474
PathAppend(tchBuild, lpszFile);
64656475
if (PathIsFile(tchBuild)) {
64666476
lstrcpy(lpszFile, tchBuild);
6467-
return IniFindStatus::Found;
6477+
return IniFindStatus::Found | IniFindStatus::AppData;
64686478
}
64696479
}
64706480
}
6481+
if (!portable) {
6482+
lstrcpy(tchBuild, tchModule);
6483+
lstrcpy(&tchBuild[nameIndex], lpszFile);
6484+
if (PathIsFile(tchBuild)) {
6485+
lstrcpy(lpszFile, tchBuild);
6486+
return IniFindStatus::Found | IniFindStatus::Program;
6487+
}
6488+
}
64716489
return IniFindStatus::Relative;
64726490
}
64736491
if (PathIsFile(lpszFile)) {
@@ -6506,6 +6524,15 @@ void FindIniFile() noexcept {
65066524

65076525
if (FlagSet(status, IniFindStatus::Found)) {
65086526
WCHAR tchTmp[MAX_PATH];
6527+
if (FlagSet(status, IniFindStatus::Program) && !finder.portable && finder.folder[0] != nullptr) {
6528+
// copy ini to %LOCALAPPDATA%\Notepad4
6529+
PathCombine(tchTmp, finder.folder[0], WC_NOTEPAD4);
6530+
SHCreateDirectoryEx(nullptr, tchTmp, nullptr);
6531+
PathAppend(tchTmp, &tchTest[finder.nameIndex]);
6532+
CopyFile(tchTest, tchTmp, TRUE);
6533+
// always use %LOCALAPPDATA% for non-portable installation
6534+
lstrcpy(tchTest, tchTmp);
6535+
}
65096536
// check ini redirection
65106537
if (GetPrivateProfileString(INI_SECTION_NAME_NOTEPAD4, L"Notepad4.ini", L"", tchTmp, COUNTOF(tchTmp), tchTest)) {
65116538
if (!ExpandEnvironmentStringsEx(tchTmp, tchTest)) {
@@ -6527,8 +6554,13 @@ void FindIniFile() noexcept {
65276554
relative = FlagSet(status, IniFindStatus::Relative);
65286555
}
65296556
if (relative) {
6530-
lstrcpy(tchTest, finder.tchModule);
6531-
lstrcpy(&tchTest[finder.nameIndex], pszFile);
6557+
if (!finder.portable && finder.folder[0] != nullptr) {
6558+
PathCombine(tchTest, finder.folder[0], WC_NOTEPAD4);
6559+
PathAppend(tchTest, pszFile);
6560+
} else {
6561+
lstrcpy(tchTest, finder.tchModule);
6562+
lstrcpy(&tchTest[finder.nameIndex], pszFile);
6563+
}
65326564
PathRenameExtension(tchTest, L".ini");
65336565
pszFile = tchTest;
65346566
if (PathIsFile(tchTest)) {

0 commit comments

Comments
 (0)