Added the ability to tell mega-glest where to look for glest.ini, servers.ini and all of the various .log files. The new optional environment variable that mega-glest will look for is:

GLESTHOME
This commit is contained in:
Mark Vejvoda 2010-03-21 23:40:35 +00:00
parent 9756217445
commit 344c4778d1
14 changed files with 434 additions and 26 deletions

View File

@ -25,6 +25,14 @@ Configuration::~Configuration(){
}
void Configuration::load(const string &path){
if(fileExists(path) == false) {
throw runtime_error("Cannot find file: " + path);
}
else if(fileExists(fileName) == false) {
throw runtime_error("Cannot find file: " + fileName);
}
loadStructure(path);
loadValues(fileName);
}

View File

@ -15,9 +15,12 @@
#include "lang.h"
#include "game_constants.h"
#include "config.h"
#include <stdlib.h>
#include "platform_util.h"
#include "leak_dumper.h"
using namespace Shared::Util;
using namespace Shared::Platform;
namespace Glest{ namespace Game{
@ -105,4 +108,18 @@ string formatString(const string &str){
return outStr;
}
string getGameReadWritePath() {
string path = "";
if(getenv("GLESTHOME") != NULL) {
path = getenv("GLESTHOME");
if(path != "" && EndsWith(path, "/") == false && EndsWith(path, "\\") == false) {
path += "/";
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
}
return path;
}
}}//end namespace

View File

@ -0,0 +1,42 @@
// ==============================================================
// This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Martiño Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _GLEST_GAME_GAMEUTIL_H_
#define _GLEST_GAME_GAMEUTIL_H_
#include <string>
#include <vector>
#include "util.h"
using std::string;
using Shared::Util::sharedLibVersionString;
namespace Glest{ namespace Game{
extern const string mailString;
extern const string glestVersionString;
extern const string networkVersionString;
string getCrashDumpFileName();
string getNetworkVersionString();
string getAboutString1(int i);
string getAboutString2(int i);
string getTeammateName(int i);
string getTeammateRole(int i);
string formatString(const string &str);
string getGameReadWritePath();
}}//end namespace
#endif

View File

@ -12,12 +12,13 @@
#include "config.h"
#include "util.h"
#include "leak_dumper.h"
#include "game_constants.h"
#include "platform_util.h"
#include "game_util.h"
#include "leak_dumper.h"
using namespace Shared::Platform;
using namespace Shared::Util;
namespace Glest{ namespace Game{
@ -34,7 +35,15 @@ const char *GameConstants::folder_path_tutorials = "tutorials";
// =====================================================
Config::Config(){
properties.load("glest.ini");
string cfgFile = "glest.ini";
if(getGameReadWritePath() != "") {
cfgFile = getGameReadWritePath() + cfgFile;
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile = [%s]\n",__FILE__,__FUNCTION__,__LINE__,cfgFile.c_str());
properties.load(cfgFile);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile = [%s]\n",__FILE__,__FUNCTION__,__LINE__,cfgFile.c_str());
}
Config &Config::getInstance(){

View File

@ -54,7 +54,7 @@ public:
program->showMessage(msg);
}
else {
message("An error ocurred and Glest will close.\nPlease report this bug to "+mailString+", attaching the generated "+getCrashDumpFileName()+" file.");
message("An error ocurred and Glest will close.\nError msg = [" + (msg != NULL ? string(msg) : string("?")) + "]\n\nPlease report this bug to "+mailString+", attaching the generated "+getCrashDumpFileName()+" file.");
}
exit(0);
@ -152,6 +152,9 @@ void MainWindow::eventClose(){
int glestMain(int argc, char** argv){
SystemFlags::enableNetworkDebugInfo = true;
SystemFlags::enableDebugText = true;
MainWindow *mainWindow= NULL;
Program *program= NULL;
ExceptionHandler exceptionHandler;
@ -160,6 +163,8 @@ int glestMain(int argc, char** argv){
try{
Config &config = Config::getInstance();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
SystemFlags::enableNetworkDebugInfo = config.getBool("DebugNetwork","0");
SystemFlags::enableDebugText = config.getBool("DebugMode","0");

View File

@ -278,7 +278,11 @@ void Program::init(WindowGl *window, bool initSound){
//log start
Logger &logger= Logger::getInstance();
logger.setFile("glest.log");
string logFile = "glest.log";
if(getGameReadWritePath() != "") {
logFile = getGameReadWritePath() + logFile;
}
logger.setFile(logFile);
logger.clear();
//lang

View File

@ -44,7 +44,12 @@ MenuStateJoinGame::MenuStateJoinGame(Program *program, MainMenu *mainMenu, bool
Config &config= Config::getInstance();
NetworkManager &networkManager= NetworkManager::getInstance();
servers.load(serverFileName);
serversSavedFile = serverFileName;
if(getGameReadWritePath() != "") {
serversSavedFile = getGameReadWritePath() + serversSavedFile;
}
servers.load(serversSavedFile);
//buttons
buttonReturn.init(325, 300, 125);
@ -317,7 +322,7 @@ void MenuStateJoinGame::update()
{
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] clientInterface->getLaunchGame() - A\n",__FILE__,__FUNCTION__);
servers.save(serverFileName);
servers.save(serversSavedFile);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] clientInterface->getLaunchGame() - B\n",__FILE__,__FUNCTION__);

View File

@ -0,0 +1,69 @@
// ==============================================================
// This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2005 Martiño Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _GLEST_GAME_MENUSTATEJOINGAME_H_
#define _GLEST_GAME_MENUSTATEJOINGAME_H_
#include "properties.h"
#include "main_menu.h"
#include "chat_manager.h"
using Shared::Util::Properties;
namespace Glest{ namespace Game{
class NetworkMessageIntro;
// ===============================
// class MenuStateJoinGame
// ===============================
class MenuStateJoinGame: public MenuState{
private:
static const int newServerIndex;
static const string serverFileName;
private:
GraphicButton buttonReturn;
GraphicButton buttonConnect;
GraphicLabel labelServer;
GraphicLabel labelServerType;
GraphicLabel labelServerIp;
GraphicLabel labelStatus;
GraphicLabel labelInfo;
GraphicListBox listBoxServerType;
GraphicListBox listBoxServers;
bool connected;
int playerIndex;
Properties servers;
Console console;
ChatManager chatManager;
string serversSavedFile;
public:
MenuStateJoinGame(Program *program, MainMenu *mainMenu, bool connect= false, Ip serverIp= Ip());
void mouseClick(int x, int y, MouseButton mouseButton);
void mouseMove(int x, int y, const MouseState *mouseState);
void render();
void update();
virtual void keyDown(char key);
virtual void keyPress(char c);
private:
void connectToServer();
};
}}//end namespace
#endif

View File

@ -233,13 +233,13 @@ bool isdir(const char *path)
bool EndsWith(const string &str, const string& key)
{
size_t keylen = key.length();
size_t strlen = str.length();
bool result = false;
if (str.length() > key.length()) {
result = (0 == str.compare (str.length() - key.length(), key.length(), key));
}
if(keylen <= strlen)
return string::npos != str.rfind(key.c_str(),strlen - keylen, keylen);
else
return false;
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] result [%d] str = [%s] key = [%s]\n",__FILE__,__FUNCTION__,result,str.c_str(),key.c_str());
return result;
}
//finds all filenames like path and gets their checksum of all files combined

View File

@ -270,13 +270,13 @@ bool isdir(const char *path)
bool EndsWith(const string &str, const string& key)
{
size_t keylen = key.length();
size_t strlen = str.length();
if(keylen <= strlen)
return string::npos != str.rfind(key.c_str(),strlen - keylen, keylen);
else
return false;
bool result = false;
if (str.length() > key.length()) {
result = (0 == str.compare (str.length() - key.length(), key.length(), key));
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] result [%d] str = [%s] key = [%s]\n",__FILE__,__FUNCTION__,result,str.c_str(),key.c_str());
return result;
}
//finds all filenames like path and gets their checksum of all files combined
@ -475,7 +475,7 @@ void getFullscreenVideoInfo(int &colorBits,int &screenWidth,int &screenHeight) {
//const SDL_VideoInfo* vidInfo = SDL_GetVideoInfo();
//colorBits = vidInfo->vfmt->BitsPerPixel;
//screenWidth = vidInfo->current_w;
/*
//screenHeight = vidInfo->current_h;
int cx = GetSystemMetrics(SM_CXVIRTUALSCREEN);

View File

@ -155,9 +155,6 @@ const char* WSAGetLastErrorMessage(const char* pcMessagePrefix,
namespace Shared{ namespace Platform{
bool Socket::enableDebugText = true;
bool Socket::enableNetworkDebugInfo = true;
// =====================================================
// class Ip
// =====================================================

View File

@ -0,0 +1,129 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martiño Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "leak_dumper.h"
#ifdef SL_LEAK_DUMP
AllocInfo::AllocInfo(){
ptr= NULL;
file= "";
line= -1;
bytes= -1;
array= false;
free= true;
}
AllocInfo::AllocInfo(void* ptr, const char* file, int line, size_t bytes, bool array){
this->ptr= ptr;
this->file= file;
this->line= line;
this->bytes= bytes;
this->array= array;
free= false;
}
// =====================================================
// class AllocRegistry
// =====================================================
// ===================== PRIVATE =======================
AllocRegistry::AllocRegistry(){
allocCount= 0;
allocBytes= 0;
nonMonitoredCount= 0;
nonMonitoredBytes= 0;
}
// ===================== PUBLIC ========================
AllocRegistry &AllocRegistry::getInstance(){
static AllocRegistry allocRegistry;
return allocRegistry;
}
AllocRegistry::~AllocRegistry(){
string leakLog = "leak_dump.log";
if(getGameReadWritePath() != "") {
leakLog = getGameReadWritePath() + leakLog;
}
dump(leakLog.c_str());
}
void AllocRegistry::allocate(AllocInfo info){
++allocCount;
allocBytes+= info.bytes;
unsigned hashCode= reinterpret_cast<unsigned>(info.ptr) % maxAllocs;
for(int i=hashCode; i<maxAllocs; ++i){
if(allocs[i].free){
allocs[i]= info;
return;
}
}
for(int i=0; i<hashCode; ++i){
if(allocs[i].free){
allocs[i]= info;
return;
}
}
++nonMonitoredCount;
nonMonitoredBytes+= info.bytes;
}
void AllocRegistry::deallocate(void* ptr, bool array){
unsigned hashCode= reinterpret_cast<unsigned>(ptr) % maxAllocs;
for(int i=hashCode; i<maxAllocs; ++i){
if(!allocs[i].free && allocs[i].ptr==ptr && allocs[i].array==array){
allocs[i].free= true;
return;
}
}
for(int i=0; i<hashCode; ++i){
if(!allocs[i].free && allocs[i].ptr==ptr && allocs[i].array==array){
allocs[i].free= true;
return;
}
}
}
void AllocRegistry::reset(){
for(int i=0; i<maxAllocs; ++i){
allocs[i]= AllocInfo();
}
}
void AllocRegistry::dump(const char *path){
FILE *f= fopen(path, "wt");
int leakCount=0;
size_t leakBytes=0;
fprintf(f, "Memory leak dump\n\n");
for(int i=0; i<maxAllocs; ++i){
if(!allocs[i].free){
leakBytes+= allocs[i].bytes;
fprintf(f, "%d.\tfile: %s, line: %d, bytes: %d, array: %d\n", ++leakCount, allocs[i].file, allocs[i].line, allocs[i].bytes, allocs[i].array);
}
}
fprintf(f, "\nTotal leaks: %d, %d bytes\n", leakCount, leakBytes);
fprintf(f, "Total allocations: %d, %d bytes\n", allocCount, allocBytes);
fprintf(f, "Not monitored allocations: %d, %d bytes\n", nonMonitoredCount, nonMonitoredBytes);
fclose(f);
}
#endif

View File

@ -0,0 +1,117 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martiño Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "profiler.h"
#ifdef SL_PROFILE
#include <stdexcept>
using namespace std;
namespace Shared{ namespace Util{
// =====================================================
// class Section
// =====================================================
Section::Section(const string &name){
this->name= name;
milisElapsed= 0;
parent= NULL;
}
Section *Section::getChild(const string &name){
SectionContainer::iterator it;
for(it= children.begin(); it!=children.end(); ++it){
if((*it)->getName()==name){
return *it;
}
}
return NULL;
}
void Section::print(FILE *outStream, int tabLevel){
float percent= (parent==NULL || parent->milisElapsed==0)? 100.0f: 100.0f*milisElapsed/parent->milisElapsed;
string pname= parent==NULL? "": parent->getName();
for(int i=0; i<tabLevel; ++i)
fprintf(outStream, "\t");
fprintf(outStream, "%s: ", name.c_str());
fprintf(outStream, "%d ms, ", milisElapsed);
fprintf(outStream, "%.1f%s\n", percent, "%");
SectionContainer::iterator it;
for(it= children.begin(); it!=children.end(); ++it){
(*it)->print(outStream, tabLevel+1);
}
}
// =====================================================
// class Profiler
// =====================================================
Profiler::Profiler(){
rootSection= new Section("Root");
currSection= rootSection;
rootSection->start();
}
Profiler::~Profiler(){
rootSection->stop();
string profileLog = "profiler.log";
if(getGameReadWritePath() != "") {
profileLog = getGameReadWritePath() + profileLog;
}
FILE *f= fopen(profileLog.c_str(), "w");
if(f==NULL)
throw runtime_error("Can not open file: " + profileLog);
fprintf(f, "Profiler Results\n\n");
rootSection->print(f);
fclose(f);
}
Profiler &Profiler::getInstance(){
static Profiler profiler;
return profiler;
}
void Profiler::sectionBegin(const string &name){
Section *childSection= currSection->getChild(name);
if(childSection==NULL){
childSection= new Section(name);
currSection->addChild(childSection);
childSection->setParent(currSection);
}
currSection= childSection;
childSection->start();
}
void Profiler::sectionEnd(const string &name){
if(name==currSection->getName()){
currSection->stop();
currSection= currSection->getParent();
}
else{
throw runtime_error("Profile: Leaving section is not current section: "+name);
}
}
}};//end namespace
#endif

View File

@ -16,6 +16,7 @@
#include <cstring>
#include "conversion.h"
#include "util.h"
#include "leak_dumper.h"
using namespace std;
@ -29,17 +30,22 @@ namespace Shared{ namespace Util{
void Properties::load(const string &path){
ifstream fileStream;
char lineBuffer[maxLine];
char lineBuffer[maxLine]="";
string line, key, value;
int pos;
int pos=0;
this->path= path;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
fileStream.open(path.c_str(), ios_base::in);
if(fileStream.fail()){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
throw runtime_error("Can't open propertyMap file: " + path);
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
propertyMap.clear();
while(!fileStream.eof()){
fileStream.getline(lineBuffer, maxLine);