]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_vrs.c
Documented 16_vrs files
[16.git] / src / lib / 16_vrs.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <ctype.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "16_vrs.h"
9
10 // Read .vrs file into memory 
11 int read_vrs(char *filename, struct vrs_container *vrs_cont){
12         // Initialise a local copy of becessary variables
13         // so vrs_cont won't be dirty on error
14         int fd;
15         unsigned long size;
16         unsigned char *buffer;
17         // Open filename, get size of file,
18         // populate the vrs_container if all tests pass
19         fd = open(filename, O_RDONLY|O_BINARY);
20         // Insert sanity cheks later
21         size = lseek(fd, 0, SEEK_END);
22         buffer = malloc(size);
23         lseek(fd, 0, SEEK_SET);
24         read(fd, buffer, size);
25         vrs_cont->size = size;
26         vrs_cont->buffer = buffer;
27         // 0 is an invalid value for ids under vrs specifications,
28         // so it is safe to flush ids to this value
29         vrs_cont->anchor_sprite_id = 0;
30         vrs_cont->current_sprite_id = 0;
31         return 0;
32 }
33
34 // Seek and return a specified .vrl blob from .vrs blob in memory 
35 struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id){
36         uint16_t *ids; 
37         uint32_t *vrl_list;
38         struct vrl_container *vrl_cont;
39         int counter = 0;
40         // If id is invalid, return null
41         if(id == 0){
42                 // Probably add an error message?
43                 return 0;
44         }
45         // Get id list from .vrs blob (base + offset)
46         ids = (uint16_t*)vrs_cont->buffer + (unsigned long)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST];
47         // Loop through the id list until we found the right one or hit the end of the list
48         // Counter is keeping track of the offset(in ids/vrl blobs)
49         while(ids[counter] != id && ids[counter]){
50                 counter++;
51         }
52         // Return null if we couldn't find the requested id
53         if(!ids[counter]){
54                 // Error message?
55                 return 0;
56         }
57         // Get vrl list from .vrs blob (base + offset)
58         vrl_list = (uint32_t *)(vrs_cont->buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
59         // Allocate memory for vrl_cont
60         vrl_cont = (struct vrl_container*)malloc(sizeof(struct vrl_container));
61         // Get vrl_header from .vrs (base + offset from vrl_list)
62         // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
63         vrl_cont->vrl_header = (struct vrl1_vgax_header*)(vrs_cont->buffer + vrl_list[counter]);
64         // Get .vrl size by integer arithmetics (next vrl - current vrl)
65         // Untested. May be an incorrect way to do so
66         vrl_cont->size = vrl_list[counter+1] - vrl_list[counter];
67         return vrl_cont;
68 }