1 // Configuration.cpp: implementation of the CConfiguration class.
3 //////////////////////////////////////////////////////////////////////
6 #include "nel_launcher.h"
7 #include "Configuration.h"
8 #include <nel/misc/debug.h>
12 static char THIS_FILE
[]=__FILE__
;
16 #define CONFIG_FILE "nel_launcher.cfg"
18 #define KEY_HOST "StartupHost"
19 #define KEY_VERSION "Version"
20 #define KEY_URL_MAIN "StartupPage"
21 #define KEY_URL_RN "RNPage"
22 #define KEY_URL_NEWS "NewsPage"
23 #define KEY_APPLICATION "Application"
25 //////////////////////////////////////////////////////////////////////
26 // Construction/Destruction
27 //////////////////////////////////////////////////////////////////////
29 CConfiguration::CConfiguration()
33 CConfiguration::~CConfiguration()
37 BOOL
CConfiguration::Load()
44 if(f
.Open(CONFIG_FILE
, CFile::modeRead
))
46 nlinfo("Opening configuration file...");
48 dwSize
= f
.GetLength();
49 f
.Read(csBuffer
.GetBuffer(dwSize
), dwSize
);
50 csBuffer
.ReleaseBuffer();
53 // Reading the configuration file version
54 GetValue(csBuffer
, KEY_VERSION
, csValue
);
55 NLMISC::fromString(csValue
, m_dVersion
);
56 nlinfo("Config' version %s", csValue
);
58 if(m_dVersion
< APP
.m_dVersion
)
60 nlinfo("Launcher version > config version, loading config from resources...");
65 nlinfo("Launcher version <= config version, loading config from configuration file...");
67 HINSTANCE hInst
= (HINSTANCE
)GetModuleHandle(NULL
);
69 if(!GetValue(csBuffer
, KEY_HOST
, m_csHost
))
70 m_csHost
.LoadString(hInst
, IDS_HOST
);
72 if(!GetValue(csBuffer
, KEY_URL_MAIN
, m_csUrlMain
))
73 m_csUrlMain
.LoadString(hInst
, IDS_URLMAIN
);
75 if(!GetValue(csBuffer
, KEY_URL_RN
, m_csUrlRN
))
76 m_csUrlRN
.LoadString(hInst
, IDS_URLRN
);
78 if(!GetValue(csBuffer
, KEY_URL_NEWS
, m_csUrlNews
))
79 m_csUrlNews
.LoadString(hInst
, IDS_URLNEWS
);
81 if(!GetValue(csBuffer
, KEY_APPLICATION
, m_csApp
, 0))
82 m_csApp
.LoadString(hInst
, IDS_APPLICATION_APP
);
84 if(!GetValue(csBuffer
, KEY_APPLICATION
, m_csExe
, 1))
85 m_csExe
.LoadString(hInst
, IDS_APPLICATION_EXE
);
87 if(!GetValue(csBuffer
, KEY_APPLICATION
, m_csBasePath
, 2))
88 m_csBasePath
.LoadString(hInst
, IDS_APPLICATION_BASEPATH
);
90 if(!GetValue(csBuffer
, KEY_APPLICATION
, m_csAppBasePath
, 3))
91 m_csAppBasePath
.LoadString(hInst
, IDS_APPLICATION_APPBASEPATH
);
96 // No configuration file is available
97 // Take configuration from exe resource
98 nlinfo("No configuration file found, loading config from resources...");
101 nlinfo("Host %s", m_csHost
);
102 nlinfo("URL main page %s", m_csUrlMain
);
103 nlinfo("URL 'release notes' %s", m_csUrlRN
);
104 nlinfo("URL 'news' %s", m_csUrlNews
);
105 nlinfo("App %s", m_csApp
);
106 nlinfo("Exe %s", m_csExe
);
107 nlinfo("BasePath %s", m_csBasePath
);
108 nlinfo("AppBasePath %s", m_csAppBasePath
);
113 void CConfiguration::LoadFromResource()
115 HINSTANCE hInst
= (HINSTANCE
)GetModuleHandle(NULL
);
117 m_csHost
.LoadString(hInst
, IDS_HOST
);
118 m_csUrlMain
.LoadString(hInst
, IDS_URLMAIN
);
119 m_csUrlRN
.LoadString(hInst
, IDS_URLRN
);
120 m_csUrlNews
.LoadString(hInst
, IDS_URLNEWS
);
121 m_csApp
.LoadString(hInst
, IDS_APPLICATION_APP
);
122 m_csExe
.LoadString(hInst
, IDS_APPLICATION_EXE
);
123 m_csBasePath
.LoadString(hInst
, IDS_APPLICATION_BASEPATH
);
124 m_csAppBasePath
.LoadString(hInst
, IDS_APPLICATION_APPBASEPATH
);
125 m_csAppBasePath
.TrimRight();
128 BOOL
CConfiguration::GetValue(CString
& csBuffer
, CString csKey
, CString
& csValue
, int iIndex
)
135 while((iOffset2
= csBuffer
.Find('\n', iOffset1
)) != -1)
137 csRow
= csBuffer
.Mid(iOffset1
, iOffset2
- iOffset1
);
138 if(csRow
.Right(1) == "\r")
139 csRow
= csRow
.Left(csRow
.GetLength()-1);
143 if(csRow
.Left(2) != "//" && csRow
.GetLength() > 0 && csRow
.Find('=') != -1)
145 csKeyTmp
= csRow
.Mid(0, csRow
.Find('='));
147 csKeyTmp
.TrimRight();
148 if(!csKeyTmp
.CompareNoCase(csKey
))
150 // This is the key we're looking for
151 csValue
= csRow
.Mid(csRow
.Find('=')+1);
154 if(csValue
.Left(1) == "\"")
156 if(csValue
.Right(1) == ";")
157 csValue
= csValue
.Left(csValue
.GetLength()-1);
158 if(csValue
.Right(1) == "\"")
159 csValue
= csValue
.Left(csValue
.GetLength()-1);
161 if(csValue
.Left(1) == "{")
163 // This is a key is multiple values, delete '{' and '}'
166 if(csValue
.Right(1) == "}")
167 csValue
= csValue
.Left(csValue
.GetLength()-1);
171 for(int i
= 0; i
<= iIndex
; i
++)
173 if(csValue
.IsEmpty())
176 if(csValue
.Left(1) == "\"")
178 if(csValue
.Find('"') != -1)
182 csValue
= csValue
.Left(csValue
.Find('\"'));
185 csValue
= csValue
.Mid(csValue
.Find('\"')+1);
187 if(csValue
.Left(1) == ",")
197 iOffset1
= iOffset2
+ 1;