3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "rollback_interface.h"
22 #include "util/serialize.h"
23 #include "util/string.h"
24 #include "util/numeric.h"
25 #include "util/basic_macros.h"
29 #include "nodemetadata.h"
30 #include "exceptions.h"
32 #include "inventorymanager.h"
33 #include "inventory.h"
37 RollbackNode::RollbackNode(Map *map, v3s16 p, IGameDef *gamedef)
39 INodeDefManager *ndef = gamedef->ndef();
40 MapNode n = map->getNodeNoEx(p);
41 name = ndef->get(n).name;
44 NodeMetadata *metap = map->getNodeMetadata(p);
46 std::ostringstream os(std::ios::binary);
47 metap->serialize(os, 1); // FIXME: version bump??
53 std::string RollbackAction::toString() const
55 std::ostringstream os(std::ios::binary);
58 os << "set_node " << PP(p);
59 os << ": (" << serializeJsonString(n_old.name);
60 os << ", " << itos(n_old.param1);
61 os << ", " << itos(n_old.param2);
62 os << ", " << serializeJsonString(n_old.meta);
63 os << ") -> (" << serializeJsonString(n_new.name);
64 os << ", " << itos(n_new.param1);
65 os << ", " << itos(n_new.param2);
66 os << ", " << serializeJsonString(n_new.meta);
68 case TYPE_MODIFY_INVENTORY_STACK:
69 os << "modify_inventory_stack (";
70 os << serializeJsonString(inventory_location);
71 os << ", " << serializeJsonString(inventory_list);
72 os << ", " << inventory_index;
73 os << ", " << (inventory_add ? "add" : "remove");
74 os << ", " << serializeJsonString(inventory_stack.getItemString());
77 return "<unknown action>";
83 bool RollbackAction::isImportant(IGameDef *gamedef) const
85 if (type != TYPE_SET_NODE)
87 // If names differ, action is always important
88 if(n_old.name != n_new.name)
90 // If metadata differs, action is always important
91 if(n_old.meta != n_new.meta)
93 INodeDefManager *ndef = gamedef->ndef();
94 // Both are of the same name, so a single definition is needed
95 const ContentFeatures &def = ndef->get(n_old.name);
96 // If the type is flowing liquid, action is not important
97 if (def.liquid_type == LIQUID_FLOWING)
99 // Otherwise action is important
104 bool RollbackAction::getPosition(v3s16 *dst) const
110 case TYPE_MODIFY_INVENTORY_STACK: {
111 InventoryLocation loc;
112 loc.deSerialize(inventory_location);
113 if (loc.type != InventoryLocation::NODEMETA) {
116 if (dst) *dst = loc.p;
124 bool RollbackAction::applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const
130 case TYPE_SET_NODE: {
131 INodeDefManager *ndef = gamedef->ndef();
132 // Make sure position is loaded from disk
133 map->emergeBlock(getContainerPos(p, MAP_BLOCKSIZE), false);
134 // Check current node
135 MapNode current_node = map->getNodeNoEx(p);
136 std::string current_name = ndef->get(current_node).name;
137 // If current node not the new node, it's bad
138 if (current_name != n_new.name) {
141 // Create rollback node
142 MapNode n(ndef, n_old.name, n_old.param1, n_old.param2);
145 if (!map->addNodeWithEvent(p, n)) {
146 infostream << "RollbackAction::applyRevert(): "
147 << "AddNodeWithEvent failed at "
148 << PP(p) << " for " << n_old.name
152 if (n_old.meta.empty()) {
153 map->removeNodeMetadata(p);
155 NodeMetadata *meta = map->getNodeMetadata(p);
157 meta = new NodeMetadata(gamedef->idef());
158 if (!map->setNodeMetadata(p, meta)) {
160 infostream << "RollbackAction::applyRevert(): "
161 << "setNodeMetadata failed at "
162 << PP(p) << " for " << n_old.name
167 std::istringstream is(n_old.meta, std::ios::binary);
168 meta->deSerialize(is, 1); // FIXME: version bump??
170 // Inform other things that the meta data has changed
171 v3s16 blockpos = getContainerPos(p, MAP_BLOCKSIZE);
173 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
175 map->dispatchEvent(&event);
176 // Set the block to be saved
177 MapBlock *block = map->getBlockNoCreateNoEx(blockpos);
179 block->raiseModified(MOD_STATE_WRITE_NEEDED,
180 MOD_REASON_REPORT_META_CHANGE);
182 } catch (InvalidPositionException &e) {
183 infostream << "RollbackAction::applyRevert(): "
184 << "InvalidPositionException: " << e.what()
190 case TYPE_MODIFY_INVENTORY_STACK: {
191 InventoryLocation loc;
192 loc.deSerialize(inventory_location);
193 Inventory *inv = imgr->getInventory(loc);
195 infostream << "RollbackAction::applyRevert(): Could not get "
196 "inventory at " << inventory_location << std::endl;
199 InventoryList *list = inv->getList(inventory_list);
201 infostream << "RollbackAction::applyRevert(): Could not get "
202 "inventory list \"" << inventory_list << "\" in "
203 << inventory_location << std::endl;
206 if (list->getSize() <= inventory_index) {
207 infostream << "RollbackAction::applyRevert(): List index "
208 << inventory_index << " too large in "
209 << "inventory list \"" << inventory_list << "\" in "
210 << inventory_location << std::endl;
214 // If item was added, take away item, otherwise add removed item
216 // Silently ignore different current item
217 if (list->getItem(inventory_index).name !=
218 gamedef->idef()->getAlias(inventory_stack.name))
220 list->takeItem(inventory_index, inventory_stack.count);
222 list->addItem(inventory_index, inventory_stack);
224 // Inventory was modified; send to clients
225 imgr->setInventoryModified(loc);
228 errorstream << "RollbackAction::applyRevert(): type not handled"
232 } catch(SerializationError &e) {
233 errorstream << "RollbackAction::applyRevert(): n_old.name=" << n_old.name
234 << ", SerializationError: " << e.what() << std::endl;