Ability to define cartridge directory TIC-80 standalone uses #175

This commit is contained in:
BADIM-PC\Vadim 2017-11-19 23:23:27 +03:00
parent dba2665fef
commit 192a80351a
5 changed files with 130 additions and 51 deletions

View File

@ -733,7 +733,7 @@ static bool loadProject(Console* console, const char* data, s32 size, tic_cartri
static bool hasExt(const char* name, const char* ext) static bool hasExt(const char* name, const char* ext)
{ {
return strstr(name, ext) == name + strlen(name) - strlen(ext); return strcmp(name + strlen(name) - strlen(ext), ext) == 0;
} }
#endif #endif
@ -2584,6 +2584,8 @@ static void cmdLoadCart(Console* console, const char* name)
loadCart(console->tic, &embed.file, data, size, true); loadCart(console->tic, &embed.file, data, size, true);
} }
strcpy(console->romName, fsFilename(name));
embed.yes = true; embed.yes = true;
SDL_free(data); SDL_free(data);

100
src/fs.c
View File

@ -29,12 +29,11 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#if !defined(__WINRT__) && !defined(__WINDOWS__)
#include <unistd.h>
#endif
#if defined(__WINRT__) || defined(__WINDOWS__) #if defined(__WINRT__) || defined(__WINDOWS__)
#include <direct.h> #include <direct.h>
#include <windows.h>
#else
#include <unistd.h>
#endif #endif
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
@ -540,18 +539,80 @@ static void makeDir(const char* name)
#endif #endif
} }
bool fsExistsFile(FileSystem* fs, const char* name) const char* fsFilename(const char *path)
{ {
const char* path = getFilePath(fs, name); const char* full = fsFullname(path);
FILE* file = tic_fopen(UTF8ToString(path), UTF8ToString("rb")); const char* base = fsBasename(path);
if(file) return base ? full + strlen(fsBasename(path)) : NULL;
{
fclose(file);
return true;
} }
return false; const char* fsFullname(const char *path)
{
char* result = NULL;
#if defined(__WINDOWS__) || defined(__WINRT__)
static wchar_t wpath[FILENAME_MAX];
GetFullPathNameW(UTF8ToString(path), sizeof(wpath), wpath, NULL);
result = StringToUTF8(wpath);
#else
#endif
return result;
}
const char* fsBasename(const char *path)
{
char* result = NULL;
#if defined(__WINDOWS__) || defined(__WINRT__)
{
char* full = (char*)fsFullname(path);
struct tic_stat_struct s;
if(tic_stat(UTF8ToString(full), &s) == 0)
{
result = full;
if(S_ISREG(s.st_mode))
{
const char* ptr = result + strlen(result);
while(ptr >= result)
{
if(*ptr == '\\')
{
result[ptr-result] = '\0';
break;
}
ptr--;
}
}
}
}
if(result && result[strlen(result)-1] != '\\')
strcat(result, "\\");
#else
#endif
return result;
}
bool fsExists(const char* name)
{
struct tic_stat_struct s;
return tic_stat(UTF8ToString(name), &s) == 0;
}
bool fsExistsFile(FileSystem* fs, const char* name)
{
return fsExists(getFilePath(fs, name));
} }
bool fsSaveFile(FileSystem* fs, const char* name, const void* data, size_t size, bool overwrite) bool fsSaveFile(FileSystem* fs, const char* name, const void* data, size_t size, bool overwrite)
@ -714,11 +775,21 @@ void fsOpenWorkingFolder(FileSystem* fs)
fsOpenSystemPath(fs, path); fsOpenSystemPath(fs, path);
} }
void createFileSystem(void(*callback)(FileSystem*)) void createFileSystem(const char* path, void(*callback)(FileSystem*))
{ {
FileSystem* fs = (FileSystem*)SDL_malloc(sizeof(FileSystem)); FileSystem* fs = (FileSystem*)SDL_malloc(sizeof(FileSystem));
memset(fs, 0, sizeof(FileSystem)); memset(fs, 0, sizeof(FileSystem));
fs->net = createNet();
if(path)
{
strcpy(fs->dir, path);
callback(fs);
}
else
{
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
strcpy(fs->dir, "/" TIC_PACKAGE "/" TIC_NAME "/"); strcpy(fs->dir, "/" TIC_PACKAGE "/" TIC_NAME "/");
@ -738,8 +809,6 @@ void createFileSystem(void(*callback)(FileSystem*))
#endif #endif
fs->net = createNet();
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
EM_ASM_ EM_ASM_
( (
@ -766,3 +835,4 @@ void createFileSystem(void(*callback)(FileSystem*))
callback(fs); callback(fs);
#endif #endif
} }
}

View File

@ -47,7 +47,7 @@ typedef void(*OpenCallback)(const char* name, const void* buffer, size_t size, v
typedef struct FileSystem FileSystem; typedef struct FileSystem FileSystem;
void createFileSystem(void(*callback)(FileSystem*)); void createFileSystem(const char* path, void(*callback)(FileSystem*));
void fsEnumFiles(FileSystem* fs, ListCallback callback, void* data); void fsEnumFiles(FileSystem* fs, ListCallback callback, void* data);
void fsAddFile(FileSystem* fs, AddCallback callback, void* data); void fsAddFile(FileSystem* fs, AddCallback callback, void* data);
@ -61,6 +61,10 @@ void* fsLoadRootFile(FileSystem* fs, const char* name, s32* size);
void fsMakeDir(FileSystem* fs, const char* name); void fsMakeDir(FileSystem* fs, const char* name);
bool fsExistsFile(FileSystem* fs, const char* name); bool fsExistsFile(FileSystem* fs, const char* name);
const char* fsBasename(const char *path);
const char* fsFilename(const char *path);
const char* fsFullname(const char *path);
bool fsExists(const char* name);
void* fsReadFile(const char* path, s32* size); void* fsReadFile(const char* path, s32* size);
bool fsWriteFile(const char* path, const void* data, s32 size); bool fsWriteFile(const char* path, const void* data, s32 size);
bool fsCopyFile(const char* src, const char* dst); bool fsCopyFile(const char* src, const char* dst);

View File

@ -2419,7 +2419,7 @@ static void onFSInitialized(FileSystem* fs)
void onEmscriptenWget(const char* file) void onEmscriptenWget(const char* file)
{ {
studio.argv[1] = DEFAULT_CART; studio.argv[1] = DEFAULT_CART;
createFileSystem(onFSInitialized); createFileSystem(NULL, onFSInitialized);
} }
void onEmscriptenWgetError(const char* error) {} void onEmscriptenWgetError(const char* error) {}
@ -2446,12 +2446,14 @@ s32 main(s32 argc, char **argv)
{ {
emscripten_async_wget(studio.argv[1], DEFAULT_CART, onEmscriptenWget, onEmscriptenWgetError); emscripten_async_wget(studio.argv[1], DEFAULT_CART, onEmscriptenWget, onEmscriptenWgetError);
} }
else createFileSystem(onFSInitialized); else createFileSystem(NULL, onFSInitialized);
emscripten_set_main_loop(tick, TIC_FRAMERATE == 60 ? 0 : TIC_FRAMERATE, 1); emscripten_set_main_loop(tick, TIC_FRAMERATE == 60 ? 0 : TIC_FRAMERATE, 1);
#else #else
createFileSystem(onFSInitialized); printf("filename %s\n", fsFilename("../TIC-80/config.tic"));
createFileSystem(argc > 1 && fsExists(argv[1]) ? fsBasename(argv[1]) : NULL, onFSInitialized);
{ {
u64 nextTick = SDL_GetPerformanceCounter(); u64 nextTick = SDL_GetPerformanceCounter();

View File

@ -375,7 +375,7 @@ static void replace(char* src, const char* what, const char* with)
static bool hasExt(const char* name, const char* ext) static bool hasExt(const char* name, const char* ext)
{ {
return strstr(name, ext) == name + strlen(name) - strlen(ext); return strcmp(name + strlen(name) - strlen(ext), ext) == 0;
} }
static void cutExt(char* name, const char* ext) static void cutExt(char* name, const char* ext)
@ -400,7 +400,7 @@ static bool addMenuItem(const char* name, const char* info, s32 id, void* ptr, b
MenuItem* item = &data->items[data->count++]; MenuItem* item = &data->items[data->count++];
item->name = SDL_strdup(name); item->name = SDL_strdup(name);
bool project = false;
if(dir) if(dir)
{ {
char folder[FILENAME_MAX]; char folder[FILENAME_MAX];
@ -417,7 +417,7 @@ static bool addMenuItem(const char* name, const char* info, s32 id, void* ptr, b
else else
{ {
cutExt(item->label, ProjectExt); cutExt(item->label, ProjectExt);
item->project = true; project = true;
} }
@ -429,6 +429,7 @@ static bool addMenuItem(const char* name, const char* info, s32 id, void* ptr, b
item->id = id; item->id = id;
item->dir = dir; item->dir = dir;
item->cover = NULL; item->cover = NULL;
item->project = project;
} }
return data->count < MAX_CARTS; return data->count < MAX_CARTS;