Debian Codegraph @

src/cdogs/player_template.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2014, 2016-2020, 2023 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "player_template.h"
30
31 #include <json/json.h>
32
33 #include <cdogs/character.h>
34 #include <cdogs/files.h>
35 #include <cdogs/json_utils.h>
36 #include <cdogs/log.h>
37
38 #ifdef __EMSCRIPTEN__
39 #include <emscripten.h>
40 #endif
41
42 #define VERSION 4
43
44 PlayerTemplates gPlayerTemplates;
45
46 static void LoadPlayerTemplate(
47 CArray *templates, json_t *node, const int version)
48 {
49 PlayerTemplate t;
50 memset(&t, 0, sizeof t);
51 strncpy(
52 t.name, json_find_first_label(node, "Name")->child->text,
53 PLAYER_NAME_MAXLEN - 1);
54 t.CharClassName = GetString(node, "Face");
55 // Hair
56 if (version < 3)
57 {
58 char *face;
59 CSTRDUP(face, t.CharClassName);
60 CharacterOldFaceToHeadParts(t.CharClassName, &t.CharClassName, t.HeadParts);
61 CFREE(face);
62 }
63 else
64 {
65 LoadStr(&t.HeadParts[HEAD_PART_HAIR], node, "HairType");
66 if (version < 4)
67 {
68 CharacterOldHairToHeadParts(t.HeadParts);
69 }
70 else
71 {
72 LoadStr(&t.HeadParts[HEAD_PART_FACEHAIR], node, "FacehairType");
73 LoadStr(&t.HeadParts[HEAD_PART_HAT], node, "HatType");
74 LoadStr(&t.HeadParts[HEAD_PART_GLASSES], node, "GlassesType");
75 }
76 }
77 // Colors
78 if (version == 1)
79 {
80 // Version 1 used integer palettes
81 int skin, arms, body, legs, hair;
82 LoadInt(&skin, node, "Skin");
83 LoadInt(&arms, node, "Arms");
84 LoadInt(&body, node, "Body");
85 LoadInt(&legs, node, "Legs");
86 LoadInt(&hair, node, "Hair");
87 ConvertCharacterColors(skin, arms, body, legs, hair, &t.Colors);
88 }
89 else
90 {
91 LoadColor(&t.Colors.Skin, node, "Skin");
92 LoadColor(&t.Colors.Arms, node, "Arms");
93 LoadColor(&t.Colors.Body, node, "Body");
94 LoadColor(&t.Colors.Legs, node, "Legs");
95 LoadColor(&t.Colors.Hair, node, "Hair");
96 }
97 if (version < 3)
98 {
99 t.Colors.Feet = t.Colors.Legs;
100 }
101 LoadColor(&t.Colors.Feet, node, "Feet");
102 if (version < 4)
103 {
104 t.Colors.Facehair = t.Colors.Hat = t.Colors.Glasses = t.Colors.Hair;
105 }
106 LoadColor(&t.Colors.Facehair, node, "Facehair");
107 LoadColor(&t.Colors.Hat, node, "Hat");
108 LoadColor(&t.Colors.Glasses, node, "Glasses");
109
110 CArrayPushBack(templates, &t);
111 LOG(LM_MAIN, LL_DEBUG, "loaded player template %s (%s)", t.name,
112 t.CharClassName);
113 }
114
115 void PlayerTemplatesLoad(PlayerTemplates *pt, const CharacterClasses *classes)
116 {
117 // Note: not used, but included in function to express dependency
118 CASSERT(
119 classes->Classes.size > 0,
120 "cannot load player templates without character classes");
121 json_t *root = NULL;
122
123 CArrayInit(&pt->Classes, sizeof(PlayerTemplate));
124 CArrayInit(&pt->CustomClasses, sizeof(PlayerTemplate));
125 FILE *f = fopen(GetConfigFilePath(PLAYER_TEMPLATE_FILE), "r");
126 if (!f)
127 {
128 LOG(LM_MAIN, LL_WARN, "player templates file missing '%s'",
129 PLAYER_TEMPLATE_FILE);
130 goto bail;
131 }
132
133 if (json_stream_parse(f, &root) != JSON_OK)
134 {
135 LOG(LM_MAIN, LL_ERROR, "parsing player templates '%s'",
136 PLAYER_TEMPLATE_FILE);
137 goto bail;
138 }
139
140 PlayerTemplatesLoadJSON(&pt->Classes, root);
141
142 bail:
143 json_free_value(&root);
144 if (f != NULL)
145 {
146 fclose(f);
147 }
148 }
149
150 void PlayerTemplatesLoadJSON(CArray *classes, json_t *node)
151 {
152 int version = 1;
153 LoadInt(&version, node, "Version");
154
155 if (json_find_first_label(node, "PlayerTemplates") == NULL)
156 {
157 LOG(LM_MAIN, LL_ERROR, "unknown player templates format");
158 return;
159 }
160 json_t *child =
161 json_find_first_label(node, "PlayerTemplates")->child->child;
162 while (child != NULL)
163 {
164 LoadPlayerTemplate(classes, child, version);
165 child = child->next;
166 }
167 }
168
169 void PlayerTemplatesClear(CArray *classes)
170 {
171 CA_FOREACH(PlayerTemplate, pt, *classes)
172 CFREE(pt->CharClassName);
173 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
174 {
175 CFREE(pt->HeadParts[hp]);
176 }
177 CA_FOREACH_END()
178 CArrayClear(classes);
179 }
180
181 void PlayerTemplatesTerminate(PlayerTemplates *pt)
182 {
183 PlayerTemplatesClear(&pt->Classes);
184 CArrayTerminate(&pt->Classes);
185 PlayerTemplatesClear(&pt->CustomClasses);
186 CArrayTerminate(&pt->Classes);
187 }
188
189 PlayerTemplate *PlayerTemplateGetById(PlayerTemplates *pt, const int id)
190 {
191 if (id < (int)pt->CustomClasses.size)
192 {
193 return CArrayGet(&pt->CustomClasses, id);
194 }
195 else if (id < (int)(pt->CustomClasses.size + pt->Classes.size))
196 {
197 return CArrayGet(&pt->Classes, id - (int)pt->CustomClasses.size);
198 }
199 return NULL;
200 }
201
202 static void SavePlayerTemplate(const PlayerTemplate *t, json_t *templates)
203 {
204 json_t *node = json_new_object();
205 AddStringPair(node, "Name", t->name);
206 AddStringPair(node, "Face", t->CharClassName);
207 if (t->HeadParts[HEAD_PART_HAIR])
208 {
209 AddStringPair(node, "HairType", t->HeadParts[HEAD_PART_HAIR]);
210 }
211 if (t->HeadParts[HEAD_PART_FACEHAIR])
212 {
213 AddStringPair(node, "FacehairType", t->HeadParts[HEAD_PART_FACEHAIR]);
214 }
215 if (t->HeadParts[HEAD_PART_HAT])
216 {
217 AddStringPair(node, "HatType", t->HeadParts[HEAD_PART_HAT]);
218 }
219 if (t->HeadParts[HEAD_PART_GLASSES])
220 {
221 AddStringPair(node, "GlassesType", t->HeadParts[HEAD_PART_GLASSES]);
222 }
223 AddColorPair(node, "Body", t->Colors.Body);
224 AddColorPair(node, "Arms", t->Colors.Arms);
225 AddColorPair(node, "Legs", t->Colors.Legs);
226 AddColorPair(node, "Skin", t->Colors.Skin);
227 AddColorPair(node, "Hair", t->Colors.Hair);
228 AddColorPair(node, "Facehair", t->Colors.Facehair);
229 AddColorPair(node, "Hat", t->Colors.Hat);
230 AddColorPair(node, "Glasses", t->Colors.Glasses);
231 AddColorPair(node, "Feet", t->Colors.Feet);
232 json_insert_child(templates, node);
233 }
234 void PlayerTemplatesSave(const PlayerTemplates *pt)
235 {
236 FILE *f = fopen(GetConfigFilePath(PLAYER_TEMPLATE_FILE), "w");
237 char *text = NULL;
238 char *formatText = NULL;
239 json_t *root = json_new_object();
240
241 if (f == NULL)
242 {
243 LOG(LM_MAIN, LL_ERROR, "saving player templates '%s'",
244 PLAYER_TEMPLATE_FILE);
245 goto bail;
246 }
247
248 json_insert_pair_into_object(
249 root, "Version", json_new_number(TOSTRING(VERSION)));
250 json_t *templatesNode = json_new_array();
251 CA_FOREACH(const PlayerTemplate, t, pt->Classes)
252 SavePlayerTemplate(t, templatesNode);
253 CA_FOREACH_END()
254 json_insert_pair_into_object(root, "PlayerTemplates", templatesNode);
255
256 json_tree_to_string(root, &text);
257 formatText = json_format_string(text);
258 fputs(formatText, f);
259
260 bail:
261 // clean up
262 free(formatText);
263 free(text);
264 json_free_value(&root);
265 if (f != NULL)
266 {
267 fclose(f);
268 #ifdef __EMSCRIPTEN__
269 EM_ASM(
270 // persist changes
271 FS.syncfs(
272 false, function(err) { assert(!err); }););
273 #endif
274 }
275 }
276
277 void PlayerTemplateToPlayerData(PlayerData *p, const PlayerTemplate *t)
278 {
279 memset(p->name, 0, sizeof p->name);
280 strcpy(p->name, t->name);
281 p->Char.Class = StrCharacterClass(t->CharClassName);
282 if (p->Char.Class == NULL)
283 {
284 p->Char.Class = StrCharacterClass("Jones");
285 }
286 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
287 {
288 CFREE(p->Char.HeadParts[hp]);
289 p->Char.HeadParts[hp] = NULL;
290 if (t->HeadParts[hp])
291 {
292 CSTRDUP(p->Char.HeadParts[hp], t->HeadParts[hp]);
293 }
294 }
295 p->Char.Colors = t->Colors;
296 }
297
298 static void PlayerTemplateFromCharacter(PlayerTemplate *t, const Character *c)
299 {
300 CFREE(t->CharClassName);
301 CSTRDUP(t->CharClassName, c->Class->Name);
302 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
303 {
304 if (c->HeadParts[hp])
305 {
306 CFREE(t->HeadParts[hp]);
307 CSTRDUP(t->HeadParts[hp], c->HeadParts[hp]);
308 }
309 }
310 t->Colors = c->Colors;
311 }
312
313 void PlayerTemplateFromPlayerData(PlayerTemplate *t, const PlayerData *p)
314 {
315 memset(t->name, 0, sizeof t->name);
316 strcpy(t->name, p->name);
317 PlayerTemplateFromCharacter(t, &p->Char);
318 }
319
320 void PlayerTemplateAddCharacter(CArray *classes, const Character *c)
321 {
322 PlayerTemplate t;
323 memset(&t, 0, sizeof t);
324 strncpy(t.name, c->PlayerTemplateName, PLAYER_NAME_MAXLEN - 1);
325 PlayerTemplateFromCharacter(&t, c);
326 CArrayPushBack(classes, &t);
327 LOG(LM_MAIN, LL_DEBUG, "loaded player template from characters %s (%s)",
328 t.name, t.CharClassName);
329 }