From fd1d4e1b327a0dfe7b6df306b01eb355d627a291 Mon Sep 17 00:00:00 2001 From: albertzeyer Date: Fri, 24 Jul 2009 16:42:43 +0000 Subject: [PATCH] some fixes (removed all chdir) git-svn-id: https://clonekeenplus.svn.sourceforge.net/svnroot/clonekeenplus/cgenius/trunk@153 4df4b0f3-56ce-47cb-b001-ed939b7d65a6 --- src/CLogFile.cpp | 3 + src/fileio.cpp | 55 ------------------- src/fileio/CPatcher.cpp | 100 ++++++++++------------------------ src/fileio/CPatcher.h | 2 +- src/include/fileio.h | 1 - src/vorticon/CEGAGraphics.cpp | 9 +-- 6 files changed, 37 insertions(+), 133 deletions(-) diff --git a/src/CLogFile.cpp b/src/CLogFile.cpp index 93fcde916..f62b881f3 100644 --- a/src/CLogFile.cpp +++ b/src/CLogFile.cpp @@ -11,6 +11,8 @@ #include "CLogFile.h" #include #include "FindFile.h" +#include "Debug.h" + CLogFile::CLogFile() {} @@ -109,6 +111,7 @@ void CLogFile::textOut(int Color, bool List, const char *Text) void CLogFile::textOut(const char *Text) { + notes << Text << endl; fprintf(m_Logfile,"%s",Text); fflush(m_Logfile); } diff --git a/src/fileio.cpp b/src/fileio.cpp index 36b26c4ee..a09f1a563 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -38,55 +38,6 @@ void addmaptile(unsigned int t) } } -bool renameFilenamesLowerCase(const std::string& dir_name) -{ -#ifdef TARGET_WIN32 - return true; -#else - DIR *p_Dir; - char newname[256]; - char buf[256]; - struct dirent *dp; - - memset(newname,0,256); - strcpy(buf,dir_name.c_str()); - - if(buf[0] == '\0') - strcpy(buf,"./"); - - chdir("data"); - - if((p_Dir = opendir(buf))==NULL) - return false; - - bool retval = true; - // This function checks if all the files in the directory are lower case. - // If they aren't rename them - while((dp = readdir(p_Dir)) != NULL) - { - if(dp->d_type == DT_REG) - { - strcpy(newname,dp->d_name); - - int len = strlen(newname); - for(int pos=0 ; pos < len ; pos++ ) - if(newname[pos] >= 'A' && newname[pos] <= 'Z') - newname[pos] += ('a'-'A'); - - if(strncmp(newname,dp->d_name,strlen(dp->d_name)) != 0) - if(rename(dp->d_name,newname) != -1) - retval &= true; - } - } - - closedir(p_Dir); - - chdir("../"); - - return retval; - -#endif -} short checkConsistencyofGameData(stGameData *p_GameData) { @@ -129,12 +80,6 @@ short checkConsistencyofGameData(stGameData *p_GameData) p_GameData->FileList[24] = "sounds.ck" + itoa(p_GameData->Episode); } - // Rename all the the files in the directory to lower case - if(!renameFilenamesLowerCase(p_GameData->DataDirectory)) - { - g_pLogFile->ftextOut(PURPLE,"WARNING: There was an error while trying to format correctly the game data."); - g_pLogFile->ftextOut(PURPLE,"Please check, if you have write permissions on the game data directory and it is accessible
"); - } // Finally check if they really exist! int c=0; diff --git a/src/fileio/CPatcher.cpp b/src/fileio/CPatcher.cpp index 194d5480a..91287394c 100644 --- a/src/fileio/CPatcher.cpp +++ b/src/fileio/CPatcher.cpp @@ -10,22 +10,16 @@ #include #include #include "../FindFile.h" +#include "../StringUtils.h" CPatcher::CPatcher(int episode, int version,unsigned char *data, const std::string& datadir) { m_episode = episode; m_version = version; m_data = data; m_datadirectory = datadir; - if(m_datadirectory != "") m_datadirectory += "/"; } -CPatcher::~CPatcher() { - while(!m_TextList.empty()) - { - delete *(m_TextList.begin()); - m_TextList.pop_front(); - } -} +CPatcher::~CPatcher() {} void CPatcher::patchMemory() { @@ -35,49 +29,40 @@ void CPatcher::patchMemory() // then read out of the list the patch commands and apply them to the // Exe-file data m_data - // change to the proper directory - chdir("data"); - chdir(m_datadirectory.c_str()); - // TODO: Extend this part further with more commands while(!m_TextList.empty()) { - char line[256]; + std::string line = *m_TextList.begin(); - strcpy(line,*(m_TextList.begin())); - - if(strncmp(line,"\%version",strlen("\%version")) == 0) + if(strCaseStartsWith(line,"\%version")) { - char *verstring; - char detected_version[5]; + std::string verstring = line.substr(strlen("\%version")); - verstring = line + strlen("\%version"); - - sscanf(verstring,"%s",detected_version); - - if((!strcmp(detected_version,"1.31") && m_version == 131 ) - || (!strcmp(detected_version,"1.1") && m_version == 110 ) - || !strcmp(detected_version,"ALL")) + if((stringcaseequal(verstring,"1.31") && m_version == 131 ) + || (stringcaseequal(verstring,"1.1") && m_version == 110 ) + || stringcaseequal(verstring,"ALL")) { while(!m_TextList.empty()) { // Get the next line - strcpy(line,*(m_TextList.begin())); + line = *m_TextList.begin(); // Now we really start to process the commands - if( strncmp(line,"\%patchfile",strlen("\%patchfile")) == 0 ) + if( strCaseStartsWith(line,"\%patchfile") ) { - unsigned long offset; - char *newbuf; - char patch_file_name[256]; - newbuf = line + strlen("\%patchfile"); - sscanf(newbuf,"%lx %s",&offset,patch_file_name); // Only hexadecimal numbers supported - patchMemfromFile((const char*)patch_file_name,offset); + size_t offset = 0; + std::string patch_file_name; + std::string newbuf = line.substr(strlen("\%patchfile")); + size_t p = newbuf.find(' '); + if(p != std::string::npos) { + sscanf(newbuf.substr(0,p).c_str(), "%lx", &offset); // Only hexadecimal numbers supported + patch_file_name = newbuf.substr(p+1); + patchMemfromFile("data/" + m_datadirectory + "/" + patch_file_name,offset); + } } if(!m_TextList.empty()) { - delete *(m_TextList.begin()); m_TextList.pop_front(); } } @@ -86,50 +71,38 @@ void CPatcher::patchMemory() if(!m_TextList.empty()) { - delete *(m_TextList.begin()); m_TextList.pop_front(); } } - // change back to "data" dir - char curdir[256]; - while(1) - { - char *reldir; - getcwd(curdir,256); - reldir = curdir+strlen(curdir)-strlen("data"); - if(strcmp(reldir,"data")) - chdir(".."); - else - break; - } - chdir(".."); } bool CPatcher::loadPatchfile() { - bool ret = false; - chdir("data"); - chdir(m_datadirectory.c_str()); + std::string fullfname = GetFullFileName("data/" + m_datadirectory); + if(fullfname.size() == 0) + return false; + // Detect the patchfile - DIR *dir = opendir("."); - struct dirent *dp; - + DIR *dir = opendir(Utf8ToSystemNative(fullfname).c_str()); + + bool ret = false; if(dir) { + struct dirent *dp; while( ( dp = readdir(dir) ) ) { if(strstr(dp->d_name,".pat")) { // The file was found! now read it into the memory! - char* buf; std::ifstream Patchfile; OpenGameFileR(Patchfile, dp->d_name); while(!Patchfile.eof()) { - buf = new char[256]; - Patchfile.getline(buf,256); + char buf[256]; + Patchfile.getline(buf,sizeof(buf)); + fix_markend(buf); m_TextList.push_back(buf); } @@ -141,19 +114,6 @@ bool CPatcher::loadPatchfile() } } - char curdir[256]; - while(1) - { - char *reldir; - getcwd(curdir,256); - reldir = curdir+strlen(curdir)-strlen("data"); - if(strcmp(reldir,"data")) - chdir(".."); - else - break; - } - - chdir(".."); closedir(dir); return ret; diff --git a/src/fileio/CPatcher.h b/src/fileio/CPatcher.h index e4de4427c..9880fee16 100644 --- a/src/fileio/CPatcher.h +++ b/src/fileio/CPatcher.h @@ -28,7 +28,7 @@ private: unsigned char *m_data; std::string m_datadirectory; - std::list m_TextList; + std::list m_TextList; }; diff --git a/src/include/fileio.h b/src/include/fileio.h index 831d51e1b..1e6d99c4c 100644 --- a/src/include/fileio.h +++ b/src/include/fileio.h @@ -15,7 +15,6 @@ void addmaptile(unsigned int t); void addenemytile(unsigned int t, stCloneKeenPlus *pCKP); short checkConsistencyofGameData(stGameData *p_GameData); std::string formatPathString(const std::string& path); -bool renameFilenamesLowerCase(const char *dir_name); void assignChangeTileAttribute(stTile *tile, int episode); #endif diff --git a/src/vorticon/CEGAGraphics.cpp b/src/vorticon/CEGAGraphics.cpp index f9bbadcea..8366b260e 100644 --- a/src/vorticon/CEGAGraphics.cpp +++ b/src/vorticon/CEGAGraphics.cpp @@ -57,12 +57,11 @@ bool CEGAGraphics::loadData() std::string buf; vector databuf; - chdir("data"); // TODO: You must be able to use another directory if(m_path == "") buf = "egahead.ck" + itoa(m_episode); else buf = m_path + "/egahead.ck" + itoa(m_episode); - std::ifstream HeadFile; OpenGameFileR(HeadFile, buf, ios::binary); + std::ifstream HeadFile; OpenGameFileR(HeadFile, "data/" + buf, ios::binary); if(!HeadFile) return false; @@ -113,7 +112,7 @@ bool CEGAGraphics::loadData() buf = "egalatch.ck" + itoa(m_episode); else buf = m_path + "/egalatch.ck" + itoa(m_episode); - m_Latch->loadData(buf,(compressed>>1)); // The second bit tells, if latch is compressed. + m_Latch->loadData("data/" + buf,(compressed>>1)); // The second bit tells, if latch is compressed. m_Sprit = new CEGASprit(SpritePlaneSize, @@ -126,9 +125,7 @@ bool CEGAGraphics::loadData() buf = "egasprit.ck" + itoa(m_episode); else buf = m_path + "/egasprit.ck" + itoa(m_episode); - m_Sprit->loadData(buf,(compressed>>1)); - - chdir("../"); + m_Sprit->loadData("data/" + buf,(compressed>>1)); return true; }