cart.c (raw)
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include <u.h> #include <libc.h> #include "util.h" #include "user.h" #include "cart.h" Cart* create_cart(char* name) { char path[64] = {0}; char file[64] = {0}; Cart* cart; Blob* cart_data; scat(path, "carts/"); scat(path, name); scpy(path, file, 64); 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; } } |