]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_vrs.c
16_vrs now operates on files in far memory
[16.git] / src / lib / 16_vrs.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
3  *
4  * This file is part of Project 16.
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22 #include "src/lib/16_vrs.h"
23 #include "src/lib/typedefst.h"
24
25 extern global_game_variables_t gvar;
26
27 // Read .vrs file into far memory
28 int read_vrs(char *filename, struct vrs_container *vrs_cont){
29         int fd;
30         dword size;
31         byte huge *buffer;
32         // Open filename, get size of file,
33         // populate the vrs_container if all tests pass
34         fd = open(filename, O_RDONLY|O_BINARY);
35         size = filelength(fd);
36         close(fd);
37         // Insert sanity cheks later
38         CA_LoadFile(filename, buffer, gvar->mm, gvar->mmi);
39         vrs_cont->size = size;
40         vrs_cont->buffer = buffer;
41         return 0;
42 }
43
44 // Seek and return a specified .vrl blob from .vrs blob in far memory
45 struct vrl_container get_vrl_by_id(struct vrs_container huge *vrs_cont, uint16_t id){
46         uint16_t huge *ids;
47         uint32_t huge *vrl_list;
48         struct vrl_container huge *vrl_cont;
49         int counter = 0;
50         // If id is invalid, return null
51         if(id == 0){
52                 // Probably add an error message?
53                 return 0;
54         }
55         // Get id list from .vrs blob (base + offset)
56         ids = (uint16_t huge*)vrs_cont->buffer + 
57                 (dword)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST];
58         // Loop through the id list until we found the right one or hit the end of the list
59         // Counter is keeping track of the offset(in ids/vrl blobs)
60         while(ids[counter] != id && ids[counter]){
61                 counter++;
62         }
63         // Return null if we couldn't find the requested id
64         if(!ids[counter]){
65                 // Error message?
66                 return 0;
67         }
68         // Get vrl list from .vrs blob (base + offset)
69         vrl_list = (uint32_t huge *)(vrs_cont->buffer + 
70                                         vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
71         // Allocate memory for vrl_cont
72         vrl_cont = (struct vrl_container)malloc(sizeof(struct vrl_container));
73         // Get vrl_header from .vrs (base + offset from vrl_list)
74         // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
75         vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_list[counter]);
76         // Get .vrl size by integer arithmetics (next vrl - current vrl)
77         // Untested. May be an incorrect way to do so
78         vrl_cont->size = vrl_list[counter+1] - vrl_list[counter];
79         return vrl_cont;
80 }