Sfoglia il codice sorgente

Config can now be set using command line arguments

Panagiotis Christopoulos Charitos 9 anni fa
parent
commit
e257f7be93
3 ha cambiato i file con 50 aggiunte e 1 eliminazioni
  1. 3 0
      include/anki/misc/ConfigSet.h
  2. 2 1
      sandbox/Main.cpp
  3. 45 0
      src/misc/ConfigSet.cpp

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

@@ -47,6 +47,9 @@ public:
 
 	ANKI_USE_RESULT Error saveToFile(CString filename) const;
 
+	ANKI_USE_RESULT Error setFromCommandLineArguments(
+		U cmdLineArgsCount, char* cmdLineArgs[]);
+
 protected:
 	void newOption(const CString& name, const CString& value);
 	void newOption(const CString& name, F64 value);

+ 2 - 1
sandbox/Main.cpp

@@ -27,7 +27,7 @@ MyApp* app = nullptr;
 //==============================================================================
 Error MyApp::init(int argc, char* argv[])
 {
-	if(argc != 3)
+	if(argc < 3)
 	{
 		ANKI_LOGE("usage: %s /path/to/config.xml relative/path/to/scene.lua",
 			argv[0]);
@@ -37,6 +37,7 @@ Error MyApp::init(int argc, char* argv[])
 	// Config
 	Config config;
 	ANKI_CHECK(config.loadFromFile(argv[1]));
+	ANKI_CHECK(config.setFromCommandLineArguments(argc, argv));
 
 	// Init super class
 	ANKI_CHECK(App::init(config, allocAligned, nullptr));

+ 45 - 0
src/misc/ConfigSet.cpp

@@ -228,4 +228,49 @@ Error ConfigSet::saveToFile(CString filename) const
 	return ErrorCode::NONE;
 }
 
+//==============================================================================
+Error ConfigSet::setFromCommandLineArguments(
+	U cmdLineArgsCount, char* cmdLineArgs[])
+{
+	for(U i = 0; i < cmdLineArgsCount; ++i)
+	{
+		const char* arg = cmdLineArgs[i];
+		ANKI_ASSERT(arg);
+		if(CString(arg) == "-cfg")
+		{
+			if(i + 2 >= cmdLineArgsCount)
+			{
+				ANKI_LOGE("Wrong number of arguments after -cfg");
+				return ErrorCode::USER_DATA;
+			}
+
+			// Get the option
+			arg = cmdLineArgs[i + 1];
+			ANKI_ASSERT(arg);
+			Option* option = tryFind(arg);
+			if(option == nullptr)
+			{
+				ANKI_LOGE("Option name following -cfg not found: %s", arg);
+				return ErrorCode::USER_DATA;
+			}
+
+			// Set the value
+			arg = cmdLineArgs[i + 2];
+			ANKI_ASSERT(arg);
+			if(option->m_type == 0)
+			{
+				option->m_strVal.destroy(m_alloc);
+				option->m_strVal.create(m_alloc, arg);
+			}
+			else
+			{
+				CString val(arg);
+				ANKI_CHECK(val.toF64(option->m_fVal));
+			}
+		}
+	}
+
+	return ErrorCode::NONE;
+}
+
 } // end namespace anki