Issue #45. Moved process creation code to helpers, other misc changes.

This commit is contained in:
WrinklyNinja
2013-08-12 15:27:00 +01:00
parent 2118886da2
commit 397ea3e7ff
9 changed files with 97 additions and 90 deletions
+2
View File
@@ -51,6 +51,8 @@ namespace boss {
static const unsigned int no_game_detected = 10;
static const unsigned int subversion_error = 11;
static const unsigned int git_error = 12;
static const unsigned int windows_error = 13;
static const unsigned int sorting_error = 14;
private:
std::string _what;
unsigned int _code;
+4
View File
@@ -217,6 +217,10 @@ namespace boss {
return g_path_local / bossFolderName / "report.html";
}
fs::path Game::GraphPath() const {
return g_path_local / bossFolderName / "graph.svg";
}
void Game::RefreshActivePluginsList() {
lo_game_handle gh;
char ** pluginArr;
+1
View File
@@ -67,6 +67,7 @@ namespace boss {
boost::filesystem::path MasterlistPath() const;
boost::filesystem::path UserlistPath() const;
boost::filesystem::path ReportPath() const;
boost::filesystem::path GraphPath() const;
bool IsActive(const std::string& plugin) const;
-1
View File
@@ -62,5 +62,4 @@ namespace boss {
const boost::filesystem::path g_path_log = g_path_local / "BOSSDebugLog.txt";
const boost::filesystem::path g_path_l10n = "resources/l10n";
const boost::filesystem::path g_path_graphvis = "resources/graphvis/dot.exe";
const boost::filesystem::path g_path_graph = g_path_local / "graph.svg";
}
-1
View File
@@ -63,7 +63,6 @@ namespace boss {
extern const boost::filesystem::path g_path_log;
extern const boost::filesystem::path g_path_l10n;
extern const boost::filesystem::path g_path_graphvis;
extern const boost::filesystem::path g_path_graph;
}
#endif
+1 -1
View File
@@ -47,7 +47,7 @@ namespace boss {
vertex_t vSource = boost::source(e, g);
vertex_t vTarget = boost::target(e, g);
throw boss::error(boss::error::invalid_args, "Back edge detected between plugins \"" + g[vSource]->Name() + "\" and \"" + g[vTarget]->Name() + "\".");
throw boss::error(boss::error::sorting_error, "Back edge detected between plugins \"" + g[vSource]->Name() + "\" and \"" + g[vTarget]->Name() + "\".");
}
};
+86
View File
@@ -29,6 +29,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/crc.hpp>
#include <boost/regex.hpp>
#include <boost/log/trivial.hpp>
#include <alphanum.hpp>
@@ -50,6 +51,7 @@
# include "windows.h"
# include "shlobj.h"
#endif
#define BUFSIZE 4096
namespace boss {
using namespace std;
@@ -264,6 +266,90 @@ namespace boss {
return g_lang_any;
}
//Runs a command using the Win32 API.
bool RunCommand(const std::string& command, std::string& output) {
HANDLE consoleWrite = NULL;
HANDLE consoleRead = NULL;
SECURITY_ATTRIBUTES saAttr;
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
CHAR chBuf[BUFSIZE];
DWORD dwRead;
DWORD exitCode;
//Init attributes.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
BOOST_LOG_TRIVIAL(trace) << "Creating a pipe for the process.";
//Create I/O pipes.
if (!CreatePipe(&consoleRead, &consoleWrite, &saAttr, 0)) {
BOOST_LOG_TRIVIAL(error) << "Could not create pipe for process.";
throw error(error::windows_error, "Could not create pipe for process.");
}
//Create a child process.
BOOST_LOG_TRIVIAL(trace) << "Creating a child process.";
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = consoleWrite;
siStartInfo.hStdOutput = consoleWrite;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
siStartInfo.wShowWindow = SW_HIDE;
const int utf16Len = MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, NULL, 0);
wchar_t * cmdLine = new wchar_t[utf16Len];
MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, cmdLine, utf16Len);
bool result = CreateProcess(NULL,
cmdLine, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
CREATE_NO_WINDOW, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
delete [] cmdLine;
if (!result) {
BOOST_LOG_TRIVIAL(error) << "Could not create process.";
throw error(error::windows_error, "Could not create process.");
}
BOOST_LOG_TRIVIAL(trace) << "Waiting for process to complete.";
WaitForSingleObject(piProcInfo.hProcess, INFINITE);
BOOST_LOG_TRIVIAL(trace) << "Getting the process exit code.";
if (!GetExitCodeProcess(piProcInfo.hProcess, &exitCode)) {
BOOST_LOG_TRIVIAL(error) << "Could not get process exit code.";
throw error(error::windows_error, "Could not get process exit code.");
}
BOOST_LOG_TRIVIAL(trace) << "Getting the process output.";
if (!ReadFile(consoleRead, chBuf, BUFSIZE, &dwRead, NULL)) {
BOOST_LOG_TRIVIAL(error) << "Could not read process output.";
throw error(error::windows_error, "Could not read process output.");
}
output = string(chBuf, dwRead);
return exitCode == 0;
}
//////////////////////////////
// Version Class Functions
+3
View File
@@ -69,6 +69,9 @@ namespace boss {
std::string GetLangString(const unsigned int num);
unsigned int GetLangNum(const std::string& str);
//Runs a command using the Win32 API.
bool RunCommand(const std::string& command, std::string& output);
//Version class for more robust version comparisons.
class Version {
private:
-87
View File
@@ -31,99 +31,12 @@
#include <git2.h>
#if _WIN32 || _WIN64
# ifndef UNICODE
# define UNICODE
# endif
# ifndef _UNICODE
# define _UNICODE
# endif
# include "windows.h"
# include "shlobj.h"
#endif
#define BUFSIZE 4096
using namespace std;
namespace fs = boost::filesystem;
namespace boss {
bool RunCommand(const std::string& command, std::string& output) {
HANDLE consoleWrite = NULL;
HANDLE consoleRead = NULL;
SECURITY_ATTRIBUTES saAttr;
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
CHAR chBuf[BUFSIZE];
DWORD dwRead;
DWORD exitCode;
//Init attributes.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
//Create I/O pipes.
if (!CreatePipe(&consoleRead, &consoleWrite, &saAttr, 0)) {
BOOST_LOG_TRIVIAL(error) << "Could not create pipe for Subversion process.";
throw error(error::subversion_error, "Could not create pipe for Subversion process.");
}
//Create a child process.
BOOST_LOG_TRIVIAL(trace) << "Creating a child process.";
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = consoleWrite;
siStartInfo.hStdOutput = consoleWrite;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
siStartInfo.wShowWindow = SW_HIDE;
const int utf16Len = MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, NULL, 0);
wchar_t * cmdLine = new wchar_t[utf16Len];
MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, cmdLine, utf16Len);
bool result = CreateProcess(NULL,
cmdLine, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
delete [] cmdLine;
if (!result) {
BOOST_LOG_TRIVIAL(error) << "Could not create Subversion process.";
throw error(error::subversion_error, "Could not create Subversion process.");
}
WaitForSingleObject(piProcInfo.hProcess, INFINITE);
if (!GetExitCodeProcess(piProcInfo.hProcess, &exitCode)) {
BOOST_LOG_TRIVIAL(error) << "Could not get Subversion process exit code.";
throw error(error::subversion_error, "Could not get Subversion process exit code.");
}
if (!ReadFile(consoleRead, chBuf, BUFSIZE, &dwRead, NULL)) {
BOOST_LOG_TRIVIAL(error) << "Could not read Subversion process output.";
throw error(error::subversion_error, "Could not read Subversion process output.");
}
output = string(chBuf, dwRead);
return exitCode == 0;
}
//Gets revision + date string.
string GetRevision(const std::string& buffer) {
string revision, date;