node_path.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*************************************************************************/
  2. /* node_path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "node_path.h"
  31. #include "core/print_string.h"
  32. void NodePath::_update_hash_cache() const {
  33. uint32_t h = data->absolute ? 1 : 0;
  34. int pc = data->path.size();
  35. const StringName *sn = data->path.ptr();
  36. for (int i = 0; i < pc; i++) {
  37. h = h ^ sn[i].hash();
  38. }
  39. int spc = data->subpath.size();
  40. const StringName *ssn = data->subpath.ptr();
  41. for (int i = 0; i < spc; i++) {
  42. h = h ^ ssn[i].hash();
  43. }
  44. data->hash_cache_valid = true;
  45. data->hash_cache = h;
  46. }
  47. void NodePath::prepend_period() {
  48. if (data->path.size() && data->path[0].operator String() != ".") {
  49. data->path.insert(0, ".");
  50. data->hash_cache_valid = false;
  51. }
  52. }
  53. bool NodePath::is_absolute() const {
  54. if (!data)
  55. return false;
  56. return data->absolute;
  57. }
  58. int NodePath::get_name_count() const {
  59. if (!data)
  60. return 0;
  61. return data->path.size();
  62. }
  63. StringName NodePath::get_name(int p_idx) const {
  64. ERR_FAIL_COND_V(!data, StringName());
  65. ERR_FAIL_INDEX_V(p_idx, data->path.size(), StringName());
  66. return data->path[p_idx];
  67. }
  68. int NodePath::get_subname_count() const {
  69. if (!data)
  70. return 0;
  71. return data->subpath.size();
  72. }
  73. StringName NodePath::get_subname(int p_idx) const {
  74. ERR_FAIL_COND_V(!data, StringName());
  75. ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName());
  76. return data->subpath[p_idx];
  77. }
  78. void NodePath::unref() {
  79. if (data && data->refcount.unref()) {
  80. memdelete(data);
  81. }
  82. data = NULL;
  83. }
  84. bool NodePath::operator==(const NodePath &p_path) const {
  85. if (data == p_path.data)
  86. return true;
  87. if (!data || !p_path.data)
  88. return false;
  89. if (data->absolute != p_path.data->absolute)
  90. return false;
  91. int path_size = data->path.size();
  92. if (path_size != p_path.data->path.size()) {
  93. return false;
  94. }
  95. int subpath_size = data->subpath.size();
  96. if (subpath_size != p_path.data->subpath.size()) {
  97. return false;
  98. }
  99. const StringName *l_path_ptr = data->path.ptr();
  100. const StringName *r_path_ptr = p_path.data->path.ptr();
  101. for (int i = 0; i < path_size; i++) {
  102. if (l_path_ptr[i] != r_path_ptr[i])
  103. return false;
  104. }
  105. const StringName *l_subpath_ptr = data->subpath.ptr();
  106. const StringName *r_subpath_ptr = p_path.data->subpath.ptr();
  107. for (int i = 0; i < subpath_size; i++) {
  108. if (l_subpath_ptr[i] != r_subpath_ptr[i])
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool NodePath::operator!=(const NodePath &p_path) const {
  114. return (!(*this == p_path));
  115. }
  116. void NodePath::operator=(const NodePath &p_path) {
  117. if (this == &p_path)
  118. return;
  119. unref();
  120. if (p_path.data && p_path.data->refcount.ref()) {
  121. data = p_path.data;
  122. }
  123. }
  124. NodePath::operator String() const {
  125. if (!data)
  126. return String();
  127. String ret;
  128. if (data->absolute)
  129. ret = "/";
  130. for (int i = 0; i < data->path.size(); i++) {
  131. if (i > 0)
  132. ret += "/";
  133. ret += data->path[i].operator String();
  134. }
  135. for (int i = 0; i < data->subpath.size(); i++) {
  136. ret += ":" + data->subpath[i].operator String();
  137. }
  138. return ret;
  139. }
  140. NodePath::NodePath(const NodePath &p_path) {
  141. data = NULL;
  142. if (p_path.data && p_path.data->refcount.ref()) {
  143. data = p_path.data;
  144. }
  145. }
  146. Vector<StringName> NodePath::get_names() const {
  147. if (data)
  148. return data->path;
  149. return Vector<StringName>();
  150. }
  151. Vector<StringName> NodePath::get_subnames() const {
  152. if (data)
  153. return data->subpath;
  154. return Vector<StringName>();
  155. }
  156. StringName NodePath::get_concatenated_subnames() const {
  157. ERR_FAIL_COND_V(!data, StringName());
  158. if (!data->concatenated_subpath) {
  159. int spc = data->subpath.size();
  160. String concatenated;
  161. const StringName *ssn = data->subpath.ptr();
  162. for (int i = 0; i < spc; i++) {
  163. concatenated += i == 0 ? ssn[i].operator String() : ":" + ssn[i];
  164. }
  165. data->concatenated_subpath = concatenated;
  166. }
  167. return data->concatenated_subpath;
  168. }
  169. NodePath NodePath::rel_path_to(const NodePath &p_np) const {
  170. ERR_FAIL_COND_V(!is_absolute(), NodePath());
  171. ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
  172. Vector<StringName> src_dirs = get_names();
  173. Vector<StringName> dst_dirs = p_np.get_names();
  174. //find common parent
  175. int common_parent = 0;
  176. while (true) {
  177. if (src_dirs.size() == common_parent)
  178. break;
  179. if (dst_dirs.size() == common_parent)
  180. break;
  181. if (src_dirs[common_parent] != dst_dirs[common_parent])
  182. break;
  183. common_parent++;
  184. }
  185. common_parent--;
  186. Vector<StringName> relpath;
  187. for (int i = src_dirs.size() - 1; i > common_parent; i--) {
  188. relpath.push_back("..");
  189. }
  190. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  191. relpath.push_back(dst_dirs[i]);
  192. }
  193. if (relpath.size() == 0)
  194. relpath.push_back(".");
  195. return NodePath(relpath, p_np.get_subnames(), false);
  196. }
  197. NodePath NodePath::get_as_property_path() const {
  198. if (!data->path.size()) {
  199. return *this;
  200. } else {
  201. Vector<StringName> new_path = data->subpath;
  202. String initial_subname = data->path[0];
  203. for (int i = 1; i < data->path.size(); i++) {
  204. initial_subname += "/" + data->path[i];
  205. }
  206. new_path.insert(0, initial_subname);
  207. return NodePath(Vector<StringName>(), new_path, false);
  208. }
  209. }
  210. NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
  211. data = NULL;
  212. if (p_path.size() == 0)
  213. return;
  214. data = memnew(Data);
  215. data->refcount.init();
  216. data->absolute = p_absolute;
  217. data->path = p_path;
  218. data->has_slashes = true;
  219. data->hash_cache_valid = false;
  220. }
  221. NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute) {
  222. data = NULL;
  223. if (p_path.size() == 0 && p_subpath.size() == 0)
  224. return;
  225. data = memnew(Data);
  226. data->refcount.init();
  227. data->absolute = p_absolute;
  228. data->path = p_path;
  229. data->subpath = p_subpath;
  230. data->has_slashes = true;
  231. data->hash_cache_valid = false;
  232. }
  233. void NodePath::simplify() {
  234. if (!data)
  235. return;
  236. for (int i = 0; i < data->path.size(); i++) {
  237. if (data->path.size() == 1)
  238. break;
  239. if (data->path[i].operator String() == ".") {
  240. data->path.remove(i);
  241. i--;
  242. } else if (data->path[i].operator String() == ".." && i > 0 && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
  243. //remove both
  244. data->path.remove(i - 1);
  245. data->path.remove(i - 1);
  246. i -= 2;
  247. if (data->path.size() == 0) {
  248. data->path.push_back(".");
  249. break;
  250. }
  251. }
  252. }
  253. data->hash_cache_valid = false;
  254. }
  255. NodePath NodePath::simplified() const {
  256. NodePath np = *this;
  257. np.simplify();
  258. return np;
  259. }
  260. NodePath::NodePath(const String &p_path) {
  261. data = NULL;
  262. if (p_path.length() == 0)
  263. return;
  264. String path = p_path;
  265. Vector<StringName> subpath;
  266. int absolute = (path[0] == '/') ? 1 : 0;
  267. bool last_is_slash = true;
  268. bool has_slashes = false;
  269. int slices = 0;
  270. int subpath_pos = path.find(":");
  271. if (subpath_pos != -1) {
  272. int from = subpath_pos + 1;
  273. for (int i = from; i <= path.length(); i++) {
  274. if (path[i] == ':' || path[i] == 0) {
  275. String str = path.substr(from, i - from);
  276. if (str == "") {
  277. if (path[i] == 0) continue; // Allow end-of-path :
  278. ERR_EXPLAIN("Invalid NodePath: " + p_path);
  279. ERR_FAIL();
  280. }
  281. subpath.push_back(str);
  282. from = i + 1;
  283. }
  284. }
  285. path = path.substr(0, subpath_pos);
  286. }
  287. for (int i = absolute; i < path.length(); i++) {
  288. if (path[i] == '/') {
  289. last_is_slash = true;
  290. has_slashes = true;
  291. } else {
  292. if (last_is_slash)
  293. slices++;
  294. last_is_slash = false;
  295. }
  296. }
  297. if (slices == 0 && !absolute && !subpath.size())
  298. return;
  299. data = memnew(Data);
  300. data->refcount.init();
  301. data->absolute = absolute ? true : false;
  302. data->has_slashes = has_slashes;
  303. data->subpath = subpath;
  304. data->hash_cache_valid = false;
  305. if (slices == 0)
  306. return;
  307. data->path.resize(slices);
  308. last_is_slash = true;
  309. int from = absolute;
  310. int slice = 0;
  311. for (int i = absolute; i < path.length() + 1; i++) {
  312. if (path[i] == '/' || path[i] == 0) {
  313. if (!last_is_slash) {
  314. String name = path.substr(from, i - from);
  315. ERR_FAIL_INDEX(slice, data->path.size());
  316. data->path.write[slice++] = name;
  317. }
  318. from = i + 1;
  319. last_is_slash = true;
  320. } else {
  321. last_is_slash = false;
  322. }
  323. }
  324. }
  325. bool NodePath::is_empty() const {
  326. return !data;
  327. }
  328. NodePath::NodePath() {
  329. data = NULL;
  330. }
  331. NodePath::~NodePath() {
  332. unref();
  333. }