Skip to content

Commit

Permalink
Merge pull request #25 from yellowcake-org/develop
Browse files Browse the repository at this point in the history
Releasing 0.8.5
  • Loading branch information
0xceed authored Nov 25, 2023
2 parents e6d60a7 + fe7cc69 commit 06c9fa1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 24 deletions.
52 changes: 36 additions & 16 deletions examples/ycimap/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ static arg_lit_t *help; // NOLINT(cppcoreguidelines-avoid-non-const-global-varia
static arg_file_t *input, *resources; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static arg_end_t *end; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

uint32_t type_byte_from_proto(uint32_t pid, const char *root, char *type);
yc_res_map_status_t type_byte_from_proto(uint32_t pid, const char *root, char *type, uint32_t *result);

char *proto_filename(uint32_t pid, const char *root, const char *type);

yc_res_pro_object_item_type_t ycimap_fetch_items_type(uint32_t pid, const void *context);
yc_res_pro_object_scenery_type_t ycimap_fetch_scenery_type(uint32_t pid, const void *context);
yc_res_map_status_t ycimap_fetch_items_type(
uint32_t pid, yc_res_pro_object_item_type_t *result, const void *context
);

yc_res_map_status_t ycimap_fetch_scenery_type(
uint32_t pid, yc_res_pro_object_scenery_type_t *result, const void *context
);

void *ycimap_io_fopen(const char *filename, const char *mode);

int ycimap_io_fclose(void *stream);

int ycimap_io_fseek(void *stream, long offset, int whence);

size_t ycimap_io_fread(void *dest, size_t len, size_t cnt, void *str);

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -65,7 +73,7 @@ int main(int argc, char *argv[]) {
.fread = &ycimap_io_fread,
};

yc_res_map_parse_result_t result = {NULL};
yc_res_map_parse_result_t result = { NULL };
yc_res_map_status_t status = yc_res_map_parse(filename, &io_api, &db_api, &result);

if (YC_RES_MAP_STATUS_OK != status) {
Expand Down Expand Up @@ -202,43 +210,55 @@ char *proto_filename(uint32_t pid, const char *root, const char *type) {
return proto_name;
}

uint32_t type_byte_from_proto(uint32_t pid, const char *root, char *type) {
yc_res_map_status_t type_byte_from_proto(uint32_t pid, const char *root, char *type, uint32_t *result) {
char *proto_name = proto_filename(pid, root, type);
FILE *file = fopen(proto_name, "rb");

if (NULL == result) { goto error; }
if (NULL == file) { goto error; }

if (0 != fseek(file, 0x20, SEEK_CUR)) { goto error; }

uint32_t result = 0xFFFFFFFF;
if (0 == fread(&result, sizeof(uint32_t), 1, file)) { goto error; }
uint32_t read = 0xFFFFFFFF;
if (0 == fread(&read, sizeof(uint32_t), 1, file)) { goto error; }

free(proto_name);
fclose(file); // NOLINT(cert-err33-c)

// from BE
return ((result >> 24) & 0xff) |
((result << 8) & 0xff0000) |
((result >> 8) & 0xff00) |
((result << 24) & 0xff000000);
*result =
((read >> 24) & 0xff) |
((read << 8) & 0xff0000) |
((read >> 8) & 0xff00) |
((read << 24) & 0xff000000);

return YC_RES_MAP_STATUS_OK;

error:
fclose(file);
free(proto_name);
return 0xFFFFFFFF;

return YC_RES_MAP_STATUS_CORR;
}

yc_res_pro_object_item_type_t ycimap_fetch_items_type(uint32_t pid, const void *context) {
return type_byte_from_proto(pid, context, "ITEMS");
yc_res_map_status_t ycimap_fetch_items_type(
uint32_t pid, yc_res_pro_object_item_type_t *result, const void *context
) {
return type_byte_from_proto(pid, context, "ITEMS", result);
}

yc_res_pro_object_scenery_type_t ycimap_fetch_scenery_type(uint32_t pid, const void *context) {
return type_byte_from_proto(pid, context, "SCENERY");
yc_res_map_status_t ycimap_fetch_scenery_type(
uint32_t pid, yc_res_pro_object_scenery_type_t *result, const void *context
) {
return type_byte_from_proto(pid, context, "SCENERY", result);
}

void *ycimap_io_fopen(const char *filename, const char *mode) { return fopen(filename, mode); }

int ycimap_io_fclose(void *stream) { return fclose(stream); }

int ycimap_io_fseek(void *stream, long offset, int whence) { return fseek(stream, offset, whence); }

size_t ycimap_io_fread(void *dest, size_t len, size_t cnt, void *str) {
return fread(dest, len, cnt, str);
}
9 changes: 7 additions & 2 deletions include/map/methods/methods.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef LIBYCRES_INCLUDE_MAP_METHODS_H
#define LIBYCRES_INCLUDE_MAP_METHODS_H

typedef yc_res_pro_object_item_type_t yc_res_map_parse_db_api_item_fetcher_t(uint32_t pid, const void *context);
typedef yc_res_pro_object_scenery_type_t yc_res_map_parse_db_api_scenery_fetcher_t(uint32_t pid, const void *context);
typedef yc_res_map_status_t yc_res_map_parse_db_api_item_fetcher_t(
uint32_t pid, yc_res_pro_object_item_type_t *result, const void *context
);

typedef yc_res_map_status_t yc_res_map_parse_db_api_scenery_fetcher_t(
uint32_t pid, yc_res_pro_object_scenery_type_t *result, const void *context
);

typedef struct yc_res_map_parse_db_api {
const void *context;
Expand Down
3 changes: 3 additions & 0 deletions include/pro/methods/methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ bool yc_res_pro_is_valid_id(uint32_t any_id);

uint16_t yc_res_pro_index_from_object_id(uint32_t any_id);
uint16_t yc_res_pro_index_from_script_id(uint32_t script_id);
uint16_t yc_res_pro_index_from_sprite_id(uint32_t sprite_id);

yc_res_pro_object_type_t yc_res_pro_object_type_from_pid(uint32_t proto_id);
yc_res_pro_object_type_t yc_res_pro_object_type_from_fid(uint32_t sprite_id);
yc_res_pro_script_type_t yc_res_pro_script_type_from_sid(uint32_t script_id);

uint32_t yc_res_pro_fid_from(uint16_t sprite_idx, yc_res_pro_object_type_t type);

typedef struct yc_res_pro_parse_result {
yc_res_pro_object_t *object;
} yc_res_pro_parse_result_t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ yc_res_map_status_t yc_res_map_parse_object_patch_item(
into->item = malloc(sizeof(yc_res_map_level_object_patch_item_t));
if (NULL == into->item) { return YC_RES_MAP_STATUS_MEM; }

into->item->type = fetchers->item_type_from_pid(pid, fetchers->context);
yc_res_map_status_t status = fetchers->item_type_from_pid(pid, &into->item->type, fetchers->context);
if (YC_RES_MAP_STATUS_OK != status) { return status; }

switch (into->item->type) {
case YC_RES_PRO_OBJECT_ITEM_TYPE_WEAPON:
into->item->data.weapon = malloc(sizeof(yc_res_map_level_object_patch_item_weapon_t));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ yc_res_map_status_t yc_res_map_parse_object_patch_scenery(
into->scenery = malloc(sizeof(yc_res_map_level_object_patch_scenery_t));
if (NULL == into->scenery) { return YC_RES_MAP_STATUS_MEM; }

into->scenery->type = fetchers->scenery_type_from_pid(pid, fetchers->context);
yc_res_map_status_t status = fetchers->scenery_type_from_pid(pid, &into->scenery->type, fetchers->context);
if (YC_RES_MAP_STATUS_OK != status) { return status; }

switch (into->scenery->type) {
case YC_RES_PRO_OBJECT_SCENERY_TYPE_DOOR:
into->scenery->data.door = malloc(sizeof(yc_res_map_level_object_patch_scenery_door_t));
Expand Down
4 changes: 2 additions & 2 deletions source/formats/map/yc_res_map_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ yc_res_map_status_t yc_res_map_parse_tiles(void *file, const yc_res_io_fs_api_t
if (0 == api->fread(&floor_idx, sizeof(uint16_t), 1, file)) { return YC_RES_MAP_STATUS_IO; }

level->roof.idxes[pos_y][grid_size_horizontal - 1 - pos_x] =
yc_res_byteorder_uint16(roof_idx);
yc_res_pro_index_from_sprite_id(yc_res_byteorder_uint16(roof_idx));

level->floor.idxes[pos_y][grid_size_horizontal - 1 - pos_x] =
yc_res_byteorder_uint16(floor_idx);
yc_res_pro_index_from_sprite_id(yc_res_byteorder_uint16(floor_idx));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions source/formats/pro/yc_res_pro_ids.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ bool yc_res_pro_is_valid_id(uint32_t any_id) {
}

uint16_t yc_res_pro_index_from_object_id(uint32_t any_id) {
return any_id & 0x00FFFFFF;
return (any_id & 0x00FFFFFF) & 0xFFF;
}

uint16_t yc_res_pro_index_from_script_id(uint32_t script_id) {
return script_id & 0x00FFFFFF;
return (script_id & 0x00FFFFFF) & 0xFFF;
}

uint16_t yc_res_pro_index_from_sprite_id(uint32_t sprite_id) {
return (sprite_id & 0x00FFFFFF) & 0xFFF;
}
5 changes: 5 additions & 0 deletions source/formats/pro/yc_res_pro_idxes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <libycres.h>

uint32_t yc_res_pro_fid_from(uint16_t sprite_idx, yc_res_pro_object_type_t type) {
return (type << 24) | sprite_idx;
}

0 comments on commit 06c9fa1

Please sign in to comment.