/** ****************************************************************************** @file /common/rm/rmmodel.cpp @brief RM pristupujici k modelum @author Vta @version 1.0 ******************************************************************************/ #include "common/rm/rmmodel.h" #include "common/utils.h" #ifndef _K_EDITOR_ using namespace gui; #endif // ifndef namespace rm { void TRM_model::createRecord(int id) { this->lock(); if ((*(this->models))[id]==NULL) { (*(this->models))[id]=new TModel; } this->unlock(); } void TRM_model::loadData(int id) // RM ma tuto metodu jako pure virtual { this->loadModel(id); } /// Pomocny buffer static char bu_path[K8_MAX_PATH_LEN]; /// Pomocny buffer static char bu_file[K8_MAX_FILE_NAME_LEN]; void TRM_model::loadModel(int id) { this->lock(); if ((*(this->models))[id]==NULL) // pokud TModel jeste nebyl vytvoren { this->createRecord(id); if (this->index->getData(bu_file,K8_MAX_FILE_NAME_LEN,"model",id,"file",1,NULL)!=XML_ERROR_NO_SUCH_A_PATH) { strpartpath(bu_path,RESOURCES_DIR,this->rmconfi->getPack(),MODEL_DIR,bu_file,K8_MAX_PATH_LEN,""); try { #ifndef _K_EDITOR_ (*(this->models))[id]->loadGeometry(bu_path); #endif } catch(E_8K_ASE_READER) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not load the model having id %i - the main ase file is corrupted",id); THROW(E_8K_RM,"RMmodel can not load a model"); } int sekfile=2; while (this->index->getData(bu_file,K8_MAX_FILE_NAME_LEN,"model",id,"file",sekfile,NULL)!=XML_ERROR_NO_SUCH_A_PATH) { strpartpath(bu_path,RESOURCES_DIR,this->rmconfi->getPack(),MODEL_DIR,bu_file,K8_MAX_PATH_LEN,""); try { #ifndef _K_EDITOR_ (*(this->models))[id]->loadAnimation(bu_path); #endif } catch(E_8K_ASE_READER) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not load the model having id %i - the secondary file (%i) is corrupted",id,sekfile); THROW(E_8K_RM,"RMmodel can not load a model"); } sekfile++; } } this->writeGroup(id); // zapise skupinu } this->unlock(); } void TRM_model::delModel(int id) { this->lock(); this->index->delData(0,"model",id,NULL); if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getModelFile())!=XML_ERROR_OK) // zapisu index do souboru { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not write to the index file"); THROW(E_8K_RM,"RMmodel can not write to the index file"); } if ((*(this->models))[id]!=NULL) // odstanim i z pameti { delete((*(this->models))[id]); (*(this->models))[id]=NULL; } this->unlock(); } int TRM_model::getMaxId() { this->lock(); int maxindex; if (this->models->getMaxId()>this->index->getMaxId()) maxindex=this->models->getMaxId(); else maxindex=this->index->getMaxId(); this->unlock(); return maxindex; } void TRM_model::addToModel(int id,char * secondary_file) { this->lock(); int max=this->index->getMaxId("model",id,"file",0,NULL); if (max<1) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not add animation to the non-existing model id %i",id); THROW(E_8K_RM,"RMmodel can not add animation to the non-existing model"); } max++; this->index->setData(secondary_file,"model",id,"file",max,NULL); if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getModelFile())!=XML_ERROR_OK) // zapisu index do souboru { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not write to the index file"); THROW(E_8K_RM,"RMmodel can not write to the index file"); } this->unlock(); } int TRM_model::addModel(int id, int startid, char **filenames, int size) { this->lock(); int maxindex; if (id==-1) { // vezmu prvni volny - tj. max (id_ze_souboru,id_v_pameti) pro pripad, ze uz je neco ulozeno maxindex=this->getMaxId(); maxindex++; if (startid<1) startid=1; int i; for (i=startid;iindex->getData(NULL,0,"model",i,"file",1,NULL)==XML_ERROR_NO_SUCH_A_PATH ) && ((*(this->models))[i]==NULL) )// aby nebylo nic v souboru ani nacteno - nic se neprepsalo { break; } } maxindex=i; // vezmu prvni volnou hodnotu } else { maxindex=id; } if (maxindex<1) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not add a model having id %i < 1",maxindex); THROW(E_8K_RM,"RMmodel can not add a model having id less than 1"); } if (this->index->getData(NULL,0,"model",maxindex,"file",1,NULL)!=XML_ERROR_NO_SUCH_A_PATH) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not add a model having id %i, such a model exists",maxindex); THROW(E_8K_RM,"RMmodel can not add a model"); } int i; for (i=1;i<=size;i++) { this->index->setData(filenames[i-1],"model",maxindex,"file",i,NULL); // zapisi vsechny soubory } if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getModelFile())!=XML_ERROR_OK) // zapisu index do souboru { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmodel can not write to the index file"); THROW(E_8K_RM,"RMmodel can not write to the index file"); } this->loadModel(maxindex); this->unlock(); return maxindex; } TModel * TRM_model::getModel(int id) { this->lock(); if ((*(this->models))[id]==NULL) { this->createRecord(id); } TModel * ret=(*(this->models))[id]; this->unlock(); return ret; } void TRM_model::freeData(int id) { this->freeModel(id); } void TRM_model::freeModel(int id) { this->lock(); if ((*(this->models))[id]!=NULL) { delete((*(this->models))[id]); (*(this->models))[id]=NULL; } /* else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "Nelze dealokovat dosud nenacteny objekt s id %i",id); THROW(E_8K_RM,"Nelze dealokovat dosud nenacteny objekt"); }*/ this->unlock(); } int TRM_model::getModels(DA ** models) { this->lock(); *models=this->models; int ret=this->models->getMaxId(); this->unlock(); return ret; } int TRM_model::getModelsCount() { this->lock(); int num=this->index->getMaxId(); this->unlock(); return num; } int TRM_model::getModelsLoaded() { this->lock(); int num=0; int j; for (j=this->models->getNext(-1);j!=-1;j=this->models->getNext(j)) { num++; } this->unlock(); return num; } TRM_model::TRM_model(TRM_conf_i * rmc) // konstruktor nema smyl zamykat... { this->rmconfi=rmc; this->models=new DA; char *path=(char*)KMemAlloc(100*sizeof(char)); size_t pathsize=100; int retvalue; try { strpath(&path,RESOURCES_DIR,this->rmconfi->getPack(),XML_DIR,this->rmconfi->getModelFile(),pathsize,".xml"); } catch (E_8K_RM) { KMemFree(path); // pro pripad vyjimky GLOBALLOGID(PRIORITY_FATAL, "files.xml is corrupted"); THROW(E_8K_RM,"files.xml is corrupted"); } retvalue=this->readInfoFile(path); // do RMtxt nactu informace kde ma hledat vsechny model KMemFree(path); if (retvalue!=XML_ERROR_OK) // chyba pri cteni fajlu o jazycich { GLOBALLOGID(PRIORITY_MISSING_FILE,"RMmodel the file %s is corrupted",path); THROW(E_8K_RM,"RMmodel the index file is corrupted"); } GLOBALLOGID(PRIORITY_CONFIRM ,"RMmodel was destroyed"); } TRM_model::~TRM_model() { delete this->rmconfi; for (int j=this->models->getNext(-1);j!=-1;j=this->models->getNext(j)) { delete((*(this->models))[j]); } GLOBALLOGID(PRIORITY_CONFIRM ,"RMmodel was destroyed"); if (this->models!=NULL) delete this->models; } }