node_path.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**************************************************************************/
  2. /* node_path.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. #include "node_path.h"
  31. #include "core/variant/variant.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->concatenated_path = StringName();
  51. data->hash_cache_valid = false;
  52. }
  53. }
  54. bool NodePath::is_absolute() const {
  55. if (!data) {
  56. return false;
  57. }
  58. return data->absolute;
  59. }
  60. int NodePath::get_name_count() const {
  61. if (!data) {
  62. return 0;
  63. }
  64. return data->path.size();
  65. }
  66. StringName NodePath::get_name(int p_idx) const {
  67. ERR_FAIL_NULL_V(data, StringName());
  68. ERR_FAIL_INDEX_V(p_idx, data->path.size(), StringName());
  69. return data->path[p_idx];
  70. }
  71. int NodePath::get_subname_count() const {
  72. if (!data) {
  73. return 0;
  74. }
  75. return data->subpath.size();
  76. }
  77. StringName NodePath::get_subname(int p_idx) const {
  78. ERR_FAIL_NULL_V(data, StringName());
  79. ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName());
  80. return data->subpath[p_idx];
  81. }
  82. int NodePath::get_total_name_count() const {
  83. if (!data) {
  84. return 0;
  85. }
  86. return data->path.size() + data->subpath.size();
  87. }
  88. void NodePath::unref() {
  89. if (data && data->refcount.unref()) {
  90. memdelete(data);
  91. }
  92. data = nullptr;
  93. }
  94. bool NodePath::operator==(const NodePath &p_path) const {
  95. if (data == p_path.data) {
  96. return true;
  97. }
  98. if (!data || !p_path.data) {
  99. return false;
  100. }
  101. if (data->hash_cache_valid && p_path.data->hash_cache_valid) {
  102. if (data->hash_cache != p_path.data->hash_cache) {
  103. return false;
  104. }
  105. }
  106. if (data->absolute != p_path.data->absolute) {
  107. return false;
  108. }
  109. int path_size = data->path.size();
  110. if (path_size != p_path.data->path.size()) {
  111. return false;
  112. }
  113. int subpath_size = data->subpath.size();
  114. if (subpath_size != p_path.data->subpath.size()) {
  115. return false;
  116. }
  117. const StringName *l_path_ptr = data->path.ptr();
  118. const StringName *r_path_ptr = p_path.data->path.ptr();
  119. for (int i = 0; i < path_size; i++) {
  120. if (l_path_ptr[i] != r_path_ptr[i]) {
  121. return false;
  122. }
  123. }
  124. const StringName *l_subpath_ptr = data->subpath.ptr();
  125. const StringName *r_subpath_ptr = p_path.data->subpath.ptr();
  126. for (int i = 0; i < subpath_size; i++) {
  127. if (l_subpath_ptr[i] != r_subpath_ptr[i]) {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. bool NodePath::operator!=(const NodePath &p_path) const {
  134. return (!(*this == p_path));
  135. }
  136. void NodePath::operator=(const NodePath &p_path) {
  137. if (this == &p_path) {
  138. return;
  139. }
  140. unref();
  141. if (p_path.data && p_path.data->refcount.ref()) {
  142. data = p_path.data;
  143. }
  144. }
  145. NodePath::operator String() const {
  146. if (!data) {
  147. return String();
  148. }
  149. String ret;
  150. if (data->absolute) {
  151. ret = "/";
  152. }
  153. ret += get_concatenated_names();
  154. String subpath = get_concatenated_subnames();
  155. if (!subpath.is_empty()) {
  156. ret += ":" + subpath;
  157. }
  158. return ret;
  159. }
  160. Vector<StringName> NodePath::get_names() const {
  161. if (data) {
  162. return data->path;
  163. }
  164. return Vector<StringName>();
  165. }
  166. Vector<StringName> NodePath::get_subnames() const {
  167. if (data) {
  168. return data->subpath;
  169. }
  170. return Vector<StringName>();
  171. }
  172. StringName NodePath::get_concatenated_names() const {
  173. ERR_FAIL_NULL_V(data, StringName());
  174. if (!data->concatenated_path) {
  175. int pc = data->path.size();
  176. String concatenated;
  177. const StringName *sn = data->path.ptr();
  178. for (int i = 0; i < pc; i++) {
  179. if (i > 0) {
  180. concatenated += "/";
  181. }
  182. concatenated += sn[i].operator String();
  183. }
  184. data->concatenated_path = concatenated;
  185. }
  186. return data->concatenated_path;
  187. }
  188. StringName NodePath::get_concatenated_subnames() const {
  189. ERR_FAIL_NULL_V(data, StringName());
  190. if (!data->concatenated_subpath) {
  191. int spc = data->subpath.size();
  192. String concatenated;
  193. const StringName *ssn = data->subpath.ptr();
  194. for (int i = 0; i < spc; i++) {
  195. if (i > 0) {
  196. concatenated += ":";
  197. }
  198. concatenated += ssn[i].operator String();
  199. }
  200. data->concatenated_subpath = concatenated;
  201. }
  202. return data->concatenated_subpath;
  203. }
  204. NodePath NodePath::slice(int p_begin, int p_end) const {
  205. const int name_count = get_name_count();
  206. const int total_count = get_total_name_count();
  207. int begin = CLAMP(p_begin, -total_count, total_count);
  208. if (begin < 0) {
  209. begin += total_count;
  210. }
  211. int end = CLAMP(p_end, -total_count, total_count);
  212. if (end < 0) {
  213. end += total_count;
  214. }
  215. const int sub_begin = MAX(begin - name_count, 0);
  216. const int sub_end = MAX(end - name_count, 0);
  217. const Vector<StringName> names = get_names().slice(begin, end);
  218. const Vector<StringName> sub_names = get_subnames().slice(sub_begin, sub_end);
  219. const bool absolute = is_absolute() && (begin == 0);
  220. return NodePath(names, sub_names, absolute);
  221. }
  222. NodePath NodePath::rel_path_to(const NodePath &p_np) const {
  223. ERR_FAIL_COND_V(!is_absolute(), NodePath());
  224. ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
  225. Vector<StringName> src_dirs = get_names();
  226. Vector<StringName> dst_dirs = p_np.get_names();
  227. //find common parent
  228. int common_parent = 0;
  229. while (true) {
  230. if (src_dirs.size() == common_parent) {
  231. break;
  232. }
  233. if (dst_dirs.size() == common_parent) {
  234. break;
  235. }
  236. if (src_dirs[common_parent] != dst_dirs[common_parent]) {
  237. break;
  238. }
  239. common_parent++;
  240. }
  241. common_parent--;
  242. Vector<StringName> relpath;
  243. relpath.resize(src_dirs.size() + dst_dirs.size() + 1);
  244. StringName *relpath_ptr = relpath.ptrw();
  245. int path_size = 0;
  246. StringName back_str("..");
  247. for (int i = common_parent + 1; i < src_dirs.size(); i++) {
  248. relpath_ptr[path_size++] = back_str;
  249. }
  250. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  251. relpath_ptr[path_size++] = dst_dirs[i];
  252. }
  253. if (path_size == 0) {
  254. relpath_ptr[path_size++] = ".";
  255. }
  256. relpath.resize(path_size);
  257. return NodePath(relpath, p_np.get_subnames(), false);
  258. }
  259. NodePath NodePath::get_as_property_path() const {
  260. if (!data || !data->path.size()) {
  261. return *this;
  262. } else {
  263. Vector<StringName> new_path = data->subpath;
  264. String initial_subname = data->path[0];
  265. for (int i = 1; i < data->path.size(); i++) {
  266. initial_subname += "/" + data->path[i];
  267. }
  268. new_path.insert(0, initial_subname);
  269. return NodePath(Vector<StringName>(), new_path, false);
  270. }
  271. }
  272. bool NodePath::is_empty() const {
  273. return !data;
  274. }
  275. void NodePath::simplify() {
  276. if (!data) {
  277. return;
  278. }
  279. for (int i = 0; i < data->path.size(); i++) {
  280. if (data->path.size() == 1) {
  281. break;
  282. }
  283. if (data->path[i].operator String() == ".") {
  284. data->path.remove_at(i);
  285. i--;
  286. } else if (i > 0 && data->path[i].operator String() == ".." && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
  287. //remove both
  288. data->path.remove_at(i - 1);
  289. data->path.remove_at(i - 1);
  290. i -= 2;
  291. if (data->path.is_empty()) {
  292. data->path.push_back(".");
  293. break;
  294. }
  295. }
  296. }
  297. data->concatenated_path = StringName();
  298. data->hash_cache_valid = false;
  299. }
  300. NodePath NodePath::simplified() const {
  301. NodePath np = *this;
  302. np.simplify();
  303. return np;
  304. }
  305. NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
  306. if (p_path.is_empty() && !p_absolute) {
  307. return;
  308. }
  309. data = memnew(Data);
  310. data->refcount.init();
  311. data->absolute = p_absolute;
  312. data->path = p_path;
  313. data->hash_cache_valid = false;
  314. }
  315. NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute) {
  316. if (p_path.is_empty() && p_subpath.is_empty() && !p_absolute) {
  317. return;
  318. }
  319. data = memnew(Data);
  320. data->refcount.init();
  321. data->absolute = p_absolute;
  322. data->path = p_path;
  323. data->subpath = p_subpath;
  324. data->hash_cache_valid = false;
  325. }
  326. NodePath::NodePath(const NodePath &p_path) {
  327. if (p_path.data && p_path.data->refcount.ref()) {
  328. data = p_path.data;
  329. }
  330. }
  331. NodePath::NodePath(const String &p_path) {
  332. if (p_path.length() == 0) {
  333. return;
  334. }
  335. String path = p_path;
  336. Vector<StringName> subpath;
  337. bool absolute = (path[0] == '/');
  338. bool last_is_slash = true;
  339. int slices = 0;
  340. int subpath_pos = path.find_char(':');
  341. if (subpath_pos != -1) {
  342. int from = subpath_pos + 1;
  343. for (int i = from; i <= path.length(); i++) {
  344. if (path[i] == ':' || path[i] == 0) {
  345. String str = path.substr(from, i - from);
  346. if (str.is_empty()) {
  347. if (path[i] == 0) {
  348. continue; // Allow end-of-path :
  349. }
  350. ERR_FAIL_MSG(vformat("Invalid NodePath '%s'.", p_path));
  351. }
  352. subpath.push_back(str);
  353. from = i + 1;
  354. }
  355. }
  356. path = path.substr(0, subpath_pos);
  357. }
  358. for (int i = (int)absolute; i < path.length(); i++) {
  359. if (path[i] == '/') {
  360. last_is_slash = true;
  361. } else {
  362. if (last_is_slash) {
  363. slices++;
  364. }
  365. last_is_slash = false;
  366. }
  367. }
  368. if (slices == 0 && !absolute && !subpath.size()) {
  369. return;
  370. }
  371. data = memnew(Data);
  372. data->refcount.init();
  373. data->absolute = absolute;
  374. data->subpath = subpath;
  375. data->hash_cache_valid = false;
  376. if (slices == 0) {
  377. return;
  378. }
  379. data->path.resize(slices);
  380. last_is_slash = true;
  381. int from = (int)absolute;
  382. int slice = 0;
  383. for (int i = (int)absolute; i < path.length() + 1; i++) {
  384. if (path[i] == '/' || path[i] == 0) {
  385. if (!last_is_slash) {
  386. String name = path.substr(from, i - from);
  387. ERR_FAIL_INDEX(slice, data->path.size());
  388. data->path.write[slice++] = name;
  389. }
  390. from = i + 1;
  391. last_is_slash = true;
  392. } else {
  393. last_is_slash = false;
  394. }
  395. }
  396. }
  397. NodePath::~NodePath() {
  398. unref();
  399. }