refactor(config): 移除平台检测和配置相关代码

将应用配置信息移至Application类,移除平台检测和配置相关文件
简化ShaderManager的初始化逻辑,使用相对路径替代平台检测
This commit is contained in:
ChestnutYueyue 2026-02-17 12:53:01 +08:00
parent 61dea772f7
commit d2660a86bb
11 changed files with 28 additions and 1351 deletions

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <extra2d/config/app_config.h>
#include <extra2d/core/module.h> #include <extra2d/core/module.h>
#include <extra2d/core/registry.h> #include <extra2d/core/registry.h>
#include <extra2d/core/service_locator.h> #include <extra2d/core/service_locator.h>
#include <extra2d/core/types.h> #include <extra2d/core/types.h>
#include <string>
namespace extra2d { namespace extra2d {
@ -25,6 +25,13 @@ public:
Application(const Application &) = delete; Application(const Application &) = delete;
Application &operator=(const Application &) = delete; Application &operator=(const Application &) = delete;
/**
* @brief
*/
std::string appName = "Extra2D App";
std::string appVersion = "1.0.0";
std::string organization = "";
/** /**
* @brief * @brief
* @tparam T * @tparam T
@ -48,13 +55,6 @@ public:
*/ */
bool init(); bool init();
/**
* @brief
* @param config
* @return true
*/
bool init(const AppConfig &config);
/** /**
* @brief * @brief
*/ */
@ -131,8 +131,6 @@ private:
int frameCount_ = 0; int frameCount_ = 0;
float fpsTimer_ = 0.0f; float fpsTimer_ = 0.0f;
int currentFps_ = 0; int currentFps_ = 0;
AppConfig appConfig_;
}; };
} // namespace extra2d } // namespace extra2d

View File

@ -1,61 +0,0 @@
#pragma once
#include <extra2d/config/platform_config.h>
#include <extra2d/core/types.h>
#include <string>
namespace extra2d {
/**
* @file app_config.h
* @brief
*
*
* IModuleConfig
*
* ModuleRegistry ConfigManager
*
*/
/**
* @brief
*
*/
struct AppConfig {
std::string appName = "Extra2D App";
std::string appVersion = "1.0.0";
std::string organization = "";
std::string configFile = "config.json";
PlatformType targetPlatform = PlatformType::Auto;
/**
* @brief
* @return
*/
static AppConfig createDefault();
/**
* @brief
* @return true false
*/
bool validate() const;
/**
* @brief
*/
void reset();
/**
* @brief
* @param other
*/
void merge(const AppConfig& other);
/**
* @brief
* @return true
*/
bool isValid() const { return validate(); }
};
}

View File

@ -1,87 +0,0 @@
#pragma once
#include <extra2d/core/types.h>
#include <string>
namespace extra2d {
/**
* @file platform_config.h
* @brief
*
*
* IModuleConfig::applyPlatformConstraints()
*/
/**
* @brief
*/
enum class PlatformType {
Auto,
Windows,
Switch,
Linux,
macOS
};
/**
* @brief
*/
struct PlatformCapabilities {
bool supportsWindowed = true;
bool supportsFullscreen = true;
bool supportsBorderless = true;
bool supportsCursor = true;
bool supportsCursorHide = true;
bool supportsDPIAwareness = true;
bool supportsVSync = true;
bool supportsMultiMonitor = true;
bool supportsClipboard = true;
bool supportsGamepad = true;
bool supportsTouch = false;
bool supportsKeyboard = true;
bool supportsMouse = true;
bool supportsResize = true;
bool supportsHighDPI = true;
int maxTextureSize = 16384;
int preferredScreenWidth = 1920;
int preferredScreenHeight = 1080;
float defaultDPI = 96.0f;
bool hasWindowSupport() const { return supportsWindowed || supportsFullscreen || supportsBorderless; }
bool hasInputSupport() const { return supportsKeyboard || supportsMouse || supportsGamepad || supportsTouch; }
bool isDesktop() const { return supportsKeyboard && supportsMouse && supportsWindowed; }
bool isConsole() const { return !supportsWindowed && supportsGamepad; }
};
/**
* @brief
*/
class PlatformConfig {
public:
virtual ~PlatformConfig() = default;
virtual PlatformType platformType() const = 0;
virtual const char* platformName() const = 0;
virtual const PlatformCapabilities& capabilities() const = 0;
virtual int getRecommendedWidth() const = 0;
virtual int getRecommendedHeight() const = 0;
virtual bool isResolutionSupported(int width, int height) const = 0;
};
/**
* @brief
* @param type Auto
* @return
*/
UniquePtr<PlatformConfig> createPlatformConfig(PlatformType type = PlatformType::Auto);
/**
* @brief
* @param type
* @return
*/
const char* getPlatformTypeName(PlatformType type);
}

View File

@ -1,210 +0,0 @@
#pragma once
#include <extra2d/config/app_config.h>
#include <extra2d/config/platform_config.h>
#include <extra2d/core/types.h>
#include <string>
namespace extra2d {
// ============================================================================
// 平台检测器工具类
// ============================================================================
class PlatformDetector {
public:
/**
* @brief
* @return
*/
static PlatformType detect();
/**
* @brief
* @return "Windows", "Linux", "macOS", "Switch"
*/
static const char* platformName();
/**
* @brief
* @param type
* @return
*/
static const char* platformName(PlatformType type);
/**
* @brief
* @return true
*/
static bool isDesktopPlatform();
/**
* @brief
* @return true
*/
static bool isConsolePlatform();
/**
* @brief
* @return true
*/
static bool isMobilePlatform();
/**
* @brief
* @return
*/
static PlatformCapabilities capabilities();
/**
* @brief
* @param type
* @return
*/
static PlatformCapabilities capabilities(PlatformType type);
/**
* @brief
* @return
*/
static AppConfig platformDefaults();
/**
* @brief
* @param type
* @return
*/
static AppConfig platformDefaults(PlatformType type);
/**
* @brief
* @param width
* @param height
*/
static void getRecommendedResolution(int& width, int& height);
/**
* @brief DPI
* @return DPI
*/
static float getDefaultDPI();
/**
* @brief
* @param feature
* @return true
*/
static bool supportsFeature(const std::string& feature);
/**
* @brief
* @return MB 0
*/
static int getSystemMemoryMB();
/**
* @brief CPU
* @return CPU
*/
static int getCPUCoreCount();
/**
* @brief 线
* @return true
*/
static bool supportsMultithreadedRendering();
/**
* @brief
* @param appName
* @return
*/
static std::string getConfigPath(const std::string& appName);
/**
* @brief
* @param appName
* @return
*/
static std::string getSavePath(const std::string& appName);
/**
* @brief
* @param appName
* @return
*/
static std::string getCachePath(const std::string& appName);
/**
* @brief
* @param appName
* @return
*/
static std::string getLogPath(const std::string& appName);
/**
* @brief Shader
* Switch平台使用romfs使
* @param appName
* @return
*/
static std::string getResourcePath(const std::string& appName = "");
/**
* @brief Shader路径
* @param appName
* @return Shader目录路径
*/
static std::string getShaderPath(const std::string& appName = "");
/**
* @brief Shader缓存路径
* Switch平台使用sdmc使
* @param appName
* @return Shader缓存目录路径
*/
static std::string getShaderCachePath(const std::string& appName = "");
/**
* @brief 使romfs
* @return 使romfs返回true
*/
static bool usesRomfs();
/**
* @brief
* Switch平台不支持热重载romfs只读
* @return true
*/
static bool supportsHotReload();
/**
* @brief
* @return true
*/
static bool isLittleEndian();
/**
* @brief
* @return true
*/
static bool isBigEndian();
/**
* @brief
* @return
*/
static std::string getPlatformSummary();
private:
static PlatformCapabilities getWindowsCapabilities();
static PlatformCapabilities getLinuxCapabilities();
static PlatformCapabilities getMacOSCapabilities();
static PlatformCapabilities getSwitchCapabilities();
static AppConfig getWindowsDefaults();
static AppConfig getLinuxDefaults();
static AppConfig getMacOSDefaults();
static AppConfig getSwitchDefaults();
};
}

View File

@ -10,10 +10,7 @@
#include <extra2d/core/module.h> #include <extra2d/core/module.h>
#include <extra2d/core/registry.h> #include <extra2d/core/registry.h>
// Config // Config removed - app info now in Application class
#include <extra2d/config/app_config.h>
#include <extra2d/config/platform_config.h>
#include <extra2d/config/platform_detector.h>
// Platform // Platform
#include <extra2d/platform/iinput.h> #include <extra2d/platform/iinput.h>

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <extra2d/config/platform_detector.h>
#include <extra2d/graphics/shader/shader_cache.h> #include <extra2d/graphics/shader/shader_cache.h>
#include <extra2d/graphics/shader/shader_hot_reloader.h> #include <extra2d/graphics/shader/shader_hot_reloader.h>
#include <extra2d/graphics/shader/shader_interface.h> #include <extra2d/graphics/shader/shader_interface.h>

View File

@ -44,17 +44,10 @@ Application::~Application() {
} }
bool Application::init() { bool Application::init() {
AppConfig cfg;
return init(cfg);
}
bool Application::init(const AppConfig &config) {
if (initialized_) { if (initialized_) {
return true; return true;
} }
appConfig_ = config;
// 初始化所有模块(拓扑排序) // 初始化所有模块(拓扑排序)
// 服务通过 E2D_AUTO_REGISTER_SERVICE 宏自动注册 // 服务通过 E2D_AUTO_REGISTER_SERVICE 宏自动注册
if (!Registry::instance().init()) { if (!Registry::instance().init()) {

View File

@ -1,60 +0,0 @@
#include <extra2d/config/app_config.h>
#include <extra2d/utils/logger.h>
namespace extra2d {
AppConfig AppConfig::createDefault() {
AppConfig config;
config.appName = "Extra2D App";
config.appVersion = "1.0.0";
config.organization = "";
config.configFile = "config.json";
config.targetPlatform = PlatformType::Auto;
return config;
}
bool AppConfig::validate() const {
if (appName.empty()) {
E2D_LOG_ERROR("Config validation failed: app name cannot be empty");
return false;
}
if (appVersion.empty()) {
E2D_LOG_ERROR("Config validation failed: app version cannot be empty");
return false;
}
if (configFile.empty()) {
E2D_LOG_ERROR("Config validation failed: config file cannot be empty");
return false;
}
return true;
}
void AppConfig::reset() {
*this = createDefault();
E2D_LOG_INFO("App config reset to defaults");
}
void AppConfig::merge(const AppConfig& other) {
if (other.appName != "Extra2D App") {
appName = other.appName;
}
if (other.appVersion != "1.0.0") {
appVersion = other.appVersion;
}
if (!other.organization.empty()) {
organization = other.organization;
}
if (other.configFile != "config.json") {
configFile = other.configFile;
}
if (other.targetPlatform != PlatformType::Auto) {
targetPlatform = other.targetPlatform;
}
E2D_LOG_INFO("Merged app config");
}
}

View File

@ -1,224 +0,0 @@
#include <extra2d/config/app_config.h>
#include <extra2d/config/platform_config.h>
#include <extra2d/utils/logger.h>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __SWITCH__
#include <switch.h>
#endif
namespace extra2d {
namespace {
class WindowsPlatformConfig : public PlatformConfig {
public:
WindowsPlatformConfig() {
caps_.supportsWindowed = true;
caps_.supportsFullscreen = true;
caps_.supportsBorderless = true;
caps_.supportsCursor = true;
caps_.supportsCursorHide = true;
caps_.supportsDPIAwareness = true;
caps_.supportsVSync = true;
caps_.supportsMultiMonitor = true;
caps_.supportsClipboard = true;
caps_.supportsGamepad = true;
caps_.supportsTouch = false;
caps_.supportsKeyboard = true;
caps_.supportsMouse = true;
caps_.supportsResize = true;
caps_.supportsHighDPI = true;
caps_.maxTextureSize = 16384;
caps_.preferredScreenWidth = 1920;
caps_.preferredScreenHeight = 1080;
caps_.defaultDPI = 96.0f;
}
PlatformType platformType() const override { return PlatformType::Windows; }
const char *platformName() const override { return "Windows"; }
const PlatformCapabilities &capabilities() const override { return caps_; }
int getRecommendedWidth() const override { return 1920; }
int getRecommendedHeight() const override { return 1080; }
bool isResolutionSupported(int width, int height) const override {
return width >= 320 && height >= 240 && width <= caps_.maxTextureSize &&
height <= caps_.maxTextureSize;
}
private:
PlatformCapabilities caps_;
};
class LinuxPlatformConfig : public PlatformConfig {
public:
LinuxPlatformConfig() {
caps_.supportsWindowed = true;
caps_.supportsFullscreen = true;
caps_.supportsBorderless = true;
caps_.supportsCursor = true;
caps_.supportsCursorHide = true;
caps_.supportsDPIAwareness = true;
caps_.supportsVSync = true;
caps_.supportsMultiMonitor = true;
caps_.supportsClipboard = true;
caps_.supportsGamepad = true;
caps_.supportsTouch = false;
caps_.supportsKeyboard = true;
caps_.supportsMouse = true;
caps_.supportsResize = true;
caps_.supportsHighDPI = true;
caps_.maxTextureSize = 16384;
caps_.preferredScreenWidth = 1920;
caps_.preferredScreenHeight = 1080;
caps_.defaultDPI = 96.0f;
}
PlatformType platformType() const override { return PlatformType::Linux; }
const char *platformName() const override { return "Linux"; }
const PlatformCapabilities &capabilities() const override { return caps_; }
int getRecommendedWidth() const override { return 1920; }
int getRecommendedHeight() const override { return 1080; }
bool isResolutionSupported(int width, int height) const override {
return width >= 320 && height >= 240;
}
private:
PlatformCapabilities caps_;
};
class MacOSPlatformConfig : public PlatformConfig {
public:
MacOSPlatformConfig() {
caps_.supportsWindowed = true;
caps_.supportsFullscreen = true;
caps_.supportsBorderless = true;
caps_.supportsCursor = true;
caps_.supportsCursorHide = true;
caps_.supportsDPIAwareness = true;
caps_.supportsVSync = true;
caps_.supportsMultiMonitor = true;
caps_.supportsClipboard = true;
caps_.supportsGamepad = true;
caps_.supportsTouch = false;
caps_.supportsKeyboard = true;
caps_.supportsMouse = true;
caps_.supportsResize = true;
caps_.supportsHighDPI = true;
caps_.maxTextureSize = 16384;
caps_.preferredScreenWidth = 1920;
caps_.preferredScreenHeight = 1080;
caps_.defaultDPI = 144.0f;
}
PlatformType platformType() const override { return PlatformType::macOS; }
const char *platformName() const override { return "macOS"; }
const PlatformCapabilities &capabilities() const override { return caps_; }
int getRecommendedWidth() const override { return 1920; }
int getRecommendedHeight() const override { return 1080; }
bool isResolutionSupported(int width, int height) const override {
return width >= 320 && height >= 240;
}
private:
PlatformCapabilities caps_;
};
class SwitchPlatformConfig : public PlatformConfig {
public:
SwitchPlatformConfig() {
caps_.supportsWindowed = false;
caps_.supportsFullscreen = true;
caps_.supportsBorderless = false;
caps_.supportsCursor = false;
caps_.supportsCursorHide = false;
caps_.supportsDPIAwareness = false;
caps_.supportsVSync = true;
caps_.supportsMultiMonitor = false;
caps_.supportsClipboard = false;
caps_.supportsGamepad = true;
caps_.supportsTouch = true;
caps_.supportsKeyboard = false;
caps_.supportsMouse = false;
caps_.supportsResize = false;
caps_.supportsHighDPI = false;
caps_.maxTextureSize = 8192;
caps_.preferredScreenWidth = 1920;
caps_.preferredScreenHeight = 1080;
caps_.defaultDPI = 96.0f;
}
PlatformType platformType() const override { return PlatformType::Switch; }
const char *platformName() const override { return "Nintendo Switch"; }
const PlatformCapabilities &capabilities() const override { return caps_; }
int getRecommendedWidth() const override { return 1920; }
int getRecommendedHeight() const override { return 1080; }
bool isResolutionSupported(int width, int height) const override {
return (width == 1920 && height == 1080) ||
(width == 1280 && height == 720);
}
private:
PlatformCapabilities caps_;
};
}
UniquePtr<PlatformConfig> createPlatformConfig(PlatformType type) {
if (type == PlatformType::Auto) {
#ifdef _WIN32
type = PlatformType::Windows;
#elif defined(__SWITCH__)
type = PlatformType::Switch;
#elif defined(__linux__)
type = PlatformType::Linux;
#elif defined(__APPLE__)
type = PlatformType::macOS;
#else
type = PlatformType::Windows;
#endif
}
switch (type) {
case PlatformType::Windows:
E2D_LOG_INFO("Creating Windows platform config");
return makeUnique<WindowsPlatformConfig>();
case PlatformType::Switch:
E2D_LOG_INFO("Creating Nintendo Switch platform config");
return makeUnique<SwitchPlatformConfig>();
case PlatformType::Linux:
E2D_LOG_INFO("Creating Linux platform config");
return makeUnique<LinuxPlatformConfig>();
case PlatformType::macOS:
E2D_LOG_INFO("Creating macOS platform config");
return makeUnique<MacOSPlatformConfig>();
default:
E2D_LOG_WARN("Unknown platform type, defaulting to Windows");
return makeUnique<WindowsPlatformConfig>();
}
}
const char *getPlatformTypeName(PlatformType type) {
switch (type) {
case PlatformType::Auto:
return "Auto";
case PlatformType::Windows:
return "Windows";
case PlatformType::Switch:
return "Switch";
case PlatformType::Linux:
return "Linux";
case PlatformType::macOS:
return "macOS";
default:
return "Unknown";
}
}
}

View File

@ -1,678 +0,0 @@
#include <extra2d/config/platform_detector.h>
#include <extra2d/config/platform_config.h>
#include <extra2d/utils/logger.h>
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#include <psapi.h>
#elif defined(__linux__)
#include <sys/sysinfo.h>
#include <unistd.h>
#include <pwd.h>
#include <cstdlib>
#elif defined(__APPLE__)
#include <sys/sysctl.h>
#include <unistd.h>
#include <pwd.h>
#include <cstdlib>
#endif
#ifdef __SWITCH__
#include <switch.h>
#endif
namespace extra2d {
/**
* @brief
* 使
* @return
*/
PlatformType PlatformDetector::detect() {
#ifdef _WIN32
return PlatformType::Windows;
#elif defined(__SWITCH__)
return PlatformType::Switch;
#elif defined(__linux__)
return PlatformType::Linux;
#elif defined(__APPLE__)
return PlatformType::macOS;
#else
return PlatformType::Windows;
#endif
}
/**
* @brief
* @return "Windows", "Linux", "macOS", "Switch"
*/
const char* PlatformDetector::platformName() {
return platformName(detect());
}
/**
* @brief
* @param type
* @return
*/
const char* PlatformDetector::platformName(PlatformType type) {
switch (type) {
case PlatformType::Windows: return "Windows";
case PlatformType::Switch: return "Nintendo Switch";
case PlatformType::Linux: return "Linux";
case PlatformType::macOS: return "macOS";
case PlatformType::Auto: return "Auto";
default: return "Unknown";
}
}
/**
* @brief
* @return true
*/
bool PlatformDetector::isDesktopPlatform() {
PlatformType type = detect();
return type == PlatformType::Windows ||
type == PlatformType::Linux ||
type == PlatformType::macOS;
}
/**
* @brief
* @return true
*/
bool PlatformDetector::isConsolePlatform() {
return detect() == PlatformType::Switch;
}
/**
* @brief
* @return true
*/
bool PlatformDetector::isMobilePlatform() {
return false;
}
/**
* @brief
* @return
*/
PlatformCapabilities PlatformDetector::capabilities() {
return capabilities(detect());
}
/**
* @brief
* @param type
* @return
*/
PlatformCapabilities PlatformDetector::capabilities(PlatformType type) {
switch (type) {
case PlatformType::Windows:
return getWindowsCapabilities();
case PlatformType::Switch:
return getSwitchCapabilities();
case PlatformType::Linux:
return getLinuxCapabilities();
case PlatformType::macOS:
return getMacOSCapabilities();
default:
return getWindowsCapabilities();
}
}
/**
* @brief
* @return
*/
AppConfig PlatformDetector::platformDefaults() {
return platformDefaults(detect());
}
/**
* @brief
* @param type
* @return
*/
AppConfig PlatformDetector::platformDefaults(PlatformType type) {
switch (type) {
case PlatformType::Windows:
return getWindowsDefaults();
case PlatformType::Switch:
return getSwitchDefaults();
case PlatformType::Linux:
return getLinuxDefaults();
case PlatformType::macOS:
return getMacOSDefaults();
default:
return AppConfig::createDefault();
}
}
/**
* @brief
* @param width
* @param height
*/
void PlatformDetector::getRecommendedResolution(int& width, int& height) {
PlatformCapabilities caps = capabilities();
width = caps.preferredScreenWidth;
height = caps.preferredScreenHeight;
}
/**
* @brief DPI
* @return DPI
*/
float PlatformDetector::getDefaultDPI() {
return capabilities().defaultDPI;
}
/**
* @brief
* @param feature
* @return true
*/
bool PlatformDetector::supportsFeature(const std::string& feature) {
PlatformCapabilities caps = capabilities();
if (feature == "windowed") return caps.supportsWindowed;
if (feature == "fullscreen") return caps.supportsFullscreen;
if (feature == "borderless") return caps.supportsBorderless;
if (feature == "cursor") return caps.supportsCursor;
if (feature == "cursor_hide") return caps.supportsCursorHide;
if (feature == "dpi_awareness") return caps.supportsDPIAwareness;
if (feature == "vsync") return caps.supportsVSync;
if (feature == "multi_monitor") return caps.supportsMultiMonitor;
if (feature == "clipboard") return caps.supportsClipboard;
if (feature == "gamepad") return caps.supportsGamepad;
if (feature == "touch") return caps.supportsTouch;
if (feature == "keyboard") return caps.supportsKeyboard;
if (feature == "mouse") return caps.supportsMouse;
if (feature == "resize") return caps.supportsResize;
if (feature == "high_dpi") return caps.supportsHighDPI;
return false;
}
/**
* @brief
* @return MB 0
*/
int PlatformDetector::getSystemMemoryMB() {
#ifdef _WIN32
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
if (GlobalMemoryStatusEx(&status)) {
return static_cast<int>(status.ullTotalPhys / (1024 * 1024));
}
return 0;
#elif defined(__SWITCH__)
return 4096;
#elif defined(__linux__)
struct sysinfo info;
if (sysinfo(&info) == 0) {
return static_cast<int>(info.totalram * info.mem_unit / (1024 * 1024));
}
return 0;
#elif defined(__APPLE__)
int mib[2] = {CTL_HW, HW_MEMSIZE};
int64_t memSize = 0;
size_t length = sizeof(memSize);
if (sysctl(mib, 2, &memSize, &length, nullptr, 0) == 0) {
return static_cast<int>(memSize / (1024 * 1024));
}
return 0;
#else
return 0;
#endif
}
/**
* @brief CPU
* @return CPU
*/
int PlatformDetector::getCPUCoreCount() {
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return static_cast<int>(sysinfo.dwNumberOfProcessors);
#elif defined(__SWITCH__)
return 4;
#elif defined(__linux__) || defined(__APPLE__)
long cores = sysconf(_SC_NPROCESSORS_ONLN);
return static_cast<int>(cores > 0 ? cores : 1);
#else
return 1;
#endif
}
/**
* @brief 线
* @return true
*/
bool PlatformDetector::supportsMultithreadedRendering() {
#ifdef __SWITCH__
return false;
#else
return getCPUCoreCount() >= 2;
#endif
}
/**
* @brief
* @param appName
* @return
*/
std::string PlatformDetector::getConfigPath(const std::string& appName) {
#ifdef _WIN32
char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_APPDATA, nullptr, 0, path))) {
return std::string(path) + "\\" + appName + "\\config";
}
return ".\\config";
#elif defined(__SWITCH__)
return "sdmc:/config/" + appName;
#elif defined(__linux__)
const char* configHome = getenv("XDG_CONFIG_HOME");
if (configHome && configHome[0] != '\0') {
return std::string(configHome) + "/" + appName;
}
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/.config/" + appName;
}
return "./config";
#elif defined(__APPLE__)
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/Library/Application Support/" + appName + "/config";
}
return "./config";
#else
return "./config";
#endif
}
/**
* @brief
* @param appName
* @return
*/
std::string PlatformDetector::getSavePath(const std::string& appName) {
#ifdef _WIN32
char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_APPDATA, nullptr, 0, path))) {
return std::string(path) + "\\" + appName + "\\saves";
}
return ".\\saves";
#elif defined(__SWITCH__)
return "sdmc:/saves/" + appName;
#elif defined(__linux__)
const char* dataHome = getenv("XDG_DATA_HOME");
if (dataHome && dataHome[0] != '\0') {
return std::string(dataHome) + "/" + appName + "/saves";
}
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/.local/share/" + appName + "/saves";
}
return "./saves";
#elif defined(__APPLE__)
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/Library/Application Support/" + appName + "/saves";
}
return "./saves";
#else
return "./saves";
#endif
}
/**
* @brief
* @param appName
* @return
*/
std::string PlatformDetector::getCachePath(const std::string& appName) {
#ifdef _WIN32
char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, path))) {
return std::string(path) + "\\" + appName + "\\cache";
}
return ".\\cache";
#elif defined(__SWITCH__)
return "sdmc:/cache/" + appName;
#elif defined(__linux__)
const char* cacheHome = getenv("XDG_CACHE_HOME");
if (cacheHome && cacheHome[0] != '\0') {
return std::string(cacheHome) + "/" + appName;
}
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/.cache/" + appName;
}
return "./cache";
#elif defined(__APPLE__)
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/Library/Caches/" + appName;
}
return "./cache";
#else
return "./cache";
#endif
}
/**
* @brief
* @param appName
* @return
*/
std::string PlatformDetector::getLogPath(const std::string& appName) {
#ifdef _WIN32
char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, path))) {
return std::string(path) + "\\" + appName + "\\logs";
}
return ".\\logs";
#elif defined(__SWITCH__)
return "sdmc:/logs/" + appName;
#elif defined(__linux__)
const char* cacheHome = getenv("XDG_CACHE_HOME");
if (cacheHome && cacheHome[0] != '\0') {
return std::string(cacheHome) + "/" + appName + "/logs";
}
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/.cache/" + appName + "/logs";
}
return "./logs";
#elif defined(__APPLE__)
const char* home = getenv("HOME");
if (!home) {
struct passwd* pwd = getpwuid(getuid());
if (pwd) home = pwd->pw_dir;
}
if (home) {
return std::string(home) + "/Library/Logs/" + appName;
}
return "./logs";
#else
return "./logs";
#endif
}
/**
* @brief Shader
* Switch平台使用romfs使
* @param appName
* @return
*/
std::string PlatformDetector::getResourcePath(const std::string& appName) {
#ifdef __SWITCH__
(void)appName;
return "romfs:/";
#else
(void)appName;
return "./resources/";
#endif
}
/**
* @brief Shader路径
* @param appName
* @return Shader目录路径
*/
std::string PlatformDetector::getShaderPath(const std::string& appName) {
#ifdef __SWITCH__
(void)appName;
return "romfs:/shaders/";
#else
(void)appName;
return "./shaders/";
#endif
}
/**
* @brief Shader缓存路径
* Switch平台使用sdmc使
* @param appName
* @return Shader缓存目录路径
*/
std::string PlatformDetector::getShaderCachePath(const std::string& appName) {
#ifdef __SWITCH__
std::string name = appName.empty() ? "extra2d" : appName;
return "sdmc:/cache/" + name + "/shaders/";
#else
return getCachePath(appName.empty() ? "extra2d" : appName) + "/shaders/";
#endif
}
/**
* @brief 使romfs
* @return 使romfs返回true
*/
bool PlatformDetector::usesRomfs() {
#ifdef __SWITCH__
return true;
#else
return false;
#endif
}
/**
* @brief
* Switch平台不支持热重载romfs只读
* @return true
*/
bool PlatformDetector::supportsHotReload() {
#ifdef __SWITCH__
return false;
#else
return true;
#endif
}
/**
* @brief
* @return true
*/
bool PlatformDetector::isLittleEndian() {
union {
uint32_t i;
char c[4];
} test = {0x01020304};
return test.c[0] == 0x04;
}
/**
* @brief
* @return true
*/
bool PlatformDetector::isBigEndian() {
return !isLittleEndian();
}
/**
* @brief
* @return
*/
std::string PlatformDetector::getPlatformSummary() {
std::string summary;
summary += "Platform: ";
summary += platformName();
summary += "\n";
summary += "Memory: ";
summary += std::to_string(getSystemMemoryMB());
summary += " MB\n";
summary += "CPU Cores: ";
summary += std::to_string(getCPUCoreCount());
summary += "\n";
summary += "Endianness: ";
summary += isLittleEndian() ? "Little Endian" : "Big Endian";
summary += "\n";
summary += "Desktop Platform: ";
summary += isDesktopPlatform() ? "Yes" : "No";
summary += "\n";
summary += "Console Platform: ";
summary += isConsolePlatform() ? "Yes" : "No";
summary += "\n";
summary += "Recommended Resolution: ";
int width, height;
getRecommendedResolution(width, height);
summary += std::to_string(width);
summary += "x";
summary += std::to_string(height);
summary += "\n";
summary += "Default DPI: ";
summary += std::to_string(static_cast<int>(getDefaultDPI()));
return summary;
}
PlatformCapabilities PlatformDetector::getWindowsCapabilities() {
PlatformCapabilities caps;
caps.supportsWindowed = true;
caps.supportsFullscreen = true;
caps.supportsBorderless = true;
caps.supportsCursor = true;
caps.supportsCursorHide = true;
caps.supportsDPIAwareness = true;
caps.supportsVSync = true;
caps.supportsMultiMonitor = true;
caps.supportsClipboard = true;
caps.supportsGamepad = true;
caps.supportsTouch = false;
caps.supportsKeyboard = true;
caps.supportsMouse = true;
caps.supportsResize = true;
caps.supportsHighDPI = true;
caps.maxTextureSize = 16384;
caps.preferredScreenWidth = 1920;
caps.preferredScreenHeight = 1080;
caps.defaultDPI = 96.0f;
return caps;
}
PlatformCapabilities PlatformDetector::getLinuxCapabilities() {
PlatformCapabilities caps;
caps.supportsWindowed = true;
caps.supportsFullscreen = true;
caps.supportsBorderless = true;
caps.supportsCursor = true;
caps.supportsCursorHide = true;
caps.supportsDPIAwareness = true;
caps.supportsVSync = true;
caps.supportsMultiMonitor = true;
caps.supportsClipboard = true;
caps.supportsGamepad = true;
caps.supportsTouch = false;
caps.supportsKeyboard = true;
caps.supportsMouse = true;
caps.supportsResize = true;
caps.supportsHighDPI = true;
caps.maxTextureSize = 16384;
caps.preferredScreenWidth = 1920;
caps.preferredScreenHeight = 1080;
caps.defaultDPI = 96.0f;
return caps;
}
PlatformCapabilities PlatformDetector::getMacOSCapabilities() {
PlatformCapabilities caps;
caps.supportsWindowed = true;
caps.supportsFullscreen = true;
caps.supportsBorderless = true;
caps.supportsCursor = true;
caps.supportsCursorHide = true;
caps.supportsDPIAwareness = true;
caps.supportsVSync = true;
caps.supportsMultiMonitor = true;
caps.supportsClipboard = true;
caps.supportsGamepad = true;
caps.supportsTouch = false;
caps.supportsKeyboard = true;
caps.supportsMouse = true;
caps.supportsResize = true;
caps.supportsHighDPI = true;
caps.maxTextureSize = 16384;
caps.preferredScreenWidth = 1920;
caps.preferredScreenHeight = 1080;
caps.defaultDPI = 144.0f;
return caps;
}
PlatformCapabilities PlatformDetector::getSwitchCapabilities() {
PlatformCapabilities caps;
caps.supportsWindowed = false;
caps.supportsFullscreen = true;
caps.supportsBorderless = false;
caps.supportsCursor = false;
caps.supportsCursorHide = false;
caps.supportsDPIAwareness = false;
caps.supportsVSync = true;
caps.supportsMultiMonitor = false;
caps.supportsClipboard = false;
caps.supportsGamepad = true;
caps.supportsTouch = true;
caps.supportsKeyboard = false;
caps.supportsMouse = false;
caps.supportsResize = false;
caps.supportsHighDPI = false;
caps.maxTextureSize = 8192;
caps.preferredScreenWidth = 1920;
caps.preferredScreenHeight = 1080;
caps.defaultDPI = 96.0f;
return caps;
}
AppConfig PlatformDetector::getWindowsDefaults() {
AppConfig config = AppConfig::createDefault();
return config;
}
AppConfig PlatformDetector::getLinuxDefaults() {
AppConfig config = AppConfig::createDefault();
return config;
}
AppConfig PlatformDetector::getMacOSDefaults() {
AppConfig config = AppConfig::createDefault();
return config;
}
AppConfig PlatformDetector::getSwitchDefaults() {
AppConfig config = AppConfig::createDefault();
return config;
}
}

View File

@ -20,13 +20,18 @@ ShaderManager& ShaderManager::getInstance() {
* @return truefalse * @return truefalse
*/ */
bool ShaderManager::init(Ptr<IShaderFactory> factory, const std::string& appName) { bool ShaderManager::init(Ptr<IShaderFactory> factory, const std::string& appName) {
std::string shaderDir = PlatformDetector::getShaderPath(appName); // 使用相对路径作为Shader目录
std::string cacheDir = PlatformDetector::getShaderCachePath(appName); std::string shaderDir = "shaders/";
std::string cacheDir = "cache/shaders/";
hotReloadSupported_ = PlatformDetector::supportsHotReload(); // 非Switch平台支持热重载
#ifndef __SWITCH__
hotReloadSupported_ = true;
#else
hotReloadSupported_ = false;
#endif
E2D_LOG_INFO("Platform: {} (HotReload: {})", E2D_LOG_INFO("ShaderManager init (HotReload: {})",
PlatformDetector::platformName(),
hotReloadSupported_ ? "supported" : "not supported"); hotReloadSupported_ ? "supported" : "not supported");
return init(shaderDir, cacheDir, factory); return init(shaderDir, cacheDir, factory);
@ -56,7 +61,12 @@ bool ShaderManager::init(const std::string& shaderDir,
cacheDir_ = cacheDir; cacheDir_ = cacheDir;
factory_ = factory; factory_ = factory;
hotReloadSupported_ = PlatformDetector::supportsHotReload(); // 非Switch平台支持热重载
#ifndef __SWITCH__
hotReloadSupported_ = true;
#else
hotReloadSupported_ = false;
#endif
#ifdef __SWITCH__ #ifdef __SWITCH__
if (!ShaderCache::getInstance().init(cacheDir_)) { if (!ShaderCache::getInstance().init(cacheDir_)) {