godot/core/node_path.cpp

419 lines
9.1 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* node_path.cpp */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2017-08-05 12:12:53 +00:00
#include "node_path.h"
2014-02-10 01:10:30 +00:00
#include "print_string.h"
2014-02-10 01:10:30 +00:00
uint32_t NodePath::hash() const {
if (!data)
return 0;
uint32_t h = data->absolute ? 1 : 0;
int pc = data->path.size();
const StringName *sn = data->path.ptr();
for (int i = 0; i < pc; i++) {
2014-02-10 01:10:30 +00:00
h = h ^ sn[i].hash();
}
int spc = data->subpath.size();
const StringName *ssn = data->subpath.ptr();
for (int i = 0; i < spc; i++) {
2014-02-10 01:10:30 +00:00
h = h ^ ssn[i].hash();
}
h = h ^ data->property.hash();
return h;
}
void NodePath::prepend_period() {
if (data->path.size() && data->path[0].operator String() != ".") {
data->path.insert(0, ".");
}
}
2014-02-10 01:10:30 +00:00
bool NodePath::is_absolute() const {
if (!data)
return false;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return data->absolute;
}
int NodePath::get_name_count() const {
if (!data)
return 0;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return data->path.size();
}
StringName NodePath::get_name(int p_idx) const {
ERR_FAIL_COND_V(!data, StringName());
ERR_FAIL_INDEX_V(p_idx, data->path.size(), StringName());
2014-02-10 01:10:30 +00:00
return data->path[p_idx];
}
StringName NodePath::get_property() const {
if (!data)
return StringName();
return data->property;
}
int NodePath::get_subname_count() const {
if (!data)
return 0;
return data->subpath.size();
}
StringName NodePath::get_subname(int p_idx) const {
ERR_FAIL_COND_V(!data, StringName());
ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName());
2014-02-10 01:10:30 +00:00
return data->subpath[p_idx];
}
void NodePath::unref() {
if (data && data->refcount.unref()) {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
memdelete(data);
}
data = NULL;
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
bool NodePath::operator==(const NodePath &p_path) const {
2014-02-10 01:10:30 +00:00
if (data == p_path.data)
2014-02-10 01:10:30 +00:00
return true;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (!data || !p_path.data)
return false;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (data->absolute != p_path.data->absolute)
return false;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (data->path.size() != p_path.data->path.size())
return false;
if (data->subpath.size() != p_path.data->subpath.size())
return false;
if (data->property != p_path.data->property)
return false;
for (int i = 0; i < data->path.size(); i++) {
2016-03-08 23:00:52 +00:00
if (data->path[i] != p_path.data->path[i])
2014-02-10 01:10:30 +00:00
return false;
}
for (int i = 0; i < data->subpath.size(); i++) {
2014-02-10 01:10:30 +00:00
if (data->subpath[i] != p_path.data->subpath[i])
2014-02-10 01:10:30 +00:00
return false;
}
return true;
}
bool NodePath::operator!=(const NodePath &p_path) const {
2014-02-10 01:10:30 +00:00
return (!(*this == p_path));
}
2016-03-08 23:00:52 +00:00
void NodePath::operator=(const NodePath &p_path) {
2016-03-08 23:00:52 +00:00
if (this == &p_path)
2014-02-10 01:10:30 +00:00
return;
unref();
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (p_path.data && p_path.data->refcount.ref()) {
2016-03-08 23:00:52 +00:00
data = p_path.data;
2014-02-10 01:10:30 +00:00
}
}
NodePath::operator String() const {
if (!data)
return String();
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
String ret;
if (data->absolute)
ret = "/";
2016-03-08 23:00:52 +00:00
for (int i = 0; i < data->path.size(); i++) {
2016-03-08 23:00:52 +00:00
if (i > 0)
ret += "/";
ret += data->path[i].operator String();
2014-02-10 01:10:30 +00:00
}
for (int i = 0; i < data->subpath.size(); i++) {
2014-02-10 01:10:30 +00:00
ret += ":" + data->subpath[i].operator String();
2014-02-10 01:10:30 +00:00
}
if (data->property.operator String() != "")
ret += ":" + String(data->property);
2014-02-10 01:10:30 +00:00
return ret;
}
NodePath::NodePath(const NodePath &p_path) {
2014-02-10 01:10:30 +00:00
data = NULL;
2014-02-10 01:10:30 +00:00
if (p_path.data && p_path.data->refcount.ref()) {
2016-03-08 23:00:52 +00:00
data = p_path.data;
2014-02-10 01:10:30 +00:00
}
}
Vector<StringName> NodePath::get_names() const {
if (data)
return data->path;
return Vector<StringName>();
}
Vector<StringName> NodePath::get_subnames() const {
2014-02-10 01:10:30 +00:00
if (data)
return data->subpath;
return Vector<StringName>();
}
NodePath NodePath::rel_path_to(const NodePath &p_np) const {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!is_absolute(), NodePath());
ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
2014-02-10 01:10:30 +00:00
Vector<StringName> src_dirs = get_names();
Vector<StringName> dst_dirs = p_np.get_names();
//find common parent
int common_parent = 0;
2014-02-10 01:10:30 +00:00
while (true) {
if (src_dirs.size() == common_parent)
2014-02-10 01:10:30 +00:00
break;
if (dst_dirs.size() == common_parent)
2014-02-10 01:10:30 +00:00
break;
if (src_dirs[common_parent] != dst_dirs[common_parent])
2014-02-10 01:10:30 +00:00
break;
common_parent++;
}
common_parent--;
Vector<StringName> relpath;
for (int i = src_dirs.size() - 1; i > common_parent; i--) {
2014-02-10 01:10:30 +00:00
relpath.push_back("..");
}
for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
2014-02-10 01:10:30 +00:00
relpath.push_back(dst_dirs[i]);
}
if (relpath.size() == 0)
2014-02-10 01:10:30 +00:00
relpath.push_back(".");
return NodePath(relpath, p_np.get_subnames(), false, p_np.get_property());
2014-02-10 01:10:30 +00:00
}
NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute, const String &p_property) {
2014-02-10 01:10:30 +00:00
data = NULL;
2016-03-08 23:00:52 +00:00
if (p_path.size() == 0)
2014-02-10 01:10:30 +00:00
return;
2016-03-08 23:00:52 +00:00
data = memnew(Data);
2014-02-10 01:10:30 +00:00
data->refcount.init();
data->absolute = p_absolute;
data->path = p_path;
data->property = p_property;
2014-02-10 01:10:30 +00:00
}
NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute, const String &p_property) {
2014-02-10 01:10:30 +00:00
data = NULL;
2014-02-10 01:10:30 +00:00
if (p_path.size() == 0)
2014-02-10 01:10:30 +00:00
return;
data = memnew(Data);
2014-02-10 01:10:30 +00:00
data->refcount.init();
data->absolute = p_absolute;
data->path = p_path;
data->subpath = p_subpath;
data->property = p_property;
2014-02-10 01:10:30 +00:00
}
void NodePath::simplify() {
if (!data)
return;
for (int i = 0; i < data->path.size(); i++) {
if (data->path.size() == 1)
break;
if (data->path[i].operator String() == ".") {
data->path.remove(i);
i--;
} else if (data->path[i].operator String() == ".." && i > 0 && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
//remove both
data->path.remove(i - 1);
data->path.remove(i - 1);
i -= 2;
if (data->path.size() == 0) {
data->path.push_back(".");
break;
}
}
}
}
NodePath NodePath::simplified() const {
NodePath np = *this;
np.simplify();
return np;
}
NodePath::NodePath(const String &p_path) {
2014-02-10 01:10:30 +00:00
data = NULL;
2014-02-10 01:10:30 +00:00
if (p_path.length() == 0)
2014-02-10 01:10:30 +00:00
return;
String path = p_path;
2014-02-10 01:10:30 +00:00
StringName property;
Vector<StringName> subpath;
2016-03-08 23:00:52 +00:00
int absolute = (path[0] == '/') ? 1 : 0;
bool last_is_slash = true;
int slices = 0;
int subpath_pos = path.find(":");
2014-02-10 01:10:30 +00:00
if (subpath_pos != -1) {
2016-03-08 23:00:52 +00:00
int from = subpath_pos + 1;
2016-03-08 23:00:52 +00:00
for (int i = from; i <= path.length(); i++) {
2014-02-10 01:10:30 +00:00
if (path[i] == ':' || path[i] == 0) {
2014-02-10 01:10:30 +00:00
String str = path.substr(from, i - from);
if (path[i] == ':') {
if (str == "") {
ERR_EXPLAIN("Invalid NodePath: " + p_path);
2014-02-10 01:10:30 +00:00
ERR_FAIL();
}
subpath.push_back(str);
} else {
//property can be empty
property = str;
2014-02-10 01:10:30 +00:00
}
from = i + 1;
2014-02-10 01:10:30 +00:00
}
}
path = path.substr(0, subpath_pos);
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
for (int i = absolute; i < path.length(); i++) {
2016-03-08 23:00:52 +00:00
if (path[i] == '/') {
2016-03-08 23:00:52 +00:00
last_is_slash = true;
2014-02-10 01:10:30 +00:00
} else {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (last_is_slash)
slices++;
2016-03-08 23:00:52 +00:00
last_is_slash = false;
2014-02-10 01:10:30 +00:00
}
}
2016-03-08 23:00:52 +00:00
if (slices == 0 && !absolute && !property)
2014-02-10 01:10:30 +00:00
return;
2016-03-08 23:00:52 +00:00
data = memnew(Data);
2014-02-10 01:10:30 +00:00
data->refcount.init();
data->absolute = absolute ? true : false;
data->property = property;
data->subpath = subpath;
2016-03-08 23:00:52 +00:00
if (slices == 0)
2014-02-10 01:10:30 +00:00
return;
2016-03-08 23:00:52 +00:00
data->path.resize(slices);
last_is_slash = true;
int from = absolute;
int slice = 0;
2016-03-08 23:00:52 +00:00
for (int i = absolute; i < path.length() + 1; i++) {
2016-03-08 23:00:52 +00:00
if (path[i] == '/' || path[i] == 0) {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (!last_is_slash) {
2016-03-08 23:00:52 +00:00
String name = path.substr(from, i - from);
ERR_FAIL_INDEX(slice, data->path.size());
data->path[slice++] = name;
2014-02-10 01:10:30 +00:00
}
from = i + 1;
last_is_slash = true;
2014-02-10 01:10:30 +00:00
} else {
last_is_slash = false;
2014-02-10 01:10:30 +00:00
}
}
}
bool NodePath::is_empty() const {
return !data;
}
NodePath::NodePath() {
data = NULL;
2014-02-10 01:10:30 +00:00
}
NodePath::~NodePath() {
unref();
}