node_path.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "print_string.h"
  32. uint32_t NodePath::hash() const {
  33. if (!data)
  34. return 0;
  35. uint32_t h = data->absolute ? 1 : 0;
  36. int pc = data->path.size();
  37. const StringName *sn = data->path.ptr();
  38. for (int i = 0; i < pc; i++) {
  39. h = h ^ sn[i].hash();
  40. }
  41. int spc = data->subpath.size();
  42. const StringName *ssn = data->subpath.ptr();
  43. for (int i = 0; i < spc; i++) {
  44. h = h ^ ssn[i].hash();
  45. }
  46. return h;
  47. }
  48. void NodePath::prepend_period() {
  49. if (data->path.size() && data->path[0].operator String() != ".") {
  50. data->path.insert(0, ".");
  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. if (data->path.size() != p_path.data->path.size())
  92. return false;
  93. if (data->subpath.size() != p_path.data->subpath.size())
  94. return false;
  95. for (int i = 0; i < data->path.size(); i++) {
  96. if (data->path[i] != p_path.data->path[i])
  97. return false;
  98. }
  99. for (int i = 0; i < data->subpath.size(); i++) {
  100. if (data->subpath[i] != p_path.data->subpath[i])
  101. return false;
  102. }
  103. return true;
  104. }
  105. bool NodePath::operator!=(const NodePath &p_path) const {
  106. return (!(*this == p_path));
  107. }
  108. void NodePath::operator=(const NodePath &p_path) {
  109. if (this == &p_path)
  110. return;
  111. unref();
  112. if (p_path.data && p_path.data->refcount.ref()) {
  113. data = p_path.data;
  114. }
  115. }
  116. NodePath::operator String() const {
  117. if (!data)
  118. return String();
  119. String ret;
  120. if (data->absolute)
  121. ret = "/";
  122. for (int i = 0; i < data->path.size(); i++) {
  123. if (i > 0)
  124. ret += "/";
  125. ret += data->path[i].operator String();
  126. }
  127. for (int i = 0; i < data->subpath.size(); i++) {
  128. ret += ":" + data->subpath[i].operator String();
  129. }
  130. return ret;
  131. }
  132. NodePath::NodePath(const NodePath &p_path) {
  133. data = NULL;
  134. if (p_path.data && p_path.data->refcount.ref()) {
  135. data = p_path.data;
  136. }
  137. }
  138. Vector<StringName> NodePath::get_names() const {
  139. if (data)
  140. return data->path;
  141. return Vector<StringName>();
  142. }
  143. Vector<StringName> NodePath::get_subnames() const {
  144. if (data)
  145. return data->subpath;
  146. return Vector<StringName>();
  147. }
  148. StringName NodePath::get_concatenated_subnames() const {
  149. ERR_FAIL_COND_V(!data, StringName());
  150. if (!data->concatenated_subpath) {
  151. int spc = data->subpath.size();
  152. String concatenated;
  153. const StringName *ssn = data->subpath.ptr();
  154. for (int i = 0; i < spc; i++) {
  155. concatenated += i == 0 ? ssn[i].operator String() : ":" + ssn[i];
  156. }
  157. data->concatenated_subpath = concatenated;
  158. }
  159. return data->concatenated_subpath;
  160. }
  161. NodePath NodePath::rel_path_to(const NodePath &p_np) const {
  162. ERR_FAIL_COND_V(!is_absolute(), NodePath());
  163. ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
  164. Vector<StringName> src_dirs = get_names();
  165. Vector<StringName> dst_dirs = p_np.get_names();
  166. //find common parent
  167. int common_parent = 0;
  168. while (true) {
  169. if (src_dirs.size() == common_parent)
  170. break;
  171. if (dst_dirs.size() == common_parent)
  172. break;
  173. if (src_dirs[common_parent] != dst_dirs[common_parent])
  174. break;
  175. common_parent++;
  176. }
  177. common_parent--;
  178. Vector<StringName> relpath;
  179. for (int i = src_dirs.size() - 1; i > common_parent; i--) {
  180. relpath.push_back("..");
  181. }
  182. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  183. relpath.push_back(dst_dirs[i]);
  184. }
  185. if (relpath.size() == 0)
  186. relpath.push_back(".");
  187. return NodePath(relpath, p_np.get_subnames(), false);
  188. }
  189. NodePath NodePath::get_as_property_path() const {
  190. if (!data->path.size()) {
  191. return *this;
  192. } else {
  193. Vector<StringName> new_path = data->subpath;
  194. String initial_subname = data->path[0];
  195. for (size_t i = 1; i < data->path.size(); i++) {
  196. initial_subname += i == 0 ? data->path[i].operator String() : "/" + data->path[i];
  197. }
  198. new_path.insert(0, initial_subname);
  199. return NodePath(Vector<StringName>(), new_path, false);
  200. }
  201. }
  202. NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
  203. data = NULL;
  204. if (p_path.size() == 0)
  205. return;
  206. data = memnew(Data);
  207. data->refcount.init();
  208. data->absolute = p_absolute;
  209. data->path = p_path;
  210. data->has_slashes = true;
  211. }
  212. NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute) {
  213. data = NULL;
  214. if (p_path.size() == 0 && p_subpath.size() == 0)
  215. return;
  216. data = memnew(Data);
  217. data->refcount.init();
  218. data->absolute = p_absolute;
  219. data->path = p_path;
  220. data->subpath = p_subpath;
  221. data->has_slashes = true;
  222. }
  223. void NodePath::simplify() {
  224. if (!data)
  225. return;
  226. for (int i = 0; i < data->path.size(); i++) {
  227. if (data->path.size() == 1)
  228. break;
  229. if (data->path[i].operator String() == ".") {
  230. data->path.remove(i);
  231. i--;
  232. } else if (data->path[i].operator String() == ".." && i > 0 && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
  233. //remove both
  234. data->path.remove(i - 1);
  235. data->path.remove(i - 1);
  236. i -= 2;
  237. if (data->path.size() == 0) {
  238. data->path.push_back(".");
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. NodePath NodePath::simplified() const {
  245. NodePath np = *this;
  246. np.simplify();
  247. return np;
  248. }
  249. NodePath::NodePath(const String &p_path) {
  250. data = NULL;
  251. if (p_path.length() == 0)
  252. return;
  253. String path = p_path;
  254. Vector<StringName> subpath;
  255. int absolute = (path[0] == '/') ? 1 : 0;
  256. bool last_is_slash = true;
  257. bool has_slashes = false;
  258. int slices = 0;
  259. int subpath_pos = path.find(":");
  260. if (subpath_pos != -1) {
  261. int from = subpath_pos + 1;
  262. for (int i = from; i <= path.length(); i++) {
  263. if (path[i] == ':' || path[i] == 0) {
  264. String str = path.substr(from, i - from);
  265. if (str == "") {
  266. if (path[i] == 0) continue; // Allow end-of-path :
  267. ERR_EXPLAIN("Invalid NodePath: " + p_path);
  268. ERR_FAIL();
  269. }
  270. subpath.push_back(str);
  271. from = i + 1;
  272. }
  273. }
  274. path = path.substr(0, subpath_pos);
  275. }
  276. for (int i = absolute; i < path.length(); i++) {
  277. if (path[i] == '/') {
  278. last_is_slash = true;
  279. has_slashes = true;
  280. } else {
  281. if (last_is_slash)
  282. slices++;
  283. last_is_slash = false;
  284. }
  285. }
  286. if (slices == 0 && !absolute && !subpath.size())
  287. return;
  288. data = memnew(Data);
  289. data->refcount.init();
  290. data->absolute = absolute ? true : false;
  291. data->has_slashes = has_slashes;
  292. data->subpath = subpath;
  293. if (slices == 0)
  294. return;
  295. data->path.resize(slices);
  296. last_is_slash = true;
  297. int from = absolute;
  298. int slice = 0;
  299. for (int i = absolute; i < path.length() + 1; i++) {
  300. if (path[i] == '/' || path[i] == 0) {
  301. if (!last_is_slash) {
  302. String name = path.substr(from, i - from);
  303. ERR_FAIL_INDEX(slice, data->path.size());
  304. data->path[slice++] = name;
  305. }
  306. from = i + 1;
  307. last_is_slash = true;
  308. } else {
  309. last_is_slash = false;
  310. }
  311. }
  312. }
  313. bool NodePath::is_empty() const {
  314. return !data;
  315. }
  316. NodePath::NodePath() {
  317. data = NULL;
  318. }
  319. NodePath::~NodePath() {
  320. unref();
  321. }