Skip to content

Commit 489ebaa

Browse files
committed
changes to win32, added some functions for linux
1 parent e244993 commit 489ebaa

File tree

4 files changed

+138
-123
lines changed

4 files changed

+138
-123
lines changed

source/gameanalytics/Platform/GADeviceOSX.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,25 @@ SCNetworkReachabilityRef createReachabilityRef()
7070

7171
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
7272
// The target host is not reachable.
73-
return "offline";
73+
return gameanalytics::CONNECTION_OFFLINE;
7474
}
7575

7676
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
7777
{
78-
// If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
79-
return "wifi";
78+
// If the target host is reachable and no connection is required then we'll assume (for now) that you're on lan...
79+
return gameanalytics::CONNECTION_LAN;
8080
}
8181

8282
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
8383
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) {
8484
// ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
8585
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) {
8686
// ... and no [user] intervention is needed...
87-
return "wifi";
87+
return gameanalytics::CONNECTION_WIFI;
8888
}
8989
}
9090

91-
return "";
91+
return gameanalytics::CONNECTION_LAN;
9292
}
9393

9494
int getNumCpuCores()

source/gameanalytics/Platform/GALinux.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,62 @@ void gameanalytics::GAPlatformLinux::signalHandler(int sig, siginfo_t* info, voi
165165
}
166166

167167

168+
int64_t gameanalytics::GAPlatformLinux::getAppMemoryUsage() const
169+
{
170+
struct task_basic_info info;
171+
172+
mach_msg_type_number_t infoSize = TASK_BASIC_INFO_COUNT;
173+
kern_return_t result = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &infoSize);
174+
175+
if(result == KERN_SUCCESS)
176+
{
177+
return utilities::convertBytesToMB(info.resident_size);
178+
}
179+
180+
return 0;
181+
}
182+
183+
int64_t gameanalytics::GAPlatformLinux::getSysMemoryUsage() const
184+
{
185+
mach_port_t port = mach_host_self();
186+
mach_msg_type_number_t hostSize = sizeof(vm_statistics_data_t) / sizeof(integer_t);
187+
188+
vm_size_t pageSize;
189+
host_page_size(port, &pageSize);
190+
191+
vm_statistics_data_t stats;
192+
193+
if(host_statistics(port, HOST_VM_INFO, (host_info_t)&stats, &hostSize) == KERN_SUCCESS)
194+
{
195+
const int64_t freeMemory = (stats.free_count + stats.inactive_count) * pageSize;
196+
return getTotalDeviceMemory() - utilities::convertBytesToMB(freeMemory);
197+
}
198+
199+
return 0;
200+
}
201+
202+
int64_t gameanalytics::GAPlatformLinux::getBootTime() const
203+
{
204+
const size_t len = 4;
205+
int mib[len] = {0,0,0,0};
206+
struct kinfo_proc kp = {};
207+
208+
const size_t pidId = 3;
209+
210+
size_t num = len;
211+
sysctlnametomib("kern.proc.pid", mib, &num);
212+
mib[pidId] = getpid();
213+
214+
num = sizeof(kp);
215+
sysctl(mib, len, &kp, &num, NULL, 0);
216+
217+
struct timeval startTime = kp.kp_proc.p_un.__p_starttime;
218+
struct timeval currentTime = {};
219+
220+
gettimeofday(&currentTime, NULL);
221+
222+
return currentTime.tv_sec - startTime.tv_sec;
223+
}
224+
225+
168226
#endif

source/gameanalytics/Platform/GAUwp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ std::string gameanalytics::GAPlatformUWP::getConnectionType()
6464
{
6565
if (connectionProfile->IsWlanConnectionProfile)
6666
{
67-
return "wifi";
67+
return CONNECTION_WIFI;
6868
}
6969
else if (connectionProfile->IsWwanConnectionProfile)
7070
{
71-
return "wwan";
71+
return CONNECTION_WWAN;
7272
}
7373
else
7474
{
75-
return "lan";
75+
return CONNECTION_LAN;
7676
}
7777
}
7878
else
7979
{
80-
return "offline";
80+
return CONNECTION_OFFLINE;
8181
}
8282
}
8383

source/gameanalytics/Platform/GAWin32.cpp

Lines changed: 71 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -83,104 +83,6 @@ std::string GAPlatformWin32::getConnectionType()
8383
return "";
8484
}
8585

86-
std::string GAPlatformWin32::getDeviceManufacturer()
87-
{
88-
#if !GA_SHARED_LIB && defined(GA_USE_WBEM_SERVICES)
89-
__try
90-
{
91-
IWbemLocator* locator = nullptr;
92-
IWbemServices* services = nullptr;
93-
94-
auto hResult = CoInitializeEx(0, COINIT_MULTITHREADED);
95-
if (FAILED(hResult))
96-
{
97-
return UNKNOWN_VALUE;
98-
}
99-
hResult = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&locator);
100-
101-
102-
auto hasFailed = [&hResult]() {
103-
if (FAILED(hResult))
104-
{
105-
return true;
106-
}
107-
return false;
108-
};
109-
110-
auto getValue = [&hResult, &hasFailed](IWbemClassObject* classObject, LPCWSTR property) {
111-
BSTR propertyValueText = L"unknown";
112-
VARIANT propertyValue;
113-
hResult = classObject->Get(property, 0, &propertyValue, 0, 0);
114-
if (!hasFailed()) {
115-
if ((propertyValue.vt == VT_NULL) || (propertyValue.vt == VT_EMPTY)) {
116-
}
117-
else if (propertyValue.vt & VT_ARRAY) {
118-
propertyValueText = L"unknown"; //Array types not supported
119-
}
120-
else {
121-
propertyValueText = propertyValue.bstrVal;
122-
}
123-
}
124-
VariantClear(&propertyValue);
125-
return propertyValueText;
126-
};
127-
128-
BSTR manufacturer = L"unknown";
129-
if (!hasFailed())
130-
{
131-
// Connect to the root\cimv2 namespace with the current user and obtain pointer pSvc to make IWbemServices calls.
132-
hResult = locator->ConnectServer(L"ROOT\\CIMV2", nullptr, nullptr, 0, NULL, 0, 0, &services);
133-
134-
if (!hasFailed()) {
135-
// Set the IWbemServices proxy so that impersonation of the user (client) occurs.
136-
hResult = CoSetProxyBlanket(services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL,
137-
RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
138-
139-
if (!hasFailed()) {
140-
IEnumWbemClassObject* classObjectEnumerator = nullptr;
141-
hResult = services->ExecQuery(L"WQL", L"SELECT * FROM Win32_ComputerSystem", WBEM_FLAG_FORWARD_ONLY |
142-
WBEM_FLAG_RETURN_IMMEDIATELY, nullptr, &classObjectEnumerator);
143-
if (!hasFailed())
144-
{
145-
IWbemClassObject* classObject;
146-
ULONG uReturn = 0;
147-
hResult = classObjectEnumerator->Next(WBEM_INFINITE, 1, &classObject, &uReturn);
148-
if (uReturn != 0) {
149-
manufacturer = getValue(classObject, (LPCWSTR)L"Manufacturer");
150-
}
151-
152-
if(classObject)
153-
classObject->Release();
154-
}
155-
156-
if(classObjectEnumerator)
157-
classObjectEnumerator->Release();
158-
}
159-
}
160-
}
161-
162-
if (locator) {
163-
locator->Release();
164-
}
165-
if (services) {
166-
services->Release();
167-
}
168-
CoUninitialize();
169-
170-
std::string manufacturer = _com_util::ConvertBSTRToString(manufacturer);
171-
return manufacturer;
172-
}
173-
__except(EXCEPTION_EXECUTE_HANDLER)
174-
{
175-
logging::GALogger::e("Failed to get device's model");
176-
return UNKNOWN_VALUE;
177-
}
178-
179-
#else
180-
return UNKNOWN_VALUE;
181-
#endif
182-
}
183-
18486
std::string GAPlatformWin32::getBuildPlatform()
18587
{
18688
return "windows";
@@ -204,33 +106,30 @@ std::string GAPlatformWin32::getPersistentPath()
204106
return path;
205107
}
206108

207-
std::string GAPlatformWin32::getDeviceModel()
109+
std::string getRegistryKey(HKEY key, const TCHAR* subkey, const TCHAR* value)
208110
{
209111
__try
210112
{
211-
constexpr const TCHAR* subkey = _T("SYSTEM\\CurrentControlSet\\Control\\SystemInformation");
212-
constexpr const TCHAR* value = _T("SystemProductName");
213-
214113
constexpr DWORD maxBufSize = 128;
215114

216115
DWORD size = 0;
217116
TCHAR buffer[maxBufSize] = _T("");
218-
RegGetValue(HKEY_LOCAL_MACHINE, subkey, value, RRF_RT_REG_SZ, NULL, buffer, &size);
117+
RegGetValue(key, subkey, value, RRF_RT_REG_SZ, NULL, buffer, &size);
219118

220119
size = std::min(size, maxBufSize);
221120

222121
if (!GetLastError() && size > 0)
223122
{
224-
std::string modelName;
123+
std::string val;
225124

226125
#ifdef UNICODE
227126
std::wstring wstr(buffer, buffer + size);
228-
modelName = utilities::GAUtilities::ws2s(wstr);
127+
val = utilities::GAUtilities::ws2s(wstr);
229128
#else
230-
modelName = std::string(buffer, buffer + size);
129+
val = std::string(buffer, buffer + size);
231130
#endif
232131

233-
return modelName;
132+
return val;
234133
}
235134
}
236135
__except(EXCEPTION_EXECUTE_HANDLER)
@@ -241,6 +140,30 @@ std::string GAPlatformWin32::getDeviceModel()
241140
return UNKNOWN_VALUE;
242141
}
243142

143+
std::string GAPlatformWin32::getDeviceModel()
144+
{
145+
constexpr const TCHAR* subkey = _T("SYSTEM\\CurrentControlSet\\Control\\SystemInformation");
146+
constexpr const TCHAR* value = _T("SystemProductName");
147+
148+
return getRegistryKey(HKEY_LOCAL_MACHINE, subkey, value);
149+
}
150+
151+
std::string GAPlatformWin32::getDeviceManufacturer()
152+
{
153+
constexpr const TCHAR* subkey = _T("SYSTEM\\CurrentControlSet\\Control\\SystemInformation");
154+
constexpr const TCHAR* value = _T("SystemManufacturer");
155+
156+
return getRegistryKey(HKEY_LOCAL_MACHINE, subkey, value);
157+
}
158+
159+
std::string GAPlatformWin32::getCpuModel() const
160+
{
161+
constexpr const TCHAR* subkey = _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
162+
constexpr const TCHAR* value = _T("ProcessorName");
163+
164+
return getRegistryKey(HKEY_LOCAL_MACHINE, subkey, value);
165+
}
166+
244167
void GAPlatformWin32::setupUncaughtExceptionHandler()
245168
{
246169
signal(SIGILL, signalHandler);
@@ -293,12 +216,6 @@ void GAPlatformWin32::signalHandler(int sig)
293216
}
294217
}
295218

296-
std::string GAPlatformWin32::getCpuModel() const
297-
{
298-
// todo
299-
return "";
300-
}
301-
302219
std::string GAPlatformWin32::getGpuModel() const
303220
{
304221
//todo
@@ -307,7 +224,31 @@ std::string GAPlatformWin32::getGpuModel() const
307224

308225
int GAPlatformWin32::getNumCpuCores() const
309226
{
310-
//todo
227+
DWORD len = 0;
228+
GetLogicalProcessorInformation(nullptr, &len);
229+
230+
if(len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
231+
{
232+
const int size = len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
233+
auto buffer = std::make_unique<SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]>(size);
234+
if(buffer)
235+
{
236+
if(GetLogicalProcessorInformation(buffer.get(), &len))
237+
{
238+
int numProcessors = 0;
239+
for(int i = 0; i < size; ++i)
240+
{
241+
if(buffer[i].Relationship == RelationProcessorCore)
242+
{
243+
++numProcessors;
244+
}
245+
}
246+
247+
return numProcessors;
248+
}
249+
}
250+
}
251+
311252
return 0;
312253
}
313254

@@ -374,6 +315,22 @@ int64_t GAPlatformWin32::getBootTime() const
374315
return 0ll;
375316
}
376317

318+
std::string GAPlatformWin32::getConnectionType()
319+
{
320+
DWORD flags = 0;
321+
if(InternetGetConnectedState(&flags, 0))
322+
{
323+
if(flags & INTERNET_CONNECTION_OFFLINE)
324+
return CONNECTION_OFFLINE;
325+
326+
if(flags & INTERNET_CONNECTION_LAN)
327+
return CONNECTION_LAN;
328+
329+
return CONNECTION_WIFI;
330+
}
331+
332+
return CONNECTION_OFFLINE;
333+
}
377334

378335
}
379336
#endif

0 commit comments

Comments
 (0)