node_path.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/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_COND_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_COND_V(!data, StringName());
  78. ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName());
  79. return data->subpath[p_idx];
  80. }
  81. void NodePath::unref() {
  82. if (data && data->refcount.unref()) {
  83. memdelete(data);
  84. }
  85. data = nullptr;
  86. }
  87. bool NodePath::operator==(const NodePath &p_path) const {
  88. if (data == p_path.data) {
  89. return true;
  90. }
  91. if (!data || !p_path.data) {
  92. return false;
  93. }
  94. if (data->absolute != p_path.data->absolute) {
  95. return false;
  96. }
  97. int path_size = data->path.size();
  98. if (path_size != p_path.data->path.size()) {
  99. return false;
  100. }
  101. int subpath_size = data->subpath.size();
  102. if (subpath_size != p_path.data->subpath.size()) {
  103. return false;
  104. }
  105. const StringName *l_path_ptr = data->path.ptr();
  106. const StringName *r_path_ptr = p_path.data->path.ptr();
  107. for (int i = 0; i < path_size; i++) {
  108. if (l_path_ptr[i] != r_path_ptr[i]) {
  109. return false;
  110. }
  111. }
  112. const StringName *l_subpath_ptr = data->subpath.ptr();
  113. const StringName *r_subpath_ptr = p_path.data->subpath.ptr();
  114. for (int i = 0; i < subpath_size; i++) {
  115. if (l_subpath_ptr[i] != r_subpath_ptr[i]) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. bool NodePath::operator!=(const NodePath &p_path) const {
  122. return (!(*this == p_path));
  123. }
  124. void NodePath::operator=(const NodePath &p_path) {
  125. if (this == &p_path) {
  126. return;
  127. }
  128. unref();
  129. if (p_path.data && p_path.data->refcount.ref()) {
  130. data = p_path.data;
  131. }
  132. }
  133. NodePath::operator String() const {
  134. if (!data) {
  135. return String();
  136. }
  137. String ret;
  138. if (data->absolute) {
  139. ret = "/";
  140. }
  141. for (int i = 0; i < data->path.size(); i++) {
  142. if (i > 0) {
  143. ret += "/";
  144. }
  145. ret += data->path[i].operator String();
  146. }
  147. for (int i = 0; i < data->subpath.size(); i++) {
  148. ret += ":" + data->subpath[i].operator String();
  149. }
  150. return ret;
  151. }
  152. NodePath::NodePath(const NodePath &p_path) {
  153. data = nullptr;
  154. if (p_path.data && p_path.data->refcount.ref()) {
  155. data = p_path.data;
  156. }
  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_subnames() const {
  171. ERR_FAIL_COND_V(!data, StringName());
  172. if (!data->concatenated_subpath) {
  173. int spc = data->subpath.size();
  174. String concatenated;
  175. const StringName *ssn = data->subpath.ptr();
  176. for (int i = 0; i < spc; i++) {
  177. concatenated += i == 0 ? ssn[i].operator String() : ":" + ssn[i];
  178. }
  179. data->concatenated_subpath = concatenated;
  180. }
  181. return data->concatenated_subpath;
  182. }
  183. NodePath NodePath::rel_path_to(const NodePath &p_np) const {
  184. ERR_FAIL_COND_V(!is_absolute(), NodePath());
  185. ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
  186. Vector<StringName> src_dirs = get_names();
  187. Vector<StringName> dst_dirs = p_np.get_names();
  188. //find common parent
  189. int common_parent = 0;
  190. while (true) {
  191. if (src_dirs.size() == common_parent) {
  192. break;
  193. }
  194. if (dst_dirs.size() == common_parent) {
  195. break;
  196. }
  197. if (src_dirs[common_parent] != dst_dirs[common_parent]) {
  198. break;
  199. }
  200. common_parent++;
  201. }
  202. common_parent--;
  203. Vector<StringName> relpath;
  204. relpath.resize(src_dirs.size() + dst_dirs.size() + 1);
  205. StringName *relpath_ptr = relpath.ptrw();
  206. int path_size = 0;
  207. StringName back_str("..");
  208. for (int i = common_parent + 1; i < src_dirs.size(); i++) {
  209. relpath_ptr[path_size++] = back_str;
  210. }
  211. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  212. relpath_ptr[path_size++] = dst_dirs[i];
  213. }
  214. if (path_size == 0) {
  215. relpath_ptr[path_size++] = ".";
  216. }
  217. relpath.resize(path_size);
  218. return NodePath(relpath, p_np.get_subnames(), false);
  219. }
  220. NodePath NodePath::get_as_property_path() const {
  221. if (!data || !data->path.size()) {
  222. return *this;
  223. } else {
  224. Vector<StringName> new_path = data->subpath;
  225. String initial_subname = data->path[0];
  226. for (int i = 1; i < data->path.size(); i++) {
  227. initial_subname += "/" + data->path[i];
  228. }
  229. new_path.insert(0, initial_subname);
  230. return NodePath(Vector<StringName>(), new_path, false);
  231. }
  232. }
  233. NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
  234. data = nullptr;
  235. if (p_path.size() == 0) {
  236. return;
  237. }
  238. data = memnew(Data);
  239. data->refcount.init();
  240. data->absolute = p_absolute;
  241. data->path = p_path;
  242. data->has_slashes = true;
  243. data->hash_cache_valid = false;
  244. }
  245. NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute) {
  246. data = nullptr;
  247. if (p_path.size() == 0 && p_subpath.size() == 0) {
  248. return;
  249. }
  250. data = memnew(Data);
  251. data->refcount.init();
  252. data->absolute = p_absolute;
  253. data->path = p_path;
  254. data->subpath = p_subpath;
  255. data->has_slashes = true;
  256. data->hash_cache_valid = false;
  257. }
  258. void NodePath::simplify() {
  259. if (!data) {
  260. return;
  261. }
  262. for (int i = 0; i < data->path.size(); i++) {
  263. if (data->path.size() == 1) {
  264. break;
  265. }
  266. if (data->path[i].operator String() == ".") {
  267. data->path.remove(i);
  268. i--;
  269. } else if (data->path[i].operator String() == ".." && i > 0 && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
  270. //remove both
  271. data->path.remove(i - 1);
  272. data->path.remove(i - 1);
  273. i -= 2;
  274. if (data->path.size() == 0) {
  275. data->path.push_back(".");
  276. break;
  277. }
  278. }
  279. }
  280. data->hash_cache_valid = false;
  281. }
  282. NodePath NodePath::simplified() const {
  283. NodePath np = *this;
  284. np.simplify();
  285. return np;
  286. }
  287. NodePath::NodePath(const String &p_path) {
  288. data = nullptr;
  289. if (p_path.length() == 0) {
  290. return;
  291. }
  292. String path = p_path;
  293. Vector<StringName> subpath;
  294. bool absolute = (path[0] == '/');
  295. bool last_is_slash = true;
  296. bool has_slashes = false;
  297. int slices = 0;
  298. int subpath_pos = path.find(":");
  299. if (subpath_pos != -1) {
  300. int from = subpath_pos + 1;
  301. for (int i = from; i <= path.length(); i++) {
  302. if (path[i] == ':' || path[i] == 0) {
  303. String str = path.substr(from, i - from);
  304. if (str == "") {
  305. if (path[i] == 0) {
  306. continue; // Allow end-of-path :
  307. }
  308. ERR_FAIL_MSG("Invalid NodePath '" + p_path + "'.");
  309. }
  310. subpath.push_back(str);
  311. from = i + 1;
  312. }
  313. }
  314. path = path.substr(0, subpath_pos);
  315. }
  316. for (int i = (int)absolute; i < path.length(); i++) {
  317. if (path[i] == '/') {
  318. last_is_slash = true;
  319. has_slashes = true;
  320. } else {
  321. if (last_is_slash) {
  322. slices++;
  323. }
  324. last_is_slash = false;
  325. }
  326. }
  327. if (slices == 0 && !absolute && !subpath.size()) {
  328. return;
  329. }
  330. data = memnew(Data);
  331. data->refcount.init();
  332. data->absolute = absolute;
  333. data->has_slashes = has_slashes;
  334. data->subpath = subpath;
  335. data->hash_cache_valid = false;
  336. if (slices == 0) {
  337. return;
  338. }
  339. data->path.resize(slices);
  340. last_is_slash = true;
  341. int from = (int)absolute;
  342. int slice = 0;
  343. for (int i = (int)absolute; i < path.length() + 1; i++) {
  344. if (path[i] == '/' || path[i] == 0) {
  345. if (!last_is_slash) {
  346. String name = path.substr(from, i - from);
  347. ERR_FAIL_INDEX(slice, data->path.size());
  348. data->path.write[slice++] = name;
  349. }
  350. from = i + 1;
  351. last_is_slash = true;
  352. } else {
  353. last_is_slash = false;
  354. }
  355. }
  356. }
  357. bool NodePath::is_empty() const {
  358. return !data;
  359. }
  360. NodePath::NodePath() {
  361. data = nullptr;
  362. }
  363. NodePath::~NodePath() {
  364. unref();
  365. }