Something went wrong. Try again.
The game Sim Cinema Deluxe 2.5, now for UNIX terminals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299/* save.c: where the game keeps its files, and saving and loading them. */#include "save.h"
#include <dirent.h>#include <errno.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>
#define PREFS_MAGIC 0x50444353u /* "SCDP" in little-endian byte order */#define COMPANY_MAGIC 0x43444353u /* "SCDC" */#define SAVE_VERSION 1u /* bump whenever the structs in game.h change */
typedef struct save_header { uint32_t magic; uint32_t version; uint64_t size;} save_header;
typedef struct company_save { company company; active_film current_film; game_log messages; rng rng;} company_save;
static void mkdir_p(const char *path) { char buf[1024]; snprintf(buf, sizeof buf, "%s", path); for (char *p = buf + 1; *p; p++) if (*p == '/') { *p = 0; mkdir(buf, 0755); *p = '/'; } mkdir(buf, 0755);}
const char *data_dir(void) { static char dir[1024]; if (dir[0]) return dir; const char *env = getenv("SCDX_DIR"), *home = getenv("HOME");#ifndef __APPLE__ const char *xdg = getenv("XDG_DATA_HOME");#endif if (env && env[0]) snprintf(dir, sizeof dir, "%s", env);#ifdef __APPLE__ else if (home && home[0]) snprintf(dir, sizeof dir, "%s/Library/Application Support/SCDX", home);#else else if (xdg && xdg[0]) snprintf(dir, sizeof dir, "%s/scdx", xdg); else if (home && home[0]) snprintf(dir, sizeof dir, "%s/.local/share/scdx", home);#endif else snprintf(dir, sizeof dir, "."); char companies[1100]; snprintf(companies, sizeof companies, "%s/companies", dir); mkdir_p(companies); return dir;}
static bool data_path(char *out, size_t n, const char *sub, const char *name) { int len = sub ? snprintf(out, n, "%s/%s/%s", data_dir(), sub, name) : snprintf(out, n, "%s/%s", data_dir(), name); return len > 0 && (size_t)len < n;}
static bool write_file(const char *path, uint32_t magic, const void *data, size_t size) { char tmp[1100]; if (snprintf(tmp, sizeof tmp, "%s.tmp", path) >= (int)sizeof tmp) return false; FILE *fp = fopen(tmp, "wb"); if (!fp) return false; save_header h = {magic, SAVE_VERSION, size}; bool ok = fwrite(&h, sizeof h, 1, fp) == 1 && fwrite(data, size, 1, fp) == 1; if (fclose(fp) != 0) ok = false; if (ok) ok = rename(tmp, path) == 0; if (!ok) remove(tmp); return ok;}
static load_result read_file(const char *path, uint32_t magic, void *buf, size_t size) { FILE *fp = fopen(path, "rb"); if (!fp) return errno == ENOENT ? LOAD_MISSING : LOAD_BAD; save_header h; bool ok = fread(&h, sizeof h, 1, fp) == 1 && h.magic == magic && h.version == SAVE_VERSION && h.size == size && fread(buf, size, 1, fp) == 1 && fgetc(fp) == EOF; fclose(fp); return ok ? LOAD_OK : LOAD_BAD;}
#define TERMINATE(s) ((s)[sizeof (s) - 1] = '\0')
static bool valid_person(person *p) { TERMINATE(p->name); return true;}
static bool valid_film(film *f) { TERMINATE(f->title); TERMINATE(f->tagline); TERMINATE(f->description); for (int c = 0; c < NUM_CATEGORIES; c++) valid_person(&f->crew[c]); return (unsigned)f->genre <= GENRE_SCIFI && (unsigned)f->rating <= RATING_NC17 && (unsigned)f->distributor < NUM_DISTRIBUTORS && f->stars >= 0 && f->stars <= 4;}
static bool valid_preferences(preferences *p) { TERMINATE(p->player_name); TERMINATE(p->company_name); size_t cap = sizeof p->casts[0].people / sizeof p->casts[0].people[0]; for (int c = 0; c < NUM_CATEGORIES; c++) { if (p->casts[c].count > cap) return false; for (size_t i = 0; i < p->casts[c].count; i++) valid_person(&p->casts[c].people[i]); } if (p->casts[P_DIRECTOR].count < 1) return false; for (int i = 0; i < 20; i++) TERMINATE(p->top20[i].film_name); return true;}
static bool valid_company(company *co) { TERMINATE(co->company_name); TERMINATE(co->ceo_name); if ((unsigned)co->status > STATUS_MOGUL) return false; if (co->archive_count > sizeof co->archive / sizeof co->archive[0]) return false; for (size_t i = 0; i < co->archive_count; i++) if (!valid_film(&co->archive[i])) return false; return true;}
static bool valid_log(game_log *l) { size_t cap = sizeof l->messages / sizeof l->messages[0]; if (l->size > cap || l->start >= cap) return false; for (size_t i = 0; i < cap; i++) { TERMINATE(l->messages[i].message); if ((unsigned)l->messages[i].kind > MSG_FILM) return false; } return true;}
static bool valid_active_film(active_film *af) { if ((unsigned)af->state > FILM_RELEASED) return false; if (af->state == NO_FILM) return true; if (!valid_film(&af->record)) return false; if (af->month < 0 || af->month > 11 || af->week < 0 || af->week > 3) return false;
if (af->state == FILM_IN_PROD) { film_dev *dev = &af->state_data.development; TERMINATE(dev->screening_city); if (dev->progress < 0 || dev->progress > 100) return false; if (dev->num_phases < 2 || dev->num_phases > 4) return false; for (int p = 0; p < NUM_PHASES; p++) if (dev->phase_progress[p] < 0 || dev->phase_progress[p] > 100) return false; } else { film_release *rel = &af->state_data.release; box_office *bo = &rel->box_office; if (rel->num_reviews < 0 || rel->num_reviews > 4) return false; if (rel->history_len < 0 || (size_t)rel->history_len > sizeof rel->gross_history / sizeof rel->gross_history[0]) return false; for (int i = 0; i < rel->num_reviews; i++) if ((unsigned)rel->reviews[i].newspaper >= NUM_NEWSPAPERS || rel->reviews[i].stars < 1 || rel->reviews[i].stars > 4) return false; if (bo->num_new_releases > sizeof bo->new_releases / sizeof bo->new_releases[0]) return false; for (size_t i = 0; i < sizeof bo->competitors / sizeof bo->competitors[0]; i++) TERMINATE(bo->competitors[i].title); for (size_t i = 0; i < sizeof bo->new_releases / sizeof bo->new_releases[0]; i++) TERMINATE(bo->new_releases[i].title); if (bo->last_rank < 0 || bo->last_rank > 10) return false; } return true;}
static bool valid_company_save(company_save *cs) { return cs->rng.s != 0 && valid_company(&cs->company) && valid_log(&cs->messages) && valid_active_film(&cs->current_film);}
/* preferences */
bool save_prefs(const game *g) { char path[1100]; return data_path(path, sizeof path, NULL, "prefs.sav") && write_file(path, PREFS_MAGIC, &g->preferences, sizeof g->preferences);}
load_result load_prefs(game *g) { char path[1100]; if (!data_path(path, sizeof path, NULL, "prefs.sav")) return LOAD_BAD; preferences *tmp = malloc(sizeof *tmp); if (!tmp) return LOAD_BAD; load_result r = read_file(path, PREFS_MAGIC, tmp, sizeof *tmp); if (r == LOAD_OK && !valid_preferences(tmp)) r = LOAD_BAD; if (r == LOAD_OK) memcpy(&g->preferences, tmp, sizeof *tmp); if (r == LOAD_BAD) { char bad[1100]; snprintf(bad, sizeof bad, "%s.bad", path); rename(path, bad); } free(tmp); return r;}
/* companies */
bool new_company_file(const char *company_name, char *file, size_t n) { char base[96]; size_t k = 0; for (const unsigned char *p = (const unsigned char *)company_name; *p && k < sizeof base - 1; p++) base[k++] = (*p >= 0x80 || (*p >= '0' && *p <= '9') || ((*p | 32) >= 'a' && (*p | 32) <= 'z') || *p == ' ' || *p == '-' || *p == '_') ? (char)*p : '_'; while (k > 0 && ((unsigned char)base[k - 1] & 0xC0) == 0x80) k--; if (k > 0 && ((unsigned char)base[k - 1] & 0x80)) k--; base[k] = 0; if (!base[0]) snprintf(base, sizeof base, "company"); for (int i = 1; i < 1000; i++) { char path[1100]; if (i == 1) snprintf(file, n, "%s.sav", base); else snprintf(file, n, "%s %d.sav", base, i); if (!data_path(path, sizeof path, "companies", file)) return false; struct stat st; if (stat(path, &st) != 0) return true; } return false;}
bool save_company(const game *g, const char *file) { char path[1100]; if (!data_path(path, sizeof path, "companies", file)) return false; company_save *cs = malloc(sizeof *cs); if (!cs) return false; cs->company = g->company; cs->current_film = g->current_film; cs->messages = g->messages; cs->rng = g->rng; bool ok = write_file(path, COMPANY_MAGIC, cs, sizeof *cs); free(cs); return ok;}
static company_save *read_company(const char *file) { char path[1100]; if (!data_path(path, sizeof path, "companies", file)) return NULL; company_save *cs = malloc(sizeof *cs); if (cs && (read_file(path, COMPANY_MAGIC, cs, sizeof *cs) != LOAD_OK || !valid_company_save(cs))) { free(cs); cs = NULL; } return cs;}
bool load_company(game *g, const char *file) { company_save *cs = read_company(file); if (!cs) return false; g->company = cs->company; g->current_film = cs->current_film; g->messages = cs->messages; g->rng = cs->rng; free(cs); return true;}
bool delete_company_file(const char *file) { char path[1100]; return data_path(path, sizeof path, "companies", file) && remove(path) == 0;}
static int newest_first(const void *a, const void *b) { const company_info *x = a, *y = b; return (x->saved < y->saved) - (x->saved > y->saved);}
int list_companies(company_info *out, int max) { char dir[1100]; if (!data_path(dir, sizeof dir, NULL, "companies")) return 0; DIR *d = opendir(dir); if (!d) return 0; int n = 0; struct dirent *e; while (n < max && (e = readdir(d)) != NULL) { size_t len = strlen(e->d_name); if (len < 5 || strcmp(e->d_name + len - 4, ".sav") != 0 || e->d_name[0] == '.') continue; if (len >= sizeof out[n].file) continue; company_info *ci = &out[n++]; memset(ci, 0, sizeof *ci); snprintf(ci->file, sizeof ci->file, "%s", e->d_name); char path[1100]; struct stat st; if (data_path(path, sizeof path, "companies", ci->file) && stat(path, &st) == 0) ci->saved = st.st_mtime; company_save *cs = read_company(ci->file); if (!cs) continue; ci->ok = true; snprintf(ci->name, sizeof ci->name, "%s", cs->company.company_name); ci->status = status_title(&cs->company); ci->films = cs->company.archive_count; ci->film_state = cs->current_film.state; snprintf(ci->film_title, sizeof ci->film_title, "%s", cs->current_film.record.title); free(cs); } closedir(d); qsort(out, (size_t)n, sizeof *out, newest_first); return n;}