Skip to content

Commit

Permalink
Allow users to override read fucntions
Browse files Browse the repository at this point in the history
  • Loading branch information
dulsi committed Nov 4, 2020
1 parent 85992db commit 41dd452
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
82 changes: 66 additions & 16 deletions gamerzilla.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ static FILE *logFile = NULL;
static GamerzillaAccessGame accessCallback = NULL;
static void *accessData = NULL;

size_t fileSize(const char *filename)
{
struct stat statbuf;
if (0 == stat(filename, &statbuf))
{
return statbuf.st_size;
}
return 0;
}

void *fileOpen(const char *filename)
{
FILE *f = fopen(filename, "rb");
return f;
}

size_t fileRead(void *fd, void *buf, size_t count)
{
return fread(buf, 1, count, (FILE *)fd);
}

void fileClose(void *fd)
{
fclose((FILE *)fd);
}

static GamerzillaSize game_size = &fileSize;
static GamerzillaOpen game_open = &fileOpen;
static GamerzillaRead game_read = &fileRead;
static GamerzillaClose game_close = &fileClose;

typedef struct {
size_t size;
size_t len;
Expand Down Expand Up @@ -190,6 +221,11 @@ static size_t curlWriteFile(void *ptr, size_t size, size_t nmemb, void *fd)
return written;
}

size_t jsonRead(void *buffer, size_t buflen, void *data)
{
return (*game_read)(data, buffer, buflen);
}

static void gamerzillaClear(Gamerzilla *g, bool memFree)
{
if (memFree)
Expand Down Expand Up @@ -970,34 +1006,37 @@ static void GamerzillaSetGameInfo_internal(CURL *c, Gamerzilla *g)
uint8_t cmd = CMD_SETGAMEIMAGE;
uint32_t hostsz = strlen(g->short_name);
uint32_t sz = htonl(hostsz);
struct stat statbuf[2];
size_t st_size[2];
st_size[0] = (*game_size)(g->image);
char data[1024];
if (0 == stat(g->image, &statbuf[0]))
if (0 != st_size[0])
{
writesocket(server_socket, &cmd, sizeof(cmd));
writesocket(server_socket, &sz, sizeof(sz));
writesocket(server_socket, g->short_name, hostsz);
hostsz = statbuf[0].st_size;
hostsz = st_size[0];
sz = htonl(hostsz);
writesocket(server_socket, &sz, sizeof(sz));
FILE *f = fopen(g->image, "rb");
void *f = (*game_open)(g->image);
while (hostsz > 0)
{
size_t ct = fread(data, 1, 1024, f);
size_t ct = (*game_read)(f, data, 1024);
if (ct > 0)
{
hostsz -= ct;
writesocket(server_socket, data, ct);
}
}
fclose(f);
(*game_close)(f);
}
for (int i = 0; i < g->numTrophy; i++)
{
cmd = CMD_SETTROPHYIMAGE;
hostsz = strlen(g->short_name);
sz = htonl(hostsz);
if ((0 == stat(g->trophy[i].true_image, &statbuf[0])) && (0 == stat(g->trophy[i].false_image, &statbuf[1])))
st_size[0] = (*game_size)(g->trophy[i].true_image);
st_size[1] = (*game_size)(g->trophy[i].false_image);
if ((0 != st_size[0]) && (0 != st_size[1]))
{
writesocket(server_socket, &cmd, sizeof(cmd));
writesocket(server_socket, &sz, sizeof(sz));
Expand All @@ -1006,34 +1045,34 @@ static void GamerzillaSetGameInfo_internal(CURL *c, Gamerzilla *g)
sz = htonl(hostsz);
writesocket(server_socket, &sz, sizeof(sz));
writesocket(server_socket, g->trophy[i].name, hostsz);
hostsz = statbuf[0].st_size;
hostsz = st_size[0];
sz = htonl(hostsz);
writesocket(server_socket, &sz, sizeof(sz));
FILE *f = fopen(g->trophy[i].true_image, "rb");
void *f = (*game_open)(g->trophy[i].true_image);
while (hostsz > 0)
{
size_t ct = fread(data, 1, 1024, f);
size_t ct = (*game_read)(f, data, 1024);
if (ct > 0)
{
hostsz -= ct;
writesocket(server_socket, data, ct);
}
}
fclose(f);
hostsz = statbuf[1].st_size;
(*game_close)(f);
hostsz = st_size[1];
sz = htonl(hostsz);
writesocket(server_socket, &sz, sizeof(sz));
f = fopen(g->trophy[i].false_image, "rb");
f = (*game_open)(g->trophy[i].false_image);
while (hostsz > 0)
{
size_t ct = fread(data, 1, 1024, f);
size_t ct = (*game_read)(f, data, 1024);
if (ct > 0)
{
hostsz -= ct;
writesocket(server_socket, data, ct);
}
}
fclose(f);
(*game_close)(f);
}
}
}
Expand Down Expand Up @@ -1288,7 +1327,10 @@ int GamerzillaSetGameFromFile(const char *filename, const char *datadir)
{
GamerzillaInitGame(&current);
json_error_t error;
json_t *root = json_load_file(filename, 0, &error);
json_t *root = NULL;
void *fd = (*game_open)(filename);
root = json_load_callback(&jsonRead, fd, 0, &error);
(*game_close)(fd);
if (root)
{
GamerzillaMerge(curl[0], &current, &currentData, root);
Expand Down Expand Up @@ -2110,6 +2152,14 @@ void GamerzillaQuit()
}
}

void GamerzillaSetRead(GamerzillaSize gameSize, GamerzillaOpen gameOpen, GamerzillaRead gameRead, GamerzillaClose gameClose)
{
game_size = gameSize;
game_open = gameOpen;
game_read = gameRead;
game_close = gameClose;
}

void GamerzillaSetLog(int level, FILE *f)
{
logLevel = level;
Expand Down
7 changes: 7 additions & 0 deletions gamerzilla.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ typedef struct

typedef void (*GamerzillaAccessGame)(const char *short_name, const char *name, void *user_data);

typedef size_t (*GamerzillaSize)(const char *filename);
typedef void *(*GamerzillaOpen)(const char *filename);
typedef size_t (*GamerzillaRead)(void *fd, void *buf, size_t count);
typedef void (*GamerzillaClose)(void *fd);

extern bool GamerzillaStart(bool server, const char *savedir);
extern bool GamerzillaConnect(const char *url, const char *username, const char *password);
extern void GamerzillaInitGame(Gamerzilla *g);
Expand All @@ -56,6 +61,8 @@ extern void GamerzillaServerProcess(struct timeval *timeout);
extern void GamerzillaServerListen(GamerzillaAccessGame callback, void *user_data);
extern void GamerzillaQuit();

extern void GamerzillaSetRead(GamerzillaSize gameSize, GamerzillaOpen gameOpen, GamerzillaRead gameRead, GamerzillaClose gameClose);

extern void GamerzillaSetLog(int level, FILE *f);

#ifdef __cplusplus
Expand Down

0 comments on commit 41dd452

Please sign in to comment.