2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-09 16:58:33 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-12-05 22:56:09 +01:00
|
|
|
#include "Pyrogenesis.h"
|
2004-06-09 15:53:32 +02:00
|
|
|
#include "Parser.h"
|
|
|
|
#include "ConfigDB.h"
|
|
|
|
#include "CLogger.h"
|
2007-12-20 21:21:45 +01:00
|
|
|
#include "Filesystem.h"
|
2004-07-04 17:36:48 +02:00
|
|
|
#include "scripting/ScriptingHost.h"
|
2004-06-09 15:53:32 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
#define LOG_CATEGORY L"config"
|
2004-08-15 22:57:31 +02:00
|
|
|
|
2008-07-13 23:22:03 +02:00
|
|
|
typedef std::map <CStr, CConfigValueSet> TConfigMap;
|
2004-06-09 15:53:32 +02:00
|
|
|
TConfigMap CConfigDB::m_Map[CFG_LAST];
|
2009-11-03 22:46:35 +01:00
|
|
|
CStrW CConfigDB::m_ConfigFile[CFG_LAST];
|
2004-06-09 15:53:32 +02:00
|
|
|
bool CConfigDB::m_UseVFS[CFG_LAST];
|
|
|
|
|
2009-12-28 15:49:31 +01:00
|
|
|
#define GET_NS_PRIVATE(cx, obj) (EConfigNamespace)((intptr_t)JS_GetPrivate(cx, obj) >> 1)
|
|
|
|
|
2004-07-04 17:36:48 +02:00
|
|
|
namespace ConfigNamespace_JS
|
|
|
|
{
|
|
|
|
JSBool GetProperty( JSContext* cx, JSObject* obj, jsval id, jsval* vp )
|
|
|
|
{
|
2009-12-28 15:49:31 +01:00
|
|
|
EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);
|
2004-07-04 17:36:48 +02:00
|
|
|
if (cfgNs < 0 || cfgNs >= CFG_LAST)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
CStr propName = g_ScriptingHost.ValueToString(id);
|
|
|
|
CConfigValue *val=g_ConfigDB.GetValue(cfgNs, propName);
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
JSString *js_str=JS_NewStringCopyN(cx, val->m_String.c_str(), val->m_String.size());
|
|
|
|
*vp = STRING_TO_JSVAL(js_str);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool SetProperty( JSContext* cx, JSObject* obj, jsval id, jsval* vp )
|
|
|
|
{
|
2009-12-28 15:49:31 +01:00
|
|
|
EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);
|
2004-07-04 17:36:48 +02:00
|
|
|
if (cfgNs < 0 || cfgNs >= CFG_LAST)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
CStr propName = g_ScriptingHost.ValueToString(id);
|
|
|
|
CConfigValue *val=g_ConfigDB.CreateValue(cfgNs, propName);
|
|
|
|
char *str;
|
|
|
|
if (JS_ConvertArguments(cx, 1, vp, "s", &str))
|
|
|
|
{
|
|
|
|
val->m_String=str;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass Class = {
|
2004-07-07 12:57:36 +02:00
|
|
|
"ConfigNamespace", JSCLASS_HAS_PRIVATE,
|
2004-07-04 17:36:48 +02:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
GetProperty, SetProperty,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
|
|
|
JS_ConvertStub, JS_FinalizeStub
|
|
|
|
};
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool Construct( JSContext* cx, JSObject* obj, uintN argc, jsval* UNUSED(argv), jsval* rval )
|
2004-07-04 17:36:48 +02:00
|
|
|
{
|
|
|
|
if (argc != 0)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2004-07-07 20:06:02 +02:00
|
|
|
JSObject *newObj=JS_NewObject(cx, &Class, NULL, obj);
|
2004-07-04 17:36:48 +02:00
|
|
|
*rval=OBJECT_TO_JSVAL(newObj);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNamespace(JSContext *cx, JSObject *obj, EConfigNamespace cfgNs)
|
|
|
|
{
|
2010-01-01 16:33:07 +01:00
|
|
|
JS_SetPrivate(cx, obj, (void *)((uintptr_t)cfgNs << 1)); // JS requires bottom bit = 0
|
2004-07-04 17:36:48 +02:00
|
|
|
}
|
2004-07-07 20:06:02 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool WriteFile( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval )
|
2004-07-07 20:06:02 +02:00
|
|
|
{
|
2009-12-28 15:49:31 +01:00
|
|
|
EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);
|
2004-07-07 20:06:02 +02:00
|
|
|
if (cfgNs < 0 || cfgNs >= CFG_LAST)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSBool useVFS;
|
|
|
|
char *path;
|
|
|
|
if (JS_ConvertArguments(cx, 2, argv, "bs", &useVFS, &path))
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
JSBool res=g_ConfigDB.WriteFile(cfgNs, useVFS?true:false, CStrW(path));
|
2004-07-07 20:06:02 +02:00
|
|
|
*rval = BOOLEAN_TO_JSVAL(res);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool Reload( JSContext* cx, JSObject* obj, uintN argc, jsval* UNUSED(argv), jsval* rval )
|
2004-07-07 20:06:02 +02:00
|
|
|
{
|
|
|
|
if (argc != 0)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-12-28 15:49:31 +01:00
|
|
|
EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);
|
2004-07-07 20:06:02 +02:00
|
|
|
if (cfgNs < 0 || cfgNs >= CFG_LAST)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSBool ret=g_ConfigDB.Reload(cfgNs);
|
|
|
|
*rval = BOOLEAN_TO_JSVAL(ret);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool SetFile( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* UNUSED(rval) )
|
2004-07-07 20:06:02 +02:00
|
|
|
{
|
|
|
|
if (argc != 0)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-12-28 15:49:31 +01:00
|
|
|
EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);
|
2004-07-07 20:06:02 +02:00
|
|
|
if (cfgNs < 0 || cfgNs >= CFG_LAST)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSBool useVFS;
|
|
|
|
char *path;
|
|
|
|
if (JS_ConvertArguments(cx, 2, argv, "bs", &useVFS, &path))
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
g_ConfigDB.SetConfigFile(cfgNs, useVFS?true:false, CStrW(path));
|
2004-07-07 20:06:02 +02:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSFunctionSpec Funcs[] = {
|
2005-05-18 07:32:09 +02:00
|
|
|
{ "writeFile", WriteFile, 2, 0, 0},
|
|
|
|
{ "reload", Reload, 0, 0, 0},
|
2005-08-07 23:58:36 +02:00
|
|
|
{ "setFile", SetFile, 2, 0, 0},
|
2004-07-07 20:06:02 +02:00
|
|
|
{0}
|
|
|
|
};
|
2004-07-04 17:36:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace ConfigDB_JS
|
|
|
|
{
|
|
|
|
JSClass Class = {
|
|
|
|
"ConfigDB", 0,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
|
|
|
JS_ConvertStub, JS_FinalizeStub
|
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec Props[] = {
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec Funcs[] = {
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool Construct( JSContext* cx, JSObject* obj, uintN argc, jsval* UNUSED(argv), jsval* rval )
|
2004-07-04 17:36:48 +02:00
|
|
|
{
|
|
|
|
if (argc != 0)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2004-07-07 20:06:02 +02:00
|
|
|
JSObject *newObj=JS_NewObject(cx, &Class, NULL, obj);
|
2004-07-04 17:36:48 +02:00
|
|
|
*rval=OBJECT_TO_JSVAL(newObj);
|
|
|
|
|
|
|
|
int flags=JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT;
|
|
|
|
#define cfg_ns(_propname, _enum) STMT (\
|
|
|
|
JSObject *nsobj=g_ScriptingHost.CreateCustomObject("ConfigNamespace"); \
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert(nsobj); \
|
2004-07-04 17:36:48 +02:00
|
|
|
ConfigNamespace_JS::SetNamespace(cx, nsobj, _enum); \
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert(JS_DefineProperty(cx, newObj, _propname, OBJECT_TO_JSVAL(nsobj), NULL, NULL, flags)); )
|
2004-07-04 17:36:48 +02:00
|
|
|
|
2009-06-23 19:48:34 +02:00
|
|
|
cfg_ns("default", CFG_DEFAULT);
|
2004-07-04 17:36:48 +02:00
|
|
|
cfg_ns("system", CFG_SYSTEM);
|
|
|
|
cfg_ns("user", CFG_USER);
|
|
|
|
cfg_ns("mod", CFG_MOD);
|
|
|
|
|
|
|
|
#undef cfg_ns
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
CConfigDB::CConfigDB()
|
|
|
|
{
|
|
|
|
g_ScriptingHost.DefineCustomObjectType(&ConfigDB_JS::Class, ConfigDB_JS::Construct, 0, ConfigDB_JS::Props, ConfigDB_JS::Funcs, NULL, NULL);
|
2004-07-07 20:06:02 +02:00
|
|
|
g_ScriptingHost.DefineCustomObjectType(&ConfigNamespace_JS::Class, ConfigNamespace_JS::Construct, 0, NULL, ConfigNamespace_JS::Funcs, NULL, NULL);
|
2004-07-04 17:36:48 +02:00
|
|
|
JSObject *js_ConfigDB=g_ScriptingHost.CreateCustomObject("ConfigDB");
|
|
|
|
g_ScriptingHost.SetGlobal("g_ConfigDB", OBJECT_TO_JSVAL(js_ConfigDB));
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CConfigValue *CConfigDB::GetValue(EConfigNamespace ns, const CStr& name)
|
2004-06-09 15:53:32 +02:00
|
|
|
{
|
2004-07-21 18:34:07 +02:00
|
|
|
CConfigValueSet* values = GetValues( ns, name );
|
|
|
|
if( !values ) return( NULL );
|
|
|
|
return &( (*values)[0] );
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CConfigValueSet *CConfigDB::GetValues(EConfigNamespace ns, const CStr& name )
|
2004-07-21 18:34:07 +02:00
|
|
|
{
|
2006-11-07 22:03:13 +01:00
|
|
|
if (ns < 0 || ns >= CFG_LAST)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"CConfigDB: Invalid ns value");
|
2006-11-07 22:03:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-10-23 16:39:28 +02:00
|
|
|
|
|
|
|
TConfigMap::iterator it = m_Map[CFG_COMMAND].find( name );
|
|
|
|
if( it != m_Map[CFG_COMMAND].end() )
|
|
|
|
return &( it->second );
|
|
|
|
|
2009-06-23 19:48:34 +02:00
|
|
|
for( int search_ns = ns; search_ns >= 0; search_ns-- )
|
2004-10-23 16:39:28 +02:00
|
|
|
{
|
|
|
|
TConfigMap::iterator it = m_Map[search_ns].find(name);
|
|
|
|
if (it != m_Map[search_ns].end())
|
|
|
|
return &( it->second );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
2004-06-09 15:53:32 +02:00
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CConfigValue *CConfigDB::CreateValue(EConfigNamespace ns, const CStr& name)
|
2004-06-09 15:53:32 +02:00
|
|
|
{
|
2006-11-07 22:03:13 +01:00
|
|
|
if (ns < 0 || ns >= CFG_LAST)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"CConfigDB: Invalid ns value");
|
2006-11-07 22:03:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-09 15:53:32 +02:00
|
|
|
|
|
|
|
CConfigValue *ret=GetValue(ns, name);
|
|
|
|
if (ret) return ret;
|
|
|
|
|
2004-07-21 18:34:07 +02:00
|
|
|
TConfigMap::iterator it=m_Map[ns].insert(m_Map[ns].begin(), make_pair(name, CConfigValueSet( 1 )));
|
|
|
|
return &(it->second[0]);
|
2004-06-09 15:53:32 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
void CConfigDB::SetConfigFile(EConfigNamespace ns, bool useVFS, const CStrW& path)
|
2004-06-09 15:53:32 +02:00
|
|
|
{
|
2006-11-07 22:03:13 +01:00
|
|
|
if (ns < 0 || ns >= CFG_LAST)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"CConfigDB: Invalid ns value");
|
2006-11-07 22:03:13 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-06-09 15:53:32 +02:00
|
|
|
|
|
|
|
m_ConfigFile[ns]=path;
|
|
|
|
m_UseVFS[ns]=useVFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CConfigDB::Reload(EConfigNamespace ns)
|
|
|
|
{
|
|
|
|
// Set up CParser
|
|
|
|
CParser parser;
|
|
|
|
CParserLine parserLine;
|
2004-07-21 18:34:07 +02:00
|
|
|
parser.InputTaskType("Assignment", "_$ident_=<_[-$arg(_minus)]_$value_,>_[-$arg(_minus)]_$value[[;]$rest]");
|
2004-07-04 17:36:48 +02:00
|
|
|
parser.InputTaskType("CommentOrBlank", "_[;[$rest]]");
|
2004-06-09 15:53:32 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
// Open file with VFS
|
|
|
|
shared_ptr<u8> buffer; size_t buflen;
|
2004-06-09 15:53:32 +02:00
|
|
|
{
|
2009-06-23 19:48:34 +02:00
|
|
|
// Handle missing files quietly
|
2009-08-02 13:07:42 +02:00
|
|
|
if (g_VFS->GetFileInfo(m_ConfigFile[ns], NULL) < 0)
|
2004-06-09 15:53:32 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Warning, LOG_CATEGORY, L"Cannot find config file \"%ls\" - ignoring", m_ConfigFile[ns].c_str());
|
2004-06-09 15:53:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-06-23 19:48:34 +02:00
|
|
|
else
|
|
|
|
{
|
2010-01-09 20:20:14 +01:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY, L"Loading config file \"%ls\"", m_ConfigFile[ns].c_str());
|
2009-06-23 19:48:34 +02:00
|
|
|
LibError ret = g_VFS->LoadFile(m_ConfigFile[ns], buffer, buflen);
|
|
|
|
if (ret != INFO::OK)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"vfs_load for \"%ls\" failed: return was %ld", m_ConfigFile[ns].c_str(), ret);
|
2009-06-23 19:48:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-06-09 15:53:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TConfigMap newMap;
|
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
char *filebuf=(char *)buffer.get();
|
2005-01-23 02:36:47 +01:00
|
|
|
char *filebufend=filebuf+buflen;
|
2004-06-09 15:53:32 +02:00
|
|
|
|
|
|
|
// Read file line by line
|
2004-06-09 16:58:33 +02:00
|
|
|
char *next=filebuf-1;
|
2004-06-09 15:53:32 +02:00
|
|
|
do
|
|
|
|
{
|
2004-06-09 16:58:33 +02:00
|
|
|
char *pos=next+1;
|
2005-01-23 02:36:47 +01:00
|
|
|
next=(char *)memchr(pos, '\n', filebufend-pos);
|
|
|
|
if (!next) next=filebufend;
|
2004-06-09 16:58:33 +02:00
|
|
|
|
|
|
|
char *lend=next;
|
|
|
|
if (*(lend-1) == '\r') lend--;
|
|
|
|
|
2004-06-09 15:53:32 +02:00
|
|
|
// Send line to parser
|
2004-07-04 17:36:48 +02:00
|
|
|
bool parseOk=parserLine.ParseString(parser, std::string(pos, lend));
|
2004-06-09 15:53:32 +02:00
|
|
|
// Get name and value from parser
|
2008-07-13 23:22:03 +02:00
|
|
|
std::string name;
|
|
|
|
std::string value;
|
2004-06-09 15:53:32 +02:00
|
|
|
|
2004-07-04 17:36:48 +02:00
|
|
|
if (parseOk &&
|
|
|
|
parserLine.GetArgCount()>=2 &&
|
2004-06-09 15:53:32 +02:00
|
|
|
parserLine.GetArgString(0, name) &&
|
|
|
|
parserLine.GetArgString(1, value))
|
|
|
|
{
|
|
|
|
// Add name and value to the map
|
2004-07-21 18:34:07 +02:00
|
|
|
size_t argCount = parserLine.GetArgCount();
|
|
|
|
|
|
|
|
newMap[name].clear();
|
|
|
|
|
|
|
|
for( size_t t = 0; t < argCount; t++ )
|
|
|
|
{
|
2004-07-22 18:18:12 +02:00
|
|
|
if( !parserLine.GetArgString( (int)t + 1, value ) )
|
2004-07-21 18:34:07 +02:00
|
|
|
continue;
|
|
|
|
CConfigValue argument;
|
|
|
|
argument.m_String = value;
|
|
|
|
newMap[name].push_back( argument );
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY, L"Loaded config string \"%hs\" = \"%hs\"", name.c_str(), value.c_str());
|
2004-07-21 18:34:07 +02:00
|
|
|
}
|
2004-06-09 15:53:32 +02:00
|
|
|
}
|
|
|
|
}
|
2005-01-23 02:36:47 +01:00
|
|
|
while (next < filebufend);
|
2004-06-09 15:53:32 +02:00
|
|
|
|
|
|
|
m_Map[ns].swap(newMap);
|
2004-06-09 16:58:33 +02:00
|
|
|
|
|
|
|
return true;
|
2004-06-09 15:53:32 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
bool CConfigDB::WriteFile(EConfigNamespace ns, bool useVFS, const CStrW& path)
|
2004-06-09 15:53:32 +02:00
|
|
|
{
|
2007-12-20 21:21:45 +01:00
|
|
|
debug_assert(useVFS);
|
|
|
|
|
2006-11-07 22:03:13 +01:00
|
|
|
if (ns < 0 || ns >= CFG_LAST)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"CConfigDB: Invalid ns value");
|
2006-11-07 22:03:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
2004-07-07 20:06:02 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
shared_ptr<u8> buf = io_Allocate(1*MiB);
|
|
|
|
char* pos = (char*)buf.get();
|
|
|
|
TConfigMap &map=m_Map[ns];
|
2008-07-19 01:51:44 +02:00
|
|
|
for(TConfigMap::const_iterator it = map.begin(); it != map.end(); ++it)
|
2004-07-07 20:06:02 +02:00
|
|
|
{
|
2009-11-06 11:59:10 +01:00
|
|
|
pos += sprintf(pos, "%s = \"%s\"\n", it->first.c_str(), it->second[0].m_String.c_str());
|
2004-07-07 20:06:02 +02:00
|
|
|
}
|
2007-12-20 21:21:45 +01:00
|
|
|
const size_t len = pos - (char*)buf.get();
|
2004-07-07 20:06:02 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
LibError ret = g_VFS->CreateFile(path, buf, len);
|
2007-12-20 21:21:45 +01:00
|
|
|
if(ret < 0)
|
2004-07-07 20:06:02 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"CConfigDB::WriteFile(): CreateFile \"%ls\" failed (error: %d)", path.c_str(), (int)ret);
|
2007-12-20 21:21:45 +01:00
|
|
|
return false;
|
2004-07-07 20:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2004-06-09 15:53:32 +02:00
|
|
|
}
|