forked from alliedmodders/stripper-source
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupport.cpp
More file actions
217 lines (187 loc) · 3.65 KB
/
support.cpp
File metadata and controls
217 lines (187 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/** vim: set ts=4 sw=4 et tw=99:
*
* === Stripper for Metamod:Source ===
* Copyright (C) 2005-2009 David "BAILOPAN" Anderson
* No warranties of any kind.
* Based on the original concept of Stripper2 by botman
*
* License: see LICENSE.TXT
* ===================================
*/
#include <new>
#include <stdio.h>
#include "support.h"
#include "parser.h"
#if defined WIN32
#include <windows.h>
#include <direct.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
Stripper g_Stripper;
stripper_game_t stripper_game;
#if defined _WIN32
char *strchrnul(const char *str, int c)
{
while(*str)
{
if (c == *str) break;
str++;
}
return const_cast<char*>(str);
}
#define EXPORT extern "C" __declspec(dllexport)
#elif defined __GNUC__
#if __GNUC__ == 4
#define EXPORT extern "C" __attribute__ ((visibility("default")))
#else
#define EXPORT extern "C"
#endif
#endif
static const char*
parse_map(const char* map, const char* entities)
{
FILE* fp;
char path[256];
g_Stripper.SetEntityList(entities);
stripper_game.path_format(path,
sizeof(path),
"%s/%s/global_filters.cfg",
stripper_game.game_path,
stripper_game.stripper_cfg_path);
fp = fopen(path, "rt");
if (fp == NULL)
{
stripper_game.log_message("Could not find global filter file: %s", path);
}
else
{
fclose(fp);
g_Stripper.ApplyFileFilter(path);
}
for (const char* pPart = map; *pPart++ != '\0';)
{
pPart = strchrnul(pPart, '_');
stripper_game.path_format(path, sizeof(path), "%s/%s/maps/%.*s.cfg",
stripper_game.game_path, stripper_game.stripper_cfg_path, pPart - map, map);
fp = fopen(path, "rt");
if (fp)
{
fclose(fp);
g_Stripper.ApplyFileFilter(path);
}
}
return g_Stripper.ToString();
}
static const char*
ent_string()
{
return g_Stripper.ToString();
}
static void
command_dump()
{
char path[255];
stripper_game.path_format(path,
sizeof(path),
"%s/%s/dumps",
stripper_game.game_path,
stripper_game.stripper_path);
#ifdef WIN32
DWORD attr = GetFileAttributes(path);
if ((attr = INVALID_FILE_ATTRIBUTES) || (!(attr & FILE_ATTRIBUTE_DIRECTORY)))
{
unlink(path);
mkdir(path);
}
#else
struct stat s;
if (stat(path, &s) != 0)
{
mkdir(path, 0775);
}
else
{
if (!(S_ISDIR(s.st_mode)))
{
unlink(path);
mkdir(path, 0775);
}
}
#endif
int num = 0;
char file[255];
char* szMapName = (char*)stripper_game.get_map_name();
//Clean the mapname
for (int i = strlen(szMapName); i >= 0; i--)
{
if (szMapName[i] == '\\' || szMapName[i] == '/')
{
szMapName += (i + 1);
break;
}
}
do
{
stripper_game.path_format(file,
sizeof(file),
"%s/%s.%04d.cfg",
path,
szMapName,
num);
FILE* fp = fopen(file, "rt");
if (!fp)
break;
fclose(fp);
} while (++num);
FILE* fp = fopen(file, "wt");
if (!fp)
{
stripper_game.log_message("Failed to create dump file %s", file);
return;
}
fprintf(fp, "%s", g_Stripper.ToString());
fclose(fp);
stripper_game.log_message("Logged map %s to file %s", szMapName, file);
}
static void
plugin_unload()
{
}
static stripper_core_t stripper_core =
{
parse_map,
ent_string,
command_dump,
plugin_unload
};
EXPORT void
LoadStripper(const stripper_game_t* game, stripper_core_t* core)
{
memcpy(&stripper_game, game, sizeof(stripper_game_t));
memcpy(core, &stripper_core, sizeof(stripper_core_t));
}
/* Overload a few things to prevent libstdc++ linking */
#if defined __linux__ || defined __APPLE__
extern "C" void __cxa_pure_virtual(void)
{
}
void* operator new(size_t size)
{
return malloc(size);
}
void* operator new[](size_t size)
{
return malloc(size);
}
void operator delete(void* ptr)
{
free(ptr);
}
void operator delete[](void* ptr)
{
free(ptr);
}
#endif