forked from mirrors/principia
Deduplicate and reduce indentation in some pkgman methods
This commit is contained in:
parent
a9408d30ca
commit
2a8ad3df09
1 changed files with 146 additions and 219 deletions
197
src/pkgman.cc
197
src/pkgman.cc
|
|
@ -933,38 +933,9 @@ void pkgman::get_cache_full_path(int level_type, uint32_t id, uint32_t save_id,
|
|||
}
|
||||
|
||||
bool pkgman::get_level_name(int level_type, uint32_t id, uint32_t save_id, char *output) {
|
||||
char filename[1024];
|
||||
|
||||
pkgman::get_level_full_path(level_type, id, save_id, filename);
|
||||
|
||||
FILE_IN_ASSET(level_type == LEVEL_MAIN);
|
||||
|
||||
FILE *fp = _fopen(filename, "rb");
|
||||
if (fp) {
|
||||
tmpbuf.reset();
|
||||
tmpbuf.ensure(sizeof(lvlinfo));
|
||||
|
||||
_fread(tmpbuf.buf, 1, sizeof(lvlinfo), fp);
|
||||
tmpbuf.size = sizeof(lvlinfo);
|
||||
|
||||
tmplvl.read(&tmpbuf, true);
|
||||
|
||||
if (tmplvl.name_len == 0) {
|
||||
strcpy(output, "<no name>");
|
||||
} else {
|
||||
memcpy(output, tmplvl.name, tmplvl.name_len*sizeof(char));
|
||||
output[tmplvl.name_len] = '\0';
|
||||
}
|
||||
|
||||
_fclose(fp);
|
||||
|
||||
uint8_t o_version;
|
||||
pkgman::get_level_data(level_type, id, save_id, output, &o_version);
|
||||
return true;
|
||||
} else {
|
||||
strcpy(output, "<no name>");
|
||||
tms_warnf("unable to open file for lid %u", id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool pkgman::get_level_data(int level_type, uint32_t id, uint32_t save_id, char *o_name, uint8_t *o_version) {
|
||||
|
|
@ -975,7 +946,12 @@ bool pkgman::get_level_data(int level_type, uint32_t id, uint32_t save_id, char
|
|||
FILE_IN_ASSET(level_type == LEVEL_MAIN);
|
||||
|
||||
FILE *fp = _fopen(filename, "rb");
|
||||
if (fp) {
|
||||
|
||||
if (!fp) {
|
||||
tms_warnf("unable to open file for lid %u", id);
|
||||
return false;
|
||||
}
|
||||
|
||||
tmpbuf.reset();
|
||||
tmpbuf.ensure(sizeof(lvlinfo));
|
||||
|
||||
|
|
@ -996,11 +972,6 @@ bool pkgman::get_level_data(int level_type, uint32_t id, uint32_t save_id, char
|
|||
_fclose(fp);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
tms_warnf("unable to open file for lid %u", id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
pkginfo *pkgman::get_pkgs(int type) {
|
||||
|
|
@ -1078,12 +1049,19 @@ lvlfile *pkgman::get_levels(int level_type) {
|
|||
struct dirent *ent;
|
||||
lvlfile *first = 0, *last = 0;
|
||||
|
||||
if ((dir = opendir(path))) {
|
||||
dir = opendir(path);
|
||||
|
||||
if (!dir) {
|
||||
tms_errorf("could not open directory %s", path);
|
||||
return first;
|
||||
}
|
||||
|
||||
while ((ent = readdir(dir))) {
|
||||
int len = strlen(ent->d_name);
|
||||
|
||||
if (len > 5 && memcmp(&ent->d_name[len-5], ext, 5) == 0) {
|
||||
if (!(len > 5 && memcmp(&ent->d_name[len-5], ext, 5) == 0))
|
||||
continue;
|
||||
|
||||
uint32_t level_id = 0;
|
||||
uint32_t save_id = 0;
|
||||
uint8_t state_level_type = LEVEL_LOCAL_STATE;
|
||||
|
|
@ -1129,7 +1107,9 @@ lvlfile *pkgman::get_levels(int level_type) {
|
|||
mtime = st.st_mtime;
|
||||
#endif
|
||||
|
||||
if (level_id != 0 || state) {
|
||||
if (!(level_id != 0 || state))
|
||||
continue;
|
||||
|
||||
lvlfile *ff = new lvlfile((state ? state_level_type : level_type), level_id);
|
||||
strcpy(ff->modified_date, date);
|
||||
ff->mtime = mtime;
|
||||
|
|
@ -1142,7 +1122,12 @@ lvlfile *pkgman::get_levels(int level_type) {
|
|||
* The level type of the current state is discerned via the filename
|
||||
* using the first "dotted" argument (level or db).
|
||||
**/
|
||||
if (pkgman::get_level_data((state ? state_level_type : orig_level_type), level_id, save_id, ff->name, &ff->version)) {
|
||||
if (!pkgman::get_level_data((state ? state_level_type : orig_level_type), level_id, save_id, ff->name, &ff->version)) {
|
||||
tms_warnf("Unable to get level name for lid %u", level_id);
|
||||
delete ff;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
first = ff;
|
||||
} else {
|
||||
|
|
@ -1165,17 +1150,8 @@ lvlfile *pkgman::get_levels(int level_type) {
|
|||
prev->next = ff;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tms_warnf("Unable to get level name for lid %u", level_id);
|
||||
delete ff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
} else {
|
||||
tms_errorf("could not open directory %s", path);
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
|
@ -1213,18 +1189,45 @@ bool lvledit::open(int lvl_type, uint32_t lvl_id) {
|
|||
const char *path = pkgman::get_level_path(lvl_type);
|
||||
|
||||
if (lvl_id == 0)
|
||||
snprintf(filename, 1023, "%s/.autosave", path);
|
||||
snprintf(filename, 1024, "%s/.autosave", path);
|
||||
else
|
||||
snprintf(filename, 1023, "%s/%d.%s", path, lvl_id, ext);
|
||||
snprintf(filename, 1024, "%s/%d.%s", path, lvl_id, ext);
|
||||
|
||||
this->lvl_type = 0;
|
||||
this->lvl_id = 0;
|
||||
|
||||
FILE_IN_ASSET(lvl_type == LEVEL_MAIN);
|
||||
|
||||
FILE *fp = _fopen(filename, "rb");
|
||||
if (!open_from_path(filename)) {
|
||||
tms_errorf("could not open level file '%s'", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
this->lvl_type = lvl_type;
|
||||
this->lvl_id = lvl_id;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lvledit::save() {
|
||||
char filename[1024];
|
||||
|
||||
const char *ext = pkgman::get_level_ext(lvl_type);
|
||||
const char *path = pkgman::get_level_path(lvl_type);
|
||||
|
||||
snprintf(filename, 1024, "%s/%d.%s", path, this->lvl_id, ext);
|
||||
|
||||
return save_to_path(filename);
|
||||
}
|
||||
|
||||
bool lvledit::open_from_path(const char *path) {
|
||||
FILE *fp = fopen(path, "rb");
|
||||
|
||||
if (!fp) {
|
||||
tms_errorf("could not open file '%s'", path);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fp) {
|
||||
_fseek(fp, 0, SEEK_END);
|
||||
long size = _ftell(fp);
|
||||
_fseek(fp, 0, SEEK_SET);
|
||||
|
|
@ -1244,82 +1247,6 @@ bool lvledit::open(int lvl_type, uint32_t lvl_id) {
|
|||
this->lb.size = size;
|
||||
this->lvl.read(&this->lb);
|
||||
this->header_size = this->lvl.get_size();
|
||||
this->lvl_type = lvl_type;
|
||||
this->lvl_id = lvl_id;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lvledit::save() {
|
||||
if (this->lvl.get_size() != this->header_size) {
|
||||
// new header size does not match old header size,
|
||||
// we need to perform a memmove on the object data
|
||||
|
||||
int diff = this->lvl.get_size() - this->header_size;
|
||||
|
||||
if (diff > 0)
|
||||
this->lb.ensure(diff);
|
||||
|
||||
char *header_end = (char*)this->lb.buf + this->header_size;
|
||||
memmove(header_end + diff, header_end, this->lb.size - this->header_size);
|
||||
|
||||
this->header_size += diff;
|
||||
this->lb.size += diff;
|
||||
}
|
||||
|
||||
int saved_size = this->lb.size;
|
||||
this->lb.size = 0;
|
||||
this->lvl.write(&this->lb);
|
||||
this->lb.size = saved_size;
|
||||
|
||||
char filename[1024];
|
||||
snprintf(filename, 1023, "%s/%d.%s", pkgman::get_level_path(this->lvl_type), this->lvl_id, pkgman::get_level_ext(this->lvl_type));
|
||||
|
||||
FILE *fp = fopen(filename, "wb");
|
||||
|
||||
if (fp) {
|
||||
fwrite(this->lb.buf, 1, this->lb.size, fp);
|
||||
fclose(fp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
tms_errorf("could not open file '%s' for writing", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool lvledit::open_from_path(const char *path) {
|
||||
FILE *fp = fopen(path, "rb");
|
||||
|
||||
if (fp) {
|
||||
fseek(fp, 0, SEEK_END);
|
||||
long size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
if (size > 2*1024*1024) {
|
||||
tms_fatalf("file too big");
|
||||
}
|
||||
|
||||
this->lb.reset();
|
||||
this->lb.size = 0;
|
||||
this->lb.ensure((int)size);
|
||||
|
||||
fread(this->lb.buf, 1, size, fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
this->lb.size = size;
|
||||
bool r = this->lvl.read(&this->lb);
|
||||
if (!r) {
|
||||
fprintf(stderr, "uh oh\n");
|
||||
}
|
||||
this->header_size = this->lvl.get_size();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -1348,15 +1275,15 @@ bool lvledit::save_to_path(const char *path) {
|
|||
|
||||
FILE *fp = fopen(path, "wb");
|
||||
|
||||
if (fp) {
|
||||
if (!fp) {
|
||||
tms_errorf("could not open file '%s' for writing", path);
|
||||
return false;
|
||||
}
|
||||
|
||||
fwrite(this->lb.buf, 1, this->lb.size, fp);
|
||||
fclose(fp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
tms_errorf("could not open file '%s' for writing", path);
|
||||
return false;
|
||||
}
|
||||
|
||||
void lvledit::print_gids() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue