aboutsummaryrefslogtreecommitdiff
path: root/sources/TZR_ReloadResource.c
blob: dda5b5812a09b1aed5f2dee2e41d2f03d511940a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "TZR_resource.h"
#include "drain.h"
#include <errno.h>
#include <sys/stat.h>

int
TZR_ReloadResource(TZR_Uint id)
{
	TZR_Resource *const res = TZR_GetResourcePointer(id);
	if (res->path == NULL) {
		TZR_Log("resource path of %u is NULL", id);
		return -1;
	}
	FILE *const fp = fopen(res->path, "rb");
	if (fp == NULL) {
		TZR_Log("%s: %s", res->path, strerror(errno));
		return -1;
	}
	size_t size;
	char *const data = drain(fp, &size);
	fclose(fp);
	if (data == NULL)
		return -1;
	TZR_Resource new_res;
	memcpy(&new_res, res, sizeof(TZR_Resource));
	const int load_rv = TZR_DirectResourceLoad(&new_res, data, size);
	free(data);
	if (load_rv)
		return -1;
	TZR_DestroyResource(res, 0);
	memcpy(res, &new_res, sizeof(TZR_Resource));
	return 0;
}