/
localserver.cpp
171 lines (144 loc) · 4.88 KB
/
localserver.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
/** @file localserver.cpp Starting and stopping local servers.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/shell/LocalServer"
#include "de/shell/Link"
#include "de/shell/DoomsdayInfo"
#include <de/CommandLine>
#include <QCoreApplication>
#include <QDir>
namespace de {
namespace shell {
static String const ERROR_LOG_NAME = "doomsday-errors.out";
DENG2_PIMPL_NOREF(LocalServer)
{
Link *link;
duint16 port;
String name;
NativePath userDir;
Instance() : link(0), port(0) {}
};
LocalServer::LocalServer() : d(new Instance)
{}
void LocalServer::setName(String const &name)
{
d->name = name;
d->name.replace("\"", "\\\""); // for use on command line
}
void LocalServer::start(duint16 port,
String const &gameMode,
QStringList additionalOptions,
NativePath const &runtimePath)
{
d->port = port;
d->userDir = runtimePath;
if(d->userDir.isEmpty())
{
// Default runtime location.
d->userDir = DoomsdayInfo::defaultServerRuntimeFolder();
}
// Get rid of a previous error log in this location.
QDir(d->userDir).remove(ERROR_LOG_NAME);
DENG2_ASSERT(d->link == 0);
CommandLine cmd;
#ifdef MACOSX
// First locate the server executable.
NativePath bin = NativePath(qApp->applicationDirPath()) / "../Resources/doomsday-server";
if(!bin.exists())
{
// Try another location: Doomsday-Shell.app -> Doomsday Engine.app/Contents/Doomsday.app
bin = NativePath(qApp->applicationDirPath()) /
"../../../Doomsday Engine.app/Contents/Doomsday.app/Contents/Resources/doomsday-server";
}
if(!bin.exists())
{
// Yet another possibility: Doomsday-Shell.app -> Doomsday.app
bin = NativePath(qApp->applicationDirPath()) /
"../../../Doomsday.app/Contents/Resources/doomsday-server";
}
if(!bin.exists())
{
// Undeployed, e.g., a developer build.
bin = NativePath(qApp->applicationDirPath()) /
"../../../../../../client/Doomsday.app/Contents/Resources/doomsday-server";
}
if(!bin.exists())
{
throw NotFoundError("LocalServer::start", "Could not find Doomsday.app");
}
/**
* @todo These options will be much simpler when libdeng2 FS is used for
* all file access.
*/
cmd.append(bin);
cmd.append("-vdmap");
cmd.append("..");
cmd.append("}Data");
cmd.append("-basedir");
cmd.append(bin.fileNamePath() / "../Resources");
String plugDir = bin.fileNamePath() / "../DengPlugins";
cmd.append("-vdmap");
cmd.append(plugDir / "doom.bundle/Contents/Resources");
cmd.append("}Data/jDoom/");
cmd.append("-vdmap");
cmd.append(plugDir / "heretic.bundle/Contents/Resources");
cmd.append("}Data/jHeretic/");
cmd.append("-vdmap");
cmd.append(plugDir / "hexen.bundle/Contents/Resources");
cmd.append("}Data/jHexen/");
#elif WIN32
NativePath bin = NativePath(qApp->applicationDirPath()) / "doomsday-server.exe";
cmd.append(bin);
cmd.append("-basedir");
cmd.append(bin.fileNamePath() / "..");
#else // UNIX
NativePath bin = NativePath(qApp->applicationDirPath()) / "doomsday-server";
if(!bin.exists()) bin = "doomsday-server"; // Perhaps it's on the path?
cmd.append(bin);
#endif
cmd.append("-userdir");
cmd.append(d->userDir);
cmd.append("-errors");
cmd.append(ERROR_LOG_NAME);
cmd.append("-game");
cmd.append(gameMode);
cmd.append("-cmd");
cmd.append("net-ip-port " + String::number(port));
if(!d->name.isEmpty())
{
cmd.append("-cmd");
cmd.append("server-name \"" + d->name + "\"");
}
foreach(String opt, additionalOptions) cmd.append(opt);
LOG_NET_NOTE("Starting local server with port %i using game mode '%s'")
<< port << gameMode;
cmd.execute();
}
void LocalServer::stop()
{
DENG2_ASSERT(d->link != 0);
}
Link *LocalServer::openLink()
{
return new Link(String("localhost:%1").arg(d->port), 30);
}
NativePath LocalServer::errorLogPath() const
{
return d->userDir / ERROR_LOG_NAME;
}
} // namespace shell
} // namespace de