node_path.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*************************************************************************/
  2. /* node_path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. h = h ^ data->property.hash();
  47. return h;
  48. }
  49. void NodePath::prepend_period() {
  50. if (data->path.size() && data->path[0].operator String() != ".") {
  51. data->path.insert(0, ".");
  52. }
  53. }
  54. bool NodePath::is_absolute() const {
  55. if (!data)
  56. return false;
  57. return data->absolute;
  58. }
  59. int NodePath::get_name_count() const {
  60. if (!data)
  61. return 0;
  62. return data->path.size();
  63. }
  64. StringName NodePath::get_name(int p_idx) const {
  65. ERR_FAIL_COND_V(!data, StringName());
  66. ERR_FAIL_INDEX_V(p_idx, data->path.size(), StringName());
  67. return data->path[p_idx];
  68. }
  69. StringName NodePath::get_property() const {
  70. if (!data)
  71. return StringName();
  72. return data->property;
  73. }
  74. int NodePath::get_subname_count() const {
  75. if (!data)
  76. return 0;
  77. return data->subpath.size();
  78. }
  79. StringName NodePath::get_subname(int p_idx) const {
  80. ERR_FAIL_COND_V(!data, StringName());
  81. ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName());
  82. return data->subpath[p_idx];
  83. }
  84. void NodePath::unref() {
  85. if (data && data->refcount.unref()) {
  86. memdelete(data);
  87. }
  88. data = NULL;
  89. }
  90. bool NodePath::operator==(const NodePath &p_path) const {
  91. if (data == p_path.data)
  92. return true;
  93. if (!data || !p_path.data)
  94. return false;
  95. if (data->absolute != p_path.data->absolute)
  96. return false;
  97. if (data->path.size() != p_path.data->path.size())
  98. return false;
  99. if (data->subpath.size() != p_path.data->subpath.size())
  100. return false;
  101. if (data->property != p_path.data->property)
  102. return false;
  103. for (int i = 0; i < data->path.size(); i++) {
  104. if (data->path[i] != p_path.data->path[i])
  105. return false;
  106. }
  107. for (int i = 0; i < data->subpath.size(); i++) {
  108. if (data->subpath[i] != p_path.data->subpath[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. if (data->property.operator String() != "")
  139. ret += ":" + String(data->property);
  140. return ret;
  141. }
  142. NodePath::NodePath(const NodePath &p_path) {
  143. data = NULL;
  144. if (p_path.data && p_path.data->refcount.ref()) {
  145. data = p_path.data;
  146. }
  147. }
  148. Vector<StringName> NodePath::get_names() const {
  149. if (data)
  150. return data->path;
  151. return Vector<StringName>();
  152. }
  153. Vector<StringName> NodePath::get_subnames() const {
  154. if (data)
  155. return data->subpath;
  156. return Vector<StringName>();
  157. }
  158. NodePath NodePath::rel_path_to(const NodePath &p_np) const {
  159. ERR_FAIL_COND_V(!is_absolute(), NodePath());
  160. ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
  161. Vector<StringName> src_dirs = get_names();
  162. Vector<StringName> dst_dirs = p_np.get_names();
  163. //find common parent
  164. int common_parent = 0;
  165. while (true) {
  166. if (src_dirs.size() == common_parent)
  167. break;
  168. if (dst_dirs.size() == common_parent)
  169. break;
  170. if (src_dirs[common_parent] != dst_dirs[common_parent])
  171. break;
  172. common_parent++;
  173. }
  174. common_parent--;
  175. Vector<StringName> relpath;
  176. for (int i = src_dirs.size() - 1; i > common_parent; i--) {
  177. relpath.push_back("..");
  178. }
  179. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  180. relpath.push_back(dst_dirs[i]);
  181. }
  182. if (relpath.size() == 0)
  183. relpath.push_back(".");
  184. return NodePath(relpath, p_np.get_subnames(), false, p_np.get_property());
  185. }
  186. NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute, const String &p_property) {
  187. data = NULL;
  188. if (p_path.size() == 0)
  189. return;
  190. data = memnew(Data);
  191. data->refcount.init();
  192. data->absolute = p_absolute;
  193. data->path = p_path;
  194. data->property = p_property;
  195. }
  196. NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute, const String &p_property) {
  197. data = NULL;
  198. if (p_path.size() == 0)
  199. return;
  200. data = memnew(Data);
  201. data->refcount.init();
  202. data->absolute = p_absolute;
  203. data->path = p_path;
  204. data->subpath = p_subpath;
  205. data->property = p_property;
  206. }
  207. void NodePath::simplify() {
  208. if (!data)
  209. return;
  210. for (int i = 0; i < data->path.size(); i++) {
  211. if (data->path.size() == 1)
  212. break;
  213. if (data->path[i].operator String() == ".") {
  214. data->path.remove(i);
  215. i--;
  216. } else if (data->path[i].operator String() == ".." && i > 0 && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
  217. //remove both
  218. data->path.remove(i - 1);
  219. data->path.remove(i - 1);
  220. i -= 2;
  221. if (data->path.size() == 0) {
  222. data->path.push_back(".");
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. NodePath NodePath::simplified() const {
  229. NodePath np = *this;
  230. np.simplify();
  231. return np;
  232. }
  233. NodePath::NodePath(const String &p_path) {
  234. data = NULL;
  235. if (p_path.length() == 0)
  236. return;
  237. String path = p_path;
  238. StringName property;
  239. Vector<StringName> subpath;
  240. int absolute = (path[0] == '/') ? 1 : 0;
  241. bool last_is_slash = true;
  242. int slices = 0;
  243. int subpath_pos = path.find(":");
  244. if (subpath_pos != -1) {
  245. int from = subpath_pos + 1;
  246. for (int i = from; i <= path.length(); i++) {
  247. if (path[i] == ':' || path[i] == 0) {
  248. String str = path.substr(from, i - from);
  249. if (path[i] == ':') {
  250. if (str == "") {
  251. ERR_EXPLAIN("Invalid NodePath: " + p_path);
  252. ERR_FAIL();
  253. }
  254. subpath.push_back(str);
  255. } else {
  256. //property can be empty
  257. property = str;
  258. }
  259. from = i + 1;
  260. }
  261. }
  262. path = path.substr(0, subpath_pos);
  263. }
  264. for (int i = absolute; i < path.length(); i++) {
  265. if (path[i] == '/') {
  266. last_is_slash = true;
  267. } else {
  268. if (last_is_slash)
  269. slices++;
  270. last_is_slash = false;
  271. }
  272. }
  273. if (slices == 0 && !absolute && !property)
  274. return;
  275. data = memnew(Data);
  276. data->refcount.init();
  277. data->absolute = absolute ? true : false;
  278. data->property = property;
  279. data->subpath = subpath;
  280. if (slices == 0)
  281. return;
  282. data->path.resize(slices);
  283. last_is_slash = true;
  284. int from = absolute;
  285. int slice = 0;
  286. for (int i = absolute; i < path.length() + 1; i++) {
  287. if (path[i] == '/' || path[i] == 0) {
  288. if (!last_is_slash) {
  289. String name = path.substr(from, i - from);
  290. ERR_FAIL_INDEX(slice, data->path.size());
  291. data->path[slice++] = name;
  292. }
  293. from = i + 1;
  294. last_is_slash = true;
  295. } else {
  296. last_is_slash = false;
  297. }
  298. }
  299. }
  300. bool NodePath::is_empty() const {
  301. return !data;
  302. }
  303. NodePath::NodePath() {
  304. data = NULL;
  305. }
  306. NodePath::~NodePath() {
  307. unref();
  308. }