Skip to content

Commit

Permalink
Merge pull request #701 from crudelios/fix-file-manager-cache
Browse files Browse the repository at this point in the history
Fix file manager cache
  • Loading branch information
bvschaik authored Apr 19, 2024
2 parents ef1547d + e5565c8 commit c87d131
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .ci_scripts/run_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

case "$BUILD_TARGET" in
"vita")
docker exec vitasdk /bin/bash -c "mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DTARGET_PLATFORM=vita .."
docker exec vitasdk /bin/bash -c "git config --global --add safe.directory /build/git && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DTARGET_PLATFORM=vita .."
;;
"switch")
docker exec switchdev /bin/bash -c "git config --global --add safe.directory /build/git && /opt/devkitpro/portlibs/switch/bin/aarch64-none-elf-cmake -DCMAKE_BUILD_TYPE=Release -DTARGET_PLATFORM=switch -B build -S ."
Expand Down
2 changes: 1 addition & 1 deletion .ci_scripts/setup_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ case "$BUILD_TARGET" in
# Note: Using a tagged version of the container to make sure that it's not updated unexpectedly
# You can update the tag by obtaining a recent one from here: https://hub.docker.com/r/gnuton/vitasdk-docker/tags
# Make sure that it compiles correctly and runs on a Vita prior to pushing the change
docker run -d --name vitasdk --workdir /build/git -v "${PWD}:/build/git" gnuton/vitasdk-docker:20210217 tail -f /dev/null
docker run -d --name vitasdk --workdir /build/git -v "${PWD}:/build/git" gnuton/vitasdk-docker:20240412 tail -f /dev/null
;;
"switch")
# You can obtain a recent devkitA64 image from https://hub.docker.com/repository/docker/devkitpro/devkita64/general
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,18 @@ if (${TARGET_PLATFORM} STREQUAL "vita")
vorbisfile
vorbis
mikmod
xmp
modplug
mpg123
FLAC
ogg
opusfile
opus
stdc++
m
SceAppUtil_stub
SceAudio_stub
SceAudioIn_stub
SceCommonDialog_stub
SceCtrl_stub
SceDisplay_stub
Expand Down
49 changes: 17 additions & 32 deletions src/platform/file_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#define fs_dir_open _wopendir
#define fs_dir_close _wclosedir
#define fs_dir_read _wreaddir
#define dir_entry_name(d) wchar_to_utf8(d->d_name)
#define fs_chdir _wchdir
#define dir_entry_name(d) wchar_to_utf8((d)->d_name)
typedef const wchar_t *dir_name;

static const char *wchar_to_utf8(const wchar_t *str)
Expand All @@ -54,12 +55,15 @@ static wchar_t *utf8_to_wchar(const char *str)
}

#else // not _WIN32
#ifndef USE_FILE_CACHE
#define fs_dir_type DIR
#define fs_dir_entry struct dirent
#define fs_dir_open opendir
#define fs_dir_close closedir
#define fs_dir_read readdir
#define dir_entry_name(d) ((d)->d_name)
#endif
#define fs_chdir chdir
typedef const char *dir_name;
#endif

Expand All @@ -71,11 +75,7 @@ typedef const char *dir_name;
#define S_ISSOCK(m) 0
#endif

#ifdef __vita__
#define CURRENT_DIR VITA_PATH_PREFIX
#define set_dir_name(n) vita_prepend_path(n)
#define free_dir_name(n)
#elif defined(_WIN32)
#if defined(_WIN32)
#define CURRENT_DIR L"."
#define set_dir_name(n) utf8_to_wchar(n)
#define free_dir_name(n)
Expand All @@ -87,7 +87,7 @@ typedef const char *dir_name;

#ifdef _WIN32
#include <direct.h>
#elif !defined(__vita__)
#else
#include <unistd.h>
#endif

Expand Down Expand Up @@ -211,36 +211,21 @@ int platform_file_manager_set_base_path(const char *path)
}
#ifdef __ANDROID__
return android_set_base_path(path);
#elif defined(_WIN32)
wchar_t *wpath = utf8_to_wchar(path);
int result = _wchdir(wpath);
free(wpath);
return result == 0;
#else
return chdir(path) == 0;
dir_name set_path = set_dir_name(path);
int result = fs_chdir(set_path);
free_dir_name(set_path);
if (result == 0) {
#ifdef USE_FILE_CACHE
platform_file_manager_cache_invalidate();
#endif
}

#ifdef __vita__
FILE *platform_file_manager_open_file(const char *filename, const char *mode)
{
if (strchr(mode, 'w')) {
char temp_filename[FILE_NAME_MAX];
strncpy(temp_filename, filename, FILE_NAME_MAX - 1);
if (!file_exists(temp_filename, NOT_LOCALIZED)) {
platform_file_manager_cache_add_file_info(filename);
}
return 1;
}
return fopen(vita_prepend_path(filename), mode);
}

int platform_file_manager_remove_file(const char *filename)
{
platform_file_manager_cache_delete_file_info(filename);
return remove(vita_prepend_path(filename)) == 0;
return 0;
#endif
}

#elif defined(_WIN32)
#if defined(_WIN32)

FILE *platform_file_manager_open_file(const char *filename, const char *mode)
{
Expand Down
21 changes: 17 additions & 4 deletions src/platform/file_manager_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ const dir_info *platform_file_manager_cache_get_dir_info(const char *dir)
// This is effectively a hack, and definitely not full-proof, but the performance gains are well worth it
if (!*file_item->extension) {
static char full_name[FILE_NAME_MAX];
if (!dir_name_offset) {
strncpy(full_name, info->name, FILE_NAME_MAX);
dir_name_offset = strlen(info->name);
}
strncpy(full_name + dir_name_offset, name, FILE_NAME_MAX - 1 - dir_name_offset);
DIR *file_d = opendir(full_name);
if (file_d) {
Expand Down Expand Up @@ -165,4 +161,21 @@ int platform_file_manager_cache_file_has_extension(const file_info *f, const cha
return platform_file_manager_compare_filename(f->extension, extension) == 0;
}

void platform_file_manager_cache_invalidate(void)
{
dir_info *info = base_dir_info;
while (info) {
file_info *file_item = info->first_file;
while (file_item) {
file_info *old_file_item = file_item;
file_item = file_item->next;
free(old_file_item);
}
dir_info *old_info = info;
info = info->next;
free(old_info);
}
base_dir_info = 0;
}

#endif // USE_FILE_CACHE
1 change: 1 addition & 0 deletions src/platform/file_manager_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const dir_info *platform_file_manager_cache_get_dir_info(const char *dir);
int platform_file_manager_cache_file_has_extension(const file_info *f, const char *extension);
void platform_file_manager_cache_add_file_info(const char *filename);
void platform_file_manager_cache_delete_file_info(const char *filename);
void platform_file_manager_cache_invalidate(void);

#endif

Expand Down
28 changes: 18 additions & 10 deletions src/platform/julius.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "input/touch.h"
#include "platform/arguments.h"
#include "platform/file_manager.h"
#include "platform/file_manager_cache.h"
#include "platform/joystick.h"
#include "platform/keyboard_input.h"
#include "platform/platform.h"
Expand All @@ -32,7 +33,7 @@
#include "platform/switch/switch.h"
#include "platform/vita/vita.h"

#if defined(_WIN32)
#if defined(_WIN32) || defined(__vita__) || defined(__SWITCH__) || defined(__ANDROID__)
#include <string.h>
#endif

Expand All @@ -59,7 +60,7 @@ enum {
static struct {
int active;
int quit;
} data = {1, 0};
} data = { 1, 0 };

static void exit_with_status(int status)
{
Expand Down Expand Up @@ -245,6 +246,9 @@ static void handle_window_event(SDL_WindowEvent *event, int *window_active)

case SDL_WINDOWEVENT_SHOWN:
SDL_Log("Window %u shown", (unsigned int) event->windowID);
#ifdef USE_FILE_CACHE
platform_file_manager_cache_invalidate();
#endif
*window_active = 1;
break;
case SDL_WINDOWEVENT_HIDDEN:
Expand Down Expand Up @@ -456,6 +460,11 @@ static int pre_init(const char *custom_data_dir)
SDL_Log("Loading game from %s", custom_data_dir);
if (!platform_file_manager_set_base_path(custom_data_dir)) {
SDL_Log("%s: directory not found", custom_data_dir);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Error",
"Julius requires the original files from Caesar 3.\n\n"
"Please enter the proper directory or copy the files to the selected directory.",
NULL);
return 0;
}
return game_pre_init();
Expand Down Expand Up @@ -503,13 +512,6 @@ static int pre_init(const char *custom_data_dir)
}
user_dir = ask_for_data_dir(1);
}
#elif defined(__vita__)
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Error",
"Julius requires the original files from Caesar 3.\n\n"
"Please add the files to:\n\n"
VITA_PATH_PREFIX,
NULL);
#else
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Julius requires the original files from Caesar 3 to run.",
Expand All @@ -533,7 +535,13 @@ static void setup(const julius_args *args)
exit_with_status(-1);
}

if (!pre_init(args->data_directory)) {
#ifdef __vita__
const char *base_dir = VITA_PATH_PREFIX;
#else
const char *base_dir = args->data_directory;
#endif

if (!pre_init(base_dir)) {
SDL_Log("Exiting: game pre-init failed");
exit_with_status(1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/sound_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void load_music_for_vita(const char *filename)
vita_music_data.buffer = 0;
}
strncpy(vita_music_data.filename, filename, FILE_NAME_MAX - 1);
SceUID fd = sceIoOpen(vita_prepend_path(filename), SCE_O_RDONLY, 0777);
SceUID fd = sceIoOpen(filename, SCE_O_RDONLY, 0777);
if (fd < 0) {
return;
}
Expand Down Expand Up @@ -379,7 +379,7 @@ static int put_custom_audio_stream(const Uint8 *audio_data, int len)
#endif

// Convert audio to SDL format
custom_music.cvt.buf = (Uint8 *)malloc((size_t)(len * custom_music.cvt.len_mult));
custom_music.cvt.buf = (Uint8 *) malloc((size_t) (len * custom_music.cvt.len_mult));
if (!custom_music.cvt.buf) {
return 0;
}
Expand Down
18 changes: 0 additions & 18 deletions src/platform/vita/vita.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#include <malloc.h>
#include "SDL.h"

#define PREPEND_PATH_OFFSET 17

static char prepended_path[2 * FILE_NAME_MAX * sizeof(char)];

// max heap size is approx. 330 MB with -d ATTRIBUTE2=12, otherwise max is 192
int _newlib_heap_size_user = 330 * 1024 * 1024;

Expand All @@ -36,20 +32,6 @@ void platform_init_callback(void)
center_mouse_cursor();
}

int chdir(const char *path)
{
return 0;
}

const char *vita_prepend_path(const char *path)
{
if (!prepended_path[0]) {
memcpy(prepended_path, VITA_PATH_PREFIX, strlen(VITA_PATH_PREFIX));
}
strncpy(prepended_path + PREPEND_PATH_OFFSET, path, 2 * FILE_NAME_MAX * sizeof(char) - PREPEND_PATH_OFFSET - 1);
return prepended_path;
}

static void vita_start_text_input(void)
{
if (!keyboard_is_capturing()) {
Expand Down
4 changes: 0 additions & 4 deletions src/platform/vita/vita.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,5 @@ void platform_hide_virtual_keyboard(void);

#define PLATFORM_USE_SOFTWARE_CURSOR

int chdir(const char *path);

const char *vita_prepend_path(const char *path);

#endif // __vita__
#endif // PLATFORM_VITA_H

0 comments on commit c87d131

Please sign in to comment.