Skip to content

Commit

Permalink
More work on mod hashing/caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MysterD committed Apr 13, 2022
1 parent 97a9360 commit c5c11a5
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 85 deletions.
12 changes: 6 additions & 6 deletions developer/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ if [ ! -f "$FILE" ]; then
fi

# no debug, direct
$FILE --server 27015 --configfile sm64config_server.txt &
$FILE --server 7777 --configfile sm64config_server.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt &
$FILE --client 127.0.0.1 7777 --configfile sm64config_client.txt &
exit

# no debug, discord
Expand All @@ -27,11 +27,11 @@ exit
#exit

# debug on server
#$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt & > /dev/null
#$WINPTY cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --server 27015 --configfile sm64config_server.txt' -ex 'quit'
#$FILE --client 127.0.0.1 7777 --configfile sm64config_client.txt & > /dev/null
#$WINPTY cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --server 7777 --configfile sm64config_server.txt' -ex 'quit'
#exit

# debug on client
$FILE --server 27015 --configfile sm64config_server.txt & > /dev/null
$WINPTY cgdb $FILE -ex 'network_receive_download' -ex 'break debug_breakpoint_here' -ex 'run --client 127.0.0.1 27015 --configfile sm64config_client.txt' -ex 'quit'
$FILE --server 7777 --configfile sm64config_server.txt & > /dev/null
$WINPTY cgdb $FILE -ex 'network_receive_download' -ex 'break debug_breakpoint_here' -ex 'run --client 127.0.0.1 7777 --configfile sm64config_client.txt' -ex 'quit'
exit
6 changes: 3 additions & 3 deletions src/pc/mods/mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ bool mod_load(struct Mods* mods, char* basePath, char* modName) {
struct ModCacheEntry* cache = mod_cache_get_from_path(fullPath);
if (cache == NULL) {
mod_md5_hash(mod);
mod_cache_add(mod->dataHash, strdup(fullPath));
mod_cache_add(mod->dataHash, 0, strdup(fullPath));
}

return true;
Expand Down Expand Up @@ -572,13 +572,13 @@ void mod_md5_hash(struct Mod* mod) {
mod->hashProcessed = true;

if (mod->isDirectory) {
mod_cache_add(mod->dataHash, strdup(mod->basePath));
mod_cache_add(mod->dataHash, 0, strdup(mod->basePath));
} else {
if (!concat_path(path, mod->basePath, mod->files[0].relativePath)) {
LOG_ERROR("Failed to combine path for mod hashing.");
return;
}
mod_cache_add(mod->dataHash, strdup(path));
mod_cache_add(mod->dataHash, 0, strdup(path));
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/pc/mods/mod_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "pc/debuglog.h"

#define MOD_CACHE_FILENAME "mod.cache"
#define MOD_CACHE_VERSION 1
#define MOD_CACHE_VERSION 2

struct ModCacheEntry* sModCacheHead = NULL;

Expand Down Expand Up @@ -49,13 +49,15 @@ struct ModCacheEntry* mod_cache_get_from_path(const char* path) {
return NULL;
}

void mod_cache_add(u8* dataHash, const char* path) {
void mod_cache_add(u8* dataHash, u64 lastLoaded, const char* path) {
if (mod_cache_get_from_hash(dataHash)) {
return;
}

struct ModCacheEntry* node = calloc(1, sizeof(struct ModCacheEntry));
memcpy(node->dataHash, dataHash, 16);
if (lastLoaded == 0) { lastLoaded = clock(); }
node->lastLoaded = lastLoaded;
node->path = (char*)path;
node->next = NULL;

Expand Down Expand Up @@ -107,17 +109,20 @@ void mod_cache_load(void) {

while (true) {
u8 dataHash[16] = { 0 };
u64 lastLoaded = 0;
u16 pathLen;

if (fread(dataHash, sizeof(u8), 16, fp) == 0) {
break;
}

fread(&lastLoaded, sizeof(u64), 1, fp);
fread(&pathLen, sizeof(u16), 1, fp);

const char* path = calloc(pathLen + 1, sizeof(u8));
fread((char*)path, sizeof(u8), pathLen + 1, fp);

mod_cache_add(dataHash, path);
mod_cache_add(dataHash, lastLoaded, path);
}
LOG_INFO("Loading mod cache complete");

Expand All @@ -138,6 +143,7 @@ void mod_cache_save(void) {
struct ModCacheEntry* node = sModCacheHead;
while (node != NULL) {
fwrite(node->dataHash, sizeof(u8), 16, fp);
fwrite(node->lastLoaded, sizeof(u64), 1, fp);
u16 pathLen = strlen(node->path);
fwrite(&pathLen, sizeof(u16), 1, fp);
fwrite(node->path, sizeof(u8), pathLen + 1, fp);
Expand Down
3 changes: 2 additions & 1 deletion src/pc/mods/mod_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

struct ModCacheEntry {
u8 dataHash[16];
u64 lastLoaded;
char* path;
struct ModCacheEntry* next;
};

void mod_cache_shutdown(void);
struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash);
struct ModCacheEntry* mod_cache_get_from_path(const char* path);
void mod_cache_add(u8* dataHash, const char* path);
void mod_cache_add(u8* dataHash, u64 lastLoaded, const char* path);
void mod_cache_load(void);
void mod_cache_save(void);

Expand Down
2 changes: 1 addition & 1 deletion src/pc/mods/mods.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void mods_activate(struct Mods* mods) {
}

// copy enabled entries
gActiveMods.entryCount = 0;
gActiveMods.size = 0;
for (int i = 0; i < mods->entryCount; i++) {
struct Mod* mod = mods->entries[i];
Expand Down Expand Up @@ -213,5 +214,4 @@ void mods_shutdown(void) {
mods_clear(&gRemoteMods);
mods_clear(&gActiveMods);
mods_clear(&gLocalMods);
mods_delete_tmp();
}
14 changes: 0 additions & 14 deletions src/pc/mods/mods_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,6 @@ static void mods_delete_folder(char* path) {
rmdir(path);
}

void mods_delete_tmp(void) {
// ensure tmpPath exists
char tmpPath[SYS_MAX_PATH] = { 0 };
if (snprintf(tmpPath, SYS_MAX_PATH - 1, "%s", fs_get_write_path(TMP_DIRECTORY)) < 0) {
LOG_ERROR("Failed to concat tmp path");
return;
}

// sanity
if (strlen(tmpPath) < 1) { return; }

// delete
mods_delete_folder(tmpPath);
}
//////////////////////////////////////////////////////////////////////////////////////////

bool mod_file_full_path(char* destination, struct Mod* mod, struct ModFile* modFile) {
Expand Down
1 change: 0 additions & 1 deletion src/pc/mods/mods_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

void mods_size_enforce(struct Mods* mods);
void mods_update_selectable(void);
void mods_delete_tmp(void);

bool mod_file_full_path(char* destination, struct Mod* mod, struct ModFile* modFile);
bool mod_file_create_directories(struct Mod* mod, struct ModFile* modFile);
Expand Down
3 changes: 3 additions & 0 deletions src/pc/network/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ bool network_allow_unknown_local_index(enum PacketType packetType) {
|| (packetType == PACKET_ACK)
|| (packetType == PACKET_MOD_LIST_REQUEST)
|| (packetType == PACKET_MOD_LIST)
|| (packetType == PACKET_MOD_LIST_ENTRY)
|| (packetType == PACKET_MOD_LIST_FILE)
|| (packetType == PACKET_MOD_LIST_DONE)
|| (packetType == PACKET_DOWNLOAD_REQUEST)
|| (packetType == PACKET_DOWNLOAD)
|| (packetType == PACKET_KEEP_ALIVE)
Expand Down
3 changes: 3 additions & 0 deletions src/pc/network/packets/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ void packet_process(struct Packet* p) {
case PACKET_MOD_LIST: network_receive_mod_list(p); break;
case PACKET_DOWNLOAD_REQUEST: network_receive_download_request(p); break;
case PACKET_DOWNLOAD: network_receive_download(p); break;
case PACKET_MOD_LIST_ENTRY: network_receive_mod_list_entry(p); break;
case PACKET_MOD_LIST_FILE: network_receive_mod_list_file(p); break;
case PACKET_MOD_LIST_DONE: network_receive_mod_list_done(p); break;

case PACKET_LUA_SYNC_TABLE_REQUEST: network_receive_lua_sync_table_request(p); break;
case PACKET_LUA_SYNC_TABLE: network_receive_lua_sync_table(p); break;
Expand Down
6 changes: 6 additions & 0 deletions src/pc/network/packets/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ enum PacketType {
PACKET_MOD_LIST,
PACKET_DOWNLOAD_REQUEST,
PACKET_DOWNLOAD,
PACKET_MOD_LIST_ENTRY,
PACKET_MOD_LIST_FILE,
PACKET_MOD_LIST_DONE,

PACKET_LUA_SYNC_TABLE_REQUEST,
PACKET_LUA_SYNC_TABLE,
Expand Down Expand Up @@ -327,6 +330,9 @@ void network_send_mod_list_request(void);
void network_receive_mod_list_request(UNUSED struct Packet* p);
void network_send_mod_list(void);
void network_receive_mod_list(struct Packet* p);
void network_receive_mod_list_entry(struct Packet* p);
void network_receive_mod_list_file(struct Packet* p);
void network_receive_mod_list_done(struct Packet* p);

// packet_download.c
void network_start_download_requests(void);
Expand Down
2 changes: 1 addition & 1 deletion src/pc/network/packets/packet_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void mark_groups_loaded_from_hash(void) {
if (mod->loadedFromCache) {
// if we loaded from cache, mark bytes as downloaded
sTotalDownloadBytes += mod->size;
LOG_INFO("Loaded from cache: %s, %lu", mod->name, mod->size);
LOG_INFO("Loaded from cache: %s, %llu", mod->name, mod->size);
} else {
// if we haven't loaded from cache, we need this offset group
u64 ogIndexStart = fileStartOffset / GROUP_SIZE;
Expand Down
Loading

0 comments on commit c5c11a5

Please sign in to comment.