| 1 | /* |
| 2 | Copyright (C) 2000 Marco Ziech ([email protected]) |
| 3 | Copyright (C) 2000 Ross Combs ([email protected]) |
| 4 | |
| 5 | This program is xfree software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #include "bni.h" |
| 21 | |
| 22 | #include "common/xalloc.h" |
| 23 | #include "fileio.h" |
| 24 | #include "common/setup_after.h" |
| 25 | |
| 26 | namespace pvpgn |
| 27 | { |
| 28 | |
| 29 | namespace bni |
| 30 | { |
| 31 | |
| 32 | extern t_bnifile * load_bni(std::FILE *f) { |
| 33 | t_bnifile *b; |
| 34 | unsigned int i; |
| 35 | |
| 36 | if (f == NULL) return NULL; |
| 37 | b = (t_bnifile*)xmalloc(sizeof(t_bnifile)); |
| 38 | file_rpush(f); |
| 39 | b->unknown1 = file_readd_le(); |
| 40 | if (b->unknown1 != 0x00000010) |
| 41 | std::fprintf(stderr, "load_bni: field 1 is not 0x00000010. Data may be invalid!\n"); |
| 42 | b->unknown2 = file_readd_le(); |
| 43 | if (b->unknown2 != 0x00000001) |
| 44 | std::fprintf(stderr, "load_bni: field 2 is not 0x00000001. Data may be invalid!\n"); |
| 45 | b->numicons = file_readd_le(); |
| 46 | if (b->numicons > BNI_MAXICONS) { |
| 47 | std::fprintf(stderr, "load_bni: more than %d (BNI_MAXICONS) icons. Increase maximum number of icons in \"bni.h\".\n", BNI_MAXICONS); |
| 48 | b->numicons = BNI_MAXICONS; |
| 49 | } |
| 50 | b->dataoffset = file_readd_le(); |
| 51 | if (b->numicons < 1) { |
| 52 | std::fprintf(stderr, "load_bni: strange, no icons present in BNI file\n"); |
| 53 | b->icons = NULL; |
| 54 | } |
| 55 | else { |
| 56 | b->icons = (struct bni_iconlist_struct*)xmalloc(b->numicons*sizeof(t_bniicon)); |
| 57 | } |
| 58 | for (i = 0; i < b->numicons; i++) { |
| 59 | b->icons->icon[i].id = file_readd_le(); |
| 60 | b->icons->icon[i].x = file_readd_le(); |
| 61 | b->icons->icon[i].y = file_readd_le(); |
| 62 | if (b->icons->icon[i].id == 0) { |
| 63 | b->icons->icon[i].tag = file_readd_le(); |
| 64 | } |
| 65 | else { |
| 66 | b->icons->icon[i].tag = 0; |
| 67 | } |
| 68 | b->icons->icon[i].unknown = file_readd_le(); |
| 69 | } |
| 70 | if (std::ftell(f) != (long)b->dataoffset) |
| 71 | std::fprintf(stderr, "load_bni: Warning, %lu bytes of garbage after BNI header\n", (unsigned long)(b->dataoffset - std::ftell(f))); |
| 72 | file_rpop(); |
| 73 | return b; |
| 74 | } |
| 75 | |
| 76 | extern int write_bni(std::FILE *f, t_bnifile *b) { |
| 77 | unsigned int i; |
| 78 | |
| 79 | if (f == NULL) return -1; |
| 80 | if (b == NULL) return -1; |
| 81 | file_wpush(f); |
| 82 | file_writed_le(b->unknown1); |
| 83 | if (b->unknown1 != 0x00000010) |
| 84 | std::fprintf(stderr, "write_bni: field 1 is not 0x00000010. Data may be invalid!\n"); |
| 85 | file_writed_le(b->unknown2); |
| 86 | if (b->unknown2 != 0x00000001) |
| 87 | std::fprintf(stderr, "write_bni: field 2 is not 0x00000001. Data may be invalid!\n"); |
| 88 | file_writed_le(b->numicons); |
| 89 | file_writed_le(b->dataoffset); |
| 90 | for (i = 0; i < b->numicons; i++) { |
| 91 | file_writed_le(b->icons->icon[i].id); |
| 92 | file_writed_le(b->icons->icon[i].x); |
| 93 | file_writed_le(b->icons->icon[i].y); |
| 94 | if (b->icons->icon[i].id == 0) { |
| 95 | file_writed_le(b->icons->icon[i].tag); |
| 96 | } |
| 97 | file_writed_le(b->icons->icon[i].unknown); |
| 98 | } |
| 99 | if (std::ftell(f) != (long)b->dataoffset) |
| 100 | std::fprintf(stderr, "Warning: dataoffset is incorrect! (=0x%lx should be 0x%lx)\n", (unsigned long)b->dataoffset, (unsigned long)std::ftell(f)); |
| 101 | file_wpop(); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | } |