Browse Source

caller-defined entry methods and string API update

karroffel 7 years ago
parent
commit
5dac1b5887
5 changed files with 72 additions and 43 deletions
  1. 0 7
      include/core/Godot.hpp
  2. 3 0
      include/core/GodotGlobal.hpp
  3. 16 1
      include/core/String.hpp
  4. 6 12
      src/core/GodotGlobal.cpp
  5. 47 23
      src/core/String.cpp

+ 0 - 7
include/core/Godot.hpp

@@ -38,13 +38,6 @@ public:
 
 
 
-
-
-#define GDNATIVE_INIT(arg) void gdnative_init(arg)
-#define GDNATIVE_TERMINATE(arg) void gdnative_terminate(arg)
-
-#define NATIVESCRIPT_INIT() void nativescript_init()
-
 #define GODOT_CLASS(Name) \
 	public: inline static const char *___get_type_name() { return static_cast<const char *>(#Name); } \
 	private:

+ 3 - 0
include/core/GodotGlobal.hpp

@@ -17,6 +17,9 @@ public:
 	static void print_warning(const String& description, const String& function, const String& file, int line);
 	static void print_error(const String& description, const String& function, const String& file, int line);
 
+	static void gdnative_init(godot_gdnative_init_options *o);
+	static void gdnative_terminate(godot_gdnative_terminate_options *o);
+	static void nativescript_init(void *handle);
 };
 
 

+ 16 - 1
include/core/String.hpp

@@ -10,6 +10,20 @@ class Variant;
 class PoolByteArray;
 class PoolRealArray;
 class PoolStringArray;
+class String;
+
+class CharString {
+
+	friend class String;
+
+	godot_char_string _char_string;
+
+public:
+	~CharString();
+
+	int length() const;
+	const char *get_data() const;
+};
 
 class String {
 	godot_string _godot_string;
@@ -42,7 +56,8 @@ public:
 	int length() const;
 	const wchar_t *unicode_str() const;
 	char *alloc_c_string() const;
-	void get_c_string(char *p_dest, int *p_size) const;
+	CharString utf8() const;
+	CharString ascii(bool p_extended = false) const;
 
 	int64_t find(String p_what) const;
 	int64_t find_from(String p_what, int64_t p_from) const;

+ 6 - 12
src/core/GodotGlobal.cpp

@@ -47,10 +47,7 @@ void Godot::print_error(const String& description, const String& function, const
 	if (c_file != NULL) godot::api->godot_free(c_file);
 }
 
-};
-
-void gdnative_init(godot_gdnative_init_options *options);
-extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *options)
+void Godot::gdnative_init(godot_gdnative_init_options *options)
 {
 	godot::api = options->api_struct;
 
@@ -63,19 +60,16 @@ extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *opti
 			default: break;
 		};
 	};
-
-	gdnative_init(options);
 }
 
-void gdnative_terminate(godot_gdnative_terminate_options *options);
-extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *options)
+void Godot::gdnative_terminate(godot_gdnative_terminate_options *options)
 {
-	gdnative_terminate(options);
+	// reserved for future use.
 }
 
-void nativescript_init();
-extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
+void Godot::nativescript_init(void *handle)
 {
 	godot::_RegisterState::nativescript_handle = handle;
-	nativescript_init();
 }
+
+};

+ 47 - 23
src/core/String.cpp

@@ -12,23 +12,33 @@
 
 namespace godot {
 
+godot::CharString::~CharString() {
+	godot::api->godot_char_string_destroy(&_char_string);
+}
+
+int godot::CharString::length() const {
+	return godot::api->godot_char_string_length(&_char_string);
+}
+
+const char *godot::CharString::get_data() const {
+	return godot::api->godot_char_string_get_data(&_char_string);
+}
+
 godot::String::String() {
 	godot::api->godot_string_new(&_godot_string);
 }
 
 String::String(const char *contents) {
-	godot::api->godot_string_new_data(&_godot_string, contents, strlen(contents));
+	godot::api->godot_string_new(&_godot_string);
+	godot::api->godot_string_parse_utf8(&_godot_string, contents);
 }
 
 String::String(const wchar_t *contents) {
-	// @Todo
-	// godot::api->godot_string_new_data(&_godot_string, contents, strlen(contents));
-	godot::api->godot_string_new(&_godot_string);
+	godot::api->godot_string_new_with_wide_string(&_godot_string, contents, wcslen(contents));
 }
 
 String::String(const wchar_t c) {
-	// @Todo
-	godot::api->godot_string_new(&_godot_string);
+	godot::api->godot_string_new_with_wide_string(&_godot_string, &c, 1);
 }
 
 String::String(const String &other) {
@@ -48,9 +58,7 @@ wchar_t String::operator[](const int idx) const {
 }
 
 int String::length() const {
-	int len = 0;
-	godot::api->godot_string_get_data(&_godot_string, nullptr, &len);
-	return len;
+	return godot::api->godot_string_length(&_godot_string);
 }
 
 void String::operator=(const String &s) {
@@ -68,8 +76,7 @@ bool String::operator!=(const String &s) {
 
 String String::operator+(const String &s) {
 	String new_string = *this;
-	new_string._godot_string =
-			godot::api->godot_string_operator_plus(&new_string._godot_string, &s._godot_string);
+	new_string._godot_string = godot::api->godot_string_operator_plus(&new_string._godot_string, &s._godot_string);
 
 	return new_string;
 }
@@ -104,27 +111,44 @@ String::operator NodePath() const {
 }
 
 const wchar_t *String::unicode_str() const {
-	return godot::api->godot_string_unicode_str(&_godot_string);
+	return godot::api->godot_string_wide_str(&_godot_string);
 }
 
 char *String::alloc_c_string() const {
-	int len;
 
-	// figure out the lenght of our string
-	get_c_string(NULL, &len);
+	godot_char_string contents = godot::api->godot_string_utf8(&_godot_string);
 
-	// allocate our buffer
-	char * result = (char *)godot::api->godot_alloc(len + 1);
-	if (result != NULL) {
-		get_c_string(result, &len);
-		result[len] = '\0';
+	int length = godot::api->godot_char_string_length(&contents);
+
+	char *result = (char *) godot::api->godot_alloc(length + 1);
+
+	if (result) {
+		memcpy(result, godot::api->godot_char_string_get_data(&contents), length + 1);
 	}
-	
+
+	godot::api->godot_char_string_destroy(&contents);
+
 	return result;
 }
 
-void String::get_c_string(char *p_dest, int *p_size) const {
-	godot::api->godot_string_get_data(&_godot_string, p_dest, p_size);
+CharString String::utf8() const {
+	CharString ret;
+
+	ret._char_string = godot::api->godot_string_utf8(&_godot_string);
+
+	return ret;
+}
+
+CharString String::ascii(bool p_extended) const {
+
+	CharString ret;
+
+	if (p_extended)
+		ret._char_string = godot::api->godot_string_ascii_extended(&_godot_string);
+	else
+		ret._char_string = godot::api->godot_string_ascii(&_godot_string);
+
+	return ret;
 }
 
 String operator+(const char *a, const String &b) {