caller-defined entry methods and string API update

pull/67/head
karroffel 2018-01-11 17:57:52 +01:00
parent bde1ce384f
commit 5dac1b5887
5 changed files with 72 additions and 43 deletions

View File

@ -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) \ #define GODOT_CLASS(Name) \
public: inline static const char *___get_type_name() { return static_cast<const char *>(#Name); } \ public: inline static const char *___get_type_name() { return static_cast<const char *>(#Name); } \
private: private:

View File

@ -17,6 +17,9 @@ public:
static void print_warning(const String& description, const String& function, const String& file, int line); 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 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);
}; };

View File

@ -10,6 +10,20 @@ class Variant;
class PoolByteArray; class PoolByteArray;
class PoolRealArray; class PoolRealArray;
class PoolStringArray; 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 { class String {
godot_string _godot_string; godot_string _godot_string;
@ -42,7 +56,8 @@ public:
int length() const; int length() const;
const wchar_t *unicode_str() const; const wchar_t *unicode_str() const;
char *alloc_c_string() 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(String p_what) const;
int64_t find_from(String p_what, int64_t p_from) const; int64_t find_from(String p_what, int64_t p_from) const;

View File

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

View File

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