Skip to content

Commit ffd0869

Browse files
committed
use hash suffix for covers
1 parent 6cf0854 commit ffd0869

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

src/studio/fs.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@
6464

6565
static const char* PublicDir = TIC_HOST;
6666

67+
typedef char string[TICNAME_MAX];
68+
6769
struct tic_fs
6870
{
69-
char dir[TICNAME_MAX];
70-
char work[TICNAME_MAX];
71+
string dir;
72+
string work;
7173
tic_net* net;
7274
};
7375

@@ -85,7 +87,7 @@ void syncfs()
8587

8688
const char* tic_fs_pathroot(tic_fs* fs, const char* name)
8789
{
88-
static char path[TICNAME_MAX];
90+
static string path;
8991

9092
snprintf(path, sizeof path, "%s%s", fs->dir, name);
9193

@@ -103,14 +105,14 @@ const char* tic_fs_pathroot(tic_fs* fs, const char* name)
103105

104106
const char* tic_fs_path(tic_fs* fs, const char* name)
105107
{
106-
static char path[TICNAME_MAX+1];
108+
static string path;
107109

108110
if(*name == '/')
109-
strncpy(path, name + 1, sizeof path);
111+
snprintf(path, sizeof path, "%s", name + 1);
110112
else if(strlen(fs->work))
111113
snprintf(path, sizeof path, "%s/%s", fs->work, name);
112114
else
113-
strncpy(path, name, sizeof path);
115+
snprintf(path, sizeof path, "%s", name);
114116

115117
return tic_fs_pathroot(fs, path);
116118
}
@@ -283,8 +285,6 @@ static void onDirResponse(const net_get_data* netData)
283285
{
284286
if(json_parse((char*)netData->done.data, netData->done.size))
285287
{
286-
typedef char string[TICNAME_MAX];
287-
288288
s32 folders = json_array("folders", 0);
289289
for(s32 i = 0, size = json_array_size(folders); i < size; i++)
290290
{
@@ -340,7 +340,7 @@ void fs_enum(const char* path, fs_list_callback callback, void* data)
340340
return;
341341
}
342342

343-
static char path2[TICNAME_MAX];
343+
static string path2;
344344
strncpy(path2, path, sizeof path2);
345345

346346
if (path2[strlen(path2) - 1] == '/') // one character
@@ -422,7 +422,7 @@ void tic_fs_enum(tic_fs* fs, fs_list_callback onItem, fs_done_callback onDone, v
422422
#if defined(BUILD_EDITORS)
423423
if(isPublic(fs))
424424
{
425-
char request[TICNAME_MAX];
425+
string request;
426426
snprintf(request, sizeof request, "%s/index.json", fs->work + STRLEN(TIC_HOST));
427427

428428
NetDirData netDirData = { onItem, onDone, data };
@@ -512,15 +512,18 @@ void tic_fs_dir(tic_fs* fs, char* dir)
512512

513513
void tic_fs_changedir(tic_fs* fs, const char* dir)
514514
{
515-
char temp[TICNAME_MAX+1];
515+
string temp;
516516

517-
if (strlen(fs->work) > 0) {
518-
snprintf(temp, TICNAME_MAX+1, "%s/%s", fs->work, dir);
519-
} else {
520-
snprintf(temp, TICNAME_MAX, "%s", dir);
517+
if (strlen(fs->work) > 0)
518+
{
519+
snprintf(temp, sizeof temp, "%s/%s", fs->work, dir);
520+
}
521+
else
522+
{
523+
snprintf(temp, sizeof temp, "%s", dir);
521524
}
522525

523-
strncpy(fs->work, temp, TICNAME_MAX - 1);
526+
snprintf(fs->work, sizeof fs->work, "%s", temp);
524527

525528
#if defined(__TIC_WINDOWS__)
526529
for(char *ptr = fs->work, *end = ptr + strlen(ptr); ptr < end; ptr++)
@@ -769,7 +772,7 @@ typedef struct
769772
tic_fs* fs;
770773
fs_load_callback done;
771774
void* data;
772-
char cachePath[TICNAME_MAX];
775+
string cachePath;
773776
} LoadFileByHashData;
774777

775778
static void fileByHashLoaded(const net_get_data* netData)
@@ -803,27 +806,22 @@ void tic_fs_hashload(tic_fs* fs, const char* name, const char* url, fs_load_call
803806
#if defined(BUILD_EDITORS)
804807
LoadFileByHashData loadFileByHashData = { fs, callback, data };
805808

806-
const char *hash = strrchr(url, '?');
809+
const char *hash = md5str(url, strlen(url));
810+
snprintf(loadFileByHashData.cachePath, sizeof loadFileByHashData.cachePath, TIC_CACHE "%s.tic", hash);
807811

808-
if(hash)
809812
{
810-
snprintf(loadFileByHashData.cachePath, sizeof loadFileByHashData.cachePath, TIC_CACHE "%s.tic", hash + 1);
811-
813+
s32 size = 0;
814+
void* buffer = tic_fs_loadroot(fs, loadFileByHashData.cachePath, &size);
815+
if (buffer)
812816
{
813-
s32 size = 0;
814-
void* buffer = tic_fs_loadroot(fs, loadFileByHashData.cachePath, &size);
815-
if (buffer)
816-
{
817-
callback(buffer, size, data);
818-
free(buffer);
819-
return;
820-
}
817+
callback(buffer, size, data);
818+
free(buffer);
819+
return;
821820
}
822-
823-
824-
tic_net_get(fs->net, url, fileByHashLoaded, MOVE(loadFileByHashData));
825821
}
826822

823+
tic_net_get(fs->net, url, fileByHashLoaded, MOVE(loadFileByHashData));
824+
827825
#endif
828826

829827
#endif
@@ -967,7 +965,7 @@ tic_fs* tic_fs_create(const char* path, tic_net* net)
967965
{
968966
tic_fs* fs = (tic_fs*)calloc(1, sizeof(tic_fs));
969967

970-
strncpy(fs->dir, path, TICNAME_MAX);
968+
snprintf(fs->dir, sizeof fs->dir, "%s", path);
971969

972970
if(path[strlen(path) - 1] != SEP[0])
973971
strcat(fs->dir, SEP);
@@ -979,11 +977,11 @@ tic_fs* tic_fs_create(const char* path, tic_net* net)
979977

980978
const char* fs_apppath()
981979
{
982-
static char apppath[TICNAME_MAX];
980+
static string apppath;
983981

984982
#if defined(__TIC_WINDOWS__)
985983
{
986-
wchar_t wideAppPath[TICNAME_MAX];
984+
wchar_t wideAppPath[sizeof apppath];
987985
GetModuleFileNameW(NULL, wideAppPath, sizeof wideAppPath);
988986
WideCharToMultiByte(CP_UTF8, 0, wideAppPath, COUNT_OF(wideAppPath), apppath, COUNT_OF(apppath), 0, 0);
989987
}
@@ -1000,7 +998,7 @@ const char* fs_apppath()
1000998

1001999
const char* fs_appfolder()
10021000
{
1003-
static char appfolder[TICNAME_MAX];
1001+
static string appfolder;
10041002
strcpy(appfolder, fs_apppath());
10051003

10061004
char* pos = strrchr(appfolder, SLASH_SYMBOL);

src/studio/screens/surf.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,22 +378,18 @@ static void requestCover(Surf* surf, SurfItem* item)
378378
tic_fs_dir(surf->fs, coverLoadingData.dir);
379379

380380
const char *url = item->url;
381-
const char *hash = strrchr(url, '?');
381+
const char *hash = md5str(item->url, strlen(item->url));
382+
snprintf(coverLoadingData.cachePath, sizeof coverLoadingData.cachePath, TIC_CACHE "%s.png", hash);
382383

383-
if(hash)
384384
{
385-
sprintf(coverLoadingData.cachePath, TIC_CACHE "%s.png", hash + 1);
385+
s32 size = 0;
386+
void* data = tic_fs_loadroot(surf->fs, coverLoadingData.cachePath, &size);
386387

388+
if (data)
387389
{
388-
s32 size = 0;
389-
void* data = tic_fs_loadroot(surf->fs, coverLoadingData.cachePath, &size);
390-
391-
if (data)
392-
{
393-
updateMenuItemCover(surf, surf->menu.pos, data, size);
394-
free(data);
395-
return;
396-
}
390+
updateMenuItemCover(surf, surf->menu.pos, data, size);
391+
free(data);
392+
return;
397393
}
398394
}
399395

@@ -404,6 +400,13 @@ static void requestCover(Surf* surf, SurfItem* item)
404400
if(pos)
405401
{
406402
strcpy(pos, "/cover.png");
403+
404+
const char *hash = strrchr(url, '?');
405+
if(hash)
406+
{
407+
strcat(path, hash);
408+
}
409+
407410
tic_net_get(surf->net, path, coverLoaded, MOVE(coverLoadingData));
408411
}
409412
}

0 commit comments

Comments
 (0)