1 #include "team.h" |
1 #include "team.h" |
|
2 |
|
3 #include "../util/inihelper.h" |
|
4 #include "../util/util.h" |
|
5 #include "../util/logging.h" |
|
6 |
|
7 static flib_team *from_ini_handleError(flib_team *result, dictionary *settingfile, char **bindingKeys) { |
|
8 if(settingfile) { |
|
9 iniparser_freedict(settingfile); |
|
10 } |
|
11 flib_team_destroy(result); |
|
12 free(bindingKeys); |
|
13 return NULL; |
|
14 } |
|
15 |
|
16 flib_team *flib_team_from_ini(const char *filename) { |
|
17 flib_team *result = flib_calloc(1, sizeof(flib_team)); |
|
18 dictionary *settingfile = NULL; |
|
19 char **bindingKeys = NULL; |
|
20 |
|
21 if(!filename) { |
|
22 flib_log_e("null parameter in flib_team_from_ini"); |
|
23 return from_ini_handleError(result, settingfile, bindingKeys); |
|
24 } |
|
25 |
|
26 if(!result) { |
|
27 return from_ini_handleError(result, settingfile, bindingKeys); |
|
28 } |
|
29 |
|
30 settingfile = iniparser_load(filename); |
|
31 if(!settingfile) { |
|
32 flib_log_e("Error loading team file %s", filename); |
|
33 return from_ini_handleError(result, settingfile, bindingKeys); |
|
34 } |
|
35 |
|
36 bool error = false; |
|
37 result->name = inihelper_getstringdup(settingfile, &error, "team", "name"); |
|
38 result->grave = inihelper_getstringdup(settingfile, &error, "team", "grave"); |
|
39 result->fort = inihelper_getstringdup(settingfile, &error, "team", "fort"); |
|
40 result->voicepack = inihelper_getstringdup(settingfile, &error, "team", "voicepack"); |
|
41 result->flag = inihelper_getstringdup(settingfile, &error, "team", "flag"); |
|
42 result->rounds = inihelper_getint(settingfile, &error, "team", "rounds"); |
|
43 result->wins = inihelper_getint(settingfile, &error, "team", "wins"); |
|
44 result->campaignProgress = inihelper_getint(settingfile, &error, "team", "campaignprogress"); |
|
45 int difficulty = inihelper_getint(settingfile, &error, "team", "difficulty"); |
|
46 |
|
47 char sectionName[10]; |
|
48 strcpy(sectionName, "hedgehog0"); |
|
49 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
|
50 sectionName[8] = '0'+i; |
|
51 result->hogs[i].name = inihelper_getstringdup(settingfile, &error, sectionName, "name"); |
|
52 result->hogs[i].hat = inihelper_getstringdup(settingfile, &error, sectionName, "hat"); |
|
53 result->hogs[i].rounds = inihelper_getint(settingfile, &error, sectionName, "rounds"); |
|
54 result->hogs[i].kills = inihelper_getint(settingfile, &error, sectionName, "kills"); |
|
55 result->hogs[i].deaths = inihelper_getint(settingfile, &error, sectionName, "deaths"); |
|
56 result->hogs[i].suicides = inihelper_getint(settingfile, &error, sectionName, "suicides"); |
|
57 result->hogs[i].difficulty = difficulty; |
|
58 result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH; |
|
59 } |
|
60 |
|
61 result->bindingCount = iniparser_getsecnkeys(settingfile, "binds"); |
|
62 result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding)); |
|
63 bindingKeys = iniparser_getseckeys(settingfile, "binds"); |
|
64 if(!result->bindings || !bindingKeys) { |
|
65 return from_ini_handleError(result, settingfile, bindingKeys); |
|
66 } |
|
67 |
|
68 for(int i=0; i<result->bindingCount; i++) { |
|
69 result->bindings[i].binding = flib_strdupnull(iniparser_getstring(settingfile, bindingKeys[i], NULL)); |
|
70 // The key names all start with "binds:", so we skip that. |
|
71 result->bindings[i].action = inihelper_urldecode(bindingKeys[i]+strlen("binds:")); |
|
72 if(!result->bindings[i].action || !result->bindings[i].binding) { |
|
73 error = true; |
|
74 } |
|
75 } |
|
76 |
|
77 if(error) { |
|
78 flib_log_e("Error reading team file %s", filename); |
|
79 return from_ini_handleError(result, settingfile, bindingKeys); |
|
80 } |
|
81 |
|
82 iniparser_freedict(settingfile); |
|
83 free(bindingKeys); |
|
84 return result; |
|
85 } |
|
86 |
|
87 void flib_team_destroy(flib_team *team) { |
|
88 if(team) { |
|
89 for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { |
|
90 free(team->hogs[i].name); |
|
91 free(team->hogs[i].hat); |
|
92 } |
|
93 free(team->name); |
|
94 free(team->grave); |
|
95 free(team->fort); |
|
96 free(team->voicepack); |
|
97 free(team->flag); |
|
98 if(team->bindings) { |
|
99 for(int i=0; i<team->bindingCount; i++) { |
|
100 free(team->bindings[i].action); |
|
101 free(team->bindings[i].binding); |
|
102 } |
|
103 } |
|
104 free(team->bindings); |
|
105 free(team->hash); |
|
106 flib_weaponset_destroy(team->weaponset); |
|
107 free(team); |
|
108 } |
|
109 } |