| 1 | /**************************************************************************/ |
| 2 | /* property_list_helper.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "property_list_helper.h" |
| 32 | |
| 33 | Vector<PropertyListHelper *> PropertyListHelper::base_helpers; // static |
| 34 | |
| 35 | void PropertyListHelper::clear_base_helpers() { // static |
| 36 | for (PropertyListHelper *helper : base_helpers) { |
| 37 | helper->clear(); |
| 38 | } |
| 39 | base_helpers.clear(); |
| 40 | } |
| 41 | |
| 42 | void PropertyListHelper::register_base_helper(PropertyListHelper *p_helper) { // static |
| 43 | base_helpers.push_back(p_helper); |
| 44 | } |
| 45 | |
| 46 | const PropertyListHelper::Property *PropertyListHelper::_get_property(const String &p_property, int *r_index, bool p_allow_oob) const { |
| 47 | const Vector<String> components = p_property.rsplit("/", true, 1); |
| 48 | if (components.size() < 2 || !components[0].begins_with(prefix)) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | const String index_string = components[0].trim_prefix(prefix); |
| 53 | if (!index_string.is_valid_int()) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | int index = index_string.to_int(); |
| 58 | if (index < 0 || (!p_allow_oob && index >= _call_array_length_getter())) { |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | *r_index = index; |
| 63 | return property_list.getptr(components[1]); |
| 64 | } |
| 65 | |
| 66 | void PropertyListHelper::_call_setter(const MethodBind *p_setter, int p_index, const Variant &p_value) const { |
| 67 | DEV_ASSERT(p_setter); |
| 68 | Variant args[] = { p_index, p_value }; |
| 69 | const Variant *argptrs[] = { &args[0], &args[1] }; |
| 70 | Callable::CallError ce; |
| 71 | p_setter->call(object, argptrs, 2, ce); |
| 72 | } |
| 73 | |
| 74 | Variant PropertyListHelper::_call_getter(const Property *p_property, int p_index) const { |
| 75 | if (!p_property->getter) { |
| 76 | return object->get(prefix + itos(p_index) + "/" + p_property->info.name); |
| 77 | } |
| 78 | |
| 79 | Callable::CallError ce; |
| 80 | Variant args[] = { p_index }; |
| 81 | const Variant *argptrs[] = { &args[0] }; |
| 82 | return p_property->getter->call(object, argptrs, 1, ce); |
| 83 | } |
| 84 | |
| 85 | int PropertyListHelper::_call_array_length_getter() const { |
| 86 | Callable::CallError ce; |
| 87 | return array_length_getter->call(object, nullptr, 0, ce); |
| 88 | } |
| 89 | |
| 90 | void PropertyListHelper::set_prefix(const String &p_prefix) { |
| 91 | prefix = p_prefix; |
| 92 | } |
| 93 | |
| 94 | void PropertyListHelper::register_property(const PropertyInfo &p_info, const Variant &p_default) { |
| 95 | Property property; |
| 96 | property.info = p_info; |
| 97 | property.default_value = p_default; |
| 98 | |
| 99 | property_list[p_info.name] = property; |
| 100 | } |
| 101 | |
| 102 | bool PropertyListHelper::is_initialized() const { |
| 103 | return !property_list.is_empty(); |
| 104 | } |
| 105 | |
| 106 | void PropertyListHelper::setup_for_instance(const PropertyListHelper &p_base, Object *p_object) { |
| 107 | DEV_ASSERT(!p_base.prefix.is_empty()); |
| 108 | DEV_ASSERT(p_base.array_length_getter != nullptr); |
| 109 | DEV_ASSERT(!p_base.property_list.is_empty()); |
| 110 | DEV_ASSERT(p_object != nullptr); |
| 111 | |
| 112 | prefix = p_base.prefix; |
| 113 | array_length_getter = p_base.array_length_getter; |
| 114 | property_list = p_base.property_list; |
| 115 | object = p_object; |
| 116 | } |
| 117 | |
| 118 | bool PropertyListHelper::is_property_valid(const String &p_property, int *r_index) const { |
| 119 | const Vector<String> components = p_property.rsplit("/", true, 1); |
| 120 | if (components.size() < 2 || !components[0].begins_with(prefix)) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | { |
| 125 | const String index_string = components[0].trim_prefix(prefix); |
| 126 | if (!index_string.is_valid_int()) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | if (r_index) { |
| 131 | *r_index = index_string.to_int(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return property_list.has(components[1]); |
| 136 | } |
| 137 | |
| 138 | void PropertyListHelper::get_property_list(List<PropertyInfo> *p_list) const { |
| 139 | const int property_count = _call_array_length_getter(); |
| 140 | for (int i = 0; i < property_count; i++) { |
| 141 | for (const KeyValue<String, Property> &E : property_list) { |
| 142 | const Property &property = E.value; |
| 143 | |
| 144 | PropertyInfo info = property.info; |
| 145 | if (!(info.usage & PROPERTY_USAGE_STORE_IF_NULL) && _call_getter(&property, i) == property.default_value) { |
| 146 | info.usage &= (~PROPERTY_USAGE_STORAGE); |
| 147 | } |
| 148 | |
| 149 | info.name = vformat("%s%d/%s", prefix, i, info.name); |
| 150 | p_list->push_back(info); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | bool PropertyListHelper::property_get_value(const String &p_property, Variant &r_ret) const { |
| 156 | int index; |
| 157 | const Property *property = _get_property(p_property, &index); |
| 158 | |
| 159 | if (property) { |
| 160 | r_ret = _call_getter(property, index); |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool PropertyListHelper::property_set_value(const String &p_property, const Variant &p_value) const { |
| 167 | int index; |
| 168 | const Property *property = _get_property(p_property, &index, allow_oob_assign); |
| 169 | |
| 170 | if (property) { |
| 171 | _call_setter(property->setter, index, p_value); |
| 172 | return true; |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | bool PropertyListHelper::property_can_revert(const String &p_property) const { |
| 178 | return is_property_valid(p_property); |
| 179 | } |
| 180 | |
| 181 | bool PropertyListHelper::property_get_revert(const String &p_property, Variant &r_value) const { |
| 182 | int index; |
| 183 | const Property *property = _get_property(p_property, &index); |
| 184 | |
| 185 | if (property) { |
| 186 | r_value = property->default_value; |
| 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | void PropertyListHelper::clear() { |
| 193 | if (is_initialized()) { |
| 194 | memdelete(array_length_getter); |
| 195 | |
| 196 | for (const KeyValue<String, Property> &E : property_list) { |
| 197 | if (E.value.setter) { |
| 198 | memdelete(E.value.setter); |
| 199 | memdelete(E.value.getter); |
| 200 | } |
| 201 | } |
| 202 | property_list.clear(); |
| 203 | } |
| 204 | } |