hi guys.
Ive created a Config file where the program will getting the values for the variables, but when i “register” a variable in the config.h and config.cpp and including the config.h in the main.cpp , im getting error variable not defined. The same error is happening for every variable in my configs.
[SPOILER]
Config.h
#pragma once
#include <string>
#include <set>
#include <vector>
#include <windows.h>
class Config {
public:
static Config* Instance();
void Reload();
struct Server {
void Load(Config *config);
int serverport;
std::wstring serveraddr;
/* time_t shutdownDuration; [example]
bool globalShout; [example]
std::vector<INT64> vitalityLevels; [example]
INT32 fixedPCCafePoints; [example] */
} *server;
protected:
Config(const wchar_t *filename);
std::wstring GetString(const wchar_t *section, const wchar_t *name, const wchar_t *defaultValue);
INT64 GetInt(const wchar_t *section, const wchar_t *name, const INT64 defaultValue);
bool GetBool(const wchar_t *section, const wchar_t *name, const bool defaultValue);
double GetDouble(const wchar_t *section, const wchar_t *name, const double defaultValue);
std::vector<INT64> GetIntList(const wchar_t *section, const wchar_t *name, const std::vector<INT64> &defaultValue);
std::set<INT64> GetIntSet(const wchar_t *section, const wchar_t *name, const std::set<INT64> &defaultValue);
std::wstring filename;
static Config *instance;
};
Config.cpp:
#include "Config.h"
#include <windows.h>
#include <fstream>
#include <sstream>
Config *Config::instance = 0;
Config::Config(const wchar_t *filename) :
filename(filename),
server(new Server)
{
Reload();
}
void Config::Reload()
{
server->Load(this);
}
void Config::Server::Load(Config *config)
{
serverport = config->GetInt(L"server", L"ServerPort", 1111);
serveraddr = config->GetString(L"server", L"ServerAddr", L"127.0.0.1");
/* [Examples]
globalShout = config->GetBool(L"server", L"GlobalShout", false);
vitalityLevels = config->GetIntList(L"server", L"VitalityLevels", std::vector<INT64>());
*/
}
//should be changed
Config* Config::Instance()
{
const static char *paths] = {
".\\Config.ini",
"..\\Config.ini",
"..\\l2server\\Config.ini",
"..\\server\\Config.ini",
"Config.ini",
0 };
if (instance) {
return instance;
}
for (size_t i(0); paths*; ++i) {
if (std::ifstream(paths*)) {
return instance;
}
}
MessageBox(0,
L"Can't find suitable config Config.ini.
"
L"To use global config for all daemons, place it in top level directory
"
L"To use local configs for single daemons, place it to daemon directory (Cached, NPC, L2Server)", L"ERROR", 0);
exit(1);
}
std::wstring Config::GetString(const wchar_t *section, const wchar_t *name, const wchar_t *defaultValue)
{
static wchar_t buffer[16384];
GetPrivateProfileString(section, name, defaultValue, buffer, sizeof(buffer), filename.c_str());
return std::wstring(buffer);
}
INT64 Config::GetInt(const wchar_t *section, const wchar_t *name, const INT64 defaultValue)
{
INT64 ret;
std::wstring s;
{
std::wstringstream ss;
ss << defaultValue;
s = GetString(section, name, ss.str().c_str());
}
std::wstringstream ss;
ss << s;
ss >> ret;
if (ss.fail()) {
ret = defaultValue;
}
return ret;
}
bool Config::GetBool(const wchar_t *section, const wchar_t *name, const bool defaultValue)
{
std::wstring s = GetString(section, name, L"");
if (s == L"1" || s == L"true" || s == L"on" || s == L"yes") {
return true;
}
else if (s == L"0" || s == L"false" || s == L"off" || s == L"no") {
return false;
}
else {
return defaultValue;
}
}
double Config::GetDouble(const wchar_t *section, const wchar_t *name, const double defaultValue)
{
double ret;
std::wstring s;
{
std::wstringstream ss;
ss << defaultValue;
s = GetString(section, name, ss.str().c_str());
}
std::wstringstream ss;
ss << s;
ss >> ret;
if (ss.fail()) {
ret = defaultValue;
}
return ret;
}
std::vector<INT64> Config::GetIntList(const wchar_t *section, const wchar_t *name, const std::vector<INT64> &defaultValue)
{
std::wstring s(GetString(section, name, L""));
if (s.empty()) {
return defaultValue;
}
std::vector<INT64> result;
if (s == L"-" || s == L"null") {
return result;
}
std::wstring part;
for (size_t i(0); i < s.size(); ++i) {
if (s* == L' ' || s* == L',' || s* == L';') {
if (!part.empty()) {
std::wstringstream ss;
INT64 i;
ss << part;
ss >> i;
result.push_back(i);
part.clear();
}
}
else if (s* >= L'0' && s* <= L'9') {
part.push_back(s*);
}
}
if (!part.empty()) {
std::wstringstream ss;
INT64 i;
ss << part;
ss >> i;
result.push_back(i);
}
return result;
}
std::set<INT64> Config::GetIntSet(const wchar_t *section, const wchar_t *name, const std::set<INT64> &defaultValue)
{
std::wstring s(GetString(section, name, L""));
if (s.empty()) {
return defaultValue;
}
std::set<INT64> result;
if (s == L"-" || s == L"null") {
return result;
}
std::wstring part;
for (size_t i(0); i < s.size(); ++i) {
if (s* == L' ' || s* == L',' || s* == L';') {
if (!part.empty()) {
std::wstringstream ss;
INT64 i;
ss << part;
ss >> i;
result.insert(i);
part.clear();
}
}
else if (s* >= L'0' && s* <= L'9') {
part.push_back(s*);
}
}
if (!part.empty()) {
std::wstringstream ss;
INT64 i;
ss << part;
ss >> i;
result.insert(i);
}
return result;
}
Main.cpp:
#include "Server.h"
#include "database.h"
#include "ScriptLoader.h"
#include "Config.h"
int main()
{
system("color 5a");
std::cout << "Loading MySQL Database" << std::endl;
Sleep(300);
MySQL:MySQL();
std::cout << "Loading Scripts" << std::endl;
Sleep(300);
ScriptLoad();
std::cout << "Server Has Started" << std::endl;
Server MyServer(serverport); //Create server on port 1111
for (int i = 0; i < 100; i++) // Max Players Online (100)
{
MyServer.ListenForNewConnection(); //Accept new connection (if someones trying to connect)
}
EngineDelete();
system("pause");
return 0;
}
[/SPOILER]