kpmpgsmkii/server/src/main.c
2021-11-21 21:22:15 -06:00

153 lines
3.9 KiB
C

/*
* Kangaroo Punch MultiPlayer Game Server Mark II
* Copyright (C) 2020-2021 Scott Duensing
*
* 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 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 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 <https://www.gnu.org/licenses/>.
*
*/
#include "os.h"
#include "util.h"
#include "stddclmr.h"
#include "thirdparty/ini/src/ini.h"
#include "database.h"
#include "array.h"
typedef struct HashValueS {
char *string;
int32_t integer;
} HashValueT;
typedef struct HashMapS {
char *key;
HashValueT value;
} HashMapT;
static char *_configServer = NULL;
static uint16_t _configPort = 0;
static char *_configDatabase = NULL;
static char *_configUser = NULL;
static char *_configPassword = NULL;
static HashMapT *_settings = NULL;
static void configRead(char *file);
static void configWrite(char *file);
static void configRead(char *file) {
ini_t *ini = NULL;
// Numeric defaults.
_configPort = 16550;
ini = ini_load(file);
if (ini) {
ini_sget(ini, "SERVER", "PORT", "%d", &_configPort);
_configServer = strdup(ini_get(ini, "SERVER", "HOST"));
_configDatabase = strdup(ini_get(ini, "SERVER", "DATA"));
_configUser = strdup(ini_get(ini, "SERVER", "USER"));
_configPassword = strdup(ini_get(ini, "SERVER", "PASS"));
ini_free(ini);
}
// String defaults.
if (!_configServer) strdup("kanga.world");
if (!_configDatabase) strdup("kpmpgsmkii");
if (!_configUser) strdup("");
if (!_configPassword) strdup("");
}
static void configWrite(char *file) {
FILE *out = NULL;
out = fopen(file, "w");
if (out) {
fprintf(out,
"[SERVER]\n"
"HOST=%s\n"
"PORT=%d\n"
"DATA=%s\n"
"USER=%s\n"
"PASS=%s\n",
_configServer,
_configPort,
_configDatabase,
_configUser,
_configPassword
);
fclose(out);
}
DEL(_configServer);
DEL(_configDatabase);
DEL(_configUser);
DEL(_configPassword);
}
int main(int argc, char *argv[]) {
char *configFile = NULL;
HashValueT tempValue;
(void)argc;
memoryStartup(argv[0]);
logOpenByHandle(memoryLogHandleGet());
configFile = utilAppNameWithNewExtensionGet(argv[0], "ini");
configRead(configFile);
// 0 1 2 3 4 5 6 7 8
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
logWrite("%s", "Kangaroo Punch MultiPlayer DOS Game Server Mark II\n");
logWrite("%s", "Copyright (C) 2020-2021 Scott Duensing scott@kangaroopunch.com\n\n");
if (dbConnect(_configServer, _configPort, _configDatabase, _configUser, _configPassword)) {
tempValue.string = NULL;
if (dbSettingsValueGet("maxClients", &tempValue.integer)) shput(_settings, "maxClients", tempValue);
if (dbSettingsValueGet("portNumber", &tempValue.integer)) shput(_settings, "portNumber", tempValue);
dbDisconnect();
} else {
logWrite("Unable to connect to database.\n");
}
// Show hashmap contents.
for (size_t i=0; i<shlenu(_settings); i++) {
if (_settings[i].value.string) {
logWrite("%s: %s\n", _settings[i].key, _settings[i].value.string);
} else {
logWrite("%s: %d\n", _settings[i].key, _settings[i].value.integer);
}
}
// Free hashmap storage.
for (size_t i=0; i<shlenu(_settings); i++) {
if (_settings[i].value.string) DEL(_settings[i].value.string);
}
shfree(_settings);
_settings = NULL;
configWrite(configFile);
DEL(configFile);
logClose();
memoryShutdown();
return 0;
}