node_path.cpp 11 KB

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