Przeglądaj źródła

JSONParser changes, TODO: good interface for accessing json streams

mikymod 12 lat temu
rodzic
commit
cbaafd97ee
6 zmienionych plików z 52 dodań i 23 usunięć
  1. 5 1
      samples/lua/lua.cpp
  2. 1 0
      src/Crown.h
  3. 20 0
      src/Device.cpp
  4. 1 0
      src/Device.h
  5. 20 4
      src/JSONParser.cpp
  6. 5 18
      src/JSONParser.h

+ 5 - 1
samples/lua/lua.cpp

@@ -5,6 +5,8 @@
 namespace crown
 {
 
+StringSetting g_boot("boot_file", "lua main file", "lua/game.raw");
+
 lua_State* L;
 
 void init()
@@ -14,7 +16,9 @@ void init()
 
 	lua_cpcall(L, luaopen_libcrownlua, NULL);
 
-	if (luaL_loadfile(L, "/home/mikymod/test/res_linux/lua/game.raw") || lua_pcall(L, 0, 0, 0))
+	const char* path = device()->filesystem()->os_path(g_boot.value());
+
+	if (luaL_loadfile(L, path) || lua_pcall(L, 0, 0, 0))
 	{
 		os::printf("error: %s", lua_tostring(L, -1));
 	}

+ 1 - 0
src/Crown.h

@@ -94,6 +94,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 // Core/Settings
 #include "IntSetting.h"
 #include "FloatSetting.h"
+#include "StringSetting.h"
 
 // Engine
 #include "Camera.h"

+ 20 - 0
src/Device.cpp

@@ -46,6 +46,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Mouse.h"
 #include "Touch.h"
 #include "Accelerometer.h"
+#include "JSONParser.h"
+#include "DiskFile.h"
 
 #ifdef CROWN_BUILD_OPENGL
 	#include "renderers/gl/GLRenderer.h"
@@ -123,6 +125,8 @@ bool Device::init(int argc, char** argv)
 
 	create_debug_renderer();
 
+	read_engine_settings();
+
 	Log::i("Crown Engine initialized.");
 
 	Log::i("Initializing Game...");
@@ -519,6 +523,22 @@ void Device::check_preferred_settings()
 	}
 }
 
+//-----------------------------------------------------------------------------
+void Device::read_engine_settings()
+{
+	DiskFile* file = m_filesystem->open("crown.cfg", FOM_READ);
+	JSONParser json(file);
+	json.parse();
+	JSONToken* tokens = json.get_tokens();
+
+	for (int i = 0; i < json.get_tokens_number(); i++)
+	{
+		os::printf("id: %d\n", i);
+		tokens[i].print();
+	}
+}
+
+
 //-----------------------------------------------------------------------------
 void Device::print_help_message()
 {

+ 1 - 0
src/Device.h

@@ -116,6 +116,7 @@ private:
 
 	void					parse_command_line(int argc, char** argv);
 	void					check_preferred_settings();
+	void					read_engine_settings();
 	void					print_help_message();
 
 private:

+ 20 - 4
src/JSONParser.cpp

@@ -1,7 +1,7 @@
 #include "JSONParser.h"
 #include "DiskFile.h"
-#include "String.h"
 #include "OS.h"
+#include "String.h"
 
 namespace crown
 {
@@ -10,8 +10,7 @@ namespace crown
 JSONParser::JSONParser(File* file, size_t size) :
 	m_file(file),
 	m_next_token(0),
-	m_prev_token(-1),
-	m_key_set(false)
+	m_prev_token(-1)
 {
 	if (size > 1024)
 	{
@@ -321,7 +320,6 @@ void JSONParser::fill_token(JSONToken* token, JSONType type, int32_t start, int3
 	m_file->read(tmp, token->m_size);
 	tmp[token->m_size] = '\0';
 	string::strcpy(token->m_value, tmp);
-	os:printf("%d\n", string::strlen(token->m_value));
 
 	m_file->seek(cur_pos);
 }
@@ -336,5 +334,23 @@ int32_t JSONParser::get_tokens_number()
 	return m_next_token;
 }
 
+// const char** JSONParser::parse_arguments(const char* first, ...)
+// {
+	// va_list args;
+
+	// va_start(args, first);
+
+	// const char* arg;
+
+	// arg = va_arg(first, const char*);
+	// os::printf("%s\n", arg);
+
+	// while ((arg = va_arg(args, const char*)) != 0)
+	// {
+	// 	os::printf("%s\n", arg);
+	// }
+	// va_end(args);
+// }
+
 
 } //namespace crown

+ 5 - 18
src/JSONParser.h

@@ -1,8 +1,11 @@
 #pragma once
 
+#include <cstdarg>
 #include "Types.h"
 #include "OS.h"
 #include "File.h"
+#include "List.h"
+#include "Dictionary.h"
 
 namespace crown
 {
@@ -38,7 +41,6 @@ struct JSONToken
 	size_t 		m_size;					// Token's dimension
 	int32_t 	m_parent;				// Token's parent
 
-
 	inline void print()
 	{
 		os::printf("Value:\t%s\n", m_value);
@@ -68,20 +70,7 @@ public:
 	/// Get next token
 	int32_t			get_tokens_number();
 
-	bool 			get_bool(const char* name);
-
-	int32_t 		get_int(const char* name);
-
-	float			get_float(const char* name);
-
-	// void			get_string(const char* name, List<char>& string);
-
-	// void			get_array(const char* name, List<const char*>& array);
-
-	// void			get_object(const char* name, Dictionary<String, const char*>& obj);
-
 private:
-
 	/// Parse string in JSON data
 	JSONError		parse_string();
 	/// Parse number or boolean in JSON data
@@ -91,6 +80,8 @@ private:
 	/// Fill token and set boundaries
 	void			fill_token(JSONToken* token, JSONType type, int32_t start, int32_t end);
 
+	// const char**	parse_arguments(const char* first, ...);
+
 	/// JSON file of data
 	File*			m_file;
 	/// JSON string offset
@@ -99,8 +90,6 @@ private:
 	int32_t			m_next_token;
 	/// Previous token e.g parent or array		
 	int32_t			m_prev_token;
-	/// true if the current token is a key, false otherwise
-	bool 			m_key_set;
 
 	/// JSON tokens list, used as default
 	JSONToken		m_tokens_list[1024];
@@ -108,8 +97,6 @@ private:
 	JSONToken* 		m_tokens;
 	/// m_tokens default size, default 1024
 	size_t			m_size;
-
-
 };
 
 } // namespace crown