/
c_wrapper.cpp
311 lines (271 loc) · 7.24 KB
/
c_wrapper.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2011-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "de/c_wrapper.h"
#include "de/LegacyCore"
#include "de/LegacyNetwork"
#include "de/Address"
#include "de/ByteRefArray"
#include "de/Block"
#include "de/LogBuffer"
#include "de/Info"
#include <QFile>
#include <cstring>
#include <stdarg.h>
#define DENG2_LEGACYNETWORK() de::LegacyCore::instance().network()
LegacyCore* LegacyCore_New(void* dengApp)
{
return reinterpret_cast<LegacyCore*>(new de::LegacyCore(reinterpret_cast<de::App*>(dengApp)));
}
void LegacyCore_Delete(LegacyCore* lc)
{
if(lc)
{
// It gets stopped automatically.
DENG2_SELF(LegacyCore, lc);
delete self;
}
}
int LegacyCore_RunEventLoop(LegacyCore* lc)
{
DENG2_SELF(LegacyCore, lc);
return self->runEventLoop();
}
void LegacyCore_Stop(LegacyCore *lc, int exitCode)
{
DENG2_SELF(LegacyCore, lc);
self->stop(exitCode);
}
void LegacyCore_SetLoopRate(LegacyCore* lc, int freqHz)
{
DENG2_SELF(LegacyCore, lc);
self->setLoopRate(freqHz);
}
void LegacyCore_SetLoopFunc(LegacyCore* lc, void (*callback)(void))
{
DENG2_SELF(LegacyCore, lc);
return self->setLoopFunc(callback);
}
void LegacyCore_PushLoop(LegacyCore* lc)
{
DENG2_SELF(LegacyCore, lc);
self->pushLoop();
}
void LegacyCore_PopLoop(LegacyCore* lc)
{
DENG2_SELF(LegacyCore, lc);
self->popLoop();
}
void LegacyCore_Timer(LegacyCore* lc, unsigned int milliseconds, void (*callback)(void))
{
DENG2_SELF(LegacyCore, lc);
self->timer(milliseconds, callback);
}
int LegacyCore_SetLogFile(LegacyCore* lc, const char* filePath)
{
try
{
DENG2_SELF(LegacyCore, lc);
self->setLogFileName(filePath);
return true;
}
catch(de::LogBuffer::FileError& er)
{
LOG_AS("LegacyCore_SetLogFile");
LOG_WARNING(er.asText());
return false;
}
}
const char* LegacyCore_LogFile(LegacyCore* lc)
{
DENG2_SELF(LegacyCore, lc);
return self->logFileName();
}
void LegacyCore_PrintLogFragment(LegacyCore* lc, const char* text)
{
DENG2_SELF(LegacyCore, lc);
self->printLogFragment(text);
}
void LegacyCore_PrintfLogFragmentAtLevel(LegacyCore* lc, legacycore_loglevel_t level, const char* format, ...)
{
char buffer[2048];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args); /// @todo unsafe
va_end(args);
// Validate the level.
de::Log::LogLevel logLevel = de::Log::LogLevel(level);
if(level < DE2_LOG_TRACE || level > DE2_LOG_CRITICAL)
{
logLevel = de::Log::MESSAGE;
}
DENG2_SELF(LegacyCore, lc);
self->printLogFragment(buffer, logLevel);
}
void LegacyCore_SetTerminateFunc(LegacyCore* lc, void (*func)(const char*))
{
DENG2_SELF(LegacyCore, lc);
self->setTerminateFunc(func);
}
void LogBuffer_Flush(void)
{
de::LogBuffer::appBuffer().flush();
}
void LogBuffer_EnableStandardOutput(int enable)
{
de::LogBuffer::appBuffer().enableStandardOutput(enable != 0);
}
int LegacyNetwork_OpenServerSocket(unsigned short port)
{
try
{
return DENG2_LEGACYNETWORK().openServerSocket(port);
}
catch(const de::Error& er)
{
LOG_AS("LegacyNetwork_OpenServerSocket");
LOG_WARNING(er.asText());
return 0;
}
}
int LegacyNetwork_Accept(int serverSocket)
{
return DENG2_LEGACYNETWORK().accept(serverSocket);
}
int LegacyNetwork_Open(const char* ipAddress, unsigned short port)
{
return DENG2_LEGACYNETWORK().open(de::Address(ipAddress, port));
}
void LegacyNetwork_GetPeerAddress(int socket, char* host, int hostMaxSize, unsigned short* port)
{
de::Address peer = DENG2_LEGACYNETWORK().peerAddress(socket);
std::memset(host, 0, hostMaxSize);
std::strncpy(host, peer.host().toString().toAscii().constData(), hostMaxSize - 1);
if(port) *port = peer.port();
}
void LegacyNetwork_Close(int socket)
{
DENG2_LEGACYNETWORK().close(socket);
}
int LegacyNetwork_Send(int socket, const void* data, int size)
{
return DENG2_LEGACYNETWORK().sendBytes(
socket, de::ByteRefArray(reinterpret_cast<const de::IByteArray::Byte*>(data), size));
}
unsigned char* LegacyNetwork_Receive(int socket, int *size)
{
de::Block data;
if(DENG2_LEGACYNETWORK().receiveBlock(socket, data))
{
// Successfully got a block of data. Return a copy of it.
unsigned char* buffer = new unsigned char[data.size()];
std::memcpy(buffer, data.constData(), data.size());
*size = data.size();
return buffer;
}
else
{
// We did not receive anything.
*size = 0;
return 0;
}
}
void LegacyNetwork_FreeBuffer(unsigned char* buffer)
{
delete [] buffer;
}
int LegacyNetwork_IsDisconnected(int socket)
{
if(!socket) return 0;
return !DENG2_LEGACYNETWORK().isOpen(socket);
}
int LegacyNetwork_BytesReady(int socket)
{
if(!socket) return 0;
return DENG2_LEGACYNETWORK().incomingForSocket(socket);
}
int LegacyNetwork_NewSocketSet()
{
return DENG2_LEGACYNETWORK().newSocketSet();
}
void LegacyNetwork_DeleteSocketSet(int set)
{
DENG2_LEGACYNETWORK().deleteSocketSet(set);
}
void LegacyNetwork_SocketSet_Add(int set, int socket)
{
DENG2_LEGACYNETWORK().addToSet(set, socket);
}
void LegacyNetwork_SocketSet_Remove(int set, int socket)
{
DENG2_LEGACYNETWORK().removeFromSet(set, socket);
}
int LegacyNetwork_SocketSet_Activity(int set)
{
if(!set) return 0;
return DENG2_LEGACYNETWORK().checkSetForActivity(set);
}
Info* Info_NewFromString(const char* utf8text)
{
try
{
QScopedPointer<de::Info> self(new de::Info);
self->parse(QString::fromUtf8(utf8text));
return reinterpret_cast<Info*>(self.take());
}
catch(de::Error& er)
{
LOG_WARNING(er.asText());
}
return 0;
}
Info* Info_NewFromFile(const char* nativePath)
{
QFile file(nativePath);
if(file.open(QFile::ReadOnly | QFile::Text))
{
return Info_NewFromString(file.readAll().constData());
}
return 0;
}
void Info_Delete(Info* info)
{
if(info)
{
DENG2_SELF(Info, info);
delete self;
}
}
int Info_FindValue(Info* info, const char* path, char* buffer, size_t bufSize)
{
if(!info) return false;
DENG2_SELF(Info, info);
const de::Info::Element* element = self->findByPath(path);
if(!element->isKey()) return false;
QString value = static_cast<const de::Info::KeyElement*>(element)->value();
if(buffer)
{
qstrncpy(buffer, value.toUtf8().constData(), bufSize);
return true;
}
else
{
// Just return the size of the value.
return value.size();
}
}