diff options
Diffstat (limited to 'core')
98 files changed, 2262 insertions, 5638 deletions
diff --git a/core/SCsub b/core/SCsub index caae3a1c9b..8d89f6427b 100644 --- a/core/SCsub +++ b/core/SCsub @@ -15,7 +15,7 @@ for x in env.global_defaults: gd_cpp = '#include "globals.h"\n' gd_cpp += gd_inc -gd_cpp += "void Globals::register_global_defaults() {\n" + gd_call + "\n}\n" +gd_cpp += "void GlobalConfig::register_global_defaults() {\n" + gd_call + "\n}\n" f = open("global_defaults.cpp", "wb") f.write(gd_cpp) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 02f3c4c5f3..4b23761e4e 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -69,11 +69,11 @@ RES _ResourceLoader::load(const String &p_path,const String& p_type_hint, bool p return ret; } -DVector<String> _ResourceLoader::get_recognized_extensions_for_type(const String& p_type) { +PoolVector<String> _ResourceLoader::get_recognized_extensions_for_type(const String& p_type) { List<String> exts; ResourceLoader::get_recognized_extensions_for_type(p_type,&exts); - DVector<String> ret; + PoolVector<String> ret; for(List<String>::Element *E=exts.front();E;E=E->next()) { ret.push_back(E->get()); @@ -102,7 +102,7 @@ StringArray _ResourceLoader::get_dependencies(const String& p_path) { bool _ResourceLoader::has(const String &p_path) { - String local_path = Globals::get_singleton()->localize_path(p_path); + String local_path = GlobalConfig::get_singleton()->localize_path(p_path); return ResourceCache::has(local_path); }; @@ -135,12 +135,12 @@ Error _ResourceSaver::save(const String &p_path,const RES& p_resource, uint32_t return ResourceSaver::save(p_path,p_resource, p_flags); } -DVector<String> _ResourceSaver::get_recognized_extensions(const RES& p_resource) { +PoolVector<String> _ResourceSaver::get_recognized_extensions(const RES& p_resource) { - ERR_FAIL_COND_V(p_resource.is_null(),DVector<String>()); + ERR_FAIL_COND_V(p_resource.is_null(),PoolVector<String>()); List<String> exts; ResourceSaver::get_recognized_extensions(p_resource,&exts); - DVector<String> ret; + PoolVector<String> ret; for(List<String>::Element *E=exts.front();E;E=E->next()) { ret.push_back(E->get()); @@ -553,6 +553,16 @@ void _OS::set_icon(const Image& p_icon) { OS::get_singleton()->set_icon(p_icon); } +int _OS::get_exit_code() const { + + return OS::get_singleton()->get_exit_code(); +} + +void _OS::set_exit_code(int p_code) { + + OS::get_singleton()->set_exit_code(p_code); +} + /** * Get current datetime with consideration for utc and * dst @@ -1112,6 +1122,9 @@ void _OS::_bind_methods() { ClassDB::bind_method(_MD("set_icon","icon"),&_OS::set_icon); + ClassDB::bind_method(_MD("get_exit_code"),&_OS::get_exit_code); + ClassDB::bind_method(_MD("set_exit_code","code"),&_OS::set_exit_code); + ClassDB::bind_method(_MD("delay_usec","usec"),&_OS::delay_usec); ClassDB::bind_method(_MD("delay_msec","msec"),&_OS::delay_msec); ClassDB::bind_method(_MD("get_ticks_msec"),&_OS::get_ticks_msec); @@ -1232,16 +1245,16 @@ _Geometry *_Geometry::get_singleton() { return singleton; } -DVector<Plane> _Geometry::build_box_planes(const Vector3& p_extents) { +PoolVector<Plane> _Geometry::build_box_planes(const Vector3& p_extents) { return Geometry::build_box_planes(p_extents); } -DVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { +PoolVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { return Geometry::build_cylinder_planes(p_radius,p_height,p_sides,p_axis); } -DVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { +PoolVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { return Geometry::build_capsule_planes(p_radius,p_height,p_sides,p_lats,p_axis); } @@ -1262,22 +1275,22 @@ Variant _Geometry::segment_intersects_segment_2d(const Vector2& p_from_a,const V }; }; -DVector<Vector2> _Geometry::get_closest_points_between_segments_2d( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2) { +PoolVector<Vector2> _Geometry::get_closest_points_between_segments_2d( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2) { Vector2 r1, r2; Geometry::get_closest_points_between_segments(p1,q1,p2,q2,r1,r2); - DVector<Vector2> r; + PoolVector<Vector2> r; r.resize(2); r.set(0,r1); r.set(1,r2); return r; } -DVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3& p1,const Vector3& p2,const Vector3& q1,const Vector3& q2) { +PoolVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3& p1,const Vector3& p2,const Vector3& q1,const Vector3& q2) { Vector3 r1, r2; Geometry::get_closest_points_between_segments(p1,p2,q1,q2,r1,r2); - DVector<Vector3> r; + PoolVector<Vector3> r; r.resize(2); r.set(0,r1); r.set(1,r2); @@ -1314,9 +1327,9 @@ bool _Geometry::point_is_inside_triangle(const Vector2& s, const Vector2& a, con return Geometry::is_point_in_triangle(s,a,b,c); } -DVector<Vector3> _Geometry::segment_intersects_sphere( const Vector3& p_from, const Vector3& p_to, const Vector3& p_sphere_pos,real_t p_sphere_radius) { +PoolVector<Vector3> _Geometry::segment_intersects_sphere( const Vector3& p_from, const Vector3& p_to, const Vector3& p_sphere_pos,real_t p_sphere_radius) { - DVector<Vector3> r; + PoolVector<Vector3> r; Vector3 res,norm; if (!Geometry::segment_intersects_sphere(p_from,p_to,p_sphere_pos,p_sphere_radius,&res,&norm)) return r; @@ -1326,9 +1339,9 @@ DVector<Vector3> _Geometry::segment_intersects_sphere( const Vector3& p_from, co r.set(1,norm); return r; } -DVector<Vector3> _Geometry::segment_intersects_cylinder( const Vector3& p_from, const Vector3& p_to, float p_height,float p_radius) { +PoolVector<Vector3> _Geometry::segment_intersects_cylinder( const Vector3& p_from, const Vector3& p_to, float p_height,float p_radius) { - DVector<Vector3> r; + PoolVector<Vector3> r; Vector3 res,norm; if (!Geometry::segment_intersects_cylinder(p_from,p_to,p_height,p_radius,&res,&norm)) return r; @@ -1339,9 +1352,9 @@ DVector<Vector3> _Geometry::segment_intersects_cylinder( const Vector3& p_from, return r; } -DVector<Vector3> _Geometry::segment_intersects_convex(const Vector3& p_from, const Vector3& p_to,const Vector<Plane>& p_planes) { +PoolVector<Vector3> _Geometry::segment_intersects_convex(const Vector3& p_from, const Vector3& p_to,const Vector<Plane>& p_planes) { - DVector<Vector3> r; + PoolVector<Vector3> r; Vector3 res,norm; if (!Geometry::segment_intersects_convex(p_from,p_to,p_planes.ptr(),p_planes.size(),&res,&norm)) return r; @@ -1566,9 +1579,9 @@ real_t _File::get_real() const{ return f->get_real(); } -DVector<uint8_t> _File::get_buffer(int p_length) const{ +PoolVector<uint8_t> _File::get_buffer(int p_length) const{ - DVector<uint8_t> data; + PoolVector<uint8_t> data; ERR_FAIL_COND_V(!f,data); ERR_FAIL_COND_V(p_length<0,data); @@ -1576,11 +1589,11 @@ DVector<uint8_t> _File::get_buffer(int p_length) const{ return data; Error err = data.resize(p_length); ERR_FAIL_COND_V(err!=OK,data); - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); int len = f->get_buffer(&w[0],p_length); - ERR_FAIL_COND_V( len < 0 , DVector<uint8_t>()); + ERR_FAIL_COND_V( len < 0 , PoolVector<uint8_t>()); - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); if (len < p_length) data.resize(p_length); @@ -1735,7 +1748,7 @@ void _File::store_line(const String& p_string){ f->store_line(p_string); } -void _File::store_buffer(const DVector<uint8_t>& p_buffer){ +void _File::store_buffer(const PoolVector<uint8_t>& p_buffer){ ERR_FAIL_COND(!f); @@ -1743,7 +1756,7 @@ void _File::store_buffer(const DVector<uint8_t>& p_buffer){ if (len==0) return; - DVector<uint8_t>::Read r = p_buffer.read(); + PoolVector<uint8_t>::Read r = p_buffer.read(); f->store_buffer(&r[0],len); } @@ -1762,13 +1775,13 @@ void _File::store_var(const Variant& p_var) { Error err = encode_variant(p_var,NULL,len); ERR_FAIL_COND( err != OK ); - DVector<uint8_t> buff; + PoolVector<uint8_t> buff; buff.resize(len); - DVector<uint8_t>::Write w = buff.write(); + PoolVector<uint8_t>::Write w = buff.write(); err = encode_variant(p_var,&w[0],len); ERR_FAIL_COND( err != OK ); - w=DVector<uint8_t>::Write(); + w=PoolVector<uint8_t>::Write(); store_32(len); store_buffer(buff); @@ -1778,10 +1791,10 @@ Variant _File::get_var() const { ERR_FAIL_COND_V(!f,Variant()); uint32_t len = get_32(); - DVector<uint8_t> buff = get_buffer(len); + PoolVector<uint8_t> buff = get_buffer(len); ERR_FAIL_COND_V(buff.size() != len, Variant()); - DVector<uint8_t>::Read r = buff.read(); + PoolVector<uint8_t>::Read r = buff.read(); Variant v; Error err = decode_variant(v,&r[0],len); @@ -2056,17 +2069,17 @@ String _Marshalls::variant_to_base64(const Variant& p_var) { Error err = encode_variant(p_var,NULL,len); ERR_FAIL_COND_V( err != OK, "" ); - DVector<uint8_t> buff; + PoolVector<uint8_t> buff; buff.resize(len); - DVector<uint8_t>::Write w = buff.write(); + PoolVector<uint8_t>::Write w = buff.write(); err = encode_variant(p_var,&w[0],len); ERR_FAIL_COND_V( err != OK, "" ); int b64len = len / 3 * 4 + 4 + 1; - DVector<uint8_t> b64buff; + PoolVector<uint8_t> b64buff; b64buff.resize(b64len); - DVector<uint8_t>::Write w64 = b64buff.write(); + PoolVector<uint8_t>::Write w64 = b64buff.write(); int strlen = base64_encode((char*)(&w64[0]), (char*)(&w[0]), len); //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen); @@ -2081,9 +2094,9 @@ Variant _Marshalls::base64_to_variant(const String& p_str) { int strlen = p_str.length(); CharString cstr = p_str.ascii(); - DVector<uint8_t> buf; + PoolVector<uint8_t> buf; buf.resize(strlen / 4 * 3 + 1); - DVector<uint8_t>::Write w = buf.write(); + PoolVector<uint8_t>::Write w = buf.write(); int len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); @@ -2094,15 +2107,15 @@ Variant _Marshalls::base64_to_variant(const String& p_str) { return v; }; -String _Marshalls::raw_to_base64(const DVector<uint8_t> &p_arr) { +String _Marshalls::raw_to_base64(const PoolVector<uint8_t> &p_arr) { int len = p_arr.size(); - DVector<uint8_t>::Read r = p_arr.read(); + PoolVector<uint8_t>::Read r = p_arr.read(); int b64len = len / 3 * 4 + 4 + 1; - DVector<uint8_t> b64buff; + PoolVector<uint8_t> b64buff; b64buff.resize(b64len); - DVector<uint8_t>::Write w64 = b64buff.write(); + PoolVector<uint8_t>::Write w64 = b64buff.write(); int strlen = base64_encode((char*)(&w64[0]), (char*)(&r[0]), len); w64[strlen] = 0; @@ -2111,22 +2124,22 @@ String _Marshalls::raw_to_base64(const DVector<uint8_t> &p_arr) { return ret; }; -DVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) { +PoolVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) { int strlen = p_str.length(); CharString cstr = p_str.ascii(); int arr_len; - DVector<uint8_t> buf; + PoolVector<uint8_t> buf; { buf.resize(strlen / 4 * 3 + 1); - DVector<uint8_t>::Write w = buf.write(); + PoolVector<uint8_t>::Write w = buf.write(); arr_len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); }; buf.resize(arr_len); - // conversion from DVector<uint8_t> to raw array? + // conversion from PoolVector<uint8_t> to raw array? return buf; }; @@ -2136,9 +2149,9 @@ String _Marshalls::utf8_to_base64(const String& p_str) { int len = cstr.length(); int b64len = len / 3 * 4 + 4 + 1; - DVector<uint8_t> b64buff; + PoolVector<uint8_t> b64buff; b64buff.resize(b64len); - DVector<uint8_t>::Write w64 = b64buff.write(); + PoolVector<uint8_t>::Write w64 = b64buff.write(); int strlen = base64_encode((char*)(&w64[0]), (char*)cstr.get_data(), len); @@ -2153,9 +2166,9 @@ String _Marshalls::base64_to_utf8(const String& p_str) { int strlen = p_str.length(); CharString cstr = p_str.ascii(); - DVector<uint8_t> buf; + PoolVector<uint8_t> buf; buf.resize(strlen / 4 * 3 + 1 + 1); - DVector<uint8_t>::Write w = buf.write(); + PoolVector<uint8_t>::Write w = buf.write(); int len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 9a4f26a12d..59f243a0ee 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -50,7 +50,7 @@ public: static _ResourceLoader *get_singleton() { return singleton; } Ref<ResourceInteractiveLoader> load_interactive(const String& p_path,const String& p_type_hint=""); RES load(const String &p_path,const String& p_type_hint="", bool p_no_cache = false); - DVector<String> get_recognized_extensions_for_type(const String& p_type); + PoolVector<String> get_recognized_extensions_for_type(const String& p_type); void set_abort_on_missing_resources(bool p_abort); StringArray get_dependencies(const String& p_path); bool has(const String& p_path); @@ -81,7 +81,7 @@ public: static _ResourceSaver *get_singleton() { return singleton; } Error save(const String &p_path,const RES& p_resource, uint32_t p_flags); - DVector<String> get_recognized_extensions(const RES& p_resource); + PoolVector<String> get_recognized_extensions(const RES& p_resource); _ResourceSaver(); @@ -246,6 +246,9 @@ public: void set_use_file_access_save_and_swap(bool p_enable); void set_icon(const Image& p_icon); + + int get_exit_code() const; + void set_exit_code(int p_code); Dictionary get_date(bool utc) const; Dictionary get_time(bool utc) const; Dictionary get_datetime(bool utc) const; @@ -342,20 +345,20 @@ protected: public: static _Geometry *get_singleton(); - DVector<Plane> build_box_planes(const Vector3& p_extents); - DVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z); - DVector<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis=Vector3::AXIS_Z); + PoolVector<Plane> build_box_planes(const Vector3& p_extents); + PoolVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z); + PoolVector<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis=Vector3::AXIS_Z); Variant segment_intersects_segment_2d(const Vector2& p_from_a,const Vector2& p_to_a,const Vector2& p_from_b,const Vector2& p_to_b); - DVector<Vector2> get_closest_points_between_segments_2d( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2); - DVector<Vector3> get_closest_points_between_segments(const Vector3& p1,const Vector3& p2,const Vector3& q1,const Vector3& q2); + PoolVector<Vector2> get_closest_points_between_segments_2d( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2); + PoolVector<Vector3> get_closest_points_between_segments(const Vector3& p1,const Vector3& p2,const Vector3& q1,const Vector3& q2); Vector3 get_closest_point_to_segment(const Vector3& p_point, const Vector3& p_a,const Vector3& p_b); Variant ray_intersects_triangle( const Vector3& p_from, const Vector3& p_dir, const Vector3& p_v0,const Vector3& p_v1,const Vector3& p_v2); Variant segment_intersects_triangle( const Vector3& p_from, const Vector3& p_to, const Vector3& p_v0,const Vector3& p_v1,const Vector3& p_v2); bool point_is_inside_triangle(const Vector2& s, const Vector2& a, const Vector2& b, const Vector2& c) const; - DVector<Vector3> segment_intersects_sphere( const Vector3& p_from, const Vector3& p_to, const Vector3& p_sphere_pos,real_t p_sphere_radius); - DVector<Vector3> segment_intersects_cylinder( const Vector3& p_from, const Vector3& p_to, float p_height,float p_radius); - DVector<Vector3> segment_intersects_convex(const Vector3& p_from, const Vector3& p_to,const Vector<Plane>& p_planes); + PoolVector<Vector3> segment_intersects_sphere( const Vector3& p_from, const Vector3& p_to, const Vector3& p_sphere_pos,real_t p_sphere_radius); + PoolVector<Vector3> segment_intersects_cylinder( const Vector3& p_from, const Vector3& p_to, float p_height,float p_radius); + PoolVector<Vector3> segment_intersects_convex(const Vector3& p_from, const Vector3& p_to,const Vector<Plane>& p_planes); real_t segment_intersects_circle(const Vector2& p_from, const Vector2& p_to, const Vector2& p_circle_pos, real_t p_circle_radius); int get_uv84_normal_bit(const Vector3& p_vector); @@ -413,7 +416,7 @@ public: Variant get_var() const; - DVector<uint8_t> get_buffer(int p_length) const; ///< get an array of bytes + PoolVector<uint8_t> get_buffer(int p_length) const; ///< get an array of bytes String get_line() const; String get_as_text() const; String get_md5(const String& p_path) const; @@ -447,7 +450,7 @@ public: Vector<String> get_csv_line(String delim=",") const; - void store_buffer(const DVector<uint8_t>& p_buffer); ///< store an array of bytes + void store_buffer(const PoolVector<uint8_t>& p_buffer); ///< store an array of bytes void store_var(const Variant& p_var); @@ -517,8 +520,8 @@ public: String variant_to_base64(const Variant& p_var); Variant base64_to_variant(const String& p_str); - String raw_to_base64(const DVector<uint8_t>& p_arr); - DVector<uint8_t> base64_to_raw(const String& p_str); + String raw_to_base64(const PoolVector<uint8_t>& p_arr); + PoolVector<uint8_t> base64_to_raw(const String& p_str); String utf8_to_base64(const String& p_str); String base64_to_utf8(const String& p_str); diff --git a/core/compressed_translation.cpp b/core/compressed_translation.cpp index 9b39eeb2c1..f8aa3915d4 100644 --- a/core/compressed_translation.cpp +++ b/core/compressed_translation.cpp @@ -356,8 +356,8 @@ void PHashTranslation::generate(const Ref<Translation> &p_from) { hash_table.resize(size); bucket_table.resize(bucket_table_size); - DVector<int>::Write htwb = hash_table.write(); - DVector<int>::Write btwb = bucket_table.write(); + PoolVector<int>::Write htwb = hash_table.write(); + PoolVector<int>::Write btwb = bucket_table.write(); uint32_t *htw = (uint32_t*)&htwb[0]; uint32_t *btw = (uint32_t*)&btwb[0]; @@ -392,7 +392,7 @@ void PHashTranslation::generate(const Ref<Translation> &p_from) { print_line("total collisions: "+itos(collisions)); strings.resize(total_compression_size); - DVector<uint8_t>::Write cw = strings.write(); + PoolVector<uint8_t>::Write cw = strings.write(); for(int i=0;i<compressed.size();i++) { memcpy(&cw[compressed[i].offset],compressed[i].compressed.get_data(),compressed[i].compressed.size()); @@ -454,11 +454,11 @@ StringName PHashTranslation::get_message(const StringName& p_src_text) const { uint32_t h = hash(0,str.get_data()); - DVector<int>::Read htr = hash_table.read(); + PoolVector<int>::Read htr = hash_table.read(); const uint32_t *htptr = (const uint32_t*)&htr[0]; - DVector<int>::Read btr = bucket_table.read(); + PoolVector<int>::Read btr = bucket_table.read(); const uint32_t *btptr = (const uint32_t*)&btr[0]; - DVector<uint8_t>::Read sr = strings.read(); + PoolVector<uint8_t>::Read sr = strings.read(); const char *sptr= (const char*)&sr[0]; uint32_t p = htptr[ h % htsize]; diff --git a/core/compressed_translation.h b/core/compressed_translation.h index 218a59c05d..cb1e084051 100644 --- a/core/compressed_translation.h +++ b/core/compressed_translation.h @@ -42,9 +42,9 @@ class PHashTranslation : public Translation { //of catching untranslated strings //load/store friendly types - DVector<int> hash_table; - DVector<int> bucket_table; - DVector<uint8_t> strings; + PoolVector<int> hash_table; + PoolVector<int> bucket_table; + PoolVector<uint8_t> strings; struct Bucket { diff --git a/core/dictionary.cpp b/core/dictionary.cpp index 62e5d6bc3c..d5d29ca0fc 100644 --- a/core/dictionary.cpp +++ b/core/dictionary.cpp @@ -29,7 +29,6 @@ #include "dictionary.h" #include "safe_refcount.h" #include "variant.h" -#include "io/json.h" struct _DictionaryVariantHash { @@ -37,18 +36,47 @@ struct _DictionaryVariantHash { }; + + + struct DictionaryPrivate { + struct Data { + Variant variant; + int order; + }; + SafeRefCount refcount; - HashMap<Variant,Variant,_DictionaryVariantHash> variant_map; + HashMap<Variant,Data,_DictionaryVariantHash> variant_map; + int counter; bool shared; }; +struct DictionaryPrivateSort { + + bool operator()(const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair *A,const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair *B) const { + + return A->data.order < B->data.order; + } +}; void Dictionary::get_key_list( List<Variant> *p_keys) const { - _p->variant_map.get_key_list(p_keys); + if (_p->variant_map.empty()) + return; + + int count = _p->variant_map.size(); + const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair**)alloca( count * sizeof(HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair *) ); + _p->variant_map.get_key_value_ptr_array(pairs); + + SortArray<const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair*,DictionaryPrivateSort> sort; + sort.sort(pairs,count); + + for(int i=0;i<count;i++) { + p_keys->push_back(pairs[i]->key); + } + } void Dictionary::_copy_on_write() const { @@ -69,30 +97,52 @@ Variant& Dictionary::operator[](const Variant& p_key) { _copy_on_write(); - return _p->variant_map[p_key]; + DictionaryPrivate::Data *v =_p->variant_map.getptr(p_key); + + if (!v) { + + DictionaryPrivate::Data d; + d.order=_p->counter++; + _p->variant_map[p_key]=d; + v =_p->variant_map.getptr(p_key); + + } + return v->variant; } const Variant& Dictionary::operator[](const Variant& p_key) const { - return _p->variant_map[p_key]; + return _p->variant_map[p_key].variant; } const Variant* Dictionary::getptr(const Variant& p_key) const { - return _p->variant_map.getptr(p_key); + const DictionaryPrivate::Data *v =_p->variant_map.getptr(p_key); + if (!v) + return NULL; + else + return &v->variant; } + Variant* Dictionary::getptr(const Variant& p_key) { - _copy_on_write(); - return _p->variant_map.getptr(p_key); + _copy_on_write(); + DictionaryPrivate::Data *v =_p->variant_map.getptr(p_key); + if (!v) + return NULL; + else + return &v->variant; + + } Variant Dictionary::get_valid(const Variant& p_key) const { - const Variant *v = getptr(p_key); + DictionaryPrivate::Data *v =_p->variant_map.getptr(p_key); if (!v) return Variant(); - return *v; + else + return v->variant; } @@ -151,6 +201,7 @@ void Dictionary::clear() { _copy_on_write(); _p->variant_map.clear(); + _p->counter=0; } bool Dictionary::is_shared() const { @@ -203,11 +254,20 @@ Array Dictionary::values() const { Array varr; varr.resize(size()); - const Variant *key=NULL; - int i=0; - while((key=next(key))){ - varr[i++] = _p->variant_map[*key]; + if (_p->variant_map.empty()) + return varr; + + int count = _p->variant_map.size(); + const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair**)alloca( count * sizeof(HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair *) ); + _p->variant_map.get_key_value_ptr_array(pairs); + + SortArray<const HashMap<Variant,DictionaryPrivate::Data,_DictionaryVariantHash>::Pair*,DictionaryPrivateSort> sort; + sort.sort(pairs,count); + + for(int i=0;i<count;i++) { + varr[i]=pairs[i]->data.variant; } + return varr; } @@ -216,22 +276,6 @@ const Variant* Dictionary::next(const Variant* p_key) const { return _p->variant_map.next(p_key); } - -Error Dictionary::parse_json(const String& p_json) { - - String errstr; - int errline=0; - if (p_json != ""){ - Error err = JSON::parse(p_json,*this,errstr,errline); - if (err!=OK) { - ERR_EXPLAIN("Error parsing JSON: "+errstr+" at line: "+itos(errline)); - ERR_FAIL_COND_V(err!=OK,err); - } - } - - return OK; -} - Dictionary Dictionary::copy() const { Dictionary n(is_shared()); @@ -246,11 +290,6 @@ Dictionary Dictionary::copy() const { return n; } -String Dictionary::to_json() const { - - return JSON::print(*this); -} - void Dictionary::operator=(const Dictionary& p_dictionary) { @@ -269,6 +308,7 @@ Dictionary::Dictionary(bool p_shared) { _p=memnew( DictionaryPrivate ); _p->refcount.init(); + _p->counter=0; _p->shared=p_shared; } diff --git a/core/dictionary.h b/core/dictionary.h index d074db8588..9fab653470 100644 --- a/core/dictionary.h +++ b/core/dictionary.h @@ -63,9 +63,6 @@ public: void clear(); - Error parse_json(const String& p_json); - String to_json() const; - bool is_shared() const; bool has(const Variant& p_key) const; diff --git a/core/dvector.cpp b/core/dvector.cpp index 7caa198c4c..f6b5a5fcbf 100644 --- a/core/dvector.cpp +++ b/core/dvector.cpp @@ -30,3 +30,44 @@ Mutex* dvector_lock=NULL; +PoolAllocator *MemoryPool::memory_pool=NULL; +uint8_t *MemoryPool::pool_memory=NULL; +size_t *MemoryPool::pool_size=NULL; + + +MemoryPool::Alloc *MemoryPool::allocs=NULL; +MemoryPool::Alloc *MemoryPool::free_list=NULL; +uint32_t MemoryPool::alloc_count=0; +uint32_t MemoryPool::allocs_used=0; +Mutex *MemoryPool::alloc_mutex=NULL; + +size_t MemoryPool::total_memory=0; +size_t MemoryPool::max_memory=0; + + +void MemoryPool::setup(uint32_t p_max_allocs) { + + allocs = memnew_arr( Alloc, p_max_allocs); + alloc_count = p_max_allocs; + allocs_used=0; + + for(uint32_t i=0;i<alloc_count-1;i++) { + + allocs[i].free_list=&allocs[i+1]; + } + + free_list=&allocs[0]; + + alloc_mutex = Mutex::create(); + +} + +void MemoryPool::cleanup() { + + memdelete_arr(allocs); + memdelete(alloc_mutex); + + ERR_EXPLAINC("There are still MemoryPool allocs in use at exit!"); + ERR_FAIL_COND(allocs_used>0); + +} diff --git a/core/dvector.h b/core/dvector.h index 9e0090fb7c..cac9e8ef85 100644 --- a/core/dvector.h +++ b/core/dvector.h @@ -30,6 +30,46 @@ #define DVECTOR_H #include "os/memory.h" +#include "os/copymem.h" +#include "pool_allocator.h" +#include "safe_refcount.h" +#include "os/rw_lock.h" + +struct MemoryPool { + + //avoid accessing these directly, must be public for template access + + static PoolAllocator *memory_pool; + static uint8_t *pool_memory; + static size_t *pool_size; + + + struct Alloc { + + SafeRefCount refcount; + uint32_t lock; + void *mem; + PoolAllocator::ID pool_id; + size_t size; + + Alloc *free_list; + + Alloc() { mem=NULL; lock=0; pool_id=POOL_ALLOCATOR_INVALID_ID; size=0; free_list=NULL; } + }; + + + static Alloc *allocs; + static Alloc *free_list; + static uint32_t alloc_count; + static uint32_t allocs_used; + static Mutex *alloc_mutex; + static size_t total_memory; + static size_t max_memory; + + + static void setup(uint32_t p_max_allocs=(1<<16)); + static void cleanup(); +}; /** @@ -37,182 +77,302 @@ */ -extern Mutex* dvector_lock; - template<class T> -class DVector { +class PoolVector { + + MemoryPool::Alloc *alloc; - mutable MID mem; + void _copy_on_write() { - void copy_on_write() { - if (!mem.is_valid()) + if (!alloc) return; - if (dvector_lock) - dvector_lock->lock(); + ERR_FAIL_COND(alloc->lock>0); - MID_Lock lock( mem ); + if (alloc->refcount.get()==1) + return; //nothing to do - if ( *(int*)lock.data() == 1 ) { - // one reference, means no refcount changes - if (dvector_lock) - dvector_lock->unlock(); - return; + //must allocate something + + MemoryPool::alloc_mutex->lock(); + if (MemoryPool::allocs_used==MemoryPool::alloc_count) { + MemoryPool::alloc_mutex->unlock(); + ERR_EXPLAINC("All memory pool allocations are in use, can't COW."); + ERR_FAIL(); } - MID new_mem= dynalloc( mem.get_size() ); + MemoryPool::Alloc *old_alloc = alloc; - if (!new_mem.is_valid()) { + //take one from the free list + alloc = MemoryPool::free_list; + MemoryPool::free_list = alloc->free_list; + //increment the used counter + MemoryPool::allocs_used++; - if (dvector_lock) - dvector_lock->unlock(); - ERR_FAIL_COND( new_mem.is_valid() ); // out of memory + //copy the alloc data + alloc->size=old_alloc->size; + alloc->refcount.init(); + alloc->pool_id=POOL_ALLOCATOR_INVALID_ID; + alloc->lock=0; + +#ifdef DEBUG_ENABLED + MemoryPool::total_memory+=alloc->size; + if (MemoryPool::total_memory>MemoryPool::max_memory) { + MemoryPool::max_memory=MemoryPool::total_memory; } +#endif - MID_Lock dst_lock( new_mem ); + MemoryPool::alloc_mutex->unlock(); - int *rc = (int*)dst_lock.data(); - *rc=1; + if (MemoryPool::memory_pool) { - T * dst = (T*)(rc + 1 ); - T * src =(T*) ((int*)lock.data() + 1 ); + } else { + alloc->mem = memalloc( alloc->size ); + } - int count = (mem.get_size() - sizeof(int)) / sizeof(T); + { + Write w; + w._ref(alloc); + Read r; + r._ref(old_alloc); + + int cur_elements = alloc->size/sizeof(T); + T*dst = (T*)w.ptr(); + const T*src = (const T*)r.ptr(); + for(int i=0;i<cur_elements;i++) { + memnew_placement(&dst[i],T(src[i])); + } + } - for (int i=0;i<count;i++) { - memnew_placement( &dst[i], T(src[i]) ); - } + if (old_alloc->refcount.unref()==true) { + //this should never happen but.. - (*(int*)lock.data())--; +#ifdef DEBUG_ENABLED + MemoryPool::alloc_mutex->lock(); + MemoryPool::total_memory-=old_alloc->size; + MemoryPool::alloc_mutex->unlock(); +#endif - // unlock all - dst_lock=MID_Lock(); - lock=MID_Lock(); + { + Write w; + w._ref(old_alloc); - mem=new_mem; + int cur_elements = old_alloc->size/sizeof(T); + T*elems = (T*)w.ptr(); + for(int i=0;i<cur_elements;i++) { + elems[i].~T(); + } - if (dvector_lock) - dvector_lock->unlock(); + } - } + if (MemoryPool::memory_pool) { + //resize memory pool + //if none, create + //if some resize + } else { - void reference( const DVector& p_dvector ) { - unreference(); + memfree( old_alloc->mem ); + old_alloc->mem=NULL; + old_alloc->size=0; - if (dvector_lock) - dvector_lock->lock(); - if (!p_dvector.mem.is_valid()) { + MemoryPool::alloc_mutex->lock(); + old_alloc->free_list=MemoryPool::free_list; + MemoryPool::free_list=old_alloc; + MemoryPool::allocs_used--; + MemoryPool::alloc_mutex->unlock(); + } - if (dvector_lock) - dvector_lock->unlock(); - return; } - MID_Lock lock(p_dvector.mem); + } - int * rc = (int*)lock.data(); - (*rc)++; + void _reference( const PoolVector& p_dvector ) { - lock = MID_Lock(); - mem=p_dvector.mem; + if (alloc==p_dvector.alloc) + return; - if (dvector_lock) - dvector_lock->unlock(); + _unreference(); - } + if (!p_dvector.alloc) { + return; + } + if (p_dvector.alloc->refcount.ref()) { + alloc=p_dvector.alloc; + } - void unreference() { + } - if (dvector_lock) - dvector_lock->lock(); - if (!mem.is_valid()) { + void _unreference() { - if (dvector_lock) - dvector_lock->unlock(); + if (!alloc) return; - } - MID_Lock lock(mem); - - int * rc = (int*)lock.data(); - (*rc)--; + if (alloc->refcount.unref()==false) { + alloc=NULL; + return; + } - if (*rc==0) { - // no one else using it, destruct + //must be disposed! - T * t= (T*)(rc+1); - int count = (mem.get_size() - sizeof(int)) / sizeof(T); + { + int cur_elements = alloc->size/sizeof(T); + Write w = write(); - for (int i=0;i<count;i++) { + for (int i=0;i<cur_elements;i++) { - t[i].~T(); + w[i].~T(); } } +#ifdef DEBUG_ENABLED + MemoryPool::alloc_mutex->lock(); + MemoryPool::total_memory-=alloc->size; + MemoryPool::alloc_mutex->unlock(); +#endif + + + if (MemoryPool::memory_pool) { + //resize memory pool + //if none, create + //if some resize + } else { + + memfree( alloc->mem ); + alloc->mem=NULL; + alloc->size=0; - lock = MID_Lock(); - mem = MID (); + MemoryPool::alloc_mutex->lock(); + alloc->free_list=MemoryPool::free_list; + MemoryPool::free_list=alloc; + MemoryPool::allocs_used--; + MemoryPool::alloc_mutex->unlock(); - if (dvector_lock) - dvector_lock->unlock(); + } + alloc=NULL; } public: - class Read { - friend class DVector; - MID_Lock lock; - const T * mem; + class Access { + friend class PoolVector; + protected: + MemoryPool::Alloc *alloc; + T * mem; + + _FORCE_INLINE_ void _ref(MemoryPool::Alloc *p_alloc) { + alloc=p_alloc; + if (alloc) { + if (atomic_increment(&alloc->lock)==1) { + if (MemoryPool::memory_pool) { + //lock it and get mem + } + } + + mem = (T*)alloc->mem; + } + } + + _FORCE_INLINE_ void _unref() { + + + if (alloc) { + if (atomic_decrement(&alloc->lock)==0) { + if (MemoryPool::memory_pool) { + //put mem back + } + } + + mem = NULL; + alloc=NULL; + } + + + } + + Access() { + alloc=NULL; + mem=NULL; + } + + + public: + virtual ~Access() { + _unref(); + } + }; + + class Read : public Access { public: - _FORCE_INLINE_ const T& operator[](int p_index) const { return mem[p_index]; } - _FORCE_INLINE_ const T *ptr() const { return mem; } + _FORCE_INLINE_ const T& operator[](int p_index) const { return this->mem[p_index]; } + _FORCE_INLINE_ const T *ptr() const { return this->mem; } + + void operator=(const Read& p_read) { + if (this->alloc==p_read.alloc) + return; + this->_unref(); + this->_ref(p_read.alloc); + } + + Read(const Read& p_read) { + this->_ref(p_read.alloc); + } + + Read() {} + - Read() { mem=NULL; } }; - class Write { - friend class DVector; - MID_Lock lock; - T * mem; + class Write : public Access { public: - _FORCE_INLINE_ T& operator[](int p_index) { return mem[p_index]; } - _FORCE_INLINE_ T *ptr() { return mem; } + _FORCE_INLINE_ T& operator[](int p_index) const { return this->mem[p_index]; } + _FORCE_INLINE_ T *ptr() const { return this->mem; } + + void operator=(const Write& p_read) { + if (this->alloc==p_read.alloc) + return; + this->_unref(); + this->_ref(p_read.alloc); + } + + Write(const Write& p_read) { + this->_ref(p_read.alloc); + } + + Write() {} - Write() { mem=NULL; } }; Read read() const { Read r; - if (mem.is_valid()) { - r.lock = MID_Lock( mem ); - r.mem = (const T*)((int*)r.lock.data()+1); + if (alloc) { + r._ref(alloc); } return r; + } Write write() { Write w; - if (mem.is_valid()) { - copy_on_write(); - w.lock = MID_Lock( mem ); - w.mem = (T*)((int*)w.lock.data()+1); + if (alloc) { + _copy_on_write(); //make sure there is only one being acessed + w._ref(alloc); } return w; } @@ -250,7 +410,7 @@ public: void set(int p_index, const T& p_val); void push_back(const T& p_val); void append(const T& p_val) { push_back(p_val); } - void append_array(const DVector<T>& p_arr) { + void append_array(const PoolVector<T>& p_arr) { int ds = p_arr.size(); if (ds==0) return; @@ -262,7 +422,7 @@ public: w[bs+i]=r[i]; } - DVector<T> subarray(int p_from, int p_to) { + PoolVector<T> subarray(int p_from, int p_to) { if (p_from<0) { p_from=size()+p_from; @@ -271,15 +431,15 @@ public: p_to=size()+p_to; } if (p_from<0 || p_from>=size()) { - DVector<T>& aux=*((DVector<T>*)0); // nullreturn + PoolVector<T>& aux=*((PoolVector<T>*)0); // nullreturn ERR_FAIL_COND_V(p_from<0 || p_from>=size(),aux) } if (p_to<0 || p_to>=size()) { - DVector<T>& aux=*((DVector<T>*)0); // nullreturn + PoolVector<T>& aux=*((PoolVector<T>*)0); // nullreturn ERR_FAIL_COND_V(p_to<0 || p_to>=size(),aux) } - DVector<T> slice; + PoolVector<T> slice; int span=1 + p_to - p_from; slice.resize(span); Read r = read(); @@ -307,7 +467,7 @@ public: } - bool is_locked() const { return mem.is_locked(); } + bool is_locked() const { return alloc && alloc->lock>0; } inline const T operator[](int p_index) const; @@ -315,27 +475,27 @@ public: void invert(); - void operator=(const DVector& p_dvector) { reference(p_dvector); } - DVector() {} - DVector(const DVector& p_dvector) { reference(p_dvector); } - ~DVector() { unreference(); } + void operator=(const PoolVector& p_dvector) { _reference(p_dvector); } + PoolVector() { alloc=NULL; } + PoolVector(const PoolVector& p_dvector) { alloc=NULL; _reference(p_dvector); } + ~PoolVector() { _unreference(); } }; template<class T> -int DVector<T>::size() const { +int PoolVector<T>::size() const { - return mem.is_valid() ? ((mem.get_size() - sizeof(int)) / sizeof(T) ) : 0; + return alloc ? alloc->size/sizeof(T) : 0; } template<class T> -T DVector<T>::get(int p_index) const { +T PoolVector<T>::get(int p_index) const { return operator[](p_index); } template<class T> -void DVector<T>::set(int p_index, const T& p_val) { +void PoolVector<T>::set(int p_index, const T& p_val) { if (p_index<0 || p_index>=size()) { ERR_FAIL_COND(p_index<0 || p_index>=size()); @@ -346,14 +506,14 @@ void DVector<T>::set(int p_index, const T& p_val) { } template<class T> -void DVector<T>::push_back(const T& p_val) { +void PoolVector<T>::push_back(const T& p_val) { resize( size() + 1 ); set( size() -1, p_val ); } template<class T> -const T DVector<T>::operator[](int p_index) const { +const T PoolVector<T>::operator[](int p_index) const { if (p_index<0 || p_index>=size()) { T& aux=*((T*)0); //nullreturn @@ -367,86 +527,122 @@ const T DVector<T>::operator[](int p_index) const { template<class T> -Error DVector<T>::resize(int p_size) { +Error PoolVector<T>::resize(int p_size) { - if (dvector_lock) - dvector_lock->lock(); - bool same = p_size==size(); + if (alloc==NULL) { - if (dvector_lock) - dvector_lock->unlock(); - // no further locking is necesary because we are supposed to own the only copy of this (using copy on write) + if (p_size==0) + return OK; //nothing to do here - if (same) - return OK; + //must allocate something + MemoryPool::alloc_mutex->lock(); + if (MemoryPool::allocs_used==MemoryPool::alloc_count) { + MemoryPool::alloc_mutex->unlock(); + ERR_EXPLAINC("All memory pool allocations are in use."); + ERR_FAIL_V(ERR_OUT_OF_MEMORY); + } - if (p_size == 0 ) { + //take one from the free list + alloc = MemoryPool::free_list; + MemoryPool::free_list = alloc->free_list; + //increment the used counter + MemoryPool::allocs_used++; - unreference(); - return OK; + //cleanup the alloc + alloc->size=0; + alloc->refcount.init(); + alloc->pool_id=POOL_ALLOCATOR_INVALID_ID; + MemoryPool::alloc_mutex->unlock(); + + } else { + + ERR_FAIL_COND_V( alloc->lock>0, ERR_LOCKED ); //can't resize if locked! } + size_t new_size = sizeof(T)*p_size; - copy_on_write(); // make it unique + if (alloc->size==new_size) + return OK; //nothing to do - ERR_FAIL_COND_V( mem.is_locked(), ERR_LOCKED ); // if after copy on write, memory is locked, fail. + if (p_size == 0 ) { + _unreference(); + return OK; + } - if (p_size > size() ) { + _copy_on_write(); // make it unique - int oldsize=size(); +#ifdef DEBUG_ENABLED + MemoryPool::alloc_mutex->lock(); + MemoryPool::total_memory-=alloc->size; + MemoryPool::total_memory+=new_size; + if (MemoryPool::total_memory>MemoryPool::max_memory) { + MemoryPool::max_memory=MemoryPool::total_memory; + } + MemoryPool::alloc_mutex->unlock(); +#endif - MID_Lock lock; - if (oldsize==0) { + int cur_elements = alloc->size / sizeof(T); - mem = dynalloc( p_size * sizeof(T) + sizeof(int) ); - lock=MID_Lock(mem); - int *rc = ((int*)lock.data()); - *rc=1; + if (p_size > cur_elements ) { + if (MemoryPool::memory_pool) { + //resize memory pool + //if none, create + //if some resize } else { - if (dynrealloc( mem, p_size * sizeof(T) + sizeof(int) )!=OK ) { - - ERR_FAIL_V(ERR_OUT_OF_MEMORY); // out of memory + if (alloc->size==0) { + alloc->mem = memalloc( new_size ); + } else { + alloc->mem = memrealloc( alloc->mem, new_size ); } - - lock=MID_Lock(mem); } + alloc->size=new_size; + Write w = write(); + for (int i=cur_elements;i<p_size;i++) { - T *t = (T*)((int*)lock.data() + 1); - - for (int i=oldsize;i<p_size;i++) { - - memnew_placement(&t[i], T ); + memnew_placement(&w[i], T ); } - lock = MID_Lock(); // clear - } else { - - int oldsize=size(); - - MID_Lock lock(mem); + } else { - T *t = (T*)((int*)lock.data() + 1); + { + Write w = write(); + for (int i=p_size;i<cur_elements;i++) { - for (int i=p_size;i<oldsize;i++) { + w[i].~T(); + } - t[i].~T(); } - lock = MID_Lock(); // clear + if (MemoryPool::memory_pool) { + //resize memory pool + //if none, create + //if some resize + } else { - if (dynrealloc( mem, p_size * sizeof(T) + sizeof(int) )!=OK ) { + if (new_size==0) { + memfree( alloc->mem ); + alloc->mem=NULL; + alloc->size=0; - ERR_FAIL_V(ERR_OUT_OF_MEMORY); // wtf error - } + MemoryPool::alloc_mutex->lock(); + alloc->free_list=MemoryPool::free_list; + MemoryPool::free_list=alloc; + MemoryPool::allocs_used--; + MemoryPool::alloc_mutex->unlock(); + } else { + alloc->mem = memrealloc( alloc->mem, new_size ); + alloc->size=new_size; + } + } } @@ -454,7 +650,7 @@ Error DVector<T>::resize(int p_size) { } template<class T> -void DVector<T>::invert() { +void PoolVector<T>::invert() { T temp; Write w = write(); int s = size(); diff --git a/core/global_constants.cpp b/core/global_constants.cpp index c5c2081b5c..88d986f60e 100644 --- a/core/global_constants.cpp +++ b/core/global_constants.cpp @@ -329,7 +329,7 @@ static _GlobalConstant _global_constants[]={ BIND_GLOBAL_CONSTANT( BUTTON_MASK_RIGHT ), BIND_GLOBAL_CONSTANT( BUTTON_MASK_MIDDLE ), - //joysticks + //joypads BIND_GLOBAL_CONSTANT( JOY_BUTTON_0 ), BIND_GLOBAL_CONSTANT( JOY_BUTTON_1 ), BIND_GLOBAL_CONSTANT( JOY_BUTTON_2 ), @@ -463,7 +463,12 @@ static _GlobalConstant _global_constants[]={ BIND_GLOBAL_CONSTANT( PROPERTY_HINT_LENGTH ), BIND_GLOBAL_CONSTANT( PROPERTY_HINT_KEY_ACCEL ), BIND_GLOBAL_CONSTANT( PROPERTY_HINT_FLAGS ), - BIND_GLOBAL_CONSTANT( PROPERTY_HINT_ALL_FLAGS ), + + BIND_GLOBAL_CONSTANT( PROPERTY_HINT_LAYERS_2D_RENDER ), + BIND_GLOBAL_CONSTANT( PROPERTY_HINT_LAYERS_2D_PHYSICS ), + BIND_GLOBAL_CONSTANT( PROPERTY_HINT_LAYERS_3D_RENDER ), + BIND_GLOBAL_CONSTANT( PROPERTY_HINT_LAYERS_3D_PHYSICS), + BIND_GLOBAL_CONSTANT( PROPERTY_HINT_FILE ), BIND_GLOBAL_CONSTANT( PROPERTY_HINT_DIR ), BIND_GLOBAL_CONSTANT( PROPERTY_HINT_GLOBAL_FILE ), diff --git a/core/globals.cpp b/core/globals.cpp index 8b335b8a0d..0b5fe4a82e 100644 --- a/core/globals.cpp +++ b/core/globals.cpp @@ -37,19 +37,19 @@ #include "io/file_access_pack.h" #include "io/file_access_network.h" -Globals *Globals::singleton=NULL; +GlobalConfig *GlobalConfig::singleton=NULL; -Globals *Globals::get_singleton() { +GlobalConfig *GlobalConfig::get_singleton() { return singleton; } -String Globals::get_resource_path() const { +String GlobalConfig::get_resource_path() const { return resource_path; }; -String Globals::localize_path(const String& p_path) const { +String GlobalConfig::localize_path(const String& p_path) const { if (resource_path=="") return p_path; //not initialied yet @@ -96,21 +96,14 @@ String Globals::localize_path(const String& p_path) const { } -void Globals::set_persisting(const String& p_name, bool p_persist) { +void GlobalConfig::set_initial_value(const String& p_name, const Variant & p_value) { ERR_FAIL_COND(!props.has(p_name)); - props[p_name].persist=p_persist; + props[p_name].initial=p_value; } -bool Globals::is_persisting(const String& p_name) const { - ERR_FAIL_COND_V(!props.has(p_name),false); - return props[p_name].persist; - -} - - -String Globals::globalize_path(const String& p_path) const { +String GlobalConfig::globalize_path(const String& p_path) const { if (p_path.begins_with("res://")) { @@ -125,7 +118,7 @@ String Globals::globalize_path(const String& p_path) const { } -bool Globals::_set(const StringName& p_name, const Variant& p_value) { +bool GlobalConfig::_set(const StringName& p_name, const Variant& p_value) { _THREAD_SAFE_METHOD_ @@ -169,7 +162,7 @@ bool Globals::_set(const StringName& p_name, const Variant& p_value) { return true; } -bool Globals::_get(const StringName& p_name,Variant &r_ret) const { +bool GlobalConfig::_get(const StringName& p_name,Variant &r_ret) const { _THREAD_SAFE_METHOD_ @@ -190,7 +183,7 @@ struct _VCSort { bool operator<(const _VCSort& p_vcs) const{ return order==p_vcs.order?name<p_vcs.name:order< p_vcs.order; } }; -void Globals::_get_property_list(List<PropertyInfo> *p_list) const { +void GlobalConfig::_get_property_list(List<PropertyInfo> *p_list) const { _THREAD_SAFE_METHOD_ @@ -208,13 +201,9 @@ void Globals::_get_property_list(List<PropertyInfo> *p_list) const { vc.order=v->order; vc.type=v->variant.get_type(); if (vc.name.begins_with("input/") || vc.name.begins_with("import/") || vc.name.begins_with("export/") || vc.name.begins_with("/remap") || vc.name.begins_with("/locale") || vc.name.begins_with("/autoload")) - vc.flags=PROPERTY_USAGE_CHECKABLE|PROPERTY_USAGE_STORAGE; + vc.flags=PROPERTY_USAGE_STORAGE; else - vc.flags=PROPERTY_USAGE_CHECKABLE|PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_STORAGE; - - if (v->persist) { - vc.flags|=PROPERTY_USAGE_CHECKED; - } + vc.flags=PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_STORAGE; vclist.insert(vc); } @@ -233,7 +222,7 @@ void Globals::_get_property_list(List<PropertyInfo> *p_list) const { -bool Globals::_load_resource_pack(const String& p_pack) { +bool GlobalConfig::_load_resource_pack(const String& p_pack) { if (PackedData::get_singleton()->is_disabled()) return false; @@ -250,7 +239,7 @@ bool Globals::_load_resource_pack(const String& p_pack) { return true; } -Error Globals::setup(const String& p_path,const String & p_main_pack) { +Error GlobalConfig::setup(const String& p_path,const String & p_main_pack) { //an absolute mess of a function, must be cleaned up and reorganized somehow at some point @@ -397,7 +386,7 @@ Error Globals::setup(const String& p_path,const String & p_main_pack) { return OK; } -bool Globals::has(String p_var) const { +bool GlobalConfig::has(String p_var) const { _THREAD_SAFE_METHOD_ @@ -627,7 +616,7 @@ static Variant _decode_variant(const String& p_string) { ERR_FAIL_COND_V(params.size()!=2,Variant()); InputEvent ie; - ie.type=InputEvent::JOYSTICK_BUTTON; + ie.type=InputEvent::JOYPAD_BUTTON; ie.device=params[0].to_int(); ie.joy_button.button_index=params[1].to_int(); @@ -639,7 +628,7 @@ static Variant _decode_variant(const String& p_string) { ERR_FAIL_COND_V(params.size()!=2,Variant()); InputEvent ie; - ie.type=InputEvent::JOYSTICK_MOTION; + ie.type=InputEvent::JOYPAD_MOTION; ie.device=params[0].to_int(); int axis = params[1].to_int();; ie.joy_motion.axis=axis>>1; @@ -702,9 +691,9 @@ static Variant _decode_variant(const String& p_string) { String data=params[4]; int datasize=data.length()/2; - DVector<uint8_t> pixels; + PoolVector<uint8_t> pixels; pixels.resize(datasize); - DVector<uint8_t>::Write wb = pixels.write(); + PoolVector<uint8_t>::Write wb = pixels.write(); const CharType *cptr=data.c_str(); int idx=0; @@ -731,7 +720,7 @@ static Variant _decode_variant(const String& p_string) { } - wb = DVector<uint8_t>::Write(); + wb = PoolVector<uint8_t>::Write(); return Image(w,h,mipmaps,imgformat,pixels); } @@ -751,12 +740,12 @@ static Variant _decode_variant(const String& p_string) { return Variant(); } -void Globals::set_registering_order(bool p_enable) { +void GlobalConfig::set_registering_order(bool p_enable) { registering_order=p_enable; } -Error Globals::_load_settings_binary(const String p_path) { +Error GlobalConfig::_load_settings_binary(const String p_path) { Error err; FileAccess *f= FileAccess::open(p_path,FileAccess::READ,&err); @@ -797,7 +786,7 @@ Error Globals::_load_settings_binary(const String p_path) { ERR_EXPLAIN("Error decoding property: "+key); ERR_CONTINUE(err!=OK); set(key,value); - set_persisting(key,true); + } set_registering_order(true); @@ -805,7 +794,7 @@ Error Globals::_load_settings_binary(const String p_path) { return OK; } -Error Globals::_load_settings(const String p_path) { +Error GlobalConfig::_load_settings(const String p_path) { Error err; @@ -884,8 +873,10 @@ Error Globals::_load_settings(const String p_path) { Variant val = _decode_variant(value); - set(subpath+var,val); - set_persisting(subpath+var,true); + StringName path = subpath+var; + + set(path,val); + //props[subpath+var]=VariantContainer(val,last_order++,true); } else { @@ -1001,9 +992,9 @@ static String _encode_variant(const Variant& p_variant) { str+=itos(img.has_mipmaps())+", "; str+=itos(img.get_width())+", "; str+=itos(img.get_height())+", "; - DVector<uint8_t> data = img.get_data(); + PoolVector<uint8_t> data = img.get_data(); int ds=data.size(); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); for(int i=0;i<ds;i++) { uint8_t byte = r[i]; const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; @@ -1040,11 +1031,11 @@ static String _encode_variant(const Variant& p_variant) { return "mbutton("+itos(ev.device)+", "+itos(ev.mouse_button.button_index)+")"; } break; - case InputEvent::JOYSTICK_BUTTON: { + case InputEvent::JOYPAD_BUTTON: { return "jbutton("+itos(ev.device)+", "+itos(ev.joy_button.button_index)+")"; } break; - case InputEvent::JOYSTICK_MOTION: { + case InputEvent::JOYPAD_MOTION: { return "jaxis("+itos(ev.device)+", "+itos(ev.joy_motion.axis * 2 + (ev.joy_motion.axis_value<0?0:1))+")"; } break; @@ -1062,31 +1053,31 @@ static String _encode_variant(const Variant& p_variant) { } -int Globals::get_order(const String& p_name) const { +int GlobalConfig::get_order(const String& p_name) const { ERR_FAIL_COND_V(!props.has(p_name),-1); return props[p_name].order; } -void Globals::set_order(const String& p_name, int p_order){ +void GlobalConfig::set_order(const String& p_name, int p_order){ ERR_FAIL_COND(!props.has(p_name)); props[p_name].order=p_order; } -void Globals::clear(const String& p_name) { +void GlobalConfig::clear(const String& p_name) { ERR_FAIL_COND(!props.has(p_name)); props.erase(p_name); } -Error Globals::save() { +Error GlobalConfig::save() { return save_custom(get_resource_path()+"/engine.cfg"); } -Error Globals::_save_settings_binary(const String& p_file,const Map<String,List<String> > &props,const CustomMap& p_custom) { +Error GlobalConfig::_save_settings_binary(const String& p_file,const Map<String,List<String> > &props,const CustomMap& p_custom) { Error err; @@ -1155,7 +1146,7 @@ Error Globals::_save_settings_binary(const String& p_file,const Map<String,List< } -Error Globals::_save_settings_text(const String& p_file,const Map<String,List<String> > &props,const CustomMap& p_custom) { +Error GlobalConfig::_save_settings_text(const String& p_file,const Map<String,List<String> > &props,const CustomMap& p_custom) { Error err; FileAccess *file = FileAccess::open(p_file,FileAccess::WRITE,&err); @@ -1194,12 +1185,12 @@ Error Globals::_save_settings_text(const String& p_file,const Map<String,List<St return OK; } -Error Globals::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array? +Error GlobalConfig::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array? return save_custom(p_file); }; -Error Globals::save_custom(const String& p_path,const CustomMap& p_custom,const Set<String>& p_ignore_masks) { +Error GlobalConfig::save_custom(const String& p_path,const CustomMap& p_custom,const Set<String>& p_ignore_masks) { ERR_FAIL_COND_V(p_path=="",ERR_INVALID_PARAMETER); @@ -1232,8 +1223,8 @@ Error Globals::save_custom(const String& p_path,const CustomMap& p_custom,const vc.name=G->key();//*k; vc.order=v->order; vc.type=v->variant.get_type(); - vc.flags=PROPERTY_USAGE_CHECKABLE|PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_STORAGE; - if (!v->persist) + vc.flags=PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_STORAGE; + if (v->variant==v->initial) continue; @@ -1327,20 +1318,23 @@ Error Globals::save_custom(const String& p_path,const CustomMap& p_custom,const Variant _GLOBAL_DEF( const String& p_var, const Variant& p_default) { - if (Globals::get_singleton()->has(p_var)) - return Globals::get_singleton()->get(p_var); - Globals::get_singleton()->set(p_var,p_default); + if (GlobalConfig::get_singleton()->has(p_var)) { + GlobalConfig::get_singleton()->set_initial_value(p_var,p_default); + return GlobalConfig::get_singleton()->get(p_var); + } + GlobalConfig::get_singleton()->set(p_var,p_default); + GlobalConfig::get_singleton()->set_initial_value(p_var,p_default); return p_default; } -void Globals::add_singleton(const Singleton &p_singleton) { +void GlobalConfig::add_singleton(const Singleton &p_singleton) { singletons.push_back(p_singleton); singleton_ptrs[p_singleton.name]=p_singleton.ptr; } -Object* Globals::get_singleton_object(const String& p_name) const { +Object* GlobalConfig::get_singleton_object(const String& p_name) const { const Map<StringName,Object*>::Element *E=singleton_ptrs.find(p_name); @@ -1351,21 +1345,21 @@ Object* Globals::get_singleton_object(const String& p_name) const { }; -bool Globals::has_singleton(const String& p_name) const { +bool GlobalConfig::has_singleton(const String& p_name) const { return get_singleton_object(p_name) != NULL; }; -void Globals::get_singletons(List<Singleton> *p_singletons) { +void GlobalConfig::get_singletons(List<Singleton> *p_singletons) { for(List<Singleton>::Element *E=singletons.front();E;E=E->next()) p_singletons->push_back(E->get()); } -Vector<String> Globals::get_optimizer_presets() const { +Vector<String> GlobalConfig::get_optimizer_presets() const { List<PropertyInfo> pi; - Globals::get_singleton()->get_property_list(&pi); + GlobalConfig::get_singleton()->get_property_list(&pi); Vector<String> names; for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) { @@ -1381,7 +1375,7 @@ Vector<String> Globals::get_optimizer_presets() const { } -void Globals::_add_property_info_bind(const Dictionary& p_info) { +void GlobalConfig::_add_property_info_bind(const Dictionary& p_info) { ERR_FAIL_COND(!p_info.has("name")); ERR_FAIL_COND(!p_info.has("type")); @@ -1400,7 +1394,7 @@ void Globals::_add_property_info_bind(const Dictionary& p_info) { set_custom_property_info(pinfo.name, pinfo); } -void Globals::set_custom_property_info(const String& p_prop,const PropertyInfo& p_info) { +void GlobalConfig::set_custom_property_info(const String& p_prop,const PropertyInfo& p_info) { ERR_FAIL_COND(!props.has(p_prop)); custom_prop_info[p_prop]=p_info; @@ -1408,37 +1402,55 @@ void Globals::set_custom_property_info(const String& p_prop,const PropertyInfo& } -void Globals::set_disable_platform_override(bool p_disable) { +void GlobalConfig::set_disable_platform_override(bool p_disable) { disable_platform_override=p_disable; } -bool Globals::is_using_datapack() const { +bool GlobalConfig::is_using_datapack() const { return using_datapack; } -void Globals::_bind_methods() { +bool GlobalConfig::property_can_revert(const String& p_name) { - ClassDB::bind_method(_MD("has","name"),&Globals::has); - ClassDB::bind_method(_MD("set_order","name","pos"),&Globals::set_order); - ClassDB::bind_method(_MD("get_order","name"),&Globals::get_order); - ClassDB::bind_method(_MD("set_persisting","name","enable"),&Globals::set_persisting); - ClassDB::bind_method(_MD("is_persisting","name"),&Globals::is_persisting); - ClassDB::bind_method(_MD("add_property_info", "hint"),&Globals::_add_property_info_bind); - ClassDB::bind_method(_MD("clear","name"),&Globals::clear); - ClassDB::bind_method(_MD("localize_path","path"),&Globals::localize_path); - ClassDB::bind_method(_MD("globalize_path","path"),&Globals::globalize_path); - ClassDB::bind_method(_MD("save"),&Globals::save); - ClassDB::bind_method(_MD("has_singleton","name"),&Globals::has_singleton); - ClassDB::bind_method(_MD("get_singleton","name"),&Globals::get_singleton_object); - ClassDB::bind_method(_MD("load_resource_pack","pack"),&Globals::_load_resource_pack); + if (!props.has(p_name)) + return false; + + return props[p_name].initial!=props[p_name].variant; + +} + +Variant GlobalConfig::property_get_revert(const String& p_name) { + + if (!props.has(p_name)) + return Variant(); + + return props[p_name].initial; +} - ClassDB::bind_method(_MD("save_custom","file"),&Globals::_save_custom_bnd); +void GlobalConfig::_bind_methods() { + + ClassDB::bind_method(_MD("has","name"),&GlobalConfig::has); + ClassDB::bind_method(_MD("set_order","name","pos"),&GlobalConfig::set_order); + ClassDB::bind_method(_MD("get_order","name"),&GlobalConfig::get_order); + ClassDB::bind_method(_MD("set_initial_value","name","value"),&GlobalConfig::set_initial_value); + ClassDB::bind_method(_MD("add_property_info", "hint"),&GlobalConfig::_add_property_info_bind); + ClassDB::bind_method(_MD("clear","name"),&GlobalConfig::clear); + ClassDB::bind_method(_MD("localize_path","path"),&GlobalConfig::localize_path); + ClassDB::bind_method(_MD("globalize_path","path"),&GlobalConfig::globalize_path); + ClassDB::bind_method(_MD("save"),&GlobalConfig::save); + ClassDB::bind_method(_MD("has_singleton","name"),&GlobalConfig::has_singleton); + ClassDB::bind_method(_MD("get_singleton","name"),&GlobalConfig::get_singleton_object); + ClassDB::bind_method(_MD("load_resource_pack","pack"),&GlobalConfig::_load_resource_pack); + ClassDB::bind_method(_MD("property_can_revert","name"),&GlobalConfig::property_can_revert); + ClassDB::bind_method(_MD("property_get_revert","name"),&GlobalConfig::property_get_revert); + + ClassDB::bind_method(_MD("save_custom","file"),&GlobalConfig::_save_custom_bnd); } -Globals::Globals() { +GlobalConfig::GlobalConfig() { singleton=this; @@ -1451,14 +1463,14 @@ Globals::Globals() { InputEvent key; key.type=InputEvent::KEY; InputEvent joyb; - joyb.type=InputEvent::JOYSTICK_BUTTON; + joyb.type=InputEvent::JOYPAD_BUTTON; - set("application/name","" ); - set("application/main_scene",""); + GLOBAL_DEF("application/name","" ); + GLOBAL_DEF("application/main_scene",""); custom_prop_info["application/main_scene"]=PropertyInfo(Variant::STRING,"application/main_scene",PROPERTY_HINT_FILE,"tscn,scn,xscn,xml,res"); - set("application/disable_stdout",false); - set("application/use_shared_user_dir",true); + GLOBAL_DEF("application/disable_stdout",false); + GLOBAL_DEF("application/use_shared_user_dir",true); key.key.scancode=KEY_RETURN; @@ -1469,7 +1481,7 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_BUTTON_0; va.push_back(joyb); - set("input/ui_accept",va); + GLOBAL_DEF("input/ui_accept",va); input_presets.push_back("input/ui_accept"); va=Array(); @@ -1477,7 +1489,7 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_BUTTON_3; va.push_back(joyb); - set("input/ui_select",va); + GLOBAL_DEF("input/ui_select",va); input_presets.push_back("input/ui_select"); va=Array(); @@ -1485,20 +1497,20 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_BUTTON_1; va.push_back(joyb); - set("input/ui_cancel",va); + GLOBAL_DEF("input/ui_cancel",va); input_presets.push_back("input/ui_cancel"); va=Array(); key.key.scancode=KEY_TAB; va.push_back(key); - set("input/ui_focus_next",va); + GLOBAL_DEF("input/ui_focus_next",va); input_presets.push_back("input/ui_focus_next"); va=Array(); key.key.scancode=KEY_TAB; key.key.mod.shift=true; va.push_back(key); - set("input/ui_focus_prev",va); + GLOBAL_DEF("input/ui_focus_prev",va); input_presets.push_back("input/ui_focus_prev"); key.key.mod.shift=false; @@ -1507,7 +1519,7 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_DPAD_LEFT; va.push_back(joyb); - set("input/ui_left",va); + GLOBAL_DEF("input/ui_left",va); input_presets.push_back("input/ui_left"); va=Array(); @@ -1515,7 +1527,7 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_DPAD_RIGHT; va.push_back(joyb); - set("input/ui_right",va); + GLOBAL_DEF("input/ui_right",va); input_presets.push_back("input/ui_right"); va=Array(); @@ -1523,7 +1535,7 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_DPAD_UP; va.push_back(joyb); - set("input/ui_up",va); + GLOBAL_DEF("input/ui_up",va); input_presets.push_back("input/ui_up"); va=Array(); @@ -1531,36 +1543,35 @@ Globals::Globals() { va.push_back(key); joyb.joy_button.button_index=JOY_DPAD_DOWN; va.push_back(joyb); - set("input/ui_down",va); + GLOBAL_DEF("input/ui_down",va); input_presets.push_back("input/ui_down"); va=Array(); key.key.scancode=KEY_PAGEUP; va.push_back(key); - set("input/ui_page_up",va); + GLOBAL_DEF("input/ui_page_up",va); input_presets.push_back("input/ui_page_up"); va=Array(); key.key.scancode=KEY_PAGEDOWN; va.push_back(key); - set("input/ui_page_down",va); + GLOBAL_DEF("input/ui_page_down",va); input_presets.push_back("input/ui_page_down"); -// set("display/orientation", "landscape"); +// GLOBAL_DEF("display/handheld/orientation", "landscape"); - custom_prop_info["display/orientation"]=PropertyInfo(Variant::STRING,"display/orientation",PROPERTY_HINT_ENUM,"landscape,portrait,reverse_landscape,reverse_portrait,sensor_landscape,sensor_portrait,sensor"); - custom_prop_info["render/mipmap_policy"]=PropertyInfo(Variant::INT,"render/mipmap_policy",PROPERTY_HINT_ENUM,"Allow,Allow For Po2,Disallow"); - custom_prop_info["render/thread_model"]=PropertyInfo(Variant::INT,"render/thread_model",PROPERTY_HINT_ENUM,"Single-Unsafe,Single-Safe,Multi-Threaded"); - custom_prop_info["physics_2d/thread_model"]=PropertyInfo(Variant::INT,"physics_2d/thread_model",PROPERTY_HINT_ENUM,"Single-Unsafe,Single-Safe,Multi-Threaded"); + custom_prop_info["display/handheld/orientation"]=PropertyInfo(Variant::STRING,"display/handheld/orientation",PROPERTY_HINT_ENUM,"landscape,portrait,reverse_landscape,reverse_portrait,sensor_landscape,sensor_portrait,sensor"); + custom_prop_info["rendering/threads/thread_model"]=PropertyInfo(Variant::INT,"rendering/threads/thread_model",PROPERTY_HINT_ENUM,"Single-Unsafe,Single-Safe,Multi-Threaded"); + custom_prop_info["physics/2d/thread_model"]=PropertyInfo(Variant::INT,"physics/2d/thread_model",PROPERTY_HINT_ENUM,"Single-Unsafe,Single-Safe,Multi-Threaded"); - set("debug/profiler_max_functions",16384); + GLOBAL_DEF("debug/profiler/max_functions",16384); using_datapack=false; } -Globals::~Globals() { +GlobalConfig::~GlobalConfig() { singleton=NULL; } diff --git a/core/globals.h b/core/globals.h index 67937c56d8..faf077f2a5 100644 --- a/core/globals.h +++ b/core/globals.h @@ -37,9 +37,9 @@ */ -class Globals : public Object { +class GlobalConfig : public Object { - GDCLASS( Globals, Object ); + GDCLASS( GlobalConfig, Object ); _THREAD_SAFE_CLASS_ public: @@ -62,6 +62,7 @@ protected: int order; bool persist; Variant variant; + Variant initial; bool hide_from_editor; bool overrided; VariantContainer(){ order=0; hide_from_editor=false; persist=false; overrided=false; } @@ -82,7 +83,7 @@ protected: bool _get(const StringName& p_name,Variant &r_ret) const; void _get_property_list(List<PropertyInfo> *p_list) const; - static Globals *singleton; + static GlobalConfig *singleton; Error _load_settings(const String p_path); Error _load_settings_binary(const String p_path); @@ -109,12 +110,14 @@ public: String localize_path(const String& p_path) const; String globalize_path(const String& p_path) const; - void set_persisting(const String& p_name, bool p_persist); - bool is_persisting(const String& p_name) const; + + void set_initial_value(const String& p_name, const Variant & p_value); + bool property_can_revert(const String& p_name); + Variant property_get_revert(const String& p_name); String get_resource_path() const; - static Globals *get_singleton(); + static GlobalConfig *get_singleton(); void clear(const String& p_name); int get_order(const String& p_name) const; @@ -144,12 +147,14 @@ public: void set_registering_order(bool p_registering); - Globals(); - ~Globals(); + GlobalConfig(); + ~GlobalConfig(); }; //not a macro any longer Variant _GLOBAL_DEF( const String& p_var, const Variant& p_default); #define GLOBAL_DEF(m_var,m_value) _GLOBAL_DEF(m_var,m_value) +#define GLOBAL_GET(m_var) GlobalConfig::get_singleton()->get(m_var) + #endif diff --git a/core/hash_map.h b/core/hash_map.h index 523a4a6214..fba12b55ec 100644 --- a/core/hash_map.h +++ b/core/hash_map.h @@ -596,6 +596,20 @@ public: hash_table_power=0; } + void get_key_value_ptr_array(const Pair **p_pairs) const { + if (!hash_table) + return; + for(int i=0;i<(1<<hash_table_power);i++) { + + Entry *e=hash_table[i]; + while(e) { + *p_pairs=&e->pair; + p_pairs++; + e=e->next; + } + } + } + void get_key_list(List<TKey> *p_keys) const { if (!hash_table) return; diff --git a/core/image.cpp b/core/image.cpp index e949cd9b38..174c840c23 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -365,8 +365,8 @@ void Image::convert( Format p_new_format ){ // int len=data.size(); - DVector<uint8_t>::Read r = data.read(); - DVector<uint8_t>::Write w = new_img.data.write(); + PoolVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Write w = new_img.data.write(); const uint8_t *rptr = r.ptr(); uint8_t *wptr = w.ptr(); @@ -409,8 +409,8 @@ void Image::convert( Format p_new_format ){ } - r = DVector<uint8_t>::Read(); - w = DVector<uint8_t>::Write(); + r = PoolVector<uint8_t>::Read(); + w = PoolVector<uint8_t>::Write(); bool gen_mipmaps=mipmaps; @@ -651,10 +651,10 @@ void Image::resize( int p_width, int p_height, Interpolation p_interpolation ) { Image dst( p_width, p_height, 0, format ); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); const unsigned char*r_ptr=r.ptr(); - DVector<uint8_t>::Write w = dst.data.write(); + PoolVector<uint8_t>::Write w = dst.data.write(); unsigned char*w_ptr=w.ptr(); @@ -693,8 +693,8 @@ void Image::resize( int p_width, int p_height, Interpolation p_interpolation ) { } - r = DVector<uint8_t>::Read(); - w = DVector<uint8_t>::Write(); + r = PoolVector<uint8_t>::Read(); + w = PoolVector<uint8_t>::Write(); if (mipmaps>0) dst.generate_mipmaps(); @@ -725,8 +725,8 @@ void Image::crop( int p_width, int p_height ) { Image dst( p_width, p_height,0, format ); { - DVector<uint8_t>::Read r = data.read(); - DVector<uint8_t>::Write w = dst.data.write(); + PoolVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Write w = dst.data.write(); for (int y=0;y<p_height;y++) { @@ -767,7 +767,7 @@ void Image::flip_y() { { - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); uint8_t up[16]; uint8_t down[16]; uint32_t pixel_size = get_format_pixel_size(format); @@ -806,7 +806,7 @@ void Image::flip_x() { { - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); uint8_t up[16]; uint8_t down[16]; uint32_t pixel_size = get_format_pixel_size(format); @@ -925,12 +925,12 @@ void Image::expand_x2_hq2x() { if (current!=FORMAT_RGBA8) convert(FORMAT_RGBA8); - DVector<uint8_t> dest; + PoolVector<uint8_t> dest; dest.resize(width*2*height*2*4); { - DVector<uint8_t>::Read r = data.read(); - DVector<uint8_t>::Write w = dest.write(); + PoolVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Write w = dest.write(); hq2x_resize((const uint32_t*)r.ptr(),width,height,(uint32_t*)w.ptr()); @@ -959,7 +959,7 @@ void Image::shrink_x2() { if (mipmaps) { //just use the lower mipmap as base and copy all - DVector<uint8_t> new_img; + PoolVector<uint8_t> new_img; int ofs = get_mipmap_offset(1); @@ -968,8 +968,8 @@ void Image::shrink_x2() { { - DVector<uint8_t>::Write w=new_img.write(); - DVector<uint8_t>::Read r=data.read(); + PoolVector<uint8_t>::Write w=new_img.write(); + PoolVector<uint8_t>::Read r=data.read(); copymem(w.ptr(),&r[ofs],new_size); } @@ -980,15 +980,15 @@ void Image::shrink_x2() { } else { - DVector<uint8_t> new_img; + PoolVector<uint8_t> new_img; ERR_FAIL_COND( !_can_modify(format) ); int ps = get_format_pixel_size(format); new_img.resize((width/2)*(height/2)*ps); { - DVector<uint8_t>::Write w=new_img.write(); - DVector<uint8_t>::Read r=data.read(); + PoolVector<uint8_t>::Write w=new_img.write(); + PoolVector<uint8_t>::Read r=data.read(); switch(format) { @@ -1027,7 +1027,7 @@ Error Image::generate_mipmaps(bool p_keep_existing) { data.resize(size); - DVector<uint8_t>::Write wp=data.write(); + PoolVector<uint8_t>::Write wp=data.write(); if (nearest_power_of_2(width)==uint32_t(width) && nearest_power_of_2(height)==uint32_t(height)) { //use fast code for powers of 2 @@ -1122,7 +1122,7 @@ bool Image::empty() const { return (data.size()==0); } -DVector<uint8_t> Image::get_data() const { +PoolVector<uint8_t> Image::get_data() const { return data; } @@ -1134,7 +1134,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps,Format p_format int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_use_mipmaps?-1:0); data.resize( size ); { - DVector<uint8_t>::Write w= data.write(); + PoolVector<uint8_t>::Write w= data.write(); zeromem(w.ptr(),size); } @@ -1146,7 +1146,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps,Format p_format } -void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const DVector<uint8_t>& p_data) { +void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t>& p_data) { ERR_FAIL_INDEX(p_width-1,MAX_WIDTH); ERR_FAIL_INDEX(p_height-1,MAX_HEIGHT); @@ -1188,7 +1188,7 @@ void Image::create( const char ** p_xpm ) { HashMap<String,Color> colormap; int colormap_size; uint32_t pixel_size; - DVector<uint8_t>::Write w; + PoolVector<uint8_t>::Write w; while (status!=DONE) { @@ -1355,7 +1355,7 @@ bool Image::is_invisible() const { int w,h; _get_mipmap_offset_and_size(1,len,w,h); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); const unsigned char *data_ptr=r.ptr(); bool detected=false; @@ -1401,7 +1401,7 @@ Image::AlphaMode Image::detect_alpha() const { int w,h; _get_mipmap_offset_and_size(1,len,w,h); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); const unsigned char *data_ptr=r.ptr(); bool bit=false; @@ -1459,8 +1459,8 @@ bool Image::operator==(const Image& p_image) const { if (data.size() == 0 && p_image.data.size() == 0) return true; - DVector<uint8_t>::Read r = data.read(); - DVector<uint8_t>::Read pr = p_image.data.read(); + PoolVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read pr = p_image.data.read(); return r.ptr() == pr.ptr(); } @@ -1502,11 +1502,11 @@ Error Image::_decompress_bc() { int mm; int size = _get_dst_image_size(wd,ht,FORMAT_RGBA8,mm); - DVector<uint8_t> newdata; + PoolVector<uint8_t> newdata; newdata.resize(size); - DVector<uint8_t>::Write w = newdata.write(); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Write w = newdata.write(); + PoolVector<uint8_t>::Read r = data.read(); int rofs=0; int wofs=0; @@ -1814,8 +1814,8 @@ Error Image::_decompress_bc() { } - w=DVector<uint8_t>::Write(); - r=DVector<uint8_t>::Read(); + w=PoolVector<uint8_t>::Write(); + r=PoolVector<uint8_t>::Read(); data=newdata; format=FORMAT_RGBA8; @@ -1927,7 +1927,7 @@ Image::Image(int p_width, int p_height,bool p_use_mipmaps, Format p_format) { } -Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const DVector<uint8_t>& p_data) { +Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t>& p_data) { width=0; height=0; @@ -1950,7 +1950,7 @@ Rect2 Image::get_used_rect() const { return Rect2(); //int data_size = len; - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); const unsigned char *rptr=r.ptr(); int ps = format==FORMAT_LA8?2:4; @@ -2005,10 +2005,10 @@ void Image::blit_rect(const Image& p_src, const Rect2& p_src_rect,const Point2& return; Rect2i src_rect( p_src_rect.pos + ( local_src_rect.pos - p_dest), local_src_rect.size ); - DVector<uint8_t>::Write wp = data.write(); + PoolVector<uint8_t>::Write wp = data.write(); uint8_t *dst_data_ptr=wp.ptr(); - DVector<uint8_t>::Read rp = p_src.data.read(); + PoolVector<uint8_t>::Read rp = p_src.data.read(); const uint8_t *src_data_ptr=rp.ptr(); int pixel_size=get_format_pixel_size(format); @@ -2049,10 +2049,10 @@ void (*Image::_image_decompress_bc)(Image *)=NULL; void (*Image::_image_decompress_etc)(Image *)=NULL; void (*Image::_image_decompress_etc2)(Image *)=NULL; -DVector<uint8_t> (*Image::lossy_packer)(const Image& ,float )=NULL; -Image (*Image::lossy_unpacker)(const DVector<uint8_t>& )=NULL; -DVector<uint8_t> (*Image::lossless_packer)(const Image& )=NULL; -Image (*Image::lossless_unpacker)(const DVector<uint8_t>& )=NULL; +PoolVector<uint8_t> (*Image::lossy_packer)(const Image& ,float )=NULL; +Image (*Image::lossy_unpacker)(const PoolVector<uint8_t>& )=NULL; +PoolVector<uint8_t> (*Image::lossless_packer)(const Image& )=NULL; +Image (*Image::lossless_unpacker)(const PoolVector<uint8_t>& )=NULL; void Image::set_compress_bc_func(void (*p_compress_func)(Image *)) { @@ -2067,7 +2067,7 @@ void Image::normalmap_to_xy() { { int len = data.size()/4; - DVector<uint8_t>::Write wp = data.write(); + PoolVector<uint8_t>::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i<len;i++) { @@ -2094,7 +2094,7 @@ void Image::srgb_to_linear() { if (format==FORMAT_RGBA8) { int len = data.size()/4; - DVector<uint8_t>::Write wp = data.write(); + PoolVector<uint8_t>::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i<len;i++) { @@ -2107,7 +2107,7 @@ void Image::srgb_to_linear() { } else if (format==FORMAT_RGB8) { int len = data.size()/3; - DVector<uint8_t>::Write wp = data.write(); + PoolVector<uint8_t>::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i<len;i++) { @@ -2128,7 +2128,7 @@ void Image::premultiply_alpha() { if (format!=FORMAT_RGBA8) return; //not needed - DVector<uint8_t>::Write wp = data.write(); + PoolVector<uint8_t>::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); @@ -2152,11 +2152,11 @@ void Image::fix_alpha_edges() { if (format!=FORMAT_RGBA8) return; //not needed - DVector<uint8_t> dcopy = data; - DVector<uint8_t>::Read rp = data.read(); + PoolVector<uint8_t> dcopy = data; + PoolVector<uint8_t>::Read rp = data.read(); const uint8_t *srcptr=rp.ptr(); - DVector<uint8_t>::Write wp = data.write(); + PoolVector<uint8_t>::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); const int max_radius=4; diff --git a/core/image.h b/core/image.h index 2c585d74d9..620160147b 100644 --- a/core/image.h +++ b/core/image.h @@ -123,14 +123,14 @@ public: Error _decompress_bc(); - static DVector<uint8_t> (*lossy_packer)(const Image& p_image,float p_quality); - static Image (*lossy_unpacker)(const DVector<uint8_t>& p_buffer); - static DVector<uint8_t> (*lossless_packer)(const Image& p_image); - static Image (*lossless_unpacker)(const DVector<uint8_t>& p_buffer); + static PoolVector<uint8_t> (*lossy_packer)(const Image& p_image,float p_quality); + static Image (*lossy_unpacker)(const PoolVector<uint8_t>& p_buffer); + static PoolVector<uint8_t> (*lossless_packer)(const Image& p_image); + static Image (*lossless_unpacker)(const PoolVector<uint8_t>& p_buffer); private: Format format; - DVector<uint8_t> data; + PoolVector<uint8_t> data; int width,height; bool mipmaps; @@ -206,7 +206,7 @@ public: * Create a new image of a given size and format. Current image will be lost */ void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format); - void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const DVector<uint8_t>& p_data); + void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t>& p_data); void create( const char ** p_xpm ); /** @@ -214,7 +214,7 @@ public: */ bool empty() const; - DVector<uint8_t> get_data() const; + PoolVector<uint8_t> get_data() const; Error load(const String& p_path); Error save_png(const String& p_path); @@ -230,7 +230,7 @@ public: /** * import an image of a specific size and format from a pointer */ - Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const DVector<uint8_t>& p_data); + Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t>& p_data); enum AlphaMode { ALPHA_NONE, diff --git a/core/input_map.cpp b/core/input_map.cpp index 439bf4b766..0379131dd3 100644 --- a/core/input_map.cpp +++ b/core/input_map.cpp @@ -125,7 +125,7 @@ List<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list,const same=(e.key.scancode==p_event.key.scancode && (p_mod_ignore || e.key.mod == p_event.key.mod)); } break; - case InputEvent::JOYSTICK_BUTTON: { + case InputEvent::JOYPAD_BUTTON: { same=(e.joy_button.button_index==p_event.joy_button.button_index); @@ -135,7 +135,7 @@ List<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list,const same=(e.mouse_button.button_index==p_event.mouse_button.button_index); } break; - case InputEvent::JOYSTICK_MOTION: { + case InputEvent::JOYPAD_MOTION: { same=(e.joy_motion.axis==p_event.joy_motion.axis && (e.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0)); @@ -241,7 +241,7 @@ void InputMap::load_from_globals() { input_map.clear();; List<PropertyInfo> pinfo; - Globals::get_singleton()->get_property_list(&pinfo); + GlobalConfig::get_singleton()->get_property_list(&pinfo); for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) { const PropertyInfo &pi=E->get(); @@ -253,7 +253,7 @@ void InputMap::load_from_globals() { add_action(name); - Array va = Globals::get_singleton()->get(pi.name);; + Array va = GlobalConfig::get_singleton()->get(pi.name);; for(int i=0;i<va.size();i++) { @@ -324,7 +324,7 @@ void InputMap::load_default() { key.key.scancode=KEY_PAGEDOWN; action_add_event("ui_page_down",key); -// set("display/orientation", "landscape"); +// set("display/handheld/orientation", "landscape"); } diff --git a/core/io/file_access_buffered.cpp b/core/io/file_access_buffered.cpp index 347edc7407..71518de38b 100644 --- a/core/io/file_access_buffered.cpp +++ b/core/io/file_access_buffered.cpp @@ -117,7 +117,7 @@ int FileAccessBuffered::get_buffer(uint8_t *p_dest,int p_elements) const { int size = (cache.buffer.size() - (file.offset - cache.offset)); size = size - (size % 4); - //DVector<uint8_t>::Read read = cache.buffer.read(); + //PoolVector<uint8_t>::Read read = cache.buffer.read(); //memcpy(p_dest, read.ptr() + (file.offset - cache.offset), size); memcpy(p_dest, cache.buffer.ptr() + (file.offset - cache.offset), size); p_dest += size; @@ -152,7 +152,7 @@ int FileAccessBuffered::get_buffer(uint8_t *p_dest,int p_elements) const { }; int r = MIN(left, to_read); - //DVector<uint8_t>::Read read = cache.buffer.read(); + //PoolVector<uint8_t>::Read read = cache.buffer.read(); //memcpy(p_dest+total_read, &read.ptr()[file.offset - cache.offset], r); memcpy(p_dest+total_read, cache.buffer.ptr() + (file.offset - cache.offset), r); diff --git a/core/io/file_access_buffered_fa.h b/core/io/file_access_buffered_fa.h index 8a15584b13..884d40a266 100644 --- a/core/io/file_access_buffered_fa.h +++ b/core/io/file_access_buffered_fa.h @@ -53,7 +53,7 @@ class FileAccessBufferedFA : public FileAccessBuffered { cache.buffer.resize(p_size); // on dvector - //DVector<uint8_t>::Write write = cache.buffer.write(); + //PoolVector<uint8_t>::Write write = cache.buffer.write(); //f.get_buffer(write.ptr(), p_size); // on vector diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index e1f6831f7b..a9dbf56c15 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -42,8 +42,8 @@ void FileAccessMemory::register_file(String p_name, Vector<uint8_t> p_data) { } String name; - if (Globals::get_singleton()) - name = Globals::get_singleton()->globalize_path(p_name); + if (GlobalConfig::get_singleton()) + name = GlobalConfig::get_singleton()->globalize_path(p_name); else name = p_name; //name = DirAccess::normalize_path(name); diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 3516ad8808..19076b57be 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -528,6 +528,14 @@ uint64_t FileAccessNetwork::_get_modified_time(const String& p_file){ } +void FileAccessNetwork::configure() { + + GLOBAL_DEF("network/remote_fs/page_size",65536); + GLOBAL_DEF("network/remote_fs/page_read_ahead",4); + GLOBAL_DEF("network/remote_fs/max_pages",20); + +} + FileAccessNetwork::FileAccessNetwork() { eof_flag=false; @@ -541,9 +549,9 @@ FileAccessNetwork::FileAccessNetwork() { id=nc->last_id++; nc->accesses[id]=this; nc->unlock_mutex(); - page_size = GLOBAL_DEF("remote_fs/page_size",65536); - read_ahead = GLOBAL_DEF("remote_fs/page_read_ahead",4); - max_pages = GLOBAL_DEF("remote_fs/max_pages",20); + page_size = GLOBAL_GET("network/remote_fs/page_size"); + read_ahead = GLOBAL_GET("network/remote_fs/page_read_ahead"); + max_pages = GLOBAL_GET("network/remote_fs/max_pages"); last_activity_val=0; waiting_on_page=-1; last_page=-1; diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index 867991bcbe..4dbfb04b10 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -162,6 +162,8 @@ public: virtual uint64_t _get_modified_time(const String& p_file); + static void configure(); + FileAccessNetwork(); ~FileAccessNetwork(); }; diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 4051ae302f..b556d46105 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -87,7 +87,7 @@ Ref<StreamPeer> HTTPClient::get_connection() const { return connection; } -Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const DVector<uint8_t>& p_body) { +Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const PoolVector<uint8_t>& p_body) { ERR_FAIL_INDEX_V(p_method,METHOD_MAX,ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(status!=STATUS_CONNECTED,ERR_INVALID_PARAMETER); @@ -120,7 +120,7 @@ Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vecto request+="\r\n"; CharString cs=request.utf8(); - DVector<uint8_t> data; + PoolVector<uint8_t> data; //Maybe this goes faster somehow? for(int i=0;i<cs.length();i++) { @@ -128,7 +128,7 @@ Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vecto } data.append_array( p_body ); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); Error err = connection->put_data(&r[0], data.size()); if (err) { diff --git a/core/io/http_client.h b/core/io/http_client.h index 2e78882303..231475775f 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -172,7 +172,7 @@ public: void set_connection(const Ref<StreamPeer>& p_connection); Ref<StreamPeer> get_connection() const; - Error request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const DVector<uint8_t>& p_body); + Error request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const PoolVector<uint8_t>& p_body); Error request( Method p_method, const String& p_url, const Vector<String>& p_headers,const String& p_body=String()); Error send_body_text(const String& p_body); Error send_body_data(const ByteArray& p_body); diff --git a/core/io/json.cpp b/core/io/json.cpp index f42155bc94..3280f94750 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -92,9 +92,9 @@ String JSON::_print_var(const Variant& p_var) { } -String JSON::print(const Dictionary& p_dict) { +String JSON::print(const Variant& p_var) { - return _print_var(p_dict); + return _print_var(p_var); } @@ -450,27 +450,24 @@ Error JSON::_parse_object(Dictionary &object,const CharType *p_str,int &index, i } -Error JSON::parse(const String& p_json,Dictionary& r_ret,String &r_err_str,int &r_err_line) { +Error JSON::parse(const String& p_json, Variant &r_ret, String &r_err_str, int &r_err_line) { const CharType *str = p_json.ptr(); int idx = 0; int len = p_json.length(); Token token; - int line=0; + r_err_line=0; String aux_key; - Error err = _get_token(str,idx,len,token,line,r_err_str); + Error err = _get_token(str,idx,len,token,r_err_line,r_err_str); if (err) return err; - if (token.type!=TK_CURLY_BRACKET_OPEN) { + err = _parse_value(r_ret,token,str,idx,len,r_err_line,r_err_str); - r_err_str="Expected '{'"; - return ERR_PARSE_ERROR; - } - - return _parse_object(r_ret,str,idx,len,r_err_line,r_err_str); + return err; + } diff --git a/core/io/json.h b/core/io/json.h index 52fcac145d..97457d223e 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -74,8 +74,8 @@ 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 Dictionary& p_dict); - static Error parse(const String& p_json,Dictionary& r_ret,String &r_err_str,int &r_err_line); + static String print(const Variant &p_var); + static Error parse(const String& p_json,Variant& r_ret,String &r_err_str,int &r_err_line); }; #endif // JSON_H diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index bc6cc0bb83..0765fc86c7 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -31,6 +31,10 @@ #include "os/keyboard.h" #include <stdio.h> + +#define ENCODE_MASK 0xFF +#define ENCODE_FLAG_64 1<<16 + Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int *r_len) { const uint8_t * buf=p_buffer; @@ -44,14 +48,14 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * uint32_t type=decode_uint32(buf); - ERR_FAIL_COND_V(type>=Variant::VARIANT_MAX,ERR_INVALID_DATA); + ERR_FAIL_COND_V((type&ENCODE_MASK)>=Variant::VARIANT_MAX,ERR_INVALID_DATA); buf+=4; len-=4; if (r_len) *r_len=4; - switch(type) { + switch(type&ENCODE_MASK) { case Variant::NIL: { @@ -68,19 +72,35 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * case Variant::INT: { ERR_FAIL_COND_V(len<4,ERR_INVALID_DATA); - int val = decode_uint32(buf); - r_variant=val; - if (r_len) - (*r_len)+=4; + if (type&ENCODE_FLAG_64) { + int64_t val = decode_uint64(buf); + r_variant=val; + if (r_len) + (*r_len)+=8; + + } else { + int32_t val = decode_uint32(buf); + r_variant=val; + if (r_len) + (*r_len)+=4; + } } break; case Variant::REAL: { ERR_FAIL_COND_V(len<(int)4,ERR_INVALID_DATA); - float val = decode_float(buf); - r_variant=val; - if (r_len) - (*r_len)+=4; + + if (type&ENCODE_FLAG_64) { + double val = decode_double(buf); + r_variant=val; + if (r_len) + (*r_len)+=8; + } else { + float val = decode_float(buf); + r_variant=val; + if (r_len) + (*r_len)+=4; + } } break; case Variant::STRING: { @@ -272,11 +292,11 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * if (datalen>0) { len-=5*4; ERR_FAIL_COND_V( len < datalen, ERR_INVALID_DATA ); - DVector<uint8_t> data; + PoolVector<uint8_t> data; data.resize(datalen); - DVector<uint8_t>::Write wr = data.write(); + PoolVector<uint8_t>::Write wr = data.write(); copymem(&wr[0],&buf[20],datalen); - wr = DVector<uint8_t>::Write(); + wr = PoolVector<uint8_t>::Write(); @@ -423,7 +443,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * (*r_len)+=4; } break; - case InputEvent::JOYSTICK_BUTTON: { + case InputEvent::JOYPAD_BUTTON: { ie.joy_button.button_index=decode_uint32(&buf[12]); if (r_len) @@ -435,7 +455,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * if (r_len) (*r_len)+=4; } break; - case InputEvent::JOYSTICK_MOTION: { + case InputEvent::JOYPAD_MOTION: { ie.joy_motion.axis=decode_uint32(&buf[12]); ie.joy_motion.axis_value=decode_float(&buf[16]); @@ -537,17 +557,17 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * ERR_FAIL_COND_V((int)count>len,ERR_INVALID_DATA); - DVector<uint8_t> data; + PoolVector<uint8_t> data; if (count) { data.resize(count); - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); for(int i=0;i<count;i++) { w[i]=buf[i]; } - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); } r_variant=data; @@ -569,18 +589,18 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * len-=4; ERR_FAIL_COND_V((int)count*4>len,ERR_INVALID_DATA); - DVector<int> data; + PoolVector<int> data; if (count) { //const int*rbuf=(const int*)buf; data.resize(count); - DVector<int>::Write w = data.write(); + PoolVector<int>::Write w = data.write(); for(int i=0;i<count;i++) { w[i]=decode_uint32(&buf[i*4]); } - w = DVector<int>::Write(); + w = PoolVector<int>::Write(); } r_variant=Variant(data); if (r_len) { @@ -596,18 +616,18 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * len-=4; ERR_FAIL_COND_V((int)count*4>len,ERR_INVALID_DATA); - DVector<float> data; + PoolVector<float> data; if (count) { //const float*rbuf=(const float*)buf; data.resize(count); - DVector<float>::Write w = data.write(); + PoolVector<float>::Write w = data.write(); for(int i=0;i<count;i++) { w[i]=decode_float(&buf[i*4]); } - w = DVector<float>::Write(); + w = PoolVector<float>::Write(); } r_variant=data; @@ -623,7 +643,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * uint32_t count = decode_uint32(buf); ERR_FAIL_COND_V(count<0,ERR_INVALID_DATA); - DVector<String> strings; + PoolVector<String> strings; buf+=4; len-=4; @@ -676,7 +696,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * len-=4; ERR_FAIL_COND_V((int)count*4*2>len,ERR_INVALID_DATA); - DVector<Vector2> varray; + PoolVector<Vector2> varray; if (r_len) { (*r_len)+=4; @@ -684,7 +704,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * if (count) { varray.resize(count); - DVector<Vector2>::Write w = varray.write(); + PoolVector<Vector2>::Write w = varray.write(); for(int i=0;i<(int)count;i++) { @@ -714,7 +734,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * len-=4; ERR_FAIL_COND_V((int)count*4*3>len,ERR_INVALID_DATA); - DVector<Vector3> varray; + PoolVector<Vector3> varray; if (r_len) { (*r_len)+=4; @@ -722,7 +742,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * if (count) { varray.resize(count); - DVector<Vector3>::Write w = varray.write(); + PoolVector<Vector3>::Write w = varray.write(); for(int i=0;i<(int)count;i++) { @@ -753,7 +773,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * len-=4; ERR_FAIL_COND_V((int)count*4*4>len,ERR_INVALID_DATA); - DVector<Color> carray; + PoolVector<Color> carray; if (r_len) { (*r_len)+=4; @@ -761,7 +781,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * if (count) { carray.resize(count); - DVector<Color>::Write w = carray.write(); + PoolVector<Color>::Write w = carray.write(); for(int i=0;i<(int)count;i++) { @@ -796,8 +816,28 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { r_len=0; + uint32_t flags=0; + + switch(p_variant.get_type()) { + + case Variant::INT: { + int64_t val = p_variant; + if (val>0x7FFFFFFF || val < -0x80000000) { + flags|=ENCODE_FLAG_64; + } + } break; + case Variant::REAL: { + + double d = p_variant; + float f = d; + if (double(f)!=d) { + flags|=ENCODE_FLAG_64; //always encode real as double + } + } break; + } + if (buf) { - encode_uint32(p_variant.get_type(),buf); + encode_uint32(p_variant.get_type()|flags,buf); buf+=4; } r_len+=4; @@ -819,20 +859,42 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } break; case Variant::INT: { - if (buf) { - encode_uint32(p_variant.operator int(),buf); - } + int64_t val = p_variant; + if (val>0x7FFFFFFF || val < -0x80000000) { + //64 bits + if (buf) { + encode_uint64(val,buf); + } - r_len+=4; + r_len+=8; + } else { + if (buf) { + encode_uint32(int32_t(val),buf); + } + r_len+=4; + } } break; case Variant::REAL: { - if (buf) { - encode_float(p_variant.operator float(),buf); + double d = p_variant; + float f = d; + if (double(f)!=d) { + if (buf) { + encode_double(p_variant.operator double(),buf); + } + + r_len+=8; + + } else { + + if (buf) { + encode_double(p_variant.operator float(),buf); + } + + r_len+=4; } - r_len+=4; } break; case Variant::NODE_PATH: { @@ -1055,7 +1117,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { case Variant::IMAGE: { Image image = p_variant; - DVector<uint8_t> data=image.get_data(); + PoolVector<uint8_t> data=image.get_data(); if (buf) { @@ -1065,7 +1127,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { encode_uint32(image.get_height(),&buf[12]); int ds=data.size(); encode_uint32(ds,&buf[16]); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); copymem(&buf[20],&r[0],ds); } @@ -1130,7 +1192,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } llen+=4; } break; - case InputEvent::JOYSTICK_BUTTON: { + case InputEvent::JOYPAD_BUTTON: { if (buf) { @@ -1146,7 +1208,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } llen+=4; } break; - case InputEvent::JOYSTICK_MOTION: { + case InputEvent::JOYPAD_MOTION: { if (buf) { @@ -1234,14 +1296,14 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { // arrays case Variant::RAW_ARRAY: { - DVector<uint8_t> data = p_variant; + PoolVector<uint8_t> data = p_variant; int datalen=data.size(); int datasize=sizeof(uint8_t); if (buf) { encode_uint32(datalen,buf); buf+=4; - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); copymem(buf,&r[0],datalen*datasize); } @@ -1253,14 +1315,14 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } break; case Variant::INT_ARRAY: { - DVector<int> data = p_variant; + PoolVector<int> data = p_variant; int datalen=data.size(); int datasize=sizeof(int32_t); if (buf) { encode_uint32(datalen,buf); buf+=4; - DVector<int>::Read r = data.read(); + PoolVector<int>::Read r = data.read(); for(int i=0;i<datalen;i++) encode_uint32(r[i],&buf[i*datasize]); @@ -1271,14 +1333,14 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } break; case Variant::REAL_ARRAY: { - DVector<real_t> data = p_variant; + PoolVector<real_t> data = p_variant; int datalen=data.size(); int datasize=sizeof(real_t); if (buf) { encode_uint32(datalen,buf); buf+=4; - DVector<real_t>::Read r = data.read(); + PoolVector<real_t>::Read r = data.read(); for(int i=0;i<datalen;i++) encode_float(r[i],&buf[i*datasize]); @@ -1290,7 +1352,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { case Variant::STRING_ARRAY: { - DVector<String> data = p_variant; + PoolVector<String> data = p_variant; int len=data.size(); if (buf) { @@ -1323,7 +1385,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } break; case Variant::VECTOR2_ARRAY: { - DVector<Vector2> data = p_variant; + PoolVector<Vector2> data = p_variant; int len=data.size(); if (buf) { @@ -1351,7 +1413,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } break; case Variant::VECTOR3_ARRAY: { - DVector<Vector3> data = p_variant; + PoolVector<Vector3> data = p_variant; int len=data.size(); if (buf) { @@ -1380,7 +1442,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) { } break; case Variant::COLOR_ARRAY: { - DVector<Color> data = p_variant; + PoolVector<Color> data = p_variant; int len=data.size(); if (buf) { diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 1d05e95010..5ff09f9fb0 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -39,7 +39,7 @@ PacketPeer::PacketPeer() { last_get_error=OK; } -Error PacketPeer::get_packet_buffer(DVector<uint8_t> &r_buffer) const { +Error PacketPeer::get_packet_buffer(PoolVector<uint8_t> &r_buffer) const { const uint8_t *buffer; int buffer_size; @@ -51,7 +51,7 @@ Error PacketPeer::get_packet_buffer(DVector<uint8_t> &r_buffer) const { if (buffer_size==0) return OK; - DVector<uint8_t>::Write w = r_buffer.write(); + PoolVector<uint8_t>::Write w = r_buffer.write(); for(int i=0;i<buffer_size;i++) w[i]=buffer[i]; @@ -59,13 +59,13 @@ Error PacketPeer::get_packet_buffer(DVector<uint8_t> &r_buffer) const { } -Error PacketPeer::put_packet_buffer(const DVector<uint8_t> &p_buffer) { +Error PacketPeer::put_packet_buffer(const PoolVector<uint8_t> &p_buffer) { int len = p_buffer.size(); if (len==0) return OK; - DVector<uint8_t>::Read r = p_buffer.read(); + PoolVector<uint8_t>::Read r = p_buffer.read(); return put_packet(&r[0],len); } @@ -108,12 +108,12 @@ Variant PacketPeer::_bnd_get_var() const { return var; }; -Error PacketPeer::_put_packet(const DVector<uint8_t> &p_buffer) { +Error PacketPeer::_put_packet(const PoolVector<uint8_t> &p_buffer) { return put_packet_buffer(p_buffer); } -DVector<uint8_t> PacketPeer::_get_packet() const { +PoolVector<uint8_t> PacketPeer::_get_packet() const { - DVector<uint8_t> raw; + PoolVector<uint8_t> raw; last_get_error=get_packet_buffer(raw); return raw; } @@ -265,7 +265,8 @@ void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { PacketPeerStream::PacketPeerStream() { - int rbsize=GLOBAL_DEF( "core/packet_stream_peer_max_buffer_po2",(16)); + int rbsize=GLOBAL_GET( "network/packets/packet_stream_peer_max_buffer_po2"); + ring_buffer.resize(rbsize); temp_buffer.resize(1<<rbsize); diff --git a/core/io/packet_peer.h b/core/io/packet_peer.h index 7aa8d8d456..bacd5214f1 100644 --- a/core/io/packet_peer.h +++ b/core/io/packet_peer.h @@ -42,8 +42,8 @@ class PacketPeer : public Reference { static void _bind_methods(); - Error _put_packet(const DVector<uint8_t> &p_buffer); - DVector<uint8_t> _get_packet() const; + Error _put_packet(const PoolVector<uint8_t> &p_buffer); + PoolVector<uint8_t> _get_packet() const; Error _get_packet_error() const; @@ -59,8 +59,8 @@ public: /* helpers / binders */ - virtual Error get_packet_buffer(DVector<uint8_t> &r_buffer) const; - virtual Error put_packet_buffer(const DVector<uint8_t> &p_buffer); + virtual Error get_packet_buffer(PoolVector<uint8_t> &r_buffer) const; + virtual Error put_packet_buffer(const PoolVector<uint8_t> &p_buffer); virtual Error get_var(Variant &r_variant) const; virtual Error put_var(const Variant& p_packet); @@ -92,6 +92,8 @@ public: virtual int get_max_packet_size() const; + + void set_stream_peer(const Ref<StreamPeer>& p_peer); void set_input_buffer_max_size(int p_max_size); PacketPeerStream(); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 5328bb68fe..16da74fdf1 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -68,6 +68,8 @@ enum { VARIANT_VECTOR3_ARRAY=35, VARIANT_COLOR_ARRAY=36, VARIANT_VECTOR2_ARRAY=37, + VARIANT_INT64=40, + VARIANT_DOUBLE=41, IMAGE_ENCODING_EMPTY=0, IMAGE_ENCODING_RAW=1, @@ -116,10 +118,18 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { r_v=int(f->get_32()); } break; + case VARIANT_INT64: { + + r_v=int64_t(f->get_64()); + } break; case VARIANT_REAL: { r_v=f->get_real(); } break; + case VARIANT_DOUBLE: { + + r_v=f->get_double(); + } break; case VARIANT_STRING: { r_v=get_unicode_string(); @@ -264,22 +274,22 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { uint32_t datalen = f->get_32(); - DVector<uint8_t> imgdata; + PoolVector<uint8_t> imgdata; imgdata.resize(datalen); - DVector<uint8_t>::Write w = imgdata.write(); + PoolVector<uint8_t>::Write w = imgdata.write(); f->get_buffer(w.ptr(),datalen); _advance_padding(datalen); - w=DVector<uint8_t>::Write(); + w=PoolVector<uint8_t>::Write(); r_v=Image(width,height,mipmaps,fmt,imgdata); } else { //compressed - DVector<uint8_t> data; + PoolVector<uint8_t> data; data.resize(f->get_32()); - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); f->get_buffer(w.ptr(),data.size()); - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); Image img; @@ -355,7 +365,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { if (path.find("://")==-1 && path.is_rel_path()) { // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); + path=GlobalConfig::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); } @@ -385,7 +395,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { if (path.find("://")==-1 && path.is_rel_path()) { // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); + path=GlobalConfig::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); } @@ -448,12 +458,12 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { uint32_t len = f->get_32(); - DVector<uint8_t> array; + PoolVector<uint8_t> array; array.resize(len); - DVector<uint8_t>::Write w = array.write(); + PoolVector<uint8_t>::Write w = array.write(); f->get_buffer(w.ptr(),len); _advance_padding(len); - w=DVector<uint8_t>::Write(); + w=PoolVector<uint8_t>::Write(); r_v=array; } break; @@ -461,9 +471,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { uint32_t len = f->get_32(); - DVector<int> array; + PoolVector<int> array; array.resize(len); - DVector<int>::Write w = array.write(); + PoolVector<int>::Write w = array.write(); f->get_buffer((uint8_t*)w.ptr(),len*4); #ifdef BIG_ENDIAN_ENABLED { @@ -475,16 +485,16 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { } #endif - w=DVector<int>::Write(); + w=PoolVector<int>::Write(); r_v=array; } break; case VARIANT_REAL_ARRAY: { uint32_t len = f->get_32(); - DVector<real_t> array; + PoolVector<real_t> array; array.resize(len); - DVector<real_t>::Write w = array.write(); + PoolVector<real_t>::Write w = array.write(); f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)); #ifdef BIG_ENDIAN_ENABLED { @@ -497,18 +507,18 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { #endif - w=DVector<real_t>::Write(); + w=PoolVector<real_t>::Write(); r_v=array; } break; case VARIANT_STRING_ARRAY: { uint32_t len = f->get_32(); - DVector<String> array; + PoolVector<String> array; array.resize(len); - DVector<String>::Write w = array.write(); + PoolVector<String>::Write w = array.write(); for(uint32_t i=0;i<len;i++) w[i]=get_unicode_string(); - w=DVector<String>::Write(); + w=PoolVector<String>::Write(); r_v=array; @@ -517,9 +527,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { uint32_t len = f->get_32(); - DVector<Vector2> array; + PoolVector<Vector2> array; array.resize(len); - DVector<Vector2>::Write w = array.write(); + PoolVector<Vector2>::Write w = array.write(); if (sizeof(Vector2)==8) { f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)*2); #ifdef BIG_ENDIAN_ENABLED @@ -537,7 +547,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { ERR_EXPLAIN("Vector2 size is NOT 8!"); ERR_FAIL_V(ERR_UNAVAILABLE); } - w=DVector<Vector2>::Write(); + w=PoolVector<Vector2>::Write(); r_v=array; } break; @@ -545,9 +555,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { uint32_t len = f->get_32(); - DVector<Vector3> array; + PoolVector<Vector3> array; array.resize(len); - DVector<Vector3>::Write w = array.write(); + PoolVector<Vector3>::Write w = array.write(); if (sizeof(Vector3)==12) { f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)*3); #ifdef BIG_ENDIAN_ENABLED @@ -565,7 +575,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { ERR_EXPLAIN("Vector3 size is NOT 12!"); ERR_FAIL_V(ERR_UNAVAILABLE); } - w=DVector<Vector3>::Write(); + w=PoolVector<Vector3>::Write(); r_v=array; } break; @@ -573,9 +583,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { uint32_t len = f->get_32(); - DVector<Color> array; + PoolVector<Color> array; array.resize(len); - DVector<Color>::Write w = array.write(); + PoolVector<Color>::Write w = array.write(); if (sizeof(Color)==16) { f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)*4); #ifdef BIG_ENDIAN_ENABLED @@ -593,7 +603,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) { ERR_EXPLAIN("Color size is NOT 16!"); ERR_FAIL_V(ERR_UNAVAILABLE); } - w=DVector<Color>::Write(); + w=PoolVector<Color>::Write(); r_v=array; } break; @@ -1047,7 +1057,7 @@ Ref<ResourceInteractiveLoader> ResourceFormatLoaderBinary::load_interactive(cons } Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); ria->open(f); @@ -1102,7 +1112,7 @@ Error ResourceFormatLoaderBinary::load_import_metadata(const String &p_path, Ref } Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); ria->recognize(f); @@ -1147,7 +1157,7 @@ void ResourceFormatLoaderBinary::get_dependencies(const String& p_path,List<Stri ERR_FAIL_COND(!f); Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); ria->get_dependencies(f,p_dependencies,p_add_types); @@ -1237,7 +1247,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path,const } Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; ria->remaps=p_map; // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); @@ -1372,7 +1382,7 @@ String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const } Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); String r = ria->recognize(f); @@ -1416,15 +1426,33 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, } break; case Variant::INT: { - f->store_32(VARIANT_INT); - int val=p_property; - f->store_32(val); + int64_t val = p_property; + if (val>0x7FFFFFFF || val < -0x80000000) { + f->store_32(VARIANT_INT64); + f->store_64(val); + + } else { + f->store_32(VARIANT_INT); + int val=p_property; + f->store_32(int32_t(val)); + + } + } break; case Variant::REAL: { - f->store_32(VARIANT_REAL); - real_t val=p_property; - f->store_real(val); + + double d = p_property; + float fl = d; + if (double(fl)!=d) { + f->store_32(VARIANT_DOUBLE); + f->store_double(d); + } else { + + f->store_32(VARIANT_REAL); + f->store_real(fl); + + } } break; case Variant::STRING: { @@ -1587,12 +1615,12 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, int dlen = val.get_data().size(); f->store_32(dlen); - DVector<uint8_t>::Read r = val.get_data().read(); + PoolVector<uint8_t>::Read r = val.get_data().read(); f->store_buffer(r.ptr(),dlen); _pad_buffer(dlen); } else { - DVector<uint8_t> data; + PoolVector<uint8_t> data; if (encoding==IMAGE_ENCODING_LOSSY) { data=Image::lossy_packer(val,quality); @@ -1604,7 +1632,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, int ds=data.size(); f->store_32(ds); if (ds>0) { - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); f->store_buffer(r.ptr(),ds); _pad_buffer(ds); @@ -1703,10 +1731,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::RAW_ARRAY: { f->store_32(VARIANT_RAW_ARRAY); - DVector<uint8_t> arr = p_property; + PoolVector<uint8_t> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<uint8_t>::Read r = arr.read(); + PoolVector<uint8_t>::Read r = arr.read(); f->store_buffer(r.ptr(),len); _pad_buffer(len); @@ -1714,10 +1742,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::INT_ARRAY: { f->store_32(VARIANT_INT_ARRAY); - DVector<int> arr = p_property; + PoolVector<int> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<int>::Read r = arr.read(); + PoolVector<int>::Read r = arr.read(); for(int i=0;i<len;i++) f->store_32(r[i]); @@ -1725,10 +1753,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::REAL_ARRAY: { f->store_32(VARIANT_REAL_ARRAY); - DVector<real_t> arr = p_property; + PoolVector<real_t> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<real_t>::Read r = arr.read(); + PoolVector<real_t>::Read r = arr.read(); for(int i=0;i<len;i++) { f->store_real(r[i]); } @@ -1737,10 +1765,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::STRING_ARRAY: { f->store_32(VARIANT_STRING_ARRAY); - DVector<String> arr = p_property; + PoolVector<String> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<String>::Read r = arr.read(); + PoolVector<String>::Read r = arr.read(); for(int i=0;i<len;i++) { save_unicode_string(r[i]); } @@ -1749,10 +1777,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::VECTOR3_ARRAY: { f->store_32(VARIANT_VECTOR3_ARRAY); - DVector<Vector3> arr = p_property; + PoolVector<Vector3> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<Vector3>::Read r = arr.read(); + PoolVector<Vector3>::Read r = arr.read(); for(int i=0;i<len;i++) { f->store_real(r[i].x); f->store_real(r[i].y); @@ -1763,10 +1791,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::VECTOR2_ARRAY: { f->store_32(VARIANT_VECTOR2_ARRAY); - DVector<Vector2> arr = p_property; + PoolVector<Vector2> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<Vector2>::Read r = arr.read(); + PoolVector<Vector2>::Read r = arr.read(); for(int i=0;i<len;i++) { f->store_real(r[i].x); f->store_real(r[i].y); @@ -1776,10 +1804,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, case Variant::COLOR_ARRAY: { f->store_32(VARIANT_COLOR_ARRAY); - DVector<Color> arr = p_property; + PoolVector<Color> arr = p_property; int len=arr.size(); f->store_32(len); - DVector<Color>::Read r = arr.read(); + PoolVector<Color>::Read r = arr.read(); for(int i=0;i<len;i++) { f->store_real(r[i].r); f->store_real(r[i].g); @@ -2192,7 +2220,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path,const RES& p_ Error ResourceFormatSaverBinary::save(const String &p_path,const RES& p_resource,uint32_t p_flags) { - String local_path = Globals::get_singleton()->localize_path(p_path); + String local_path = GlobalConfig::get_singleton()->localize_path(p_path); ResourceFormatSaverBinaryInstance saver; return saver.save(local_path,p_resource,p_flags); diff --git a/core/io/resource_format_xml.cpp b/core/io/resource_format_xml.cpp deleted file mode 100644 index 7bb9e2efb7..0000000000 --- a/core/io/resource_format_xml.cpp +++ /dev/null @@ -1,2882 +0,0 @@ -/*************************************************************************/ -/* resource_format_xml.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "resource_format_xml.h" -#include "globals.h" -#include "version.h" -#include "os/dir_access.h" - - -ResourceInteractiveLoaderXML::Tag* ResourceInteractiveLoaderXML::parse_tag(bool *r_exit, bool p_printerr, List<String> *r_order) { - - - while(get_char()!='<' && !f->eof_reached()) {} - if (f->eof_reached()) { - return NULL; - } - - Tag tag; - bool exit=false; - if (r_exit) - *r_exit=false; - - bool complete=false; - while(!f->eof_reached()) { - - CharType c=get_char(); - if (c<33 && tag.name.length() && !exit) { - break; - } else if (c=='>') { - complete=true; - break; - } else if (c=='/') { - exit=true; - } else { - tag.name+=c; - } - } - - if (f->eof_reached()) { - - return NULL; - } - - if (exit) { - if (!tag_stack.size()) { - if (!p_printerr) - return NULL; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unmatched exit tag </"+tag.name+">"); - ERR_FAIL_COND_V(!tag_stack.size(),NULL); - } - - if (tag_stack.back()->get().name!=tag.name) { - if (!p_printerr) - return NULL; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Mismatched exit tag. Got </"+tag.name+">, expected </"+tag_stack.back()->get().name+">"); - ERR_FAIL_COND_V(tag_stack.back()->get().name!=tag.name,NULL); - } - - if (!complete) { - while(get_char()!='>' && !f->eof_reached()) {} - if (f->eof_reached()) - return NULL; - } - - if (r_exit) - *r_exit=true; - - tag_stack.pop_back(); - return NULL; - - } - - if (!complete) { - String name; - CharString r_value; - bool reading_value=false; - - while(!f->eof_reached()) { - - CharType c=get_char(); - if (c=='>') { - if (r_value.size()) { - - r_value.push_back(0); - String str; - str.parse_utf8(r_value.get_data()); - tag.args[name]=str; - if (r_order) - r_order->push_back(name); - } - break; - - } else if ( ((!reading_value && (c<33)) || c=='=' || c=='"' || c=='\'') && tag.name.length()) { - - if (!reading_value && name.length()) { - - reading_value=true; - } else if (reading_value && r_value.size()) { - - r_value.push_back(0); - String str; - str.parse_utf8(r_value.get_data()); - tag.args[name]=str; - if (r_order) - r_order->push_back(name); - name=""; - r_value.clear(); - reading_value=false; - } - - } else if (reading_value) { - - r_value.push_back(c); - } else { - - name+=c; - } - } - - if (f->eof_reached()) - return NULL; - } - - tag_stack.push_back(tag); - - return &tag_stack.back()->get(); -} - - -Error ResourceInteractiveLoaderXML::close_tag(const String& p_name) { - - int level=0; - bool inside_tag=false; - - while(true) { - - if (f->eof_reached()) { - - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": EOF found while attempting to find </"+p_name+">"); - ERR_FAIL_COND_V( f->eof_reached(), ERR_FILE_CORRUPT ); - } - - uint8_t c = get_char(); - - if (c == '<') { - - if (inside_tag) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Malformed XML. Already inside Tag."); - ERR_FAIL_COND_V(inside_tag,ERR_FILE_CORRUPT); - } - inside_tag=true; - c = get_char(); - if (c == '/') { - - --level; - } else { - - ++level; - }; - } else if (c == '>') { - - if (!inside_tag) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Malformed XML. Already outside Tag"); - ERR_FAIL_COND_V(!inside_tag,ERR_FILE_CORRUPT); - } - inside_tag=false; - if (level == -1) { - tag_stack.pop_back(); - return OK; - }; - }; - } - - return OK; -} - -void ResourceInteractiveLoaderXML::unquote(String& p_str) { - - - p_str=p_str.strip_edges().replace("\"","").xml_unescape(); - - /*p_str=p_str.strip_edges(); - p_str=p_str.replace("\"",""); - p_str=p_str.replace(">","<"); - p_str=p_str.replace("<",">"); - p_str=p_str.replace("'","'"); - p_str=p_str.replace(""","\""); - for (int i=1;i<32;i++) { - - char chr[2]={i,0}; - p_str=p_str.replace("&#"+String::num(i)+";",chr); - } - p_str=p_str.replace("&","&"); -*/ - //p_str.parse_utf8( p_str.ascii(true).get_data() ); - -} - -Error ResourceInteractiveLoaderXML::goto_end_of_tag() { - - uint8_t c; - while(true) { - - c=get_char(); - if (c=='>') //closetag - break; - if (f->eof_reached()) { - - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": EOF found while attempting to find close tag."); - ERR_FAIL_COND_V( f->eof_reached(), ERR_FILE_CORRUPT ); - } - - } - tag_stack.pop_back(); - - return OK; -} - - -Error ResourceInteractiveLoaderXML::parse_property_data(String &r_data) { - - r_data=""; - CharString cs; - while(true) { - - CharType c=get_char(); - if (c=='<') - break; - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - cs.push_back(c); - } - - cs.push_back(0); - - r_data.parse_utf8(cs.get_data()); - - while(get_char()!='>' && !f->eof_reached()) {} - if (f->eof_reached()) { - - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Malformed XML."); - ERR_FAIL_COND_V( f->eof_reached(), ERR_FILE_CORRUPT ); - } - - r_data=r_data.strip_edges(); - tag_stack.pop_back(); - - return OK; -} - - -Error ResourceInteractiveLoaderXML::_parse_array_element(Vector<char> &buff,bool p_number_only,FileAccess *f,bool *end) { - - if (buff.empty()) - buff.resize(32); // optimi - - int buff_max=buff.size(); - int buff_size=0; - *end=false; - char *buffptr=&buff[0]; - bool found=false; - bool quoted=false; - - while(true) { - - char c=get_char(); - - if (c==0) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": File corrupt (zero found)."); - ERR_FAIL_V(ERR_FILE_CORRUPT); - } else if (c=='"') { - quoted=!quoted; - } else if ((!quoted && ((p_number_only && c<33) || c==',')) || c=='<') { - - - if (c=='<') { - *end=true; - break; - } - if (c<32 && f->eof_reached()) { - *end=true; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": File corrupt (unexpected EOF)."); - ERR_FAIL_V(ERR_FILE_CORRUPT); - } - - if (found) - break; - - } else { - - found=true; - if (buff_size>=buff_max) { - - buff_max++; - buff.resize(buff_max); - buffptr=buff.ptr(); - - } - - buffptr[buff_size]=c; - buff_size++; - } - } - - if (buff_size>=buff_max) { - - buff_max++; - buff.resize(buff_max); - - } - - buff[buff_size]=0; - buff_size++; - - return OK; -} - -Error ResourceInteractiveLoaderXML::parse_property(Variant& r_v, String &r_name) { - - bool exit; - Tag *tag = parse_tag(&exit); - - if (!tag) { - if (exit) // shouldn't have exited - return ERR_FILE_EOF; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": File corrupt (No Property Tag)."); - ERR_FAIL_V(ERR_FILE_CORRUPT); - } - - r_v=Variant(); - r_name=""; - - - //ERR_FAIL_COND_V(tag->name!="property",ERR_FILE_CORRUPT); - //ERR_FAIL_COND_V(!tag->args.has("name"),ERR_FILE_CORRUPT); -// ERR_FAIL_COND_V(!tag->args.has("type"),ERR_FILE_CORRUPT); - - //String name=tag->args["name"]; - //ERR_FAIL_COND_V(name=="",ERR_FILE_CORRUPT); - String type=tag->name; - String name=tag->args["name"]; - - if (type=="") { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": 'type' field is empty."); - ERR_FAIL_COND_V(type=="",ERR_FILE_CORRUPT); - } - - if (type=="dictionary") { - - Dictionary d( tag->args.has("shared") && (String(tag->args["shared"])=="true" || String(tag->args["shared"])=="1")); - - while(true) { - - Error err; - String tagname; - Variant key; - - int dictline = get_current_line(); - - - err=parse_property(key,tagname); - - if (err && err!=ERR_FILE_EOF) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error parsing dictionary: "+name+" (from line "+itos(dictline)+")"); - ERR_FAIL_COND_V(err && err!=ERR_FILE_EOF,err); - } - //ERR_FAIL_COND_V(tagname!="key",ERR_FILE_CORRUPT); - if (err) - break; - Variant value; - err=parse_property(value,tagname); - if (err) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error parsing dictionary: "+name+" (from line "+itos(dictline)+")"); - } - - ERR_FAIL_COND_V(err,err); - //ERR_FAIL_COND_V(tagname!="value",ERR_FILE_CORRUPT); - - d[key]=value; - } - - - //err=parse_property_data(name); // skip the rest - //ERR_FAIL_COND_V(err,err); - - r_name=name; - r_v=d; - return OK; - - } else if (type=="array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - - - int len=tag->args["len"].to_int(); - bool shared = tag->args.has("shared") && (String(tag->args["shared"])=="true" || String(tag->args["shared"])=="1"); - - Array array(shared); - array.resize(len); - - Error err; - Variant v; - String tagname; - int idx=0; - while( (err=parse_property(v,tagname))==OK ) { - - ERR_CONTINUE( idx <0 || idx >=len ); - - array.set(idx,v); - idx++; - } - - if (idx!=len) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array (size mismatch): "+name); - ERR_FAIL_COND_V(idx!=len,err); - } - - if (err!=ERR_FILE_EOF) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array: "+name); - ERR_FAIL_COND_V(err!=ERR_FILE_EOF,err); - } - - //err=parse_property_data(name); // skip the rest - //ERR_FAIL_COND_V(err,err); - - r_name=name; - r_v=array; - return OK; - - } else if (type=="resource") { - - if (tag->args.has("path")) { - - String path=tag->args["path"]; - String hint; - if (tag->args.has("resource_type")) - hint=tag->args["resource_type"]; - - if (path.begins_with("local://")) - path=path.replace("local://",local_path+"::"); - else if (path.find("://")==-1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); - - } - - if (remaps.has(path)) { - path=remaps[path]; - } - - //take advantage of the resource loader cache. The resource is cached on it, even if - RES res=ResourceLoader::load(path,hint); - - - if (res.is_null()) { - - WARN_PRINT(String("Couldn't load resource: "+path).ascii().get_data()); - } - - r_v=res.get_ref_ptr(); - } else if (tag->args.has("external")) { - - int index = tag->args["external"].to_int(); - if (ext_resources.has(index)) { - String path=ext_resources[index].path; - String type=ext_resources[index].type; - - //take advantage of the resource loader cache. The resource is cached on it, even if - RES res=ResourceLoader::load(path,type); - - if (res.is_null()) { - - WARN_PRINT(String("Couldn't load externalresource: "+path).ascii().get_data()); - } - - r_v=res.get_ref_ptr(); - } else { - WARN_PRINT(String("Invalid external resource index: "+itos(index)).ascii().get_data()); - - } - } - - - - - Error err=goto_end_of_tag(); - if (err) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error closing <resource> tag."); - ERR_FAIL_COND_V(err,err); - } - - - r_name=name; - - return OK; - - } else if (type=="image") { - - if (!tag->args.has("encoding")) { - //empty image - r_v=Image(); - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - return OK; - } - - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'encoding' field."); - ERR_FAIL_COND_V( !tag->args.has("encoding"), ERR_FILE_CORRUPT ); - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'width' field."); - ERR_FAIL_COND_V( !tag->args.has("width"), ERR_FILE_CORRUPT ); - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'height' field."); - ERR_FAIL_COND_V( !tag->args.has("height"), ERR_FILE_CORRUPT ); - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'format' field."); - ERR_FAIL_COND_V( !tag->args.has("format"), ERR_FILE_CORRUPT ); - - String encoding=tag->args["encoding"]; - - if (encoding=="raw") { - String width=tag->args["width"]; - String height=tag->args["height"]; - String format=tag->args["format"]; - int mipmaps=tag->args.has("mipmaps")?int(tag->args["mipmaps"].to_int()):int(0); - int custom_size = tag->args.has("custom_size")?int(tag->args["custom_size"].to_int()):int(0); - - r_name=name; - - Image::Format imgformat; - -/* - if (format=="grayscale") { - imgformat=Image::FORMAT_L8; - } else if (format=="intensity") { - imgformat=Image::FORMAT_INTENSITY; - } else if (format=="grayscale_alpha") { - imgformat=Image::FORMAT_LA8; - } else if (format=="rgb") { - imgformat=Image::FORMAT_RGB8; - } else if (format=="rgba") { - imgformat=Image::FORMAT_RGBA8; - } else if (format=="indexed") { - imgformat=Image::FORMAT_INDEXED; - } else if (format=="indexed_alpha") { - imgformat=Image::FORMAT_INDEXED_ALPHA; - } else if (format=="bc1") { - imgformat=Image::FORMAT_DXT1; - } else if (format=="bc2") { - imgformat=Image::FORMAT_DXT3; - } else if (format=="bc3") { - imgformat=Image::FORMAT_DXT5; - } else if (format=="bc4") { - imgformat=Image::FORMAT_ATI1; - } else if (format=="bc5") { - imgformat=Image::FORMAT_ATI2; - } else if (format=="pvrtc2") { - imgformat=Image::FORMAT_PVRTC2; - } else if (format=="pvrtc2a") { - imgformat=Image::FORMAT_PVRTC2A; - } else if (format=="pvrtc4") { - imgformat=Image::FORMAT_PVRTC4; - } else if (format=="pvrtc4a") { - imgformat=Image::FORMAT_PVRTC4A; - } else if (format=="etc") { - imgformat=Image::FORMAT_ETC; - } else if (format=="atc") { - imgformat=Image::FORMAT_ATC; - } else if (format=="atcai") { - imgformat=Image::FORMAT_ATC_ALPHA_INTERPOLATED; - } else if (format=="atcae") { - imgformat=Image::FORMAT_ATC_ALPHA_EXPLICIT; - } else if (format=="custom") { - imgformat=Image::FORMAT_CUSTOM; - } else { - - ERR_FAIL_V( ERR_FILE_CORRUPT ); - }*/ - - - int datasize; - int w=width.to_int(); - int h=height.to_int(); - - if (w == 0 && h == 0) { - //r_v = Image(w, h, imgformat); - r_v=Image(); - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - return OK; - }; - - - if (datasize==0) { - //r_v = Image(w, h, imgformat); - r_v=Image(); - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - return OK; - }; - - DVector<uint8_t> pixels; - pixels.resize(datasize); - DVector<uint8_t>::Write wb = pixels.write(); - - int idx=0; - uint8_t byte; - while( idx<datasize*2) { - - CharType c=get_char(); - - ERR_FAIL_COND_V(c=='<',ERR_FILE_CORRUPT); - - if ( (c>='0' && c<='9') || (c>='A' && c<='F') || (c>='a' && c<='f') ) { - - if (idx&1) { - - byte|=HEX2CHR(c); - wb[idx>>1]=byte; - } else { - - byte=HEX2CHR(c)<<4; - } - - idx++; - } - - } - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - wb=DVector<uint8_t>::Write(); - - r_v=Image(w,h,mipmaps,imgformat,pixels); - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - ERR_FAIL_COND_V(err,err); - - return OK; - } - - ERR_FAIL_V(ERR_FILE_CORRUPT); - - } else if (type=="raw_array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": RawArray missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int(); - - DVector<uint8_t> bytes; - bytes.resize(len); - DVector<uint8_t>::Write w=bytes.write(); - uint8_t *bytesptr=w.ptr(); - int idx=0; - uint8_t byte; - - while( idx<len*2) { - - CharType c=get_char(); - if (c<=32) - continue; - - if (idx&1) { - - byte|=HEX2CHR(c); - bytesptr[idx>>1]=byte; - //printf("%x\n",int(byte)); - } else { - - byte=HEX2CHR(c)<<4; - } - - idx++; - } - - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - w=DVector<uint8_t>::Write(); - r_v=bytes; - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - ERR_FAIL_COND_V(err,err); - r_name=name; - - return OK; - - } else if (type=="int_array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int(); - - DVector<int> ints; - ints.resize(len); - DVector<int>::Write w=ints.write(); - int *intsptr=w.ptr(); - int idx=0; - String str; -#if 0 - while( idx<len ) { - - - CharType c=get_char(); - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - if (c<33 || c==',' || c=='<') { - - if (str.length()) { - - intsptr[idx]=str.to_int(); - str=""; - idx++; - } - - if (c=='<') { - - while(get_char()!='>' && !f->eof_reached()) {} - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - break; - } - - } else { - - str+=c; - } - } - -#else - - Vector<char> tmpdata; - - while( idx<len ) { - - bool end=false; - Error err = _parse_array_element(tmpdata,true,f,&end); - ERR_FAIL_COND_V(err,err); - - intsptr[idx]=String::to_int(&tmpdata[0]); - idx++; - if (end) - break; - - } - -#endif - w=DVector<int>::Write(); - - r_v=ints; - Error err=goto_end_of_tag(); - ERR_FAIL_COND_V(err,err); - r_name=name; - - return OK; - } else if (type=="real_array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int();; - - DVector<real_t> reals; - reals.resize(len); - DVector<real_t>::Write w=reals.write(); - real_t *realsptr=w.ptr(); - int idx=0; - String str; - - -#if 0 - while( idx<len ) { - - - CharType c=get_char(); - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - - if (c<33 || c==',' || c=='<') { - - if (str.length()) { - - realsptr[idx]=str.to_double(); - str=""; - idx++; - } - - if (c=='<') { - - while(get_char()!='>' && !f->eof_reached()) {} - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - break; - } - - } else { - - str+=c; - } - } - -#else - - - - Vector<char> tmpdata; - - while( idx<len ) { - - bool end=false; - Error err = _parse_array_element(tmpdata,true,f,&end); - ERR_FAIL_COND_V(err,err); - - realsptr[idx]=String::to_double(&tmpdata[0]); - idx++; - - if (end) - break; - } - -#endif - - w=DVector<real_t>::Write(); - r_v=reals; - - Error err=goto_end_of_tag(); - ERR_FAIL_COND_V(err,err); - r_name=name; - - return OK; - } else if (type=="string_array") { -#if 0 - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int(); - - DVector<String> strings; - strings.resize(len); - DVector<String>::Write w=strings.write(); - String *stringsptr=w.ptr(); - int idx=0; - String str; - - bool inside_str=false; - CharString cs; - while( idx<len ) { - - - CharType c=get_char(); - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - - if (c=='"') { - if (inside_str) { - - cs.push_back(0); - String str; - str.parse_utf8(cs.get_data()); - unquote(str); - stringsptr[idx]=str; - cs.clear(); - idx++; - inside_str=false; - } else { - inside_str=true; - } - } else if (c=='<') { - - while(get_char()!='>' && !f->eof_reached()) {} - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - break; - - - } else if (inside_str){ - - cs.push_back(c); - } - } - w=DVector<String>::Write(); - r_v=strings; - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - ERR_FAIL_COND_V(err,err); - - r_name=name; - - return OK; -#endif - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": String Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - - - int len=tag->args["len"].to_int(); - - StringArray array; - array.resize(len); - DVector<String>::Write w = array.write(); - - Error err; - Variant v; - String tagname; - int idx=0; - - - while( (err=parse_property(v,tagname))==OK ) { - - ERR_CONTINUE( idx <0 || idx >=len ); - String str = v; //convert back to string - w[idx]=str; - idx++; - } - - if (idx!=len) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array (size mismatch): "+name); - ERR_FAIL_COND_V(idx!=len,err); - } - - if (err!=ERR_FILE_EOF) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array: "+name); - ERR_FAIL_COND_V(err!=ERR_FILE_EOF,err); - } - - //err=parse_property_data(name); // skip the rest - //ERR_FAIL_COND_V(err,err); - - r_name=name; - r_v=array; - return OK; - - } else if (type=="vector3_array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int();; - - DVector<Vector3> vectors; - vectors.resize(len); - DVector<Vector3>::Write w=vectors.write(); - Vector3 *vectorsptr=w.ptr(); - int idx=0; - int subidx=0; - Vector3 auxvec; - String str; - -// uint64_t tbegin = OS::get_singleton()->get_ticks_usec(); -#if 0 - while( idx<len ) { - - - CharType c=get_char(); - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - - if (c<33 || c==',' || c=='<') { - - if (str.length()) { - - auxvec[subidx]=str.to_double(); - subidx++; - str=""; - if (subidx==3) { - vectorsptr[idx]=auxvec; - - idx++; - subidx=0; - } - } - - if (c=='<') { - - while(get_char()!='>' && !f->eof_reached()) {} - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - break; - } - - } else { - - str+=c; - } - } -#else - - Vector<char> tmpdata; - - while( idx<len ) { - - bool end=false; - Error err = _parse_array_element(tmpdata,true,f,&end); - ERR_FAIL_COND_V(err,err); - - - auxvec[subidx]=String::to_double(&tmpdata[0]); - subidx++; - if (subidx==3) { - vectorsptr[idx]=auxvec; - - idx++; - subidx=0; - } - - if (end) - break; - } - - - -#endif - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Premature end of vector3 array"); - ERR_FAIL_COND_V(idx<len,ERR_FILE_CORRUPT); -// double time_taken = (OS::get_singleton()->get_ticks_usec() - tbegin)/1000000.0; - - - w=DVector<Vector3>::Write(); - r_v=vectors; - String sdfsdfg; - Error err=goto_end_of_tag(); - ERR_FAIL_COND_V(err,err); - r_name=name; - - return OK; - - } else if (type=="vector2_array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int();; - - DVector<Vector2> vectors; - vectors.resize(len); - DVector<Vector2>::Write w=vectors.write(); - Vector2 *vectorsptr=w.ptr(); - int idx=0; - int subidx=0; - Vector2 auxvec; - String str; - -// uint64_t tbegin = OS::get_singleton()->get_ticks_usec(); -#if 0 - while( idx<len ) { - - - CharType c=get_char(); - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - - if (c<22 || c==',' || c=='<') { - - if (str.length()) { - - auxvec[subidx]=str.to_double(); - subidx++; - str=""; - if (subidx==2) { - vectorsptr[idx]=auxvec; - - idx++; - subidx=0; - } - } - - if (c=='<') { - - while(get_char()!='>' && !f->eof_reached()) {} - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - break; - } - - } else { - - str+=c; - } - } -#else - - Vector<char> tmpdata; - - while( idx<len ) { - - bool end=false; - Error err = _parse_array_element(tmpdata,true,f,&end); - ERR_FAIL_COND_V(err,err); - - - auxvec[subidx]=String::to_double(&tmpdata[0]); - subidx++; - if (subidx==2) { - vectorsptr[idx]=auxvec; - - idx++; - subidx=0; - } - - if (end) - break; - } - - - -#endif - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Premature end of vector2 array"); - ERR_FAIL_COND_V(idx<len,ERR_FILE_CORRUPT); -// double time_taken = (OS::get_singleton()->get_ticks_usec() - tbegin)/1000000.0; - - - w=DVector<Vector2>::Write(); - r_v=vectors; - String sdfsdfg; - Error err=goto_end_of_tag(); - ERR_FAIL_COND_V(err,err); - r_name=name; - - return OK; - - } else if (type=="color_array") { - - if (!tag->args.has("len")) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name); - ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT); - } - int len=tag->args["len"].to_int();; - - DVector<Color> colors; - colors.resize(len); - DVector<Color>::Write w=colors.write(); - Color *colorsptr=w.ptr(); - int idx=0; - int subidx=0; - Color auxcol; - String str; - - while( idx<len ) { - - - CharType c=get_char(); - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - - - if (c<33 || c==',' || c=='<') { - - if (str.length()) { - - auxcol[subidx]=str.to_double(); - subidx++; - str=""; - if (subidx==4) { - colorsptr[idx]=auxcol; - idx++; - subidx=0; - } - } - - if (c=='<') { - - while(get_char()!='>' && !f->eof_reached()) {} - ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT); - break; - } - - } else { - - str+=c; - } - } - w=DVector<Color>::Write(); - r_v=colors; - String sdfsdfg; - Error err=parse_property_data(sdfsdfg); - ERR_FAIL_COND_V(err,err); - r_name=name; - - return OK; - } - - - String data; - Error err = parse_property_data(data); - ERR_FAIL_COND_V(err!=OK,err); - - if (type=="nil") { - // uh do nothing - - } else if (type=="bool") { - // uh do nothing - if (data.nocasecmp_to("true")==0 || data.to_int()!=0) - r_v=true; - else - r_v=false; - } else if (type=="int") { - - r_v=data.to_int(); - } else if (type=="real") { - - r_v=data.to_double(); - } else if (type=="string") { - - String str=data; - unquote(str); - r_v=str; - } else if (type=="vector3") { - - - r_v=Vector3( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double(), - data.get_slicec(',',2).to_double() - ); - - } else if (type=="vector2") { - - - r_v=Vector2( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double() - ); - - } else if (type=="plane") { - - r_v=Plane( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double(), - data.get_slicec(',',2).to_double(), - data.get_slicec(',',3).to_double() - ); - - } else if (type=="quaternion") { - - r_v=Quat( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double(), - data.get_slicec(',',2).to_double(), - data.get_slicec(',',3).to_double() - ); - - } else if (type=="rect2") { - - r_v=Rect2( - Vector2( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double() - ), - Vector2( - data.get_slicec(',',2).to_double(), - data.get_slicec(',',3).to_double() - ) - ); - - - } else if (type=="aabb") { - - r_v=AABB( - Vector3( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double(), - data.get_slicec(',',2).to_double() - ), - Vector3( - data.get_slicec(',',3).to_double(), - data.get_slicec(',',4).to_double(), - data.get_slicec(',',5).to_double() - ) - ); - - } else if (type=="matrix32") { - - Matrix32 m3; - for (int i=0;i<3;i++) { - for (int j=0;j<2;j++) { - m3.elements[i][j]=data.get_slicec(',',i*2+j).to_double(); - } - } - r_v=m3; - - } else if (type=="matrix3") { - - Matrix3 m3; - for (int i=0;i<3;i++) { - for (int j=0;j<3;j++) { - m3.elements[i][j]=data.get_slicec(',',i*3+j).to_double(); - } - } - r_v=m3; - - } else if (type=="transform") { - - Transform tr; - for (int i=0;i<3;i++) { - for (int j=0;j<3;j++) { - tr.basis.elements[i][j]=data.get_slicec(',',i*3+j).to_double(); - } - - } - tr.origin=Vector3( - data.get_slicec(',',9).to_double(), - data.get_slicec(',',10).to_double(), - data.get_slicec(',',11).to_double() - ); - r_v=tr; - - } else if (type=="color") { - - r_v=Color( - data.get_slicec(',',0).to_double(), - data.get_slicec(',',1).to_double(), - data.get_slicec(',',2).to_double(), - data.get_slicec(',',3).to_double() - ); - - } else if (type=="node_path") { - - String str=data; - unquote(str); - r_v=NodePath( str ); - } else if (type=="input_event") { - - // ? - } else { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unrecognized tag in file: "+type); - ERR_FAIL_V(ERR_FILE_CORRUPT); - } - r_name=name; - return OK; -} - - - -int ResourceInteractiveLoaderXML::get_current_line() const { - - return lines; -} - - -uint8_t ResourceInteractiveLoaderXML::get_char() const { - - uint8_t c = f->get_8(); - if (c=='\n') - lines++; - return c; - -} - - - - -/// - -void ResourceInteractiveLoaderXML::set_local_path(const String& p_local_path) { - - res_path=p_local_path; -} - -Ref<Resource> ResourceInteractiveLoaderXML::get_resource() { - - return resource; -} -Error ResourceInteractiveLoaderXML::poll() { - - if (error!=OK) - return error; - - bool exit; - Tag *tag = parse_tag(&exit); - - - if (!tag) { - error=ERR_FILE_CORRUPT; - if (!exit) // shouldn't have exited - ERR_FAIL_V(error); - error=ERR_FILE_EOF; - return error; - } - - RES res; - //Object *obj=NULL; - - bool main; - - if (tag->name=="ext_resource") { - - error=ERR_FILE_CORRUPT; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field."); - ERR_FAIL_COND_V(!tag->args.has("path"),ERR_FILE_CORRUPT); - - String type="Resource"; - if (tag->args.has("type")) - type=tag->args["type"]; - - String path = tag->args["path"]; - - - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?."); - ERR_FAIL_COND_V(path.begins_with("local://"),ERR_FILE_CORRUPT); - - if (path.find("://")==-1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); - } - - if (remaps.has(path)) { - path=remaps[path]; - } - - RES res = ResourceLoader::load(path,type); - - if (res.is_null()) { - - if (ResourceLoader::get_abort_on_missing_resources()) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> referenced nonexistent resource at: "+path); - ERR_FAIL_V(error); - } else { - ResourceLoader::notify_dependency_error(local_path,path,type); - } - } else { - - resource_cache.push_back(res); - } - - if (tag->args.has("index")) { - ExtResource er; - er.path=path; - er.type=type; - ext_resources[tag->args["index"].to_int()]=er; - } - - - Error err = close_tag("ext_resource"); - if (err) - return error; - - - error=OK; - resource_current++; - return error; - - } else if (tag->name=="resource") { - - main=false; - } else if (tag->name=="main_resource") { - main=true; - } else { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": unexpected main tag: "+tag->name); - error=ERR_FILE_CORRUPT; - ERR_FAIL_V(error); - } - - - String type; - String path; - int subres=0; - - if (!main) { - //loading resource - - error=ERR_FILE_CORRUPT; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'len' field."); - ERR_FAIL_COND_V(!tag->args.has("path"),ERR_FILE_CORRUPT); - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'type' field."); - ERR_FAIL_COND_V(!tag->args.has("type"),ERR_FILE_CORRUPT); - path=tag->args["path"]; - - error=OK; - - if (path.begins_with("local://")) { - //built-in resource (but really external) - - path=path.replace("local://",""); - subres=path.to_int(); - path=local_path+"::"+path; - } - - - if (ResourceCache::has(path)) { - Error err = close_tag(tag->name); - if (err) { - error=ERR_FILE_CORRUPT; - } - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unable to close <resource> tag."); - ERR_FAIL_COND_V( err, err ); - resource_current++; - error=OK; - return OK; - } - - type = tag->args["type"]; - } else { - type=resource_type; - } - - Object *obj = ClassDB::instance(type); - if (!obj) { - error=ERR_FILE_CORRUPT; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Object of unrecognized type in file: "+type); - } - ERR_FAIL_COND_V(!obj,ERR_FILE_CORRUPT); - - Resource *r = obj->cast_to<Resource>(); - if (!r) { - error=ERR_FILE_CORRUPT; - memdelete(obj); //bye - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Object type in resource field not a resource, type is: "+obj->get_class()); - ERR_FAIL_COND_V(!r,ERR_FILE_CORRUPT); - } - - res = RES( r ); - if (path!="") - r->set_path(path); - r->set_subindex(subres); - - //load properties - - while(true) { - - String name; - Variant v; - Error err; - err = parse_property(v,name); - if (err==ERR_FILE_EOF) //tag closed - break; - if (err!=OK) { - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": XML Parsing aborted."); - ERR_FAIL_COND_V(err!=OK,ERR_FILE_CORRUPT); - } - - obj->set(name,v); - } -#ifdef TOOLS_ENABLED - res->set_edited(false); -#endif - resource_cache.push_back(res); //keep it in mem until finished loading - resource_current++; - if (main) { - f->close(); - resource=res; - if (!ResourceCache::has(res_path)) { - resource->set_path(res_path); - } - error=ERR_FILE_EOF; - return error; - - } - error=OK; - return OK; -} - -int ResourceInteractiveLoaderXML::get_stage() const { - - return resource_current; -} -int ResourceInteractiveLoaderXML::get_stage_count() const { - - return resources_total;//+ext_resources; -} - -ResourceInteractiveLoaderXML::~ResourceInteractiveLoaderXML() { - - memdelete(f); -} - -void ResourceInteractiveLoaderXML::get_dependencies(FileAccess *f,List<String> *p_dependencies,bool p_add_types) { - - - open(f); - ERR_FAIL_COND(error!=OK); - - while(true) { - bool exit; - Tag *tag = parse_tag(&exit); - - - if (!tag) { - error=ERR_FILE_CORRUPT; - ERR_FAIL_COND(!exit); - error=ERR_FILE_EOF; - return; - } - - if (tag->name!="ext_resource") { - - return; - } - - error=ERR_FILE_CORRUPT; - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field."); - ERR_FAIL_COND(!tag->args.has("path")); - - String path = tag->args["path"]; - - ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?."); - ERR_FAIL_COND(path.begins_with("local://")); - - if (path.find("://")==-1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); - } - - if (path.ends_with("*")) { - ERR_FAIL_COND(!tag->args.has("type")); - String type = tag->args["type"]; - path = ResourceLoader::guess_full_filename(path,type); - } - - if (p_add_types && tag->args.has("type")) { - path+="::"+tag->args["type"]; - } - - p_dependencies->push_back(path); - - Error err = close_tag("ext_resource"); - if (err) - return; - - error=OK; - } - -} - -Error ResourceInteractiveLoaderXML::rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map) { - - open(p_f); - ERR_FAIL_COND_V(error!=OK,error); - - //FileAccess - - bool old_format=false; - - FileAccess *fw = NULL; - - String base_path=local_path.get_base_dir(); - - while(true) { - bool exit; - List<String> order; - - Tag *tag = parse_tag(&exit,true,&order); - - bool done=false; - - if (!tag) { - if (fw) { - memdelete(fw); - } - error=ERR_FILE_CORRUPT; - ERR_FAIL_COND_V(!exit,error); - error=ERR_FILE_EOF; - - return error; - } - - if (tag->name=="ext_resource") { - - if (!tag->args.has("index") || !tag->args.has("path") || !tag->args.has("type")) { - old_format=true; - break; - } - - if (!fw) { - - fw=FileAccess::open(p_path+".depren",FileAccess::WRITE); - fw->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); //no escape - fw->store_line("<resource_file type=\""+resource_type+"\" subresource_count=\""+itos(resources_total)+"\" version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\" version_name=\""+VERSION_FULL_NAME+"\">"); - - } - - String path = tag->args["path"]; - String index = tag->args["index"]; - String type = tag->args["type"]; - - - bool relative=false; - if (!path.begins_with("res://")) { - path=base_path.plus_file(path).simplify_path(); - relative=true; - } - - - if (p_map.has(path)) { - String np=p_map[path]; - path=np; - } - - if (relative) { - //restore relative - path=base_path.path_to_file(path); - } - - tag->args["path"]=path; - tag->args["index"]=index; - tag->args["type"]=type; - - } else { - - done=true; - } - - String tagt="\t<"; - if (exit) - tagt+="/"; - tagt+=tag->name; - - for(List<String>::Element *E=order.front();E;E=E->next()) { - tagt+=" "+E->get()+"=\""+tag->args[E->get()]+"\""; - } - tagt+=">"; - fw->store_line(tagt); - if (done) - break; - close_tag("ext_resource"); - fw->store_line("\t</ext_resource>"); - - } - - - if (old_format) { - if (fw) - memdelete(fw); - - DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); - da->remove(p_path+".depren"); - memdelete(da); - //fuck it, use the old approach; - - WARN_PRINT(("This file is old, so it can't refactor dependencies, opening and resaving: "+p_path).utf8().get_data()); - - Error err; - FileAccess *f2 = FileAccess::open(p_path,FileAccess::READ,&err); - if (err!=OK) { - ERR_FAIL_COND_V(err!=OK,ERR_FILE_CANT_OPEN); - } - - Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); - ria->res_path=ria->local_path; - ria->remaps=p_map; - // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); - ria->open(f2); - - err = ria->poll(); - - while(err==OK) { - err=ria->poll(); - } - - ERR_FAIL_COND_V(err!=ERR_FILE_EOF,ERR_FILE_CORRUPT); - RES res = ria->get_resource(); - ERR_FAIL_COND_V(!res.is_valid(),ERR_FILE_CORRUPT); - - return ResourceFormatSaverXML::singleton->save(p_path,res); - } - - if (!fw) { - - return OK; //nothing to rename, do nothing - } - - uint8_t c=f->get_8(); - while(!f->eof_reached()) { - fw->store_8(c); - c=f->get_8(); - } - f->close(); - - bool all_ok = fw->get_error()==OK; - - memdelete(fw); - - if (!all_ok) { - return ERR_CANT_CREATE; - } - - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); - da->remove(p_path); - da->rename(p_path+".depren",p_path); - memdelete(da); - - return OK; - -} - - -void ResourceInteractiveLoaderXML::open(FileAccess *p_f) { - - error=OK; - - lines=1; - f=p_f; - - - ResourceInteractiveLoaderXML::Tag *tag = parse_tag(); - if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") { - - error=ERR_FILE_CORRUPT; - ResourceLoader::notify_load_error("XML is invalid (missing header tags)"); - ERR_EXPLAIN("Not a XML:UTF-8 File: "+local_path); - ERR_FAIL(); - } - - tag_stack.clear(); - - tag = parse_tag(); - - - if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) { - - ResourceLoader::notify_load_error(local_path+": XML is not a valid resource file."); - error=ERR_FILE_CORRUPT; - ERR_EXPLAIN("Unrecognized XML File: "+local_path); - ERR_FAIL(); - } - - - if (tag->args.has("subresource_count")) - resources_total=tag->args["subresource_count"].to_int(); - resource_current=0; - resource_type=tag->args["type"]; - - String version = tag->args["version"]; - if (version.get_slice_count(".")!=2) { - - error=ERR_FILE_CORRUPT; - ResourceLoader::notify_load_error(local_path+":XML version string is invalid: "+version); - ERR_EXPLAIN("Invalid Version String '"+version+"'' in file: "+local_path); - ERR_FAIL(); - } - - int major = version.get_slicec('.',0).to_int(); - if (major>VERSION_MAJOR) { - - error=ERR_FILE_UNRECOGNIZED; - ResourceLoader::notify_load_error(local_path+": File Format '"+version+"' is too new. Please upgrade to a newer engine version."); - ERR_EXPLAIN("File Format '"+version+"' is too new! Please upgrade to a a new engine version: "+local_path); - ERR_FAIL(); - - } - - /* - String preload_depts = "deps/"+local_path.md5_text(); - if (Globals::get_singleton()->has(preload_depts)) { - ext_resources.clear(); - //ignore external resources and use these - NodePath depts=Globals::get_singleton()->get(preload_depts); - - for(int i=0;i<depts.get_name_count();i++) { - ext_resources.push_back(depts.get_name(i)); - } - print_line(local_path+" - EXTERNAL RESOURCES: "+itos(ext_resources.size())); - } -*/ - -} - -String ResourceInteractiveLoaderXML::recognize(FileAccess *p_f) { - - error=OK; - - lines=1; - f=p_f; - - ResourceInteractiveLoaderXML::Tag *tag = parse_tag(); - if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") { - - - return ""; //unrecognized - } - - tag_stack.clear(); - - tag = parse_tag(); - - if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) { - - return ""; //unrecognized - } - - return tag->args["type"]; - -} - -///////////////////// - -Ref<ResourceInteractiveLoader> ResourceFormatLoaderXML::load_interactive(const String &p_path, Error *r_error) { - - if (r_error) - *r_error=ERR_CANT_OPEN; - - Error err; - FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err); - - - if (err!=OK) { - - ERR_FAIL_COND_V(err!=OK,Ref<ResourceInteractiveLoader>()); - } - - Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); - ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); - ria->open(f); - - return ria; -} - -void ResourceFormatLoaderXML::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const { - - if (p_type=="") { - get_recognized_extensions(p_extensions); - return; - } - - List<String> extensions; - ClassDB::get_extensions_for_type(p_type,&extensions); - - extensions.sort(); - - for(List<String>::Element *E=extensions.front();E;E=E->next()) { - String ext = E->get().to_lower(); - if (ext=="res") - continue; - p_extensions->push_back("x"+ext); - } - - p_extensions->push_back("xml"); - - -} -void ResourceFormatLoaderXML::get_recognized_extensions(List<String> *p_extensions) const{ - - List<String> extensions; - ClassDB::get_resource_base_extensions(&extensions); - extensions.sort(); - - for(List<String>::Element *E=extensions.front();E;E=E->next()) { - String ext = E->get().to_lower(); - if (ext=="res") - continue; - p_extensions->push_back("x"+ext); - } - - p_extensions->push_back("xml"); -} - -bool ResourceFormatLoaderXML::handles_type(const String& p_type) const{ - - return true; -} -String ResourceFormatLoaderXML::get_resource_type(const String &p_path) const{ - - - String ext=p_path.extension().to_lower(); - if (!ext.begins_with("x")) //a lie but.. - return ""; - - FileAccess *f = FileAccess::open(p_path,FileAccess::READ); - if (!f) { - - return ""; //could not rwead - } - - Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); - ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); - String r = ria->recognize(f); - return r; -} - - -void ResourceFormatLoaderXML::get_dependencies(const String& p_path,List<String> *p_dependencies,bool p_add_types) { - - FileAccess *f = FileAccess::open(p_path,FileAccess::READ); - if (!f) { - - ERR_FAIL(); - } - - Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); - ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); - ria->get_dependencies(f,p_dependencies,p_add_types); - - -} - -Error ResourceFormatLoaderXML::rename_dependencies(const String &p_path,const Map<String,String>& p_map) { - - FileAccess *f = FileAccess::open(p_path,FileAccess::READ); - if (!f) { - - ERR_FAIL_V(ERR_CANT_OPEN); - } - - Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); - ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); - return ria->rename_dependencies(f,p_path,p_map); -} - - -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ -/****************************************************************************************/ - - - -void ResourceFormatSaverXMLInstance::escape(String& p_str) { - - p_str=p_str.replace("&","&"); - p_str=p_str.replace("<","<"); - p_str=p_str.replace(">",">"); - p_str=p_str.replace("'","'"); - p_str=p_str.replace("\"","""); - for (char i=1;i<32;i++) { - - char chr[2]={i,0}; - const char hexn[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; - const char hex[8]={'&','#','0','0',hexn[i>>4],hexn[i&0xf],';',0}; - - p_str=p_str.replace(chr,hex); - } - - -} -void ResourceFormatSaverXMLInstance::write_tabs(int p_diff) { - - for (int i=0;i<depth+p_diff;i++) { - - f->store_8('\t'); - } -} - -void ResourceFormatSaverXMLInstance::write_string(String p_str,bool p_escape) { - - /* write an UTF8 string */ - if (p_escape) - escape(p_str); - - f->store_string(p_str);; - /* - CharString cs=p_str.utf8(); - const char *data=cs.get_data(); - - while (*data) { - f->store_8(*data); - data++; - }*/ - - -} - -void ResourceFormatSaverXMLInstance::enter_tag(const char* p_tag,const String& p_args) { - - f->store_8('<'); - int cc = 0; - const char *c=p_tag; - while(*c) { - cc++; - c++; - } - f->store_buffer((const uint8_t*)p_tag,cc); - if (p_args.length()) { - f->store_8(' '); - f->store_string(p_args); - } - f->store_8('>'); - depth++; - -} -void ResourceFormatSaverXMLInstance::exit_tag(const char* p_tag) { - - depth--; - f->store_8('<'); - f->store_8('/'); - int cc = 0; - const char *c=p_tag; - while(*c) { - cc++; - c++; - } - f->store_buffer((const uint8_t*)p_tag,cc); - f->store_8('>'); - -} - -/* -static bool _check_type(const Variant& p_property) { - - if (p_property.get_type()==Variant::_RID) - return false; - if (p_property.get_type()==Variant::OBJECT) { - RES res = p_property; - if (res.is_null()) - return false; - } - - return true; -}*/ - -void ResourceFormatSaverXMLInstance::write_property(const String& p_name,const Variant& p_property,bool *r_ok) { - - if (r_ok) - *r_ok=false; - - const char* type; - String params; - bool oneliner=true; - - switch( p_property.get_type() ) { - - case Variant::NIL: type="nil"; break; - case Variant::BOOL: type="bool"; break; - case Variant::INT: type="int"; break; - case Variant::REAL: type="real"; break; - case Variant::STRING: type="string"; break; - case Variant::VECTOR2: type="vector2"; break; - case Variant::RECT2: type="rect2"; break; - case Variant::VECTOR3: type="vector3"; break; - case Variant::PLANE: type="plane"; break; - case Variant::_AABB: type="aabb"; break; - case Variant::QUAT: type="quaternion"; break; - case Variant::MATRIX32: type="matrix32"; break; - case Variant::MATRIX3: type="matrix3"; break; - case Variant::TRANSFORM: type="transform"; break; - case Variant::COLOR: type="color"; break; - case Variant::IMAGE: { - type="image"; - Image img=p_property; - if (img.empty()) { - write_tabs(); - enter_tag(type,"name=\""+p_name+"\""); - exit_tag(type); - if (r_ok) - *r_ok=true; - return; - } - params+="encoding=\"raw\""; - params+=" width=\""+itos(img.get_width())+"\""; - params+=" height=\""+itos(img.get_height())+"\""; - params+=" mipmaps=\""+itos(img.has_mipmaps())+"\""; -/* - switch(img.get_format()) { - - case Image::FORMAT_L8: params+=" format=\"grayscale\""; break; - case Image::FORMAT_INTENSITY: params+=" format=\"intensity\""; break; - case Image::FORMAT_LA8: params+=" format=\"grayscale_alpha\""; break; - case Image::FORMAT_RGB8: params+=" format=\"rgb\""; break; - case Image::FORMAT_RGBA8: params+=" format=\"rgba\""; break; - case Image::FORMAT_INDEXED : params+=" format=\"indexed\""; break; - case Image::FORMAT_INDEXED_ALPHA: params+=" format=\"indexed_alpha\""; break; - case Image::FORMAT_DXT1: params+=" format=\"bc1\""; break; - case Image::FORMAT_DXT3: params+=" format=\"bc2\""; break; - case Image::FORMAT_DXT5: params+=" format=\"bc3\""; break; - case Image::FORMAT_ATI1: params+=" format=\"bc4\""; break; - case Image::FORMAT_ATI2: params+=" format=\"bc5\""; break; - case Image::FORMAT_PVRTC2: params+=" format=\"pvrtc2\""; break; - case Image::FORMAT_PVRTC2A: params+=" format=\"pvrtc2a\""; break; - case Image::FORMAT_PVRTC4: params+=" format=\"pvrtc4\""; break; - case Image::FORMAT_PVRTC4A: params+=" format=\"pvrtc4a\""; break; - case Image::FORMAT_ETC: params+=" format=\"etc\""; break; - case Image::FORMAT_ATC: params+=" format=\"atc\""; break; - case Image::FORMAT_ATC_ALPHA_EXPLICIT: params+=" format=\"atcae\""; break; - case Image::FORMAT_ATC_ALPHA_INTERPOLATED: params+=" format=\"atcai\""; break; - case Image::FORMAT_CUSTOM: params+=" format=\"custom\" custom_size=\""+itos(img.get_data().size())+"\""; break; - default: {} - }*/ - } break; - case Variant::NODE_PATH: type="node_path"; break; - case Variant::OBJECT: { - type="resource"; - RES res = p_property; - if (res.is_null()) { - write_tabs(); - enter_tag(type,"name=\""+p_name+"\""); - exit_tag(type); - if (r_ok) - *r_ok=true; - - return; // don't save it - } - - if (external_resources.has(res)) { - - params="external=\""+itos(external_resources[res])+"\""; - } else { - params="resource_type=\""+res->get_save_class()+"\""; - - - if (res->get_path().length() && res->get_path().find("::")==-1) { - //external resource - String path=relative_paths?local_path.path_to_file(res->get_path()):res->get_path(); - escape(path); - params+=" path=\""+path+"\""; - } else { - - //internal resource - ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?"); - ERR_FAIL_COND(!resource_set.has(res)); - - params+=" path=\"local://"+itos(res->get_subindex())+"\""; - } - } - - } break; - case Variant::INPUT_EVENT: type="input_event"; break; - case Variant::DICTIONARY: type="dictionary"; params="shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break; - case Variant::ARRAY: type="array"; params="len=\""+itos(p_property.operator Array().size())+"\" shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break; - - case Variant::RAW_ARRAY: type="raw_array"; params="len=\""+itos(p_property.operator DVector < uint8_t >().size())+"\""; break; - case Variant::INT_ARRAY: type="int_array"; params="len=\""+itos(p_property.operator DVector < int >().size())+"\""; break; - case Variant::REAL_ARRAY: type="real_array"; params="len=\""+itos(p_property.operator DVector < real_t >().size())+"\""; break; - case Variant::STRING_ARRAY: oneliner=false; type="string_array"; params="len=\""+itos(p_property.operator DVector < String >().size())+"\""; break; - case Variant::VECTOR2_ARRAY: type="vector2_array"; params="len=\""+itos(p_property.operator DVector < Vector2 >().size())+"\""; break; - case Variant::VECTOR3_ARRAY: type="vector3_array"; params="len=\""+itos(p_property.operator DVector < Vector3 >().size())+"\""; break; - case Variant::COLOR_ARRAY: type="color_array"; params="len=\""+itos(p_property.operator DVector < Color >().size())+"\""; break; - default: { - - ERR_PRINT("Unknown Variant type."); - ERR_FAIL(); - } - - } - - write_tabs(); - - if (p_name!="") { - if (params.length()) - enter_tag(type,"name=\""+p_name+"\" "+params); - else - enter_tag(type,"name=\""+p_name+"\""); - } else { - if (params.length()) - enter_tag(type," "+params); - else - enter_tag(type,String()); - } - - if (!oneliner) - f->store_8('\n'); - else - f->store_8(' '); - - - switch( p_property.get_type() ) { - - case Variant::NIL: { - - } break; - case Variant::BOOL: { - - write_string( p_property.operator bool() ? "True":"False" ); - } break; - case Variant::INT: { - - write_string( itos(p_property.operator int()) ); - } break; - case Variant::REAL: { - - write_string( rtos(p_property.operator real_t()) ); - } break; - case Variant::STRING: { - - String str=p_property; - escape(str); - str="\""+str+"\""; - write_string( str,false ); - } break; - case Variant::VECTOR2: { - - Vector2 v = p_property; - write_string( rtoss(v.x) +", "+rtoss(v.y) ); - } break; - case Variant::RECT2: { - - Rect2 aabb = p_property; - write_string( rtoss(aabb.pos.x) +", "+rtoss(aabb.pos.y) +", "+rtoss(aabb.size.x) +", "+rtoss(aabb.size.y) ); - - } break; - case Variant::VECTOR3: { - - Vector3 v = p_property; - write_string( rtoss(v.x) +", "+rtoss(v.y)+", "+rtoss(v.z) ); - } break; - case Variant::PLANE: { - - Plane p = p_property; - write_string( rtoss(p.normal.x) +", "+rtoss(p.normal.y)+", "+rtoss(p.normal.z)+", "+rtoss(p.d) ); - - } break; - case Variant::_AABB: { - - AABB aabb = p_property; - write_string( rtoss(aabb.pos.x) +", "+rtoss(aabb.pos.y) +", "+rtoss(aabb.pos.z) +", "+rtoss(aabb.size.x) +", "+rtoss(aabb.size.y) +", "+rtoss(aabb.size.z) ); - - } break; - case Variant::QUAT: { - - Quat quat = p_property; - write_string( rtoss(quat.x)+", "+rtoss(quat.y)+", "+rtoss(quat.z)+", "+rtoss(quat.w)+", "); - - } break; - case Variant::MATRIX32: { - - String s; - Matrix32 m3 = p_property; - for (int i=0;i<3;i++) { - for (int j=0;j<2;j++) { - - if (i!=0 || j!=0) - s+=", "; - s+=rtoss( m3.elements[i][j] ); - } - } - - write_string(s); - - } break; - case Variant::MATRIX3: { - - String s; - Matrix3 m3 = p_property; - for (int i=0;i<3;i++) { - for (int j=0;j<3;j++) { - - if (i!=0 || j!=0) - s+=", "; - s+=rtoss( m3.elements[i][j] ); - } - } - - write_string(s); - - } break; - case Variant::TRANSFORM: { - - String s; - Transform t = p_property; - Matrix3 &m3 = t.basis; - for (int i=0;i<3;i++) { - for (int j=0;j<3;j++) { - - if (i!=0 || j!=0) - s+=", "; - s+=rtoss( m3.elements[i][j] ); - } - } - - s=s+", "+rtoss(t.origin.x) +", "+rtoss(t.origin.y)+", "+rtoss(t.origin.z); - - write_string(s); - } break; - - // misc types - case Variant::COLOR: { - - Color c = p_property; - write_string( rtoss(c.r) +", "+rtoss(c.g)+", "+rtoss(c.b)+", "+rtoss(c.a) ); - - } break; - case Variant::IMAGE: { - - String s; - Image img = p_property; - DVector<uint8_t> data = img.get_data(); - int len = data.size(); - DVector<uint8_t>::Read r = data.read(); - const uint8_t *ptr=r.ptr();; - for (int i=0;i<len;i++) { - - uint8_t byte = ptr[i]; - const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; - char str[3]={ hex[byte>>4], hex[byte&0xF], 0}; - s+=str; - } - - write_string(s); - } break; - case Variant::NODE_PATH: { - - String str=p_property; - escape(str); - str="\""+str+"\""; - write_string( str,false); - - } break; - - case Variant::OBJECT: { - /* this saver does not save resources in here - RES res = p_property; - - if (!res.is_null()) { - - String path=res->get_path(); - if (!res->is_shared() || !path.length()) { - // if no path, or path is from inside a scene - write_object( *res ); - } - - } - */ - - } break; - case Variant::INPUT_EVENT: { - - write_string( p_property.operator String() ); - } break; - case Variant::DICTIONARY: { - - Dictionary dict = p_property; - - - List<Variant> keys; - dict.get_key_list(&keys); - keys.sort(); - - for(List<Variant>::Element *E=keys.front();E;E=E->next()) { - - //if (!_check_type(dict[E->get()])) - // continue; - bool ok; - write_property("",E->get(),&ok); - ERR_CONTINUE(!ok); - - write_property("",dict[E->get()],&ok); - if (!ok) - write_property("",Variant()); //at least make the file consistent.. - } - - - - - } break; - case Variant::ARRAY: { - - Array array = p_property; - int len=array.size(); - for (int i=0;i<len;i++) { - - write_property("",array[i]); - - } - - } break; - - case Variant::RAW_ARRAY: { - - String s; - DVector<uint8_t> data = p_property; - int len = data.size(); - DVector<uint8_t>::Read r = data.read(); - const uint8_t *ptr=r.ptr();; - for (int i=0;i<len;i++) { - - uint8_t byte = ptr[i]; - const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; - char str[3]={ hex[byte>>4], hex[byte&0xF], 0}; - s+=str; - } - - write_string(s,false); - - } break; - case Variant::INT_ARRAY: { - - DVector<int> data = p_property; - int len = data.size(); - DVector<int>::Read r = data.read(); - const int *ptr=r.ptr();; - write_tabs(); - - for (int i=0;i<len;i++) { - - if (i>0) - write_string(", ",false); - - write_string(itos(ptr[i]),false); - } - - - - } break; - case Variant::REAL_ARRAY: { - - DVector<real_t> data = p_property; - int len = data.size(); - DVector<real_t>::Read r = data.read(); - const real_t *ptr=r.ptr();; - write_tabs(); - String cm=", " ; - - for (int i=0;i<len;i++) { - - if (i>0) - write_string(cm,false); - write_string(rtoss(ptr[i]),false); - } - - - } break; - case Variant::STRING_ARRAY: { - - DVector<String> data = p_property; - int len = data.size(); - DVector<String>::Read r = data.read(); - const String *ptr=r.ptr();; - String s; - //write_string("\n"); - - - - for (int i=0;i<len;i++) { - - write_tabs(0); - String str=ptr[i]; - escape(str); - write_string("<string> \""+str+"\" </string>\n",false); - } - } break; - case Variant::VECTOR2_ARRAY: { - - DVector<Vector2> data = p_property; - int len = data.size(); - DVector<Vector2>::Read r = data.read(); - const Vector2 *ptr=r.ptr();; - write_tabs(); - - for (int i=0;i<len;i++) { - - if (i>0) - write_string(", ",false); - write_string(rtoss(ptr[i].x),false); - write_string(", "+rtoss(ptr[i].y),false); - - } - - - } break; - case Variant::VECTOR3_ARRAY: { - - DVector<Vector3> data = p_property; - int len = data.size(); - DVector<Vector3>::Read r = data.read(); - const Vector3 *ptr=r.ptr();; - write_tabs(); - - for (int i=0;i<len;i++) { - - if (i>0) - write_string(", ",false); - write_string(rtoss(ptr[i].x),false); - write_string(", "+rtoss(ptr[i].y),false); - write_string(", "+rtoss(ptr[i].z),false); - - } - - - } break; - case Variant::COLOR_ARRAY: { - - DVector<Color> data = p_property; - int len = data.size(); - DVector<Color>::Read r = data.read(); - const Color *ptr=r.ptr();; - write_tabs(); - - for (int i=0;i<len;i++) { - - if (i>0) - write_string(", ",false); - - write_string(rtoss(ptr[i].r),false); - write_string(", "+rtoss(ptr[i].g),false); - write_string(", "+rtoss(ptr[i].b),false); - write_string(", "+rtoss(ptr[i].a),false); - - } - - } break; - default: {} - - } - if (oneliner) - f->store_8(' '); - else - write_tabs(-1); - exit_tag(type); - - f->store_8('\n'); - - if (r_ok) - *r_ok=true; - -} - - -void ResourceFormatSaverXMLInstance::_find_resources(const Variant& p_variant,bool p_main) { - - - switch(p_variant.get_type()) { - case Variant::OBJECT: { - - - RES res = p_variant.operator RefPtr(); - - if (res.is_null() || external_resources.has(res)) - return; - - if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) { - int index = external_resources.size(); - external_resources[res]=index; - return; - } - - if (resource_set.has(res)) - return; - - List<PropertyInfo> property_list; - - res->get_property_list( &property_list ); - property_list.sort(); - - List<PropertyInfo>::Element *I=property_list.front(); - - while(I) { - - PropertyInfo pi=I->get(); - - if (pi.usage&PROPERTY_USAGE_STORAGE) { - - Variant v=res->get(I->get().name); - _find_resources(v); - } - - I=I->next(); - } - - resource_set.insert( res ); //saved after, so the childs it needs are available when loaded - saved_resources.push_back(res); - - } break; - case Variant::ARRAY: { - - Array varray=p_variant; - int len=varray.size(); - for(int i=0;i<len;i++) { - - Variant v=varray.get(i); - _find_resources(v); - } - - } break; - case Variant::DICTIONARY: { - - Dictionary d=p_variant; - List<Variant> keys; - d.get_key_list(&keys); - for(List<Variant>::Element *E=keys.front();E;E=E->next()) { - - Variant v = d[E->get()]; - _find_resources(v); - } - } break; - default: {} - } - -} - - - -Error ResourceFormatSaverXMLInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) { - - Error err; - f = FileAccess::open(p_path, FileAccess::WRITE,&err); - ERR_FAIL_COND_V( err, ERR_CANT_OPEN ); - FileAccessRef _fref(f); - - local_path = Globals::get_singleton()->localize_path(p_path); - - relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS; - skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES; - bundle_resources=p_flags&ResourceSaver::FLAG_BUNDLE_RESOURCES; - takeover_paths=p_flags&ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS; - if (!p_path.begins_with("res://")) { - takeover_paths=false; - } - depth=0; - - // save resources - _find_resources(p_resource,true); - - ERR_FAIL_COND_V(err!=OK,err); - - write_string("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>",false); //no escape - write_string("\n",false); - enter_tag("resource_file","type=\""+p_resource->get_class()+"\" subresource_count=\""+itos(saved_resources.size()+external_resources.size())+"\" version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\" version_name=\""+VERSION_FULL_NAME+"\""); - write_string("\n",false); - - for(Map<RES,int>::Element *E=external_resources.front();E;E=E->next()) { - - write_tabs(); - String p = E->key()->get_path(); - - enter_tag("ext_resource","path=\""+p+"\" type=\""+E->key()->get_save_class()+"\" index=\""+itos(E->get())+"\""); //bundled - exit_tag("ext_resource"); //bundled - write_string("\n",false); - } - - Set<int> used_indices; - - for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) { - - RES res = E->get(); - if (E->next() && (res->get_path()=="" || res->get_path().find("::") != -1 )) { - - if (res->get_subindex()!=0) { - if (used_indices.has(res->get_subindex())) { - res->set_subindex(0); //repeated - } else { - used_indices.insert(res->get_subindex()); - } - } - } - } - - for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) { - - RES res = E->get(); - ERR_CONTINUE(!resource_set.has(res)); - bool main = (E->next()==NULL); - - write_tabs(); - - if (main) - enter_tag("main_resource",""); //bundled - else if (res->get_path().length() && res->get_path().find("::") == -1 ) - enter_tag("resource","type=\""+res->get_class()+"\" path=\""+res->get_path()+"\""); //bundled - else { - - if (res->get_subindex()==0) { - int new_subindex=1; - if (used_indices.size()) { - new_subindex=used_indices.back()->get()+1; - } - - res->set_subindex(new_subindex); - used_indices.insert(new_subindex); - } - - int idx = res->get_subindex(); - enter_tag("resource","type=\""+res->get_class()+"\" path=\"local://"+itos(idx)+"\""); - if (takeover_paths) { - res->set_path(p_path+"::"+itos(idx),true); - } -#ifdef TOOLS_ENABLED - res->set_edited(false); -#endif - - - } - write_string("\n",false); - - - List<PropertyInfo> property_list; - res->get_property_list(&property_list); -// property_list.sort(); - for(List<PropertyInfo>::Element *PE = property_list.front();PE;PE=PE->next()) { - - - if (skip_editor && PE->get().name.begins_with("__editor")) - continue; - - if (PE->get().usage&PROPERTY_USAGE_STORAGE ) { - - String name = PE->get().name; - Variant value = res->get(name); - - - if ((PE->get().usage&PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero())||(PE->get().usage&PROPERTY_USAGE_STORE_IF_NONONE && value.is_one()) ) - continue; - - - write_property(name,value); - } - - - } - - write_string("\n",false); - write_tabs(-1); - if (main) - exit_tag("main_resource"); - else - exit_tag("resource"); - - write_string("\n",false); - } - - exit_tag("resource_file"); - if (f->get_error()!=OK && f->get_error()!=ERR_FILE_EOF) { - f->close(); - return ERR_CANT_CREATE; - } - - f->close(); - //memdelete(f); - - return OK; -} - - - -Error ResourceFormatSaverXML::save(const String &p_path,const RES& p_resource,uint32_t p_flags) { - - ResourceFormatSaverXMLInstance saver; - return saver.save(p_path,p_resource,p_flags); - -} - -bool ResourceFormatSaverXML::recognize(const RES& p_resource) const { - - - return true; // all recognized! -} -void ResourceFormatSaverXML::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const { - - - //here comes the sun, lalalala - String base = p_resource->get_base_extension().to_lower(); - p_extensions->push_back("xml"); - if (base!="res") { - - p_extensions->push_back("x"+base); - } - -} - -ResourceFormatSaverXML* ResourceFormatSaverXML::singleton=NULL; -ResourceFormatSaverXML::ResourceFormatSaverXML() { - singleton=this; -} diff --git a/core/io/resource_format_xml.h b/core/io/resource_format_xml.h deleted file mode 100644 index 097c2e43f8..0000000000 --- a/core/io/resource_format_xml.h +++ /dev/null @@ -1,170 +0,0 @@ -/*************************************************************************/ -/* resource_format_xml.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef RESOURCE_FORMAT_XML_H -#define RESOURCE_FORMAT_XML_H - -#include "io/resource_loader.h" -#include "io/resource_saver.h" -#include "os/file_access.h" - - - -class ResourceInteractiveLoaderXML : public ResourceInteractiveLoader { - - String local_path; - String res_path; - - FileAccess *f; - - struct Tag { - - String name; - HashMap<String,String> args; - - }; - - _FORCE_INLINE_ Error _parse_array_element(Vector<char> &buff,bool p_number_only,FileAccess *f,bool *end); - - - struct ExtResource { - String path; - String type; - }; - - - Map<String,String> remaps; - - Map<int,ExtResource> ext_resources; - - int resources_total; - int resource_current; - String resource_type; - - mutable int lines; - uint8_t get_char() const; - int get_current_line() const; - -friend class ResourceFormatLoaderXML; - List<Tag> tag_stack; - - List<RES> resource_cache; - Tag* parse_tag(bool* r_exit=NULL,bool p_printerr=true,List<String> *r_order=NULL); - Error close_tag(const String& p_name); - _FORCE_INLINE_ void unquote(String& p_str); - Error goto_end_of_tag(); - Error parse_property_data(String &r_data); - Error parse_property(Variant& r_v, String &r_name); - - Error error; - - RES resource; - -public: - - virtual void set_local_path(const String& p_local_path); - virtual Ref<Resource> get_resource(); - virtual Error poll(); - virtual int get_stage() const; - virtual int get_stage_count() const; - - void open(FileAccess *p_f); - String recognize(FileAccess *p_f); - void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types); - Error rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map); - - - ~ResourceInteractiveLoaderXML(); - -}; - -class ResourceFormatLoaderXML : public ResourceFormatLoader { -public: - - virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path,Error *r_error=NULL); - virtual void get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const; - virtual void get_recognized_extensions(List<String> *p_extensions) const; - virtual bool handles_type(const String& p_type) const; - virtual String get_resource_type(const String &p_path) const; - virtual void get_dependencies(const String& p_path, List<String> *p_dependencies, bool p_add_types=false); - virtual Error rename_dependencies(const String &p_path,const Map<String,String>& p_map); - - -}; - - -//////////////////////////////////////////////////////////////////////////////////////////// - - -class ResourceFormatSaverXMLInstance { - - String local_path; - - - - bool takeover_paths; - bool relative_paths; - bool bundle_resources; - bool skip_editor; - FileAccess *f; - int depth; - Set<RES> resource_set; - List<RES> saved_resources; - Map<RES,int> external_resources; - - void enter_tag(const char* p_tag,const String& p_args=String()); - void exit_tag(const char* p_tag); - - void _find_resources(const Variant& p_variant,bool p_main=false); - void write_property(const String& p_name,const Variant& p_property,bool *r_ok=NULL); - - - void escape(String& p_str); - void write_tabs(int p_diff=0); - void write_string(String p_str,bool p_escape=true); - - -public: - - Error save(const String &p_path,const RES& p_resource,uint32_t p_flags=0); - - -}; - -class ResourceFormatSaverXML : public ResourceFormatSaver { -public: - static ResourceFormatSaverXML* singleton; - virtual Error save(const String &p_path,const RES& p_resource,uint32_t p_flags=0); - virtual bool recognize(const RES& p_resource) const; - virtual void get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const; - - ResourceFormatSaverXML(); -}; - - -#endif // RESOURCE_FORMAT_XML_H diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index f299a75d85..cc3c8ce006 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -164,7 +164,7 @@ RES ResourceLoader::load(const String &p_path, const String& p_type_hint, bool p if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); local_path=find_complete_path(local_path,p_type_hint); ERR_FAIL_COND_V(local_path=="",RES()); @@ -228,7 +228,7 @@ Ref<ResourceImportMetadata> ResourceLoader::load_import_metadata(const String &p if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); String extension=p_path.extension(); Ref<ResourceImportMetadata> ret; @@ -307,7 +307,7 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); local_path=find_complete_path(local_path,p_type_hint); ERR_FAIL_COND_V(local_path=="",Ref<ResourceInteractiveLoader>()); @@ -381,7 +381,7 @@ void ResourceLoader::get_dependencies(const String& p_path, List<String> *p_depe if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); String remapped_path = PathRemap::get_singleton()->get_remap(local_path); @@ -406,7 +406,7 @@ Error ResourceLoader::rename_dependencies(const String &p_path,const Map<String, if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); String remapped_path = PathRemap::get_singleton()->get_remap(local_path); @@ -434,7 +434,7 @@ String ResourceLoader::guess_full_filename(const String &p_path,const String& p_ if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); return find_complete_path(local_path,p_type); @@ -446,7 +446,7 @@ String ResourceLoader::get_resource_type(const String &p_path) { if (p_path.is_rel_path()) local_path="res://"+p_path; else - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); String remapped_path = PathRemap::get_singleton()->get_remap(local_path); String extension=remapped_path.extension(); diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index 704603f9ff..9081adaa8f 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -64,7 +64,7 @@ Error ResourceSaver::save(const String &p_path,const RES& p_resource,uint32_t p_ String old_path=p_resource->get_path(); - String local_path=Globals::get_singleton()->localize_path(p_path); + String local_path=GlobalConfig::get_singleton()->localize_path(p_path); RES rwcopy = p_resource; if (p_flags&FLAG_CHANGE_PATH) diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 218e17278e..a2812edb81 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -29,16 +29,16 @@ #include "stream_peer.h" #include "io/marshalls.h" -Error StreamPeer::_put_data(const DVector<uint8_t>& p_data) { +Error StreamPeer::_put_data(const PoolVector<uint8_t>& p_data) { int len = p_data.size(); if (len==0) return OK; - DVector<uint8_t>::Read r = p_data.read(); + PoolVector<uint8_t>::Read r = p_data.read(); return put_data(&r[0],len); } -Array StreamPeer::_put_partial_data(const DVector<uint8_t>& p_data) { +Array StreamPeer::_put_partial_data(const PoolVector<uint8_t>& p_data) { Array ret; @@ -49,7 +49,7 @@ Array StreamPeer::_put_partial_data(const DVector<uint8_t>& p_data) { return ret; } - DVector<uint8_t>::Read r = p_data.read(); + PoolVector<uint8_t>::Read r = p_data.read(); int sent; Error err = put_partial_data(&r[0],len,sent); @@ -66,18 +66,18 @@ Array StreamPeer::_get_data(int p_bytes) { Array ret; - DVector<uint8_t> data; + PoolVector<uint8_t> data; data.resize(p_bytes); if (data.size()!=p_bytes) { ret.push_back(ERR_OUT_OF_MEMORY); - ret.push_back(DVector<uint8_t>()); + ret.push_back(PoolVector<uint8_t>()); return ret; } - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); Error err = get_data(&w[0],p_bytes); - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); ret.push_back(err); ret.push_back(data); return ret; @@ -88,19 +88,19 @@ Array StreamPeer::_get_partial_data(int p_bytes) { Array ret; - DVector<uint8_t> data; + PoolVector<uint8_t> data; data.resize(p_bytes); if (data.size()!=p_bytes) { ret.push_back(ERR_OUT_OF_MEMORY); - ret.push_back(DVector<uint8_t>()); + ret.push_back(PoolVector<uint8_t>()); return ret; } - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); int received; Error err = get_partial_data(&w[0],p_bytes,received); - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); if (err!=OK) { data.resize(0); @@ -454,7 +454,7 @@ Error StreamPeerBuffer::put_data(const uint8_t* p_data,int p_bytes) { } - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); copymem(&w[pointer],p_data,p_bytes); pointer+=p_bytes; @@ -490,7 +490,7 @@ Error StreamPeerBuffer::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_r r_received=p_bytes; } - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); copymem(p_buffer,r.ptr(),r_received); } @@ -520,13 +520,13 @@ void StreamPeerBuffer::resize(int p_size){ data.resize(p_size); } -void StreamPeerBuffer::set_data_array(const DVector<uint8_t> & p_data){ +void StreamPeerBuffer::set_data_array(const PoolVector<uint8_t> & p_data){ data=p_data; pointer=0; } -DVector<uint8_t> StreamPeerBuffer::get_data_array() const{ +PoolVector<uint8_t> StreamPeerBuffer::get_data_array() const{ return data; } diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 232a908dd7..eb0f90ba50 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -38,8 +38,8 @@ protected: static void _bind_methods(); //bind helpers - Error _put_data(const DVector<uint8_t>& p_data); - Array _put_partial_data(const DVector<uint8_t>& p_data); + Error _put_data(const PoolVector<uint8_t>& p_data); + Array _put_partial_data(const PoolVector<uint8_t>& p_data); Array _get_data(int p_bytes); Array _get_partial_data(int p_bytes); @@ -96,7 +96,7 @@ class StreamPeerBuffer : public StreamPeer { GDCLASS(StreamPeerBuffer,StreamPeer); - DVector<uint8_t> data; + PoolVector<uint8_t> data; int pointer; protected: @@ -116,8 +116,8 @@ public: void resize(int p_size); - void set_data_array(const DVector<uint8_t> & p_data); - DVector<uint8_t> get_data_array() const; + void set_data_array(const PoolVector<uint8_t> & p_data); + PoolVector<uint8_t> get_data_array() const; void clear(); diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp index 751dfbadca..bfa5dce58f 100644 --- a/core/io/tcp_server.cpp +++ b/core/io/tcp_server.cpp @@ -44,7 +44,7 @@ TCP_Server* TCP_Server::create() { return _create(); } -Error TCP_Server::_listen(uint16_t p_port, DVector<String> p_accepted_hosts) { +Error TCP_Server::_listen(uint16_t p_port, PoolVector<String> p_accepted_hosts) { List<String> hosts; for(int i=0;i<p_accepted_hosts.size();i++) @@ -62,7 +62,7 @@ void TCP_Server::set_ip_type(IP::Type p_type) { void TCP_Server::_bind_methods() { ClassDB::bind_method(_MD("set_ip_type","ip_type"),&TCP_Server::set_ip_type); - ClassDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(DVector<String>())); + ClassDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(PoolVector<String>())); ClassDB::bind_method(_MD("is_connection_available"),&TCP_Server::is_connection_available); ClassDB::bind_method(_MD("take_connection"),&TCP_Server::take_connection); ClassDB::bind_method(_MD("stop"),&TCP_Server::stop); diff --git a/core/io/tcp_server.h b/core/io/tcp_server.h index 5fd65575f6..3d7b3ddd8d 100644 --- a/core/io/tcp_server.h +++ b/core/io/tcp_server.h @@ -43,7 +43,7 @@ protected: static TCP_Server* (*_create)(); //bind helper - Error _listen(uint16_t p_port, DVector<String> p_accepted_hosts=DVector<String>()); + Error _listen(uint16_t p_port, PoolVector<String> p_accepted_hosts=PoolVector<String>()); static void _bind_methods(); public: diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 29f61834b4..0d6997183f 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -295,10 +295,10 @@ bool AStar::_solve(Point* begin_point, Point* end_point) { } -DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { +PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { - ERR_FAIL_COND_V(!points.has(p_from_id),DVector<Vector3>()); - ERR_FAIL_COND_V(!points.has(p_to_id),DVector<Vector3>()); + ERR_FAIL_COND_V(!points.has(p_from_id),PoolVector<Vector3>()); + ERR_FAIL_COND_V(!points.has(p_to_id),PoolVector<Vector3>()); pass++; @@ -307,7 +307,7 @@ DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { Point* b = points[p_to_id]; if (a==b) { - DVector<Vector3> ret; + PoolVector<Vector3> ret; ret.push_back(a->pos); return ret; } @@ -319,7 +319,7 @@ DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { bool found_route=_solve(begin_point,end_point); if (!found_route) - return DVector<Vector3>(); + return PoolVector<Vector3>(); //midpoints Point *p=end_point; @@ -329,11 +329,11 @@ DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { p=p->prev_point; } - DVector<Vector3> path; + PoolVector<Vector3> path; path.resize(pc); { - DVector<Vector3>::Write w = path.write(); + PoolVector<Vector3>::Write w = path.write(); Point *p=end_point; int idx=pc-1; @@ -351,10 +351,10 @@ DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { } -DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { +PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { - ERR_FAIL_COND_V(!points.has(p_from_id),DVector<int>()); - ERR_FAIL_COND_V(!points.has(p_to_id),DVector<int>()); + ERR_FAIL_COND_V(!points.has(p_from_id),PoolVector<int>()); + ERR_FAIL_COND_V(!points.has(p_to_id),PoolVector<int>()); pass++; @@ -363,7 +363,7 @@ DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { Point* b = points[p_to_id]; if (a==b) { - DVector<int> ret; + PoolVector<int> ret; ret.push_back(a->id); return ret; } @@ -375,7 +375,7 @@ DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { bool found_route=_solve(begin_point,end_point); if (!found_route) - return DVector<int>(); + return PoolVector<int>(); //midpoints Point *p=end_point; @@ -385,11 +385,11 @@ DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { p=p->prev_point; } - DVector<int> path; + PoolVector<int> path; path.resize(pc); { - DVector<int>::Write w = path.write(); + PoolVector<int>::Write w = path.write(); p=end_point; int idx=pc-1; diff --git a/core/math/a_star.h b/core/math/a_star.h index 66a983a6ce..35e6ead226 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -113,8 +113,8 @@ public: int get_closest_point(const Vector3& p_point) const; Vector3 get_closest_pos_in_segment(const Vector3& p_point) const; - DVector<Vector3> get_point_path(int p_from_id, int p_to_id); - DVector<int> get_id_path(int p_from_id, int p_to_id); + PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id); + PoolVector<int> get_id_path(int p_from_id, int p_to_id); AStar(); ~AStar(); diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp index 5242abfa3b..b7194d7ffb 100644 --- a/core/math/bsp_tree.cpp +++ b/core/math/bsp_tree.cpp @@ -484,7 +484,7 @@ BSP_Tree::operator Variant() const { d["planes"]=plane_values; - DVector<int> dst_nodes; + PoolVector<int> dst_nodes; dst_nodes.resize(nodes.size()*3); for(int i=0;i<nodes.size();i++) { @@ -514,19 +514,19 @@ BSP_Tree::BSP_Tree(const Variant& p_variant) { ERR_FAIL_COND(!d.has("aabb")); ERR_FAIL_COND(!d.has("error_radius")); - DVector<int> src_nodes = d["nodes"]; + PoolVector<int> src_nodes = d["nodes"]; ERR_FAIL_COND(src_nodes.size()%3); if (d["planes"].get_type()==Variant::REAL_ARRAY) { - DVector<float> src_planes=d["planes"]; + PoolVector<float> src_planes=d["planes"]; int plane_count=src_planes.size(); ERR_FAIL_COND(plane_count%4); planes.resize(plane_count/4); if (plane_count) { - DVector<float>::Read r = src_planes.read(); + PoolVector<float>::Read r = src_planes.read(); for(int i=0;i<plane_count/4;i++) { planes[i].normal.x=r[i*4+0]; @@ -549,7 +549,7 @@ BSP_Tree::BSP_Tree(const Variant& p_variant) { // int node_count = src_nodes.size(); nodes.resize(src_nodes.size()/3); - DVector<int>::Read r = src_nodes.read(); + PoolVector<int>::Read r = src_nodes.read(); for(int i=0;i<nodes.size();i++) { @@ -560,12 +560,12 @@ BSP_Tree::BSP_Tree(const Variant& p_variant) { } -BSP_Tree::BSP_Tree(const DVector<Face3>& p_faces,float p_error_radius) { +BSP_Tree::BSP_Tree(const PoolVector<Face3>& p_faces,float p_error_radius) { // compute aabb int face_count=p_faces.size(); - DVector<Face3>::Read faces_r=p_faces.read(); + PoolVector<Face3>::Read faces_r=p_faces.read(); const Face3 *facesptr = faces_r.ptr(); diff --git a/core/math/bsp_tree.h b/core/math/bsp_tree.h index 3913e3d34a..236b6e5ac2 100644 --- a/core/math/bsp_tree.h +++ b/core/math/bsp_tree.h @@ -91,7 +91,7 @@ public: BSP_Tree(); BSP_Tree(const Variant& p_variant); - BSP_Tree(const DVector<Face3>& p_faces,float p_error_radius=0); + BSP_Tree(const PoolVector<Face3>& p_faces,float p_error_radius=0); BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB& p_aabb,float p_error_radius=0); ~BSP_Tree(); diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index 91f7cf179a..3232d36262 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -204,21 +204,21 @@ static bool _group_face(_FaceClassify *p_faces, int len, int p_index,int p_group } -DVector< DVector< Face3 > > Geometry::separate_objects( DVector< Face3 > p_array ) { +PoolVector< PoolVector< Face3 > > Geometry::separate_objects( PoolVector< Face3 > p_array ) { - DVector< DVector< Face3 > > objects; + PoolVector< PoolVector< Face3 > > objects; int len = p_array.size(); - DVector<Face3>::Read r=p_array.read(); + PoolVector<Face3>::Read r=p_array.read(); const Face3* arrayptr = r.ptr(); - DVector< _FaceClassify> fc; + PoolVector< _FaceClassify> fc; fc.resize( len ); - DVector< _FaceClassify >::Write fcw=fc.write(); + PoolVector< _FaceClassify >::Write fcw=fc.write(); _FaceClassify * _fcptr = fcw.ptr(); @@ -231,7 +231,7 @@ DVector< DVector< Face3 > > Geometry::separate_objects( DVector< Face3 > p_array if (error) { - ERR_FAIL_COND_V(error, DVector< DVector< Face3 > >() ); // invalid geometry + ERR_FAIL_COND_V(error, PoolVector< PoolVector< Face3 > >() ); // invalid geometry } /* group connected faces in separate objects */ @@ -257,8 +257,8 @@ DVector< DVector< Face3 > > Geometry::separate_objects( DVector< Face3 > p_array if (group>=0) { objects.resize(group); - DVector< DVector<Face3> >::Write obw=objects.write(); - DVector< Face3 > *group_faces = obw.ptr(); + PoolVector< PoolVector<Face3> >::Write obw=objects.write(); + PoolVector< Face3 > *group_faces = obw.ptr(); for (int i=0;i<len;i++) { if (!_fcptr[i].valid) @@ -487,7 +487,7 @@ static inline void _mark_outside(uint8_t*** p_cell_status,int x,int y,int z,int } } -static inline void _build_faces(uint8_t*** p_cell_status,int x,int y,int z,int len_x,int len_y,int len_z,DVector<Face3>& p_faces) { +static inline void _build_faces(uint8_t*** p_cell_status,int x,int y,int z,int len_x,int len_y,int len_z,PoolVector<Face3>& p_faces) { ERR_FAIL_INDEX(x,len_x); ERR_FAIL_INDEX(y,len_y); @@ -580,13 +580,13 @@ static inline void _build_faces(uint8_t*** p_cell_status,int x,int y,int z,int l } -DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_error ) { +PoolVector< Face3 > Geometry::wrap_geometry( PoolVector< Face3 > p_array,float *p_error ) { #define _MIN_SIZE 1.0 #define _MAX_LENGTH 20 int face_count=p_array.size(); - DVector<Face3>::Read facesr=p_array.read(); + PoolVector<Face3>::Read facesr=p_array.read(); const Face3 *faces = facesr.ptr(); AABB global_aabb; @@ -696,7 +696,7 @@ DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_erro //print_line("Wrapper (3/6): Building Faces"); - DVector<Face3> wrapped_faces; + PoolVector<Face3> wrapped_faces; for (int i=0;i<div_x;i++) { @@ -714,7 +714,7 @@ DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_erro // transform face vertices to global coords int wrapped_faces_count=wrapped_faces.size(); - DVector<Face3>::Write wrapped_facesw=wrapped_faces.write(); + PoolVector<Face3>::Write wrapped_facesw=wrapped_faces.write(); Face3* wrapped_faces_ptr=wrapped_facesw.ptr(); for(int i=0;i<wrapped_faces_count;i++) { @@ -748,7 +748,7 @@ DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_erro return wrapped_faces; } -Geometry::MeshData Geometry::build_convex_mesh(const DVector<Plane> &p_planes) { +Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes) { MeshData mesh; @@ -896,9 +896,9 @@ Geometry::MeshData Geometry::build_convex_mesh(const DVector<Plane> &p_planes) { } -DVector<Plane> Geometry::build_box_planes(const Vector3& p_extents) { +PoolVector<Plane> Geometry::build_box_planes(const Vector3& p_extents) { - DVector<Plane> planes; + PoolVector<Plane> planes; planes.push_back( Plane( Vector3(1,0,0), p_extents.x ) ); planes.push_back( Plane( Vector3(-1,0,0), p_extents.x ) ); @@ -910,9 +910,9 @@ DVector<Plane> Geometry::build_box_planes(const Vector3& p_extents) { return planes; } -DVector<Plane> Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { +PoolVector<Plane> Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { - DVector<Plane> planes; + PoolVector<Plane> planes; for (int i=0;i<p_sides;i++) { @@ -933,10 +933,10 @@ DVector<Plane> Geometry::build_cylinder_planes(float p_radius, float p_height, i } -DVector<Plane> Geometry::build_sphere_planes(float p_radius, int p_lats,int p_lons, Vector3::Axis p_axis) { +PoolVector<Plane> Geometry::build_sphere_planes(float p_radius, int p_lats,int p_lons, Vector3::Axis p_axis) { - DVector<Plane> planes; + PoolVector<Plane> planes; Vector3 axis; axis[p_axis]=1.0; @@ -969,9 +969,9 @@ DVector<Plane> Geometry::build_sphere_planes(float p_radius, int p_lats,int p_lo } -DVector<Plane> Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { +PoolVector<Plane> Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { - DVector<Plane> planes; + PoolVector<Plane> planes; Vector3 axis; axis[p_axis]=1.0; diff --git a/core/math/geometry.h b/core/math/geometry.h index dae556cd09..9800e5513c 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -808,9 +808,9 @@ public: } - static DVector< DVector< Face3 > > separate_objects( DVector< Face3 > p_array ); + static PoolVector< PoolVector< Face3 > > separate_objects( PoolVector< Face3 > p_array ); - static DVector< Face3 > wrap_geometry( DVector< Face3 > p_array, float *p_error=NULL ); ///< create a "wrap" that encloses the given geometry + static PoolVector< Face3 > wrap_geometry( PoolVector< Face3 > p_array, float *p_error=NULL ); ///< create a "wrap" that encloses the given geometry struct MeshData { @@ -919,11 +919,11 @@ public: return H; } - static MeshData build_convex_mesh(const DVector<Plane> &p_planes); - static DVector<Plane> build_sphere_planes(float p_radius, int p_lats, int p_lons, Vector3::Axis p_axis=Vector3::AXIS_Z); - static DVector<Plane> build_box_planes(const Vector3& p_extents); - static DVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z); - static DVector<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis=Vector3::AXIS_Z); + static MeshData build_convex_mesh(const PoolVector<Plane> &p_planes); + static PoolVector<Plane> build_sphere_planes(float p_radius, int p_lats, int p_lons, Vector3::Axis p_axis=Vector3::AXIS_Z); + static PoolVector<Plane> build_box_planes(const Vector3& p_extents); + static PoolVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z); + static PoolVector<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis=Vector3::AXIS_Z); static void make_atlas(const Vector<Size2i>& p_rects,Vector<Point2i>& r_result, Size2i& r_size); diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index b3daee0c7e..101e164eae 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -94,7 +94,7 @@ int TriangleMesh::_create_bvh(BVH*p_bvh,BVH** p_bb,int p_from,int p_size,int p_d } -void TriangleMesh::create(const DVector<Vector3>& p_faces) { +void TriangleMesh::create(const PoolVector<Vector3>& p_faces) { valid=false; @@ -104,7 +104,7 @@ void TriangleMesh::create(const DVector<Vector3>& p_faces) { triangles.resize(fc); bvh.resize(fc*3); //will never be larger than this (todo make better) - DVector<BVH>::Write bw = bvh.write(); + PoolVector<BVH>::Write bw = bvh.write(); { @@ -112,8 +112,8 @@ void TriangleMesh::create(const DVector<Vector3>& p_faces) { //except for the Set for repeated triangles, everything //goes in-place. - DVector<Vector3>::Read r = p_faces.read(); - DVector<Triangle>::Write w = triangles.write(); + PoolVector<Vector3>::Read r = p_faces.read(); + PoolVector<Triangle>::Write w = triangles.write(); Map<Vector3,int> db; for(int i=0;i<fc;i++) { @@ -149,16 +149,16 @@ void TriangleMesh::create(const DVector<Vector3>& p_faces) { } vertices.resize(db.size()); - DVector<Vector3>::Write vw = vertices.write(); + PoolVector<Vector3>::Write vw = vertices.write(); for (Map<Vector3,int>::Element *E=db.front();E;E=E->next()) { vw[E->get()]=E->key(); } } - DVector<BVH*> bwptrs; + PoolVector<BVH*> bwptrs; bwptrs.resize(fc); - DVector<BVH*>::Write bwp = bwptrs.write(); + PoolVector<BVH*>::Write bwp = bwptrs.write(); for(int i=0;i<fc;i++) { bwp[i]=&bw[i]; @@ -168,7 +168,7 @@ void TriangleMesh::create(const DVector<Vector3>& p_faces) { int max_alloc=fc; int max=_create_bvh(bw.ptr(),bwp.ptr(),0,fc,1,max_depth,max_alloc); - bw=DVector<BVH>::Write(); //clearup + bw=PoolVector<BVH>::Write(); //clearup bvh.resize(max_alloc); //resize back valid=true; @@ -197,9 +197,9 @@ Vector3 TriangleMesh::get_area_normal(const AABB& p_aabb) const { int level=0; - DVector<Triangle>::Read trianglesr = triangles.read(); - DVector<Vector3>::Read verticesr=vertices.read(); - DVector<BVH>::Read bvhr=bvh.read(); + PoolVector<Triangle>::Read trianglesr = triangles.read(); + PoolVector<Vector3>::Read verticesr=vertices.read(); + PoolVector<BVH>::Read bvhr=bvh.read(); const Triangle *triangleptr=trianglesr.ptr(); int pos=bvh.size()-1; @@ -299,9 +299,9 @@ bool TriangleMesh::intersect_segment(const Vector3& p_begin,const Vector3& p_end int level=0; - DVector<Triangle>::Read trianglesr = triangles.read(); - DVector<Vector3>::Read verticesr=vertices.read(); - DVector<BVH>::Read bvhr=bvh.read(); + PoolVector<Triangle>::Read trianglesr = triangles.read(); + PoolVector<Vector3>::Read verticesr=vertices.read(); + PoolVector<BVH>::Read bvhr=bvh.read(); const Triangle *triangleptr=trianglesr.ptr(); const Vector3 *vertexptr=verticesr.ptr(); @@ -422,9 +422,9 @@ bool TriangleMesh::intersect_ray(const Vector3& p_begin,const Vector3& p_dir,Vec int level=0; - DVector<Triangle>::Read trianglesr = triangles.read(); - DVector<Vector3>::Read verticesr=vertices.read(); - DVector<BVH>::Read bvhr=bvh.read(); + PoolVector<Triangle>::Read trianglesr = triangles.read(); + PoolVector<Vector3>::Read verticesr=vertices.read(); + PoolVector<BVH>::Read bvhr=bvh.read(); const Triangle *triangleptr=trianglesr.ptr(); const Vector3 *vertexptr=verticesr.ptr(); @@ -524,18 +524,18 @@ bool TriangleMesh::is_valid() const { return valid; } -DVector<Face3> TriangleMesh::get_faces() const { +PoolVector<Face3> TriangleMesh::get_faces() const { if (!valid) - return DVector<Face3>(); + return PoolVector<Face3>(); - DVector<Face3> faces; + PoolVector<Face3> faces; int ts = triangles.size(); faces.resize(triangles.size()); - DVector<Face3>::Write w=faces.write(); - DVector<Triangle>::Read r = triangles.read(); - DVector<Vector3>::Read rv = vertices.read(); + PoolVector<Face3>::Write w=faces.write(); + PoolVector<Triangle>::Read r = triangles.read(); + PoolVector<Vector3>::Read rv = vertices.read(); for(int i=0;i<ts;i++) { for(int j=0;j<3;j++) { @@ -543,7 +543,7 @@ DVector<Face3> TriangleMesh::get_faces() const { } } - w = DVector<Face3>::Write(); + w = PoolVector<Face3>::Write(); return faces; } diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index d42f9c8dde..37d32bd7ec 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -41,8 +41,8 @@ class TriangleMesh : public Reference { int indices[3]; }; - DVector<Triangle> triangles; - DVector<Vector3> vertices; + PoolVector<Triangle> triangles; + PoolVector<Vector3> vertices; struct BVH { @@ -79,7 +79,7 @@ class TriangleMesh : public Reference { int _create_bvh(BVH*p_bvh,BVH** p_bb,int p_from,int p_size,int p_depth,int&max_depth,int&max_alloc); - DVector<BVH> bvh; + PoolVector<BVH> bvh; int max_depth; bool valid; @@ -89,10 +89,10 @@ public: bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_point, Vector3 &r_normal) const; bool intersect_ray(const Vector3& p_begin,const Vector3& p_dir,Vector3 &r_point, Vector3 &r_normal) const; Vector3 get_area_normal(const AABB& p_aabb) const; - DVector<Face3> get_faces() const; + PoolVector<Face3> get_faces() const; - void create(const DVector<Vector3>& p_faces); + void create(const PoolVector<Vector3>& p_faces); TriangleMesh(); }; diff --git a/core/message_queue.cpp b/core/message_queue.cpp index 11fe2337f6..fe46e1671c 100644 --- a/core/message_queue.cpp +++ b/core/message_queue.cpp @@ -398,7 +398,7 @@ MessageQueue::MessageQueue() { buffer_end=0; buffer_max_used=0; - buffer_size=GLOBAL_DEF( "core/message_queue_size_kb", DEFAULT_QUEUE_SIZE_KB ); + buffer_size=GLOBAL_DEF( "memory/buffers/message_queue_max_size_kb", DEFAULT_QUEUE_SIZE_KB ); buffer_size*=1024; buffer = memnew_arr( uint8_t, buffer_size ); } diff --git a/core/method_ptrcall.h b/core/method_ptrcall.h index e38d59fd8f..2e3959c3ab 100644 --- a/core/method_ptrcall.h +++ b/core/method_ptrcall.h @@ -133,12 +133,12 @@ struct PtrToArg< const T* > { template<>\ struct PtrToArg<Vector<m_type> > {\ _FORCE_INLINE_ static Vector<m_type> convert(const void* p_ptr) {\ - const DVector<m_type> *dvs = reinterpret_cast<const DVector<m_type> *>(p_ptr);\ + const PoolVector<m_type> *dvs = reinterpret_cast<const PoolVector<m_type> *>(p_ptr);\ Vector<m_type> ret;\ int len = dvs->size();\ ret.resize(len);\ {\ - DVector<m_type>::Read r=dvs->read();\ + PoolVector<m_type>::Read r=dvs->read();\ for(int i=0;i<len;i++) {\ ret[i]=r[i];\ }\ @@ -146,11 +146,11 @@ struct PtrToArg<Vector<m_type> > {\ return ret;\ }\ _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void* p_ptr) {\ - DVector<m_type> *dv = reinterpret_cast<DVector<m_type> *>(p_ptr);\ + PoolVector<m_type> *dv = reinterpret_cast<PoolVector<m_type> *>(p_ptr);\ int len=p_vec.size();\ dv->resize(len);\ {\ - DVector<m_type>::Write w=dv->write();\ + PoolVector<m_type>::Write w=dv->write();\ for(int i=0;i<len;i++) {\ w[i]=p_vec[i];\ }\ @@ -160,12 +160,12 @@ struct PtrToArg<Vector<m_type> > {\ template<>\ struct PtrToArg<const Vector<m_type>& > {\ _FORCE_INLINE_ static Vector<m_type> convert(const void* p_ptr) {\ - const DVector<m_type> *dvs = reinterpret_cast<const DVector<m_type> *>(p_ptr);\ + const PoolVector<m_type> *dvs = reinterpret_cast<const PoolVector<m_type> *>(p_ptr);\ Vector<m_type> ret;\ int len = dvs->size();\ ret.resize(len);\ {\ - DVector<m_type>::Read r=dvs->read();\ + PoolVector<m_type>::Read r=dvs->read();\ for(int i=0;i<len;i++) {\ ret[i]=r[i];\ }\ @@ -226,26 +226,26 @@ MAKE_VECARR(Plane); #define MAKE_DVECARR(m_type) \ template<>\ -struct PtrToArg<DVector<m_type> > {\ - _FORCE_INLINE_ static DVector<m_type> convert(const void* p_ptr) {\ +struct PtrToArg<PoolVector<m_type> > {\ + _FORCE_INLINE_ static PoolVector<m_type> convert(const void* p_ptr) {\ const Array *arr = reinterpret_cast<const Array *>(p_ptr);\ - DVector<m_type> ret;\ + PoolVector<m_type> ret;\ int len = arr->size();\ ret.resize(len);\ {\ - DVector<m_type>::Write w=ret.write();\ + PoolVector<m_type>::Write w=ret.write();\ for(int i=0;i<len;i++) {\ w[i]=(*arr)[i];\ }\ }\ return ret;\ }\ - _FORCE_INLINE_ static void encode(DVector<m_type> p_vec, void* p_ptr) {\ + _FORCE_INLINE_ static void encode(PoolVector<m_type> p_vec, void* p_ptr) {\ Array *arr = reinterpret_cast<Array *>(p_ptr);\ int len = p_vec.size();\ arr->resize(len);\ {\ - DVector<m_type>::Read r=p_vec.read();\ + PoolVector<m_type>::Read r=p_vec.read();\ for(int i=0;i<len;i++) {\ (*arr)[i]=r[i];\ }\ @@ -253,14 +253,14 @@ struct PtrToArg<DVector<m_type> > {\ } \ };\ template<>\ -struct PtrToArg<const DVector<m_type>& > {\ - _FORCE_INLINE_ static DVector<m_type> convert(const void* p_ptr) {\ +struct PtrToArg<const PoolVector<m_type>& > {\ + _FORCE_INLINE_ static PoolVector<m_type> convert(const void* p_ptr) {\ const Array *arr = reinterpret_cast<const Array *>(p_ptr);\ - DVector<m_type> ret;\ + PoolVector<m_type> ret;\ int len = arr->size();\ ret.resize(len);\ {\ - DVector<m_type>::Write w=ret.write();\ + PoolVector<m_type>::Write w=ret.write();\ for(int i=0;i<len;i++) {\ w[i]=(*arr)[i];\ }\ @@ -297,15 +297,15 @@ MAKE_STRINGCONV(StringName); MAKE_STRINGCONV(IP_Address); template<> -struct PtrToArg<DVector<Face3> > { - _FORCE_INLINE_ static DVector<Face3> convert(const void* p_ptr) { - const DVector<Vector3> *dvs = reinterpret_cast<const DVector<Vector3> *>(p_ptr); - DVector<Face3> ret; +struct PtrToArg<PoolVector<Face3> > { + _FORCE_INLINE_ static PoolVector<Face3> convert(const void* p_ptr) { + const PoolVector<Vector3> *dvs = reinterpret_cast<const PoolVector<Vector3> *>(p_ptr); + PoolVector<Face3> ret; int len = dvs->size()/3; ret.resize(len); { - DVector<Vector3>::Read r=dvs->read(); - DVector<Face3>::Write w=ret.write(); + PoolVector<Vector3>::Read r=dvs->read(); + PoolVector<Face3>::Write w=ret.write(); for(int i=0;i<len;i++) { w[i].vertex[0]=r[i*3+0]; w[i].vertex[1]=r[i*3+1]; @@ -314,13 +314,13 @@ struct PtrToArg<DVector<Face3> > { } return ret; } - _FORCE_INLINE_ static void encode(DVector<Face3> p_vec, void* p_ptr) {\ - DVector<Vector3> *arr = reinterpret_cast<DVector<Vector3> *>(p_ptr);\ + _FORCE_INLINE_ static void encode(PoolVector<Face3> p_vec, void* p_ptr) {\ + PoolVector<Vector3> *arr = reinterpret_cast<PoolVector<Vector3> *>(p_ptr);\ int len = p_vec.size();\ arr->resize(len*3);\ {\ - DVector<Face3>::Read r=p_vec.read();\ - DVector<Vector3>::Write w=arr->write();\ + PoolVector<Face3>::Read r=p_vec.read();\ + PoolVector<Vector3>::Write w=arr->write();\ for(int i=0;i<len;i++) {\ w[i*3+0]=r[i].vertex[0];\ w[i*3+1]=r[i].vertex[1];\ @@ -330,15 +330,15 @@ struct PtrToArg<DVector<Face3> > { } \ }; template<> -struct PtrToArg<const DVector<Face3>& > { - _FORCE_INLINE_ static DVector<Face3> convert(const void* p_ptr) { - const DVector<Vector3> *dvs = reinterpret_cast<const DVector<Vector3> *>(p_ptr); - DVector<Face3> ret; +struct PtrToArg<const PoolVector<Face3>& > { + _FORCE_INLINE_ static PoolVector<Face3> convert(const void* p_ptr) { + const PoolVector<Vector3> *dvs = reinterpret_cast<const PoolVector<Vector3> *>(p_ptr); + PoolVector<Face3> ret; int len = dvs->size()/3; ret.resize(len); { - DVector<Vector3>::Read r=dvs->read(); - DVector<Face3>::Write w=ret.write(); + PoolVector<Vector3>::Read r=dvs->read(); + PoolVector<Face3>::Write w=ret.write(); for(int i=0;i<len;i++) { w[i].vertex[0]=r[i*3+0]; w[i].vertex[1]=r[i*3+1]; diff --git a/core/object.cpp b/core/object.cpp index 82377013e1..3bb917bd38 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -532,8 +532,6 @@ void Object::get_property_list(List<PropertyInfo> *p_list,bool p_reversed) const _get_property_listv(p_list,p_reversed); - if (!_use_builtin_script()) - return; if (!is_class("Script")) // can still be set, but this is for userfriendlyness p_list->push_back( PropertyInfo( Variant::OBJECT, "script/script", PROPERTY_HINT_RESOURCE_TYPE, "Script",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_STORE_IF_NONZERO)); @@ -1112,9 +1110,9 @@ Array Object::_get_method_list_bind() const { } -DVector<String> Object::_get_meta_list_bind() const { +PoolVector<String> Object::_get_meta_list_bind() const { - DVector<String> _metaret; + PoolVector<String> _metaret; List<Variant> keys; metadata.get_key_list(&keys); @@ -1942,27 +1940,37 @@ uint32_t ObjectDB::instance_counter=1; HashMap<Object*,ObjectID,ObjectDB::ObjectPtrHash> ObjectDB::instance_checks; uint32_t ObjectDB::add_instance(Object *p_object) { - GLOBAL_LOCK_FUNCTION; + ERR_FAIL_COND_V( p_object->get_instance_ID()!=0, 0 ); + + rw_lock->write_lock(); instances[++instance_counter]=p_object; #ifdef DEBUG_ENABLED instance_checks[p_object]=instance_counter; #endif + rw_lock->write_unlock(); + return instance_counter; } void ObjectDB::remove_instance(Object *p_object) { - GLOBAL_LOCK_FUNCTION; + + rw_lock->write_lock(); + instances.erase( p_object->get_instance_ID() ); #ifdef DEBUG_ENABLED instance_checks.erase(p_object); #endif + + rw_lock->write_unlock(); } Object *ObjectDB::get_instance(uint32_t p_instance_ID) { - GLOBAL_LOCK_FUNCTION; + rw_lock->read_lock(); Object**obj=instances.getptr(p_instance_ID); + rw_lock->read_unlock(); + if (!obj) return NULL; return *obj; @@ -1970,13 +1978,16 @@ Object *ObjectDB::get_instance(uint32_t p_instance_ID) { void ObjectDB::debug_objects(DebugFunc p_func) { - GLOBAL_LOCK_FUNCTION; + rw_lock->read_lock(); const uint32_t *K=NULL; while((K=instances.next(K))) { p_func(instances[*K]); } + + rw_lock->read_unlock(); + } @@ -1987,15 +1998,26 @@ void Object::get_argument_options(const StringName& p_function,int p_idx,List<St int ObjectDB::get_object_count() { - GLOBAL_LOCK_FUNCTION; - return instances.size(); + rw_lock->read_lock(); + int count =instances.size(); + rw_lock->read_unlock(); + + return count; + +} + +RWLock *ObjectDB::rw_lock=NULL; + +void ObjectDB::setup() { + + rw_lock = RWLock::create(); } void ObjectDB::cleanup() { - GLOBAL_LOCK_FUNCTION; + rw_lock->write_lock(); if (instances.size()) { WARN_PRINT("ObjectDB Instances still exist!"); @@ -2014,4 +2036,7 @@ void ObjectDB::cleanup() { } instances.clear(); instance_checks.clear(); + rw_lock->write_unlock(); + + memdelete(rw_lock); } diff --git a/core/object.h b/core/object.h index 6dd427bf41..a54693eab6 100644 --- a/core/object.h +++ b/core/object.h @@ -34,6 +34,7 @@ #include "set.h" #include "map.h" #include "vmap.h" +#include "os/rw_lock.h" #define VARIANT_ARG_LIST const Variant& p_arg1=Variant(),const Variant& p_arg2=Variant(),const Variant& p_arg3=Variant(),const Variant& p_arg4=Variant(),const Variant& p_arg5=Variant() #define VARIANT_ARG_PASS p_arg1,p_arg2,p_arg3,p_arg4,p_arg5 @@ -57,7 +58,10 @@ enum PropertyHint { PROPERTY_HINT_SPRITE_FRAME, PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer) PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags) - PROPERTY_HINT_ALL_FLAGS, + PROPERTY_HINT_LAYERS_2D_RENDER, + PROPERTY_HINT_LAYERS_2D_PHYSICS, + PROPERTY_HINT_LAYERS_3D_RENDER, + PROPERTY_HINT_LAYERS_3D_PHYSICS, PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc," PROPERTY_HINT_DIR, ///< a directort path must be passed PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc," @@ -431,7 +435,7 @@ friend void postinitialize_handler(Object*); protected: - virtual bool _use_builtin_script() const { return false; } + virtual void _initialize_classv() { initialize_class(); } virtual bool _setv(const StringName& p_name,const Variant &p_property) { return false; }; virtual bool _getv(const StringName& p_name,Variant &r_property) const { return false; }; @@ -481,7 +485,7 @@ protected: return &_class_name; } - DVector<String> _get_meta_list_bind() const; + PoolVector<String> _get_meta_list_bind() const; Array _get_property_list_bind() const; Array _get_method_list_bind() const; @@ -686,9 +690,14 @@ class ObjectDB { friend class Object; friend void unregister_core_types(); + + static RWLock *rw_lock; static void cleanup(); static uint32_t add_instance(Object *p_object); static void remove_instance(Object *p_object); +friend void register_core_types(); + static void setup(); + public: typedef void (*DebugFunc)(Object *p_obj); diff --git a/core/object_type_db.cpp b/core/object_type_db.cpp index babbb6b8ad..7432c09563 100644 --- a/core/object_type_db.cpp +++ b/core/object_type_db.cpp @@ -31,11 +31,12 @@ #ifdef NO_THREADS -#define OBJTYPE_LOCK +#define OBJTYPE_RLOCK #else -#define OBJTYPE_LOCK MutexLock _mutex_lock_(lock); +#define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock); +#define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock); #endif @@ -215,7 +216,7 @@ ClassDB::ClassInfo::~ClassInfo() { bool ClassDB::is_parent_class(const StringName &p_class,const StringName& p_inherits) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; StringName inherits=p_class; @@ -230,7 +231,7 @@ bool ClassDB::is_parent_class(const StringName &p_class,const StringName& p_inhe } void ClassDB::get_class_list( List<StringName> *p_classes) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; const StringName *k=NULL; @@ -245,7 +246,7 @@ void ClassDB::get_class_list( List<StringName> *p_classes) { void ClassDB::get_inheriters_from_class( const StringName& p_class,List<StringName> *p_classes) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; const StringName *k=NULL; @@ -259,7 +260,7 @@ void ClassDB::get_inheriters_from_class( const StringName& p_class,List<StringNa StringName ClassDB::get_parent_class(const StringName& p_class) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); ERR_FAIL_COND_V(!ti,""); @@ -268,7 +269,7 @@ StringName ClassDB::get_parent_class(const StringName& p_class) { ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); ERR_FAIL_COND_V(!ti,API_NONE); @@ -277,6 +278,7 @@ ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) { uint64_t ClassDB::get_api_hash(APIType p_api) { + OBJTYPE_RLOCK; #ifdef DEBUG_METHODS_ENABLED uint64_t hash = hash_djb2_one_64(HashMapHahserDefault::hash(VERSION_FULL_NAME)); @@ -433,12 +435,13 @@ uint64_t ClassDB::get_api_hash(APIType p_api) { bool ClassDB::class_exists(const StringName &p_class) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; return classes.has(p_class); } void ClassDB::add_compatibility_class(const StringName& p_class,const StringName& p_fallback) { + OBJTYPE_WLOCK; compat_classes[p_class]=p_fallback; } @@ -446,7 +449,7 @@ Object *ClassDB::instance(const StringName &p_class) { ClassInfo *ti; { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ti=classes.getptr(p_class); if (!ti || ti->disabled || !ti->creation_func) { if (compat_classes.has(p_class)) { @@ -462,7 +465,7 @@ Object *ClassDB::instance(const StringName &p_class) { } bool ClassDB::can_instance(const StringName &p_class) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); ERR_FAIL_COND_V(!ti,false); @@ -472,7 +475,7 @@ bool ClassDB::can_instance(const StringName &p_class) { void ClassDB::_add_class2(const StringName& p_class, const StringName& p_inherits) { - OBJTYPE_LOCK; + OBJTYPE_WLOCK; StringName name = p_class; @@ -499,7 +502,7 @@ void ClassDB::_add_class2(const StringName& p_class, const StringName& p_inherit void ClassDB::get_method_list(StringName p_class,List<MethodInfo> *p_methods,bool p_no_inheritance) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *type=classes.getptr(p_class); @@ -572,7 +575,7 @@ void ClassDB::get_method_list(StringName p_class,List<MethodInfo> *p_methods,boo MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *type=classes.getptr(p_class); @@ -589,7 +592,7 @@ MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) { void ClassDB::bind_integer_constant(const StringName& p_class, const StringName &p_name, int p_constant) { - OBJTYPE_LOCK; + OBJTYPE_WLOCK; ClassInfo *type=classes.getptr(p_class); if (!type) { @@ -611,7 +614,7 @@ void ClassDB::bind_integer_constant(const StringName& p_class, const StringName void ClassDB::get_integer_constant_list(const StringName& p_class, List<String> *p_constants, bool p_no_inheritance) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *type=classes.getptr(p_class); @@ -639,7 +642,7 @@ void ClassDB::get_integer_constant_list(const StringName& p_class, List<String> int ClassDB::get_integer_constant(const StringName& p_class, const StringName &p_name, bool *p_success) { - OBJTYPE_LOCK; + OBJTYPE_RLOCK; ClassInfo *type=classes.getptr(p_class); @@ -666,6 +669,8 @@ int ClassDB::get_integer_constant(const StringName& p_class, const StringName &p void ClassDB::add_signal(StringName p_class,const MethodInfo& p_signal) { + OBJTYPE_WLOCK; + ClassInfo *type=classes.getptr(p_class); ERR_FAIL_COND(!type); @@ -688,6 +693,8 @@ void ClassDB::add_signal(StringName p_class,const MethodInfo& p_signal) { void ClassDB::get_signal_list(StringName p_class,List<MethodInfo> *p_signals,bool p_no_inheritance) { + OBJTYPE_RLOCK; + ClassInfo *type=classes.getptr(p_class); ERR_FAIL_COND(!type); @@ -712,6 +719,7 @@ void ClassDB::get_signal_list(StringName p_class,List<MethodInfo> *p_signals,boo bool ClassDB::has_signal(StringName p_class,StringName p_signal) { + OBJTYPE_RLOCK; ClassInfo *type=classes.getptr(p_class); ClassInfo *check=type; while(check) { @@ -725,6 +733,7 @@ bool ClassDB::has_signal(StringName p_class,StringName p_signal) { bool ClassDB::get_signal(StringName p_class,StringName p_signal,MethodInfo *r_signal) { + OBJTYPE_RLOCK; ClassInfo *type=classes.getptr(p_class); ClassInfo *check=type; while(check) { @@ -743,6 +752,7 @@ bool ClassDB::get_signal(StringName p_class,StringName p_signal,MethodInfo *r_si void ClassDB::add_property_group(StringName p_class,const String& p_name,const String& p_prefix) { + OBJTYPE_WLOCK; ClassInfo *type=classes.getptr(p_class); ERR_FAIL_COND(!type); @@ -752,7 +762,13 @@ void ClassDB::add_property_group(StringName p_class,const String& p_name,const S void ClassDB::add_property(StringName p_class,const PropertyInfo& p_pinfo, const StringName& p_setter, const StringName& p_getter, int p_index) { + + lock->read_lock(); + ClassInfo *type=classes.getptr(p_class); + + lock->read_unlock(); + ERR_FAIL_COND(!type); MethodBind *mb_set=NULL; @@ -804,6 +820,9 @@ void ClassDB::add_property(StringName p_class,const PropertyInfo& p_pinfo, const ERR_FAIL(); } #endif + + OBJTYPE_WLOCK + type->property_list.push_back(p_pinfo); PropertySetGet psg; @@ -821,6 +840,8 @@ void ClassDB::add_property(StringName p_class,const PropertyInfo& p_pinfo, const void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance,const Object *p_validator) { + OBJTYPE_RLOCK; + ClassInfo *type=classes.getptr(p_class); ClassInfo *check=type; while(check) { @@ -846,6 +867,7 @@ void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool ClassDB::set_property(Object* p_object,const StringName& p_property, const Variant& p_value,bool *r_valid) { + ClassInfo *type=classes.getptr(p_object->get_class_name()); ClassInfo *check=type; while(check) { @@ -991,9 +1013,26 @@ StringName ClassDB::get_property_getter(StringName p_class,const StringName p_pr return StringName(); } +bool ClassDB::has_property(const StringName& p_class, const StringName& p_property, bool p_no_inheritance) { + + + ClassInfo *type=classes.getptr(p_class); + ClassInfo *check=type; + while(check) { + if (check->property_setget.has(p_property)) + return true; + + if (p_no_inheritance) + break; + check=check->inherits_ptr; + } + + return false; +} void ClassDB::set_method_flags(StringName p_class,StringName p_method,int p_flags) { + OBJTYPE_WLOCK; ClassInfo *type=classes.getptr(p_class); ClassInfo *check=type; ERR_FAIL_COND(!check); @@ -1054,7 +1093,7 @@ MethodBind* ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind , const } - OBJTYPE_LOCK; + OBJTYPE_WLOCK; ERR_FAIL_COND_V(!p_bind,NULL); p_bind->set_name(mdname); @@ -1098,6 +1137,8 @@ MethodBind* ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind , const void ClassDB::add_virtual_method(const StringName& p_class, const MethodInfo& p_method , bool p_virtual) { ERR_FAIL_COND(!classes.has(p_class)); + OBJTYPE_WLOCK; + #ifdef DEBUG_METHODS_ENABLED MethodInfo mi=p_method; if (p_virtual) @@ -1133,12 +1174,16 @@ void ClassDB::get_virtual_methods(const StringName& p_class, List<MethodInfo> * void ClassDB::set_class_enabled(StringName p_class,bool p_enable) { + OBJTYPE_WLOCK; + ERR_FAIL_COND(!classes.has(p_class)); classes[p_class].disabled=!p_enable; } bool ClassDB::is_class_enabled(StringName p_class) { + OBJTYPE_RLOCK; + ClassInfo *ti=classes.getptr(p_class); if (!ti || !ti->creation_func) { if (compat_classes.has(p_class)) { @@ -1190,23 +1235,19 @@ void ClassDB::get_extensions_for_type(const StringName& p_class,List<String> *p_ } -Mutex *ClassDB::lock=NULL; +RWLock *ClassDB::lock=NULL; void ClassDB::init() { #ifndef NO_THREADS - lock = Mutex::create(); + lock = RWLock::create(); #endif } void ClassDB::cleanup() { -#ifndef NO_THREADS - - memdelete(lock); -#endif //OBJTYPE_LOCK; hah not here @@ -1225,6 +1266,12 @@ void ClassDB::cleanup() { classes.clear(); resource_base_extensions.clear(); compat_classes.clear(); + +#ifndef NO_THREADS + + memdelete(lock); +#endif + } // diff --git a/core/object_type_db.h b/core/object_type_db.h index aebe767028..158a4dae23 100644 --- a/core/object_type_db.h +++ b/core/object_type_db.h @@ -156,7 +156,7 @@ public: return memnew( T ); } - static Mutex *lock; + static RWLock *lock; static HashMap<StringName,ClassInfo,StringNameHasher> classes; static HashMap<StringName,StringName,StringNameHasher> resource_base_extensions; static HashMap<StringName,StringName,StringNameHasher> compat_classes; @@ -481,6 +481,7 @@ public: static void get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance=false, const Object *p_validator=NULL); static bool set_property(Object* p_object, const StringName& p_property, const Variant& p_value, bool *r_valid=NULL); static bool get_property(Object* p_object,const StringName& p_property, Variant& r_value); + static bool has_property(const StringName& p_class,const StringName& p_property,bool p_no_inheritance=false); static Variant::Type get_property_type(const StringName& p_class, const StringName& p_property,bool *r_is_valid=NULL); static StringName get_property_setter(StringName p_class,const StringName p_property); static StringName get_property_getter(StringName p_class,const StringName p_property); diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index 7b60baeeb7..804fe15c39 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -37,7 +37,7 @@ String DirAccess::_get_root_path() const { switch(_access_type) { - case ACCESS_RESOURCES: return Globals::get_singleton()->get_resource_path(); + case ACCESS_RESOURCES: return GlobalConfig::get_singleton()->get_resource_path(); case ACCESS_USERDATA: return OS::get_singleton()->get_data_dir(); default: return ""; } @@ -204,10 +204,10 @@ String DirAccess::fix_path(String p_path) const { case ACCESS_RESOURCES: { - if (Globals::get_singleton()) { + if (GlobalConfig::get_singleton()) { if (p_path.begins_with("res://")) { - String resource_path = Globals::get_singleton()->get_resource_path(); + String resource_path = GlobalConfig::get_singleton()->get_resource_path(); if (resource_path != "") { return p_path.replace_first("res:/",resource_path); diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 388c598ac3..06723c5131 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -139,10 +139,10 @@ String FileAccess::fix_path(const String& p_path) const { case ACCESS_RESOURCES: { - if (Globals::get_singleton()) { + if (GlobalConfig::get_singleton()) { if (r_path.begins_with("res://")) { - String resource_path = Globals::get_singleton()->get_resource_path(); + String resource_path = GlobalConfig::get_singleton()->get_resource_path(); if (resource_path != "") { return r_path.replace("res:/",resource_path); diff --git a/core/os/input.cpp b/core/os/input.cpp index c662fafe79..d2bd433ed9 100644 --- a/core/os/input.cpp +++ b/core/os/input.cpp @@ -61,7 +61,7 @@ void Input::_bind_methods() { ClassDB::bind_method(_MD("get_joy_axis","device","axis"),&Input::get_joy_axis); ClassDB::bind_method(_MD("get_joy_name","device"),&Input::get_joy_name); ClassDB::bind_method(_MD("get_joy_guid","device"),&Input::get_joy_guid); - ClassDB::bind_method(_MD("get_connected_joysticks"),&Input::get_connected_joysticks); + ClassDB::bind_method(_MD("get_connected_joypads"),&Input::get_connected_joypads); ClassDB::bind_method(_MD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength); ClassDB::bind_method(_MD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration); ClassDB::bind_method(_MD("get_joy_button_string", "button_index"), &Input::get_joy_button_string); @@ -97,7 +97,7 @@ void Input::get_argument_options(const StringName& p_function,int p_idx,List<Str if (p_idx==0 && (pf=="is_action_pressed" || pf=="action_press" || pf=="action_release" || pf=="is_action_just_pressed" || pf=="is_action_just_released")) { List<PropertyInfo> pinfo; - Globals::get_singleton()->get_property_list(&pinfo); + GlobalConfig::get_singleton()->get_property_list(&pinfo); for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) { const PropertyInfo &pi=E->get(); diff --git a/core/os/input.h b/core/os/input.h index a137820972..c365894f46 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -64,7 +64,7 @@ public: virtual float get_joy_axis(int p_device,int p_axis) const=0; virtual String get_joy_name(int p_idx)=0; - virtual Array get_connected_joysticks()=0; + virtual Array get_connected_joypads()=0; virtual void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid)=0; virtual void add_joy_mapping(String p_mapping, bool p_update_existing=false)=0; virtual void remove_joy_mapping(String p_guid)=0; diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index b0008a0481..5c681f1a82 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -61,10 +61,10 @@ bool InputEvent::operator==(const InputEvent &p_event) const { && mouse_button.button_index == p_event.mouse_button.button_index && mouse_button.button_mask == p_event.mouse_button.button_mask && key.mod == p_event.key.mod; - case JOYSTICK_MOTION: + case JOYPAD_MOTION: return joy_motion.axis == p_event.joy_motion.axis && joy_motion.axis_value == p_event.joy_motion.axis_value; - case JOYSTICK_BUTTON: + case JOYPAD_BUTTON: return joy_button.pressed == p_event.joy_button.pressed && joy_button.button_index == p_event.joy_button.button_index && joy_button.pressure == p_event.joy_button.pressure; @@ -155,14 +155,14 @@ InputEvent::operator String() const { return str; } break; - case JOYSTICK_MOTION: { - str+= "Event: JoystickMotion "; + case JOYPAD_MOTION: { + str+= "Event: JoypadMotion "; str=str+"Axis: "+itos(joy_motion.axis)+" Value: " +rtos(joy_motion.axis_value); return str; } break; - case JOYSTICK_BUTTON: { - str+= "Event: JoystickButton "; + case JOYPAD_BUTTON: { + str+= "Event: JoypadButton "; str=str+"Pressed: "+itos(joy_button.pressed)+" Index: " +itos(joy_button.button_index)+" pressure "+rtos(joy_button.pressure); return str; @@ -203,9 +203,9 @@ bool InputEvent::is_pressed() const { case KEY: return key.pressed; case MOUSE_BUTTON: return mouse_button.pressed; - case JOYSTICK_BUTTON: return joy_button.pressed; + case JOYPAD_BUTTON: return joy_button.pressed; case SCREEN_TOUCH: return screen_touch.pressed; - case JOYSTICK_MOTION: return ABS(joy_motion.axis_value) > 0.5; + case JOYPAD_MOTION: return ABS(joy_motion.axis_value) > 0.5; case ACTION: return action.pressed; default: {} } diff --git a/core/os/input_event.h b/core/os/input_event.h index 8fe033aa3c..a557de2bd7 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -225,13 +225,13 @@ struct InputEventMouseMotion : public InputEventMouse { float speed_x,speed_y; }; -struct InputEventJoystickMotion { +struct InputEventJoypadMotion { - int axis; ///< Joystick axis + int axis; ///< Joypad axis float axis_value; ///< -1 to 1 }; -struct InputEventJoystickButton { +struct InputEventJoypadButton { int button_index; bool pressed; @@ -267,8 +267,8 @@ struct InputEvent { KEY, MOUSE_MOTION, MOUSE_BUTTON, - JOYSTICK_MOTION, - JOYSTICK_BUTTON, + JOYPAD_MOTION, + JOYPAD_BUTTON, SCREEN_TOUCH, SCREEN_DRAG, ACTION, @@ -282,8 +282,8 @@ struct InputEvent { union { InputEventMouseMotion mouse_motion; InputEventMouseButton mouse_button; - InputEventJoystickMotion joy_motion; - InputEventJoystickButton joy_button; + InputEventJoypadMotion joy_motion; + InputEventJoypadButton joy_button; InputEventKey key; InputEventScreenTouch screen_touch; InputEventScreenDrag screen_drag; diff --git a/core/os/main_loop.h b/core/os/main_loop.h index 6506a280a3..5251061423 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -54,6 +54,7 @@ public: NOTIFICATION_WM_QUIT_REQUEST = 7, NOTIFICATION_WM_UNFOCUS_REQUEST = 8, NOTIFICATION_OS_MEMORY_WARNING = 9, + NOTIFICATION_TRANSLATION_CHANGED = 10, }; virtual void input_event( const InputEvent& p_event ); diff --git a/core/os/memory.cpp b/core/os/memory.cpp index 4922a18335..37a523b763 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -30,9 +30,13 @@ #include "error_macros.h" #include "copymem.h" #include <stdio.h> +#include <stdlib.h> + + + void * operator new(size_t p_size,const char *p_description) { - return Memory::alloc_static( p_size, p_description ); + return Memory::alloc_static( p_size, false ); } void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)) { @@ -42,74 +46,144 @@ void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)) { #include <stdio.h> -void * Memory::alloc_static(size_t p_bytes,const char *p_alloc_from) { +#ifdef DEBUG_ENABLED +size_t Memory::mem_usage=0; +size_t Memory::max_usage=0; +#endif - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), NULL ); - return MemoryPoolStatic::get_singleton()->alloc(p_bytes,p_alloc_from); -} -void * Memory::realloc_static(void *p_memory,size_t p_bytes) { +size_t Memory::alloc_count=0; - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), NULL ); - return MemoryPoolStatic::get_singleton()->realloc(p_memory,p_bytes); -} -void Memory::free_static(void *p_ptr) { +void * Memory::alloc_static(size_t p_bytes,bool p_pad_align) { - ERR_FAIL_COND( !MemoryPoolStatic::get_singleton()); - MemoryPoolStatic::get_singleton()->free(p_ptr); -} +#ifdef DEBUG_ENABLED + bool prepad=true; +#else + bool prepad=p_pad_align; +#endif -size_t Memory::get_static_mem_available() { + void * mem = malloc( p_bytes + (prepad?PAD_ALIGN:0)); - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0); - return MemoryPoolStatic::get_singleton()->get_available_mem(); + alloc_count++; -} + ERR_FAIL_COND_V(!mem,NULL); -size_t Memory::get_static_mem_max_usage() { + if (prepad) { + uint64_t *s = (uint64_t*)mem; + *s=p_bytes; - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0); - return MemoryPoolStatic::get_singleton()->get_max_usage(); + uint8_t *s8 = (uint8_t*)mem; + +#ifdef DEBUG_ENABLED + mem_usage+=p_bytes; + if (mem_usage>max_usage) { + max_usage=mem_usage; + } +#endif + return s8 + PAD_ALIGN; + } else { + return mem; + } } -size_t Memory::get_static_mem_usage() { +void * Memory::realloc_static(void *p_memory,size_t p_bytes,bool p_pad_align) { - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0); - return MemoryPoolStatic::get_singleton()->get_total_usage(); + if (p_memory==NULL) { + return alloc_static(p_bytes,p_pad_align); + } -} + uint8_t *mem = (uint8_t*)p_memory; -void Memory::dump_static_mem_to_file(const char* p_file) { +#ifdef DEBUG_ENABLED + bool prepad=true; +#else + bool prepad=p_pad_align; +#endif - MemoryPoolStatic::get_singleton()->dump_mem_to_file(p_file); -} + if (prepad) { + mem-=PAD_ALIGN; + uint64_t *s = (uint64_t*)mem; -MID Memory::alloc_dynamic(size_t p_bytes, const char *p_descr) { +#ifdef DEBUG_ENABLED + mem_usage-=*s; + mem_usage+=p_bytes; +#endif - MemoryPoolDynamic::ID id = MemoryPoolDynamic::get_singleton()->alloc(p_bytes,p_descr); + if (p_bytes==0) { + free(mem); + return NULL; + } else { + *s=p_bytes; - return MID(id); -} -Error Memory::realloc_dynamic(MID p_mid,size_t p_bytes) { + mem = (uint8_t*)realloc(mem,p_bytes+PAD_ALIGN); + ERR_FAIL_COND_V(!mem,NULL); + + s = (uint64_t*)mem; + + *s=p_bytes; - MemoryPoolDynamic::ID id = p_mid.data?p_mid.data->id:MemoryPoolDynamic::INVALID_ID; + return mem+PAD_ALIGN; + } + } else { - if (id==MemoryPoolDynamic::INVALID_ID) - return ERR_INVALID_PARAMETER; + mem = (uint8_t*)realloc(mem,p_bytes); - return MemoryPoolDynamic::get_singleton()->realloc(p_mid, p_bytes); + ERR_FAIL_COND_V(mem==NULL && p_bytes>0,NULL); + return mem; + } } -size_t Memory::get_dynamic_mem_available() { +void Memory::free_static(void *p_ptr,bool p_pad_align) { + + ERR_FAIL_COND(p_ptr==NULL); + + uint8_t *mem = (uint8_t*)p_ptr; + +#ifdef DEBUG_ENABLED + bool prepad=true; +#else + bool prepad=p_pad_align; +#endif + + alloc_count--; + + if (prepad) { + mem-=PAD_ALIGN; + uint64_t *s = (uint64_t*)mem; + +#ifdef DEBUG_ENABLED + mem_usage-=*s; +#endif + + free(mem); + } else { + + free(mem); + } - return MemoryPoolDynamic::get_singleton()->get_available_mem(); } -size_t Memory::get_dynamic_mem_usage() { +size_t Memory::get_mem_available() { + + return 0xFFFFFFFFFFFFF; - return MemoryPoolDynamic::get_singleton()->get_total_usage(); +} + +size_t Memory::get_mem_usage(){ +#ifdef DEBUG_ENABLED + return mem_usage; +#else + return 0; +#endif +} +size_t Memory::get_mem_max_usage(){ +#ifdef DEBUG_ENABLED + return max_usage; +#else + return 0; +#endif } diff --git a/core/os/memory.h b/core/os/memory.h index b3b806f7c6..0e6dea48d3 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -31,175 +31,45 @@ #include <stddef.h> #include "safe_refcount.h" -#include "os/memory_pool_dynamic.h" -#include "os/memory_pool_static.h" /** @author Juan Linietsky <reduzio@gmail.com> */ -class MID { - - struct Data { - - SafeRefCount refcount; - MemoryPoolDynamic::ID id; - }; - - mutable Data *data; - - void unref() { - - if (!data) - return; - if (data->refcount.unref()) { - - if (data->id!=MemoryPoolDynamic::INVALID_ID) - MemoryPoolDynamic::get_singleton()->free(data->id); - MemoryPoolStatic::get_singleton()->free(data); - } - - data=NULL; - } - - void ref(Data *p_data) { - - if (data==p_data) - return; - unref(); - - if (p_data && p_data->refcount.ref()) - data=p_data; - } - -friend class MID_Lock; - - inline void lock() { - - if (data && data->id!=MemoryPoolDynamic::INVALID_ID) - MemoryPoolDynamic::get_singleton()->lock(data->id); - } - inline void unlock() { - - if (data && data->id!=MemoryPoolDynamic::INVALID_ID) - MemoryPoolDynamic::get_singleton()->unlock(data->id); - - } - - inline void * get() { - - if (data && data->id!=MemoryPoolDynamic::INVALID_ID) - return MemoryPoolDynamic::get_singleton()->get(data->id); - - return NULL; - } - - Error _resize(size_t p_size) { - - if (p_size==0 && (!data || data->id==MemoryPoolDynamic::INVALID_ID)) - return OK; - if (p_size && !data) { - // create data because we'll need it - data = (Data*)MemoryPoolStatic::get_singleton()->alloc(sizeof(Data),"MID::Data"); - ERR_FAIL_COND_V( !data,ERR_OUT_OF_MEMORY ); - data->refcount.init(); - data->id=MemoryPoolDynamic::INVALID_ID; - } - - if (p_size==0 && data && data->id==MemoryPoolDynamic::INVALID_ID) { - - MemoryPoolDynamic::get_singleton()->free(data->id); - data->id=MemoryPoolDynamic::INVALID_ID; - } - - if (p_size>0) { - - if (data->id==MemoryPoolDynamic::INVALID_ID) { - - data->id=MemoryPoolDynamic::get_singleton()->alloc(p_size,"Unnamed MID"); - ERR_FAIL_COND_V( data->id==MemoryPoolDynamic::INVALID_ID, ERR_OUT_OF_MEMORY ); - - } else { - - MemoryPoolDynamic::get_singleton()->realloc(data->id,p_size); - ERR_FAIL_COND_V( data->id==MemoryPoolDynamic::INVALID_ID, ERR_OUT_OF_MEMORY ); - - } - } - - return OK; - } -friend class Memory; - - MID(MemoryPoolDynamic::ID p_id) { - - data = (Data*)MemoryPoolStatic::get_singleton()->alloc(sizeof(Data),"MID::Data"); - data->refcount.init(); - data->id=p_id; - } -public: - - bool is_valid() const { return data; } - operator bool() const { return data; } - - - size_t get_size() const { return (data && data->id!=MemoryPoolDynamic::INVALID_ID) ? MemoryPoolDynamic::get_singleton()->get_size(data->id) : 0; } - Error resize(size_t p_size) { return _resize(p_size); } - inline void operator=(const MID& p_mid) { ref( p_mid.data ); } - inline bool is_locked() const { return (data && data->id!=MemoryPoolDynamic::INVALID_ID) ? MemoryPoolDynamic::get_singleton()->is_locked(data->id) : false; } - inline MID(const MID& p_mid) { data=NULL; ref( p_mid.data ); } - inline MID() { data = NULL; } - ~MID() { unref(); } -}; - - -class MID_Lock { - - MID mid; - -public: - - void *data() { return mid.get(); } +#ifndef PAD_ALIGN +#define PAD_ALIGN 16 //must always be greater than this at much +#endif - void operator=(const MID_Lock& p_lock ) { mid.unlock(); mid = p_lock.mid; mid.lock(); } - inline MID_Lock(const MID& p_mid) { mid=p_mid; mid.lock(); } - inline MID_Lock(const MID_Lock& p_lock) { mid=p_lock.mid; mid.lock(); } - MID_Lock() {} - ~MID_Lock() { mid.unlock(); } -}; class Memory{ Memory(); -public: +#ifdef DEBUG_ENABLED + static size_t mem_usage; + static size_t max_usage; +#endif + + static size_t alloc_count; - static void * alloc_static(size_t p_bytes,const char *p_descr=""); - static void * realloc_static(void *p_memory,size_t p_bytes); - static void free_static(void *p_ptr); - static size_t get_static_mem_available(); - static size_t get_static_mem_usage(); - static size_t get_static_mem_max_usage(); - static void dump_static_mem_to_file(const char* p_file); +public: - static MID alloc_dynamic(size_t p_bytes, const char *p_descr=""); - static Error realloc_dynamic(MID p_mid,size_t p_bytes); + static void * alloc_static(size_t p_bytes,bool p_pad_align=false); + static void * realloc_static(void *p_memory,size_t p_bytes,bool p_pad_align=false); + static void free_static(void *p_ptr,bool p_pad_align=false); - static size_t get_dynamic_mem_available(); - static size_t get_dynamic_mem_usage(); + static size_t get_mem_available(); + static size_t get_mem_usage(); + static size_t get_mem_max_usage(); -}; -template<class T> -struct MemAalign { - static _FORCE_INLINE_ int get_align() { return DEFAULT_ALIGNMENT; } }; class DefaultAllocator { public: - _FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, ""); } - _FORCE_INLINE_ static void free(void *p_ptr) { return Memory::free_static(p_ptr); } + _FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, false); } + _FORCE_INLINE_ static void free(void *p_ptr) { return Memory::free_static(p_ptr,false); } }; @@ -209,31 +79,10 @@ void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)); ///< ope void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory -#ifdef DEBUG_MEMORY_ENABLED - -#define memalloc(m_size) Memory::alloc_static(m_size, __FILE__ ":" __STR(__LINE__) ", memalloc.") -#define memrealloc(m_mem,m_size) Memory::realloc_static(m_mem,m_size) -#define memfree(m_size) Memory::free_static(m_size) - -#else - #define memalloc(m_size) Memory::alloc_static(m_size) #define memrealloc(m_mem,m_size) Memory::realloc_static(m_mem,m_size) #define memfree(m_size) Memory::free_static(m_size) -#endif - -#ifdef DEBUG_MEMORY_ENABLED -#define dynalloc(m_size) Memory::alloc_dynamic(m_size, __FILE__ ":" __STR(__LINE__) ", type: DYNAMIC") -#define dynrealloc(m_mem,m_size) m_mem.resize(m_size) - -#else - -#define dynalloc(m_size) Memory::alloc_dynamic(m_size) -#define dynrealloc(m_mem,m_size) m_mem.resize(m_size) - -#endif - _ALWAYS_INLINE_ void postinitialize_handler(void *) {} @@ -245,16 +94,8 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) { return p_obj; } -#ifdef DEBUG_MEMORY_ENABLED - -#define memnew(m_class) _post_initialize(new(__FILE__ ":" __STR(__LINE__) ", memnew type: " __STR(m_class)) m_class) - -#else - #define memnew(m_class) _post_initialize(new("") m_class) -#endif - _ALWAYS_INLINE_ void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description) { // void *failptr=0; // ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */ @@ -275,7 +116,7 @@ void memdelete(T *p_class) { if (!predelete_handler(p_class)) return; // doesn't want to be deleted p_class->~T(); - Memory::free_static(p_class); + Memory::free_static(p_class,false); } template<class T,class A> @@ -288,15 +129,9 @@ void memdelete_allocator(T *p_class) { } #define memdelete_notnull(m_v) { if (m_v) memdelete(m_v); } -#ifdef DEBUG_MEMORY_ENABLED - -#define memnew_arr( m_class, m_count ) memnew_arr_template<m_class>(m_count,__FILE__ ":" __STR(__LINE__) ", memnew_arr type: " _STR(m_class)) - -#else #define memnew_arr( m_class, m_count ) memnew_arr_template<m_class>(m_count) -#endif template<typename T> T* memnew_arr_template(size_t p_elements,const char *p_descr="") { @@ -304,14 +139,14 @@ T* memnew_arr_template(size_t p_elements,const char *p_descr="") { if (p_elements==0) return 0; /** overloading operator new[] cannot be done , because it may not return the real allocated address (it may pad the 'element count' before the actual array). Because of that, it must be done by hand. This is the - same strategy used by std::vector, and the DVector class, so it should be safe.*/ + same strategy used by std::vector, and the PoolVector class, so it should be safe.*/ size_t len = sizeof(T) * p_elements; - unsigned int *mem = (unsigned int*)Memory::alloc_static( len + MAX(sizeof(size_t), DEFAULT_ALIGNMENT), p_descr ); + uint64_t *mem = (uint64_t*)Memory::alloc_static( len , true ); T *failptr=0; //get rid of a warning ERR_FAIL_COND_V( !mem, failptr ); - *mem=p_elements; - mem = (unsigned int *)( ((uint8_t*)mem) + MAX(sizeof(size_t), DEFAULT_ALIGNMENT)); + *(mem-1)=p_elements; + T* elems = (T*)mem; /* call operator new */ @@ -330,20 +165,22 @@ T* memnew_arr_template(size_t p_elements,const char *p_descr="") { template<typename T> size_t memarr_len(const T *p_class) { - uint8_t* ptr = ((uint8_t*)p_class) - MAX(sizeof(size_t), DEFAULT_ALIGNMENT); - return *(size_t*)ptr; + uint64_t* ptr = (uint64_t*)p_class; + return *(ptr-1); } template<typename T> void memdelete_arr(T *p_class) { - unsigned int * elems = (unsigned int*)(((uint8_t*)p_class) - MAX(sizeof(size_t), DEFAULT_ALIGNMENT)); + uint64_t* ptr = (uint64_t*)p_class; + + uint64_t elem_count = *(ptr-1); - for (unsigned int i=0;i<*elems;i++) { + for (uint64_t i=0;i<elem_count;i++) { p_class[i].~T(); }; - Memory::free_static(elems); + Memory::free_static(ptr,true); } diff --git a/core/os/memory_pool_dynamic.cpp b/core/os/memory_pool_dynamic.cpp deleted file mode 100644 index 0bd64a0362..0000000000 --- a/core/os/memory_pool_dynamic.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/*************************************************************************/ -/* memory_pool_dynamic.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "memory_pool_dynamic.h" - - -MemoryPoolDynamic* MemoryPoolDynamic::singleton=NULL; - - -MemoryPoolDynamic* MemoryPoolDynamic::get_singleton() { - - return singleton; -} - - -MemoryPoolDynamic::MemoryPoolDynamic() { - - ERR_FAIL_COND(singleton!=NULL); - singleton=this; -} - -MemoryPoolDynamic::~MemoryPoolDynamic() { - - singleton=NULL; -} diff --git a/core/os/memory_pool_dynamic.h b/core/os/memory_pool_dynamic.h deleted file mode 100644 index 025e925d3c..0000000000 --- a/core/os/memory_pool_dynamic.h +++ /dev/null @@ -1,79 +0,0 @@ -/*************************************************************************/ -/* memory_pool_dynamic.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef MEMORY_POOL_DYNAMIC_H -#define MEMORY_POOL_DYNAMIC_H - -#include "typedefs.h" - - -class MemoryPoolDynamic { - - static MemoryPoolDynamic* singleton; -protected: -friend class Memory; -friend class MID; - - enum { - - INVALID_ID=0xFFFFFFFF - }; - - static MemoryPoolDynamic* get_singleton(); - - typedef uint64_t ID; - - - virtual ID alloc(size_t p_amount,const char* p_description)=0; - virtual void free(ID p_id)=0; - virtual Error realloc(ID p_id, size_t p_amount)=0; - virtual bool is_valid(ID p_id)=0; - virtual size_t get_size(ID p_id) const=0; - virtual const char* get_description(ID p_id) const=0; - - virtual Error lock(ID p_id)=0; - virtual void * get(ID p_ID)=0; - virtual Error unlock(ID p_id)=0; - virtual bool is_locked(ID p_id) const=0; - - virtual size_t get_available_mem() const=0; - virtual size_t get_total_usage() const=0; - - MemoryPoolDynamic(); -public: - virtual ~MemoryPoolDynamic(); - -}; - - -#endif - - - - - diff --git a/core/os/memory_pool_dynamic_prealloc.cpp b/core/os/memory_pool_dynamic_prealloc.cpp deleted file mode 100644 index d2efa4cc88..0000000000 --- a/core/os/memory_pool_dynamic_prealloc.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/*************************************************************************/ -/* memory_pool_dynamic_prealloc.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "memory_pool_dynamic_prealloc.h" -#include "os/memory.h" - -#include "print_string.h" -MemoryPoolDynamicPrealloc::ID MemoryPoolDynamicPrealloc::alloc(size_t p_amount,const char* p_description) { - - -// print_line("dynpool - allocating: "+itos(p_amount)); - ID id = pool_alloc->alloc(p_amount); -// print_line("dynpool - free: "+itos(pool_alloc->get_free_mem())); - return id; - -} - -void MemoryPoolDynamicPrealloc::free(ID p_id) { - - pool_alloc->free(p_id); -} - -Error MemoryPoolDynamicPrealloc::realloc(ID p_id, size_t p_amount) { - - return pool_alloc->resize(p_id,p_amount); -} - -bool MemoryPoolDynamicPrealloc::is_valid(ID p_id) { - - return true; -} - -size_t MemoryPoolDynamicPrealloc::get_size(ID p_id) const { - - return pool_alloc->get_size(p_id); -} - -const char* MemoryPoolDynamicPrealloc::get_description(ID p_id) const { - - return ""; -} - -Error MemoryPoolDynamicPrealloc::lock(ID p_id) { - -// print_line("lock: "+itos(p_id)); - return pool_alloc->lock(p_id); -} - -void * MemoryPoolDynamicPrealloc::get(ID p_ID) { - -// print_line("get: "+itos(p_ID)); - return pool_alloc->get(p_ID); -} - -Error MemoryPoolDynamicPrealloc::unlock(ID p_id) { - -// print_line("unlock: "+itos(p_id)); - pool_alloc->unlock(p_id); - return OK; -} - -bool MemoryPoolDynamicPrealloc::is_locked(ID p_id) const { - - return pool_alloc->is_locked(p_id); -} - - -size_t MemoryPoolDynamicPrealloc::get_available_mem() const { - - return pool_alloc->get_free_mem(); -} - -size_t MemoryPoolDynamicPrealloc::get_total_usage() const { - - return pool_alloc->get_used_mem(); -} - - - -MemoryPoolDynamicPrealloc::MemoryPoolDynamicPrealloc(void * p_mem,int p_size, int p_align, int p_max_entries) { - - pool_alloc = memnew( PoolAllocator(p_mem,p_size,p_align,true,p_max_entries)); - -} - -MemoryPoolDynamicPrealloc::~MemoryPoolDynamicPrealloc() { - - - memdelete( pool_alloc ); -} - diff --git a/core/os/memory_pool_dynamic_prealloc.h b/core/os/memory_pool_dynamic_prealloc.h deleted file mode 100644 index a2e51b93f3..0000000000 --- a/core/os/memory_pool_dynamic_prealloc.h +++ /dev/null @@ -1,60 +0,0 @@ -/*************************************************************************/ -/* memory_pool_dynamic_prealloc.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef MEMORY_POOL_DYNAMIC_PREALLOC_H -#define MEMORY_POOL_DYNAMIC_PREALLOC_H - -#include "pool_allocator.h" -#include "core/os/memory_pool_dynamic.h" - -class MemoryPoolDynamicPrealloc : public MemoryPoolDynamic { - - PoolAllocator *pool_alloc; - -public: - - virtual ID alloc(size_t p_amount,const char* p_description); - virtual void free(ID p_id); - virtual Error realloc(ID p_id, size_t p_amount); - virtual bool is_valid(ID p_id); - virtual size_t get_size(ID p_id) const; - virtual const char* get_description(ID p_id) const; - - virtual Error lock(ID p_id); - virtual void * get(ID p_ID); - virtual Error unlock(ID p_id); - virtual bool is_locked(ID p_id) const; - - virtual size_t get_available_mem() const; - virtual size_t get_total_usage() const; - - MemoryPoolDynamicPrealloc(void * p_mem,int p_size, int p_align = 16, int p_max_entries=PoolAllocator::DEFAULT_MAX_ALLOCS); - ~MemoryPoolDynamicPrealloc(); -}; - -#endif // MEMORY_POOL_DYNAMIC_PREALLOC_H diff --git a/core/os/memory_pool_dynamic_static.cpp b/core/os/memory_pool_dynamic_static.cpp deleted file mode 100644 index 9db4916cbd..0000000000 --- a/core/os/memory_pool_dynamic_static.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/*************************************************************************/ -/* memory_pool_dynamic_static.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "memory_pool_dynamic_static.h" -#include "os/memory.h" -#include "os/os.h" -#include "ustring.h" -#include "print_string.h" -#include <stdio.h> - -MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) { - - uint64_t check = p_id/MAX_CHUNKS; - uint64_t idx = p_id%MAX_CHUNKS; - - if (!chunk[idx].mem || chunk[idx].check!=check) - return NULL; - - return &chunk[idx]; -} - - -const MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) const { - - uint64_t check = p_id/MAX_CHUNKS; - uint64_t idx = p_id%MAX_CHUNKS; - - if (!chunk[idx].mem || chunk[idx].check!=check) - return NULL; - - return &chunk[idx]; -} - -MemoryPoolDynamic::ID MemoryPoolDynamicStatic::alloc(size_t p_amount,const char* p_description) { - - _THREAD_SAFE_METHOD_ - - int idx=-1; - - for (int i=0;i<MAX_CHUNKS;i++) { - - last_alloc++; - if (last_alloc>=MAX_CHUNKS) - last_alloc=0; - - if ( !chunk[last_alloc].mem ) { - - idx=last_alloc; - break; - } - } - - - if (idx==-1) { - ERR_EXPLAIN("Out of dynamic Memory IDs"); - ERR_FAIL_V(INVALID_ID); - //return INVALID_ID; - } - - //chunk[idx].mem = Memory::alloc_static(p_amount,p_description); - chunk[idx].mem = memalloc(p_amount); - if (!chunk[idx].mem) - return INVALID_ID; - - chunk[idx].size=p_amount; - chunk[idx].check=++last_check; - chunk[idx].descr=p_description; - chunk[idx].lock=0; - - total_usage+=p_amount; - if (total_usage>max_usage) - max_usage=total_usage; - - ID id = chunk[idx].check*MAX_CHUNKS + (uint64_t)idx; - - return id; - -} -void MemoryPoolDynamicStatic::free(ID p_id) { - - _THREAD_SAFE_METHOD_ - - Chunk *c = get_chunk(p_id); - ERR_FAIL_COND(!c); - - - total_usage-=c->size; - memfree(c->mem); - - c->mem=0; - - if (c->lock>0) { - - ERR_PRINT("Freed ID Still locked"); - } -} - -Error MemoryPoolDynamicStatic::realloc(ID p_id, size_t p_amount) { - - _THREAD_SAFE_METHOD_ - - Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(c->lock > 0 , ERR_LOCKED ); - - - void * new_mem = memrealloc(c->mem,p_amount); - - ERR_FAIL_COND_V(!new_mem,ERR_OUT_OF_MEMORY); - total_usage-=c->size; - c->mem=new_mem; - c->size=p_amount; - total_usage+=c->size; - if (total_usage>max_usage) - max_usage=total_usage; - - - return OK; -} -bool MemoryPoolDynamicStatic::is_valid(ID p_id) { - - _THREAD_SAFE_METHOD_ - - Chunk *c = get_chunk(p_id); - - return c!=NULL; - -} -size_t MemoryPoolDynamicStatic::get_size(ID p_id) const { - - _THREAD_SAFE_METHOD_ - - const Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,0); - - return c->size; - - -} -const char* MemoryPoolDynamicStatic::get_description(ID p_id) const { - - _THREAD_SAFE_METHOD_ - - const Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,""); - - return c->descr; - -} - - -bool MemoryPoolDynamicStatic::is_locked(ID p_id) const { - - _THREAD_SAFE_METHOD_ - - const Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,false); - - return c->lock>0; - -} - -Error MemoryPoolDynamicStatic::lock(ID p_id) { - - _THREAD_SAFE_METHOD_ - - Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER); - - c->lock++; - - return OK; -} -void * MemoryPoolDynamicStatic::get(ID p_id) { - - _THREAD_SAFE_METHOD_ - - const Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,NULL); - ERR_FAIL_COND_V( c->lock==0, NULL ); - - return c->mem; -} -Error MemoryPoolDynamicStatic::unlock(ID p_id) { - - _THREAD_SAFE_METHOD_ - - Chunk *c = get_chunk(p_id); - ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER); - - ERR_FAIL_COND_V( c->lock<=0, ERR_INVALID_PARAMETER ); - c->lock--; - - return OK; -} - -size_t MemoryPoolDynamicStatic::get_available_mem() const { - - return Memory::get_static_mem_available(); -} - -size_t MemoryPoolDynamicStatic::get_total_usage() const { - _THREAD_SAFE_METHOD_ - - return total_usage; -} - -MemoryPoolDynamicStatic::MemoryPoolDynamicStatic() { - - last_check=1; - last_alloc=0; - total_usage=0; - max_usage=0; -} - -MemoryPoolDynamicStatic::~MemoryPoolDynamicStatic() { - -#ifdef DEBUG_MEMORY_ENABLED - - if (OS::get_singleton()->is_stdout_verbose()) { - - if (total_usage>0) { - - ERR_PRINT("DYNAMIC ALLOC: ** MEMORY LEAKS DETECTED **"); - ERR_PRINT(String("DYNAMIC ALLOC: "+String::num(total_usage)+" bytes of memory in use at exit.").ascii().get_data()); - - ERR_PRINT("DYNAMIC ALLOC: Following is the list of leaked allocations:"); - - for (int i=0;i<MAX_CHUNKS;i++) { - - if (chunk[i].mem) { - - ERR_PRINT(String("\t"+String::num(chunk[i].size)+" bytes - "+String(chunk[i].descr)).ascii().get_data()); - } - } - - ERR_PRINT("DYNAMIC ALLOC: End of Report."); - - print_line("INFO: dynmem - max: "+itos(max_usage)+", "+itos(total_usage)+" leaked."); - } else { - - print_line("INFO: dynmem - max: "+itos(max_usage)+", no leaks."); - } - } - -#endif -} diff --git a/core/os/memory_pool_dynamic_static.h b/core/os/memory_pool_dynamic_static.h deleted file mode 100644 index 85c969174a..0000000000 --- a/core/os/memory_pool_dynamic_static.h +++ /dev/null @@ -1,86 +0,0 @@ -/*************************************************************************/ -/* memory_pool_dynamic_static.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef MEMORY_POOL_DYNAMIC_STATIC_H -#define MEMORY_POOL_DYNAMIC_STATIC_H - -#include "os/memory_pool_dynamic.h" -#include "typedefs.h" -#include "os/thread_safe.h" - -class MemoryPoolDynamicStatic : public MemoryPoolDynamic { - - _THREAD_SAFE_CLASS_ - - enum { - MAX_CHUNKS=65536 - }; - - - struct Chunk { - - uint64_t lock; - uint64_t check; - void *mem; - size_t size; - const char *descr; - - Chunk() { mem=NULL; lock=0; check=0; } - }; - - Chunk chunk[MAX_CHUNKS]; - uint64_t last_check; - int last_alloc; - size_t total_usage; - size_t max_usage; - - Chunk *get_chunk(ID p_id); - const Chunk *get_chunk(ID p_id) const; -public: - - virtual ID alloc(size_t p_amount,const char* p_description); - virtual void free(ID p_id); - virtual Error realloc(ID p_id, size_t p_amount); - virtual bool is_valid(ID p_id); - virtual size_t get_size(ID p_id) const; - virtual const char* get_description(ID p_id) const; - - virtual bool is_locked(ID p_id) const; - virtual Error lock(ID p_id); - virtual void * get(ID p_ID); - virtual Error unlock(ID p_id); - - virtual size_t get_available_mem() const; - virtual size_t get_total_usage() const; - - MemoryPoolDynamicStatic(); - virtual ~MemoryPoolDynamicStatic(); - -}; - -#endif diff --git a/core/os/memory_pool_static.cpp b/core/os/memory_pool_static.cpp deleted file mode 100644 index bd2ca1436f..0000000000 --- a/core/os/memory_pool_static.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/*************************************************************************/ -/* memory_pool_static.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "memory_pool_static.h" - -MemoryPoolStatic *MemoryPoolStatic::singleton=0; - -MemoryPoolStatic *MemoryPoolStatic::get_singleton() { - - return singleton; -} - - -MemoryPoolStatic::MemoryPoolStatic() { - - singleton=this; -} - - -MemoryPoolStatic::~MemoryPoolStatic() { - singleton=NULL; -} - - diff --git a/core/os/memory_pool_static.h b/core/os/memory_pool_static.h deleted file mode 100644 index 634d3eda60..0000000000 --- a/core/os/memory_pool_static.h +++ /dev/null @@ -1,69 +0,0 @@ -/*************************************************************************/ -/* memory_pool_static.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef MEMORY_POOL_STATIC_H -#define MEMORY_POOL_STATIC_H - -#include <stddef.h> - -#include "core/typedefs.h" - -/** - @author Juan Linietsky <red@lunatea> -*/ -class MemoryPoolStatic { -private: - - static MemoryPoolStatic *singleton; - -public: - - static MemoryPoolStatic *get_singleton(); - - virtual void* alloc(size_t p_bytes,const char *p_description)=0; ///< Pointer in p_description shold be to a const char const like "hello" - virtual void* realloc(void * p_memory,size_t p_bytes)=0; ///< Pointer in p_description shold be to a const char const like "hello" - virtual void free(void *p_ptr)=0; ///< Pointer in p_description shold be to a const char const - - virtual size_t get_available_mem() const=0; - virtual size_t get_total_usage()=0; - virtual size_t get_max_usage()=0; - - /* Most likely available only if memory debugger was compiled in */ - virtual int get_alloc_count()=0; - virtual void * get_alloc_ptr(int p_alloc_idx)=0; - virtual const char* get_alloc_description(int p_alloc_idx)=0; - virtual size_t get_alloc_size(int p_alloc_idx)=0; - - virtual void dump_mem_to_file(const char* p_file)=0; - - MemoryPoolStatic(); - virtual ~MemoryPoolStatic(); - -}; - -#endif diff --git a/core/os/os.cpp b/core/os/os.cpp index f615e54430..677bf63e69 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -187,7 +187,7 @@ const char *OS::get_last_error() const { void OS::dump_memory_to_file(const char* p_file) { - Memory::dump_static_mem_to_file(p_file); +// Memory::dump_static_mem_to_file(p_file); } static FileAccess *_OSPRF=NULL; @@ -297,7 +297,7 @@ String OS::get_locale() const { String OS::get_resource_dir() const { - return Globals::get_singleton()->get_resource_path(); + return GlobalConfig::get_singleton()->get_resource_path(); } @@ -307,7 +307,7 @@ String OS::get_system_dir(SystemDir p_dir) const { } String OS::get_safe_application_name() const { - String an = Globals::get_singleton()->get("application/name"); + String an = GlobalConfig::get_singleton()->get("application/name"); Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" "); for (int i=0;i<invalid_char.size();i++) { an = an.replace(invalid_char[i],"-"); @@ -367,16 +367,16 @@ Error OS::dialog_input_text(String p_title, String p_description, String p_parti int OS::get_static_memory_usage() const { - return Memory::get_static_mem_usage(); + return Memory::get_mem_usage(); } int OS::get_dynamic_memory_usage() const{ - return Memory::get_dynamic_mem_usage(); + return MemoryPool::total_memory; } int OS::get_static_memory_peak_usage() const { - return Memory::get_static_mem_max_usage(); + return Memory::get_mem_max_usage(); } Error OS::set_cwd(const String& p_cwd) { @@ -392,7 +392,7 @@ bool OS::has_touchscreen_ui_hint() const { int OS::get_free_static_memory() const { - return Memory::get_static_mem_available(); + return Memory::get_mem_available(); } void OS::yield() { @@ -533,7 +533,7 @@ bool OS::is_joy_known(int p_device) { } String OS::get_joy_guid(int p_device) const { - return "Default Joystick"; + return "Default Joypad"; } void OS::set_context(int p_context) { diff --git a/core/os/rw_lock.cpp b/core/os/rw_lock.cpp new file mode 100644 index 0000000000..9b2d1f8a46 --- /dev/null +++ b/core/os/rw_lock.cpp @@ -0,0 +1,21 @@ +#include "rw_lock.h" +#include "error_macros.h" +#include <stddef.h> + + + +RWLock* (*RWLock::create_func)()=0; + +RWLock *RWLock::create() { + + ERR_FAIL_COND_V( !create_func, 0 ); + + return create_func(); +} + + +RWLock::~RWLock() { + + +} + diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h new file mode 100644 index 0000000000..c513e6d636 --- /dev/null +++ b/core/os/rw_lock.h @@ -0,0 +1,46 @@ +#ifndef RWLOCK_H +#define RWLOCK_H + +#include "error_list.h" + +class RWLock { +protected: + static RWLock* (*create_func)(); + +public: + + virtual void read_lock()=0; ///< Lock the rwlock, block if locked by someone else + virtual void read_unlock()=0; ///< Unlock the rwlock, let other threads continue + virtual Error read_try_lock()=0; ///< Attempt to lock the rwlock, OK on success, ERROR means it can't lock. + + virtual void write_lock()=0; ///< Lock the rwlock, block if locked by someone else + virtual void write_unlock()=0; ///< Unlock the rwlock, let other thwrites continue + virtual Error write_try_lock()=0; ///< Attempt to lock the rwlock, OK on success, ERROR means it can't lock. + + static RWLock * create(); ///< Create a rwlock + + virtual ~RWLock(); +}; + + +class RWLockRead { + + RWLock *lock; +public: + + RWLockRead(RWLock* p_lock) { lock=p_lock; if (lock) lock->read_lock(); } + ~RWLockRead() { if (lock) lock->read_unlock(); } + +}; + +class RWLockWrite { + + RWLock *lock; +public: + + RWLockWrite(RWLock* p_lock) { lock=p_lock; if (lock) lock->write_lock(); } + ~RWLockWrite() { if (lock) lock->write_unlock(); } + +}; + +#endif // RWLOCK_H diff --git a/core/packed_data_container.cpp b/core/packed_data_container.cpp index 8bf77b735a..77afbfc214 100644 --- a/core/packed_data_container.cpp +++ b/core/packed_data_container.cpp @@ -82,7 +82,7 @@ Variant PackedDataContainer::_iter_get_ofs(const Variant& p_iter,uint32_t p_offs if (pos<0 || pos>=size) return Variant(); - DVector<uint8_t>::Read rd=data.read(); + PoolVector<uint8_t>::Read rd=data.read(); const uint8_t *r=&rd[p_offset]; uint32_t type = decode_uint32(r); @@ -131,7 +131,7 @@ Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs,const uint8_t *p_buf,boo uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const { - DVector<uint8_t>::Read rd=data.read(); + PoolVector<uint8_t>::Read rd=data.read(); const uint8_t *r=&rd[p_ofs]; uint32_t type = decode_uint32(r); @@ -140,7 +140,7 @@ uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const { int PackedDataContainer::_size(uint32_t p_ofs) const { - DVector<uint8_t>::Read rd=data.read(); + PoolVector<uint8_t>::Read rd=data.read(); const uint8_t *r=&rd[p_ofs]; uint32_t type = decode_uint32(r); @@ -160,7 +160,7 @@ int PackedDataContainer::_size(uint32_t p_ofs) const { Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs,const Variant& p_key,bool &err) const { - DVector<uint8_t>::Read rd=data.read(); + PoolVector<uint8_t>::Read rd=data.read(); const uint8_t *r=&rd[p_ofs]; uint32_t type = decode_uint32(r); @@ -344,21 +344,21 @@ Error PackedDataContainer::pack(const Variant& p_data) { _pack(p_data,tmpdata,string_cache); datalen=tmpdata.size(); data.resize(tmpdata.size()); - DVector<uint8_t>::Write w = data.write(); + PoolVector<uint8_t>::Write w = data.write(); copymem(w.ptr(),tmpdata.ptr(),tmpdata.size()); return OK; } -void PackedDataContainer::_set_data(const DVector<uint8_t>& p_data) { +void PackedDataContainer::_set_data(const PoolVector<uint8_t>& p_data) { data=p_data; datalen=data.size(); } -DVector<uint8_t> PackedDataContainer::_get_data() const { +PoolVector<uint8_t> PackedDataContainer::_get_data() const { return data; } diff --git a/core/packed_data_container.h b/core/packed_data_container.h index 5e0180a424..f8ff43f9b0 100644 --- a/core/packed_data_container.h +++ b/core/packed_data_container.h @@ -50,7 +50,7 @@ class PackedDataContainer : public Resource { }; - DVector<uint8_t> data; + PoolVector<uint8_t> data; int datalen; @@ -73,8 +73,8 @@ friend class PackedDataContainerRef; protected: - void _set_data(const DVector<uint8_t>& p_data); - DVector<uint8_t> _get_data() const; + void _set_data(const PoolVector<uint8_t>& p_data); + PoolVector<uint8_t> _get_data() const; static void _bind_methods(); public: diff --git a/core/path_remap.cpp b/core/path_remap.cpp index c0bed76ac8..980e8f76e7 100644 --- a/core/path_remap.cpp +++ b/core/path_remap.cpp @@ -124,13 +124,13 @@ void PathRemap::clear_remaps() { void PathRemap::load_remaps() { // default remaps first - DVector<String> remaps = Globals::get_singleton()->get("remap/all"); + PoolVector<String> remaps = GlobalConfig::get_singleton()->get("remap/all"); { int rlen = remaps.size(); ERR_FAIL_COND( rlen%2 ); - DVector<String>::Read r = remaps.read(); + PoolVector<String>::Read r = remaps.read(); for(int i=0;i<rlen/2;i++) { String from = r[i*2+0]; @@ -141,13 +141,13 @@ void PathRemap::load_remaps() { // platform remaps second, so override - remaps = Globals::get_singleton()->get("remap/"+OS::get_singleton()->get_name()); + remaps = GlobalConfig::get_singleton()->get("remap/"+OS::get_singleton()->get_name()); // remaps = Globals::get_singleton()->get("remap/PSP"); { int rlen = remaps.size(); ERR_FAIL_COND( rlen%2 ); - DVector<String>::Read r = remaps.read(); + PoolVector<String>::Read r = remaps.read(); for(int i=0;i<rlen/2;i++) { String from = r[i*2+0]; @@ -160,9 +160,9 @@ void PathRemap::load_remaps() { //locale based remaps - if (Globals::get_singleton()->has("locale/translation_remaps")) { + if (GlobalConfig::get_singleton()->has("locale/translation_remaps")) { - Dictionary remaps = Globals::get_singleton()->get("locale/translation_remaps"); + Dictionary remaps = GlobalConfig::get_singleton()->get("locale/translation_remaps"); List<Variant> rk; remaps.get_key_list(&rk); for(List<Variant>::Element *E=rk.front();E;E=E->next()) { diff --git a/core/pool_allocator.cpp b/core/pool_allocator.cpp index 00351890c7..e425218060 100644 --- a/core/pool_allocator.cpp +++ b/core/pool_allocator.cpp @@ -604,7 +604,7 @@ void PoolAllocator::create_pool(void * p_mem,int p_size,int p_max_entries) { PoolAllocator::PoolAllocator(int p_size,bool p_needs_locking,int p_max_entries) { - mem_ptr=Memory::alloc_static( p_size,"PoolAllocator()"); + mem_ptr=memalloc( p_size); ERR_FAIL_COND(!mem_ptr); align=1; create_pool(mem_ptr,p_size,p_max_entries); @@ -648,7 +648,7 @@ PoolAllocator::PoolAllocator(int p_align,int p_size,bool p_needs_locking,int p_m PoolAllocator::~PoolAllocator() { if (mem_ptr) - Memory::free_static( mem_ptr ); + memfree( mem_ptr ); memdelete_arr( entry_array ); memdelete_arr( entry_indices ); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 0e1dec075b..fe88d1d13d 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -44,7 +44,6 @@ #include "translation.h" #include "compressed_translation.h" #include "io/translation_loader_po.h" -#include "io/resource_format_xml.h" #include "io/resource_format_binary.h" #include "io/stream_peer_ssl.h" #include "os/input.h" @@ -56,10 +55,6 @@ #include "input_map.h" #include "undo_redo.h" -#ifdef XML_ENABLED -static ResourceFormatSaverXML *resource_saver_xml=NULL; -static ResourceFormatLoaderXML *resource_loader_xml=NULL; -#endif static ResourceFormatSaverBinary *resource_saver_binary=NULL; static ResourceFormatLoaderBinary *resource_loader_binary=NULL; @@ -85,6 +80,9 @@ extern void unregister_variant_methods(); void register_core_types() { + ObjectDB::setup(); + ResourceCache::setup(); + MemoryPool::setup(); _global_mutex=Mutex::create(); @@ -106,13 +104,6 @@ void register_core_types() { resource_loader_binary = memnew( ResourceFormatLoaderBinary ); ResourceLoader::add_resource_format_loader(resource_loader_binary); -#ifdef XML_ENABLED - resource_saver_xml = memnew( ResourceFormatSaverXML ); - ResourceSaver::add_resource_format_saver(resource_saver_xml); - resource_loader_xml = memnew( ResourceFormatLoaderXML ); - ResourceLoader::add_resource_format_loader(resource_loader_xml); -#endif - ClassDB::register_class<Object>(); @@ -172,21 +163,28 @@ void register_core_types() { } +void register_core_settings() { + //since in register core types, globals may not e present + GLOBAL_DEF( "network/packets/packet_stream_peer_max_buffer_po2",(16)); + +} + void register_core_singletons() { - Globals::get_singleton()->add_singleton( Globals::Singleton("Globals",Globals::get_singleton()) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("IP",IP::get_singleton()) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("Geometry",_Geometry::get_singleton()) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("ResourceLoader",_ResourceLoader::get_singleton()) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("ResourceSaver",_ResourceSaver::get_singleton()) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("PathRemap",PathRemap::get_singleton() ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("OS",_OS::get_singleton() ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("ClassDB",_classdb ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("Marshalls",_Marshalls::get_singleton() ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("TranslationServer",TranslationServer::get_singleton() ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("TS",TranslationServer::get_singleton() ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("Input",Input::get_singleton() ) ); - Globals::get_singleton()->add_singleton( Globals::Singleton("InputMap",InputMap::get_singleton() ) ); + + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("GlobalConfig",GlobalConfig::get_singleton()) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("IP",IP::get_singleton()) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("Geometry",_Geometry::get_singleton()) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("ResourceLoader",_ResourceLoader::get_singleton()) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("ResourceSaver",_ResourceSaver::get_singleton()) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("PathRemap",PathRemap::get_singleton() ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("OS",_OS::get_singleton() ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("ClassDB",_classdb ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("Marshalls",_Marshalls::get_singleton() ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("TranslationServer",TranslationServer::get_singleton() ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("TS",TranslationServer::get_singleton() ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("Input",Input::get_singleton() ) ); + GlobalConfig::get_singleton()->add_singleton( GlobalConfig::Singleton("InputMap",InputMap::get_singleton() ) ); } @@ -202,12 +200,6 @@ void unregister_core_types() { memdelete( _marshalls ); memdelete( _geometry ); -#ifdef XML_ENABLED - if (resource_saver_xml) - memdelete(resource_saver_xml); - if (resource_loader_xml) - memdelete(resource_loader_xml); -#endif if (resource_saver_binary) memdelete(resource_saver_binary); @@ -234,4 +226,7 @@ void unregister_core_types() { memdelete(_global_mutex); _global_mutex=NULL; //still needed at a few places }; + + MemoryPool::cleanup(); + } diff --git a/core/register_core_types.h b/core/register_core_types.h index c9e0d27686..c664d0ebf4 100644 --- a/core/register_core_types.h +++ b/core/register_core_types.h @@ -34,6 +34,7 @@ */ void register_core_types(); +void register_core_settings(); void register_core_singletons(); void unregister_core_types(); diff --git a/core/resource.cpp b/core/resource.cpp index a503e965b0..9545d8c3da 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -31,6 +31,7 @@ #include <stdio.h> #include "os/file_access.h" #include "io/resource_loader.h" +#include "script_language.h" void ResourceImportMetadata::set_editor(const String& p_editor) { @@ -164,17 +165,31 @@ void Resource::set_path(const String& p_path, bool p_take_over) { if (path_cache!="") { + ResourceCache::lock->write_lock(); ResourceCache::resources.erase(path_cache); + ResourceCache::lock->write_unlock(); } path_cache=""; - if (ResourceCache::resources.has( p_path )) { + + ResourceCache::lock->read_lock(); + bool has_path = ResourceCache::resources.has( p_path ); + ResourceCache::lock->read_unlock(); + + if (has_path) { if (p_take_over) { + ResourceCache::lock->write_lock(); ResourceCache::resources.get(p_path)->set_name(""); + ResourceCache::lock->write_unlock(); } else { ERR_EXPLAIN("Another resource is loaded from path: "+p_path); - ERR_FAIL_COND( ResourceCache::resources.has( p_path ) ); + + ResourceCache::lock->read_lock(); + bool exists = ResourceCache::resources.has( p_path ); + ResourceCache::lock->read_unlock(); + + ERR_FAIL_COND( exists ); } } @@ -182,10 +197,12 @@ void Resource::set_path(const String& p_path, bool p_take_over) { if (path_cache!="") { + ResourceCache::lock->write_lock(); ResourceCache::resources[path_cache]=this;; + ResourceCache::lock->write_unlock(); } - _change_notify("resource/path"); + _change_notify("resource_path"); _resource_path_changed(); } @@ -209,7 +226,7 @@ int Resource::get_subindex() const{ void Resource::set_name(const String& p_name) { name=p_name; - _change_notify("resource/name"); + _change_notify("resource_name"); } String Resource::get_name() const { @@ -241,7 +258,7 @@ void Resource::reload_from_file() { if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) continue; - if (E->get().name=="resource/path") + if (E->get().name=="resource_path") continue; //do not change path set(E->get().name,s->get(E->get().name)); @@ -250,8 +267,51 @@ void Resource::reload_from_file() { } +Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) { + + + List<PropertyInfo> plist; + get_property_list(&plist); + + + Resource *r = (Resource*)ClassDB::instance(get_class()); + ERR_FAIL_COND_V(!r,Ref<Resource>()); + + r->local_scene=p_for_scene; + + for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) { + + if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) + continue; + Variant p = get(E->get().name); + if (p.get_type()==Variant::OBJECT) { + + RES sr = p; + if (sr.is_valid()) { + + if (sr->is_local_to_scene()) { + if (remap_cache.has(sr)) { + p=remap_cache[sr]; + } else { + + + RES dupe = sr->duplicate_for_local_scene(p_for_scene,remap_cache); + p=dupe; + remap_cache[sr]=dupe; + } + } + } + } + + r->set(E->get().name,p); + } + + return Ref<Resource>(r); +} + Ref<Resource> Resource::duplicate(bool p_subresources) { + List<PropertyInfo> plist; get_property_list(&plist); @@ -289,24 +349,6 @@ void Resource::_take_over_path(const String& p_path) { } -void Resource::_bind_methods() { - - ClassDB::bind_method(_MD("set_path","path"),&Resource::_set_path); - ClassDB::bind_method(_MD("take_over_path","path"),&Resource::_take_over_path); - ClassDB::bind_method(_MD("get_path"),&Resource::get_path); - ClassDB::bind_method(_MD("set_name","name"),&Resource::set_name); - ClassDB::bind_method(_MD("get_name"),&Resource::get_name); - ClassDB::bind_method(_MD("get_rid"),&Resource::get_rid); - ClassDB::bind_method(_MD("set_import_metadata","metadata"),&Resource::set_import_metadata); - ClassDB::bind_method(_MD("get_import_metadata"),&Resource::get_import_metadata); - - ClassDB::bind_method(_MD("duplicate","subresources"),&Resource::duplicate,DEFVAL(false)); - ADD_SIGNAL( MethodInfo("changed") ); - ADD_GROUP("Resource","resource_"); - ADD_PROPERTY( PropertyInfo(Variant::STRING,"resource_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ), _SCS("set_path"),_SCS("get_path")); - ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"resource_name"), _SCS("set_name"),_SCS("get_name")); -} - RID Resource::get_rid() const { return RID(); @@ -378,6 +420,62 @@ uint32_t Resource::hash_edited_version() const { #endif +void Resource::set_local_to_scene(bool p_enable) { + + local_to_scene=p_enable; +} + +bool Resource::is_local_to_scene() const { + + return local_to_scene; +} + +Node* Resource::get_local_scene() const { + + if (local_scene) + return local_scene; + + if (_get_local_scene_func) { + return _get_local_scene_func(); + } + + return NULL; +} + +void Resource::setup_local_to_scene() { + + if (get_script_instance()) + get_script_instance()->call("_setup_local_to_scene"); +} + +Node* (*Resource::_get_local_scene_func)()=NULL; + + +void Resource::_bind_methods() { + + ClassDB::bind_method(_MD("set_path","path"),&Resource::_set_path); + ClassDB::bind_method(_MD("take_over_path","path"),&Resource::_take_over_path); + ClassDB::bind_method(_MD("get_path"),&Resource::get_path); + ClassDB::bind_method(_MD("set_name","name"),&Resource::set_name); + ClassDB::bind_method(_MD("get_name"),&Resource::get_name); + ClassDB::bind_method(_MD("get_rid"),&Resource::get_rid); + ClassDB::bind_method(_MD("set_import_metadata","metadata"),&Resource::set_import_metadata); + ClassDB::bind_method(_MD("get_import_metadata"),&Resource::get_import_metadata); + ClassDB::bind_method(_MD("set_local_to_scene","enable"),&Resource::set_local_to_scene); + ClassDB::bind_method(_MD("is_local_to_scene"),&Resource::is_local_to_scene); + ClassDB::bind_method(_MD("get_local_scene:Node"),&Resource::get_local_scene); + ClassDB::bind_method(_MD("setup_local_to_scene"),&Resource::setup_local_to_scene); + + ClassDB::bind_method(_MD("duplicate","subresources"),&Resource::duplicate,DEFVAL(false)); + ADD_SIGNAL( MethodInfo("changed") ); + ADD_GROUP("Resource","resource_"); + ADD_PROPERTYNZ( PropertyInfo(Variant::BOOL,"resource_local_to_scene" ), _SCS("set_local_to_scene"),_SCS("is_local_to_scene")); + ADD_PROPERTY( PropertyInfo(Variant::STRING,"resource_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ), _SCS("set_path"),_SCS("get_path")); + ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"resource_name"), _SCS("set_name"),_SCS("get_name")); + + BIND_VMETHOD( MethodInfo("_setup_local_to_scene") ); + +} Resource::Resource() { @@ -386,6 +484,7 @@ Resource::Resource() { #endif subindex=0; + local_scene=NULL; } @@ -393,8 +492,11 @@ Resource::Resource() { Resource::~Resource() { - if (path_cache!="") + if (path_cache!="") { + ResourceCache::lock->write_lock(); ResourceCache::resources.erase(path_cache); + ResourceCache::lock->write_unlock(); + } if (owners.size()) { WARN_PRINT("Resource is still owned"); } @@ -402,18 +504,24 @@ Resource::~Resource() { HashMap<String,Resource*> ResourceCache::resources; +RWLock *ResourceCache::lock=NULL; + +void ResourceCache::setup() { + + lock = RWLock::create(); +} + void ResourceCache::clear() { if (resources.size()) ERR_PRINT("Resources Still in use at Exit!"); resources.clear(); + memdelete(lock); } void ResourceCache::reload_externals() { - GLOBAL_LOCK_FUNCTION - //const String *K=NULL; //while ((K=resources.next(K))) { // resources[*K]->reload_external_data(); @@ -423,15 +531,21 @@ void ResourceCache::reload_externals() { bool ResourceCache::has(const String& p_path) { - GLOBAL_LOCK_FUNCTION + lock->read_lock();; + bool b = resources.has(p_path); + lock->read_unlock();; + - return resources.has(p_path); + return b; } Resource *ResourceCache::get(const String& p_path) { - GLOBAL_LOCK_FUNCTION + lock->read_lock(); Resource **res = resources.getptr(p_path); + + lock->read_unlock(); + if (!res) { return NULL; } @@ -443,6 +557,7 @@ Resource *ResourceCache::get(const String& p_path) { void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) { + lock->read_lock(); const String* K=NULL; while((K=resources.next(K))) { @@ -450,17 +565,22 @@ void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) { p_resources->push_back( Ref<Resource>( r )); } + lock->read_unlock(); } int ResourceCache::get_cached_resource_count() { - return resources.size(); + lock->read_lock(); + int rc = resources.size(); + lock->read_unlock(); + + return rc; } void ResourceCache::dump(const char* p_file,bool p_short) { #ifdef DEBUG_ENABLED - GLOBAL_LOCK_FUNCTION + lock->read_lock(); Map<String,int> type_count; @@ -500,5 +620,7 @@ void ResourceCache::dump(const char* p_file,bool p_short) { memdelete(f); } + lock->read_unlock(); + #endif } diff --git a/core/resource.h b/core/resource.h index cb109b3767..8b73bcdc57 100644 --- a/core/resource.h +++ b/core/resource.h @@ -61,8 +61,9 @@ class ResourceImportMetadata : public Reference { Map<String,Variant> options; StringArray _get_options() const; + protected: - virtual bool _use_builtin_script() const { return false; } + static void _bind_methods(); public: @@ -82,6 +83,7 @@ public: void get_options(List<String> *r_options) const; + ResourceImportMetadata(); }; @@ -108,6 +110,10 @@ friend class ResourceCache; uint64_t last_modified_time; #endif + bool local_to_scene; +friend class SceneState; + Node* local_scene; + protected: void emit_changed(); @@ -121,6 +127,8 @@ protected: void _take_over_path(const String& p_path); public: + static Node* (*_get_local_scene_func)(); //used by editor + virtual bool editor_can_reload_from_file(); virtual void reload_from_file(); @@ -137,12 +145,17 @@ public: int get_subindex() const; Ref<Resource> duplicate(bool p_subresources=false); + Ref<Resource> duplicate_for_local_scene(Node *p_scene,Map<Ref<Resource>,Ref<Resource> >& remap_cache); + void set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata); Ref<ResourceImportMetadata> get_import_metadata() const; + void set_local_to_scene(bool p_enable); + bool is_local_to_scene() const; + virtual void setup_local_to_scene(); - + Node* get_local_scene() const; #ifdef TOOLS_ENABLED @@ -165,9 +178,12 @@ typedef Ref<Resource> RES; class ResourceCache { friend class Resource; + static RWLock *lock; static HashMap<String,Resource*> resources; friend void unregister_core_types(); static void clear(); +friend void register_core_types(); + static void setup(); public: static void reload_externals(); diff --git a/core/rid.h b/core/rid.h index 466e922968..8dc535c9c1 100644 --- a/core/rid.h +++ b/core/rid.h @@ -113,15 +113,15 @@ protected: #ifndef DEBUG_ENABLED - _FORCE_INLINE_ bool _is_owner(RID& p_rid) const { + _FORCE_INLINE_ bool _is_owner(const RID& p_rid) const { - return this==p_rid._owner; + return this==p_rid._data->_owner; } _FORCE_INLINE_ void _remove_owner(RID& p_rid) { - return p_rid._owner=NULL; + p_rid._data->_owner=NULL; } # diff --git a/core/safe_refcount.cpp b/core/safe_refcount.cpp index de6b688b8a..ede37bbe8a 100644 --- a/core/safe_refcount.cpp +++ b/core/safe_refcount.cpp @@ -29,27 +29,85 @@ #include "safe_refcount.h" +// Atomic functions, these are used for multithread safe reference counters! + +#ifdef NO_THREADS + + +uint32_t atomic_conditional_increment( register uint32_t * pw ) { + + if (*pw==0) + return 0; + + (*pw)++; + + return *pw; +} + +uint32_t atomic_decrement( register uint32_t * pw ) { + + (*pw)--; + + return *pw; + +} + +#else + #ifdef _MSC_VER // don't pollute my namespace! #include <windows.h> -long atomic_conditional_increment( register long * pw ) { +uint32_t atomic_conditional_increment( register uint32_t * pw ) { /* try to increment until it actually works */ // taken from boost while (true) { - long tmp = static_cast< long const volatile& >( *pw ); + uint32_t tmp = static_cast< uint32_t const volatile& >( *pw ); if( tmp == 0 ) - return 0; // if zero, can't add to it anymore - if( InterlockedCompareExchange( pw, tmp + 1, tmp ) == tmp ) + return 0; // if zero, can't add to it anymore + if( InterlockedCompareExchange( (LONG volatile*)pw, tmp + 1, tmp ) == tmp ) return tmp+1; } +} +uint32_t atomic_decrement( register uint32_t * pw ) { + return InterlockedDecrement( (LONG volatile*)pw ); } -long atomic_decrement( register long * pw ) { - return InterlockedDecrement( pw ); +uint32_t atomic_increment( register uint32_t * pw ) { + return InterlockedIncrement( (LONG volatile*)pw ); } +#elif defined(__GNUC__) + +uint32_t atomic_conditional_increment( register uint32_t * pw ) { + + while (true) { + uint32_t tmp = static_cast< uint32_t const volatile& >( *pw ); + if( tmp == 0 ) + return 0; // if zero, can't add to it anymore + if( __sync_val_compare_and_swap( pw, tmp, tmp + 1 ) == tmp ) + return tmp+1; + } +} + +uint32_t atomic_decrement( register uint32_t * pw ) { + + return __sync_sub_and_fetch(pw,1); + +} + +uint32_t atomic_increment( register uint32_t * pw ) { + + return __sync_add_and_fetch(pw,1); + +} + +#else + //no threads supported? +#error Must provide atomic functions for this platform or compiler! + +#endif #endif diff --git a/core/safe_refcount.h b/core/safe_refcount.h index 5bb2a4564b..6e349d89d8 100644 --- a/core/safe_refcount.h +++ b/core/safe_refcount.h @@ -33,309 +33,18 @@ /* x86/x86_64 GCC */ #include "platform_config.h" +#include "typedefs.h" -#ifdef NO_THREADS - -struct SafeRefCount { - - int count; - -public: - - // destroy() is called when weak_count_ drops to zero. - - bool ref() { //true on success - - if (count==0) - return false; - count++; - - return true; - } - - int refval() { //true on success - - if (count==0) - return 0; - count++; - return count; - } - - bool unref() { // true if must be disposed of - - if (count>0) - count--; - - return count==0; - } - - long get() const { // nothrow - - return static_cast<int const volatile &>( count ); - } - - void init(int p_value=1) { - - count=p_value; - }; - -}; - - - - - - - - -#else - -#if defined( PLATFORM_REFCOUNT ) - -#include "platform_refcount.h" - - -#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) ) - -#define REFCOUNT_T volatile int -#define REFCOUNT_GET_T int const volatile& - -static inline int atomic_conditional_increment( volatile int * pw ) { - // int rv = *pw; - // if( rv != 0 ) ++*pw; - // return rv; - - int rv, tmp; - - __asm__ - ( - "movl %0, %%eax\n\t" - "0:\n\t" - "test %%eax, %%eax\n\t" - "je 1f\n\t" - "movl %%eax, %2\n\t" - "incl %2\n\t" - "lock\n\t" - "cmpxchgl %2, %0\n\t" - "jne 0b\n\t" - "1:": - "=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2) - "m"( *pw ): // input (%3) - "cc" // clobbers - ); - - return rv; -} - -static inline int atomic_decrement( volatile int *pw) { - - // return --(*pw); - - unsigned char rv; - - __asm__ - ( - "lock\n\t" - "decl %0\n\t" - "setne %1": - "=m" (*pw), "=qm" (rv): - "m" (*pw): - "memory" - ); - return static_cast<int>(rv); -} - -/* PowerPC32/64 GCC */ - -#elif ( defined( __GNUC__ ) ) && ( defined( __powerpc__ ) || defined( __ppc__ ) ) - -#define REFCOUNT_T int -#define REFCOUNT_GET_T int const volatile& - -inline int atomic_conditional_increment( int * pw ) -{ - // if( *pw != 0 ) ++*pw; - // return *pw; - - int rv; - - __asm__ - ( - "0:\n\t" - "lwarx %1, 0, %2\n\t" - "cmpwi %1, 0\n\t" - "beq 1f\n\t" - "addi %1, %1, 1\n\t" - "1:\n\t" - "stwcx. %1, 0, %2\n\t" - "bne- 0b": - - "=m"( *pw ), "=&b"( rv ): - "r"( pw ), "m"( *pw ): - "cc" - ); - - return rv; -} - - -inline int atomic_decrement( int * pw ) -{ - // return --*pw; - - int rv; - - __asm__ __volatile__ - ( - "sync\n\t" - "0:\n\t" - "lwarx %1, 0, %2\n\t" - "addi %1, %1, -1\n\t" - "stwcx. %1, 0, %2\n\t" - "bne- 0b\n\t" - "isync": - - "=m"( *pw ), "=&b"( rv ): - "r"( pw ), "m"( *pw ): - "memory", "cc" - ); - - return rv; -} - -/* CW ARM */ - -#elif defined( __GNUC__ ) && ( defined( __arm__ ) ) - -#define REFCOUNT_T int -#define REFCOUNT_GET_T int const volatile& - -inline int atomic_conditional_increment(volatile int* v) -{ - int t; - int tmp; - - __asm__ __volatile__( - "1: ldrex %0, [%2] \n" - " cmp %0, #0 \n" - " beq 2f \n" - " add %0, %0, #1 \n" - "2: \n" - " strex %1, %0, [%2] \n" - " cmp %1, #0 \n" - " bne 1b \n" - - : "=&r" (t), "=&r" (tmp) - : "r" (v) - : "cc", "memory"); - - return t; -} - - -inline int atomic_decrement(volatile int* v) -{ - int t; - int tmp; - - __asm__ __volatile__( - "1: ldrex %0, [%2] \n" - " add %0, %0, #-1 \n" - " strex %1, %0, [%2] \n" - " cmp %1, #0 \n" - " bne 1b \n" - - : "=&r" (t), "=&r" (tmp) - : "r" (v) - : "cc", "memory"); - - return t; -} - - - -/* CW PPC */ - -#elif ( defined( __MWERKS__ ) ) && defined( __POWERPC__ ) - -inline long atomic_conditional_increment( register long * pw ) -{ - register int a; - - asm - { - loop: - - lwarx a, 0, pw - cmpwi a, 0 - beq store - - addi a, a, 1 - - store: - - stwcx. a, 0, pw - bne- loop - } - - return a; -} - - -inline long atomic_decrement( register long * pw ) -{ - register int a; - - asm { - - sync - - loop: - - lwarx a, 0, pw - addi a, a, -1 - stwcx. a, 0, pw - bne- loop - - isync - } - - return a; -} - -/* Any Windows (MSVC) */ - -#elif defined( _MSC_VER ) - -// made functions to not pollute namespace.. - -#define REFCOUNT_T long -#define REFCOUNT_GET_T long const volatile& - -long atomic_conditional_increment( register long * pw ); -long atomic_decrement( register long * pw ); - -#if 0 -#elif defined( __GNUC__ ) && defined( ARMV6_ENABLED) - - -#endif - - - - -#else - -#error This platform cannot use safe refcount, compile with NO_THREADS or implement it. - -#endif +uint32_t atomic_conditional_increment( register uint32_t * counter ); +uint32_t atomic_decrement( register uint32_t * pw ); +uint32_t atomic_increment( register uint32_t * pw ); struct SafeRefCount { - REFCOUNT_T count; + uint32_t count; public: @@ -346,7 +55,7 @@ public: return atomic_conditional_increment( &count ) != 0; } - int refval() { //true on success + uint32_t refval() { //true on success return atomic_conditional_increment( &count ); } @@ -360,20 +69,18 @@ public: return false; } - long get() const { // nothrow + uint32_t get() const { // nothrow - return static_cast<REFCOUNT_GET_T>( count ); + return count; } - void init(int p_value=1) { + void init(uint32_t p_value=1) { count=p_value; - }; + } }; -#endif // no thread safe - #endif diff --git a/core/script_debugger_remote.cpp b/core/script_debugger_remote.cpp index c1d78f129a..62fcd5247f 100644 --- a/core/script_debugger_remote.cpp +++ b/core/script_debugger_remote.cpp @@ -134,7 +134,7 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script,bool p_can_continue) { ERR_FAIL(); } - OS::get_singleton()->enable_for_stealing_focus(Globals::get_singleton()->get("editor_pid")); + OS::get_singleton()->enable_for_stealing_focus(GlobalConfig::get_singleton()->get("editor_pid")); packet_peer_stream->put_var("debug_enter"); packet_peer_stream->put_var(2); @@ -1009,12 +1009,12 @@ ScriptDebuggerRemote::ScriptDebuggerRemote() { phl.userdata=this; add_print_handler(&phl); requested_quit=false; - performance = Globals::get_singleton()->get_singleton_object("Performance"); + performance = GlobalConfig::get_singleton()->get_singleton_object("Performance"); last_perf_time=0; poll_every=0; request_scene_tree=NULL; live_edit_funcs=NULL; - max_cps = GLOBAL_DEF("debug/max_remote_stdout_chars_per_second",2048); + max_cps = GLOBAL_DEF("network/debug/max_remote_stdout_chars_per_second",2048); char_count=0; msec_count=0; last_msec=0; @@ -1024,7 +1024,7 @@ ScriptDebuggerRemote::ScriptDebuggerRemote() { eh.userdata=this; add_error_handler(&eh); - profile_info.resize(CLAMP(int(Globals::get_singleton()->get("debug/profiler_max_functions")),128,65535)); + profile_info.resize(CLAMP(int(GlobalConfig::get_singleton()->get("debug/profiler/max_functions")),128,65535)); profile_info_ptrs.resize(profile_info.size()); profiling=false; max_frame_functions=16; diff --git a/core/string_db.cpp b/core/string_db.cpp index 471195ceda..be35a44ed1 100644 --- a/core/string_db.cpp +++ b/core/string_db.cpp @@ -41,9 +41,12 @@ StringName _scs_create(const char *p_chr) { } bool StringName::configured=false; +Mutex* StringName::lock=NULL; void StringName::setup() { + lock = Mutex::create(); + ERR_FAIL_COND(configured); for(int i=0;i<STRING_TABLE_LEN;i++) { @@ -54,7 +57,8 @@ void StringName::setup() { void StringName::cleanup() { - _global_lock(); + lock->lock(); + int lost_strings=0; for(int i=0;i<STRING_TABLE_LEN;i++) { @@ -78,7 +82,9 @@ void StringName::cleanup() { if (OS::get_singleton()->is_stdout_verbose() && lost_strings) { print_line("StringName: "+itos(lost_strings)+" unclaimed string names at exit."); } - _global_unlock(); + lock->unlock(); + + memdelete(lock); } void StringName::unref() { @@ -87,7 +93,7 @@ void StringName::unref() { if (_data && _data->refcount.unref()) { - _global_lock(); + lock->lock(); if (_data->prev) { _data->prev->next=_data->next; @@ -103,7 +109,7 @@ void StringName::unref() { } memdelete(_data); - _global_unlock(); + lock->unlock(); } _data=NULL; @@ -186,7 +192,7 @@ StringName::StringName(const char *p_name) { if (!p_name || p_name[0]==0) return; //empty, ignore - _global_lock(); + lock->lock(); uint32_t hash = String::hash(p_name); @@ -206,7 +212,7 @@ StringName::StringName(const char *p_name) { if (_data) { if (_data->refcount.ref()) { // exists - _global_unlock(); + lock->unlock(); return; } else { @@ -226,8 +232,7 @@ StringName::StringName(const char *p_name) { _table[idx]=_data; - _global_unlock(); - + lock->unlock(); } StringName::StringName(const StaticCString& p_static_string) { @@ -238,7 +243,7 @@ StringName::StringName(const StaticCString& p_static_string) { ERR_FAIL_COND( !p_static_string.ptr || !p_static_string.ptr[0]); - _global_lock(); + lock->lock(); uint32_t hash = String::hash(p_static_string.ptr); @@ -258,7 +263,7 @@ StringName::StringName(const StaticCString& p_static_string) { if (_data) { if (_data->refcount.ref()) { // exists - _global_unlock(); + lock->unlock(); return; } else { @@ -278,7 +283,8 @@ StringName::StringName(const StaticCString& p_static_string) { _table[idx]=_data; - _global_unlock(); + lock->unlock(); + } @@ -292,7 +298,7 @@ StringName::StringName(const String& p_name) { if (p_name==String()) return; - _global_lock(); + lock->lock(); uint32_t hash = p_name.hash(); @@ -311,7 +317,7 @@ StringName::StringName(const String& p_name) { if (_data) { if (_data->refcount.ref()) { // exists - _global_unlock(); + lock->unlock(); return; } else { @@ -332,7 +338,7 @@ StringName::StringName(const String& p_name) { _table[idx]->prev=_data; _table[idx]=_data; - _global_unlock(); + lock->unlock(); } @@ -344,7 +350,7 @@ StringName StringName::search(const char *p_name) { if (!p_name[0]) return StringName(); - _global_lock(); + lock->lock(); uint32_t hash = String::hash(p_name); @@ -361,12 +367,13 @@ StringName StringName::search(const char *p_name) { } if (_data && _data->refcount.ref()) { - _global_unlock(); + lock->unlock(); + return StringName(_data); } - _global_unlock(); + lock->unlock(); return StringName(); //does not exist @@ -380,7 +387,7 @@ StringName StringName::search(const CharType *p_name) { if (!p_name[0]) return StringName(); - _global_lock(); + lock->lock(); uint32_t hash = String::hash(p_name); @@ -397,12 +404,12 @@ StringName StringName::search(const CharType *p_name) { } if (_data && _data->refcount.ref()) { - _global_unlock(); + lock->unlock(); return StringName(_data); } - _global_unlock(); + lock->unlock(); return StringName(); //does not exist } @@ -410,7 +417,7 @@ StringName StringName::search(const String &p_name) { ERR_FAIL_COND_V( p_name=="", StringName() ); - _global_lock(); + lock->lock(); uint32_t hash = p_name.hash(); @@ -427,12 +434,12 @@ StringName StringName::search(const String &p_name) { } if (_data && _data->refcount.ref()) { - _global_unlock(); + lock->unlock(); return StringName(_data); } - _global_unlock(); + lock->unlock(); return StringName(); //does not exist } diff --git a/core/string_db.h b/core/string_db.h index fd24159265..a14cdbc7ba 100644 --- a/core/string_db.h +++ b/core/string_db.h @@ -32,7 +32,7 @@ #include "hash_map.h" #include "ustring.h" #include "safe_refcount.h" - +#include "os/mutex.h" /** @author Juan Linietsky <reduzio@gmail.com> */ @@ -83,6 +83,7 @@ class StringName { friend void register_core_types(); friend void unregister_core_types(); + static Mutex *lock; static void setup(); static void cleanup(); static bool configured; diff --git a/core/translation.cpp b/core/translation.cpp index 9ac1d6a31e..dd0f59f343 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -309,6 +309,7 @@ static const char* locale_list[]={ "sa_IN", // Sanskrit (India) "sat_IN", // Santali (India) "sc_IT", // Sardinian (Italy) +"sco", // Scots "sd_IN", // Sindhi (India) "se_NO", // Northern Sami (Norway) "sgs_LT", // Samogitian (Lithuania) @@ -799,9 +800,9 @@ static bool is_valid_locale(const String& p_locale) { return false; } -DVector<String> Translation::_get_messages() const { +PoolVector<String> Translation::_get_messages() const { - DVector<String> msgs; + PoolVector<String> msgs; msgs.resize(translation_map.size()*2); int idx=0; for (const Map<StringName, StringName>::Element *E=translation_map.front();E;E=E->next()) { @@ -814,9 +815,9 @@ DVector<String> Translation::_get_messages() const { return msgs; } -DVector<String> Translation::_get_message_list() const { +PoolVector<String> Translation::_get_message_list() const { - DVector<String> msgs; + PoolVector<String> msgs; msgs.resize(translation_map.size()); int idx=0; for (const Map<StringName, StringName>::Element *E=translation_map.front();E;E=E->next()) { @@ -829,12 +830,12 @@ DVector<String> Translation::_get_message_list() const { } -void Translation::_set_messages(const DVector<String>& p_messages){ +void Translation::_set_messages(const PoolVector<String>& p_messages){ int msg_count=p_messages.size(); ERR_FAIL_COND(msg_count%2); - DVector<String>::Read r = p_messages.read(); + PoolVector<String>::Read r = p_messages.read(); for(int i=0;i<msg_count;i+=2) { @@ -938,6 +939,10 @@ void TranslationServer::set_locale(const String& p_locale) { else { locale=univ_locale; } + + if (OS::get_singleton()->get_main_loop()) { + OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED); + } } String TranslationServer::get_locale() const { @@ -1047,13 +1052,13 @@ TranslationServer *TranslationServer::singleton=NULL; bool TranslationServer::_load_translations(const String& p_from) { - if (Globals::get_singleton()->has(p_from)) { - DVector<String> translations=Globals::get_singleton()->get(p_from); + if (GlobalConfig::get_singleton()->has(p_from)) { + PoolVector<String> translations=GlobalConfig::get_singleton()->get(p_from); int tcount=translations.size(); if (tcount) { - DVector<String>::Read r = translations.read(); + PoolVector<String>::Read r = translations.read(); for(int i=0;i<tcount;i++) { @@ -1089,7 +1094,7 @@ void TranslationServer::setup() { options+=locale_list[idx]; idx++; } - Globals::get_singleton()->set_custom_property_info("locale/fallback",PropertyInfo(Variant::STRING,"locale/fallback",PROPERTY_HINT_ENUM,options)); + GlobalConfig::get_singleton()->set_custom_property_info("locale/fallback",PropertyInfo(Variant::STRING,"locale/fallback",PROPERTY_HINT_ENUM,options)); } #endif //load translations diff --git a/core/translation.h b/core/translation.h index 01edc82139..85ab4a229d 100644 --- a/core/translation.h +++ b/core/translation.h @@ -42,10 +42,10 @@ class Translation : public Resource { String locale; Map<StringName, StringName> translation_map; - DVector<String> _get_message_list() const; + PoolVector<String> _get_message_list() const; - DVector<String> _get_messages() const; - void _set_messages(const DVector<String>& p_messages); + PoolVector<String> _get_messages() const; + void _set_messages(const PoolVector<String>& p_messages); protected: static void _bind_methods(); diff --git a/core/typedefs.h b/core/typedefs.h index b24f259cc6..176c77570d 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -77,10 +77,6 @@ #endif -#ifndef DEFAULT_ALIGNMENT -#define DEFAULT_ALIGNMENT 1 -#endif - //custom, gcc-safe offsetof, because gcc complains a lot. template<class T> diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index 5bdc8ef72d..acb262d400 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -27,7 +27,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "undo_redo.h" - +#include "os/os.h" void UndoRedo::_discard_redo() { @@ -54,12 +54,14 @@ void UndoRedo::_discard_redo() { void UndoRedo::create_action(const String& p_name,MergeMode p_mode) { + uint32_t ticks = OS::get_singleton()->get_ticks_msec(); + if (action_level==0) { _discard_redo(); // Check if the merge operation is valid - if (p_mode!=MERGE_DISABLE && actions.size() && actions[actions.size()-1].name==p_name) { + if (p_mode!=MERGE_DISABLE && actions.size() && actions[actions.size()-1].name==p_name && actions[actions.size()-1].last_tick+800 > ticks) { current_action=actions.size()-2; @@ -83,12 +85,15 @@ void UndoRedo::create_action(const String& p_name,MergeMode p_mode) { } } + actions[actions.size()-1].last_tick=ticks; + merge_mode=p_mode; } else { Action new_action; new_action.name=p_name; + new_action.last_tick=ticks; actions.push_back(new_action); merge_mode=MERGE_DISABLE; diff --git a/core/undo_redo.h b/core/undo_redo.h index 3d14dd9ee2..7664cf7cb5 100644 --- a/core/undo_redo.h +++ b/core/undo_redo.h @@ -76,6 +76,7 @@ private: String name; List<Operation> do_ops; List<Operation> undo_ops; + uint64_t last_tick; }; Vector<Action> actions; diff --git a/core/ustring.cpp b/core/ustring.cpp index 0c26fe90c6..27bb8eac72 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -1547,20 +1547,20 @@ String::String(const StrRange& p_range) { int String::hex_to_int(bool p_with_prefix) const { - int l = length(); + int l = length(); if (p_with_prefix && l<3) return 0; - const CharType *s=ptr(); + const CharType *s=ptr(); - int sign = s[0]=='-' ? -1 : 1; + int sign = s[0]=='-' ? -1 : 1; - if (sign<0) { - s++; - l--; + if (sign<0) { + s++; + l--; if (p_with_prefix && l<2) - return 0; - } + return 0; + } if (p_with_prefix) { if (s[0]!='0' || s[1]!='x') @@ -1569,26 +1569,74 @@ int String::hex_to_int(bool p_with_prefix) const { l-=2; }; - int hex=0; + int hex=0; - while(*s) { + while(*s) { - CharType c = LOWERCASE(*s); - int n; - if (c>='0' && c<='9') { - n=c-'0'; - } else if (c>='a' && c<='f') { - n=(c-'a')+10; - } else { - return 0; - } + CharType c = LOWERCASE(*s); + int n; + if (c>='0' && c<='9') { + n=c-'0'; + } else if (c>='a' && c<='f') { + n=(c-'a')+10; + } else { + return 0; + } - hex*=16; - hex+=n; - s++; - } + hex*=16; + hex+=n; + s++; + } + + return hex*sign; + +} + + +int64_t String::hex_to_int64(bool p_with_prefix) const { + + int l = length(); + if (p_with_prefix && l<3) + return 0; + + const CharType *s=ptr(); + + int64_t sign = s[0]=='-' ? -1 : 1; + + if (sign<0) { + s++; + l--; + if (p_with_prefix && l<2) + return 0; + } + + if (p_with_prefix) { + if (s[0]!='0' || s[1]!='x') + return 0; + s+=2; + l-=2; + }; + + int64_t hex=0; + + while(*s) { + + CharType c = LOWERCASE(*s); + int64_t n; + if (c>='0' && c<='9') { + n=c-'0'; + } else if (c>='a' && c<='f') { + n=(c-'a')+10; + } else { + return 0; + } + + hex*=16; + hex+=n; + s++; + } - return hex*sign; + return hex*sign; } diff --git a/core/ustring.h b/core/ustring.h index 8b008c2ba4..9a145143d0 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -147,6 +147,7 @@ public: int hex_to_int(bool p_with_prefix = true) const; int to_int() const; + int64_t hex_to_int64(bool p_with_prefix = true) const; int64_t to_int64() const; static int to_int(const char* p_str, int p_len=-1); static double to_double(const char* p_str); diff --git a/core/variant.cpp b/core/variant.cpp index 19b0ea7129..69160fffa7 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -842,37 +842,37 @@ bool Variant::is_zero() const { // arrays case RAW_ARRAY: { - return reinterpret_cast<const DVector<uint8_t>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<uint8_t>*>(_data._mem)->size()==0; } break; case INT_ARRAY: { - return reinterpret_cast<const DVector<int>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<int>*>(_data._mem)->size()==0; } break; case REAL_ARRAY: { - return reinterpret_cast<const DVector<real_t>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<real_t>*>(_data._mem)->size()==0; } break; case STRING_ARRAY: { - return reinterpret_cast<const DVector<String>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<String>*>(_data._mem)->size()==0; } break; case VECTOR2_ARRAY: { - return reinterpret_cast<const DVector<Vector2>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<Vector2>*>(_data._mem)->size()==0; } break; case VECTOR3_ARRAY: { - return reinterpret_cast<const DVector<Vector3>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<Vector3>*>(_data._mem)->size()==0; } break; case COLOR_ARRAY: { - return reinterpret_cast<const DVector<Color>*>(_data._mem)->size()==0; + return reinterpret_cast<const PoolVector<Color>*>(_data._mem)->size()==0; } break; default: {} @@ -1070,37 +1070,37 @@ void Variant::reference(const Variant& p_variant) { // arrays case RAW_ARRAY: { - memnew_placement( _data._mem, DVector<uint8_t> ( *reinterpret_cast<const DVector<uint8_t>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<uint8_t> ( *reinterpret_cast<const PoolVector<uint8_t>*>(p_variant._data._mem) ) ); } break; case INT_ARRAY: { - memnew_placement( _data._mem, DVector<int> ( *reinterpret_cast<const DVector<int>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<int> ( *reinterpret_cast<const PoolVector<int>*>(p_variant._data._mem) ) ); } break; case REAL_ARRAY: { - memnew_placement( _data._mem, DVector<real_t> ( *reinterpret_cast<const DVector<real_t>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<real_t> ( *reinterpret_cast<const PoolVector<real_t>*>(p_variant._data._mem) ) ); } break; case STRING_ARRAY: { - memnew_placement( _data._mem, DVector<String> ( *reinterpret_cast<const DVector<String>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<String> ( *reinterpret_cast<const PoolVector<String>*>(p_variant._data._mem) ) ); } break; case VECTOR2_ARRAY: { - memnew_placement( _data._mem, DVector<Vector2> ( *reinterpret_cast<const DVector<Vector2>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<Vector2> ( *reinterpret_cast<const PoolVector<Vector2>*>(p_variant._data._mem) ) ); } break; case VECTOR3_ARRAY: { - memnew_placement( _data._mem, DVector<Vector3> ( *reinterpret_cast<const DVector<Vector3>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<Vector3> ( *reinterpret_cast<const PoolVector<Vector3>*>(p_variant._data._mem) ) ); } break; case COLOR_ARRAY: { - memnew_placement( _data._mem, DVector<Color> ( *reinterpret_cast<const DVector<Color>*>(p_variant._data._mem) ) ); + memnew_placement( _data._mem, PoolVector<Color> ( *reinterpret_cast<const PoolVector<Color>*>(p_variant._data._mem) ) ); } break; default: {} @@ -1198,37 +1198,37 @@ void Variant::clear() { // arrays case RAW_ARRAY: { - reinterpret_cast< DVector<uint8_t>* >(_data._mem)->~DVector<uint8_t>(); + reinterpret_cast< PoolVector<uint8_t>* >(_data._mem)->~PoolVector<uint8_t>(); } break; case INT_ARRAY: { - reinterpret_cast< DVector<int>* >(_data._mem)->~DVector<int>(); + reinterpret_cast< PoolVector<int>* >(_data._mem)->~PoolVector<int>(); } break; case REAL_ARRAY: { - reinterpret_cast< DVector<real_t>* >(_data._mem)->~DVector<real_t>(); + reinterpret_cast< PoolVector<real_t>* >(_data._mem)->~PoolVector<real_t>(); } break; case STRING_ARRAY: { - reinterpret_cast< DVector<String>* >(_data._mem)->~DVector<String>(); + reinterpret_cast< PoolVector<String>* >(_data._mem)->~PoolVector<String>(); } break; case VECTOR2_ARRAY: { - reinterpret_cast< DVector<Vector2>* >(_data._mem)->~DVector<Vector2>(); + reinterpret_cast< PoolVector<Vector2>* >(_data._mem)->~PoolVector<Vector2>(); } break; case VECTOR3_ARRAY: { - reinterpret_cast< DVector<Vector3>* >(_data._mem)->~DVector<Vector3>(); + reinterpret_cast< PoolVector<Vector3>* >(_data._mem)->~PoolVector<Vector3>(); } break; case COLOR_ARRAY: { - reinterpret_cast< DVector<Color>* >(_data._mem)->~DVector<Color>(); + reinterpret_cast< PoolVector<Color>* >(_data._mem)->~PoolVector<Color>(); } break; default: {} /* not needed */ @@ -1467,7 +1467,7 @@ Variant::operator double() const { case NIL: return 0; case BOOL: return _data._bool ? 1.0 : 0.0; - case INT: return (float)_data._int; + case INT: return (double)_data._int; case REAL: return _data._real; case STRING: return operator String().to_double(); default: { @@ -1502,10 +1502,10 @@ Variant::operator String() const { switch( type ) { - case NIL: return ""; + case NIL: return "Null"; case BOOL: return _data._bool ? "True" : "False"; - case INT: return String::num(_data._int); - case REAL: return String::num(_data._real); + case INT: return itos(_data._int); + case REAL: return rtos(_data._real); case STRING: return *reinterpret_cast<const String*>(_data._mem); case VECTOR2: return "("+operator Vector2()+")"; case RECT2: return "("+operator Rect2()+")"; @@ -1578,7 +1578,7 @@ Variant::operator String() const { } break; case VECTOR2_ARRAY: { - DVector<Vector2> vec = operator DVector<Vector2>(); + PoolVector<Vector2> vec = operator PoolVector<Vector2>(); String str("["); for(int i=0;i<vec.size();i++) { @@ -1591,7 +1591,7 @@ Variant::operator String() const { } break; case VECTOR3_ARRAY: { - DVector<Vector3> vec = operator DVector<Vector3>(); + PoolVector<Vector3> vec = operator PoolVector<Vector3>(); String str("["); for(int i=0;i<vec.size();i++) { @@ -1604,7 +1604,7 @@ Variant::operator String() const { } break; case STRING_ARRAY: { - DVector<String> vec = operator DVector<String>(); + PoolVector<String> vec = operator PoolVector<String>(); String str("["); for(int i=0;i<vec.size();i++) { @@ -1617,7 +1617,7 @@ Variant::operator String() const { } break; case INT_ARRAY: { - DVector<int> vec = operator DVector<int>(); + PoolVector<int> vec = operator PoolVector<int>(); String str("["); for(int i=0;i<vec.size();i++) { @@ -1630,7 +1630,7 @@ Variant::operator String() const { } break; case REAL_ARRAY: { - DVector<real_t> vec = operator DVector<real_t>(); + PoolVector<real_t> vec = operator PoolVector<real_t>(); String str("["); for(int i=0;i<vec.size();i++) { @@ -1893,13 +1893,13 @@ inline DA _convert_array_from_variant(const Variant& p_variant) { case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); } - case Variant::RAW_ARRAY: { return _convert_array<DA,DVector<uint8_t> >( p_variant.operator DVector<uint8_t> () ); } - case Variant::INT_ARRAY: { return _convert_array<DA,DVector<int> >( p_variant.operator DVector<int> () ); } - case Variant::REAL_ARRAY: { return _convert_array<DA,DVector<real_t> >( p_variant.operator DVector<real_t> () ); } - case Variant::STRING_ARRAY: { return _convert_array<DA,DVector<String> >( p_variant.operator DVector<String> () ); } - case Variant::VECTOR2_ARRAY: { return _convert_array<DA,DVector<Vector2> >( p_variant.operator DVector<Vector2> () ); } - case Variant::VECTOR3_ARRAY: { return _convert_array<DA,DVector<Vector3> >( p_variant.operator DVector<Vector3> () ); } - case Variant::COLOR_ARRAY: { return _convert_array<DA,DVector<Color> >( p_variant.operator DVector<Color>() ); } + case Variant::RAW_ARRAY: { return _convert_array<DA,PoolVector<uint8_t> >( p_variant.operator PoolVector<uint8_t> () ); } + case Variant::INT_ARRAY: { return _convert_array<DA,PoolVector<int> >( p_variant.operator PoolVector<int> () ); } + case Variant::REAL_ARRAY: { return _convert_array<DA,PoolVector<real_t> >( p_variant.operator PoolVector<real_t> () ); } + case Variant::STRING_ARRAY: { return _convert_array<DA,PoolVector<String> >( p_variant.operator PoolVector<String> () ); } + case Variant::VECTOR2_ARRAY: { return _convert_array<DA,PoolVector<Vector2> >( p_variant.operator PoolVector<Vector2> () ); } + case Variant::VECTOR3_ARRAY: { return _convert_array<DA,PoolVector<Vector3> >( p_variant.operator PoolVector<Vector3> () ); } + case Variant::COLOR_ARRAY: { return _convert_array<DA,PoolVector<Color> >( p_variant.operator PoolVector<Color>() ); } default: { return DA(); } } @@ -1914,64 +1914,64 @@ Variant::operator Array() const { return _convert_array_from_variant<Array >(*this); } -Variant::operator DVector<uint8_t>() const { +Variant::operator PoolVector<uint8_t>() const { if (type==RAW_ARRAY) - return *reinterpret_cast<const DVector<uint8_t>* >(_data._mem); + return *reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem); else - return _convert_array_from_variant<DVector<uint8_t> >(*this); + return _convert_array_from_variant<PoolVector<uint8_t> >(*this); } -Variant::operator DVector<int>() const { +Variant::operator PoolVector<int>() const { if (type==INT_ARRAY) - return *reinterpret_cast<const DVector<int>* >(_data._mem); + return *reinterpret_cast<const PoolVector<int>* >(_data._mem); else - return _convert_array_from_variant<DVector<int> >(*this); + return _convert_array_from_variant<PoolVector<int> >(*this); } -Variant::operator DVector<real_t>() const { +Variant::operator PoolVector<real_t>() const { if (type==REAL_ARRAY) - return *reinterpret_cast<const DVector<real_t>* >(_data._mem); + return *reinterpret_cast<const PoolVector<real_t>* >(_data._mem); else - return _convert_array_from_variant<DVector<real_t> >(*this); + return _convert_array_from_variant<PoolVector<real_t> >(*this); } -Variant::operator DVector<String>() const { +Variant::operator PoolVector<String>() const { if (type==STRING_ARRAY) - return *reinterpret_cast<const DVector<String>* >(_data._mem); + return *reinterpret_cast<const PoolVector<String>* >(_data._mem); else - return _convert_array_from_variant<DVector<String> >(*this); + return _convert_array_from_variant<PoolVector<String> >(*this); } -Variant::operator DVector<Vector3>() const { +Variant::operator PoolVector<Vector3>() const { if (type==VECTOR3_ARRAY) - return *reinterpret_cast<const DVector<Vector3>* >(_data._mem); + return *reinterpret_cast<const PoolVector<Vector3>* >(_data._mem); else - return _convert_array_from_variant<DVector<Vector3> >(*this); + return _convert_array_from_variant<PoolVector<Vector3> >(*this); } -Variant::operator DVector<Vector2>() const { +Variant::operator PoolVector<Vector2>() const { if (type==VECTOR2_ARRAY) - return *reinterpret_cast<const DVector<Vector2>* >(_data._mem); + return *reinterpret_cast<const PoolVector<Vector2>* >(_data._mem); else - return _convert_array_from_variant<DVector<Vector2> >(*this); + return _convert_array_from_variant<PoolVector<Vector2> >(*this); } -Variant::operator DVector<Color>() const { +Variant::operator PoolVector<Color>() const { if (type==COLOR_ARRAY) - return *reinterpret_cast<const DVector<Color>* >(_data._mem); + return *reinterpret_cast<const PoolVector<Color>* >(_data._mem); else - return _convert_array_from_variant<DVector<Color> >(*this); + return _convert_array_from_variant<PoolVector<Color> >(*this); } @@ -1990,13 +1990,13 @@ Variant::operator Vector<RID>() const { Variant::operator Vector<Vector2>() const { - DVector<Vector2> from=operator DVector<Vector2>(); + PoolVector<Vector2> from=operator PoolVector<Vector2>(); Vector<Vector2> to; int len=from.size(); if (len==0) return Vector<Vector2>(); to.resize(len); - DVector<Vector2>::Read r = from.read(); + PoolVector<Vector2>::Read r = from.read(); Vector2 *w = &to[0]; for (int i=0;i<len;i++) { @@ -2005,16 +2005,16 @@ Variant::operator Vector<Vector2>() const { return to; } -Variant::operator DVector<Plane>() const { +Variant::operator PoolVector<Plane>() const { Array va= operator Array(); - DVector<Plane> planes; + PoolVector<Plane> planes; int va_size=va.size(); if (va_size==0) return planes; planes.resize(va_size); - DVector<Plane>::Write w = planes.write(); + PoolVector<Plane>::Write w = planes.write(); for(int i=0;i<va_size;i++) w[i]=va[i]; @@ -2022,17 +2022,17 @@ Variant::operator DVector<Plane>() const { return planes; } -Variant::operator DVector<Face3>() const { +Variant::operator PoolVector<Face3>() const { - DVector<Vector3> va= operator DVector<Vector3>(); - DVector<Face3> faces; + PoolVector<Vector3> va= operator PoolVector<Vector3>(); + PoolVector<Face3> faces; int va_size=va.size(); if (va_size==0) return faces; faces.resize(va_size/3); - DVector<Face3>::Write w = faces.write(); - DVector<Vector3>::Read r = va.read(); + PoolVector<Face3>::Write w = faces.write(); + PoolVector<Vector3>::Read r = va.read(); for(int i=0;i<va_size;i++) w[i/3].vertex[i%3]=r[i]; @@ -2072,7 +2072,7 @@ Variant::operator Vector<Variant>() const { Variant::operator Vector<uint8_t>() const { - DVector<uint8_t> from=operator DVector<uint8_t>(); + PoolVector<uint8_t> from=operator PoolVector<uint8_t>(); Vector<uint8_t> to; int len=from.size(); to.resize(len); @@ -2084,7 +2084,7 @@ Variant::operator Vector<uint8_t>() const { } Variant::operator Vector<int>() const { - DVector<int> from=operator DVector<int>(); + PoolVector<int> from=operator PoolVector<int>(); Vector<int> to; int len=from.size(); to.resize(len); @@ -2096,7 +2096,7 @@ Variant::operator Vector<int>() const { } Variant::operator Vector<real_t>() const { - DVector<real_t> from=operator DVector<real_t>(); + PoolVector<real_t> from=operator PoolVector<real_t>(); Vector<real_t> to; int len=from.size(); to.resize(len); @@ -2109,7 +2109,7 @@ Variant::operator Vector<real_t>() const { Variant::operator Vector<String>() const { - DVector<String> from=operator DVector<String>(); + PoolVector<String> from=operator PoolVector<String>(); Vector<String> to; int len=from.size(); to.resize(len); @@ -2122,13 +2122,13 @@ Variant::operator Vector<String>() const { } Variant::operator Vector<Vector3>() const { - DVector<Vector3> from=operator DVector<Vector3>(); + PoolVector<Vector3> from=operator PoolVector<Vector3>(); Vector<Vector3> to; int len=from.size(); if (len==0) return Vector<Vector3>(); to.resize(len); - DVector<Vector3>::Read r = from.read(); + PoolVector<Vector3>::Read r = from.read(); Vector3 *w = &to[0]; for (int i=0;i<len;i++) { @@ -2139,13 +2139,13 @@ Variant::operator Vector<Vector3>() const { } Variant::operator Vector<Color>() const { - DVector<Color> from=operator DVector<Color>(); + PoolVector<Color> from=operator PoolVector<Color>(); Vector<Color> to; int len=from.size(); if (len==0) return Vector<Color>(); to.resize(len); - DVector<Color>::Read r = from.read(); + PoolVector<Color>::Read r = from.read(); Color *w = &to[0]; for (int i=0;i<len;i++) { @@ -2167,7 +2167,7 @@ Variant::operator IP_Address() const { if (type==REAL_ARRAY || type==INT_ARRAY || type==RAW_ARRAY) { - DVector<int> addr=operator DVector<int>(); + PoolVector<int> addr=operator PoolVector<int>(); if (addr.size()==4) { return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3)); } @@ -2418,7 +2418,7 @@ Variant::Variant(const Array& p_array) { } -Variant::Variant(const DVector<Plane>& p_array) { +Variant::Variant(const PoolVector<Plane>& p_array) { type=ARRAY; @@ -2467,11 +2467,11 @@ Variant::Variant(const Vector<Vector2>& p_array) { type=NIL; - DVector<Vector2> v; + PoolVector<Vector2> v; int len=p_array.size(); if (len>0) { v.resize(len); - DVector<Vector2>::Write w = v.write(); + PoolVector<Vector2>::Write w = v.write(); const Vector2 *r = p_array.ptr(); for (int i=0;i<len;i++) @@ -2481,59 +2481,59 @@ Variant::Variant(const Vector<Vector2>& p_array) { } -Variant::Variant(const DVector<uint8_t>& p_raw_array) { +Variant::Variant(const PoolVector<uint8_t>& p_raw_array) { type=RAW_ARRAY; - memnew_placement( _data._mem, DVector<uint8_t>(p_raw_array) ); + memnew_placement( _data._mem, PoolVector<uint8_t>(p_raw_array) ); } -Variant::Variant(const DVector<int>& p_int_array) { +Variant::Variant(const PoolVector<int>& p_int_array) { type=INT_ARRAY; - memnew_placement( _data._mem, DVector<int>(p_int_array) ); + memnew_placement( _data._mem, PoolVector<int>(p_int_array) ); } -Variant::Variant(const DVector<real_t>& p_real_array) { +Variant::Variant(const PoolVector<real_t>& p_real_array) { type=REAL_ARRAY; - memnew_placement( _data._mem, DVector<real_t>(p_real_array) ); + memnew_placement( _data._mem, PoolVector<real_t>(p_real_array) ); } -Variant::Variant(const DVector<String>& p_string_array) { +Variant::Variant(const PoolVector<String>& p_string_array) { type=STRING_ARRAY; - memnew_placement( _data._mem, DVector<String>(p_string_array) ); + memnew_placement( _data._mem, PoolVector<String>(p_string_array) ); } -Variant::Variant(const DVector<Vector3>& p_vector3_array) { +Variant::Variant(const PoolVector<Vector3>& p_vector3_array) { type=VECTOR3_ARRAY; - memnew_placement( _data._mem, DVector<Vector3>(p_vector3_array) ); + memnew_placement( _data._mem, PoolVector<Vector3>(p_vector3_array) ); } -Variant::Variant(const DVector<Vector2>& p_vector2_array) { +Variant::Variant(const PoolVector<Vector2>& p_vector2_array) { type=VECTOR2_ARRAY; - memnew_placement( _data._mem, DVector<Vector2>(p_vector2_array) ); + memnew_placement( _data._mem, PoolVector<Vector2>(p_vector2_array) ); } -Variant::Variant(const DVector<Color>& p_color_array) { +Variant::Variant(const PoolVector<Color>& p_color_array) { type=COLOR_ARRAY; - memnew_placement( _data._mem, DVector<Color>(p_color_array) ); + memnew_placement( _data._mem, PoolVector<Color>(p_color_array) ); } -Variant::Variant(const DVector<Face3>& p_face_array) { +Variant::Variant(const PoolVector<Face3>& p_face_array) { - DVector<Vector3> vertices; + PoolVector<Vector3> vertices; int face_count=p_face_array.size(); vertices.resize(face_count*3); if (face_count) { - DVector<Face3>::Read r = p_face_array.read(); - DVector<Vector3>::Write w = vertices.write(); + PoolVector<Face3>::Read r = p_face_array.read(); + PoolVector<Vector3>::Write w = vertices.write(); for(int i=0;i<face_count;i++) { @@ -2541,8 +2541,8 @@ Variant::Variant(const DVector<Face3>& p_face_array) { w[i*3+j]=r[i].vertex[j]; } - r=DVector<Face3>::Read(); - w=DVector<Vector3>::Write(); + r=PoolVector<Face3>::Read(); + w=PoolVector<Vector3>::Write(); } @@ -2567,7 +2567,7 @@ Variant::Variant(const Vector<Variant>& p_array) { Variant::Variant(const Vector<uint8_t>& p_array) { type=NIL; - DVector<uint8_t> v; + PoolVector<uint8_t> v; int len=p_array.size(); v.resize(len); for (int i=0;i<len;i++) @@ -2578,7 +2578,7 @@ Variant::Variant(const Vector<uint8_t>& p_array) { Variant::Variant(const Vector<int>& p_array) { type=NIL; - DVector<int> v; + PoolVector<int> v; int len=p_array.size(); v.resize(len); for (int i=0;i<len;i++) @@ -2589,7 +2589,7 @@ Variant::Variant(const Vector<int>& p_array) { Variant::Variant(const Vector<real_t>& p_array) { type=NIL; - DVector<real_t> v; + PoolVector<real_t> v; int len=p_array.size(); v.resize(len); for (int i=0;i<len;i++) @@ -2600,7 +2600,7 @@ Variant::Variant(const Vector<real_t>& p_array) { Variant::Variant(const Vector<String>& p_array) { type=NIL; - DVector<String> v; + PoolVector<String> v; int len=p_array.size(); v.resize(len); for (int i=0;i<len;i++) @@ -2611,11 +2611,11 @@ Variant::Variant(const Vector<String>& p_array) { Variant::Variant(const Vector<Vector3>& p_array) { type=NIL; - DVector<Vector3> v; + PoolVector<Vector3> v; int len=p_array.size(); if (len>0) { v.resize(len); - DVector<Vector3>::Write w = v.write(); + PoolVector<Vector3>::Write w = v.write(); const Vector3 *r = p_array.ptr(); for (int i=0;i<len;i++) @@ -2627,7 +2627,7 @@ Variant::Variant(const Vector<Vector3>& p_array) { Variant::Variant(const Vector<Color>& p_array) { type=NIL; - DVector<Color> v; + PoolVector<Color> v; int len=p_array.size(); v.resize(len); for (int i=0;i<len;i++) @@ -2826,27 +2826,27 @@ uint32_t Variant::hash() const { } break; case RAW_ARRAY: { - const DVector<uint8_t>& arr = *reinterpret_cast<const DVector<uint8_t>* >(_data._mem); + const PoolVector<uint8_t>& arr = *reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem); int len = arr.size(); - DVector<uint8_t>::Read r = arr.read(); + PoolVector<uint8_t>::Read r = arr.read(); return hash_djb2_buffer((uint8_t*)&r[0],len); } break; case INT_ARRAY: { - const DVector<int>& arr = *reinterpret_cast<const DVector<int>* >(_data._mem); + const PoolVector<int>& arr = *reinterpret_cast<const PoolVector<int>* >(_data._mem); int len = arr.size(); - DVector<int>::Read r = arr.read(); + PoolVector<int>::Read r = arr.read(); return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int)); } break; case REAL_ARRAY: { - const DVector<real_t>& arr = *reinterpret_cast<const DVector<real_t>* >(_data._mem); + const PoolVector<real_t>& arr = *reinterpret_cast<const PoolVector<real_t>* >(_data._mem); int len = arr.size(); - DVector<real_t>::Read r = arr.read(); + PoolVector<real_t>::Read r = arr.read(); return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t)); @@ -2854,9 +2854,9 @@ uint32_t Variant::hash() const { case STRING_ARRAY: { uint32_t hash=5831; - const DVector<String>& arr = *reinterpret_cast<const DVector<String>* >(_data._mem); + const PoolVector<String>& arr = *reinterpret_cast<const PoolVector<String>* >(_data._mem); int len = arr.size(); - DVector<String>::Read r = arr.read(); + PoolVector<String>::Read r = arr.read(); for(int i=0;i<len;i++) { hash = hash_djb2_one_32(r[i].hash(),hash); @@ -2867,9 +2867,9 @@ uint32_t Variant::hash() const { case VECTOR2_ARRAY: { uint32_t hash=5831; - const DVector<Vector2>& arr = *reinterpret_cast<const DVector<Vector2>* >(_data._mem); + const PoolVector<Vector2>& arr = *reinterpret_cast<const PoolVector<Vector2>* >(_data._mem); int len = arr.size(); - DVector<Vector2>::Read r = arr.read(); + PoolVector<Vector2>::Read r = arr.read(); for(int i=0;i<len;i++) { hash = hash_djb2_one_float(r[i].x,hash); @@ -2882,9 +2882,9 @@ uint32_t Variant::hash() const { case VECTOR3_ARRAY: { uint32_t hash=5831; - const DVector<Vector3>& arr = *reinterpret_cast<const DVector<Vector3>* >(_data._mem); + const PoolVector<Vector3>& arr = *reinterpret_cast<const PoolVector<Vector3>* >(_data._mem); int len = arr.size(); - DVector<Vector3>::Read r = arr.read(); + PoolVector<Vector3>::Read r = arr.read(); for(int i=0;i<len;i++) { hash = hash_djb2_one_float(r[i].x,hash); @@ -2898,9 +2898,9 @@ uint32_t Variant::hash() const { case COLOR_ARRAY: { uint32_t hash=5831; - const DVector<Color>& arr = *reinterpret_cast<const DVector<Color>* >(_data._mem); + const PoolVector<Color>& arr = *reinterpret_cast<const PoolVector<Color>* >(_data._mem); int len = arr.size(); - DVector<Color>::Read r = arr.read(); + PoolVector<Color>::Read r = arr.read(); for(int i=0;i<len;i++) { hash = hash_djb2_one_float(r[i].r,hash); diff --git a/core/variant.h b/core/variant.h index f4bb3b6c8b..764ba9ae60 100644 --- a/core/variant.h +++ b/core/variant.h @@ -63,13 +63,13 @@ struct PropertyInfo; struct MethodInfo; -typedef DVector<uint8_t> ByteArray; -typedef DVector<int> IntArray; -typedef DVector<real_t> RealArray; -typedef DVector<String> StringArray; -typedef DVector<Vector2> Vector2Array; -typedef DVector<Vector3> Vector3Array; -typedef DVector<Color> ColorArray; +typedef PoolVector<uint8_t> ByteArray; +typedef PoolVector<int> IntArray; +typedef PoolVector<real_t> RealArray; +typedef PoolVector<String> StringArray; +typedef PoolVector<Vector2> Vector2Array; +typedef PoolVector<Vector3> Vector3Array; +typedef PoolVector<Color> ColorArray; class Variant { public: @@ -141,7 +141,7 @@ private: union { bool _bool; - int _int; + int64_t _int; double _real; Matrix32 *_matrix32; AABB* _aabb; @@ -227,14 +227,14 @@ public: operator Dictionary() const; operator Array() const; - operator DVector<uint8_t>() const; - operator DVector<int>() const; - operator DVector<real_t>() const; - operator DVector<String>() const; - operator DVector<Vector3>() const; - operator DVector<Color>() const; - operator DVector<Plane>() const; - operator DVector<Face3>() const; + operator PoolVector<uint8_t>() const; + operator PoolVector<int>() const; + operator PoolVector<real_t>() const; + operator PoolVector<String>() const; + operator PoolVector<Vector3>() const; + operator PoolVector<Color>() const; + operator PoolVector<Plane>() const; + operator PoolVector<Face3>() const; operator Vector<Variant>() const; @@ -246,7 +246,7 @@ public: operator Vector<Color>() const; operator Vector<RID>() const; operator Vector<Vector2>() const; - operator DVector<Vector2>() const; + operator PoolVector<Vector2>() const; operator Vector<Plane>() const; // some core type enums to convert to @@ -295,14 +295,14 @@ public: Variant(const Dictionary& p_dictionary); Variant(const Array& p_array); - Variant(const DVector<Plane>& p_array); // helper - Variant(const DVector<uint8_t>& p_raw_array); - Variant(const DVector<int>& p_int_array); - Variant(const DVector<real_t>& p_real_array); - Variant(const DVector<String>& p_string_array); - Variant(const DVector<Vector3>& p_vector3_array); - Variant(const DVector<Color>& p_color_array); - Variant(const DVector<Face3>& p_face_array); + Variant(const PoolVector<Plane>& p_array); // helper + Variant(const PoolVector<uint8_t>& p_raw_array); + Variant(const PoolVector<int>& p_int_array); + Variant(const PoolVector<real_t>& p_real_array); + Variant(const PoolVector<String>& p_string_array); + Variant(const PoolVector<Vector3>& p_vector3_array); + Variant(const PoolVector<Color>& p_color_array); + Variant(const PoolVector<Face3>& p_face_array); Variant(const Vector<Variant>& p_array); @@ -315,7 +315,7 @@ public: Variant(const Vector<Plane>& p_array); // helper Variant(const Vector<RID>& p_array); // helper Variant(const Vector<Vector2>& p_array); // helper - Variant(const DVector<Vector2>& p_array); // helper + Variant(const PoolVector<Vector2>& p_array); // helper Variant(const IP_Address& p_address); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index cedf21b377..6aa37601a0 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -32,6 +32,7 @@ #include "core_string_names.h" #include "script_language.h" + typedef void (*VariantFunc)(Variant& r_ret,Variant& p_self,const Variant** p_args); typedef void (*VariantConstructFunc)(Variant& r_ret,const Variant** p_args); @@ -146,6 +147,15 @@ struct _VariantCall { }; // void addfunc(Variant::Type p_type, const StringName& p_name,VariantFunc p_func); + + static void make_func_return_variant(Variant::Type p_type,const StringName& p_name) { + +#ifdef DEBUG_ENABLED + type_funcs[p_type].functions[p_name].returns=true; +#endif + } + + static void addfunc(Variant::Type p_type, Variant::Type p_return,const StringName& p_name,VariantFunc p_func, const Vector<Variant>& p_defaultarg,const Arg& p_argtype1=Arg(),const Arg& p_argtype2=Arg(),const Arg& p_argtype3=Arg(),const Arg& p_argtype4=Arg(),const Arg& p_argtype5=Arg()) { FuncData funcdata; @@ -305,7 +315,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var retval.resize(len); ByteArray::Write w = retval.write(); copymem(w.ptr(), charstr.ptr(), len); - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); r_ret = retval; } @@ -320,7 +330,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var retval.resize(len); ByteArray::Write w = retval.write(); copymem(w.ptr(), charstr.ptr(), len); - w = DVector<uint8_t>::Write(); + w = PoolVector<uint8_t>::Write(); r_ret = retval; } @@ -413,10 +423,6 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var r_ret=Variant(); } - static void _call_Vector2_floorf(Variant& r_ret,Variant& p_self,const Variant** p_args) { - r_ret = reinterpret_cast<Vector2*>(p_self._data._mem)->floor(); - }; - VCALL_LOCALMEM0R(Quat,length); VCALL_LOCALMEM0R(Quat,length_squared); VCALL_LOCALMEM0R(Quat,normalized); @@ -455,8 +461,6 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM0R(Dictionary,hash); VCALL_LOCALMEM0R(Dictionary,keys); VCALL_LOCALMEM0R(Dictionary,values); - VCALL_LOCALMEM1R(Dictionary,parse_json); - VCALL_LOCALMEM0R(Dictionary,to_json); VCALL_LOCALMEM2(Array,set); VCALL_LOCALMEM1R(Array,get); @@ -1436,6 +1440,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC0(STRING,RAW_ARRAY,String,to_utf8,varray()); + ADDFUNC0(VECTOR2,VECTOR2,Vector2,normalized,varray()); ADDFUNC0(VECTOR2,REAL,Vector2,length,varray()); ADDFUNC0(VECTOR2,REAL,Vector2,angle,varray()); @@ -1449,7 +1454,6 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC1(VECTOR2,VECTOR2,Vector2,rotated,REAL,"phi",varray()); ADDFUNC0(VECTOR2,VECTOR2,Vector2,tangent,varray()); ADDFUNC0(VECTOR2,VECTOR2,Vector2,floor,varray()); - ADDFUNC0(VECTOR2,VECTOR2,Vector2,floorf,varray()); ADDFUNC1(VECTOR2,VECTOR2,Vector2,snapped,VECTOR2,"by",varray()); ADDFUNC0(VECTOR2,REAL,Vector2,get_aspect,varray()); ADDFUNC1(VECTOR2,REAL,Vector2,dot,VECTOR2,"with",varray()); @@ -1556,9 +1560,6 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC0(DICTIONARY,ARRAY,Dictionary,keys,varray()); ADDFUNC0(DICTIONARY,ARRAY,Dictionary,values,varray()); - ADDFUNC1(DICTIONARY,INT,Dictionary,parse_json,STRING,"json",varray()); - ADDFUNC0(DICTIONARY,STRING,Dictionary,to_json,varray()); - ADDFUNC0(ARRAY,INT,Array,size,varray()); ADDFUNC0(ARRAY,BOOL,Array,empty,varray()); ADDFUNC0(ARRAY,NIL,Array,clear,varray()); @@ -1777,8 +1778,8 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl _VariantCall::add_constant(Variant::INPUT_EVENT,"KEY",InputEvent::KEY); _VariantCall::add_constant(Variant::INPUT_EVENT,"MOUSE_MOTION",InputEvent::MOUSE_MOTION); _VariantCall::add_constant(Variant::INPUT_EVENT,"MOUSE_BUTTON",InputEvent::MOUSE_BUTTON); - _VariantCall::add_constant(Variant::INPUT_EVENT,"JOYSTICK_MOTION",InputEvent::JOYSTICK_MOTION); - _VariantCall::add_constant(Variant::INPUT_EVENT,"JOYSTICK_BUTTON",InputEvent::JOYSTICK_BUTTON); + _VariantCall::add_constant(Variant::INPUT_EVENT,"JOYPAD_MOTION",InputEvent::JOYPAD_MOTION); + _VariantCall::add_constant(Variant::INPUT_EVENT,"JOYPAD_BUTTON",InputEvent::JOYPAD_BUTTON); _VariantCall::add_constant(Variant::INPUT_EVENT,"SCREEN_TOUCH",InputEvent::SCREEN_TOUCH); _VariantCall::add_constant(Variant::INPUT_EVENT,"SCREEN_DRAG",InputEvent::SCREEN_DRAG); _VariantCall::add_constant(Variant::INPUT_EVENT,"ACTION",InputEvent::ACTION); diff --git a/core/variant_op.cpp b/core/variant_op.cpp index 94a2ea9977..00baffdd3b 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -186,16 +186,16 @@ case m_name: { \ r_valid=false;\ return;\ }\ - const DVector<m_type> &array_a=*reinterpret_cast<const DVector<m_type> *>(p_a._data._mem);\ - const DVector<m_type> &array_b=*reinterpret_cast<const DVector<m_type> *>(p_b._data._mem);\ + const PoolVector<m_type> &array_a=*reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem);\ + const PoolVector<m_type> &array_b=*reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem);\ \ int a_len = array_a.size();\ if (a_len m_opa array_b.size()){\ _RETURN( m_ret_s);\ }else {\ \ - DVector<m_type>::Read ra = array_a.read();\ - DVector<m_type>::Read rb = array_b.read();\ + PoolVector<m_type>::Read ra = array_a.read();\ + PoolVector<m_type>::Read rb = array_b.read();\ \ for(int i=0;i<a_len;i++) {\ if (ra[i] m_opb rb[i])\ @@ -212,9 +212,9 @@ case m_name: { \ r_valid=false;\ _RETURN( NIL);\ }\ - const DVector<m_type> &array_a=*reinterpret_cast<const DVector<m_type> *>(p_a._data._mem);\ - const DVector<m_type> &array_b=*reinterpret_cast<const DVector<m_type> *>(p_b._data._mem);\ - DVector<m_type> sum = array_a;\ + const PoolVector<m_type> &array_a=*reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem);\ + const PoolVector<m_type> &array_b=*reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem);\ + PoolVector<m_type> sum = array_a;\ sum.append_array(array_b);\ _RETURN( sum );\ } @@ -683,7 +683,7 @@ void Variant::evaluate(const Operator& p_op, const Variant& p_a, const Variant& case INT: { switch(p_b.type) { case BOOL: { - int b = p_b._data._bool; + int64_t b = p_b._data._bool; if (b==0) { r_valid=false; @@ -693,7 +693,7 @@ void Variant::evaluate(const Operator& p_op, const Variant& p_a, const Variant& } break; case INT: { - int b = p_b._data._int; + int64_t b = p_b._data._int; if (b==0) { r_valid=false; @@ -1049,10 +1049,10 @@ Variant Variant::get_named(const StringName& p_index, bool *r_valid) const { } break; #define DEFAULT_OP_DVECTOR_SET(m_name, dv_type, skip_cond)\ - DEFAULT_OP_ARRAY_CMD(m_name, DVector<dv_type>, if(skip_cond) return;, arr->set(index, p_value);return) + DEFAULT_OP_ARRAY_CMD(m_name, PoolVector<dv_type>, if(skip_cond) return;, arr->set(index, p_value);return) #define DEFAULT_OP_DVECTOR_GET(m_name, dv_type)\ - DEFAULT_OP_ARRAY_CMD(m_name, const DVector<dv_type>, ;, return arr->get(index)) + DEFAULT_OP_ARRAY_CMD(m_name, const PoolVector<dv_type>, ;, return arr->get(index)) void Variant::set(const Variant& p_index, const Variant& p_value, bool *r_valid) { @@ -1744,7 +1744,7 @@ void Variant::set(const Variant& p_index, const Variant& p_value, bool *r_valid) } - if (ie.type==InputEvent::JOYSTICK_BUTTON) { + if (ie.type==InputEvent::JOYPAD_BUTTON) { if (str=="button_index") { if (p_value.type!=Variant::REAL && p_value.type!=Variant::INT) @@ -1769,7 +1769,7 @@ void Variant::set(const Variant& p_index, const Variant& p_value, bool *r_valid) } - if (ie.type==InputEvent::JOYSTICK_MOTION) { + if (ie.type==InputEvent::JOYPAD_MOTION) { if (str=="axis") { if (p_value.type!=Variant::REAL && p_value.type!=Variant::INT) @@ -2367,7 +2367,7 @@ Variant Variant::get(const Variant& p_index, bool *r_valid) const { } - if (ie.type==InputEvent::JOYSTICK_BUTTON) { + if (ie.type==InputEvent::JOYPAD_BUTTON) { if (str=="button_index") { valid=true; @@ -2382,7 +2382,7 @@ Variant Variant::get(const Variant& p_index, bool *r_valid) const { } - if (ie.type==InputEvent::JOYSTICK_MOTION) { + if (ie.type==InputEvent::JOYPAD_MOTION) { if (str=="axis") { valid=true; @@ -2561,10 +2561,10 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { int index = p_index; - const DVector<uint8_t> *arr=reinterpret_cast<const DVector<uint8_t>* >(_data._mem); + const PoolVector<uint8_t> *arr=reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem); int l=arr->size(); if (l) { - DVector<uint8_t>::Read r = arr->read(); + PoolVector<uint8_t>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2580,10 +2580,10 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { int index = p_index; - const DVector<int> *arr=reinterpret_cast<const DVector<int>* >(_data._mem); + const PoolVector<int> *arr=reinterpret_cast<const PoolVector<int>* >(_data._mem); int l=arr->size(); if (l) { - DVector<int>::Read r = arr->read(); + PoolVector<int>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2599,10 +2599,10 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { real_t index = p_index; - const DVector<real_t> *arr=reinterpret_cast<const DVector<real_t>* >(_data._mem); + const PoolVector<real_t> *arr=reinterpret_cast<const PoolVector<real_t>* >(_data._mem); int l=arr->size(); if (l) { - DVector<real_t>::Read r = arr->read(); + PoolVector<real_t>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2618,11 +2618,11 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::STRING) { String index = p_index; - const DVector<String> *arr=reinterpret_cast<const DVector<String>* >(_data._mem); + const PoolVector<String> *arr=reinterpret_cast<const PoolVector<String>* >(_data._mem); int l=arr->size(); if (l) { - DVector<String>::Read r = arr->read(); + PoolVector<String>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2638,11 +2638,11 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::VECTOR2) { Vector2 index = p_index; - const DVector<Vector2> *arr=reinterpret_cast<const DVector<Vector2>* >(_data._mem); + const PoolVector<Vector2> *arr=reinterpret_cast<const PoolVector<Vector2>* >(_data._mem); int l=arr->size(); if (l) { - DVector<Vector2>::Read r = arr->read(); + PoolVector<Vector2>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2658,11 +2658,11 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::VECTOR3) { Vector3 index = p_index; - const DVector<Vector3> *arr=reinterpret_cast<const DVector<Vector3>* >(_data._mem); + const PoolVector<Vector3> *arr=reinterpret_cast<const PoolVector<Vector3>* >(_data._mem); int l=arr->size(); if (l) { - DVector<Vector3>::Read r = arr->read(); + PoolVector<Vector3>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2679,12 +2679,12 @@ bool Variant::in(const Variant& p_index, bool *r_valid) const { if (p_index.get_type()==Variant::COLOR) { Color index = p_index; - const DVector<Color> *arr=reinterpret_cast<const DVector<Color>* >(_data._mem); + const PoolVector<Color> *arr=reinterpret_cast<const PoolVector<Color>* >(_data._mem); int l=arr->size(); if (l) { - DVector<Color>::Read r = arr->read(); + PoolVector<Color>::Read r = arr->read(); for(int i=0;i<l;i++) { if (r[i]==index) return true; @@ -2863,7 +2863,7 @@ void Variant::get_property_list(List<PropertyInfo> *p_list) const { } - if (ie.type==InputEvent::JOYSTICK_BUTTON) { + if (ie.type==InputEvent::JOYPAD_BUTTON) { p_list->push_back( PropertyInfo(Variant::INT,"button_index") ); p_list->push_back( PropertyInfo(Variant::BOOL,"pressed") ); @@ -2871,7 +2871,7 @@ void Variant::get_property_list(List<PropertyInfo> *p_list) const { } - if (ie.type==InputEvent::JOYSTICK_MOTION) { + if (ie.type==InputEvent::JOYPAD_MOTION) { p_list->push_back( PropertyInfo(Variant::INT,"axis") ); p_list->push_back( PropertyInfo(Variant::REAL,"value") ); @@ -2992,7 +2992,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { return true; } break; case RAW_ARRAY: { - const DVector<uint8_t> *arr=reinterpret_cast<const DVector<uint8_t>*>(_data._mem); + const PoolVector<uint8_t> *arr=reinterpret_cast<const PoolVector<uint8_t>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3000,7 +3000,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { } break; case INT_ARRAY: { - const DVector<int> *arr=reinterpret_cast<const DVector<int>*>(_data._mem); + const PoolVector<int> *arr=reinterpret_cast<const PoolVector<int>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3008,7 +3008,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { } break; case REAL_ARRAY: { - const DVector<real_t> *arr=reinterpret_cast<const DVector<real_t>*>(_data._mem); + const PoolVector<real_t> *arr=reinterpret_cast<const PoolVector<real_t>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3016,7 +3016,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { } break; case STRING_ARRAY: { - const DVector<String> *arr=reinterpret_cast<const DVector<String>*>(_data._mem); + const PoolVector<String> *arr=reinterpret_cast<const PoolVector<String>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3024,7 +3024,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { } break; case VECTOR2_ARRAY: { - const DVector<Vector2> *arr=reinterpret_cast<const DVector<Vector2>*>(_data._mem); + const PoolVector<Vector2> *arr=reinterpret_cast<const PoolVector<Vector2>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3032,7 +3032,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { } break; case VECTOR3_ARRAY: { - const DVector<Vector3> *arr=reinterpret_cast<const DVector<Vector3>*>(_data._mem); + const PoolVector<Vector3> *arr=reinterpret_cast<const PoolVector<Vector3>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3040,7 +3040,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { } break; case COLOR_ARRAY: { - const DVector<Color> *arr=reinterpret_cast<const DVector<Color>*>(_data._mem); + const PoolVector<Color> *arr=reinterpret_cast<const PoolVector<Color>*>(_data._mem); if (arr->size()==0) return false; r_iter=0; @@ -3123,7 +3123,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { return true; } break; case RAW_ARRAY: { - const DVector<uint8_t> *arr=reinterpret_cast<const DVector<uint8_t>*>(_data._mem); + const PoolVector<uint8_t> *arr=reinterpret_cast<const PoolVector<uint8_t>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3133,7 +3133,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { } break; case INT_ARRAY: { - const DVector<int> *arr=reinterpret_cast<const DVector<int>*>(_data._mem); + const PoolVector<int> *arr=reinterpret_cast<const PoolVector<int>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3143,7 +3143,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { } break; case REAL_ARRAY: { - const DVector<real_t> *arr=reinterpret_cast<const DVector<real_t>*>(_data._mem); + const PoolVector<real_t> *arr=reinterpret_cast<const PoolVector<real_t>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3153,7 +3153,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { } break; case STRING_ARRAY: { - const DVector<String> *arr=reinterpret_cast<const DVector<String>*>(_data._mem); + const PoolVector<String> *arr=reinterpret_cast<const PoolVector<String>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3163,7 +3163,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { } break; case VECTOR2_ARRAY: { - const DVector<Vector2> *arr=reinterpret_cast<const DVector<Vector2>*>(_data._mem); + const PoolVector<Vector2> *arr=reinterpret_cast<const PoolVector<Vector2>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3173,7 +3173,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { } break; case VECTOR3_ARRAY: { - const DVector<Vector3> *arr=reinterpret_cast<const DVector<Vector3>*>(_data._mem); + const PoolVector<Vector3> *arr=reinterpret_cast<const PoolVector<Vector3>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3183,7 +3183,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { } break; case COLOR_ARRAY: { - const DVector<Color> *arr=reinterpret_cast<const DVector<Color>*>(_data._mem); + const PoolVector<Color> *arr=reinterpret_cast<const PoolVector<Color>*>(_data._mem); int idx=r_iter; idx++; if (idx>=arr->size()) @@ -3255,7 +3255,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { return arr->get(idx); } break; case RAW_ARRAY: { - const DVector<uint8_t> *arr=reinterpret_cast<const DVector<uint8_t>*>(_data._mem); + const PoolVector<uint8_t> *arr=reinterpret_cast<const PoolVector<uint8_t>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3266,7 +3266,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { return arr->get(idx); } break; case INT_ARRAY: { - const DVector<int> *arr=reinterpret_cast<const DVector<int>*>(_data._mem); + const PoolVector<int> *arr=reinterpret_cast<const PoolVector<int>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3277,7 +3277,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { return arr->get(idx); } break; case REAL_ARRAY: { - const DVector<real_t> *arr=reinterpret_cast<const DVector<real_t>*>(_data._mem); + const PoolVector<real_t> *arr=reinterpret_cast<const PoolVector<real_t>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3288,7 +3288,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { return arr->get(idx); } break; case STRING_ARRAY: { - const DVector<String> *arr=reinterpret_cast<const DVector<String>*>(_data._mem); + const PoolVector<String> *arr=reinterpret_cast<const PoolVector<String>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3300,7 +3300,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { } break; case VECTOR2_ARRAY: { - const DVector<Vector2> *arr=reinterpret_cast<const DVector<Vector2>*>(_data._mem); + const PoolVector<Vector2> *arr=reinterpret_cast<const PoolVector<Vector2>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3312,7 +3312,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { } break; case VECTOR3_ARRAY: { - const DVector<Vector3> *arr=reinterpret_cast<const DVector<Vector3>*>(_data._mem); + const PoolVector<Vector3> *arr=reinterpret_cast<const PoolVector<Vector3>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3324,7 +3324,7 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const { } break; case COLOR_ARRAY: { - const DVector<Color> *arr=reinterpret_cast<const DVector<Color>*>(_data._mem); + const PoolVector<Color> *arr=reinterpret_cast<const PoolVector<Color>*>(_data._mem); int idx=r_iter; #ifdef DEBUG_ENABLED if (idx<0 || idx>=arr->size()) { @@ -3358,8 +3358,8 @@ void Variant::blend(const Variant& a, const Variant& b, float c, Variant &r_dst) switch(a.type) { case NIL: { r_dst=Variant(); } return; case INT:{ - int va=a._data._int; - int vb=b._data._int; + int64_t va=a._data._int; + int64_t vb=b._data._int; r_dst=int(va + vb * c + 0.5); } return; case REAL:{ @@ -3423,8 +3423,8 @@ void Variant::interpolate(const Variant& a, const Variant& b, float c,Variant &r case NIL:{ r_dst=Variant(); } return; case BOOL:{ r_dst=a; } return; case INT:{ - int va=a._data._int; - int vb=b._data._int; + int64_t va=a._data._int; + int64_t vb=b._data._int; r_dst=int((1.0-c) * va + vb * c); } return; case REAL:{ @@ -3495,20 +3495,20 @@ void Variant::interpolate(const Variant& a, const Variant& b, float c,Variant &r case REAL_ARRAY:{ r_dst=a; } return; case STRING_ARRAY:{ r_dst=a; } return; case VECTOR2_ARRAY:{ - const DVector<Vector2> *arr_a=reinterpret_cast<const DVector<Vector2>* >(a._data._mem); - const DVector<Vector2> *arr_b=reinterpret_cast<const DVector<Vector2>* >(b._data._mem); + const PoolVector<Vector2> *arr_a=reinterpret_cast<const PoolVector<Vector2>* >(a._data._mem); + const PoolVector<Vector2> *arr_b=reinterpret_cast<const PoolVector<Vector2>* >(b._data._mem); int sz = arr_a->size(); if (sz==0 || arr_b->size()!=sz) { r_dst=a; } else { - DVector<Vector2> v; + PoolVector<Vector2> v; v.resize(sz); { - DVector<Vector2>::Write vw=v.write(); - DVector<Vector2>::Read ar=arr_a->read(); - DVector<Vector2>::Read br=arr_b->read(); + PoolVector<Vector2>::Write vw=v.write(); + PoolVector<Vector2>::Read ar=arr_a->read(); + PoolVector<Vector2>::Read br=arr_b->read(); for(int i=0;i<sz;i++) { vw[i]=ar[i].linear_interpolate(br[i],c); @@ -3523,20 +3523,20 @@ void Variant::interpolate(const Variant& a, const Variant& b, float c,Variant &r case VECTOR3_ARRAY:{ - const DVector<Vector3> *arr_a=reinterpret_cast<const DVector<Vector3>* >(a._data._mem); - const DVector<Vector3> *arr_b=reinterpret_cast<const DVector<Vector3>* >(b._data._mem); + const PoolVector<Vector3> *arr_a=reinterpret_cast<const PoolVector<Vector3>* >(a._data._mem); + const PoolVector<Vector3> *arr_b=reinterpret_cast<const PoolVector<Vector3>* >(b._data._mem); int sz = arr_a->size(); if (sz==0 || arr_b->size()!=sz) { r_dst=a; } else { - DVector<Vector3> v; + PoolVector<Vector3> v; v.resize(sz); { - DVector<Vector3>::Write vw=v.write(); - DVector<Vector3>::Read ar=arr_a->read(); - DVector<Vector3>::Read br=arr_b->read(); + PoolVector<Vector3>::Write vw=v.write(); + PoolVector<Vector3>::Read ar=arr_a->read(); + PoolVector<Vector3>::Read br=arr_b->read(); for(int i=0;i<sz;i++) { vw[i]=ar[i].linear_interpolate(br[i],c); diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index 597e975873..1a11eb80d5 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -802,7 +802,7 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in int len = Image::get_image_data_size(width,height,format,mipmaps); - DVector<uint8_t> buffer; + PoolVector<uint8_t> buffer; buffer.resize(len); if (buffer.size()!=len) { @@ -810,7 +810,7 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in } { - DVector<uint8_t>::Write w=buffer.write(); + PoolVector<uint8_t>::Write w=buffer.write(); for(int i=0;i<len;i++) { get_token(p_stream,token,line,r_err_str); @@ -1083,7 +1083,7 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in return ERR_PARSE_ERROR; } - ie.type=InputEvent::JOYSTICK_BUTTON; + ie.type=InputEvent::JOYPAD_BUTTON; get_token(p_stream,token,line,r_err_str); if (token.type!=TK_NUMBER) { @@ -1107,7 +1107,7 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in return ERR_PARSE_ERROR; } - ie.type=InputEvent::JOYSTICK_MOTION; + ie.type=InputEvent::JOYPAD_MOTION; get_token(p_stream,token,line,r_err_str); if (token.type!=TK_NUMBER) { @@ -1156,11 +1156,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in if (err) return err; - DVector<uint8_t> arr; + PoolVector<uint8_t> arr; { int len=args.size(); arr.resize(len); - DVector<uint8_t>::Write w = arr.write(); + PoolVector<uint8_t>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=args[i]; } @@ -1177,11 +1177,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in if (err) return err; - DVector<int> arr; + PoolVector<int> arr; { int len=args.size(); arr.resize(len); - DVector<int>::Write w = arr.write(); + PoolVector<int>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=int(args[i]); } @@ -1198,11 +1198,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in if (err) return err; - DVector<float> arr; + PoolVector<float> arr; { int len=args.size(); arr.resize(len); - DVector<float>::Write w = arr.write(); + PoolVector<float>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=args[i]; } @@ -1251,11 +1251,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in } - DVector<String> arr; + PoolVector<String> arr; { int len=cs.size(); arr.resize(len); - DVector<String>::Write w = arr.write(); + PoolVector<String>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=cs[i]; } @@ -1273,11 +1273,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in if (err) return err; - DVector<Vector2> arr; + PoolVector<Vector2> arr; { int len=args.size()/2; arr.resize(len); - DVector<Vector2>::Write w = arr.write(); + PoolVector<Vector2>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=Vector2(args[i*2+0],args[i*2+1]); } @@ -1294,11 +1294,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in if (err) return err; - DVector<Vector3> arr; + PoolVector<Vector3> arr; { int len=args.size()/3; arr.resize(len); - DVector<Vector3>::Write w = arr.write(); + PoolVector<Vector3>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=Vector3(args[i*3+0],args[i*3+1],args[i*3+2]); } @@ -1315,11 +1315,11 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in if (err) return err; - DVector<Color> arr; + PoolVector<Color> arr; { int len=args.size()/4; arr.resize(len); - DVector<Color>::Write w = arr.write(); + PoolVector<Color>::Write w = arr.write(); for(int i=0;i<len;i++) { w[i]=Color(args[i*4+0],args[i*4+1],args[i*4+2],args[i*4+3]); } @@ -1387,7 +1387,7 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in return err; ERR_FAIL_COND_V(params.size()!=2,ERR_PARSE_ERROR); InputEvent ie; - ie.type=InputEvent::JOYSTICK_BUTTON; + ie.type=InputEvent::JOYPAD_BUTTON; ie.device=params[0].to_int(); ie.joy_button.button_index=params[1].to_int(); @@ -1403,7 +1403,7 @@ Error VariantParser::parse_value(Token& token,Variant &value,Stream *p_stream,in ERR_FAIL_COND_V(params.size()!=2,ERR_PARSE_ERROR); InputEvent ie; - ie.type=InputEvent::JOYSTICK_MOTION; + ie.type=InputEvent::JOYPAD_MOTION; ie.device=params[0].to_int(); int axis=params[1].to_int(); ie.joy_motion.axis=axis>>1; @@ -1986,9 +1986,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str String s; - DVector<uint8_t> data = img.get_data(); + PoolVector<uint8_t> data = img.get_data(); int len = data.size(); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); const uint8_t *ptr=r.ptr();; for (int i=0;i<len;i++) { @@ -2065,11 +2065,11 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str str+="MBUTTON,"+itos(ev.mouse_button.button_index); } break; - case InputEvent::JOYSTICK_BUTTON: { + case InputEvent::JOYPAD_BUTTON: { str+="JBUTTON,"+itos(ev.joy_button.button_index); } break; - case InputEvent::JOYSTICK_MOTION: { + case InputEvent::JOYPAD_MOTION: { str+="JAXIS,"+itos(ev.joy_motion.axis)+","+itos(ev.joy_motion.axis_value); } break; case InputEvent::NONE: { @@ -2128,9 +2128,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud,"ByteArray( "); String s; - DVector<uint8_t> data = p_variant; + PoolVector<uint8_t> data = p_variant; int len = data.size(); - DVector<uint8_t>::Read r = data.read(); + PoolVector<uint8_t>::Read r = data.read(); const uint8_t *ptr=r.ptr();; for (int i=0;i<len;i++) { @@ -2147,9 +2147,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str case Variant::INT_ARRAY: { p_store_string_func(p_store_string_ud,"IntArray( "); - DVector<int> data = p_variant; + PoolVector<int> data = p_variant; int len = data.size(); - DVector<int>::Read r = data.read(); + PoolVector<int>::Read r = data.read(); const int *ptr=r.ptr();; for (int i=0;i<len;i++) { @@ -2167,9 +2167,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str case Variant::REAL_ARRAY: { p_store_string_func(p_store_string_ud,"FloatArray( "); - DVector<real_t> data = p_variant; + PoolVector<real_t> data = p_variant; int len = data.size(); - DVector<real_t>::Read r = data.read(); + PoolVector<real_t>::Read r = data.read(); const real_t *ptr=r.ptr();; for (int i=0;i<len;i++) { @@ -2185,9 +2185,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str case Variant::STRING_ARRAY: { p_store_string_func(p_store_string_ud,"StringArray( "); - DVector<String> data = p_variant; + PoolVector<String> data = p_variant; int len = data.size(); - DVector<String>::Read r = data.read(); + PoolVector<String>::Read r = data.read(); const String *ptr=r.ptr();; String s; //write_string("\n"); @@ -2208,9 +2208,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str case Variant::VECTOR2_ARRAY: { p_store_string_func(p_store_string_ud,"Vector2Array( "); - DVector<Vector2> data = p_variant; + PoolVector<Vector2> data = p_variant; int len = data.size(); - DVector<Vector2>::Read r = data.read(); + PoolVector<Vector2>::Read r = data.read(); const Vector2 *ptr=r.ptr();; for (int i=0;i<len;i++) { @@ -2226,9 +2226,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str case Variant::VECTOR3_ARRAY: { p_store_string_func(p_store_string_ud,"Vector3Array( "); - DVector<Vector3> data = p_variant; + PoolVector<Vector3> data = p_variant; int len = data.size(); - DVector<Vector3>::Read r = data.read(); + PoolVector<Vector3>::Read r = data.read(); const Vector3 *ptr=r.ptr();; for (int i=0;i<len;i++) { @@ -2245,9 +2245,9 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud,"ColorArray( "); - DVector<Color> data = p_variant; + PoolVector<Color> data = p_variant; int len = data.size(); - DVector<Color>::Read r = data.read(); + PoolVector<Color>::Read r = data.read(); const Color *ptr=r.ptr();; for (int i=0;i<len;i++) { diff --git a/core/vector.h b/core/vector.h index 8113099a61..3119657cbb 100644 --- a/core/vector.h +++ b/core/vector.h @@ -32,7 +32,7 @@ /** * @class Vector * @author Juan Linietsky - * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use DVector for large arrays. + * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use PoolVector for large arrays. */ #include "os/memory.h" #include "error_macros.h" @@ -46,19 +46,20 @@ class Vector { // internal helpers - _FORCE_INLINE_ SafeRefCount* _get_refcount() const { + _FORCE_INLINE_ uint32_t* _get_refcount() const { if (!_ptr) return NULL; - return reinterpret_cast<SafeRefCount*>((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount)); + return reinterpret_cast<uint32_t*>(_ptr)-2; } - _FORCE_INLINE_ int* _get_size() const { + _FORCE_INLINE_ uint32_t* _get_size() const { if (!_ptr) - return NULL; - return reinterpret_cast<int*>((uint8_t*)_ptr-sizeof(int)); + return NULL; + + return reinterpret_cast<uint32_t*>(_ptr)-1; } _FORCE_INLINE_ T* _get_data() const { @@ -71,7 +72,7 @@ class Vector { _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const { //return nearest_power_of_2_templated(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int)); - return nearest_power_of_2(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int)); + return nearest_power_of_2(p_elements*sizeof(T)); } _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { @@ -79,8 +80,8 @@ class Vector { size_t o; size_t p; if (_mul_overflow(p_elements, sizeof(T), &o)) return false; - if (_add_overflow(o, sizeof(SafeRefCount)+sizeof(int), &p)) return false; - *out = nearest_power_of_2(p); + *out = nearest_power_of_2(o); + if (_add_overflow(o, 32, &p)) return false; //no longer allocated here return true; #else // Speed is more important than correctness here, do the operations unchecked @@ -104,7 +105,7 @@ public: _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ int size() const { - int* size = _get_size(); + uint32_t* size = (uint32_t*)_get_size(); if (size) return *size; else @@ -190,22 +191,22 @@ void Vector<T>::_unref(void *p_data) { if (!p_data) return; - SafeRefCount *src = reinterpret_cast<SafeRefCount*>((uint8_t*)p_data-sizeof(int)-sizeof(SafeRefCount)); + uint32_t *refc = _get_refcount(); - if (!src->unref()) + if (atomic_decrement(refc)>0) return; // still in use // clean up - int *count = (int*)(src+1); + uint32_t *count = _get_size(); T *data = (T*)(count+1); - for (int i=0;i<*count;i++) { + for (uint32_t i=0;i<*count;i++) { // call destructors data[i].~T(); } // free mem - memfree((uint8_t*)p_data-sizeof(int)-sizeof(SafeRefCount)); + Memory::free_static((uint8_t*)p_data,true); } @@ -215,18 +216,22 @@ void Vector<T>::_copy_on_write() { if (!_ptr) return; - if (_get_refcount()->get() > 1 ) { + uint32_t *refc = _get_refcount(); + + if (*refc > 1) { /* in use by more than me */ - void* mem_new = memalloc(_get_alloc_size(*_get_size())); - SafeRefCount *src_new=(SafeRefCount *)mem_new; - src_new->init(); - int * _size = (int*)(src_new+1); - *_size=*_get_size(); + uint32_t current_size = *_get_size(); + + uint32_t* mem_new = (uint32_t*)Memory::alloc_static(_get_alloc_size(current_size),true); - T*_data=(T*)(_size+1); + + *(mem_new-2)=1; //refcount + *(mem_new-1)=current_size; //size + + T*_data=(T*)(mem_new); // initialize new elements - for (int i=0;i<*_size;i++) { + for (uint32_t i=0;i<current_size;i++) { memnew_placement(&_data[i], T( _get_data()[i] ) ); } @@ -280,16 +285,17 @@ Error Vector<T>::resize(int p_size) { if (size()==0) { // alloc from scratch - void* ptr=memalloc(alloc_size); + uint32_t *ptr=(uint32_t*)Memory::alloc_static(alloc_size,true); ERR_FAIL_COND_V( !ptr ,ERR_OUT_OF_MEMORY); - _ptr=(T*)((uint8_t*)ptr+sizeof(int)+sizeof(SafeRefCount)); - _get_refcount()->init(); // init refcount - *_get_size()=0; // init size (currently, none) + *(ptr-1)=0; //size, currently none + *(ptr-2)=1; //refcount + + _ptr=(T*)ptr; } else { - void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount), alloc_size); + void *_ptrnew = (T*)Memory::realloc_static(_ptr, alloc_size,true); ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); - _ptr=(T*)((uint8_t*)_ptrnew+sizeof(int)+sizeof(SafeRefCount)); + _ptr=(T*)(_ptrnew); } // construct the newly created elements @@ -305,16 +311,16 @@ Error Vector<T>::resize(int p_size) { } else if (p_size<size()) { // deinitialize no longer needed elements - for (int i=p_size;i<*_get_size();i++) { + for (uint32_t i=p_size;i<*_get_size();i++) { T* t = &_get_data()[i]; t->~T(); } - void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount), alloc_size); + void *_ptrnew = (T*)Memory::realloc_static(_ptr, alloc_size,true); ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); - _ptr=(T*)((uint8_t*)_ptrnew+sizeof(int)+sizeof(SafeRefCount)); + _ptr=(T*)(_ptrnew); *_get_size()=p_size; @@ -382,8 +388,9 @@ void Vector<T>::_copy_from(const Vector& p_from) { if (!p_from._ptr) return; //nothing to do - if (p_from._get_refcount()->ref()) // could reference + if (atomic_conditional_increment(p_from._get_refcount())>0) { // could reference _ptr=p_from._ptr; + } } |