1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2015 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "nel/misc/app_context.h"
22 #include "nel/misc/dynloadlib.h"
23 #include "nel/misc/command.h"
34 //INelContext *NelContext = NULL;
35 INelContext
*INelContext::_NelContext
= NULL
;
37 INelContext
** INelContext::_getInstance()
39 static INelContext
*nelContext
= NULL
;
45 INelContext
&INelContext::getInstance()
47 if (*(_getInstance()) == NULL
)
49 _NelContext
= new CApplicationContext
;
50 *(_getInstance()) = _NelContext
;
56 bool INelContext::isContextInitialised()
58 return (*_getInstance()) != NULL
;
62 INelContext::~INelContext()
64 // unregister still undeleted local command into the global command registry
65 if (ICommand::LocalCommands
)
67 ICommand::TCommand::iterator
first(ICommand::LocalCommands
->begin()), last(ICommand::LocalCommands
->end());
68 for (; first
!= last
; ++first
)
70 ICommand
*command
= first
->second
;
71 CCommandRegistry::getInstance().unregisterCommand(command
);
75 CInstanceCounterLocalManager::releaseInstance();
78 *(_getInstance()) = NULL
;
83 void INelContext::contextReady()
85 // Register the NeL Context
86 // This assert doesn't work for Linux due to ELF symbol relocation
88 nlassert(*(_getInstance()) == NULL
);
89 #endif // NL_OS_WINDOWS
91 *(_getInstance()) = this;
93 // set numeric locale to C to avoid the use of decimal separators different of a dot
94 char *locale
= setlocale(LC_NUMERIC
, "C");
96 // register any pending thinks
98 // register local instance counter in the global instance counter manager
99 CInstanceCounterLocalManager::getInstance().registerLocalManager();
101 // register local commands into the global command registry (except it there is no command at all)
102 if (ICommand::LocalCommands
!= NULL
)
104 ICommand::TCommand::iterator
first(ICommand::LocalCommands
->begin()), last(ICommand::LocalCommands
->end());
105 for (; first
!= last
; ++first
)
107 CCommandRegistry::getInstance().registerCommand(first
->second
);
112 CApplicationContext::CApplicationContext()
120 DefaultMemDisplayer
= NULL
;
121 DefaultMsgBoxDisplayer
= NULL
;
122 DebugNeedAssert
= false;
124 AlreadyCreateSharedAmongThreads
= false;
125 WindowedApplication
= false;
130 CApplicationContext::~CApplicationContext()
133 TSingletonRegistry::iterator it
= _SingletonRegistry
.begin(), iend
= _SingletonRegistry
.end();
137 // can't use nldebug there because it'll create new displayers
138 std::string message
= toString("Instance '%s' still allocated at %p\n", it
->first
.c_str(), it
->second
);
141 OutputDebugStringW(nlUtf8ToWide(message
));
143 printf("%s", message
.c_str());
151 void *CApplicationContext::getSingletonPointer(const std::string
&singletonName
)
153 TSingletonRegistry::iterator
it(_SingletonRegistry
.find(singletonName
));
154 if (it
!= _SingletonRegistry
.end())
157 // nlwarning("Can't find singleton '%s'", singletonName.c_str());
161 void CApplicationContext::setSingletonPointer(const std::string
&singletonName
, void *ptr
)
163 nlassert(_SingletonRegistry
.find(singletonName
) == _SingletonRegistry
.end());
164 _SingletonRegistry
[singletonName
] = ptr
;
167 void CApplicationContext::releaseSingletonPointer(const std::string
&singletonName
, void *ptr
)
169 nlassert(_SingletonRegistry
.find(singletonName
) != _SingletonRegistry
.end());
170 nlassert(_SingletonRegistry
.find(singletonName
)->second
== ptr
);
171 _SingletonRegistry
.erase(singletonName
);
175 CLog
*CApplicationContext::getErrorLog()
180 void CApplicationContext::setErrorLog(CLog
*errorLog
)
185 CLog
*CApplicationContext::getWarningLog()
190 void CApplicationContext::setWarningLog(CLog
*warningLog
)
192 WarningLog
= warningLog
;
195 CLog
*CApplicationContext::getInfoLog()
200 void CApplicationContext::setInfoLog(CLog
*infoLog
)
205 CLog
*CApplicationContext::getDebugLog()
210 void CApplicationContext::setDebugLog(CLog
*debugLog
)
215 CLog
*CApplicationContext::getAssertLog()
220 void CApplicationContext::setAssertLog(CLog
*assertLog
)
222 AssertLog
= assertLog
;
225 CMemDisplayer
*CApplicationContext::getDefaultMemDisplayer()
227 return DefaultMemDisplayer
;
230 void CApplicationContext::setDefaultMemDisplayer(CMemDisplayer
*memDisplayer
)
232 DefaultMemDisplayer
= memDisplayer
;
235 CMsgBoxDisplayer
*CApplicationContext::getDefaultMsgBoxDisplayer()
237 return DefaultMsgBoxDisplayer
;
240 void CApplicationContext::setDefaultMsgBoxDisplayer(CMsgBoxDisplayer
*msgBoxDisplayer
)
242 DefaultMsgBoxDisplayer
= msgBoxDisplayer
;
245 bool CApplicationContext::getDebugNeedAssert()
247 return DebugNeedAssert
;
250 void CApplicationContext::setDebugNeedAssert(bool needAssert
)
252 DebugNeedAssert
= needAssert
;
255 bool CApplicationContext::getNoAssert()
260 void CApplicationContext::setNoAssert(bool noAssert
)
265 bool CApplicationContext::getAlreadyCreateSharedAmongThreads()
267 return AlreadyCreateSharedAmongThreads
;
270 void CApplicationContext::setAlreadyCreateSharedAmongThreads(bool b
)
272 AlreadyCreateSharedAmongThreads
= b
;
275 bool CApplicationContext::isWindowedApplication()
277 return WindowedApplication
;
280 void CApplicationContext::setWindowedApplication(bool b
)
282 WindowedApplication
= b
;
285 CLibraryContext::CLibraryContext(INelContext
&applicationContext
)
286 : _ApplicationContext(&applicationContext
)
292 void *CLibraryContext::getSingletonPointer(const std::string
&singletonName
)
294 // nlassert(_ApplicationContext != NULL);
296 // just forward the call
297 return _ApplicationContext
->getSingletonPointer(singletonName
);
300 void CLibraryContext::setSingletonPointer(const std::string
&singletonName
, void *ptr
)
302 // nlassert(_ApplicationContext != NULL);
304 // just forward the call
305 _ApplicationContext
->setSingletonPointer(singletonName
, ptr
);
308 void CLibraryContext::releaseSingletonPointer(const std::string
&singletonName
, void *ptr
)
310 // nlassert(_ApplicationContext != NULL);
312 // just forward the call
313 _ApplicationContext
->releaseSingletonPointer(singletonName
, ptr
);
317 CLog
*CLibraryContext::getErrorLog()
319 // nlassert(_ApplicationContext != NULL);
321 // just forward the call
322 return _ApplicationContext
->getErrorLog();
325 void CLibraryContext::setErrorLog(CLog
*errorLog
)
327 // nlassert(_ApplicationContext != NULL);
329 // just forward the call
330 _ApplicationContext
->setErrorLog(errorLog
);
333 CLog
*CLibraryContext::getWarningLog()
335 // nlassert(_ApplicationContext != NULL);
337 // just forward the call
338 return _ApplicationContext
->getWarningLog();
341 void CLibraryContext::setWarningLog(CLog
*warningLog
)
343 // nlassert(_ApplicationContext != NULL);
345 // just forward the call
346 _ApplicationContext
->setWarningLog(warningLog
);
349 CLog
*CLibraryContext::getInfoLog()
351 // nlassert(_ApplicationContext != NULL);
353 // just forward the call
354 return _ApplicationContext
->getInfoLog();
357 void CLibraryContext::setInfoLog(CLog
*infoLog
)
359 // nlassert(_ApplicationContext != NULL);
361 // just forward the call
362 _ApplicationContext
->setInfoLog(infoLog
);
365 CLog
*CLibraryContext::getDebugLog()
367 // nlassert(_ApplicationContext != NULL);
369 // just forward the call
370 return _ApplicationContext
->getDebugLog();
373 void CLibraryContext::setDebugLog(CLog
*debugLog
)
375 // nlassert(_ApplicationContext != NULL);
377 // just forward the call
378 _ApplicationContext
->setDebugLog(debugLog
);
381 CLog
*CLibraryContext::getAssertLog()
383 // nlassert(_ApplicationContext != NULL);
385 // just forward the call
386 return _ApplicationContext
->getAssertLog();
389 void CLibraryContext::setAssertLog(CLog
*assertLog
)
391 // nlassert(_ApplicationContext != NULL);
393 // just forward the call
394 _ApplicationContext
->setAssertLog(assertLog
);
397 CMemDisplayer
*CLibraryContext::getDefaultMemDisplayer()
399 // nlassert(_ApplicationContext != NULL);
401 // just forward the call
402 return _ApplicationContext
->getDefaultMemDisplayer();
405 void CLibraryContext::setDefaultMemDisplayer(CMemDisplayer
*memDisplayer
)
407 // nlassert(_ApplicationContext != NULL);
409 // just forward the call
410 _ApplicationContext
->setDefaultMemDisplayer(memDisplayer
);
413 CMsgBoxDisplayer
*CLibraryContext::getDefaultMsgBoxDisplayer()
415 // nlassert(_ApplicationContext != NULL);
417 // just forward the call
418 return _ApplicationContext
->getDefaultMsgBoxDisplayer();
421 void CLibraryContext::setDefaultMsgBoxDisplayer(CMsgBoxDisplayer
*msgBoxDisplayer
)
423 // nlassert(_ApplicationContext != NULL);
425 // just forward the call
426 _ApplicationContext
->setDefaultMsgBoxDisplayer(msgBoxDisplayer
);
429 bool CLibraryContext::getDebugNeedAssert()
431 // nlassert(_ApplicationContext != NULL);
433 // just forward the call
434 return _ApplicationContext
->getDebugNeedAssert();
437 void CLibraryContext::setDebugNeedAssert(bool needAssert
)
439 // nlassert(_ApplicationContext != NULL);
441 // just forward the call
442 _ApplicationContext
->setDebugNeedAssert(needAssert
);
445 bool CLibraryContext::getNoAssert()
447 // nlassert(_ApplicationContext != NULL);
449 // just forward the call
450 return _ApplicationContext
->getNoAssert();
455 void CLibraryContext::setNoAssert(bool noAssert
)
457 // nlassert(_ApplicationContext != NULL);
459 // just forward the call
460 _ApplicationContext
->setNoAssert(noAssert
);
463 bool CLibraryContext::getAlreadyCreateSharedAmongThreads()
465 return _ApplicationContext
->getAlreadyCreateSharedAmongThreads();
468 void CLibraryContext::setAlreadyCreateSharedAmongThreads(bool b
)
470 _ApplicationContext
->setAlreadyCreateSharedAmongThreads(b
);
473 bool CLibraryContext::isWindowedApplication()
475 return _ApplicationContext
->isWindowedApplication();
478 void CLibraryContext::setWindowedApplication(bool b
)
480 _ApplicationContext
->setWindowedApplication(b
);
483 void initNelLibrary(NLMISC::CLibrary
&lib
)
485 nlassert(lib
.isLibraryLoaded());
487 TInitLibraryFunc
*funptrptr
= reinterpret_cast<TInitLibraryFunc
*>(lib
.getSymbolAddress("libraryEntry"));
488 nlassert(funptrptr
!= NULL
);
490 TInitLibraryFunc funptr
= *funptrptr
;
492 // call the initialisation function
493 funptr(NLMISC::INelContext::getInstance());
498 } // namespace NLMISC