Tools/Extractors: Fixed possible name issues (underscore instead of space) when attempting to extract M2 models from MPQ

This commit is contained in:
Shauren
2013-06-27 19:31:38 +02:00
parent 36c866f9ff
commit 50e7b93bbd
8 changed files with 90 additions and 101 deletions

View File

@@ -395,13 +395,14 @@ namespace VMAP
fclose(model_list);
fclose(model_list_copy);
}
// temporary use defines to simplify read/check code (close file and return at fail)
#define READ_OR_RETURN(V, S) if (fread((V), (S), 1, rf) != 1) { \
fclose(rf); printf("readfail, op = %i\n", readOperation); return(false); }
#define READ_OR_RETURN_WITH_DELETE(V, S) if (fread((V), (S), 1, rf) != 1) { \
fclose(rf); printf("readfail, op = %i\n", readOperation); delete[] V; return(false); };
#define CMP_OR_RETURN(V, S) if (strcmp((V), (S)) != 0) { \
fclose(rf); printf("cmpfail, %s!=%s\n", V, S);return(false); }
// temporary use defines to simplify read/check code (close file and return at fail)
#define READ_OR_RETURN(V, S) if (fread((V), (S), 1, rf) != 1) { \
fclose(rf); printf("readfail, op = %i\n", readOperation); return(false); }
#define READ_OR_RETURN_WITH_DELETE(V, S) if (fread((V), (S), 1, rf) != 1) { \
fclose(rf); printf("readfail, op = %i\n", readOperation); delete[] V; return(false); };
#define CMP_OR_RETURN(V, S) if (strcmp((V), (S)) != 0) { \
fclose(rf); printf("cmpfail, %s!=%s\n", V, S);return(false); }
bool GroupModel_Raw::Read(FILE* rf)
{

View File

@@ -44,26 +44,29 @@ char* GetPlainName(char* FileName)
return FileName;
}
void fixnamen(char* name, size_t len)
void FixNameCase(char* name, size_t len)
{
for (size_t i = 0; i < len-3; i++)
{
if (i > 0 && name[i] >= 'A' && name[i] <= 'Z' && isalpha(name[i-1]))
name[i] |= 0x20;
else if ((i == 0 || !isalpha(name[i-1])) && name[i]>='a' && name[i]<='z')
name[i] &= ~0x20;
}
char* ptr = name + len - 1;
//extension in lowercase
for (size_t i = len - 3; i < len; i++)
name[i] |= 0x20;
for (; *ptr != '.'; --ptr)
*ptr |= 0x20;
for (; ptr >= name; --ptr)
{
if (ptr > name && *ptr >= 'A' && *ptr <= 'Z' && isalpha(*(ptr - 1)))
*ptr |= 0x20;
else if ((ptr == name || !isalpha(*(ptr - 1))) && *ptr >= 'a' && *ptr <= 'z')
*ptr &= ~0x20;
}
}
void fixname2(char* name, size_t len)
void FixNameSpaces(char* name, size_t len)
{
for (size_t i=0; i<len-3; i++)
{
if(name[i] == ' ')
name[i] = '_';
name[i] = '_';
}
}
@@ -132,23 +135,24 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY)
{
if (size)
{
char *buf = new char[size];
char* buf = new char[size];
ADT.read(buf, size);
char *p=buf;
int t=0;
ModelInstansName = new string[size];
while (p<buf+size)
char* p = buf;
int t = 0;
ModelInstanceNames = new std::string[size];
while (p < buf + size)
{
fixnamen(p,strlen(p));
std::string path(p);
char* s = GetPlainName(p);
fixname2(s,strlen(s));
FixNameCase(s, strlen(s));
FixNameSpaces(s, strlen(s));
ModelInstansName[t++] = s;
ModelInstanceNames[t++] = s;
string path(p);
ExtractSingleModel(path);
p = p+strlen(p)+1;
p += strlen(p) + 1;
}
delete[] buf;
}
@@ -159,16 +163,18 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY)
{
char* buf = new char[size];
ADT.read(buf, size);
char* p=buf;
char* p = buf;
int q = 0;
WmoInstansName = new string[size];
while (p<buf+size)
WmoInstanceNames = new std::string[size];
while (p < buf + size)
{
char* s = GetPlainName(p);
fixnamen(s, strlen(s));
fixname2(s, strlen(s));
FixNameCase(s, strlen(s));
FixNameSpaces(s, strlen(s));
WmoInstanceNames[q++] = s;
p += strlen(p) + 1;
WmoInstansName[q++] = s;
}
delete[] buf;
}
@@ -183,9 +189,10 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY)
{
uint32 id;
ADT.read(&id, 4);
ModelInstance inst(ADT,ModelInstansName[id].c_str(), map_num, tileX, tileY, dirfile);
ModelInstance inst(ADT, ModelInstanceNames[id].c_str(), map_num, tileX, tileY, dirfile);
}
delete[] ModelInstansName;
delete[] ModelInstanceNames;
ModelInstanceNames = NULL;
}
}
else if (!strcmp(fourcc,"MODF"))
@@ -197,14 +204,18 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY)
{
uint32 id;
ADT.read(&id, 4);
WMOInstance inst(ADT,WmoInstansName[id].c_str(), map_num, tileX, tileY, dirfile);
WMOInstance inst(ADT, WmoInstanceNames[id].c_str(), map_num, tileX, tileY, dirfile);
}
delete[] WmoInstansName;
delete[] WmoInstanceNames;
WmoInstanceNames = NULL;
}
}
//======================
ADT.seek(nextpos);
}
ADT.close();
fclose(dirfile);
return true;

View File

@@ -113,14 +113,14 @@ private:
//size_t mcnk_offsets[256], mcnk_sizes[256];
MPQFile ADT;
//mcell Mcell;
string Adtfilename;
std::string Adtfilename;
public:
ADTFile(char* filename);
~ADTFile();
int nWMO;
int nMDX;
string* WmoInstansName;
string* ModelInstansName;
std::string* WmoInstanceNames;
std::string* ModelInstanceNames;
bool init(uint32 map_num, uint32 tileX, uint32 tileY);
//void LoadMapChunks();
@@ -133,11 +133,11 @@ public:
*/
};
const char * GetPlainName(const char * FileName);
char * GetPlainName(char * FileName);
char * GetExtension(char * FileName);
void fixnamen(char *name, size_t len);
void fixname2(char *name, size_t len);
char const* GetPlainName(char const* FileName);
char* GetPlainName(char* FileName);
char* GetExtension(char* FileName);
void FixNameCase(char* name, size_t len);
void FixNameSpaces(char* name, size_t len);
//void fixMapNamen(char *name, size_t len);
#endif

View File

@@ -86,6 +86,11 @@ class DBCFile
friend class DBCFile;
friend class DBCFile::Iterator;
Record& operator=(Record const&);
Record(Record const& right) : file(right.file), offset(right.offset)
{
}
};
/** Iterator that iterates over records
*/
@@ -94,6 +99,10 @@ class DBCFile
public:
Iterator(DBCFile &file, unsigned char* offset) : record(file, offset) { }
Iterator(Iterator const& right) : record(right.record)
{
}
/// Advance (prefix only)
Iterator& operator++()
{
@@ -116,8 +125,14 @@ class DBCFile
return record.offset != b.record.offset;
}
Iterator& operator=(Iterator const& right)
{
record.offset = right.record.offset;
return *this;
}
private:
Record record;
};
// Get record by id

View File

@@ -8,18 +8,17 @@
bool ExtractSingleModel(std::string& fname)
{
char * name = GetPlainName((char*)fname.c_str());
char * ext = GetExtension(name);
// < 3.1.0 ADT MMDX section store filename.mdx filenames for corresponded .m2 file
if (!strcmp(ext, ".mdx"))
if (fname.substr(fname.length() - 4, 4) == ".mdx")
{
// replace .mdx -> .m2
fname.erase(fname.length()-2,2);
fname.erase(fname.length() - 2, 2);
fname.append("2");
}
// >= 3.1.0 ADT MMDX section store filename.m2 filenames for corresponded .m2 file
// nothing do
std::string originalName = fname;
char* name = GetPlainName((char*)fname.c_str());
FixNameCase(name, strlen(name));
FixNameSpaces(name, strlen(name));
std::string output(szWorkDirWmo);
output += "/";
@@ -28,7 +27,7 @@ bool ExtractSingleModel(std::string& fname)
if (FileExists(output.c_str()))
return true;
Model mdl(fname);
Model mdl(originalName);
if (!mdl.open())
return false;
@@ -60,9 +59,9 @@ void ExtractGameobjectModels()
if (path.length() < 4)
continue;
fixnamen((char*)path.c_str(), path.size());
FixNameCase((char*)path.c_str(), path.size());
char * name = GetPlainName((char*)path.c_str());
fixname2(name, strlen(name));
FixNameSpaces(name, strlen(name));
char * ch_ext = GetExtension(name);
if (!ch_ext)
@@ -72,18 +71,11 @@ void ExtractGameobjectModels()
bool result = false;
if (!strcmp(ch_ext, ".wmo"))
{
result = ExtractSingleWmo(path);
}
else if (!strcmp(ch_ext, ".mdl"))
{
// TODO: extract .mdl files, if needed
else if (!strcmp(ch_ext, ".mdl")) // TODO: extract .mdl files, if needed
continue;
}
else //if (!strcmp(ch_ext, ".mdx") || !strcmp(ch_ext, ".m2"))
{
result = ExtractSingleModel(path);
}
if (result)
{

View File

@@ -46,7 +46,7 @@ bool Model::open()
_unload();
memcpy(&header, f.getBuffer(), sizeof(ModelHeader));
if(header.nBoundingTriangles > 0)
if (header.nBoundingTriangles > 0)
{
f.seek(0);
f.seekRelative(header.ofsBoundingVertices);

View File

@@ -309,7 +309,7 @@ bool ExtractSingleWmo(std::string& fname)
char szLocalFile[1024];
const char * plain_name = GetPlainName(fname.c_str());
sprintf(szLocalFile, "%s/%s", szWorkDirWmo, plain_name);
fixnamen(szLocalFile,strlen(szLocalFile));
FixNameCase(szLocalFile,strlen(szLocalFile));
if (FileExists(szLocalFile))
return true;
@@ -360,7 +360,7 @@ bool ExtractSingleWmo(std::string& fname)
sprintf(groupFileName, "%s_%03u.wmo", temp, i);
//printf("Trying to open groupfile %s\n",groupFileName);
string s = groupFileName;
std::string s = groupFileName;
WMOGroup fgroup(s);
if(!fgroup.open())
{
@@ -424,36 +424,6 @@ void getGamePath()
#endif
}
bool scan_patches(char* scanmatch, std::vector<std::string>& pArchiveNames)
{
int i;
char path[512];
for (i = 1; i <= 99; i++)
{
if (i != 1)
{
sprintf(path, "%s-%d.MPQ", scanmatch, i);
}
else
{
sprintf(path, "%s.MPQ", scanmatch);
}
#ifdef __linux__
if(FILE* h = fopen64(path, "rb"))
#else
if(FILE* h = fopen(path, "rb"))
#endif
{
fclose(h);
//matches.push_back(path);
pArchiveNames.push_back(path);
}
}
return(true);
}
bool processArgv(int argc, char ** argv, const char *versionString)
{
bool result = true;

View File

@@ -83,7 +83,7 @@ bool WDTFile::init(char* /*map_id*/, unsigned int mapID)
while (p < buf + size)
{
char* s=wdtGetPlainName(p);
fixnamen(s,strlen(s));
FixNameCase(s,strlen(s));
p=p+strlen(p)+1;
gWmoInstansName[q++] = s;
}