Skip to content

Commit

Permalink
Make Environment singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
CrendKing committed Apr 21, 2021
1 parent 781d475 commit 9261c7b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 58 deletions.
32 changes: 14 additions & 18 deletions src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ static constexpr const WCHAR *SELECTED_ITEM_OBJECT_NAME = L"Global\\CompareMenuS

static std::array<WCHAR, MAX_PATH> g_quotedPathBuffer {};

std::wstring Environment::modulePath;
std::wstring Environment::comparerPath;
std::wstring Environment::comparerArgs;
Item Environment::selectedItem;
HANDLE Environment::_hMapFile = nullptr;
LPWSTR Environment::_mappingBuffer = nullptr;
Environment::Environment() {
modulePath.resize(MAX_PATH);
}

Environment::~Environment() {
if (_mappingBuffer != nullptr) {
UnmapViewOfFile(_mappingBuffer);
}
if (_hMapFile != nullptr) {
CloseHandle(_hMapFile);
}
}

auto Environment::Initialize(HINSTANCE hInstance) -> bool {
modulePath.resize(MAX_PATH);
if (GetModuleFileNameW(hInstance, modulePath.data(), static_cast<DWORD>(modulePath.size())) == 0) {
return false;
}
Expand All @@ -35,16 +40,7 @@ auto Environment::Initialize(HINSTANCE hInstance) -> bool {
return true;
}

auto Environment::Destroy() -> void {
if (_mappingBuffer != nullptr) {
UnmapViewOfFile(_mappingBuffer);
}
if (_hMapFile != nullptr) {
CloseHandle(_hMapFile);
}
}

auto Environment::QuotePath(PCWSTR path) -> const WCHAR * {
auto Environment::QuotePath(PCWSTR path) const -> const WCHAR * {
if (path == nullptr) {
return path;
}
Expand All @@ -61,7 +57,7 @@ auto Environment::LoadSelectedItem() -> void {
}
}

auto Environment::FlushSelectedItem() -> void {
auto Environment::FlushSelectedItem() const -> void {
if (selectedItem.name.empty()) {
CopyMemory(_mappingBuffer, L"\0", sizeof(WCHAR));
} else {
Expand Down
26 changes: 13 additions & 13 deletions src/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@


struct Environment {
static auto Initialize(HINSTANCE hInstance) -> bool;
static auto Destroy() -> void;
static inline std::unique_ptr<Environment> INSTANCE;

static auto QuotePath(PCWSTR path) -> const WCHAR *;
static auto LoadSelectedItem() -> void;
static auto FlushSelectedItem() -> void;
Environment();
~Environment();

static constexpr const WCHAR *COMPARER_ARGS_LEFT_PLACEHOLDER = L"%LEFT%";
static constexpr const WCHAR *COMPARER_ARGS_RIGHT_PLACEHOLDER = L"%RIGHT%";
auto Initialize(HINSTANCE hInstance) -> bool;
auto QuotePath(PCWSTR path) const -> const WCHAR *;
auto LoadSelectedItem() -> void;
auto FlushSelectedItem() const -> void;

static std::wstring modulePath;
static std::wstring comparerPath;
static std::wstring comparerArgs;
std::wstring modulePath;
std::wstring comparerPath;
std::wstring comparerArgs;

static Item selectedItem;
Item selectedItem;

private:
static HANDLE _hMapFile;
static LPWSTR _mappingBuffer;
HANDLE _hMapFile = nullptr;
LPWSTR _mappingBuffer = nullptr;
};
14 changes: 7 additions & 7 deletions src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ auto STDMETHODCALLTYPE CCompareMenu::Initialize(__RPC__in_string LPCWSTR pszComm
ATL::CComVariant variant;

if (SUCCEEDED(ppb->Read(REGISTRY_COMPARER_PATH_VALUE_NAME, &variant, nullptr))) {
Environment::comparerPath = variant.bstrVal;
Environment::INSTANCE->comparerPath = variant.bstrVal;
} else {
Environment::comparerPath.clear();
Environment::INSTANCE->comparerPath.clear();
}

if (SUCCEEDED(ppb->Read(REGISTRY_COMPARER_ARGS_VALUE_NAME, &variant, nullptr))) {
Environment::comparerArgs = variant.bstrVal;
Environment::INSTANCE->comparerArgs = variant.bstrVal;
} else {
Environment::comparerArgs.clear();
Environment::INSTANCE->comparerArgs.clear();
}

Environment::LoadSelectedItem();
Environment::INSTANCE->LoadSelectedItem();

return S_OK;
}
Expand All @@ -34,7 +34,7 @@ auto STDMETHODCALLTYPE CCompareMenu::GetTitle(__RPC__in_opt IShellItemArray *psi
}

auto STDMETHODCALLTYPE CCompareMenu::GetIcon(__RPC__in_opt IShellItemArray *psiItemArray, __RPC__deref_out_opt_string LPWSTR *ppszIcon) -> HRESULT {
return SHStrDupW((Environment::modulePath + L",0").c_str(), ppszIcon);
return SHStrDupW((Environment::INSTANCE->modulePath + L",0").c_str(), ppszIcon);
}

auto STDMETHODCALLTYPE CCompareMenu::GetToolTip(__RPC__in_opt IShellItemArray *psiItemArray, __RPC__deref_out_opt_string LPWSTR *ppszInfotip) -> HRESULT {
Expand All @@ -46,7 +46,7 @@ auto STDMETHODCALLTYPE CCompareMenu::GetCanonicalName(__RPC__out GUID *pguidComm
}

auto STDMETHODCALLTYPE CCompareMenu::GetState(__RPC__in_opt IShellItemArray *psiItemArray, BOOL fOkToBeSlow, __RPC__out EXPCMDSTATE *pCmdState) -> HRESULT {
*pCmdState = Environment::comparerPath.empty() || Environment::comparerArgs.empty() ? ECS_DISABLED : ECS_ENABLED;
*pCmdState = Environment::INSTANCE->comparerPath.empty() || Environment::INSTANCE->comparerArgs.empty() ? ECS_DISABLED : ECS_ENABLED;
return S_OK;
}

Expand Down
9 changes: 6 additions & 3 deletions src/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ class DiffMenuModule : public ATL::CAtlDllModuleT<DiffMenuModule> {
} g_module;

extern "C" auto WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) -> BOOL {
if (dwReason == DLL_PROCESS_ATTACH && !Environment::Initialize(hInstance)) {
return FALSE;
if (dwReason == DLL_PROCESS_ATTACH) {
Environment::INSTANCE->INSTANCE = std::make_unique<Environment>();
if (!Environment::INSTANCE->INSTANCE->Initialize(hInstance)) {
return FALSE;
}
}
if (dwReason == DLL_PROCESS_DETACH) {
Environment::Destroy();
Environment::INSTANCE->INSTANCE.reset();
}

return g_module.DllMain(dwReason, lpReserved);
Expand Down
37 changes: 20 additions & 17 deletions src/sub_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "environment.h"


static constexpr const WCHAR *COMPARER_ARGS_LEFT_PLACEHOLDER = L"%LEFT%";
static constexpr const WCHAR *COMPARER_ARGS_RIGHT_PLACEHOLDER = L"%RIGHT%";

#define CheckHr(expr) { hr = (expr); if (FAILED(hr)) { return hr; } }

auto STDMETHODCALLTYPE CSubCommand::GetToolTip(__RPC__in_opt IShellItemArray *psiItemArray, __RPC__deref_out_opt_string LPWSTR *ppszInfotip) -> HRESULT {
Expand All @@ -25,12 +28,12 @@ auto STDMETHODCALLTYPE CSubCommand::EnumSubCommands(__RPC__deref_out_opt IEnumEx
auto STDMETHODCALLTYPE CInfoSubCommand::GetTitle(__RPC__in_opt IShellItemArray *psiItemArray, __RPC__deref_out_opt_string LPWSTR *ppszName) -> HRESULT {
std::wstring infoTitle;

if (Environment::selectedItem.name.empty()) {
if (Environment::INSTANCE->selectedItem.name.empty()) {
infoTitle = L"None selected";
} else {
infoTitle.append(Environment::selectedItem.name).append(L" (");
infoTitle.append(Environment::INSTANCE->selectedItem.name).append(L" (");

if (Environment::selectedItem.isFolder) {
if (Environment::INSTANCE->selectedItem.isFolder) {
infoTitle += L"Folder)";
} else {
infoTitle += L"File)";
Expand Down Expand Up @@ -69,7 +72,7 @@ auto STDMETHODCALLTYPE CSelectSubCommand::GetTitle(__RPC__in_opt IShellItemArray
}

auto STDMETHODCALLTYPE CSelectSubCommand::GetIcon(__RPC__in_opt IShellItemArray *psiItemArray, __RPC__deref_out_opt_string LPWSTR *ppszIcon) -> HRESULT {
return SHStrDupW((Environment::modulePath + L",0").c_str(), ppszIcon);
return SHStrDupW((Environment::INSTANCE->modulePath + L",0").c_str(), ppszIcon);
}

auto STDMETHODCALLTYPE CSelectSubCommand::GetState(__RPC__in_opt IShellItemArray *psiItemArray, BOOL fOkToBeSlow, __RPC__out EXPCMDSTATE *pCmdState) -> HRESULT {
Expand All @@ -80,13 +83,13 @@ auto STDMETHODCALLTYPE CSelectSubCommand::GetState(__RPC__in_opt IShellItemArray

if (itemCount != 1) {
*pCmdState = ECS_HIDDEN;
} else if (Environment::selectedItem.name.empty()) {
} else if (Environment::INSTANCE->selectedItem.name.empty()) {
*pCmdState = ECS_ENABLED;
} else {
Item currItem;
CheckHr(currItem.ExtractItemAt(psiItemArray, 0));

*pCmdState = currItem.name == Environment::selectedItem.name ? ECS_HIDDEN : ECS_ENABLED;
*pCmdState = currItem.name == Environment::INSTANCE->selectedItem.name ? ECS_HIDDEN : ECS_ENABLED;
}

return S_OK;
Expand All @@ -102,8 +105,8 @@ auto STDMETHODCALLTYPE CSelectSubCommand::Invoke(__RPC__in_opt IShellItemArray *
return E_UNEXPECTED;
}

CheckHr(Environment::selectedItem.ExtractItemAt(psiItemArray, 0));
Environment::FlushSelectedItem();
CheckHr(Environment::INSTANCE->selectedItem.ExtractItemAt(psiItemArray, 0));
Environment::INSTANCE->FlushSelectedItem();

return S_OK;
}
Expand All @@ -120,11 +123,11 @@ auto CDiffSubCommand::ExtractItems(IShellItemArray *psiItemArray, Item &firstIte
CheckHr(secondItem.ExtractItemAt(psiItemArray, 0));

if (itemCount == 1) {
if (Environment::selectedItem.name.empty()) {
if (Environment::INSTANCE->selectedItem.name.empty()) {
return S_FALSE;
}

firstItem = Environment::selectedItem;
firstItem = Environment::INSTANCE->selectedItem;
} else {
CheckHr(firstItem.ExtractItemAt(psiItemArray, 1));
}
Expand All @@ -137,7 +140,7 @@ auto STDMETHODCALLTYPE CDiffSubCommand::GetTitle(__RPC__in_opt IShellItemArray *
}

auto CDiffSubCommand::GetIcon(__RPC__in_opt IShellItemArray *psiItemArray, __RPC__deref_out_opt_string LPWSTR *ppszIcon) -> HRESULT {
return SHStrDupW((Environment::comparerPath + L",0").c_str(), ppszIcon);
return SHStrDupW((Environment::INSTANCE->comparerPath + L",0").c_str(), ppszIcon);
}

auto STDMETHODCALLTYPE CDiffSubCommand::GetState(__RPC__in_opt IShellItemArray *psiItemArray, BOOL fOkToBeSlow, __RPC__out EXPCMDSTATE *pCmdState) -> HRESULT {
Expand Down Expand Up @@ -169,21 +172,21 @@ auto STDMETHODCALLTYPE CDiffSubCommand::Invoke(__RPC__in_opt IShellItemArray *ps
return E_UNEXPECTED;
}

std::wstring cmdline = Environment::comparerArgs;
ReplaceSubString(cmdline, Environment::COMPARER_ARGS_LEFT_PLACEHOLDER, Environment::QuotePath(firstItem.name.c_str()));
ReplaceSubString(cmdline, Environment::COMPARER_ARGS_RIGHT_PLACEHOLDER, Environment::QuotePath(secondItem.name.c_str()));
std::wstring cmdline = Environment::INSTANCE->comparerArgs;
ReplaceSubString(cmdline, COMPARER_ARGS_LEFT_PLACEHOLDER, Environment::INSTANCE->QuotePath(firstItem.name.c_str()));
ReplaceSubString(cmdline, COMPARER_ARGS_RIGHT_PLACEHOLDER, Environment::INSTANCE->QuotePath(secondItem.name.c_str()));

cmdline.insert(0, L" ");
cmdline.insert(0, Environment::comparerPath);
cmdline.insert(0, Environment::INSTANCE->comparerPath);

STARTUPINFOW si = { .cb = sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessW(nullptr, cmdline.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) {
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);

Environment::selectedItem.name.clear();
Environment::FlushSelectedItem();
Environment::INSTANCE->selectedItem.name.clear();
Environment::INSTANCE->FlushSelectedItem();

return S_OK;
}
Expand Down

0 comments on commit 9261c7b

Please sign in to comment.