readd some win32 files so that we can more easily run static analyzers

that are windows only against the source
This commit is contained in:
DarthArgus
2016-07-23 18:00:40 -07:00
parent fd56145248
commit efaaa4bb42
28 changed files with 4965 additions and 0 deletions
@@ -0,0 +1,22 @@
// ConsoleInput.cpp
// Copyright 2000-02, Sony Online Entertainment Inc., all rights reserved.
// Author: Justin Randall
//-----------------------------------------------------------------------
#include "FirstTaskManager.h"
#include "Console.h"
#include <conio.h>
//-----------------------------------------------------------------------
const char Console::getNextChar()
{
char result = 0;
if(_kbhit())
result = static_cast<char>(_getche());
return result;
}
//-----------------------------------------------------------------------
@@ -0,0 +1,33 @@
#include "FirstTaskManager.h"
namespace EnvironmentVariable
{
bool addToEnvironmentVariable(const char* key, const char* value)
{
bool retval = false;
char oldValue[256];
DWORD tmp = GetEnvironmentVariable(key, oldValue, sizeof(oldValue));
if (tmp != 0)
{
std::string s(oldValue);
s += ";";
s += value;
//Bad things happen if the first character happens to be ; (ie from an empty environment string)
const char* newValue = s.c_str();
if (newValue[0] == ';')
++newValue;
retval = (SetEnvironmentVariable(key, newValue) != 0);
}
else
{
retval = (SetEnvironmentVariable(key, value) != 0);
}
return retval;
}
bool setEnvironmentVariable(const char* key, const char* value)
{
return (SetEnvironmentVariable(key, value) != 0);
}
};
@@ -0,0 +1,153 @@
#include "FirstTaskManager.h"
#include "sharedFoundation/FirstSharedFoundation.h"
#include "ProcessSpawner.h"
#include <map>
#include <string>
#include "TaskManager.h"
#include <stdio.h>
uint32 ProcessSpawner::prefix;
std::map<uint32, HANDLE> procById;
//-----------------------------------------------------------------------
bool tokenize (const std::string & str, std::vector<std::string> & result)
{
size_t end_pos = 0;
size_t start_pos = 0;
result.clear ();
for (;;)
{
if (end_pos >= str.size ())
break;
start_pos = str.find_first_not_of (' ', end_pos);
if (start_pos == str.npos)
break;
//----------------------------------------------------------------------
if (str [start_pos] == '\"')
{
if (++start_pos >= str.size ())
break;
end_pos = str.find_first_of ('\"', start_pos);
}
else
end_pos = str.find_first_of (' ', start_pos);
//----------------------------------------------------------------------
if (start_pos == end_pos)
break;
if (end_pos == str.npos)
{
result.push_back (str.substr (start_pos));
break;
}
else
result.push_back (str.substr (start_pos, end_pos - start_pos));
++start_pos;
}
return true;
}
uint32 ProcessSpawner::execute(const std::string & processName, const std::vector<std::string> & parameters)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
char cmd[1024] = {"\0"};
std::string cmdLine;
cmdLine = processName.c_str();
cmdLine += " ";
std::vector<std::string>::const_iterator i;
for(i = parameters.begin(); i != parameters.end(); ++i)
{
cmdLine += (*i).c_str();
cmdLine += " ";
}
_snprintf(cmd, 1024, "%s.exe", processName.c_str());
// _snprintf(cmd, 1024, "%s", processName.c_str());
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
const int result = CreateProcess(cmd, const_cast<char *>(cmdLine.c_str()), NULL, NULL, false, 0, 0, 0, &si, &pi);
UNREF (result);
#ifdef _DEBUG
if (!result)
{
DWORD iErr = GetLastError();
char * errStr = strerror(iErr);
DEBUG_REPORT_LOG(true, ("ProcessSpawner: %s - %s\n", cmd, errStr));
}
#endif
procById.insert(std::pair<uint32, HANDLE>(pi.dwProcessId, pi.hProcess));
return pi.dwProcessId;
}
//-----------------------------------------------------------------------
uint32 ProcessSpawner::execute(const std::string & cmd)
{
std::vector<std::string> args;
size_t firstArg = cmd.find_first_of(" ");
std::string processName;
if(firstArg < cmd.size())
{
std::string a = cmd.substr(firstArg + 1);
tokenize(a, args);
processName = cmd.substr(0, firstArg);
}
else
{
processName = cmd;
}
return execute(processName, args);
}
//-----------------------------------------------------------------------
bool ProcessSpawner::isProcessActive(uint32 pid)
{
bool result = false;
std::map<uint32, HANDLE>::const_iterator f = procById.find(pid);
if(f != procById.end())
{
DWORD exitCode;
GetExitCodeProcess((*f).second, &exitCode);
result = (exitCode == STILL_ACTIVE);
}
return result;
}
//-----------------------------------------------------------------------
void ProcessSpawner::kill(uint32 pid)
{
HANDLE p = OpenProcess(PROCESS_TERMINATE, false, (DWORD)pid);
if(p)
TerminateProcess(p, 0);
}
//-----------------------------------------------------------------------
void ProcessSpawner::forceCore(const unsigned long pid)
{
ProcessSpawner::kill(pid);
}
//-----------------------------------------------------------------------
@@ -0,0 +1,147 @@
// TaskManagerSysInfo.cpp
// Copyright 2000-02, Sony Online Entertainment Inc., all rights reserved.
// Author: Justin Randall
//-----------------------------------------------------------------------
#include "FirstTaskManager.h"
#include "TaskManagerSysInfo.h"
#pragma warning ( disable : 4201)
#include <windows.h>
#include <tlhelp32.h>
//-----------------------------------------------------------------------
TaskManagerSysInfo::TaskManagerSysInfo() :
averageScore()
{
update();
}
//-----------------------------------------------------------------------
TaskManagerSysInfo::TaskManagerSysInfo(const TaskManagerSysInfo &)
{
}
//-----------------------------------------------------------------------
TaskManagerSysInfo::~TaskManagerSysInfo()
{
}
//-----------------------------------------------------------------------
TaskManagerSysInfo & TaskManagerSysInfo::operator = (const TaskManagerSysInfo & rhs)
{
if(this != &rhs)
{
// make assignments if right hand side is not this instance
}
return *this;
}
//-----------------------------------------------------------------------
const float TaskManagerSysInfo::getScore() const
{
std::list<float>::const_iterator i;
float avg = 0.0f;
for(i = averageScore.begin(); i != averageScore.end(); ++i)
{
avg += (*i);
}
avg = avg / averageScore.size();
return avg;
}
//-----------------------------------------------------------------------
void TaskManagerSysInfo::update()
{
static int64 activeTime[2] = {0};
static int64 currentTime[2] = {0};
float currentScore = 0.0f;
activeTime[0] = activeTime[1];
currentTime[0] = currentTime[1];
activeTime[1] = 0;
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
double procAvg = 0.0f;
MEMORYSTATUS memStat;
GlobalMemoryStatus(&memStat);
currentScore = static_cast<float>(static_cast<float>(memStat.dwMemoryLoad) * 0.005f);
if(hProcessSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32))
{
do
{
HANDLE proc = OpenProcess(PROCESS_QUERY_INFORMATION, false, pe32.th32ProcessID);
// some stuf with the enumerated processes
FILETIME createTime = {0};
FILETIME exitTime = {0};
FILETIME kernelTime = {0};
FILETIME userTime = {0};
GetProcessTimes(proc, &createTime, &exitTime, &kernelTime, &userTime);
int64 totals;
// SDK docs say:
// It is not recommended that you add and subtract values
// from the FILETIME structure to obtain relative times. Instead, you should
// Copy the resulting FILETIME structure to a ULARGE_INTEGER structure.
// Use normal 64-bit arithmetic on the ULARGE_INTEGER value.
int64 c;
int64 e;
int64 k;
int64 u;
memcpy(&c, &createTime, sizeof(int64));
memcpy(&e, &exitTime, sizeof(int64));
memcpy(&k, &kernelTime, sizeof(int64));
memcpy(&u, &userTime, sizeof(int64));
totals = k + u;
FILETIME fst;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &fst);
int64 runTime;
memcpy(&runTime, &fst, sizeof(int64));
runTime = runTime - c;
if(c || e || k || u)
{
activeTime[1] += k + e;
}
}
while (Process32Next(hProcessSnap, &pe32));
}
}
FILETIME fst;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &fst);
memcpy(&currentTime[1], &fst, sizeof(int64));
int64 timeSlice = currentTime[1] - currentTime[0];
int64 activeSlice = activeTime[1] - activeTime[0];
procAvg = static_cast<double>(static_cast<double>(activeSlice) / timeSlice);
//REPORT_LOG(true, ("%f\n", procAvg));
currentScore = currentScore + static_cast<float>(procAvg * 0.5);
averageScore.insert(averageScore.end(), currentScore);
if(averageScore.size() > 100)
averageScore.erase(averageScore.begin());
}
//-----------------------------------------------------------------------
@@ -0,0 +1,50 @@
#include "FirstTaskManager.h"
#include "sharedFoundation/FirstSharedFoundation.h"
#include "sharedCompression/SetupSharedCompression.h"
#include "sharedDebug/SetupSharedDebug.h"
#include "sharedFile/SetupSharedFile.h"
#include "sharedFoundation/ConfigFile.h"
#include "sharedFoundation/SetupSharedFoundation.h"
#include "sharedNetworkMessages/SetupSharedNetworkMessages.h"
#include "sharedRandom/SetupSharedRandom.h"
#include "sharedThread/SetupSharedThread.h"
#include "TaskManager.h"
#include "ConfigTaskManager.h"
//=====================================================================
int main(int argc, char **argv)
{
SetupSharedThread::install();
SetupSharedDebug::install(1024);
//-- setup foundation
SetupSharedFoundation::Data setupFoundationData(SetupSharedFoundation::Data::D_game);
setupFoundationData.argc = argc;
setupFoundationData.argv = argv;
setupFoundationData.createWindow = false;
setupFoundationData.clockUsesSleep = true;
SetupSharedFoundation::install (setupFoundationData);
SetupSharedCompression::install();
SetupSharedFile::install(false);
SetupSharedNetworkMessages::install();
SetupSharedRandom::install(static_cast<uint32>(time(NULL))); //lint !e1924 !e64 // NULL is a C-Style cast?
ConfigTaskManager::install();
TaskManager::install();
//-- run server
SetupSharedFoundation::callbackWithExceptionHandling(TaskManager::run);
TaskManager::remove();
SetupSharedFoundation::remove();
SetupSharedThread::remove();
return 0;
}
//=====================================================================