25 |
25 |
26 #include <string.h> |
26 #include <string.h> |
27 #include <stdlib.h> |
27 #include <stdlib.h> |
28 |
28 |
29 static flib_team *from_ini_handleError(flib_team *result, flib_ini *settingfile) { |
29 static flib_team *from_ini_handleError(flib_team *result, flib_ini *settingfile) { |
30 flib_ini_destroy(settingfile); |
30 flib_ini_destroy(settingfile); |
31 flib_team_destroy(result); |
31 flib_team_destroy(result); |
32 return NULL; |
32 return NULL; |
33 } |
33 } |
34 |
34 |
35 flib_team *flib_team_from_ini(const char *filename) { |
35 flib_team *flib_team_from_ini(const char *filename) { |
36 if(log_badargs_if(filename==NULL)) { |
36 if(log_badargs_if(filename==NULL)) { |
37 return NULL; |
37 return NULL; |
38 } |
38 } |
39 |
39 |
40 flib_team *result = flib_calloc(1, sizeof(flib_team)); |
40 flib_team *result = flib_calloc(1, sizeof(flib_team)); |
41 flib_ini *ini = NULL; |
41 flib_ini *ini = NULL; |
42 |
42 |
43 if(!result) { |
43 if(!result) { |
44 return from_ini_handleError(result, ini); |
44 return from_ini_handleError(result, ini); |
45 } |
45 } |
46 |
46 |
47 ini = flib_ini_load(filename); |
47 ini = flib_ini_load(filename); |
48 if(!ini) { |
48 if(!ini) { |
49 flib_log_e("Error loading team file %s", filename); |
49 flib_log_e("Error loading team file %s", filename); |
50 return from_ini_handleError(result, ini); |
50 return from_ini_handleError(result, ini); |
51 } |
51 } |
52 |
52 |
53 if(flib_ini_enter_section(ini, "team")) { |
53 if(flib_ini_enter_section(ini, "team")) { |
54 flib_log_e("Missing section \"Team\" in team file %s", filename); |
54 flib_log_e("Missing section \"Team\" in team file %s", filename); |
55 return from_ini_handleError(result, ini); |
55 return from_ini_handleError(result, ini); |
56 } |
56 } |
57 bool error = false; |
57 bool error = false; |
58 error |= flib_ini_get_str(ini, &result->name, "name"); |
58 error |= flib_ini_get_str(ini, &result->name, "name"); |
59 error |= flib_ini_get_str(ini, &result->grave, "grave"); |
59 error |= flib_ini_get_str(ini, &result->grave, "grave"); |
60 error |= flib_ini_get_str(ini, &result->fort, "fort"); |
60 error |= flib_ini_get_str(ini, &result->fort, "fort"); |
61 error |= flib_ini_get_str(ini, &result->voicepack, "voicepack"); |
61 error |= flib_ini_get_str(ini, &result->voicepack, "voicepack"); |
62 error |= flib_ini_get_str(ini, &result->flag, "flag"); |
62 error |= flib_ini_get_str(ini, &result->flag, "flag"); |
63 error |= flib_ini_get_int(ini, &result->rounds, "rounds"); |
63 error |= flib_ini_get_int(ini, &result->rounds, "rounds"); |
64 error |= flib_ini_get_int(ini, &result->wins, "wins"); |
64 error |= flib_ini_get_int(ini, &result->wins, "wins"); |
65 error |= flib_ini_get_int(ini, &result->campaignProgress, "campaignprogress"); |
65 error |= flib_ini_get_int(ini, &result->campaignProgress, "campaignprogress"); |
66 |
66 |
67 int difficulty = 0; |
67 int difficulty = 0; |
68 error |= flib_ini_get_int(ini, &difficulty, "difficulty"); |
68 error |= flib_ini_get_int(ini, &difficulty, "difficulty"); |
69 |
69 |
70 if(error) { |
70 if(error) { |
71 flib_log_e("Missing or malformed entry in section \"Team\" in file %s", filename); |
71 flib_log_e("Missing or malformed entry in section \"Team\" in file %s", filename); |
72 return from_ini_handleError(result, ini); |
72 return from_ini_handleError(result, ini); |
73 } |
73 } |
74 |
74 |
75 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
75 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
76 char sectionName[32]; |
76 char sectionName[32]; |
77 if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) { |
77 if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) { |
78 return from_ini_handleError(result, ini); |
78 return from_ini_handleError(result, ini); |
79 } |
79 } |
80 if(flib_ini_enter_section(ini, sectionName)) { |
80 if(flib_ini_enter_section(ini, sectionName)) { |
81 flib_log_e("Missing section \"%s\" in team file %s", sectionName, filename); |
81 flib_log_e("Missing section \"%s\" in team file %s", sectionName, filename); |
82 return from_ini_handleError(result, ini); |
82 return from_ini_handleError(result, ini); |
83 } |
83 } |
84 flib_hog *hog = &result->hogs[i]; |
84 flib_hog *hog = &result->hogs[i]; |
85 error |= flib_ini_get_str(ini, &hog->name, "name"); |
85 error |= flib_ini_get_str(ini, &hog->name, "name"); |
86 error |= flib_ini_get_str(ini, &hog->hat, "hat"); |
86 error |= flib_ini_get_str(ini, &hog->hat, "hat"); |
87 error |= flib_ini_get_int(ini, &hog->rounds, "rounds"); |
87 error |= flib_ini_get_int(ini, &hog->rounds, "rounds"); |
88 error |= flib_ini_get_int(ini, &hog->kills, "kills"); |
88 error |= flib_ini_get_int(ini, &hog->kills, "kills"); |
89 error |= flib_ini_get_int(ini, &hog->deaths, "deaths"); |
89 error |= flib_ini_get_int(ini, &hog->deaths, "deaths"); |
90 error |= flib_ini_get_int(ini, &hog->suicides, "suicides"); |
90 error |= flib_ini_get_int(ini, &hog->suicides, "suicides"); |
91 result->hogs[i].difficulty = difficulty; |
91 result->hogs[i].difficulty = difficulty; |
92 result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH; |
92 result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH; |
93 |
93 |
94 if(error) { |
94 if(error) { |
95 flib_log_e("Missing or malformed entry in section \"%s\" in file %s", sectionName, filename); |
95 flib_log_e("Missing or malformed entry in section \"%s\" in file %s", sectionName, filename); |
96 return from_ini_handleError(result, ini); |
96 return from_ini_handleError(result, ini); |
97 } |
97 } |
98 } |
98 } |
99 |
99 |
100 if(!flib_ini_enter_section(ini, "binds")) { |
100 if(!flib_ini_enter_section(ini, "binds")) { |
101 result->bindingCount = flib_ini_get_keycount(ini); |
101 result->bindingCount = flib_ini_get_keycount(ini); |
102 if(result->bindingCount<0) { |
102 if(result->bindingCount<0) { |
103 flib_log_e("Error reading bindings from file %s", filename); |
103 flib_log_e("Error reading bindings from file %s", filename); |
104 result->bindingCount = 0; |
104 result->bindingCount = 0; |
105 } |
105 } |
106 result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding)); |
106 result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding)); |
107 if(!result->bindings) { |
107 if(!result->bindings) { |
108 return from_ini_handleError(result, ini); |
108 return from_ini_handleError(result, ini); |
109 } |
109 } |
110 for(int i=0; i<result->bindingCount; i++) { |
110 for(int i=0; i<result->bindingCount; i++) { |
111 char *keyname = flib_ini_get_keyname(ini, i); |
111 char *keyname = flib_ini_get_keyname(ini, i); |
112 if(!keyname) { |
112 if(!keyname) { |
113 error = true; |
113 error = true; |
114 } else { |
114 } else { |
115 result->bindings[i].action = flib_urldecode(keyname); |
115 result->bindings[i].action = flib_urldecode(keyname); |
116 error |= !result->bindings[i].action; |
116 error |= !result->bindings[i].action; |
117 error |= flib_ini_get_str(ini, &result->bindings[i].binding, keyname); |
117 error |= flib_ini_get_str(ini, &result->bindings[i].binding, keyname); |
118 } |
118 } |
119 free(keyname); |
119 free(keyname); |
120 } |
120 } |
121 } |
121 } |
122 |
122 |
123 if(error) { |
123 if(error) { |
124 flib_log_e("Error reading team file %s", filename); |
124 flib_log_e("Error reading team file %s", filename); |
125 return from_ini_handleError(result, ini); |
125 return from_ini_handleError(result, ini); |
126 } |
126 } |
127 |
127 |
128 flib_ini_destroy(ini); |
128 flib_ini_destroy(ini); |
129 return result; |
129 return result; |
130 } |
130 } |
131 |
131 |
132 void flib_team_destroy(flib_team *team) { |
132 void flib_team_destroy(flib_team *team) { |
133 if(team) { |
133 if(team) { |
134 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
134 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
135 free(team->hogs[i].name); |
135 free(team->hogs[i].name); |
136 free(team->hogs[i].hat); |
136 free(team->hogs[i].hat); |
137 flib_weaponset_destroy(team->hogs[i].weaponset); |
137 flib_weaponset_destroy(team->hogs[i].weaponset); |
138 } |
138 } |
139 free(team->name); |
139 free(team->name); |
140 free(team->grave); |
140 free(team->grave); |
141 free(team->fort); |
141 free(team->fort); |
142 free(team->voicepack); |
142 free(team->voicepack); |
143 free(team->flag); |
143 free(team->flag); |
144 if(team->bindings) { |
144 if(team->bindings) { |
145 for(int i=0; i<team->bindingCount; i++) { |
145 for(int i=0; i<team->bindingCount; i++) { |
146 free(team->bindings[i].action); |
146 free(team->bindings[i].action); |
147 free(team->bindings[i].binding); |
147 free(team->bindings[i].binding); |
148 } |
148 } |
149 } |
149 } |
150 free(team->bindings); |
150 free(team->bindings); |
151 free(team->ownerName); |
151 free(team->ownerName); |
152 free(team); |
152 free(team); |
153 } |
153 } |
154 } |
154 } |
155 |
155 |
156 static int writeTeamSection(const flib_team *team, flib_ini *ini) { |
156 static int writeTeamSection(const flib_team *team, flib_ini *ini) { |
157 if(flib_ini_create_section(ini, "team")) { |
157 if(flib_ini_create_section(ini, "team")) { |
158 return -1; |
158 return -1; |
159 } |
159 } |
160 bool error = false; |
160 bool error = false; |
161 error |= flib_ini_set_str(ini, "name", team->name); |
161 error |= flib_ini_set_str(ini, "name", team->name); |
162 error |= flib_ini_set_str(ini, "grave", team->grave); |
162 error |= flib_ini_set_str(ini, "grave", team->grave); |
163 error |= flib_ini_set_str(ini, "fort", team->fort); |
163 error |= flib_ini_set_str(ini, "fort", team->fort); |
164 error |= flib_ini_set_str(ini, "voicepack", team->voicepack); |
164 error |= flib_ini_set_str(ini, "voicepack", team->voicepack); |
165 error |= flib_ini_set_str(ini, "flag", team->flag); |
165 error |= flib_ini_set_str(ini, "flag", team->flag); |
166 error |= flib_ini_set_int(ini, "rounds", team->rounds); |
166 error |= flib_ini_set_int(ini, "rounds", team->rounds); |
167 error |= flib_ini_set_int(ini, "wins", team->wins); |
167 error |= flib_ini_set_int(ini, "wins", team->wins); |
168 error |= flib_ini_set_int(ini, "campaignprogress", team->campaignProgress); |
168 error |= flib_ini_set_int(ini, "campaignprogress", team->campaignProgress); |
169 error |= flib_ini_set_int(ini, "difficulty", team->hogs[0].difficulty); |
169 error |= flib_ini_set_int(ini, "difficulty", team->hogs[0].difficulty); |
170 return error; |
170 return error; |
171 } |
171 } |
172 |
172 |
173 static int writeHogSections(const flib_team *team, flib_ini *ini) { |
173 static int writeHogSections(const flib_team *team, flib_ini *ini) { |
174 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
174 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
175 const flib_hog *hog = &team->hogs[i]; |
175 const flib_hog *hog = &team->hogs[i]; |
176 char sectionName[32]; |
176 char sectionName[32]; |
177 if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) { |
177 if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) { |
178 return -1; |
178 return -1; |
179 } |
179 } |
180 if(flib_ini_create_section(ini, sectionName)) { |
180 if(flib_ini_create_section(ini, sectionName)) { |
181 return -1; |
181 return -1; |
182 } |
182 } |
183 bool error = false; |
183 bool error = false; |
184 error |= flib_ini_set_str(ini, "name", hog->name); |
184 error |= flib_ini_set_str(ini, "name", hog->name); |
185 error |= flib_ini_set_str(ini, "hat", hog->hat); |
185 error |= flib_ini_set_str(ini, "hat", hog->hat); |
186 error |= flib_ini_set_int(ini, "rounds", hog->rounds); |
186 error |= flib_ini_set_int(ini, "rounds", hog->rounds); |
187 error |= flib_ini_set_int(ini, "kills", hog->kills); |
187 error |= flib_ini_set_int(ini, "kills", hog->kills); |
188 error |= flib_ini_set_int(ini, "deaths", hog->deaths); |
188 error |= flib_ini_set_int(ini, "deaths", hog->deaths); |
189 error |= flib_ini_set_int(ini, "suicides", hog->suicides); |
189 error |= flib_ini_set_int(ini, "suicides", hog->suicides); |
190 if(error) { |
190 if(error) { |
191 return error; |
191 return error; |
192 } |
192 } |
193 } |
193 } |
194 return 0; |
194 return 0; |
195 } |
195 } |
196 |
196 |
197 static int writeBindingSection(const flib_team *team, flib_ini *ini) { |
197 static int writeBindingSection(const flib_team *team, flib_ini *ini) { |
198 if(team->bindingCount == 0) { |
198 if(team->bindingCount == 0) { |
199 return 0; |
199 return 0; |
200 } |
200 } |
201 if(flib_ini_create_section(ini, "binds")) { |
201 if(flib_ini_create_section(ini, "binds")) { |
202 return -1; |
202 return -1; |
203 } |
203 } |
204 for(int i=0; i<team->bindingCount; i++) { |
204 for(int i=0; i<team->bindingCount; i++) { |
205 bool error = false; |
205 bool error = false; |
206 char *action = flib_urlencode(team->bindings[i].action); |
206 char *action = flib_urlencode(team->bindings[i].action); |
207 if(action) { |
207 if(action) { |
208 error |= flib_ini_set_str(ini, action, team->bindings[i].binding); |
208 error |= flib_ini_set_str(ini, action, team->bindings[i].binding); |
209 free(action); |
209 free(action); |
210 } else { |
210 } else { |
211 error = true; |
211 error = true; |
212 } |
212 } |
213 if(error) { |
213 if(error) { |
214 return error; |
214 return error; |
215 } |
215 } |
216 } |
216 } |
217 return 0; |
217 return 0; |
218 } |
218 } |
219 |
219 |
220 int flib_team_to_ini(const char *filename, const flib_team *team) { |
220 int flib_team_to_ini(const char *filename, const flib_team *team) { |
221 int result = -1; |
221 int result = -1; |
222 if(!log_badargs_if2(filename==NULL, team==NULL)) { |
222 if(!log_badargs_if2(filename==NULL, team==NULL)) { |
223 flib_ini *ini = flib_ini_create(filename); |
223 flib_ini *ini = flib_ini_create(filename); |
224 bool error = false; |
224 bool error = false; |
225 error |= writeTeamSection(team, ini); |
225 error |= writeTeamSection(team, ini); |
226 error |= writeHogSections(team, ini); |
226 error |= writeHogSections(team, ini); |
227 error |= writeBindingSection(team, ini); |
227 error |= writeBindingSection(team, ini); |
228 if(!error) { |
228 if(!error) { |
229 result = flib_ini_save(ini, filename); |
229 result = flib_ini_save(ini, filename); |
230 } |
230 } |
231 flib_ini_destroy(ini); |
231 flib_ini_destroy(ini); |
232 } |
232 } |
233 return result; |
233 return result; |
234 } |
234 } |
235 |
235 |
236 int flib_team_set_weaponset(flib_team *team, const flib_weaponset *set) { |
236 int flib_team_set_weaponset(flib_team *team, const flib_weaponset *set) { |
237 if(team) { |
237 if(team) { |
238 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
238 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
239 flib_weaponset_destroy(team->hogs[i].weaponset); |
239 flib_weaponset_destroy(team->hogs[i].weaponset); |
240 team->hogs[i].weaponset = flib_weaponset_copy(set); |
240 team->hogs[i].weaponset = flib_weaponset_copy(set); |
241 if(set && !team->hogs[i].weaponset) { |
241 if(set && !team->hogs[i].weaponset) { |
242 return -1; |
242 return -1; |
243 } |
243 } |
244 } |
244 } |
245 } |
245 } |
246 return 0; |
246 return 0; |
247 } |
247 } |
248 |
248 |
249 void flib_team_set_health(flib_team *team, int health) { |
249 void flib_team_set_health(flib_team *team, int health) { |
250 if(team) { |
250 if(team) { |
251 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
251 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
252 team->hogs[i].initialHealth = health; |
252 team->hogs[i].initialHealth = health; |
253 } |
253 } |
254 } |
254 } |
255 } |
255 } |
256 |
256 |
257 static char *strdupWithError(const char *in, bool *error) { |
257 static char *strdupWithError(const char *in, bool *error) { |
258 char *out = flib_strdupnull(in); |
258 char *out = flib_strdupnull(in); |
259 if(in && !out) { |
259 if(in && !out) { |
260 *error = true; |
260 *error = true; |
261 } |
261 } |
262 return out; |
262 return out; |
263 } |
263 } |
264 |
264 |
265 flib_team *flib_team_copy(const flib_team *team) { |
265 flib_team *flib_team_copy(const flib_team *team) { |
266 flib_team *result = NULL; |
266 flib_team *result = NULL; |
267 if(team) { |
267 if(team) { |
268 flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team)); |
268 flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team)); |
269 if(tmpTeam) { |
269 if(tmpTeam) { |
270 bool error = false; |
270 bool error = false; |
271 |
271 |
272 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
272 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
273 tmpTeam->hogs[i].name = strdupWithError(team->hogs[i].name, &error); |
273 tmpTeam->hogs[i].name = strdupWithError(team->hogs[i].name, &error); |
274 tmpTeam->hogs[i].hat = strdupWithError(team->hogs[i].hat, &error); |
274 tmpTeam->hogs[i].hat = strdupWithError(team->hogs[i].hat, &error); |
275 tmpTeam->hogs[i].rounds = team->hogs[i].rounds; |
275 tmpTeam->hogs[i].rounds = team->hogs[i].rounds; |
276 tmpTeam->hogs[i].kills = team->hogs[i].kills; |
276 tmpTeam->hogs[i].kills = team->hogs[i].kills; |
277 tmpTeam->hogs[i].deaths = team->hogs[i].deaths; |
277 tmpTeam->hogs[i].deaths = team->hogs[i].deaths; |
278 tmpTeam->hogs[i].suicides = team->hogs[i].suicides; |
278 tmpTeam->hogs[i].suicides = team->hogs[i].suicides; |
279 tmpTeam->hogs[i].difficulty = team->hogs[i].difficulty; |
279 tmpTeam->hogs[i].difficulty = team->hogs[i].difficulty; |
280 tmpTeam->hogs[i].initialHealth = team->hogs[i].initialHealth; |
280 tmpTeam->hogs[i].initialHealth = team->hogs[i].initialHealth; |
281 tmpTeam->hogs[i].weaponset = flib_weaponset_copy(team->hogs[i].weaponset); |
281 tmpTeam->hogs[i].weaponset = flib_weaponset_copy(team->hogs[i].weaponset); |
282 if(team->hogs[i].weaponset && !tmpTeam->hogs[i].weaponset) { |
282 if(team->hogs[i].weaponset && !tmpTeam->hogs[i].weaponset) { |
283 error = true; |
283 error = true; |
284 } |
284 } |
285 } |
285 } |
286 |
286 |
287 tmpTeam->name = strdupWithError(team->name, &error); |
287 tmpTeam->name = strdupWithError(team->name, &error); |
288 tmpTeam->grave = strdupWithError(team->grave, &error); |
288 tmpTeam->grave = strdupWithError(team->grave, &error); |
289 tmpTeam->fort = strdupWithError(team->fort, &error); |
289 tmpTeam->fort = strdupWithError(team->fort, &error); |
290 tmpTeam->voicepack = strdupWithError(team->voicepack, &error); |
290 tmpTeam->voicepack = strdupWithError(team->voicepack, &error); |
291 tmpTeam->flag = strdupWithError(team->flag, &error); |
291 tmpTeam->flag = strdupWithError(team->flag, &error); |
292 tmpTeam->ownerName = strdupWithError(team->ownerName, &error); |
292 tmpTeam->ownerName = strdupWithError(team->ownerName, &error); |
293 |
293 |
294 tmpTeam->bindingCount = team->bindingCount; |
294 tmpTeam->bindingCount = team->bindingCount; |
295 if(team->bindings) { |
295 if(team->bindings) { |
296 tmpTeam->bindings = flib_calloc(team->bindingCount, sizeof(flib_binding)); |
296 tmpTeam->bindings = flib_calloc(team->bindingCount, sizeof(flib_binding)); |
297 if(tmpTeam->bindings) { |
297 if(tmpTeam->bindings) { |
298 for(int i=0; i<tmpTeam->bindingCount; i++) { |
298 for(int i=0; i<tmpTeam->bindingCount; i++) { |
299 tmpTeam->bindings[i].action = strdupWithError(team->bindings[i].action, &error); |
299 tmpTeam->bindings[i].action = strdupWithError(team->bindings[i].action, &error); |
300 tmpTeam->bindings[i].binding = strdupWithError(team->bindings[i].binding, &error); |
300 tmpTeam->bindings[i].binding = strdupWithError(team->bindings[i].binding, &error); |
301 } |
301 } |
302 } else { |
302 } else { |
303 error = true; |
303 error = true; |
304 } |
304 } |
305 } |
305 } |
306 |
306 |
307 tmpTeam->rounds = team->rounds; |
307 tmpTeam->rounds = team->rounds; |
308 tmpTeam->wins = team->wins; |
308 tmpTeam->wins = team->wins; |
309 tmpTeam->campaignProgress = team->campaignProgress; |
309 tmpTeam->campaignProgress = team->campaignProgress; |
310 |
310 |
311 tmpTeam->colorIndex = team->colorIndex; |
311 tmpTeam->colorIndex = team->colorIndex; |
312 tmpTeam->hogsInGame = team->hogsInGame; |
312 tmpTeam->hogsInGame = team->hogsInGame; |
313 tmpTeam->remoteDriven = team->remoteDriven; |
313 tmpTeam->remoteDriven = team->remoteDriven; |
314 |
314 |
315 if(!error) { |
315 if(!error) { |
316 result = tmpTeam; |
316 result = tmpTeam; |
317 tmpTeam = 0; |
317 tmpTeam = 0; |
318 } |
318 } |
319 } |
319 } |
320 flib_team_destroy(tmpTeam); |
320 flib_team_destroy(tmpTeam); |
321 } |
321 } |
322 return result; |
322 return result; |
323 } |
323 } |