载入中...
搜索中...
未找到
System.cpp
浏览该文件的文档.
1#include "system/System.h"
2
3#include "common/Exception.h"
4#include "common/Module.h"
5#include "common/Capability.h"
6#include "common/GpuInfo.h"
7#include "common/config.h"
8
9#include <simplesquirrel/simplesquirrel.hpp>
10
11#include <SDL2/SDL.h>
12#include <SDL2/SDL_clipboard.h>
13#include <SDL2/SDL_cpuinfo.h>
14#include <SDL2/SDL_power.h>
15
16#include <chrono>
17#include <cstdio>
18#include <cstring>
19#include <fstream>
20#include <string>
21
22#if defined(EVENGINE_WINDOWS) || defined(_WIN32)
23#ifndef NOMINMAX
24#define NOMINMAX
25#endif
26#include <windows.h>
27#include <psapi.h>
28#elif defined(__APPLE__)
29#include <mach/mach.h>
30#endif
31
32namespace eve::system {
33namespace {
34
36eve::caps::IGpuInfo *gpuInfoOrNull() {
38 return (info && info->gpuReady()) ? info : nullptr;
39}
40
41} // namespace
42
44
45System::System() { SDL_InitSubSystem(SDL_INIT_TIMER); }
46
47std::string System::getEngineVersion() const { return EVENGINE_VERSION; }
48
49std::string System::getPlatform() const {
50 const char *p = SDL_GetPlatform();
51 return p ? p : "Unknown";
52}
53
54std::string System::getOS() const {
55#if defined(EVENGINE_WINDOWS)
56 return "Windows";
57#elif defined(EVENGINE_IOS)
58 return "iOS";
59#elif defined(EVENGINE_MACOSX)
60 return "OS X";
61#elif defined(EVENGINE_ANDROID)
62 return "Android";
63#elif defined(EVENGINE_LINUX)
64 return "Linux";
65#else
66 return getPlatform();
67#endif
68}
69
71 int n = SDL_GetCPUCount();
72 return n > 0 ? n : 1;
73}
74
76 int n = SDL_GetCPUCacheLineSize();
77 return n > 0 ? n : 0;
78}
79
81 int mb = SDL_GetSystemRAM();
82 return mb > 0 ? mb : 0;
83}
84
86#if defined(EVENGINE_WINDOWS) || defined(_WIN32)
87 PROCESS_MEMORY_COUNTERS pmc{};
88 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
89 return static_cast<int>(pmc.WorkingSetSize / (1024ull * 1024ull));
90 return 0;
91#elif defined(__APPLE__)
92 task_vm_info_data_t info{};
93 mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
94 if (task_info(mach_task_self(), TASK_VM_INFO, reinterpret_cast<task_info_t>(&info), &count) ==
95 KERN_SUCCESS) {
96 return static_cast<int>(info.phys_footprint / (1024ull * 1024ull));
97 }
98 return 0;
99#elif defined(__linux__) || defined(EVENGINE_ANDROID) || defined(EVENGINE_LINUX)
100 std::ifstream in("/proc/self/status");
101 if (!in) return 0;
102 std::string key;
103 while (in >> key) {
104 if (key == "VmRSS:") {
105 long kb = 0;
106 in >> kb;
107 return static_cast<int>(kb / 1024);
108 }
109 std::string rest;
110 std::getline(in, rest);
111 }
112 return 0;
113#else
114 return 0;
115#endif
116}
117
118float System::getWallTime() const {
119 using clock = std::chrono::system_clock;
120 auto tp = clock::now().time_since_epoch();
121 auto us = std::chrono::duration_cast<std::chrono::microseconds>(tp).count();
122 return float(double(us) / 1'000'000.0);
123}
124
126 if (ms < 0) throw Exception("System.sleepMilliseconds: ms must be >= 0");
127 if (ms == 0) return;
128 SDL_Delay(static_cast<Uint32>(ms));
129}
130
131std::string System::getPowerState() const {
132 int secs = 0, pct = 0;
133 SDL_PowerState st = SDL_GetPowerInfo(&secs, &pct);
134 switch (st) {
135 case SDL_POWERSTATE_ON_BATTERY: return "on_battery";
136 case SDL_POWERSTATE_NO_BATTERY: return "no_battery";
137 case SDL_POWERSTATE_CHARGING: return "charging";
138 case SDL_POWERSTATE_CHARGED: return "charged";
139 default: return "unknown";
140 }
141}
142
144 int secs = -1, pct = -1;
145 SDL_GetPowerInfo(&secs, &pct);
146 return secs;
147}
148
150 int secs = -1, pct = -1;
151 SDL_GetPowerInfo(&secs, &pct);
152 return pct;
153}
154
155std::string System::getClipboardText() const {
156 if (!SDL_HasClipboardText()) return {};
157 char *t = SDL_GetClipboardText();
158 if (!t) return {};
159 std::string out(t);
160 SDL_free(t);
161 return out;
162}
163
164void System::setClipboardText(const std::string &text) {
165 if (SDL_SetClipboardText(text.c_str()) != 0)
166 throw Exception("System.setClipboardText failed: %s", SDL_GetError());
167}
168
169std::string System::getGpuName() const {
170 auto *info = gpuInfoOrNull();
171 return info ? info->gpuName() : std::string();
172}
173
174std::string System::getGpuVendor() const {
175 auto *info = gpuInfoOrNull();
176 return info ? info->gpuVendor() : std::string();
177}
178
179std::string System::getGpuDeviceType() const {
180 auto *info = gpuInfoOrNull();
181 return info ? info->gpuDeviceType() : std::string();
182}
183
185 auto *info = gpuInfoOrNull();
186 return info ? info->gpuMemoryTotalMB() : 0;
187}
188
189void System::expose(ssq::Table &table) {
190 auto cls = table.addClass(name, System::create, false);
191 expose(cls);
192}
193
194void System::expose(ssq::Class &cls) {
195 cls.addFunc("getName", &System::getName);
196 cls.addFunc("getEngineVersion", &System::getEngineVersion);
197 cls.addFunc("getPlatform", &System::getPlatform);
198 cls.addFunc("getOS", &System::getOS);
199 cls.addFunc("getProcessorCount", &System::getProcessorCount);
200 cls.addFunc("getCPUCacheLineSize", &System::getCPUCacheLineSize);
201 cls.addFunc("getSystemRAM", &System::getSystemRAM);
202 cls.addFunc("getProcessMemoryMB", &System::getProcessMemoryMB);
203 cls.addFunc("getWallTime", &System::getWallTime);
204 cls.addFunc("sleepMilliseconds", &System::sleepMilliseconds);
205 cls.addFunc("getPowerState", &System::getPowerState);
206 cls.addFunc("getPowerSecondsLeft", &System::getPowerSecondsLeft);
207 cls.addFunc("getPowerPercent", &System::getPowerPercent);
208 cls.addFunc("getClipboardText", &System::getClipboardText);
209 cls.addFunc("setClipboardText", &System::setClipboardText);
210 cls.addFunc("getGpuName", &System::getGpuName);
211 cls.addFunc("getGpuVendor", &System::getGpuVendor);
212 cls.addFunc("getGpuDeviceType", &System::getGpuDeviceType);
213 cls.addFunc("getGpuMemoryTotalMB", &System::getGpuMemoryTotalMB);
214}
215
216} // namespace eve::system
HSQOBJECT cls
Definition ECS.cpp:21
glm::vec3 n
Definition Grass.cpp:64
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
glm::vec4 p[6]
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
System module — OS / hardware / wall-clock / clipboard / optional GPU info. Frame timing stays on Tim...
Definition System.h:16
int getProcessorCount() const
Definition System.cpp:70
std::string getGpuName() const
GPU queries — empty/0 if Graphics not initialized yet.
Definition System.cpp:169
std::string getGpuDeviceType() const
"discrete" | "integrated" | "virtual" | "cpu" | "other" | ""
Definition System.cpp:179
int getPowerSecondsLeft() const
Estimated seconds of battery left (-1 if unknown).
Definition System.cpp:143
int getProcessMemoryMB() const
Current process resident memory in megabytes (0 if unknown).
Definition System.cpp:85
float getWallTime() const
UTC wall-clock seconds since Unix epoch (float for script).
Definition System.cpp:118
std::string getGpuVendor() const
Definition System.cpp:174
std::string getPlatform() const
Raw SDL platform string (e.g. "Mac OS X", "Windows").
Definition System.cpp:49
int getCPUCacheLineSize() const
CPU L1 cache line size in bytes (0 if unknown).
Definition System.cpp:75
std::string getOS() const
Normalized OS id for games: "Windows" | "OS X" | "Linux" | "Android" | "iOS" | "Unknown"
Definition System.cpp:54
int getPowerPercent() const
Battery percent 0–100, or -1 if unknown.
Definition System.cpp:149
std::string getPowerState() const
Power state string: "unknown" | "on_battery" | "no_battery" | "charging" | "charged"
Definition System.cpp:131
std::string getClipboardText() const
Definition System.cpp:155
int getGpuMemoryTotalMB() const
Sum of DEVICE_LOCAL heap sizes in MB (0 if unknown / not ready).
Definition System.cpp:184
int getSystemRAM() const
System RAM in megabytes (0 if unknown).
Definition System.cpp:80
void setClipboardText(const std::string &text)
Definition System.cpp:164
void sleepMilliseconds(int ms)
Definition System.cpp:125
std::string getEngineVersion() const
Engine version string (e.g. "v0.1.0").
Definition System.cpp:47
I * query()
Definition Capability.h:77