Просмотр исходного кода

Adding saving/loading support to config

Panagiotis Christopoulos Charitos 10 лет назад
Родитель
Сommit
324d6d9838

+ 4 - 0
include/anki/misc/ConfigSet.h

@@ -43,6 +43,10 @@ public:
 	CString getString(const CString& name) const;
 	/// @}
 
+	ANKI_USE_RESULT Error loadFromFile(CString filename);
+
+	ANKI_USE_RESULT Error saveToFile(CString filename) const;
+
 protected:
 	void newOption(const CString& name, const CString& value);
 	void newOption(const CString& name, F64 value);

+ 2 - 0
include/anki/misc/Xml.h

@@ -106,6 +106,8 @@ private:
 class XmlDocument
 {
 public:
+	static CString XML_HEADER;
+
 	ANKI_USE_RESULT Error loadFile(
 		const CString& filename, GenericMemoryPoolAllocator<U8> alloc);
 

+ 1 - 2
src/core/Config.cpp

@@ -64,7 +64,6 @@ Config::Config()
 	newOption("lodDistance", 10.0); // Distance that used to calculate the LOD
 	newOption("samples", 1);
 	newOption("tessellation", true);
-	newOption("sceneFrameAllocatorSize", 1024 * 1024);
 	newOption("clusterSizeZ", 32);
 	newOption("imageReflectionMaxDistance", 30.0);
 
@@ -86,8 +85,8 @@ Config::Config()
 	//
 	// Window
 	//
-	newOption("glminor", 5);
 	newOption("glmajor", 4);
+	newOption("glminor", 5);
 	newOption("fullscreenDesktopResolution", false);
 	newOption("debugContext",
 #if ANKI_DEBUG == 1

+ 1 - 1
src/gr/gl/GlState.cpp

@@ -181,7 +181,7 @@ void GlState::init1()
 	{
 		m_transferBuffer.create(m_manager->getAllocator(),
 			m_dynamicBuffers[BufferUsage::TRANSFER].m_size
-			/ sizeof(Aligned16Type));
+				/ sizeof(Aligned16Type));
 
 		auto& buff = m_dynamicBuffers[BufferUsage::TRANSFER];
 		buff.m_address = reinterpret_cast<U8*>(&m_transferBuffer[0]);

+ 8 - 4
src/gr/gl/PipelineImpl.cpp

@@ -210,11 +210,15 @@ Error PipelineImpl::createGlPipeline()
 
 		ANKI_LOGE("Ppline error log follows (vs:%u, fs:%u):\n%s",
 			m_in.m_shaders[ShaderType::VERTEX].isCreated()
-			? m_in.m_shaders[ShaderType::VERTEX]->getImplementation().getGlName()
-			: -1,
+				? m_in.m_shaders[ShaderType::VERTEX]
+					  ->getImplementation()
+					  .getGlName()
+				: -1,
 			m_in.m_shaders[ShaderType::FRAGMENT].isCreated()
-			? m_in.m_shaders[ShaderType::FRAGMENT]->getImplementation().getGlName()
-			: -1,
+				? m_in.m_shaders[ShaderType::FRAGMENT]
+					  ->getImplementation()
+					  .getGlName()
+				: -1,
 			&infoLogTxt[0]);
 		err = ErrorCode::USER_DATA;
 	}

+ 86 - 0
src/misc/ConfigSet.cpp

@@ -4,7 +4,9 @@
 // http://www.anki3d.org/LICENSE
 
 #include <anki/misc/ConfigSet.h>
+#include <anki/misc/Xml.h>
 #include <anki/util/Logger.h>
+#include <anki/util/File.h>
 
 namespace anki
 {
@@ -142,4 +144,88 @@ CString ConfigSet::getString(const CString& name) const
 	return o->m_strVal.toCString();
 }
 
+//==============================================================================
+Error ConfigSet::loadFromFile(CString filename)
+{
+	ANKI_LOGI("Loading config file %s", &filename[0]);
+	XmlDocument xml;
+	ANKI_CHECK(xml.loadFile(filename, m_alloc));
+
+	XmlElement rootel;
+	ANKI_CHECK(xml.getChildElement("config", rootel));
+
+	for(Option& option : m_options)
+	{
+		XmlElement el;
+		ANKI_CHECK(
+			rootel.getChildElementOptional(option.m_name.toCString(), el));
+
+		if(el)
+		{
+			if(option.m_type == 1)
+			{
+				ANKI_CHECK(el.getF64(option.m_fVal));
+			}
+			else
+			{
+				CString txt;
+				ANKI_CHECK(el.getText(txt));
+				option.m_strVal.destroy(m_alloc);
+				option.m_strVal.create(m_alloc, txt);
+			}
+		}
+		else
+		{
+			ANKI_LOGW("Missing option for \"%s\". Will use the default value",
+				&option.m_name[0]);
+		}
+	}
+
+	return ErrorCode::NONE;
+}
+
+//==============================================================================
+Error ConfigSet::saveToFile(CString filename) const
+{
+	ANKI_LOGI("Saving config file %s", &filename[0]);
+
+	File file;
+	ANKI_CHECK(file.open(filename, File::OpenFlag::WRITE));
+
+	ANKI_CHECK(file.writeText("%s\n<config>\n", &XmlDocument::XML_HEADER[0]));
+
+	for(const Option& option : m_options)
+	{
+		if(option.m_type == 1)
+		{
+			// Some of the options are integers so try not to make them appear
+			// as floats in the file
+			if(option.m_fVal == round(option.m_fVal) && option.m_fVal >= 0.0)
+			{
+				ANKI_CHECK(file.writeText("\t<%s>%u</%s>\n",
+					&option.m_name[0],
+					U(option.m_fVal),
+					&option.m_name[0]));
+			}
+			else
+			{
+				ANKI_CHECK(file.writeText("\t<%s>%f</%s>\n",
+					&option.m_name[0],
+					option.m_fVal,
+					&option.m_name[0]));
+			}
+		}
+		else
+		{
+			ANKI_CHECK(file.writeText("\t<%s>%s</%s>\n",
+				&option.m_name[0],
+				&option.m_strVal[0],
+				&option.m_name[0]));
+		}
+	}
+
+	ANKI_CHECK(file.writeText("</config>\n"));
+	return ErrorCode::NONE;
+}
+
 } // end namespace anki

+ 3 - 0
src/misc/Xml.cpp

@@ -303,6 +303,9 @@ Error XmlElement::getSiblingElementsCount(U32& out) const
 // XmlDocument                                                                 =
 //==============================================================================
 
+//==============================================================================
+CString XmlDocument::XML_HEADER = R"(<?xml version="1.0" encoding="UTF-8" ?>)";
+
 //==============================================================================
 Error XmlDocument::loadFile(
 	const CString& filename, GenericMemoryPoolAllocator<U8> alloc)

+ 48 - 42
src/script/Event.cpp

@@ -10,7 +10,8 @@
 #include <anki/scene/SceneGraph.h>
 #include <anki/Event.h>
 
-namespace anki {
+namespace anki
+{
 
 //==============================================================================
 static EventManager* getEventManager(lua_State* l)
@@ -53,29 +54,30 @@ static inline int pwrapLightEventsetIntensityMultiplier(lua_State* l)
 	(void)voidp;
 	PtrSize size;
 	(void)size;
-	
+
 	LuaBinder::checkArgsCount(l, 2);
-	
+
 	// Get "this" as "self"
-	if(LuaBinder::checkUserData(l, 1, classnameLightEvent, 840634010629725278, ud))
+	if(LuaBinder::checkUserData(
+		   l, 1, classnameLightEvent, 840634010629725278, ud))
 	{
 		return -1;
 	}
-	
+
 	LightEvent* self = ud->getData<LightEvent>();
-	
+
 	// Pop arguments
 	if(LuaBinder::checkUserData(l, 2, "Vec4", 6804478823655046386, ud))
 	{
 		return -1;
 	}
-	
+
 	Vec4* iarg0 = ud->getData<Vec4>();
 	const Vec4& arg0(*iarg0);
-	
+
 	// Call the method
 	self->setIntensityMultiplier(arg0);
-	
+
 	return 0;
 }
 
@@ -88,7 +90,7 @@ static int wrapLightEventsetIntensityMultiplier(lua_State* l)
 	{
 		return res;
 	}
-	
+
 	lua_error(l);
 	return 0;
 }
@@ -103,33 +105,34 @@ static inline int pwrapLightEventsetFrequency(lua_State* l)
 	(void)voidp;
 	PtrSize size;
 	(void)size;
-	
+
 	LuaBinder::checkArgsCount(l, 3);
-	
+
 	// Get "this" as "self"
-	if(LuaBinder::checkUserData(l, 1, classnameLightEvent, 840634010629725278, ud))
+	if(LuaBinder::checkUserData(
+		   l, 1, classnameLightEvent, 840634010629725278, ud))
 	{
 		return -1;
 	}
-	
+
 	LightEvent* self = ud->getData<LightEvent>();
-	
+
 	// Pop arguments
 	F32 arg0;
 	if(LuaBinder::checkNumber(l, 2, arg0))
 	{
 		return -1;
 	}
-	
+
 	F32 arg1;
 	if(LuaBinder::checkNumber(l, 3, arg1))
 	{
 		return -1;
 	}
-	
+
 	// Call the method
 	self->setFrequency(arg0, arg1);
-	
+
 	return 0;
 }
 
@@ -142,7 +145,7 @@ static int wrapLightEventsetFrequency(lua_State* l)
 	{
 		return res;
 	}
-	
+
 	lua_error(l);
 	return 0;
 }
@@ -152,8 +155,10 @@ static int wrapLightEventsetFrequency(lua_State* l)
 static inline void wrapLightEvent(lua_State* l)
 {
 	LuaBinder::createClass(l, classnameLightEvent);
-	LuaBinder::pushLuaCFuncMethod(l, "setIntensityMultiplier", wrapLightEventsetIntensityMultiplier);
-	LuaBinder::pushLuaCFuncMethod(l, "setFrequency", wrapLightEventsetFrequency);
+	LuaBinder::pushLuaCFuncMethod(
+		l, "setIntensityMultiplier", wrapLightEventsetIntensityMultiplier);
+	LuaBinder::pushLuaCFuncMethod(
+		l, "setFrequency", wrapLightEventsetFrequency);
 	lua_settop(l, 0);
 }
 
@@ -186,53 +191,54 @@ static inline int pwrapEventManagernewLightEvent(lua_State* l)
 	(void)voidp;
 	PtrSize size;
 	(void)size;
-	
+
 	LuaBinder::checkArgsCount(l, 4);
-	
+
 	// Get "this" as "self"
-	if(LuaBinder::checkUserData(l, 1, classnameEventManager, -6959305329499243407, ud))
+	if(LuaBinder::checkUserData(
+		   l, 1, classnameEventManager, -6959305329499243407, ud))
 	{
 		return -1;
 	}
-	
+
 	EventManager* self = ud->getData<EventManager>();
-	
+
 	// Pop arguments
 	F32 arg0;
 	if(LuaBinder::checkNumber(l, 2, arg0))
 	{
 		return -1;
 	}
-	
+
 	F32 arg1;
 	if(LuaBinder::checkNumber(l, 3, arg1))
 	{
 		return -1;
 	}
-	
+
 	if(LuaBinder::checkUserData(l, 4, "SceneNode", -2220074417980276571, ud))
 	{
 		return -1;
 	}
-	
+
 	SceneNode* iarg2 = ud->getData<SceneNode>();
 	SceneNode* arg2(iarg2);
-	
+
 	// Call the method
 	LightEvent* ret = self->newEvent<LightEvent>(arg0, arg1, arg2);
-	
+
 	// Push return value
 	if(ANKI_UNLIKELY(ret == nullptr))
 	{
 		lua_pushstring(l, "Glue code returned nullptr");
 		return -1;
 	}
-	
+
 	voidp = lua_newuserdata(l, sizeof(UserData));
 	ud = static_cast<UserData*>(voidp);
 	luaL_setmetatable(l, "LightEvent");
 	ud->initPointed(840634010629725278, const_cast<LightEvent*>(ret));
-	
+
 	return 1;
 }
 
@@ -245,7 +251,7 @@ static int wrapEventManagernewLightEvent(lua_State* l)
 	{
 		return res;
 	}
-	
+
 	lua_error(l);
 	return 0;
 }
@@ -255,7 +261,8 @@ static int wrapEventManagernewLightEvent(lua_State* l)
 static inline void wrapEventManager(lua_State* l)
 {
 	LuaBinder::createClass(l, classnameEventManager);
-	LuaBinder::pushLuaCFuncMethod(l, "newLightEvent", wrapEventManagernewLightEvent);
+	LuaBinder::pushLuaCFuncMethod(
+		l, "newLightEvent", wrapEventManagernewLightEvent);
 	lua_settop(l, 0);
 }
 
@@ -269,24 +276,24 @@ static inline int pwrapgetEventManager(lua_State* l)
 	(void)voidp;
 	PtrSize size;
 	(void)size;
-	
+
 	LuaBinder::checkArgsCount(l, 0);
-	
+
 	// Call the function
 	EventManager* ret = getEventManager(l);
-	
+
 	// Push return value
 	if(ANKI_UNLIKELY(ret == nullptr))
 	{
 		lua_pushstring(l, "Glue code returned nullptr");
 		return -1;
 	}
-	
+
 	voidp = lua_newuserdata(l, sizeof(UserData));
 	ud = static_cast<UserData*>(voidp);
 	luaL_setmetatable(l, "EventManager");
 	ud->initPointed(-6959305329499243407, const_cast<EventManager*>(ret));
-	
+
 	return 1;
 }
 
@@ -299,7 +306,7 @@ static int wrapgetEventManager(lua_State* l)
 	{
 		return res;
 	}
-	
+
 	lua_error(l);
 	return 0;
 }
@@ -314,4 +321,3 @@ void wrapModuleEvent(lua_State* l)
 }
 
 } // end namespace anki
-

+ 3 - 1
testapp/Main.cpp

@@ -299,10 +299,12 @@ Error init(int argc, char* argv[])
 	{
 		config.set("dataPaths", "assets");
 	}
-	config.set("sceneFrameAllocatorSize", 1024 * 1024 * 10);
 	// config.set("maxTextureSize", 256);
 	// config.set("lodDistance", 3.0);
 
+	// config.saveToFile("./config.xml");
+	// config.loadFromFile("./config.xml");
+
 	app = new MyApp;
 	ANKI_CHECK(app->create(config, allocAligned, nullptr));
 	ANKI_CHECK(app->init());