author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 28 Oct 2019 18:58:13 +0100 | |
changeset 15511 | 3c0a3c824c49 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
10017 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU General Public License |
|
7 |
* as published by the Free Software Foundation; either version 2 |
|
8 |
* of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 |
*/ |
|
19 |
||
20 |
#include "weapon.h" |
|
21 |
||
22 |
#include "../util/inihelper.h" |
|
23 |
#include "../util/logging.h" |
|
24 |
#include "../util/util.h" |
|
25 |
#include "../util/list.h" |
|
26 |
||
27 |
#include <stdlib.h> |
|
28 |
#include <ctype.h> |
|
29 |
#include <string.h> |
|
30 |
||
31 |
static void setField(char field[WEAPONS_COUNT+1], const char *line, int lineLen, bool no9) { |
|
32 |
if(lineLen>WEAPONS_COUNT) { |
|
33 |
lineLen = WEAPONS_COUNT; |
|
34 |
} |
|
35 |
||
36 |
char min = '0'; |
|
37 |
char max = no9 ? '8' : '9'; |
|
38 |
for(int i=0; i<lineLen; i++) { |
|
39 |
if(line[i] >= min && line[i] <= max) { |
|
40 |
field[i] = line[i]; |
|
41 |
} else { |
|
42 |
flib_log_w("Invalid character in weapon config string \"%.*s\", position %i", lineLen, line, i); |
|
43 |
field[i] = '0'; |
|
44 |
} |
|
45 |
} |
|
46 |
for(int i=lineLen; i<WEAPONS_COUNT; i++) { |
|
47 |
field[i] = '0'; |
|
48 |
} |
|
49 |
field[WEAPONS_COUNT] = 0; |
|
50 |
} |
|
51 |
||
52 |
flib_weaponset *flib_weaponset_create(const char *name) { |
|
53 |
flib_weaponset *result = NULL; |
|
54 |
if(!log_badargs_if(name==NULL)) { |
|
55 |
flib_weaponset *newSet = flib_calloc(1, sizeof(flib_weaponset)); |
|
56 |
if(newSet) { |
|
57 |
newSet->name = flib_strdupnull(name); |
|
58 |
if(newSet->name) { |
|
59 |
setField(newSet->loadout, "", 0, false); |
|
60 |
setField(newSet->crateprob, "", 0, false); |
|
61 |
setField(newSet->crateammo, "", 0, false); |
|
62 |
setField(newSet->delay, "", 0, false); |
|
63 |
result = newSet; |
|
64 |
newSet = NULL; |
|
65 |
} |
|
66 |
} |
|
67 |
flib_weaponset_destroy(newSet); |
|
68 |
} |
|
69 |
return result; |
|
70 |
} |
|
71 |
||
72 |
void flib_weaponset_destroy(flib_weaponset *cfg) { |
|
73 |
if(cfg) { |
|
74 |
free(cfg->name); |
|
75 |
free(cfg); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
flib_weaponset *flib_weaponset_copy(const flib_weaponset *weaponset) { |
|
80 |
if(!weaponset) { |
|
81 |
return NULL; |
|
82 |
} |
|
83 |
||
84 |
flib_weaponset *result = flib_weaponset_create(weaponset->name); |
|
85 |
if(result) { |
|
86 |
memcpy(result->loadout, weaponset->loadout, WEAPONS_COUNT+1); |
|
87 |
memcpy(result->crateprob, weaponset->crateprob, WEAPONS_COUNT+1); |
|
88 |
memcpy(result->delay, weaponset->delay, WEAPONS_COUNT+1); |
|
89 |
memcpy(result->crateammo, weaponset->crateammo, WEAPONS_COUNT+1); |
|
90 |
} |
|
91 |
||
92 |
return result; |
|
93 |
} |
|
94 |
||
95 |
void flib_weaponsetlist_destroy(flib_weaponsetlist *list) { |
|
96 |
if(list) { |
|
97 |
for(int i=0; i<list->weaponsetCount; i++) { |
|
98 |
flib_weaponset_destroy(list->weaponsets[i]); |
|
99 |
} |
|
100 |
free(list->weaponsets); |
|
101 |
free(list); |
|
102 |
} |
|
103 |
} |
|
7230
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
104 |
|
10017 | 105 |
flib_weaponset *flib_weaponset_from_ammostring(const char *name, const char *ammostring) { |
106 |
flib_weaponset *result = NULL; |
|
107 |
if(!log_badargs_if2(name==NULL, ammostring==NULL)) { |
|
108 |
result = flib_weaponset_create(name); |
|
109 |
if(result) { |
|
110 |
int fieldlen = strlen(ammostring)/4; |
|
111 |
setField(result->loadout, ammostring, fieldlen, false); |
|
112 |
setField(result->crateprob, ammostring + fieldlen, fieldlen, true); |
|
113 |
setField(result->delay, ammostring + 2*fieldlen, fieldlen, true); |
|
114 |
setField(result->crateammo, ammostring + 3*fieldlen, fieldlen, true); |
|
115 |
} |
|
116 |
} |
|
117 |
return result; |
|
118 |
} |
|
119 |
||
120 |
static int fillWeaponsetFromIni(flib_weaponsetlist *list, flib_ini *ini, int index) { |
|
121 |
int result = -1; |
|
122 |
char *keyname = flib_ini_get_keyname(ini, index); |
|
123 |
char *decodedKeyname = flib_urldecode(keyname); |
|
124 |
char *ammostring = NULL; |
|
125 |
if(decodedKeyname && !flib_ini_get_str(ini, &ammostring, keyname)) { |
|
126 |
flib_weaponset *set = flib_weaponset_from_ammostring(decodedKeyname, ammostring); |
|
127 |
if(set) { |
|
128 |
result = flib_weaponsetlist_insert(list, set, list->weaponsetCount); |
|
129 |
if(result) { |
|
130 |
flib_weaponset_destroy(set); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
free(ammostring); |
|
135 |
free(decodedKeyname); |
|
136 |
free(keyname); |
|
137 |
return result; |
|
138 |
} |
|
139 |
||
140 |
static int fillWeaponsetsFromIni(flib_weaponsetlist *list, flib_ini *ini) { |
|
141 |
bool error = false; |
|
142 |
int weaponsets = flib_ini_get_keycount(ini); |
|
143 |
||
144 |
for(int i=0; i<weaponsets && !error; i++) { |
|
145 |
error |= fillWeaponsetFromIni(list, ini, i); |
|
146 |
} |
|
147 |
return error; |
|
148 |
} |
|
149 |
||
150 |
flib_weaponsetlist *flib_weaponsetlist_from_ini(const char *filename) { |
|
151 |
flib_weaponsetlist *result = NULL; |
|
152 |
if(!log_badargs_if(filename==NULL)) { |
|
153 |
flib_ini *ini = flib_ini_load(filename); |
|
154 |
if(!ini) { |
|
155 |
flib_log_e("Missing file %s.", filename); |
|
156 |
} else if(flib_ini_enter_section(ini, "General")) { |
|
157 |
flib_log_e("Missing section \"General\" in file %s.", filename); |
|
158 |
} else { |
|
159 |
flib_weaponsetlist *tmpList = flib_weaponsetlist_create(); |
|
160 |
if(tmpList && !fillWeaponsetsFromIni(tmpList, ini)) { |
|
161 |
result = tmpList; |
|
162 |
tmpList = NULL; |
|
163 |
} |
|
164 |
flib_weaponsetlist_destroy(tmpList); |
|
165 |
} |
|
166 |
flib_ini_destroy(ini); |
|
167 |
} |
|
168 |
return result; |
|
169 |
} |
|
170 |
||
171 |
static bool needsEscape(char c) { |
|
172 |
return !((c>='0' && c<='9') || (c>='a' && c <='z')); |
|
173 |
} |
|
174 |
||
175 |
static int writeWeaponsetToIni(flib_ini *ini, flib_weaponset *set) { |
|
176 |
int result = -1; |
|
177 |
char weaponstring[WEAPONS_COUNT*4+1]; |
|
178 |
strcpy(weaponstring, set->loadout); |
|
179 |
strcat(weaponstring, set->crateprob); |
|
180 |
strcat(weaponstring, set->delay); |
|
181 |
strcat(weaponstring, set->crateammo); |
|
182 |
||
183 |
char *escapedname = flib_urlencode_pred(set->name, needsEscape); |
|
184 |
if(escapedname) { |
|
185 |
result = flib_ini_set_str(ini, escapedname, weaponstring); |
|
186 |
} |
|
187 |
free(escapedname); |
|
188 |
return result; |
|
189 |
} |
|
190 |
||
191 |
int flib_weaponsetlist_to_ini(const char *filename, const flib_weaponsetlist *list) { |
|
192 |
int result = -1; |
|
193 |
if(!log_badargs_if2(filename==NULL, list==NULL)) { |
|
194 |
flib_ini *ini = flib_ini_create(NULL); |
|
195 |
if(ini && !flib_ini_create_section(ini, "General")) { |
|
196 |
bool error = false; |
|
197 |
for(int i=0; i<list->weaponsetCount && !error; i++) { |
|
198 |
error |= writeWeaponsetToIni(ini, list->weaponsets[i]); |
|
199 |
} |
|
200 |
||
201 |
if(!error) { |
|
202 |
result = flib_ini_save(ini, filename); |
|
203 |
} |
|
204 |
} |
|
205 |
flib_ini_destroy(ini); |
|
206 |
} |
|
207 |
return result; |
|
208 |
} |
|
209 |
||
210 |
flib_weaponsetlist *flib_weaponsetlist_create() { |
|
211 |
return flib_calloc(1, sizeof(flib_weaponsetlist)); |
|
212 |
} |
|
213 |
||
214 |
GENERATE_STATIC_LIST_INSERT(insertWeaponset, flib_weaponset*) |
|
215 |
GENERATE_STATIC_LIST_DELETE(deleteWeaponset, flib_weaponset*) |
|
216 |
||
217 |
int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) { |
|
218 |
if(!log_badargs_if2(list==NULL, set==NULL) |
|
219 |
&& !insertWeaponset(&list->weaponsets, &list->weaponsetCount, set, pos)) { |
|
220 |
return 0; |
|
221 |
} |
|
222 |
return -1; |
|
223 |
} |
|
224 |
||
225 |
int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) { |
|
226 |
if(!log_badargs_if(list==NULL)) { |
|
227 |
flib_weaponset *elem = list->weaponsets[pos]; |
|
228 |
if(!deleteWeaponset(&list->weaponsets, &list->weaponsetCount, pos)) { |
|
229 |
flib_weaponset_destroy(elem); |
|
230 |
return 0; |
|
231 |
} |
|
232 |
} |
|
233 |
return -1; |
|
234 |
} |