Skip to content

Commit 8e7ac73

Browse files
committed
Move all io and memory stuff to a new submodule called libs3util
1 parent 90d0375 commit 8e7ac73

20 files changed

+657
-1446
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libs3util"]
2+
path = libs3util
3+
url = ../libs3util.git

CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.2.2)
22
project(libs3dat)
33

4+
add_subdirectory(libs3util)
5+
46
include(CPack)
57
include(CTest)
68
include(TestBigEndian)
@@ -41,12 +43,14 @@ endif()
4143

4244
configure_file(config.h.in ${PROJECT_BINARY_DIR}/config.h)
4345

44-
include_directories(src ${PROJECT_BINARY_DIR})
46+
include_directories(src ${PROJECT_BINARY_DIR} libs3util/src)
47+
4548

4649
#s3dat
4750
file(GLOB_RECURSE s3dat_SOURCES "src/*.[ch]")
4851

4952
add_library(s3dat SHARED ${s3dat_SOURCES})
53+
target_link_libraries(s3dat s3util)
5054

5155
add_executable(cli cli.c)
5256
target_link_libraries(cli s3dat)

cli.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int main() {
1616
void* monitor_handle = s3dat_get_default_ioset(S3DAT_IOSET_LIBC)->open_func(MONITOR_MEMORY, true);
1717
#endif
1818

19-
s3dat_exception_t* ex = NULL;
19+
s3util_exception_t* ex = NULL;
2020

2121
DIR* gfx_dir = opendir("GFX");
2222
DIR* snd_dir = opendir("SND");
@@ -55,7 +55,7 @@ int main() {
5555
s3dat_readfile_name(handle, name, &ex);
5656

5757
printf("[%i] new file %s\n", i, name);
58-
if(s3dat_catch_exception(&ex)) {
58+
if(s3util_catch_exception(&ex)) {
5959
if(s3dat_indexlen(handle, s3dat_snd) == 0) {
6060
printf("[%i] %hu settler sequences\n", i, s3dat_indexlen(handle, s3dat_settler));
6161
printf("[%i] %hu shadow sequences\n", i, s3dat_indexlen(handle, s3dat_shadow));
@@ -71,7 +71,7 @@ int main() {
7171
}
7272

7373
s3dat_add_utf8_encoding(handle, &ex);
74-
s3dat_catch_exception(&ex);
74+
s3util_catch_exception(&ex);
7575

7676
/*uint16_t stringindex_len = s3dat_indexlen(handle, s3dat_string);
7777
if(stringindex_len > 0 && false) {
@@ -126,7 +126,7 @@ int main() {
126126
if(guiindex_len > 0 && false) {
127127
for(uint16_t gui = 0;gui != guiindex_len;gui++) {
128128
s3dat_ref_t* bmp = s3dat_extract_gui(handle, gui, &ex);
129-
if(s3dat_catch_exception(&ex)) {
129+
if(s3util_catch_exception(&ex)) {
130130
printf("%02hx: ", gui);
131131
uint32_t guitype = *s3dat_gui_meta(bmp);
132132
for(int32_t pi = 31;pi >= 0;pi--) {

config.h.in

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#ifndef CONFIG_H
2-
#define CONFIG_H
1+
#ifndef S3DAT_CONFIG_H
2+
#define S3DAT_CONFIG_H
33

44
#cmakedefine USE_ICONV
55
#cmakedefine IS_BE
66
#cmakedefine PRIVATE_FILENAME
77

8-
#endif /*CONFIG_H*/
8+
#endif /*S3DAT_CONFIG_H*/

example_restype.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ void delete_example_res(example_res_t* example_res) {
2828

2929
example_res_t* create_example_res(s3dat_t* handle, s3dat_exception_t** throws) {
3030
// allocate everything through s3dat_alloc_func
31-
example_res_t* eres = s3dat_alloc_func(handle, sizeof(example_res_t), throws);
31+
example_res_t* eres = s3util_alloc_func(s3dat_memset(handle), sizeof(example_res_t), throws);
3232

3333
// it wont do anything, if *throws == null (there is no exception)
34-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
34+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
3535
return eres;
3636
}
3737

invert.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "s3dat_ext.h"
22

3-
void invert_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3dat_exception_t** throws) {
3+
void invert_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws) {
44
s3dat_t* handle = me->parent;
55
S3DAT_EXHANDLER_CALL(me, res, throws, __FILE__, __func__, __LINE__);
66

@@ -14,17 +14,17 @@ void invert_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3dat_exceptio
1414
}
1515
}
1616

17-
s3dat_exception_t* invert_ex;
18-
s3dat_exception_t** throws;
17+
s3util_exception_t* invert_ex;
18+
s3util_exception_t** throws;
1919

2020
int main() {
2121
throws = &invert_ex;
2222

2323
s3dat_t* read = s3dat_new_malloc();
2424
s3dat_t* write = NULL;
25-
25+
2626
s3dat_readfile_name(read, "GFX/Siedler3_10.f8007e01f.dat", throws);
27-
if(!s3dat_catch_exception(throws)) {
27+
if(!s3util_catch_exception(throws)) {
2828
goto end;
2929
}
3030

@@ -38,10 +38,10 @@ int main() {
3838
s3dat_add_extracthandler(read, pack_ex);
3939

4040
write = s3dat_writeable_fork(read, "GFX/Siedler3_10.f8007e01f.dat.invert", throws);
41-
if(!s3dat_catch_exception(throws)) goto end;
41+
if(!s3util_catch_exception(throws)) goto end;
4242

4343
s3dat_writefile(write, throws);
44-
s3dat_catch_exception(throws);
44+
s3util_catch_exception(throws);
4545

4646
end:
4747
s3dat_delete_fork(write);

src/bitmap.c

+50-50
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55

66
uint8_t s3dat_image_header[4] = {12, 0, 0, 0};
77

8-
void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res, s3dat_exception_t** throws) {
8+
void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws) {
99
s3dat_t* handle = me->parent;
1010

1111
uint32_t from = s3dat_internal_seek_to(handle, res, throws);
12-
S3DAT_HANDLE_EXCEPTION(handle, throws, __FILE__, __func__, __LINE__);
12+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
1313

1414
void* header;
1515
uint32_t header_size = 12;
1616
if(res->type == s3dat_gui) header_size = 8;
1717
if(res->type == s3dat_landscape) header_size = 6;
1818

1919

20-
header = s3dat_alloc_func(handle, header_size, throws);
21-
S3DAT_HANDLE_EXCEPTION(handle, throws, __FILE__, __func__, __LINE__);
20+
header = s3util_alloc_func(s3dat_memset(handle), header_size, throws);
21+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
2222

23-
if(!handle->read_func(handle->io_arg, header, header_size)) {
24-
s3dat_free_func(handle, header);
25-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
23+
if(!s3dat_ioset(handle)->read_func(s3dat_ioset(handle)->arg, header, header_size)) {
24+
s3util_free_func(s3dat_memset(handle), header);
25+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
2626
return;
2727
}
2828

2929
if(from % 2 == 1) {
30-
s3dat_internal_read8(handle, throws); // this fill byte can be dropped
31-
S3DAT_HANDLE_EXCEPTION(handle, throws, __FILE__, __func__, __LINE__);
30+
S3DAT_INTERNAL_READ(8, handle, throws); // this fill byte can be dropped
31+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
3232
}
3333

3434
uint16_t* img_meta;
3535

3636
if(res->type != s3dat_gui && res->type != s3dat_landscape) {
3737
if(memcmp(header, s3dat_image_header, 4) != 0) {
38-
s3dat_throw(handle, throws, S3DAT_EXCEPTION_HEADER, __FILE__, __func__, __LINE__);
38+
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_HEADER, __FILE__, __func__, __LINE__);
3939
return;
4040
}
4141

@@ -45,28 +45,28 @@ void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res,
4545
}
4646

4747
if(img_meta[0] == 0 || img_meta[1] == 0) {
48-
s3dat_throw(handle, throws, S3DAT_EXCEPTION_HEADER, __FILE__, __func__, __LINE__);
48+
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_HEADER, __FILE__, __func__, __LINE__);
4949
return;
5050
}
5151

5252
uint32_t pixel_size = 2;
5353
if(res->type == s3dat_shadow) pixel_size = 0;
5454
if(res->type == s3dat_torso) pixel_size = 1;
5555

56-
uint16_t width = s3dat_le16(img_meta[0]);
57-
uint16_t height = s3dat_le16(img_meta[1]);
56+
uint16_t width = s3util_le16(img_meta[0]);
57+
uint16_t height = s3util_le16(img_meta[1]);
5858

5959
uint32_t bfr_size = pixel_size*width*height+header_size;
60-
void* bfr = s3dat_alloc_func(handle, bfr_size, throws);
60+
void* bfr = s3util_alloc_func(s3dat_memset(handle), bfr_size, throws);
6161

6262
if(*throws != NULL) {
63-
s3dat_free_func(handle, header);
64-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
63+
s3util_free_func(s3dat_memset(handle), header);
64+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
6565
return;
6666
}
6767

6868
memcpy(bfr, header, header_size);
69-
s3dat_free_func(handle, header);
69+
s3util_free_func(s3dat_memset(handle), header);
7070

7171
uint32_t read_size = header_size;
7272

@@ -76,26 +76,26 @@ void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res,
7676
bool end = false;
7777

7878
while(!end) {
79-
uint16_t meta = s3dat_internal_read16LE(handle, throws);
79+
uint16_t meta = S3DAT_INTERNAL_READ(16LE, handle, throws);
8080
if(*throws != NULL) {
81-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
81+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
8282
end = true;
8383
} else {
8484
x += (meta & 0xFF);
8585
x += ((meta >> 8) & 0x7F);
8686
uint16_t data_len = ((meta & 0xFF)*pixel_size)+2;
8787

8888
uint8_t data_bfr[data_len];
89-
*((uint16_t*)data_bfr) = s3dat_le16(meta);
90-
if(handle->read_func(handle->io_arg, data_bfr+2, data_len-2)) {
89+
*((uint16_t*)data_bfr) = s3util_le16(meta);
90+
if(s3dat_ioset(handle)->read_func(s3dat_ioset(handle)->arg, data_bfr+2, (size_t)data_len-2)) {
9191
if(read_size+data_len > bfr_size) {
92-
void* bfr2 = s3dat_alloc_func(handle, bfr_size*2, throws);
92+
void* bfr2 = s3util_alloc_func(s3dat_memset(handle), bfr_size*2, throws);
9393
if(*throws != NULL) {
9494
end = true;
95-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
95+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
9696
} else {
9797
memcpy(bfr2, bfr, read_size);
98-
s3dat_free_func(handle, bfr);
98+
s3util_free_func(s3dat_memset(handle), bfr);
9999
bfr = bfr2;
100100
bfr_size *= 2;
101101
}
@@ -106,7 +106,7 @@ void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res,
106106
read_size += data_len;
107107
}
108108
} else {
109-
s3dat_throw(handle, throws, S3DAT_EXCEPTION_IOERROR, __FILE__, __func__, __LINE__);
109+
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_IOERROR, __FILE__, __func__, __LINE__);
110110
end = true;
111111
}
112112
}
@@ -122,29 +122,29 @@ void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res,
122122

123123
if((x > width || y > height) && *throws == NULL) {
124124
end = true;
125-
s3dat_throw(handle, throws, S3DAT_EXCEPTION_OUT_OF_RANGE, __FILE__, __func__, __LINE__);
125+
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_OUT_OF_RANGE, __FILE__, __func__, __LINE__);
126126
}
127127
}
128128

129129
if(*throws != NULL) {
130-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
131-
s3dat_free_func(handle, bfr);
130+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
131+
s3util_free_func(s3dat_memset(handle), bfr);
132132
return;
133133
}
134134

135135
if(bfr_size != read_size) {
136-
void* bfr2 = s3dat_alloc_func(handle, read_size, NULL);
136+
void* bfr2 = s3util_alloc_func(s3dat_memset(handle), read_size, NULL);
137137
if(bfr2) {
138138
memcpy(bfr2, bfr, read_size);
139-
s3dat_free_func(handle, bfr);
139+
s3util_free_func(s3dat_memset(handle), bfr);
140140
bfr = bfr2;
141141
}
142142
}
143143

144144
s3dat_ref_t* pack = s3dat_new_packed(handle, throws);
145145
if(*throws != NULL) {
146-
s3dat_free_func(handle, bfr);
147-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
146+
s3util_free_func(s3dat_memset(handle), bfr);
147+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
148148
} else {
149149
pack->data.pkd->parent = handle;
150150
pack->data.pkd->data = bfr;
@@ -153,7 +153,7 @@ void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res,
153153
}
154154
}
155155

156-
void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_type type, s3dat_packed_t* packed, s3dat_exception_t** throws) {
156+
void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_type type, s3dat_packed_t* packed, s3util_exception_t** throws) {
157157
uint32_t pixel_size = 2;
158158
if(type == s3dat_shadow) pixel_size = 0;
159159
if(type == s3dat_torso) pixel_size = 1;
@@ -191,8 +191,8 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
191191
}
192192

193193
packed->len = header_size+(metas*2)+(datas*pixel_size);
194-
packed->data = s3dat_alloc_func(handle, header_size+(metas*2)+(datas*pixel_size), throws);
195-
S3DAT_HANDLE_EXCEPTION(handle, throws, __FILE__, __func__, __LINE__);
194+
packed->data = s3util_alloc_func(s3dat_memset(handle), header_size+(metas*2)+(datas*pixel_size), throws);
195+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
196196

197197
uint16_t* size_meta;
198198

@@ -206,13 +206,13 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
206206
memcpy(packed->data, s3dat_image_header, 4);
207207
size_meta = packed->data+4;
208208

209-
uint16_t* off_ptr = packed->data+8;
210-
off_ptr[0] = s3dat_le16(bitmap->xoff);
211-
off_ptr[1] = s3dat_le16(bitmap->yoff);
209+
int16_t* off_ptr = packed->data+8;
210+
off_ptr[0] = s3util_le16((uint16_t)bitmap->xoff);
211+
off_ptr[1] = s3util_le16((uint16_t)bitmap->yoff);
212212
}
213213

214-
size_meta[0] = s3dat_le16(bitmap->width);
215-
size_meta[1] = s3dat_le16(bitmap->height);
214+
size_meta[0] = s3util_le16(bitmap->width);
215+
size_meta[1] = s3util_le16(bitmap->height);
216216

217217
uint16_t* meta = packed->data+header_size;
218218
uint8_t* data = packed->data+header_size+2;
@@ -233,7 +233,7 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
233233
current_clear++;
234234
}else if(stat == 2) {
235235
stat = 3;
236-
} else if(stat == 0) {
236+
} else { // stat == 0
237237
stat = 1;
238238
}
239239
}
@@ -245,7 +245,7 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
245245
current_clear = 0;
246246

247247
if((x+1) == bitmap->width) tmp_meta |= (1<<15);
248-
*meta = s3dat_le16(tmp_meta);
248+
*meta = s3util_le16(tmp_meta);
249249

250250
meta = (uint16_t*)data;
251251
data += 2;
@@ -269,7 +269,7 @@ s3dat_color_t s3dat_internal_ex(void* addr, s3dat_color_type type) {
269269
return color;
270270
}
271271

272-
uint16_t raw = s3dat_le16p(addr);
272+
uint16_t raw = s3util_le16p(addr);
273273

274274
if(type == s3dat_rgb555) {
275275
color.red = (uint8_t)(((raw >> 10) & 0x1F)*d58);
@@ -316,26 +316,26 @@ void s3dat_internal_8b_to_native(s3dat_color_t* color, void* to, s3dat_color_typ
316316
green = (green) << 5;
317317
}
318318

319-
*ptr16 = s3dat_le16(red+green+blue);
319+
*ptr16 = s3util_le16(red + green + blue);
320320
}
321321

322322
s3dat_color_t s3dat_internal_error_color = {0, 0, 0, 0};
323323

324-
s3dat_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uint8_t brightness, uint32_t x, s3dat_exception_t** throws) {
324+
s3dat_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uint8_t brightness, uint32_t x, s3util_exception_t** throws) {
325325
if(palette > handle->palette_index->len) {
326-
s3dat_throw(handle, throws, S3DAT_EXCEPTION_OUT_OF_RANGE, __FILE__, __func__, __LINE__);
326+
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_OUT_OF_RANGE, __FILE__, __func__, __LINE__);
327327
return s3dat_internal_error_color;
328328
}
329329

330-
s3dat_internal_seek_func(handle, handle->palette_index->pointers[palette]+brightness*handle->palette_line_length+x, S3DAT_SEEK_SET, throws);
330+
s3dat_internal_seek_func(handle, handle->palette_index->pointers[palette]+brightness*handle->palette_line_length+x, S3UTIL_SEEK_SET, throws);
331331
if(*throws != NULL) {
332-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
332+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
333333
return s3dat_internal_error_color;
334334
}
335335

336-
uint16_t color = s3dat_internal_read16LE(handle, throws);
336+
uint16_t color = S3DAT_INTERNAL_READ(16LE, handle, throws);
337337
if(*throws != NULL) {
338-
s3dat_add_to_stack(handle, throws, __FILE__, __func__, __LINE__);
338+
s3util_add_to_stack(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
339339
return s3dat_internal_error_color;
340340
}
341341

0 commit comments

Comments
 (0)