Path: blob/master/modules/multiplayer/scene_replication_config.cpp
10277 views
/**************************************************************************/1/* scene_replication_config.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "scene_replication_config.h"3132bool SceneReplicationConfig::_set(const StringName &p_name, const Variant &p_value) {33String prop_name = p_name;3435if (prop_name.begins_with("properties/")) {36int idx = prop_name.get_slicec('/', 1).to_int();37String what = prop_name.get_slicec('/', 2);3839if (properties.size() == idx && what == "path") {40ERR_FAIL_COND_V(p_value.get_type() != Variant::NODE_PATH, false);41NodePath path = p_value;42ERR_FAIL_COND_V(path.is_empty() || path.get_subname_count() == 0, false);43add_property(path);44return true;45}46ERR_FAIL_INDEX_V(idx, properties.size(), false);47const ReplicationProperty &prop = properties.get(idx);48if (what == "replication_mode") {49ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);50ReplicationMode mode = (ReplicationMode)p_value.operator int();51ERR_FAIL_COND_V(mode < REPLICATION_MODE_NEVER || mode > REPLICATION_MODE_ON_CHANGE, false);52property_set_replication_mode(prop.name, mode);53return true;54}55ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);56if (what == "spawn") {57property_set_spawn(prop.name, p_value);58return true;59} else if (what == "sync") {60// Deprecated.61property_set_sync(prop.name, p_value);62return true;63} else if (what == "watch") {64// Deprecated.65property_set_watch(prop.name, p_value);66return true;67}68}69return false;70}7172bool SceneReplicationConfig::_get(const StringName &p_name, Variant &r_ret) const {73String prop_name = p_name;7475if (prop_name.begins_with("properties/")) {76int idx = prop_name.get_slicec('/', 1).to_int();77String what = prop_name.get_slicec('/', 2);78ERR_FAIL_INDEX_V(idx, properties.size(), false);79const ReplicationProperty &prop = properties.get(idx);80if (what == "path") {81r_ret = prop.name;82return true;83} else if (what == "spawn") {84r_ret = prop.spawn;85return true;86} else if (what == "replication_mode") {87r_ret = prop.mode;88return true;89}90}91return false;92}9394void SceneReplicationConfig::_get_property_list(List<PropertyInfo> *p_list) const {95for (int i = 0; i < properties.size(); i++) {96p_list->push_back(PropertyInfo(Variant::STRING, "properties/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));97p_list->push_back(PropertyInfo(Variant::STRING, "properties/" + itos(i) + "/spawn", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));98p_list->push_back(PropertyInfo(Variant::INT, "properties/" + itos(i) + "/replication_mode", PROPERTY_HINT_ENUM, "Never,Always,On Change", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));99}100}101102void SceneReplicationConfig::reset_state() {103dirty = false;104properties.clear();105sync_props.clear();106spawn_props.clear();107watch_props.clear();108}109110TypedArray<NodePath> SceneReplicationConfig::get_properties() const {111TypedArray<NodePath> paths;112for (const ReplicationProperty &prop : properties) {113paths.push_back(prop.name);114}115return paths;116}117118void SceneReplicationConfig::add_property(const NodePath &p_path, int p_index) {119ERR_FAIL_COND(properties.find(p_path));120ERR_FAIL_COND(p_path == NodePath());121122if (p_index < 0 || p_index == properties.size()) {123properties.push_back(ReplicationProperty(p_path));124dirty = true;125return;126}127128ERR_FAIL_INDEX(p_index, properties.size());129130List<ReplicationProperty>::Element *I = properties.front();131int c = 0;132while (c < p_index) {133I = I->next();134c++;135}136properties.insert_before(I, ReplicationProperty(p_path));137dirty = true;138}139140void SceneReplicationConfig::remove_property(const NodePath &p_path) {141properties.erase(p_path);142dirty = true;143}144145bool SceneReplicationConfig::has_property(const NodePath &p_path) const {146for (const ReplicationProperty &property : properties) {147if (property.name == p_path) {148return true;149}150}151return false;152}153154int SceneReplicationConfig::property_get_index(const NodePath &p_path) const {155int i = 0;156for (List<ReplicationProperty>::ConstIterator itr = properties.begin(); itr != properties.end(); ++itr, ++i) {157if (itr->name == p_path) {158return i;159}160}161ERR_FAIL_V(-1);162}163164bool SceneReplicationConfig::property_get_spawn(const NodePath &p_path) {165List<ReplicationProperty>::Element *E = properties.find(p_path);166ERR_FAIL_COND_V(!E, false);167return E->get().spawn;168}169170void SceneReplicationConfig::property_set_spawn(const NodePath &p_path, bool p_enabled) {171List<ReplicationProperty>::Element *E = properties.find(p_path);172ERR_FAIL_COND(!E);173if (E->get().spawn == p_enabled) {174return;175}176E->get().spawn = p_enabled;177dirty = true;178}179180bool SceneReplicationConfig::property_get_sync(const NodePath &p_path) {181List<ReplicationProperty>::Element *E = properties.find(p_path);182ERR_FAIL_COND_V(!E, false);183return E->get().mode == REPLICATION_MODE_ALWAYS;184}185186void SceneReplicationConfig::property_set_sync(const NodePath &p_path, bool p_enabled) {187if (p_enabled) {188property_set_replication_mode(p_path, REPLICATION_MODE_ALWAYS);189} else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ALWAYS) {190property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);191}192}193194bool SceneReplicationConfig::property_get_watch(const NodePath &p_path) {195List<ReplicationProperty>::Element *E = properties.find(p_path);196ERR_FAIL_COND_V(!E, false);197return E->get().mode == REPLICATION_MODE_ON_CHANGE;198}199200void SceneReplicationConfig::property_set_watch(const NodePath &p_path, bool p_enabled) {201if (p_enabled) {202property_set_replication_mode(p_path, REPLICATION_MODE_ON_CHANGE);203} else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ON_CHANGE) {204property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);205}206}207208SceneReplicationConfig::ReplicationMode SceneReplicationConfig::property_get_replication_mode(const NodePath &p_path) {209List<ReplicationProperty>::Element *E = properties.find(p_path);210ERR_FAIL_COND_V(!E, REPLICATION_MODE_NEVER);211return E->get().mode;212}213214void SceneReplicationConfig::property_set_replication_mode(const NodePath &p_path, ReplicationMode p_mode) {215List<ReplicationProperty>::Element *E = properties.find(p_path);216ERR_FAIL_COND(!E);217if (E->get().mode == p_mode) {218return;219}220E->get().mode = p_mode;221dirty = true;222}223224void SceneReplicationConfig::_update() {225if (!dirty) {226return;227}228dirty = false;229sync_props.clear();230spawn_props.clear();231watch_props.clear();232for (const ReplicationProperty &prop : properties) {233if (prop.spawn) {234spawn_props.push_back(prop.name);235}236switch (prop.mode) {237case REPLICATION_MODE_ALWAYS:238sync_props.push_back(prop.name);239break;240case REPLICATION_MODE_ON_CHANGE:241watch_props.push_back(prop.name);242break;243default:244break;245}246}247}248249const List<NodePath> &SceneReplicationConfig::get_spawn_properties() {250if (dirty) {251_update();252}253return spawn_props;254}255256const List<NodePath> &SceneReplicationConfig::get_sync_properties() {257if (dirty) {258_update();259}260return sync_props;261}262263const List<NodePath> &SceneReplicationConfig::get_watch_properties() {264if (dirty) {265_update();266}267return watch_props;268}269270void SceneReplicationConfig::_bind_methods() {271ClassDB::bind_method(D_METHOD("get_properties"), &SceneReplicationConfig::get_properties);272ClassDB::bind_method(D_METHOD("add_property", "path", "index"), &SceneReplicationConfig::add_property, DEFVAL(-1));273ClassDB::bind_method(D_METHOD("has_property", "path"), &SceneReplicationConfig::has_property);274ClassDB::bind_method(D_METHOD("remove_property", "path"), &SceneReplicationConfig::remove_property);275ClassDB::bind_method(D_METHOD("property_get_index", "path"), &SceneReplicationConfig::property_get_index);276ClassDB::bind_method(D_METHOD("property_get_spawn", "path"), &SceneReplicationConfig::property_get_spawn);277ClassDB::bind_method(D_METHOD("property_set_spawn", "path", "enabled"), &SceneReplicationConfig::property_set_spawn);278ClassDB::bind_method(D_METHOD("property_get_replication_mode", "path"), &SceneReplicationConfig::property_get_replication_mode);279ClassDB::bind_method(D_METHOD("property_set_replication_mode", "path", "mode"), &SceneReplicationConfig::property_set_replication_mode);280281BIND_ENUM_CONSTANT(REPLICATION_MODE_NEVER);282BIND_ENUM_CONSTANT(REPLICATION_MODE_ALWAYS);283BIND_ENUM_CONSTANT(REPLICATION_MODE_ON_CHANGE);284285// Deprecated.286ClassDB::bind_method(D_METHOD("property_get_sync", "path"), &SceneReplicationConfig::property_get_sync);287ClassDB::bind_method(D_METHOD("property_set_sync", "path", "enabled"), &SceneReplicationConfig::property_set_sync);288ClassDB::bind_method(D_METHOD("property_get_watch", "path"), &SceneReplicationConfig::property_get_watch);289ClassDB::bind_method(D_METHOD("property_set_watch", "path", "enabled"), &SceneReplicationConfig::property_set_watch);290}291292293