/** ****************************************************************************** @file /common/rm/rmconf.cpp @brief RM pristupujici ke konfiguracnim souborum @author Vta @version 1.0 ******************************************************************************/ #include "common/rm/rmconf.h" using namespace rm; void xmlToProfile(TProfile ** data,TXMLdata * xml,int id) { if (xml->getData((*data)->name,K8_MAX_NAME_LEN,"name",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (bonus) having id %i",id); THROW(E_8K_RM,"Bad XML file (bonus)"); } } void profileToXML(TXMLdata * xml,TProfile * data) { xml->setData(data->name,"name",0,NULL); } void freeProfileStruct(TProfile ** data) { KMemFree((*data)); (*data)=NULL; } TRM_conf::TRM_conf() { this->waseddited=0; this->config=new TXMLdata(); size_t k8packsize=100; char *k8pack=(char*)KMemAlloc(k8packsize*sizeof(char)); size_t pathsize=100; char *path=(char*)KMemAlloc(pathsize*sizeof(char)); // nejdriv naplnim uplne prazdne, hodnoty se kdyztak prepisou // if (config->getData(NULL,0,"default_K8pack",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) config->setData(DEFAULT_K8PACK,"default_K8pack",0,NULL); // if (config->getDataInt("default_language",0,NULL)<0) config->setDataInt(DEFAULT_LANGUAGE,"default_language",0,NULL); // if (config->getDataInt("default_speed",0,NULL)<0) config->setDataInt(DEFAULT_SPEED,"default_speed",0,NULL); // if (config->getDataInt("default_sfx_volume",0,NULL)<0) config->setDataInt(DEFAULT_SFX_VOLUME,"default_sfx_volume",0,NULL); // if (config->getDataInt("default_music_volume",0,NULL)<0) config->setDataInt(DEFAULT_MUSIC_VOLUME,"default_music_volume",0,NULL); // if (config->getDataInt("default_fullscreen",0,NULL)<0) config->setDataInt(DEFAULT_FULLSCREEN,"default_fullscreen",0,NULL); // if (config->getDataInt("default_resolution",0,NULL)<0) config->setDataInt(DEFAULT_RESOLUTION,"default_resolution",0,NULL); // if (config->getDataInt("default_announce",0,NULL)<0) config->setDataInt(DEFAULT_ANNOUNCE,"default_announce",0,NULL); // if (config->getDataInt("default_autosave",0,NULL)<0) config->setDataInt(DEFAULT_AUTOSAVE,"default_autosave",0,NULL); // nactu config.xml int retvalue=0; #ifdef WIN32 strpath(&path,CONFIG_DIR,"","config","",pathsize,".xml"); #else snprintf(path,pathsize,"%s/.8Kingdoms/config.xml", getenv("HOME")); #endif retvalue=this->config->readFile(path); if (retvalue!=XML_ERROR_OK) // kdyz neni, vytvorim { GLOBALLOGID(PRIORITY_MISSING_FILE,"config.xml is missing or is damaged"); // config->setDTD("\n \n \n]>"); // nastavim DTD // zapisu retvalue=config->saveToFile(path); // if (retvalue!=XML_ERROR_OK) { GLOBALLOGID(PRIORITY_MISSING_FILE,"Can not create config.xml"); } } // nactu informace o souborech - zjistim kde je files.xml (hlavni file) this->config->getDataResize(&k8pack,k8packsize,"default_K8pack",0,NULL); strpath(&path,RESOURCES_DIR,k8pack,XML_DIR,"files",pathsize,".xml"); retvalue=this->readInfoFile(path); KMemFree(path); KMemFree(k8pack); if (retvalue!=0) // chyba pri cteni fajlu files.xml { GLOBALLOGID(PRIORITY_MISSING_FILE,"files.xml is missing or is damaged",XML_DIR); THROW(E_8K_RM,"files.xml is corrupted"); } // ziskani profilu this->profile=new(DA); DA daids; this->config->getListOfIds(&daids,"profiles",0,NULL); int i; for (i=daids.getNext(0);i!=-1;i=daids.getNext(i)) { if (daids[i]!=0) // kvuli poslednimu prvku ids[max] { (*this->profile)[i]=(TProfile*) KMemAlloc(sizeof(TProfile)); (*this->profile)[i]->id=i; this->config->getData((*this->profile)[i]->name,K8_MAX_NAME_LEN,"profiles",0,"profile",i,"name",0,NULL); if ( ((*this->profile)[i]->language_id=this->config->getDataInt("profiles",0,"profile",i,"language",0,NULL)) == XML_ERROR_NO_SUCH_A_PATH) { (*this->profile)[i]->language_id=this->config->getDataInt("default_language",0,NULL); } if ( ((*this->profile)[i]->autosave=this->config->getDataInt("profiles",0,"profile",i,"autosave",0,NULL)) < 0) { (*this->profile)[i]->autosave=this->config->getDataInt("default_autosave",0,NULL); } else { if ((*this->profile)[i]->autosave>1) (*this->profile)[i]->autosave=0; } if ( ((*this->profile)[i]->speed=this->config->getDataInt("profiles",0,"profile",i,"speed",0,NULL)) < 0) { (*this->profile)[i]->speed=this->config->getDataInt("default_speed",0,NULL); } else { if ((*this->profile)[i]->speed>3) (*this->profile)[i]->speed=3; } if ( ((*this->profile)[i]->sfx_volume=this->config->getDataInt("profiles",0,"profile",i,"sfx_volume",0,NULL)) == XML_ERROR_NO_SUCH_A_PATH) { (*this->profile)[i]->sfx_volume=this->config->getDataInt("default_sfx_volume",0,NULL); } if ( ((*this->profile)[i]->music_volume=this->config->getDataInt("profiles",0,"profile",i,"music_volume",0,NULL)) == XML_ERROR_NO_SUCH_A_PATH) { (*this->profile)[i]->music_volume=this->config->getDataInt("default_music_volume",0,NULL); } if ( ((*this->profile)[i]->fullscreen=this->config->getDataInt("profiles",0,"profile",i,"fullscreen",0,NULL)) == XML_ERROR_NO_SUCH_A_PATH) { (*this->profile)[i]->fullscreen=this->config->getDataInt("default_fullscreen",0,NULL); } else { if ((*this->profile)[i]->fullscreen<=0) (*this->profile)[i]->fullscreen=0; else (*this->profile)[i]->fullscreen=1; } if ( ((*this->profile)[i]->announce=this->config->getDataInt("profiles",0,"profile",i,"announce",0,NULL)) == XML_ERROR_NO_SUCH_A_PATH) { (*this->profile)[i]->announce=this->config->getDataInt("default_announce",0,NULL); } else { if ((*this->profile)[i]->announce<=0) (*this->profile)[i]->announce=0; else (*this->profile)[i]->announce=1; } } } GLOBALLOGID(PRIORITY_CONFIRM,"RMconf was created"); } void TRM_conf::setToConfig(char * entry, char * data) { this->lock(); this->config->setData(data,entry,0,NULL); this->unlock(); } int TRM_conf::getFromConfig(char ** output, char * entry, size_t &size) { this->lock(); int i=this->config->getDataResize(output,size,entry,0,0); if (i==-1) // nenaslo { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "The requested entry from config.xml is missing"); THROW(E_8K_RM,"The requested entry from config.xml is missing"); } this->unlock(); return i; } int TRM_conf::getFromFiles(char ** output, char * entry, size_t &size) { this->lock(); int i=this->index->getDataResize(output,size,entry,0,0); if (i==-1) // nenaslo { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "The file %s is corrupted",entry); THROW(E_8K_RM,"One or more index-files is corrupted"); // return 0; } this->unlock(); return i; } void TRM_conf::getProfiles(DA ** profiles) { (*profiles)=this->profile; } /* void TRM_conf::saveConfigBeforeExit() { this->waseddited=1; } */ TRM_conf::~TRM_conf() { int i; // if (this->waseddited==1) // pokud byl editovan, zapis do souboru { this->config->delData(0,"profiles",0,NULL); for (i=this->profile->getNext(0);i!=-1;i=this->profile->getNext(i)) { this->config->setData((*this->profile)[i]->name,"profiles",0,"profile",i,"name",0,NULL); this->config->setDataInt((*this->profile)[i]->language_id,"profiles",0,"profile",i,"language",0,NULL); this->config->setDataInt((*this->profile)[i]->speed,"profiles",0,"profile",i,"speed",0,NULL); this->config->setDataInt((*this->profile)[i]->sfx_volume,"profiles",0,"profile",i,"sfx_volume",0,NULL); this->config->setDataInt((*this->profile)[i]->music_volume,"profiles",0,"profile",i,"music_volume",0,NULL); this->config->setDataInt((*this->profile)[i]->fullscreen,"profiles",0,"profile",i,"fullscreen",0,NULL); this->config->setDataInt((*this->profile)[i]->announce,"profiles",0,"profile",i,"announce",0,NULL); this->config->setDataInt((*this->profile)[i]->autosave,"profiles",0,"profile",i,"autosave",0,NULL); } size_t pathsize=200; char *path=(char*)KMemAlloc(pathsize*sizeof(char)); size_t k8packsize=100; char *k8pack=(char*)KMemAlloc(k8packsize*sizeof(char)); this->config->getDataResize(&k8pack,k8packsize,"default_K8pack",0,0); #ifdef WIN32 strpath(&path,CONFIG_DIR,"","config","",pathsize,".xml"); #else snprintf(path,pathsize,"%s/.8Kingdoms/config.xml", getenv("HOME")); #endif this->config->saveToFile(path); KMemFree(path); KMemFree(k8pack); } for (i=this->profile->getNext(0);i!=-1;i=this->profile->getNext(i)) { KMemFree((*this->profile)[i]); } GLOBALLOGID(PRIORITY_CONFIRM ,"RMconf was destroyed"); delete (this->config); delete (this->profile); }