added functions to handle score cache files and directories
authorHolger Schemel <info@artsoft.org>
Sat, 24 Apr 2021 23:50:32 +0000 (01:50 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 1 May 2021 13:38:31 +0000 (15:38 +0200)
src/libgame/setup.c
src/libgame/setup.h

index dbfec24dd8202d1a924230408950d822064c0dd6..1d3be4fa3e064992cc59eecaa6fb029f8f9c6db1 100644 (file)
@@ -88,6 +88,16 @@ static char *getLevelClassDescription(TreeInfo *ti)
     return "Unknown Level Class";
 }
 
+static char *getCacheDir(void)
+{
+  static char *cache_dir = NULL;
+
+  if (cache_dir == NULL)
+    cache_dir = getPath2(getMainUserGameDataDir(), CACHE_DIRECTORY);
+
+  return cache_dir;
+}
+
 static char *getScoreDir(char *level_subdir)
 {
   static char *score_dir = NULL;
@@ -114,6 +124,27 @@ static char *getScoreDir(char *level_subdir)
   return score_dir;
 }
 
+static char *getScoreCacheDir(char *level_subdir)
+{
+  static char *score_dir = NULL;
+  static char *score_level_dir = NULL;
+  char *score_subdir = SCORES_DIRECTORY;
+
+  if (score_dir == NULL)
+    score_dir = getPath2(getCacheDir(), score_subdir);
+
+  if (level_subdir != NULL)
+  {
+    checked_free(score_level_dir);
+
+    score_level_dir = getPath2(score_dir, level_subdir);
+
+    return score_level_dir;
+  }
+
+  return score_dir;
+}
+
 static char *getScoreTapeDir(char *level_subdir, int nr)
 {
   static char *score_tape_dir = NULL;
@@ -169,16 +200,6 @@ static char *getLevelSetupDir(char *level_subdir)
   return levelsetup_dir;
 }
 
-static char *getCacheDir(void)
-{
-  static char *cache_dir = NULL;
-
-  if (cache_dir == NULL)
-    cache_dir = getPath2(getMainUserGameDataDir(), CACHE_DIRECTORY);
-
-  return cache_dir;
-}
-
 static char *getNetworkDir(void)
 {
   static char *network_dir = NULL;
@@ -607,6 +628,21 @@ char *getScoreFilename(int nr)
   return filename;
 }
 
+char *getScoreCacheFilename(int nr)
+{
+  static char *filename = NULL;
+  char basename[MAX_FILENAME_LEN];
+
+  checked_free(filename);
+
+  sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
+
+  // used instead of "leveldir_current->subdir" (for network games)
+  filename = getPath2(getScoreCacheDir(levelset.identifier), basename);
+
+  return filename;
+}
+
 char *getScoreTapeBasename(char *name)
 {
   static char basename[MAX_FILENAME_LEN];
@@ -1116,6 +1152,14 @@ void InitScoreDirectory(char *level_subdir)
   createDirectory(getScoreDir(level_subdir), "level score", permissions);
 }
 
+void InitScoreCacheDirectory(char *level_subdir)
+{
+  createDirectory(getMainUserGameDataDir(), "main user data", PERMS_PRIVATE);
+  createDirectory(getCacheDir(), "cache data", PERMS_PRIVATE);
+  createDirectory(getScoreCacheDir(NULL), "main score", PERMS_PRIVATE);
+  createDirectory(getScoreCacheDir(level_subdir), "level score", PERMS_PRIVATE);
+}
+
 void InitScoreTapeDirectory(char *level_subdir, int nr)
 {
   int permissions = (program.global_scores ? PERMS_PUBLIC : PERMS_PRIVATE);
index b84bb822dd7abe6d98670b9c1f065b9fe1748fc6..da6c3f9fb9695bfd844a24c4fdf51ba0d1bf3252 100644 (file)
@@ -266,6 +266,7 @@ char *getProgramConfigFilename(char *);
 char *getTapeFilename(int);
 char *getSolutionTapeFilename(int);
 char *getScoreFilename(int);
+char *getScoreCacheFilename(int);
 char *getScoreTapeBasename(char *);
 char *getScoreTapeFilename(char *, int);
 char *getSetupFilename(void);
@@ -286,6 +287,7 @@ char *getCustomMusicDirectory(void);
 
 void InitTapeDirectory(char *);
 void InitScoreDirectory(char *);
+void InitScoreCacheDirectory(char *);
 void InitScoreTapeDirectory(char *, int);
 void InitUserLevelDirectory(char *);
 void InitNetworkLevelDirectory(char *);