2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-12-04 11:11:14 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-12-04 11:11:14 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma hdrstop
|
2012-12-22 15:18:19 +00:00
|
|
|
#include "precompiled.h"
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
#include "win_local.h"
|
|
|
|
#include <lmerr.h>
|
|
|
|
#include <lmcons.h>
|
|
|
|
#include <lmwksta.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#undef StrCmpN
|
|
|
|
#undef StrCmpNI
|
|
|
|
#undef StrCmpI
|
2012-11-28 10:27:27 +00:00
|
|
|
|
2012-12-04 01:30:46 +00:00
|
|
|
// RB begin
|
|
|
|
#if !defined(__MINGW32__)
|
2012-11-26 18:58:24 +00:00
|
|
|
#include <comdef.h>
|
|
|
|
#include <comutil.h>
|
|
|
|
#include <Wbemidl.h>
|
|
|
|
|
2012-12-04 01:30:46 +00:00
|
|
|
|
2012-11-28 10:27:27 +00:00
|
|
|
// RB: no <atlbase.h> with Visual C++ 2010 Express
|
|
|
|
#if defined(USE_MFC_TOOLS)
|
|
|
|
#include <atlbase.h>
|
|
|
|
#else
|
|
|
|
#include "win_nanoafx.h"
|
|
|
|
#endif
|
2012-12-04 01:30:46 +00:00
|
|
|
|
|
|
|
#endif // #if !defined(__MINGW32__)
|
2012-11-28 10:27:27 +00:00
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#pragma comment (lib, "wbemuuid.lib")
|
|
|
|
|
|
|
|
#pragma warning(disable:4740) // warning C4740: flow in or out of inline asm code suppresses global optimization
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_Milliseconds
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
int Sys_Milliseconds()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
static DWORD sys_timeBase = timeGetTime();
|
|
|
|
return timeGetTime() - sys_timeBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
Sys_Microseconds
|
|
|
|
========================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
uint64 Sys_Microseconds()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
static uint64 ticksPerMicrosecondTimes1024 = 0;
|
2012-12-04 11:11:14 +00:00
|
|
|
|
|
|
|
if( ticksPerMicrosecondTimes1024 == 0 )
|
|
|
|
{
|
|
|
|
ticksPerMicrosecondTimes1024 = ( ( uint64 )Sys_ClockTicksPerSecond() << 10 ) / 1000000;
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( ticksPerMicrosecondTimes1024 > 0 );
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
|
|
|
return ( ( uint64 )( ( int64 )Sys_GetClockTicks() << 10 ) ) / ticksPerMicrosecondTimes1024;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_GetSystemRam
|
|
|
|
|
|
|
|
returns amount of physical memory in MB
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
int Sys_GetSystemRam()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
MEMORYSTATUSEX statex;
|
2012-12-04 11:11:14 +00:00
|
|
|
statex.dwLength = sizeof( statex );
|
|
|
|
GlobalMemoryStatusEx( &statex );
|
2012-11-26 18:58:24 +00:00
|
|
|
int physRam = statex.ullTotalPhys / ( 1024 * 1024 );
|
|
|
|
// HACK: For some reason, ullTotalPhys is sometimes off by a meg or two, so we round up to the nearest 16 megs
|
|
|
|
physRam = ( physRam + 8 ) & ~15;
|
|
|
|
return physRam;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_GetDriveFreeSpace
|
|
|
|
returns in megabytes
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
int Sys_GetDriveFreeSpace( const char* path )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
DWORDLONG lpFreeBytesAvailable;
|
|
|
|
DWORDLONG lpTotalNumberOfBytes;
|
|
|
|
DWORDLONG lpTotalNumberOfFreeBytes;
|
|
|
|
int ret = 26;
|
|
|
|
//FIXME: see why this is failing on some machines
|
2012-12-04 11:11:14 +00:00
|
|
|
if( ::GetDiskFreeSpaceEx( path, ( PULARGE_INTEGER )&lpFreeBytesAvailable, ( PULARGE_INTEGER )&lpTotalNumberOfBytes, ( PULARGE_INTEGER )&lpTotalNumberOfFreeBytes ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ret = ( double )( lpFreeBytesAvailable ) / ( 1024.0 * 1024.0 );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
Sys_GetDriveFreeSpaceInBytes
|
|
|
|
========================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
int64 Sys_GetDriveFreeSpaceInBytes( const char* path )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
DWORDLONG lpFreeBytesAvailable;
|
|
|
|
DWORDLONG lpTotalNumberOfBytes;
|
|
|
|
DWORDLONG lpTotalNumberOfFreeBytes;
|
|
|
|
int64 ret = 1;
|
|
|
|
//FIXME: see why this is failing on some machines
|
2012-12-04 11:11:14 +00:00
|
|
|
if( ::GetDiskFreeSpaceEx( path, ( PULARGE_INTEGER )&lpFreeBytesAvailable, ( PULARGE_INTEGER )&lpTotalNumberOfBytes, ( PULARGE_INTEGER )&lpTotalNumberOfFreeBytes ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ret = lpFreeBytesAvailable;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_GetVideoRam
|
|
|
|
returns in megabytes
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
int Sys_GetVideoRam()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
unsigned int retSize = 64;
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-12-04 01:30:46 +00:00
|
|
|
// RB begin
|
|
|
|
#if !defined(__MINGW32__)
|
2012-11-26 18:58:24 +00:00
|
|
|
CComPtr<IWbemLocator> spLoc = NULL;
|
2012-12-04 11:11:14 +00:00
|
|
|
HRESULT hr = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_SERVER, IID_IWbemLocator, ( LPVOID* ) &spLoc );
|
|
|
|
if( hr != S_OK || spLoc == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return retSize;
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
CComBSTR bstrNamespace( _T( "\\\\.\\root\\CIMV2" ) );
|
|
|
|
CComPtr<IWbemServices> spServices;
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// Connect to CIM
|
|
|
|
hr = spLoc->ConnectServer( bstrNamespace, NULL, NULL, 0, NULL, 0, 0, &spServices );
|
2012-12-04 11:11:14 +00:00
|
|
|
if( hr != WBEM_S_NO_ERROR )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return retSize;
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
|
|
|
// Switch the security level to IMPERSONATE so that provider will grant access to system-level objects.
|
2012-11-26 18:58:24 +00:00
|
|
|
hr = CoSetProxyBlanket( spServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
|
2012-12-04 11:11:14 +00:00
|
|
|
if( hr != S_OK )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return retSize;
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// Get the vid controller
|
|
|
|
CComPtr<IEnumWbemClassObject> spEnumInst = NULL;
|
2012-12-04 11:11:14 +00:00
|
|
|
hr = spServices->CreateInstanceEnum( CComBSTR( "Win32_VideoController" ), WBEM_FLAG_SHALLOW, NULL, &spEnumInst );
|
|
|
|
if( hr != WBEM_S_NO_ERROR || spEnumInst == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return retSize;
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
ULONG uNumOfInstances = 0;
|
|
|
|
CComPtr<IWbemClassObject> spInstance = NULL;
|
|
|
|
hr = spEnumInst->Next( 10000, 1, &spInstance, &uNumOfInstances );
|
2012-12-04 11:11:14 +00:00
|
|
|
|
|
|
|
if( hr == S_OK && spInstance )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// Get properties from the object
|
|
|
|
CComVariant varSize;
|
|
|
|
hr = spInstance->Get( CComBSTR( _T( "AdapterRAM" ) ), 0, &varSize, 0, 0 );
|
2012-12-04 11:11:14 +00:00
|
|
|
if( hr == S_OK )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
retSize = varSize.intVal / ( 1024 * 1024 );
|
2012-12-04 11:11:14 +00:00
|
|
|
if( retSize == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
retSize = 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 01:30:46 +00:00
|
|
|
#endif
|
|
|
|
// RB end
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return retSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_GetCurrentMemoryStatus
|
|
|
|
|
|
|
|
returns OS mem info
|
|
|
|
all values are in kB except the memoryload
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
void Sys_GetCurrentMemoryStatus( sysMemoryStats_t& stats )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
MEMORYSTATUSEX statex = {};
|
|
|
|
unsigned __int64 work;
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
statex.dwLength = sizeof( statex );
|
|
|
|
GlobalMemoryStatusEx( &statex );
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
memset( &stats, 0, sizeof( stats ) );
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
stats.memoryLoad = statex.dwMemoryLoad;
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullTotalPhys >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.totalPhysical = *( int* )&work;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullAvailPhys >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.availPhysical = *( int* )&work;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullAvailPageFile >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.availPageFile = *( int* )&work;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullTotalPageFile >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.totalPageFile = *( int* )&work;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullTotalVirtual >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.totalVirtual = *( int* )&work;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullAvailVirtual >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.availVirtual = *( int* )&work;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
work = statex.ullAvailExtendedVirtual >> 20;
|
2012-12-04 11:11:14 +00:00
|
|
|
stats.availExtendedVirtual = *( int* )&work;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_LockMemory
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
bool Sys_LockMemory( void* ptr, int bytes )
|
|
|
|
{
|
|
|
|
return ( VirtualLock( ptr, ( SIZE_T )bytes ) != FALSE );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_UnlockMemory
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
bool Sys_UnlockMemory( void* ptr, int bytes )
|
|
|
|
{
|
|
|
|
return ( VirtualUnlock( ptr, ( SIZE_T )bytes ) != FALSE );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_SetPhysicalWorkMemory
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
void Sys_SetPhysicalWorkMemory( int minBytes, int maxBytes )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
::SetProcessWorkingSetSize( GetCurrentProcess(), minBytes, maxBytes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_GetCurrentUser
|
|
|
|
================
|
|
|
|
*/
|
2012-12-04 11:11:14 +00:00
|
|
|
char* Sys_GetCurrentUser()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
static char s_userName[1024];
|
|
|
|
unsigned long size = sizeof( s_userName );
|
2012-12-04 11:11:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( !GetUserName( s_userName, &size ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
strcpy( s_userName, "player" );
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
|
|
|
if( !s_userName[0] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
strcpy( s_userName, "player" );
|
|
|
|
}
|
2012-12-04 11:11:14 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return s_userName;
|
2012-12-04 11:11:14 +00:00
|
|
|
}
|
2012-11-26 18:58:24 +00:00
|
|
|
|