Store cache data in a separate folder

This commit is contained in:
ROllerozxa 2025-03-22 20:49:20 +01:00
commit 043d64ead6
2 changed files with 90 additions and 1 deletions

49
lib/SDL3_polyfills.h vendored Normal file
View file

@ -0,0 +1,49 @@
#pragma once
// Put polyfills for stuff we want to use from SDL3. Remove this when we're on SDL3.
#include <SDL.h>
#include <jni.h>
const char *SDL_GetAndroidCachePath(void)
{
static char *s_AndroidCachePath = NULL;
if (!s_AndroidCachePath) {
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
jobject activity = (jobject)SDL_AndroidGetActivity();
if (!env || !activity) {
SDL_Log("Failed to get JNI environment or Activity.");
return NULL;
}
// Get the getCacheDir() method
jclass activityClass = (*env)->GetObjectClass(env, activity);
jmethodID getCacheDirMethod = (*env)->GetMethodID(env, activityClass, "getCacheDir", "()Ljava/io/File;");
if (!getCacheDirMethod) {
SDL_Log("Failed to get getCacheDir method.");
return NULL;
}
jobject cacheDirFile = (*env)->CallObjectMethod(env, activity, getCacheDirMethod);
jclass fileClass = (*env)->GetObjectClass(env, cacheDirFile);
jmethodID getAbsolutePathMethod = (*env)->GetMethodID(env, fileClass, "getAbsolutePath", "()Ljava/lang/String;");
if (!getAbsolutePathMethod) {
SDL_Log("Failed to get getAbsolutePath method.");
return NULL;
}
jstring absolutePath = (jstring)(*env)->CallObjectMethod(env, cacheDirFile, getAbsolutePathMethod);
const char* cachePath = (*env)->GetStringUTFChars(env, absolutePath, NULL);
char* result = SDL_strdup(cachePath);
(*env)->ReleaseStringUTFChars(env, absolutePath, cachePath);
s_AndroidCachePath = result;
}
return s_AndroidCachePath;
}

View file

@ -7,6 +7,10 @@
#include <unistd.h>
#ifdef __ANDROID__
#include <SDL3_polyfills.h>
#endif
#ifdef TMS_BACKEND_WINDOWS
#include <direct.h>
#define mkdir(dirname, ...) _mkdir(dirname)
@ -95,7 +99,43 @@ const char *tms_storage_path(void)
const char *tms_storage_cache_path(void)
{
return tms_storage_path();
#ifdef __ANDROID__
return SDL_GetAndroidCachePath();
#elif defined(__EMSCRIPTEN__)
return SDL_GetPrefPath("Bithack", "Principia");
#elif defined(SCREENSHOT_BUILD)
return "/tmp/principia_cache";
#else
static char *cache_path = 0;
if (cache_path)
return cache_path;
char *path = (char *)malloc(1024);
if (_storage_portable) { // Portable
char* exedir = SDL_GetBasePath();
strcpy(path, exedir);
strcat(path, "cache");
} else { // System
#ifdef TMS_BACKEND_WINDOWS
strcpy(path, getenv("LOCALAPPDATA"));
strcat(path, "\\Principia");
#else
const char* xdg_cache_home = getenv("XDG_CACHE_HOME");
if (!xdg_cache_home) {
strcpy(path, getenv("HOME"));
strcat(path, "/.cache/principia");
} else {
strcpy(path, xdg_cache_home);
strcat(path, "/principia");
}
#endif
}
cache_path = path;
return cache_path;
#endif
}
void tms_storage_create_dirs(void)