1 /* Project 16 Source Code~
2 * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
4 * This file is part of Project 16.
6 * Project 16 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Project 16 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
19 * Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "src/lib/16_vrs.h"
24 // Read .vrs file into memory
25 int read_vrs(char *filename, struct vrs_container *vrs_cont){
26 // Initialise a local copy of becessary variables
27 // so vrs_cont won't be dirty on error
31 // Open filename, get size of file,
32 // populate the vrs_container if all tests pass
33 fd = open(filename, O_RDONLY|O_BINARY);
34 // Insert sanity cheks later
35 size = lseek(fd, 0, SEEK_END);
36 buffer = malloc(size);
37 lseek(fd, 0, SEEK_SET);
38 read(fd, buffer, size);
39 vrs_cont->size = size;
40 vrs_cont->buffer = buffer;
41 // 0 is an invalid value for ids under vrs specifications,
42 // so it is safe to flush ids to this value
43 vrs_cont->anchor_sprite_id = 0;
44 vrs_cont->current_sprite_id = 0;
48 // Seek and return a specified .vrl blob from .vrs blob in memory
49 struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id){
52 struct vrl_container *vrl_cont;
54 // If id is invalid, return null
56 // Probably add an error message?
59 // Get id list from .vrs blob (base + offset)
60 ids = (uint16_t*)vrs_cont->buffer + (dword)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST];
61 // Loop through the id list until we found the right one or hit the end of the list
62 // Counter is keeping track of the offset(in ids/vrl blobs)
63 while(ids[counter] != id && ids[counter]){
66 // Return null if we couldn't find the requested id
71 // Get vrl list from .vrs blob (base + offset)
72 vrl_list = (uint32_t *)(vrs_cont->buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
73 // Allocate memory for vrl_cont
74 vrl_cont = (struct vrl_container*)malloc(sizeof(struct vrl_container));
75 // Get vrl_header from .vrs (base + offset from vrl_list)
76 // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
77 vrl_cont->vrl_header = (struct vrl1_vgax_header*)(vrs_cont->buffer + vrl_list[counter]);
78 // Get .vrl size by integer arithmetics (next vrl - current vrl)
79 // Untested. May be an incorrect way to do so
80 vrl_cont->size = vrl_list[counter+1] - vrl_list[counter];