- added logic to delete old texture after doing a model texture conversion

This commit is contained in:
Mark Vejvoda 2011-03-12 22:11:09 +00:00
parent c67d7d48c2
commit f35e920d47
6 changed files with 39 additions and 114 deletions

View File

@ -202,11 +202,8 @@ void MenuStateKeysetup::mouseClick(int x, int y, MouseButton mouseButton){
Config &configKeys = Config::getInstance(std::pair<ConfigType,ConfigType>(cfgMainKeys,cfgUserKeys));
string userKeysFile = configKeys.getFileName(true);
#ifdef WIN32
int result = _unlink(userKeysFile.c_str());
#else
int result = unlink(userKeysFile.c_str());
#endif
bool result = removeFile(userKeysFile.c_str());
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] delete file [%s] returned %d\n",__FILE__,__FUNCTION__,__LINE__,userKeysFile.c_str(),result);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] delete file [%s] returned %d\n",__FILE__,__FUNCTION__,__LINE__,userKeysFile.c_str(),result);
configKeys.reload();

View File

@ -139,7 +139,8 @@ public:
void loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
void loadV3(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
void load(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
void save(const string &dir, FILE *f, TextureManager *textureManager, string convertTextureToFormat);
void save(const string &dir, FILE *f, TextureManager *textureManager,
string convertTextureToFormat, std::map<string,int> &textureDeleteList);
void deletePixels();

View File

@ -148,6 +148,7 @@ bool StartsWith(const std::string &str, const std::string &key);
bool EndsWith(const string &str, const string& key);
string replaceAll(string& context, const string& from, const string& to);
bool removeFile(string file);
void removeFolder(const string path);
int getScreenW();

View File

@ -411,7 +411,8 @@ void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,
}
}
void Mesh::save(const string &dir, FILE *f, TextureManager *textureManager, string convertTextureToFormat) {
void Mesh::save(const string &dir, FILE *f, TextureManager *textureManager,
string convertTextureToFormat, std::map<string,int> &textureDeleteList) {
MeshHeader meshHeader;
memset(&meshHeader, 0, sizeof(struct MeshHeader));
@ -465,15 +466,21 @@ void Mesh::save(const string &dir, FILE *f, TextureManager *textureManager, stri
if(convertTextureToFormat == "tga") {
texture->getPixmap()->saveTga(file);
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
}
else if(convertTextureToFormat == "bmp") {
texture->getPixmap()->saveBmp(file);
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
}
//else if(convertTextureToFormat == "jpg") {
// texture->getPixmap()->saveJpg(file);
//}
else if(convertTextureToFormat == "png") {
texture->getPixmap()->savePng(file);
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
}
else {
throw runtime_error("Unsuppoted texture format: [" + convertTextureToFormat + "]");
@ -645,44 +652,6 @@ void Model::save(const string &path, string convertTextureToFormat) {
}
}
/*void Model::loadG3dOld(const string &path){
try{
FILE *f=fopen(path.c_str(),"rb");
if (f==NULL){
throw runtime_error("Error opening 3d model file");
}
string dir= cutLastFile(path);
//read header
ModelHeaderOld modelHeader;
fread(&modelHeader, sizeof(ModelHeader), 1, f);
meshCount= modelHeader.meshCount;
if(modelHeader.id[0]!='G' || modelHeader.id[1]!='3' || modelHeader.id[2]!='D'){
throw runtime_error("Model: "+path+": is not a valid G3D model");
}
switch(modelHeader.version){
case 3:{
meshes= new Mesh[meshCount];
for(uint32 i=0; i<meshCount; ++i){
meshes[i].load(dir, f, textureManager);
meshes[i].buildInterpolationData();
}
break;
}
default:
throw runtime_error("Unknown model version");
}
fclose(f);
}
catch(exception &e){
throw runtime_error("Exception caught loading 3d file: " + path +"\n"+ e.what());
}
}*/
//load a model from a g3d file
void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
@ -769,23 +738,6 @@ void Model::saveG3d(const string &path, string convertTextureToFormat) {
throw runtime_error("Cant open file for writting: "+path);
}
/*
ModelHeader modelHeader;
modelHeader.id[0]= 'G';
modelHeader.id[1]= '3';
modelHeader.id[2]= 'D';
modelHeader.version= 3;
modelHeader.meshCount= meshCount;
string dir= cutLastFile(path);
fwrite(&modelHeader, sizeof(ModelHeader), 1, f);
for(int i=0; i<meshCount; ++i){
meshes[i].save(dir, f);
}
fclose(f);*/
convertTextureToFormat = toLower(convertTextureToFormat);
//file header
@ -797,7 +749,7 @@ void Model::saveG3d(const string &path, string convertTextureToFormat) {
fwrite(&fileHeader, sizeof(FileHeader), 1, f);
//version 4
// file versions
if(fileHeader.version == 4 || fileHeader.version == 3 || fileHeader.version == 2) {
//model header
ModelHeader modelHeader;
@ -806,27 +758,17 @@ void Model::saveG3d(const string &path, string convertTextureToFormat) {
fwrite(&modelHeader, sizeof(ModelHeader), 1, f);
std::map<string,int> textureDeleteList;
for(uint32 i = 0; i < meshCount; ++i) {
meshes[i].save(path, f, textureManager,convertTextureToFormat);
meshes[i].save(path, f, textureManager,convertTextureToFormat,textureDeleteList);
}
}
/*
//version 3
else if(fileHeader.version == 3) {
fwrite(&meshCount, sizeof(meshCount), 1, f);
for(uint32 i=0; i<meshCount; ++i){
meshes[i].saveV3(dir, f, textureManager,convertTextureToFormat);
// Now delete old textures since they were converted to a new format
for(std::map<string,int>::iterator iterMap = textureDeleteList.begin();
iterMap != textureDeleteList.end(); ++iterMap) {
}
}
//version 2
else if(fileHeader.version == 2) {
fwrite(&meshCount, sizeof(meshCount), 1, f);
for(uint32 i=0; i<meshCount; ++i){
meshes[i].saveV2(dir, f, textureManager,convertTextureToFormat);
}
}
*/
else {
throw runtime_error("Invalid model version: "+ intToStr(fileHeader.version));
}

View File

@ -356,11 +356,7 @@ void removeFolder(const string path) {
if(isdir(item.c_str()) == false) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] file item [%s]\n",__FILE__,__FUNCTION__,__LINE__,item.c_str());
#ifdef WIN32
int result = _unlink(item.c_str());
#else
int result = unlink(item.c_str());
#endif
bool result = removeFile(item);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileitem [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,item.c_str(),result);
}
}
@ -485,11 +481,8 @@ void clearFolderTreeContentsCheckSum(vector<string> paths, string pathSearchStri
string crcCacheFile = getFormattedCRCCacheFileName(cacheKeys);
if(fileExists(crcCacheFile) == true) {
#ifdef WIN32
int result = _unlink(crcCacheFile.c_str());
#else
int result = unlink(crcCacheFile.c_str());
#endif
bool result = removeFile(crcCacheFile);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileitem [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,crcCacheFile.c_str(),result);
}
}
@ -560,11 +553,7 @@ void clearFolderTreeContentsCheckSum(const string &path, const string filterFile
}
string crcCacheFile = getFormattedCRCCacheFileName(cacheKeys);
if(fileExists(crcCacheFile) == true) {
#ifdef WIN32
int result = _unlink(crcCacheFile.c_str());
#else
int result = unlink(crcCacheFile.c_str());
#endif
bool result = removeFile(crcCacheFile.c_str());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileitem [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,crcCacheFile.c_str(),result);
}
}
@ -722,11 +711,7 @@ void clearFolderTreeContentsCheckSumList(vector<string> paths, string pathSearch
}
string crcCacheFile = getFormattedCRCCacheFileName(cacheKeys);
if(fileExists(crcCacheFile) == true) {
#ifdef WIN32
int result = _unlink(crcCacheFile.c_str());
#else
int result = unlink(crcCacheFile.c_str());
#endif
bool result = removeFile(crcCacheFile.c_str());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileitem [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,crcCacheFile.c_str(),result);
}
}
@ -876,11 +861,7 @@ void clearFolderTreeContentsCheckSumList(const string &path, const string filter
}
string crcCacheFile = getFormattedCRCCacheFileName(cacheKeys);
if(fileExists(crcCacheFile) == true) {
#ifdef WIN32
int result = _unlink(crcCacheFile.c_str());
#else
int result = unlink(crcCacheFile.c_str());
#endif
bool result = removeFile(crcCacheFile.c_str());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileitem [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,crcCacheFile.c_str(),result);
}
}
@ -1365,6 +1346,16 @@ bool executeShellCommand(string cmd) {
return result;
}
bool removeFile(string file) {
#ifdef WIN32
int result = _unlink(file.c_str());
#else
int result = unlink(file.c_str());
#endif
return (result == 0);
}
// =====================================
// ModeInfo
// =====================================

View File

@ -72,11 +72,8 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread CANCELLED, deleting file for writing [%s]\n",fullFilePath.c_str());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread CANCELLED, deleting file for writing [%s]\n",fullFilePath.c_str());
#ifdef WIN32
_unlink(fullFilePath.c_str());
#else
unlink(fullFilePath.c_str());
#endif
removeFile(fullFilePath);
return -1;
}
@ -311,11 +308,7 @@ FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, stri
ftpfile.stream = NULL;
}
if(result != ftp_crt_SUCCESS) {
#ifdef WIN32
_unlink(destFile.c_str());
#else
unlink(destFile.c_str());
#endif
removeFile(destFile);
}
return result;