/* Index des fichiers fournis par les mods (voir mods.h). Construit une fois au démarrage : * clé = chemin relatif en majuscules avec '\\', triée pour une recherche dichotomique. */ #include #include #include #include #include "mods.h" #include "rt.h" #include "addr.h" #ifdef _WIN32 #include #else #include #include #endif typedef struct { char *key, *path; int mod; } Entry; typedef struct { Entry *e; int n, cap; } Index; static Index idx_files, idx_hd; static char **mod_names; static int n_mods; static char *dupstr(const char *s) { size_t n = strlen(s) + 1; char *d = (char *)malloc(n); if (d) memcpy(d, s, n); return d; } static void make_key(const char *rel, char *key, size_t n) { size_t i; for (i = 0; rel[i] && i + 1 < n; i++) key[i] = (char)(rel[i] == '/' ? '\\' : toupper((unsigned char)rel[i])); key[i] = 0; } static void add(Index *ix, const char *key, const char *path, int mod) { if (ix->n == ix->cap) { int cap = ix->cap ? ix->cap * 2 : 256; Entry *e = (Entry *)realloc(ix->e, (size_t)cap * sizeof *e); if (!e) return; ix->e = e; ix->cap = cap; } ix->e[ix->n].key = dupstr(key); ix->e[ix->n].path = dupstr(path); ix->e[ix->n].mod = mod; ix->n++; } /* ------------------------------------------------------------------ parcours de dossiers */ typedef void (*VisitFn)(const char *name, int is_dir, void *ctx); static void list_dir(const char *dir, VisitFn fn, void *ctx) { #ifdef _WIN32 char spec[600]; struct _finddata_t fd; intptr_t h; snprintf(spec, sizeof spec, "%s\\*", dir); h = _findfirst(spec, &fd); if (h == -1) return; do { if (!strcmp(fd.name, ".") || !strcmp(fd.name, "..")) continue; fn(fd.name, (fd.attrib & _A_SUBDIR) != 0, ctx); } while (_findnext(h, &fd) == 0); _findclose(h); #else DIR *d = opendir(dir); struct dirent *e; if (!d) return; while ((e = readdir(d))) { char p[1024]; struct stat st; if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; snprintf(p, sizeof p, "%s/%s", dir, e->d_name); if (stat(p, &st) == 0) fn(e->d_name, S_ISDIR(st.st_mode), ctx); } closedir(d); #endif } typedef struct { Index *ix; int mod; char host[600]; char rel[300]; int depth; } Walk; static void walk_visit(const char *name, int is_dir, void *ctx) { Walk *w = (Walk *)ctx, sub = *w; char key[300]; snprintf(sub.host, sizeof sub.host, "%s/%s", w->host, name); if (w->rel[0]) snprintf(sub.rel, sizeof sub.rel, "%s\\%s", w->rel, name); else snprintf(sub.rel, sizeof sub.rel, "%s", name); if (is_dir) { if (w->depth < 8) { sub.depth = w->depth + 1; list_dir(sub.host, walk_visit, &sub); } return; } make_key(sub.rel, key, sizeof key); add(w->ix, key, sub.host, w->mod); } static void walk(Index *ix, int mod, const char *root) { Walk w; memset(&w, 0, sizeof w); w.ix = ix; w.mod = mod; snprintf(w.host, sizeof w.host, "%s", root); list_dir(root, walk_visit, &w); } typedef struct { char **names; int n, cap; } NameList; static void collect_mod(const char *name, int is_dir, void *ctx) { NameList *l = (NameList *)ctx; if (!is_dir || name[0] == '.' || name[0] == '_') return; /* _xxx : désactivé par convention */ if (l->n == l->cap) { int cap = l->cap ? l->cap * 2 : 16; char **p = (char **)realloc(l->names, (size_t)cap * sizeof *p); if (!p) return; l->names = p; l->cap = cap; } l->names[l->n++] = dupstr(name); } static int cmp_name(const void *a, const void *b) { const char *x = *(const char *const *)a, *y = *(const char *const *)b; for (; *x && tolower((unsigned char)*x) == tolower((unsigned char)*y); x++, y++) {} return tolower((unsigned char)*x) - tolower((unsigned char)*y); } /* clé croissante, puis mod décroissant : le premier d'une série de doublons est le plus prioritaire */ static int cmp_entry(const void *a, const void *b) { const Entry *x = (const Entry *)a, *y = (const Entry *)b; int c = strcmp(x->key, y->key); return c ? c : y->mod - x->mod; } static void finish(Index *ix) { int i, j = 0; if (!ix->n) return; qsort(ix->e, (size_t)ix->n, sizeof *ix->e, cmp_entry); for (i = 0; i < ix->n; i++) { if (j && !strcmp(ix->e[j - 1].key, ix->e[i].key)) { free(ix->e[i].key); free(ix->e[i].path); continue; } ix->e[j++] = ix->e[i]; } ix->n = j; } static int listed(const char *csv, const char *name) { const char *p = csv; size_t n = strlen(name); if (!csv) return 0; while (*p) { const char *e = strchr(p, ','); size_t len; while (*p == ' ') p++; len = e ? (size_t)(e - p) : strlen(p); while (len && p[len - 1] == ' ') len--; if (len == n) { size_t k; for (k = 0; k < n && tolower((unsigned char)p[k]) == tolower((unsigned char)name[k]); k++) {} if (k == n) return 1; } if (!e) break; p = e + 1; } return 0; } /* version du jeu d'où viennent les fichiers du mod : clé game= de mod.ini (« fr » si absente, pour les mods * faits avant les autres versions). Les images diffèrent d'une version à l'autre (texte dans les décors, * journaux, carte, barre d'actions) : un mod ne vaut que pour sa version. */ static void mod_game(const char *dir, const char *name, char *out, size_t n) { char p[600], line[256]; FILE *f; snprintf(out, n, "fr"); snprintf(p, sizeof p, "%s/%s/mod.ini", dir, name); if (!(f = fopen(p, "r"))) return; while (fgets(line, sizeof line, f)) { char *s = line, *e; while (*s == ' ' || *s == '\t') s++; if (tolower((unsigned char)s[0]) != 'g' || tolower((unsigned char)s[1]) != 'a' || tolower((unsigned char)s[2]) != 'm' || tolower((unsigned char)s[3]) != 'e') continue; s += 4; while (*s == ' ' || *s == '\t') s++; if (*s++ != '=') continue; while (*s == ' ' || *s == '\t') s++; for (e = s; *e && *e != '\r' && *e != '\n' && *e != ' ' && *e != '\t' && *e != ';'; e++) *e = (char)tolower((unsigned char)*e); *e = 0; if (*s) snprintf(out, n, "%s", s); break; } fclose(f); } int mods_init(const char *dir, const char *disabled) { NameList l = {0}; int i; mods_shutdown(); if (!dir || !*dir) return 0; list_dir(dir, collect_mod, &l); if (l.n) qsort(l.names, (size_t)l.n, sizeof *l.names, cmp_name); mod_names = (char **)calloc((size_t)(l.n ? l.n : 1), sizeof *mod_names); for (i = 0; i < l.n; i++) { char p[600], game[16]; if (listed(disabled, l.names[i])) { fprintf(stderr, "[mods] %s : désactivé\n", l.names[i]); free(l.names[i]); continue; } mod_game(dir, l.names[i], game, sizeof game); if (strcmp(game, DTR_VERSION)) { fprintf(stderr, "[mods] %s : fait pour la version %s, pas %s : ignoré\n", l.names[i], game, DTR_VERSION); free(l.names[i]); continue; } snprintf(p, sizeof p, "%s/%s/files", dir, l.names[i]); walk(&idx_files, n_mods, p); snprintf(p, sizeof p, "%s/%s/hd", dir, l.names[i]); walk(&idx_hd, n_mods, p); mod_names[n_mods++] = l.names[i]; } free(l.names); finish(&idx_files); finish(&idx_hd); for (i = 0; i < n_mods; i++) fprintf(stderr, "[mods] %s : actif\n", mod_names[i]); if (n_mods) fprintf(stderr, "[mods] %d fichier(s) remplacé(s), %d image(s) HD\n", idx_files.n, idx_hd.n); return n_mods; } static void free_index(Index *ix) { int i; for (i = 0; i < ix->n; i++) { free(ix->e[i].key); free(ix->e[i].path); } free(ix->e); memset(ix, 0, sizeof *ix); } void mods_shutdown(void) { int i; free_index(&idx_files); free_index(&idx_hd); for (i = 0; i < n_mods; i++) free(mod_names[i]); free(mod_names); mod_names = NULL; n_mods = 0; } static const Entry *lookup(const Index *ix, const char *key) { int lo = 0, hi = ix->n - 1; while (lo <= hi) { int mid = (lo + hi) / 2, c = strcmp(key, ix->e[mid].key); if (!c) return &ix->e[mid]; if (c < 0) hi = mid - 1; else lo = mid + 1; } return NULL; } int mods_find_file(const char *rel, char *out, size_t n) { char key[300]; const Entry *e; if (!idx_files.n) return 0; make_key(rel, key, sizeof key); e = lookup(&idx_files, key); if (!e) return 0; snprintf(out, n, "%s", e->path); return 1; } int mods_hd_count(void) { return idx_hd.n; } int mods_find_hd(const char *name, char *out, size_t n) { char key[300]; const Entry *e; if (!idx_hd.n) return 0; snprintf(key, sizeof key, "%s.PNG", name); make_key(key, key, sizeof key); e = lookup(&idx_hd, key); if (!e) return 0; snprintf(out, n, "%s", e->path); return 1; } int mods_count(void) { return n_mods; } const char *mods_name(int i) { return i >= 0 && i < n_mods ? mod_names[i] : ""; }