Bug fix CPatcher Class for Mods, especially.

git-svn-id: https://clonekeenplus.svn.sourceforge.net/svnroot/clonekeenplus/cgenius/trunk@158 4df4b0f3-56ce-47cb-b001-ed939b7d65a6
This commit is contained in:
gerstrong
2009-07-24 21:38:30 +00:00
parent 01c6db1b9b
commit cc3aed0af2

View File

@@ -11,6 +11,7 @@
#include <fstream> #include <fstream>
#include "../FindFile.h" #include "../FindFile.h"
#include "../StringUtils.h" #include "../StringUtils.h"
#include "../CLogFile.h"
CPatcher::CPatcher(int episode, int version,unsigned char *data, const std::string& datadir) { CPatcher::CPatcher(int episode, int version,unsigned char *data, const std::string& datadir) {
m_episode = episode; m_episode = episode;
@@ -36,11 +37,11 @@ void CPatcher::patchMemory()
if(strCaseStartsWith(line,"\%version")) if(strCaseStartsWith(line,"\%version"))
{ {
std::string verstring = line.substr(strlen("\%version")); std::string verstring = line.substr(strlen("\%version "));
if((stringcaseequal(verstring,"1.31") && m_version == 131 ) if((strCaseStartsWith(verstring,"1.31") && m_version == 131 )
|| (stringcaseequal(verstring,"1.1") && m_version == 110 ) || (strCaseStartsWith(verstring,"1.1") && m_version == 110 )
|| stringcaseequal(verstring,"ALL")) || strCaseStartsWith(verstring,"ALL"))
{ {
while(!m_TextList.empty()) while(!m_TextList.empty())
{ {
@@ -55,8 +56,9 @@ void CPatcher::patchMemory()
std::string newbuf = line.substr(strlen("\%patchfile")); std::string newbuf = line.substr(strlen("\%patchfile"));
size_t p = newbuf.find(' '); size_t p = newbuf.find(' ');
if(p != std::string::npos) { if(p != std::string::npos) {
sscanf(newbuf.substr(0,p).c_str(), "%lx", &offset); // Only hexadecimal numbers supported char temp[256];
patch_file_name = newbuf.substr(p+1); sscanf(newbuf.c_str(), "%lx %s", &offset, temp); // Only hexadecimal numbers supported
patch_file_name.append(temp);
patchMemfromFile("data/" + m_datadirectory + "/" + patch_file_name,offset); patchMemfromFile("data/" + m_datadirectory + "/" + patch_file_name,offset);
} }
} }
@@ -77,46 +79,50 @@ void CPatcher::patchMemory()
} }
struct PatchListFiller {
std::set<std::string> list;
bool operator() (const std::string& filename) {
std::string ext = GetFileExtension(filename);
if (stringcaseequal(ext, "pat"))
list.insert(filename);
return true;
}
};
bool CPatcher::loadPatchfile() bool CPatcher::loadPatchfile()
{ {
std::string fullfname = GetFullFileName("data/" + m_datadirectory); std::string path = "data/" + m_datadirectory;
if(fullfname.size() == 0)
//Get the list of ".pat" files
PatchListFiller patchlist;
FindFiles(patchlist, path, false, FM_REG);
// Nothing to play, just quit
if (!patchlist.list.size())
return false; return false;
// Detect the patchfile if (patchlist.list.size() > 1)
DIR *dir = opendir(Utf8ToSystemNative(fullfname).c_str()); g_pLogFile->textOut(PURPLE,"Multiple Patch are not yet supported! Please remove a file. Taking one File.<br>");
bool ret = false; while(!patchlist.list.empty())
if(dir)
{ {
struct dirent *dp; std::string buf = *patchlist.list.begin();
while( ( dp = readdir(dir) ) ) std::ifstream Patchfile; OpenGameFileR(Patchfile, buf);
while(!Patchfile.eof())
{ {
if(strstr(dp->d_name,".pat")) char buf[256];
{ Patchfile.getline(buf, sizeof(buf));
// The file was found! now read it into the memory! fix_markend(buf);
m_TextList.push_back(buf);
std::ifstream Patchfile; OpenGameFileR(Patchfile, dp->d_name);
while(!Patchfile.eof())
{
char buf[256];
Patchfile.getline(buf,sizeof(buf));
fix_markend(buf);
m_TextList.push_back(buf);
}
Patchfile.close();
ret = true;
break;
}
} }
Patchfile.close();
patchlist.list.clear();
} }
closedir(dir); return true;
return ret;
} }
void CPatcher::patchMemfromFile(const std::string& patch_file_name, int offset) void CPatcher::patchMemfromFile(const std::string& patch_file_name, int offset)