Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: efa7bca52d4d43f18121de5110b125387a88b2fb (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
   core/application.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <errno.h>
#include <signal.h>

#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>

#include "sys/sys.h"
#include "math/mathlib.h"
#include "filesystem/filesystem.h"
#include "core/application.h"
#include "core/cvar.h"
#include "core/entity.h"
#include "core/func.h"
#include "core/gameconnection.h"
#include "core/gameserver.h"

namespace core
{

// --------------- engine functions  ------------------------------
void func_print(std::string const &args)
{
	con_print << args << "\n";
}

void func_help(std::string const &args)
{
	con_print << "This is the help function\n";
}

void func_quit(std::string const  &args)
{
	application()->shutdown();
	application()->quit(0);
}

void func_connect(std::string const &args)
{
	std::istringstream argstream(args);
	std::string host;
	if (!(argstream >> host))
		host.clear();

	application()->connect(host);
}

void func_disconnect(std::string const  &args)
{
	application()->disconnect();
}

/*
void func_name(std::string const &args) {
	std::istringstream argstream(args);
	std::string name;
	if (argstream >> name) {
		if (name.size() > 16) 
			name = name.substr(0,16);
	} else {
		con_print << "name " << Player::local.name() << "\n";
		return;
	}

	if (name == Player::local.name()) {
		con_print << "name " << name << "\n";
		return;
	}

	if (application()->netserver) {
		std::ostringstream osstream;
		osstream << "msg info " << Player::local.name() << " renamed to " << name << "\n";
		application()->netserver->broadcast(osstream.str());	

		con_print << "msg info " << Player::local.name() << " renamed to " << name << "\n";
	} else if (application()->netconnection.connected()) {
		std::ostringstream osstream;
		osstream << "name " << name << "\n";
		application()->netconnection.send(osstream.str());

		con_print << "name " << name << "\n";
	} else {
		con_print << "name " << name << "\n";
	}

	Player::local.player_name = name;
}
*/ 

// --------------- signal_handler -----------------------------------

extern "C" void signal_handler(int signum)
{
	switch (signum) {
		case SIGHUP:
		case SIGINT:
		case SIGQUIT:
		case SIGTERM:
			if (Application::instance()) {
				con_warn << "Received signal " << signum << ", shutting down...\n";
				application()->shutdown();
				application()->quit(0);
			} else {
				std::cerr << "Received signal " << signum << ", terminated...\n";
				application()->quit(1);
			}
			break;
		default:
			std::cerr << "Received signal " << signum << ", terminated...\n";
			application()->quit(1);
			break;
	}
}

// --------------- Application -----------------------------

Application *Application::application_instance = 0;

Application::Application()
{
	if (application_instance) {
		std::cerr << "multiple core::Application instances!\n";
		sys::quit(2);
	}

	application_instance = this;
	application_time = 0;
	application_game = 0;

	sys::signal(SIGHUP, signal_handler);
	sys::signal(SIGINT, signal_handler);
	sys::signal(SIGQUIT, signal_handler);
	sys::signal(SIGTERM, signal_handler);
}

Application::~Application()
{
	application_instance = 0;
}

void Application::init()
{
	con_debug << "Debug messages enabled\n";
	con_print << "Initializing core...\n";
	
	filesystem::init();

	CommandBuffer::init();
	// dedicated server has set this to 1
	Cvar::sv_dedicated = Cvar::get("sv_dedicated", "0", Cvar::ReadOnly);
	// client can set this to 1
	Cvar::sv_private = Cvar::get("sv_private", "0");

	// load save cvars
	load_config();

	// framerate settings
	Cvar::sv_framerate = Cvar::get("sv_framerate", "25");

	// network settings
	Cvar::net_host = Cvar::get("net_host", "0.0.0.0", Cvar::Archive);
	Cvar::net_port = Cvar::get("net_port", "8042", Cvar::Archive);
	Cvar::net_maxclients = Cvar::get("net_maxclients", "16", Cvar::Archive);
	
	// register our engine functions
	Func::add("print", func_print);
	Func::add("help", func_help);
	Func::add("quit", func_quit);
	
	Func::add("connect", func_connect);
	Func::add("disconnect", func_disconnect);
	
	//Func::add("name", func_name);
}

void Application::shutdown()
{
	con_print << "Shutting down core...\n";

	if (connected())
		disconnect();

	if (application_game) {
		delete application_game;
		application_game = 0;
	}

	save_config();

	// remove our engine functions
	Func::remove("print");
	Func::remove("help");
	Func::remove("quit");
	
	Func::remove("connect");
	Func::remove("disconnect");
	//Func::remove("name");

	CommandBuffer::shutdown();

	filesystem::shutdown();
}

void Application::quit(int status)
{
	sys::quit(status);
}


void Application::connect(std::string const &host)
{
	if (connected()) {
		con_warn << "Connected. Disconnect first.\n";
		return;
	}

	if (application_game) {
		delete application_game;
		application_game = 0;
	}
		
	if (host.size()) {
		application_game = new GameConnection(host);

		if (!application_game->running()) {
			delete application_game;
			application_game = 0;
		}
	} else {
		application_game = new GameServer();

		if (application_game->running()) {
			con_print << "Connected to local game.\n";
		} else {
			delete application_game;
			application_game = 0;
			con_warn << "Could not connect to local game!\n";
		}
	}
}

void Application::disconnect()
{

	if(application_game) {
		delete application_game;
		application_game = 0;
		con_print << "Disconnected.\n";
	}
}

void Application::frame(float seconds)
{
	// execute commands in the buffer
	CommandBuffer::exec();

	application_time += seconds;
	
	// don't run zero lenght time frames
	if (seconds == 0.0f)
		return;

	if (!connected())
		return;
	
	// run a game interface frame	
	application_game->frame(seconds);
			
	if (!application_game->running()) 
		disconnect();
}

void Application::save_config()
{
	std::string filename(filesystem::writedir);
	if (!Cvar::sv_dedicated->value())
		filename.append("client.cfg");
	else
		filename.append("server.cfg");
	std::ofstream ofs(filename.c_str());

	if (!ofs.is_open()) {
		con_warn << "Could not write " << filename << std::endl;
		return;
	}
	
	std::map<std::string, Cvar*>::iterator it;
	for (it = Cvar::registry.begin(); it != Cvar::registry.end(); it++) {

		if (((*it).second->flags() & Cvar::Archive) == Cvar::Archive)
			ofs << "set " << (*it).first << " " << (*it).second->str() << std::endl;	
	}
	ofs.close();
}

void Application::load_config()
{
	std::string filename(filesystem::writedir);
	if (!Cvar::sv_dedicated->value())
		filename.append("client.cfg");
	else
		filename.append("server.cfg");
	std::ifstream ifs(filename.c_str(), std::ifstream::in);

	if (!ifs.is_open()) {
		con_warn << "Could not read " << filename << std::endl;
		return;
	}

	char line[MAXCMDSIZE];
	while (ifs.getline(line, MAXCMDSIZE-1)) {
		cmd() << line << "\n";
		
	}
	
	// execute commands in the buffer
	CommandBuffer::exec();
}
}