1 /*
2 * Seven Kingdoms: Ancient Adversaries
3 *
4 * Copyright 1997,1998 Enlight Software Ltd.
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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/>.
18 *
19 */
20
21 //Filename : ORESDB.CPP
22 //Description : Resource library with database index reading object
23
24 #include <string.h>
25 #include <stdint.h>
26 #include <OSYS.h>
27 #include <ODB.h>
28 #include <ORESDB.h>
29
30 //-------------------------------------------------------//
31 //
32 // Resource library reading object
33 //
34 // Files required :
35 //
36 // - A resource file build by LIBDB.EXE, it is always in .RES extension
37 // - A database file with Index Pointer field, the values of the field
38 // are automatically calculated by LIBDB.EXE
39 //
40 //-------------------------------------------------------//
41
42 //---------- Begin of function ResourceDb::deinit ---------//
43 //
deinit()44 void ResourceDb::deinit()
45 {
46 if( init_flag )
47 {
48 if( !use_common_buf && data_buf )
49 {
50 mem_del(data_buf);
51 data_buf = NULL;
52 }
53
54 if( !read_all )
55 file_close();
56
57 init_flag = false;
58 }
59 }
60 //----------- End of function ResourceDb::deinit ----------//
61
62 //---------- Begin of function ResourceDb::init_imported ----------//
63 //
64 // If the whole database has been read into memory, then only no need to
65 // tell ResourceDb the database name and the index offset
66 //
67 // <char*> resName = name of the resource file (e.g. "GIF.RES")
68 // <int> readAll = whether read all data into the buffer or read one each time
69 // [int] useCommonBuf = whether use the vga common buffer to store the data or not
70 // (default:0)
71 //
init_imported(const char * resName,int cacheWholeFile,int useCommonBuf)72 void ResourceDb::init_imported(const char* resName, int cacheWholeFile, int useCommonBuf)
73 {
74 deinit();
75
76 read_all = cacheWholeFile;
77
78 file_open( resName );
79
80 if( read_all )
81 {
82 data_buf_size = file_size();
83
84 data_buf = mem_add( data_buf_size );
85 file_read( data_buf, data_buf_size );
86 file_close();
87
88 use_common_buf = 0; // don't use vga buffer if read all
89 }
90 else
91 {
92 use_common_buf = useCommonBuf;
93
94 if( use_common_buf )
95 data_buf = sys.common_data_buf;
96 else
97 data_buf = NULL;
98 }
99
100 init_flag = true;
101 }
102 //----------- End of function ResourceDb::init_imported -------------//
103
104
105
106 //---------- Begin of function ResourceDb::read_imported ----------//
107 //
108 // If ResourceDb is initialized using init_imported(),
109 // then use read_imported to read the record
110 //
111 // <long> offset = offset to the data in the resource file
112 //
113 // Return : <char*> data pointer
114 // NULL if the record has not index to data
115 //
read_imported(long offset)116 char* ResourceDb::read_imported(long offset)
117 {
118 err_when(!init_flag);
119
120 //-------- read from buffer ---------//
121 // ##### begin Gilbert 4/10 #######//
122 if( read_all )
123 {
124 err_when(offset < 0 || offset >= data_buf_size);
125 return data_buf + offset + sizeof(int32_t); // bypass the long parameters which is the size of the data
126 }
127 // ##### end Gilbert 4/10 #######//
128
129 //---------- read from file ---------//
130
131 // ##### begin Gilbert 2/10 ######//
132 err_when(offset >= file_size());
133 // ##### end Gilbert 2/10 ######//
134 file_seek( offset );
135
136 data_buf_size = file_get_long();
137
138 err_when(use_common_buf && data_buf_size > COMMON_DATA_BUF_SIZE);
139
140 if( !use_common_buf )
141 data_buf = mem_resize( data_buf, data_buf_size );
142
143 file_read( data_buf, data_buf_size );
144
145 return data_buf;
146 }
147 //----------- End of function ResourceDb::read_imported -------------//
148