]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_vrs.c
==== OK i removed all warnings wwww
[16.git] / src / lib / 16_vrs.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 #include "src/lib/16_vrs.h"\r
23 #include "src/lib/typdefst.h"\r
24 \r
25 // Read .vrs file into far memory\r
26 int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont){\r
27         int fd;\r
28         dword size;\r
29         byte huge *buffer;\r
30         vrl1_vgax_offset_t **vrl_line_offsets;\r
31         uint32_t huge *vrl_headers_offsets;\r
32         uint16_t huge *vrl_id_iter;\r
33         uint32_t vrl_size;\r
34         int num_of_vrl=0, i;\r
35         struct vrl1_vgax_header huge *curr_vrl;\r
36         int success=1;\r
37 \r
38         // Open filename, get size of file,\r
39         // populate the vrs_container if all tests pass\r
40         fd = open(filename, O_RDONLY|O_BINARY);\r
41         // Insert sanity cheks later\r
42         size = lseek(fd, 0, SEEK_END);\r
43         buffer = malloc(size);\r
44         lseek(fd, 0, SEEK_SET);\r
45         read(fd, buffer, size);\r
46         close(fd);\r
47         if(!success)\r
48         {\r
49                 fprintf(stderr, "Unablee to load file");\r
50                 exit(3);\r
51         }\r
52         vrs_cont->data_size = size - sizeof(struct vrs_header);\r
53         vrs_cont->buffer = buffer;\r
54 \r
55         // Calculate vrl offsets\r
56 \r
57         // Count sprites\r
58         vrl_id_iter = (uint16_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
59         while(vrl_id_iter[num_of_vrl]){\r
60                 num_of_vrl++;\r
61         }\r
62         // Allocate memory for vrl line offsets table\r
63         vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);\r
64 \r
65         vrl_headers_offsets = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
66         // Calculate line offsets for each vrl\r
67         for(i = 0; i < num_of_vrl; i++){\r
68                 curr_vrl = (struct vrl1_vgax_header huge *)(buffer + vrl_headers_offsets[i]);\r
69 \r
70                 // Calc. vrl size as (next_offset - curr_offset)\r
71                 if (i != num_of_vrl - 1){\r
72                         vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);\r
73                 }\r
74                 // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)\r
75                 else{\r
76                         vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);\r
77                 }\r
78                 vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);\r
79         }\r
80         vrs_cont->vrl_line_offsets = vrl_line_offsets;\r
81         return 0;\r
82 }\r
83 \r
84 // Seek and return a specified .vrl blob from .vrs blob in far memory\r
85 int get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){\r
86         uint16_t huge *ids;\r
87         uint32_t huge *vrl_offs_list;\r
88         int counter = 0;\r
89 \r
90         // If id is invalid, return -1\r
91         if(id == 0){\r
92                 // Probably add an error message?\r
93                 return -1;\r
94         }\r
95 \r
96         // Get id list from .vrs blob (base + offset)\r
97         ids = (uint16_t huge*)(vrs_cont->buffer +\r
98                 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
99 \r
100         // Loop through the id list until we found the right one or hit the end of the list\r
101         // Counter is keeping track of the offset(in ids/vrl blobs)\r
102         while(ids[counter] != id && ids[counter]){\r
103                 counter++;\r
104         }\r
105         // Return -2 if we couldn't find the requested id\r
106         if(!ids[counter]){\r
107                 // Error message?\r
108                 return -2;\r
109         }\r
110 \r
111         // Get vrl offsets list from .vrs blob (base + offset)\r
112         vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer +\r
113                                         vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
114 \r
115         // Get vrl_header from .vrs (base + offset from vrl_list)\r
116         // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)\r
117         vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]);\r
118 \r
119         // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)\r
120         if(ids[counter+1]){\r
121                 vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);\r
122         }\r
123         // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0\r
124         else{\r
125                 vrl_cont->data_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);\r
126         }\r
127 \r
128         // Retrive line offsets form .vrs\r
129         vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];\r
130 \r
131         return 0;\r
132 }\r