Debian Codegraph @

src/cdogs/campaign_entry.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, 2020-2021 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 "campaign_entry.h"
30
31 #include <stdio.h>
32
33 #include <cdogs/cwolfmap/cwolfmap.h>
34 #include <cdogs/files.h>
35 #include <cdogs/map_new.h>
36 #include <cdogs/map_wolf.h>
37 #include <cdogs/mission.h>
38 #include <cdogs/utils.h>
39
40 bool IsCampaignOK(const char *path, char **title, int *numMissions)
41 {
42 if (strcmp(StrGetFileExt(path), "cdogscpn") == 0 ||
43 strcmp(StrGetFileExt(path), "CDOGSCPN") == 0)
44 {
45 return MapNewScanArchive(path, title, numMissions) == 0;
46 }
47 if (IsCampaignOldFile(path))
48 {
49 return ScanCampaignOld(path, title, numMissions) == 0;
50 }
51 int spearMission = 1;
52 char buf[CDOGS_PATH_MAX];
53 if (path[strlen(path)-2] == '?')
54 {
55 spearMission = atoi(&path[strlen(path) - 1]);
56 strcpy(buf, path);
57 buf[strlen(buf) - 2] = '\0';
58 path = buf;
59 }
60 if (CWGetType(path, NULL, NULL, spearMission) != CWMAPTYPE_UNKNOWN)
61 {
62 return MapWolfScan(path, spearMission, title, numMissions) == 0;
63 }
64
65 return false;
66 }
67
68 void CampaignEntryInit(CampaignEntry *entry, const char *title, GameMode mode)
69 {
70 memset(entry, 0, sizeof *entry);
71 CSTRDUP(entry->Info, title);
72 entry->Mode = mode;
73 }
74 void CampaignEntryCopy(CampaignEntry *dst, const CampaignEntry *src)
75 {
76 memcpy(dst, src, sizeof *dst);
77 if (src->Filename)
78 CSTRDUP(dst->Filename, src->Filename);
79 if (src->Path)
80 CSTRDUP(dst->Path, src->Path);
81 if (src->Info)
82 CSTRDUP(dst->Info, src->Info);
83 }
84 bool CampaignEntryTryLoad(
85 CampaignEntry *entry, const char *path, GameMode mode)
86 {
87 char *buf;
88 int numMissions;
89 if (!IsCampaignOK(path, &buf, &numMissions))
90 {
91 return false;
92 }
93 // cap length of title
94 size_t maxLen = 70;
95 if (strlen(buf) > maxLen)
96 {
97 buf[maxLen] = '\0';
98 }
99 char title[256];
100 sprintf(title, "%s (%d)", buf, numMissions);
101 CampaignEntryInit(entry, title, mode);
102 CSTRDUP(entry->Filename, PathGetBasename(path));
103 // Get relative path for the campaign entry, so when we transmit it to
104 // network clients they can load it regardless of install path
105 // Note: relative path is counted from data dir
106 char pathBuf[CDOGS_PATH_MAX];
107 char dataDirBuf[CDOGS_PATH_MAX];
108 GetDataFilePath(dataDirBuf, "");
109 RelPath(pathBuf, path, dataDirBuf);
110 CSTRDUP(entry->Path, pathBuf);
111 entry->NumMissions = numMissions;
112 CFREE(buf);
113 return true;
114 }
115 void CampaignEntryTerminate(CampaignEntry *entry)
116 {
117 CFREE(entry->Filename);
118 CFREE(entry->Path);
119 CFREE(entry->Info);
120 memset(entry, 0, sizeof *entry);
121 }