Mercurial > hg > batmud > maputils
annotate src/patchmap.c @ 2835:9405a18756c8 default tip
Add entry for Caz.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 18 Jun 2024 11:21:06 +0300 |
parents | 0fda0c7e7f1c |
children |
rev | line source |
---|---|
549 | 1 /* |
1826 | 2 * Patch a map with given diff file |
3 * Programmed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
2417 | 4 * (C) Copyright 2008-2022 Tecnic Software productions (TNSP) |
549 | 5 */ |
1308
d11f9751e299
Renamed maputils.[ch] -> libmaputils.[ch], adjusted other code accordingly;
Matti Hamalainen <ccr@tnsp.org>
parents:
1249
diff
changeset
|
6 #include "libmaputils.h" |
549 | 7 #include "th_util.h" |
8 #include "th_args.h" | |
9 #include "th_string.h" | |
10 | |
1827 | 11 |
714 | 12 char *mapFilename = NULL, |
13 *patchFilename = NULL, | |
14 *destFilename = NULL; | |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
15 bool optCheck = false; |
549 | 16 |
17 | |
18 /* Arguments | |
19 */ | |
1615 | 20 static const th_optarg optList[] = |
1484
2350cd3a6feb
Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents:
1317
diff
changeset
|
21 { |
714 | 22 { 0, '?', "help", "Show this help", OPT_NONE }, |
23 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
24 { 2, 'q', "quiet", "Be quiet", OPT_NONE }, | |
25 { 3, 'c', "check", "Check non-patched blocks", OPT_NONE }, | |
26 { 4, 'o', "output", "Output filename", OPT_ARGREQ }, | |
549 | 27 }; |
28 | |
1484
2350cd3a6feb
Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents:
1317
diff
changeset
|
29 static const int optListN = sizeof(optList) / sizeof(optList[0]); |
549 | 30 |
31 | |
32 void argShowHelp(void) | |
33 { | |
1249 | 34 th_print_banner(stdout, th_prog_name, |
35 "[options] <mapfile> <patchfile>"); | |
36 | |
2240
0799192a29ff
Adjust to th-libs th_args_help() API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
2236
diff
changeset
|
37 th_args_help(stdout, optList, optListN, 0, 80 - 2); |
549 | 38 } |
39 | |
40 | |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
41 bool argHandleOpt(const int optN, char *optArg, char *currArg) |
549 | 42 { |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
43 switch (optN) |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
44 { |
714 | 45 case 0: |
46 argShowHelp(); | |
47 exit(0); | |
48 break; | |
549 | 49 |
714 | 50 case 1: |
2007
0dc8668abbbc
Fix to match changes in th-libs API.
Matti Hamalainen <ccr@tnsp.org>
parents:
1827
diff
changeset
|
51 th_verbosity++; |
714 | 52 break; |
1543 | 53 |
714 | 54 case 2: |
2007
0dc8668abbbc
Fix to match changes in th-libs API.
Matti Hamalainen <ccr@tnsp.org>
parents:
1827
diff
changeset
|
55 th_verbosity = -1; |
714 | 56 break; |
1543 | 57 |
714 | 58 case 3: |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
59 optCheck = true; |
714 | 60 THMSG(2, "Checking non-patched blocks for match.\n"); |
61 break; | |
1543 | 62 |
714 | 63 case 4: |
64 destFilename = optArg; | |
65 THMSG(2, "Output file set to '%s'.\n", destFilename); | |
66 break; | |
1543 | 67 |
714 | 68 default: |
69 THERR("Unknown option '%s'.\n", currArg); | |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
70 return false; |
714 | 71 } |
1543 | 72 |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
73 return true; |
549 | 74 } |
75 | |
76 | |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
77 bool argHandleFile(char *currArg) |
549 | 78 { |
2177 | 79 if (mapFilename == NULL) |
714 | 80 mapFilename = currArg; |
1692 | 81 else |
2177 | 82 if (patchFilename == NULL) |
714 | 83 patchFilename = currArg; |
1317 | 84 else |
85 { | |
714 | 86 THERR("Too many input map files specified!\n"); |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
87 return false; |
714 | 88 } |
549 | 89 |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
90 return true; |
549 | 91 } |
92 | |
93 | |
94 int main(int argc, char *argv[]) | |
95 { | |
2537
e96e757ab01e
Make program return values handling a bit more consistent.
Matti Hamalainen <ccr@tnsp.org>
parents:
2534
diff
changeset
|
96 int res = 0; |
2177 | 97 MapBlock *map = NULL, *patch = NULL; |
98 FILE *outFile = NULL; | |
1543 | 99 |
1770 | 100 // Initialize |
714 | 101 th_init("patchmap", "Patch a mapfile with a diff", "0.1", NULL, NULL); |
2007
0dc8668abbbc
Fix to match changes in th-libs API.
Matti Hamalainen <ccr@tnsp.org>
parents:
1827
diff
changeset
|
102 th_verbosity = 1; |
549 | 103 |
1770 | 104 // Parse arguments |
714 | 105 if (!th_args_process(argc, argv, optList, optListN, |
1484
2350cd3a6feb
Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents:
1317
diff
changeset
|
106 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
2177 | 107 { |
108 res = 1; | |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
109 goto out; |
2177 | 110 } |
1543 | 111 |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
112 if (mapFilename == NULL || patchFilename == NULL) |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
113 { |
2539
0fda0c7e7f1c
Show help when there's an argument error or nothing to do.
Matti Hamalainen <ccr@tnsp.org>
parents:
2537
diff
changeset
|
114 argShowHelp(); |
0fda0c7e7f1c
Show help when there's an argument error or nothing to do.
Matti Hamalainen <ccr@tnsp.org>
parents:
2537
diff
changeset
|
115 THERR("Nothing to do.\n"); |
2177 | 116 res = 1; |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
117 goto out; |
714 | 118 } |
1543 | 119 |
1770 | 120 // Read map and patch file |
2470
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
121 if ((map = mapBlockParseFile(mapFilename, false)) == NULL || |
d0aad04c3e61
th-libs now uses stdbool.h if possible, so we need to rename all
Matti Hamalainen <ccr@tnsp.org>
parents:
2417
diff
changeset
|
122 (patch = mapBlockParseFile(patchFilename, true)) == NULL) |
2177 | 123 { |
2537
e96e757ab01e
Make program return values handling a bit more consistent.
Matti Hamalainen <ccr@tnsp.org>
parents:
2534
diff
changeset
|
124 res = -2; |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
125 goto out; |
2177 | 126 } |
1543 | 127 |
1770 | 128 // Check map and diff sizes |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
129 if (optCheck && (map->width != patch->width || map->height != patch->height)) |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
130 { |
714 | 131 THERR("Map and patch dimensions do not match (%d x %d [map] <-> %d x %d [patch])\n", |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
132 map->width, map->height, |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
133 patch->width, patch->height); |
2177 | 134 |
2537
e96e757ab01e
Make program return values handling a bit more consistent.
Matti Hamalainen <ccr@tnsp.org>
parents:
2534
diff
changeset
|
135 res = -2; |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
136 goto out; |
714 | 137 } |
1543 | 138 |
1770 | 139 // Generate |
2232 | 140 for (int yc = 0; yc < map->height; yc++) |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
141 { |
1770 | 142 unsigned char |
2232 | 143 *dp = map->data + (map->scansize * yc), |
144 *sp = patch->data + (patch->scansize * yc); | |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
145 |
2232 | 146 for (int xc = 0; xc < map->width; xc++, sp++, dp++) |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
147 { |
2232 | 148 const int i = (*sp & 63); |
149 unsigned char ch; | |
1543 | 150 |
2232 | 151 if (*sp == 0xfd) |
152 ch = ' '; | |
1692 | 153 else |
154 if (i < nmapPieces) | |
2232 | 155 ch = mapPieces[i].symbol; |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
156 else |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
157 { |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
158 THERR("[%d,%d] invalid symbol index %d (%d) in patch\n", |
2232 | 159 xc+1, yc+1, *sp, i); |
2537
e96e757ab01e
Make program return values handling a bit more consistent.
Matti Hamalainen <ccr@tnsp.org>
parents:
2534
diff
changeset
|
160 res = -7; |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
161 goto out; |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
162 } |
1543 | 163 |
2232 | 164 if (*sp < 64) |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
165 { |
2232 | 166 if (optCheck && *sp != *dp) |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
167 { |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
168 THERR("[%d,%d] mismatch %d != %d\n", |
2232 | 169 xc+1, yc+1, *sp, *dp); |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
170 } |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
171 } |
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
172 else |
2232 | 173 *dp = ch; |
714 | 174 } |
175 } | |
1543 | 176 |
1770 | 177 // Open output file |
714 | 178 if (destFilename == NULL) |
179 outFile = stdout; | |
1692 | 180 else |
181 if ((outFile = fopen(destFilename, "wb")) == NULL) | |
1314
5d6ddd4d6781
Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
1310
diff
changeset
|
182 { |
714 | 183 THERR("Error opening output file '%s'!\n", |
184 destFilename); | |
2537
e96e757ab01e
Make program return values handling a bit more consistent.
Matti Hamalainen <ccr@tnsp.org>
parents:
2534
diff
changeset
|
185 res = -6; |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
186 goto out; |
714 | 187 } |
1543 | 188 |
1310
a6be5a68032f
Rename map block handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1309
diff
changeset
|
189 mapBlockPrint(outFile, map); |
1543 | 190 |
2534
76b67c40fbf5
Rename "exit" label to "out".
Matti Hamalainen <ccr@tnsp.org>
parents:
2470
diff
changeset
|
191 out: |
2208 | 192 if (outFile != NULL) |
193 fclose(outFile); | |
2177 | 194 |
195 mapBlockFree(map); | |
196 mapBlockFree(patch); | |
197 | |
198 return res; | |
549 | 199 } |