/** * Save all kinds of important info to the savegame. * @param fp The file to save to. * @return True if and only if all bytes were written successful. */ bool Info_Save(FILE *fp) { static uint16 savegameVersion = 0x0290; if (!fwrite_le_uint16(savegameVersion, fp)) return false; if (!SaveLoad_Save(s_saveInfo, fp, NULL)) return false; return true; }
/** * Save all Structures to a file. It converts pointers to indices where needed. * @param fp The file to save to. * @return True if and only if all bytes were written successful. */ bool Structure_Save(FILE *fp) { PoolFindStruct find; find.houseID = HOUSE_INVALID; find.type = 0xFFFF; find.index = 0xFFFF; while (true) { Structure *s; Structure ss; s = Structure_Find(&find); if (s == NULL) break; ss = *s; if (!SaveLoad_Save(s_saveStructure, fp, &ss)) return false; } return true; }
/** * Save from a struct to a file. * @param sld The description of the struct. * @param fp The file to write to. * @param object The object instance to write from. * @return True if and only if the writing was successful. */ bool SaveLoad_Save(const SaveLoadDesc *sld, FILE *fp, void *object) { while (sld->type_disk != SLDT_NULL) { uint32 value = 0; uint16 i; for (i = 0; i < sld->count; i++) { void *ptr = (sld->address == NULL ? ((uint8 *)object) + sld->offset : (uint8 *)sld->address) + i * sld->size; switch (sld->type_memory) { case SLDT_NULL: value = 0; break; case SLDT_UINT8: value = *(uint8 *)ptr; break; case SLDT_UINT16: value = *(uint16 *)ptr; break; case SLDT_UINT32: value = *(uint32 *)ptr; break; case SLDT_INT8: value = *(int8 *)ptr; break; case SLDT_INT16: value = *(int16 *)ptr; break; case SLDT_INT32: value = *(int32 *)ptr; break; case SLDT_SLD: if (!SaveLoad_Save(sld->sld, fp, ptr)) return false; break; case SLDT_CALLBACK: value = sld->callback(object, 0, false); break; } switch (sld->type_disk) { case SLDT_CALLBACK: case SLDT_SLD: case SLDT_NULL: break; case SLDT_UINT8: { uint8 v = (value > 0xFF) ? 0xFF : (uint8)value; if (fwrite(&v, sizeof(uint8), 1, fp) != 1) return false; } break; case SLDT_UINT16: { uint16 v = (uint16)value; if (fwrite(&v, sizeof(uint16), 1, fp) != 1) return false; } break; case SLDT_UINT32: { uint32 v = (uint32)value; if (fwrite(&v, sizeof(uint32), 1, fp) != 1) return false; } break; case SLDT_INT8: { int8 v = (int8)value; if (fwrite(&v, sizeof(int8), 1, fp) != 1) return false; } break; case SLDT_INT16: { int16 v = (int16)value; if (fwrite(&v, sizeof(int16), 1, fp) != 1) return false; } break; case SLDT_INT32: { int32 v = (int32)value; if (fwrite(&v, sizeof(int32), 1, fp) != 1) return false; } break; } } sld++; } return true; }