summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/bind/core_bind.cpp6
-rw-r--r--core/bind/core_bind.h2
-rw-r--r--core/dictionary.cpp4
-rw-r--r--core/io/json.cpp53
-rw-r--r--core/io/json.h4
-rw-r--r--core/object.h5
-rw-r--r--core/os/dir_access.cpp3
-rw-r--r--core/os/file_access.h2
8 files changed, 56 insertions, 23 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp
index 999befaf67..f6011c3976 100644
--- a/core/bind/core_bind.cpp
+++ b/core/bind/core_bind.cpp
@@ -2694,12 +2694,12 @@ Variant JSONParseResult::get_result() const {
}
void _JSON::_bind_methods() {
- ClassDB::bind_method(D_METHOD("print", "value"), &_JSON::print);
+ ClassDB::bind_method(D_METHOD("print", "value", "indent", "sort_keys"), &_JSON::print, DEFVAL(String()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse);
}
-String _JSON::print(const Variant &p_value) {
- return JSON::print(p_value);
+String _JSON::print(const Variant &p_value, const String &p_indent, bool p_sort_keys) {
+ return JSON::print(p_value, p_indent, p_sort_keys);
}
Ref<JSONParseResult> _JSON::parse(const String &p_json) {
diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h
index 8163b08d76..b642a907fb 100644
--- a/core/bind/core_bind.h
+++ b/core/bind/core_bind.h
@@ -719,7 +719,7 @@ protected:
public:
static _JSON *get_singleton() { return singleton; }
- String print(const Variant &p_value);
+ String print(const Variant &p_value, const String &p_indent = "", bool p_sort_keys = false);
Ref<JSONParseResult> parse(const String &p_json);
_JSON();
diff --git a/core/dictionary.cpp b/core/dictionary.cpp
index 48e65c734f..44fce2474f 100644
--- a/core/dictionary.cpp
+++ b/core/dictionary.cpp
@@ -204,7 +204,9 @@ const Variant *Dictionary::next(const Variant *p_key) const {
if (p_key == NULL) {
// caller wants to get the first element
- return &_p->variant_map.front().key();
+ if (_p->variant_map.front())
+ return &_p->variant_map.front().key();
+ return NULL;
}
OrderedHashMap<Variant, Variant, _DictionaryVariantHash>::Element E = _p->variant_map.find(*p_key);
diff --git a/core/io/json.cpp b/core/io/json.cpp
index 2e9170bc34..ddfc792cc7 100644
--- a/core/io/json.cpp
+++ b/core/io/json.cpp
@@ -43,7 +43,25 @@ const char *JSON::tk_name[TK_MAX] = {
"EOF",
};
-String JSON::_print_var(const Variant &p_var) {
+static String _make_indent(const String& p_indent, int p_size) {
+
+ String indent_text = "";
+ if (!p_indent.empty()) {
+ for (int i = 0; i < p_size; i++)
+ indent_text += p_indent;
+ }
+ return indent_text;
+}
+
+String JSON::_print_var(const Variant &p_var, const String& p_indent, int p_cur_indent, bool p_sort_keys) {
+
+ String colon = ":";
+ String end_statement = "";
+
+ if (!p_indent.empty()) {
+ colon += " ";
+ end_statement += "\n";
+ }
switch (p_var.get_type()) {
@@ -57,41 +75,50 @@ String JSON::_print_var(const Variant &p_var) {
case Variant::ARRAY: {
String s = "[";
+ s += end_statement;
Array a = p_var;
for (int i = 0; i < a.size(); i++) {
- if (i > 0)
- s += ", ";
- s += _print_var(a[i]);
+ if (i > 0) {
+ s += ",";
+ s += end_statement;
+ }
+ s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(a[i], p_indent, p_cur_indent + 1, p_sort_keys);
}
- s += "]";
+ s += end_statement + _make_indent(p_indent, p_cur_indent) + "]";
return s;
};
case Variant::DICTIONARY: {
String s = "{";
+ s += end_statement;
Dictionary d = p_var;
List<Variant> keys;
d.get_key_list(&keys);
+ if (p_sort_keys)
+ keys.sort();
+
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
- if (E != keys.front())
- s += ", ";
- s += _print_var(String(E->get()));
- s += ":";
- s += _print_var(d[E->get()]);
+ if (E != keys.front()) {
+ s += ",";
+ s += end_statement;
+ }
+ s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(String(E->get()), p_indent, p_cur_indent + 1, p_sort_keys);
+ s += colon;
+ s += _print_var(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys);
}
- s += "}";
+ s += end_statement + _make_indent(p_indent, p_cur_indent) + "}";
return s;
};
default: return "\"" + String(p_var).json_escape() + "\"";
}
}
-String JSON::print(const Variant &p_var) {
+String JSON::print(const Variant &p_var, const String& p_indent, bool p_sort_keys) {
- return _print_var(p_var);
+ return _print_var(p_var, p_indent, 0, p_sort_keys);
}
Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) {
diff --git a/core/io/json.h b/core/io/json.h
index 893a88e264..5e1a89f069 100644
--- a/core/io/json.h
+++ b/core/io/json.h
@@ -64,7 +64,7 @@ class JSON {
static const char *tk_name[TK_MAX];
- static String _print_var(const Variant &p_var);
+ static String _print_var(const Variant &p_var, const String& p_indent, int p_cur_indent, bool p_sort_keys);
static Error _get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
static Error _parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
@@ -72,7 +72,7 @@ class JSON {
static Error _parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
public:
- static String print(const Variant &p_var);
+ static String print(const Variant &p_var, const String& p_indent = "", bool p_sort_keys = true);
static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);
};
diff --git a/core/object.h b/core/object.h
index 3ac699f978..0a0c781649 100644
--- a/core/object.h
+++ b/core/object.h
@@ -1,4 +1,4 @@
-/*************************************************************************/
+/*************************************************************************/
/* object.h */
/*************************************************************************/
/* This file is part of: */
@@ -109,10 +109,11 @@ enum PropertyUsageFlags {
PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 17,
PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 18,
PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 19,
+ PROPERTY_USAGE_INTERNAL = 1 << 20,
PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK,
PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED,
- PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK,
+ PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNAL,
};
#define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal)
diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp
index 6d4b46f4da..e19c8e8ea5 100644
--- a/core/os/dir_access.cpp
+++ b/core/os/dir_access.cpp
@@ -333,6 +333,9 @@ Error DirAccess::copy(String p_from, String p_to, int chmod_flags) {
if (err == OK && chmod_flags != -1) {
fdst->close();
err = fdst->_chmod(p_to, chmod_flags);
+ // If running on a platform with no chmod support (i.e., Windows), don't fail
+ if (err == ERR_UNAVAILABLE)
+ err = OK;
}
memdelete(fsrc);
diff --git a/core/os/file_access.h b/core/os/file_access.h
index 455dd1ea99..6fda3d9668 100644
--- a/core/os/file_access.h
+++ b/core/os/file_access.h
@@ -141,7 +141,7 @@ public:
virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType
- virtual Error _chmod(const String &p_path, int p_mod) { return FAILED; }
+ virtual Error _chmod(const String &p_path, int p_mod) { return ERR_UNAVAILABLE; }
static FileAccess *create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files.
static FileAccess *create_for_path(const String &p_path);