Skip to content

Commit

Permalink
wzpropertyproviders: Additional property support
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Oct 17, 2023
1 parent 19cadbe commit 05048ff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/wzpropertyproviders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
#include <psapi.h>
#endif

// Includes for Unix
#if defined(WZ_OS_UNIX)
#include <unistd.h>
#include <cstdlib>
#endif

// MARK: - BuildPropertyProvider

enum class BuildProperty {
Expand Down Expand Up @@ -160,6 +166,64 @@ static std::string Get_WinPackageFullName()
}
#endif

enum class WZ_CONTAINER_TYPE
{
NONE_DETECTED,
FLATPAK,
SNAP,
APPIMAGE,
WIN_PACKAGE // ex. MSIX package
};

static std::string to_string(const WZ_CONTAINER_TYPE& type)
{
switch (type)
{
case WZ_CONTAINER_TYPE::NONE_DETECTED:
return "";
case WZ_CONTAINER_TYPE::FLATPAK:
return "flatpak";
case WZ_CONTAINER_TYPE::SNAP:
return "snap";
case WZ_CONTAINER_TYPE::APPIMAGE:
return "appimage";
case WZ_CONTAINER_TYPE::WIN_PACKAGE:
return "winpackage";
}
return ""; // silence warning
}

static WZ_CONTAINER_TYPE GetCurrentContainerType()
{
#if defined(WZ_OS_WIN)
if (!Get_WinPackageFullName().empty())
{
return WZ_CONTAINER_TYPE::WIN_PACKAGE;
}
#elif defined(WZ_OS_UNIX)
if (access("/.flatpak-info", F_OK) == 0)
{
return WZ_CONTAINER_TYPE::FLATPAK;
}
// Check multiple variables, as is done by both WebKit and SDL
// See: https://snapcraft.io/docs/environment-variables
if (std::getenv("SNAP") && std::getenv("SNAP_NAME") && std::getenv("SNAP_REVISION"))
{
return WZ_CONTAINER_TYPE::SNAP;
}
if (std::getenv("APPIMAGE"))
{
return WZ_CONTAINER_TYPE::APPIMAGE;
}
#endif
return WZ_CONTAINER_TYPE::NONE_DETECTED;
}

static std::string GetCurrentContainerTypeStr()
{
return to_string(GetCurrentContainerType());
}

static std::string GetCurrentBuildPropertyValue(const BuildProperty& property)
{
using BP = BuildProperty;
Expand Down Expand Up @@ -251,6 +315,7 @@ static const std::unordered_map<std::string, EnvironmentPropertyProvider::Enviro
{"WIN_LOADEDMODULENAMES", EnvironmentPropertyProvider::EnvironmentProperty::WIN_LOADEDMODULENAMES},
{"ENV_VAR_NAMES", EnvironmentPropertyProvider::EnvironmentProperty::ENV_VAR_NAMES},
{"SYSTEM_RAM", EnvironmentPropertyProvider::EnvironmentProperty::SYSTEM_RAM},
{"CONTAINER_TYPE", EnvironmentPropertyProvider::EnvironmentProperty::CONTAINER_TYPE}
};

#if defined(WZ_OS_WIN)
Expand Down Expand Up @@ -562,6 +627,8 @@ std::string EnvironmentPropertyProvider::GetCurrentEnvironmentPropertyValue(cons
return GetEnvironmentVariableNames();
case EP::SYSTEM_RAM:
return std::to_string(wzGetCurrentSystemRAM());
case EP::CONTAINER_TYPE:
return GetCurrentContainerTypeStr();
}
return ""; // silence warning
}
Expand Down
2 changes: 2 additions & 0 deletions src/wzpropertyproviders.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class EnvironmentPropertyProvider : public PropertyMatcher::PropertyProvider {
// WZ 4.3.3+
ENV_VAR_NAMES,
SYSTEM_RAM, // system RAM in MiB
// WZ 4.4.0+
CONTAINER_TYPE
};
private:
std::string GetCurrentEnvironmentPropertyValue(const EnvironmentProperty& property);
Expand Down

0 comments on commit 05048ff

Please sign in to comment.