xrxs/cart.c

121 lines
2.4 KiB
C

#include <u.h>
#include <libc.h>
#include "config.h"
#include "util.h"
#include "user.h"
#include "cart.h"
Cart* create_cart(char* name) {
char path[256] = {0};
char file[256] = {0};
Cart* cart;
Blob* cart_data;
scat(path, DATA_DIR);
scat(path, name);
scpy(path, file, 256);
ccat(file, '/');
scat(file, name);
scat(file, ".rom");
cart_data = read_bytes(file);
if (cart_data != nil) {
cart = (Cart*)malloc(sizeof(Cart));
scpy(name, cart->name, 32);
cart->rom = cart_data;
scpy(path, file, 64);
scat(file, "/data/sprites0");
cart->sprite_data = read_bytes(file);
scpy(path, file, 64);
scat(file, "/data/text0");
cart->txt_data = read_bytes(file);
scpy(path, file, 64);
scat(file, "/data/audio0");
cart->audio_data = read_bytes(file);
return cart;
}
return nil;
}
Cart* find_cart(UserInfo* table, char* name) {
UserInfo* u = table;
int i = 0;
for (i = 0; i < 64; i++) {
if (slen(u->name) > 0 && u->cart != nil && scmp(u->cart->name, name))
return u->cart;
u++;
}
return nil;
}
int get_chunk(UserInfo* table, char* uname, char* chunk) {
UserInfo* u = find_user(table, uname);
char type[8] = {0};
char chunk_id[64] = {0};
char* c = chunk;
Blob* data;
if (u == nil || u->cart == nil)
return 0;
while (*c && *c != ' ') {
ccat(type, *c++);
}
if (*c == ' ')
c++;
scat(chunk_id, "carts/");
scat(chunk_id, u->cart->name);
ccat(chunk_id, '/');
scat(chunk_id, type);
scat(chunk_id, c);
data = read_bytes(chunk_id);
if (data == nil) {
return 0;
}
if (scmp(type, "sprite")) {
u->cart->sprite_data = data;
} else if (scmp(type, "audio")) {
u->cart->audio_data = data;
} else {
u->cart->txt_data = data;
}
return 1;
}
uint count_carts(UserInfo* table, char* name) {
UserInfo* u = table;
uint i, j;
j = 0;
for (i = 0; i < 64; i++) {
if (slen(u->name) > 0 && u->cart != nil && scmp(u->cart->name, name))
j++;
}
return j;
}
void destroy_cart(Cart* self) {
if (self != nil) {
free(self->rom->data);
free(self->rom);
if (self->sprite_data != nil) {
free(self->sprite_data->data);
free(self->sprite_data);
}
if (self->audio_data != nil) {
free(self->audio_data->data);
free(self->audio_data);
}
if (self->txt_data != nil) {
free(self->txt_data->data);
free(self->txt_data);
}
free(self);
self = nil;
}
}