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.
28 #include "util/serialize.h"
29 #include "util/string.h"
30 #include "util/numeric.h"
31 #include "inventorymanager.h" // deserializing InventoryLocations
35 #define POINTS_PER_NODE (16.0)
37 #define SQLRES(f, good) \
39 throw FileNotGoodException(std::string("RollbackManager: " \
40 "SQLite3 error (" __FILE__ ":" TOSTRING(__LINE__) \
41 "): ") + sqlite3_errmsg(db)); \
43 #define SQLOK(f) SQLRES(f, SQLITE_OK)
45 #define SQLOK_ERRSTREAM(s, m) \
46 if ((s) != SQLITE_OK) { \
47 errorstream << "RollbackManager: " << (m) << ": " \
48 << sqlite3_errmsg(db) << std::endl; \
51 #define FINALIZE_STATEMENT(statement) \
52 SQLOK_ERRSTREAM(sqlite3_finalize(statement), "Failed to finalize " #statement)
54 class ItemStackRow : public ItemStack {
56 ItemStackRow & operator = (const ItemStack & other)
58 *static_cast<ItemStack *>(this) = other;
70 std::string location, list;
76 int oldParam1, oldParam2;
79 int newParam1, newParam2;
92 RollbackManager::RollbackManager(const std::string & world_path,
93 IGameDef * gamedef_) :
95 current_actor_is_guess(false)
97 verbosestream << "RollbackManager::RollbackManager(" << world_path
100 std::string txt_filename = world_path + DIR_DELIM "rollback.txt";
101 std::string migrating_flag = txt_filename + ".migrating";
102 database_path = world_path + DIR_DELIM "rollback.sqlite";
104 bool created = initDatabase();
106 if (fs::PathExists(txt_filename) && (created ||
107 fs::PathExists(migrating_flag))) {
108 std::ofstream of(migrating_flag.c_str());
110 migrate(txt_filename);
111 fs::DeleteSingleFileOrEmptyDirectory(migrating_flag);
116 RollbackManager::~RollbackManager()
120 FINALIZE_STATEMENT(stmt_insert);
121 FINALIZE_STATEMENT(stmt_replace);
122 FINALIZE_STATEMENT(stmt_select);
123 FINALIZE_STATEMENT(stmt_select_range);
124 FINALIZE_STATEMENT(stmt_select_withActor);
125 FINALIZE_STATEMENT(stmt_knownActor_select);
126 FINALIZE_STATEMENT(stmt_knownActor_insert);
127 FINALIZE_STATEMENT(stmt_knownNode_select);
128 FINALIZE_STATEMENT(stmt_knownNode_insert);
130 SQLOK_ERRSTREAM(sqlite3_close(db), "Could not close db");
134 void RollbackManager::registerNewActor(const int id, const std::string &name)
136 Entity actor = {id, name};
137 knownActors.push_back(actor);
141 void RollbackManager::registerNewNode(const int id, const std::string &name)
143 Entity node = {id, name};
144 knownNodes.push_back(node);
148 int RollbackManager::getActorId(const std::string &name)
150 for (std::vector<Entity>::const_iterator iter = knownActors.begin();
151 iter != knownActors.end(); ++iter) {
152 if (iter->name == name) {
157 SQLOK(sqlite3_bind_text(stmt_knownActor_insert, 1, name.c_str(), name.size(), NULL));
158 SQLRES(sqlite3_step(stmt_knownActor_insert), SQLITE_DONE);
159 SQLOK(sqlite3_reset(stmt_knownActor_insert));
161 int id = sqlite3_last_insert_rowid(db);
162 registerNewActor(id, name);
168 int RollbackManager::getNodeId(const std::string &name)
170 for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
171 iter != knownNodes.end(); ++iter) {
172 if (iter->name == name) {
177 SQLOK(sqlite3_bind_text(stmt_knownNode_insert, 1, name.c_str(), name.size(), NULL));
178 SQLRES(sqlite3_step(stmt_knownNode_insert), SQLITE_DONE);
179 SQLOK(sqlite3_reset(stmt_knownNode_insert));
181 int id = sqlite3_last_insert_rowid(db);
182 registerNewNode(id, name);
188 const char * RollbackManager::getActorName(const int id)
190 for (std::vector<Entity>::const_iterator iter = knownActors.begin();
191 iter != knownActors.end(); ++iter) {
192 if (iter->id == id) {
193 return iter->name.c_str();
201 const char * RollbackManager::getNodeName(const int id)
203 for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
204 iter != knownNodes.end(); ++iter) {
205 if (iter->id == id) {
206 return iter->name.c_str();
214 bool RollbackManager::createTables()
216 SQLOK(sqlite3_exec(db,
217 "CREATE TABLE IF NOT EXISTS `actor` (\n"
218 " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
219 " `name` TEXT NOT NULL\n"
221 "CREATE TABLE IF NOT EXISTS `node` (\n"
222 " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
223 " `name` TEXT NOT NULL\n"
225 "CREATE TABLE IF NOT EXISTS `action` (\n"
226 " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n"
227 " `actor` INTEGER NOT NULL,\n"
228 " `timestamp` TIMESTAMP NOT NULL,\n"
229 " `type` INTEGER NOT NULL,\n"
231 " `index` INTEGER,\n"
233 " `stackNode` INTEGER,\n"
234 " `stackQuantity` INTEGER,\n"
235 " `nodeMeta` INTEGER,\n"
239 " `oldNode` INTEGER,\n"
240 " `oldParam1` INTEGER,\n"
241 " `oldParam2` INTEGER,\n"
243 " `newNode` INTEGER,\n"
244 " `newParam1` INTEGER,\n"
245 " `newParam2` INTEGER,\n"
247 " `guessedActor` INTEGER,\n"
248 " FOREIGN KEY (`actor`) REFERENCES `actor`(`id`),\n"
249 " FOREIGN KEY (`stackNode`) REFERENCES `node`(`id`),\n"
250 " FOREIGN KEY (`oldNode`) REFERENCES `node`(`id`),\n"
251 " FOREIGN KEY (`newNode`) REFERENCES `node`(`id`)\n"
253 "CREATE INDEX IF NOT EXISTS `actionIndex` ON `action`(`x`,`y`,`z`,`timestamp`,`actor`);\n",
255 verbosestream << "SQL Rollback: SQLite3 database structure was created" << std::endl;
261 bool RollbackManager::initDatabase()
263 verbosestream << "RollbackManager: Database connection setup" << std::endl;
265 bool needs_create = !fs::PathExists(database_path);
266 SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
267 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
273 SQLOK(sqlite3_prepare_v2(db,
274 "INSERT INTO `action` (\n"
275 " `actor`, `timestamp`, `type`,\n"
276 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
278 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
279 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
283 " ?, ?, ?, ?, ?, ?,\n"
289 -1, &stmt_insert, NULL));
291 SQLOK(sqlite3_prepare_v2(db,
292 "REPLACE INTO `action` (\n"
293 " `actor`, `timestamp`, `type`,\n"
294 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
296 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
297 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
298 " `guessedActor`, `id`\n"
301 " ?, ?, ?, ?, ?, ?,\n"
307 -1, &stmt_replace, NULL));
309 SQLOK(sqlite3_prepare_v2(db,
311 " `actor`, `timestamp`, `type`,\n"
312 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
314 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
315 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
318 " WHERE `timestamp` >= ?\n"
319 " ORDER BY `timestamp` DESC, `id` DESC",
320 -1, &stmt_select, NULL));
322 SQLOK(sqlite3_prepare_v2(db,
324 " `actor`, `timestamp`, `type`,\n"
325 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
327 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
328 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
331 "WHERE `timestamp` >= ?\n"
332 " AND `x` IS NOT NULL\n"
333 " AND `y` IS NOT NULL\n"
334 " AND `z` IS NOT NULL\n"
335 " AND `x` BETWEEN ? AND ?\n"
336 " AND `y` BETWEEN ? AND ?\n"
337 " AND `z` BETWEEN ? AND ?\n"
338 "ORDER BY `timestamp` DESC, `id` DESC\n"
340 -1, &stmt_select_range, NULL));
342 SQLOK(sqlite3_prepare_v2(db,
344 " `actor`, `timestamp`, `type`,\n"
345 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
347 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
348 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
351 "WHERE `timestamp` >= ?\n"
353 "ORDER BY `timestamp` DESC, `id` DESC\n",
354 -1, &stmt_select_withActor, NULL));
356 SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `actor`",
357 -1, &stmt_knownActor_select, NULL));
359 SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `actor` (`name`) VALUES (?)",
360 -1, &stmt_knownActor_insert, NULL));
362 SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `node`",
363 -1, &stmt_knownNode_select, NULL));
365 SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `node` (`name`) VALUES (?)",
366 -1, &stmt_knownNode_insert, NULL));
368 verbosestream << "SQL prepared statements setup correctly" << std::endl;
370 while (sqlite3_step(stmt_knownActor_select) == SQLITE_ROW) {
372 sqlite3_column_int(stmt_knownActor_select, 0),
373 reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownActor_select, 1))
376 SQLOK(sqlite3_reset(stmt_knownActor_select));
378 while (sqlite3_step(stmt_knownNode_select) == SQLITE_ROW) {
380 sqlite3_column_int(stmt_knownNode_select, 0),
381 reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownNode_select, 1))
384 SQLOK(sqlite3_reset(stmt_knownNode_select));
390 bool RollbackManager::registerRow(const ActionRow & row)
392 sqlite3_stmt * stmt_do = (row.id) ? stmt_replace : stmt_insert;
394 bool nodeMeta = false;
396 SQLOK(sqlite3_bind_int (stmt_do, 1, row.actor));
397 SQLOK(sqlite3_bind_int64(stmt_do, 2, row.timestamp));
398 SQLOK(sqlite3_bind_int (stmt_do, 3, row.type));
400 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
401 const std::string & loc = row.location;
402 nodeMeta = (loc.substr(0, 9) == "nodemeta:");
404 SQLOK(sqlite3_bind_text(stmt_do, 4, row.list.c_str(), row.list.size(), NULL));
405 SQLOK(sqlite3_bind_int (stmt_do, 5, row.index));
406 SQLOK(sqlite3_bind_int (stmt_do, 6, row.add));
407 SQLOK(sqlite3_bind_int (stmt_do, 7, row.stack.id));
408 SQLOK(sqlite3_bind_int (stmt_do, 8, row.stack.count));
409 SQLOK(sqlite3_bind_int (stmt_do, 9, (int) nodeMeta));
412 std::string::size_type p1, p2;
413 p1 = loc.find(':') + 1;
415 std::string x = loc.substr(p1, p2 - p1);
417 p2 = loc.find(',', p1);
418 std::string y = loc.substr(p1, p2 - p1);
419 std::string z = loc.substr(p2 + 1);
420 SQLOK(sqlite3_bind_int(stmt_do, 10, atoi(x.c_str())));
421 SQLOK(sqlite3_bind_int(stmt_do, 11, atoi(y.c_str())));
422 SQLOK(sqlite3_bind_int(stmt_do, 12, atoi(z.c_str())));
425 SQLOK(sqlite3_bind_null(stmt_do, 4));
426 SQLOK(sqlite3_bind_null(stmt_do, 5));
427 SQLOK(sqlite3_bind_null(stmt_do, 6));
428 SQLOK(sqlite3_bind_null(stmt_do, 7));
429 SQLOK(sqlite3_bind_null(stmt_do, 8));
430 SQLOK(sqlite3_bind_null(stmt_do, 9));
433 if (row.type == RollbackAction::TYPE_SET_NODE) {
434 SQLOK(sqlite3_bind_int (stmt_do, 10, row.x));
435 SQLOK(sqlite3_bind_int (stmt_do, 11, row.y));
436 SQLOK(sqlite3_bind_int (stmt_do, 12, row.z));
437 SQLOK(sqlite3_bind_int (stmt_do, 13, row.oldNode));
438 SQLOK(sqlite3_bind_int (stmt_do, 14, row.oldParam1));
439 SQLOK(sqlite3_bind_int (stmt_do, 15, row.oldParam2));
440 SQLOK(sqlite3_bind_text(stmt_do, 16, row.oldMeta.c_str(), row.oldMeta.size(), NULL));
441 SQLOK(sqlite3_bind_int (stmt_do, 17, row.newNode));
442 SQLOK(sqlite3_bind_int (stmt_do, 18, row.newParam1));
443 SQLOK(sqlite3_bind_int (stmt_do, 19, row.newParam2));
444 SQLOK(sqlite3_bind_text(stmt_do, 20, row.newMeta.c_str(), row.newMeta.size(), NULL));
445 SQLOK(sqlite3_bind_int (stmt_do, 21, row.guessed ? 1 : 0));
448 SQLOK(sqlite3_bind_null(stmt_do, 10));
449 SQLOK(sqlite3_bind_null(stmt_do, 11));
450 SQLOK(sqlite3_bind_null(stmt_do, 12));
452 SQLOK(sqlite3_bind_null(stmt_do, 13));
453 SQLOK(sqlite3_bind_null(stmt_do, 14));
454 SQLOK(sqlite3_bind_null(stmt_do, 15));
455 SQLOK(sqlite3_bind_null(stmt_do, 16));
456 SQLOK(sqlite3_bind_null(stmt_do, 17));
457 SQLOK(sqlite3_bind_null(stmt_do, 18));
458 SQLOK(sqlite3_bind_null(stmt_do, 19));
459 SQLOK(sqlite3_bind_null(stmt_do, 20));
460 SQLOK(sqlite3_bind_null(stmt_do, 21));
464 SQLOK(sqlite3_bind_int(stmt_do, 22, row.id));
467 int written = sqlite3_step(stmt_do);
469 SQLOK(sqlite3_reset(stmt_do));
471 return written == SQLITE_DONE;
475 const std::list<ActionRow> RollbackManager::actionRowsFromSelect(sqlite3_stmt* stmt)
477 std::list<ActionRow> rows;
478 const unsigned char * text;
481 while (sqlite3_step(stmt) == SQLITE_ROW) {
484 row.actor = sqlite3_column_int (stmt, 0);
485 row.timestamp = sqlite3_column_int64(stmt, 1);
486 row.type = sqlite3_column_int (stmt, 2);
488 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
489 text = sqlite3_column_text (stmt, 3);
490 size = sqlite3_column_bytes(stmt, 3);
491 row.list = std::string(reinterpret_cast<const char*>(text), size);
492 row.index = sqlite3_column_int(stmt, 4);
493 row.add = sqlite3_column_int(stmt, 5);
494 row.stack.id = sqlite3_column_int(stmt, 6);
495 row.stack.count = sqlite3_column_int(stmt, 7);
496 row.nodeMeta = sqlite3_column_int(stmt, 8);
499 if (row.type == RollbackAction::TYPE_SET_NODE || row.nodeMeta) {
500 row.x = sqlite3_column_int(stmt, 9);
501 row.y = sqlite3_column_int(stmt, 10);
502 row.z = sqlite3_column_int(stmt, 11);
505 if (row.type == RollbackAction::TYPE_SET_NODE) {
506 row.oldNode = sqlite3_column_int(stmt, 12);
507 row.oldParam1 = sqlite3_column_int(stmt, 13);
508 row.oldParam2 = sqlite3_column_int(stmt, 14);
509 text = sqlite3_column_text (stmt, 15);
510 size = sqlite3_column_bytes(stmt, 15);
511 row.oldMeta = std::string(reinterpret_cast<const char*>(text), size);
512 row.newNode = sqlite3_column_int(stmt, 16);
513 row.newParam1 = sqlite3_column_int(stmt, 17);
514 row.newParam2 = sqlite3_column_int(stmt, 18);
515 text = sqlite3_column_text(stmt, 19);
516 size = sqlite3_column_bytes(stmt, 19);
517 row.newMeta = std::string(reinterpret_cast<const char*>(text), size);
518 row.guessed = sqlite3_column_int(stmt, 20);
522 row.location = "nodemeta:";
523 row.location += itos(row.x);
525 row.location += itos(row.y);
527 row.location += itos(row.z);
529 row.location = getActorName(row.actor);
535 SQLOK(sqlite3_reset(stmt));
541 ActionRow RollbackManager::actionRowFromRollbackAction(const RollbackAction & action)
546 row.actor = getActorId(action.actor);
547 row.timestamp = action.unix_time;
548 row.type = action.type;
550 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
551 row.location = action.inventory_location;
552 row.list = action.inventory_list;
553 row.index = action.inventory_index;
554 row.add = action.inventory_add;
555 row.stack = action.inventory_stack;
556 row.stack.id = getNodeId(row.stack.name);
561 row.oldNode = getNodeId(action.n_old.name);
562 row.oldParam1 = action.n_old.param1;
563 row.oldParam2 = action.n_old.param2;
564 row.oldMeta = action.n_old.meta;
565 row.newNode = getNodeId(action.n_new.name);
566 row.newParam1 = action.n_new.param1;
567 row.newParam2 = action.n_new.param2;
568 row.newMeta = action.n_new.meta;
569 row.guessed = action.actor_is_guess;
576 const std::list<RollbackAction> RollbackManager::rollbackActionsFromActionRows(
577 const std::list<ActionRow> & rows)
579 std::list<RollbackAction> actions;
581 for (std::list<ActionRow>::const_iterator it = rows.begin();
582 it != rows.end(); ++it) {
583 RollbackAction action;
584 action.actor = (it->actor) ? getActorName(it->actor) : "";
585 action.unix_time = it->timestamp;
586 action.type = static_cast<RollbackAction::Type>(it->type);
588 switch (action.type) {
589 case RollbackAction::TYPE_MODIFY_INVENTORY_STACK:
590 action.inventory_location = it->location.c_str();
591 action.inventory_list = it->list;
592 action.inventory_index = it->index;
593 action.inventory_add = it->add;
594 action.inventory_stack = it->stack;
595 if (action.inventory_stack.name.empty()) {
596 action.inventory_stack.name = getNodeName(it->stack.id);
600 case RollbackAction::TYPE_SET_NODE:
601 action.p = v3s16(it->x, it->y, it->z);
602 action.n_old.name = getNodeName(it->oldNode);
603 action.n_old.param1 = it->oldParam1;
604 action.n_old.param2 = it->oldParam2;
605 action.n_old.meta = it->oldMeta;
606 action.n_new.name = getNodeName(it->newNode);
607 action.n_new.param1 = it->newParam1;
608 action.n_new.param2 = it->newParam2;
609 action.n_new.meta = it->newMeta;
617 actions.push_back(action);
624 const std::list<ActionRow> RollbackManager::getRowsSince(time_t firstTime, const std::string & actor)
626 sqlite3_stmt *stmt_stmt = actor.empty() ? stmt_select : stmt_select_withActor;
627 sqlite3_bind_int64(stmt_stmt, 1, firstTime);
629 if (!actor.empty()) {
630 sqlite3_bind_int(stmt_stmt, 2, getActorId(actor));
633 const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_stmt);
634 sqlite3_reset(stmt_stmt);
640 const std::list<ActionRow> RollbackManager::getRowsSince_range(
641 time_t start_time, v3s16 p, int range, int limit)
644 sqlite3_bind_int64(stmt_select_range, 1, start_time);
645 sqlite3_bind_int (stmt_select_range, 2, static_cast<int>(p.X - range));
646 sqlite3_bind_int (stmt_select_range, 3, static_cast<int>(p.X + range));
647 sqlite3_bind_int (stmt_select_range, 4, static_cast<int>(p.Y - range));
648 sqlite3_bind_int (stmt_select_range, 5, static_cast<int>(p.Y + range));
649 sqlite3_bind_int (stmt_select_range, 6, static_cast<int>(p.Z - range));
650 sqlite3_bind_int (stmt_select_range, 7, static_cast<int>(p.Z + range));
651 sqlite3_bind_int (stmt_select_range, 8, limit);
653 const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_select_range);
654 sqlite3_reset(stmt_select_range);
660 const std::list<RollbackAction> RollbackManager::getActionsSince_range(
661 time_t start_time, v3s16 p, int range, int limit)
663 return rollbackActionsFromActionRows(getRowsSince_range(start_time, p, range, limit));
667 const std::list<RollbackAction> RollbackManager::getActionsSince(
668 time_t start_time, const std::string & actor)
670 return rollbackActionsFromActionRows(getRowsSince(start_time, actor));
674 void RollbackManager::migrate(const std::string & file_path)
676 std::cout << "Migrating from rollback.txt to rollback.sqlite." << std::endl;
678 std::ifstream fh(file_path.c_str(), std::ios::in | std::ios::ate);
680 throw FileNotGoodException("Unable to open rollback.txt");
683 std::streampos file_size = fh.tellg();
685 if (file_size < 10) {
686 errorstream << "Empty rollback log." << std::endl;
692 sqlite3_stmt *stmt_begin;
693 sqlite3_stmt *stmt_commit;
694 SQLOK(sqlite3_prepare_v2(db, "BEGIN", -1, &stmt_begin, NULL));
695 SQLOK(sqlite3_prepare_v2(db, "COMMIT", -1, &stmt_commit, NULL));
699 time_t start = time(0);
701 SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
702 sqlite3_reset(stmt_begin);
708 std::getline(fh, bit, ' ');
710 if (!atoi(bit.c_str())) {
711 std::getline(fh, bit);
714 row.timestamp = atoi(bit.c_str());
717 row.actor = getActorId(deSerializeJsonString(fh));
719 // Get the action type
720 std::getline(fh, bit, '[');
721 std::getline(fh, bit, ' ');
723 if (bit == "modify_inventory_stack") {
724 row.type = RollbackAction::TYPE_MODIFY_INVENTORY_STACK;
725 row.location = trim(deSerializeJsonString(fh));
726 std::getline(fh, bit, ' ');
727 row.list = trim(deSerializeJsonString(fh));
728 std::getline(fh, bit, ' ');
729 std::getline(fh, bit, ' ');
730 row.index = atoi(trim(bit).c_str());
731 std::getline(fh, bit, ' ');
732 row.add = (int)(trim(bit) == "add");
733 row.stack.deSerialize(deSerializeJsonString(fh));
734 row.stack.id = getNodeId(row.stack.name);
735 std::getline(fh, bit);
736 } else if (bit == "set_node") {
737 row.type = RollbackAction::TYPE_SET_NODE;
738 std::getline(fh, bit, '(');
739 std::getline(fh, bit, ',');
740 row.x = atoi(trim(bit).c_str());
741 std::getline(fh, bit, ',');
742 row.y = atoi(trim(bit).c_str());
743 std::getline(fh, bit, ')');
744 row.z = atoi(trim(bit).c_str());
745 std::getline(fh, bit, ' ');
746 row.oldNode = getNodeId(trim(deSerializeJsonString(fh)));
747 std::getline(fh, bit, ' ');
748 std::getline(fh, bit, ' ');
749 row.oldParam1 = atoi(trim(bit).c_str());
750 std::getline(fh, bit, ' ');
751 row.oldParam2 = atoi(trim(bit).c_str());
752 row.oldMeta = trim(deSerializeJsonString(fh));
753 std::getline(fh, bit, ' ');
754 row.newNode = getNodeId(trim(deSerializeJsonString(fh)));
755 std::getline(fh, bit, ' ');
756 std::getline(fh, bit, ' ');
757 row.newParam1 = atoi(trim(bit).c_str());
758 std::getline(fh, bit, ' ');
759 row.newParam2 = atoi(trim(bit).c_str());
760 row.newMeta = trim(deSerializeJsonString(fh));
761 std::getline(fh, bit, ' ');
762 std::getline(fh, bit, ' ');
763 std::getline(fh, bit);
764 row.guessed = (int)(trim(bit) == "actor_is_guess");
766 errorstream << "Unrecognized rollback action type \""
767 << bit << "\"!" << std::endl;
774 if (time(0) - t >= 1) {
775 SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE);
776 sqlite3_reset(stmt_commit);
779 << " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%"
780 << " Speed: " << i / (t - start) << "/second \r" << std::flush;
781 SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
782 sqlite3_reset(stmt_begin);
785 SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE);
786 sqlite3_reset(stmt_commit);
788 SQLOK(sqlite3_finalize(stmt_begin));
789 SQLOK(sqlite3_finalize(stmt_commit));
792 << " Done: 100% " << std::endl
793 << "Now you can delete the old rollback.txt file." << std::endl;
797 // Get nearness factor for subject's action for this action
798 // Return value: 0 = impossible, >0 = factor
799 float RollbackManager::getSuspectNearness(bool is_guess, v3s16 suspect_p,
800 time_t suspect_t, v3s16 action_p, time_t action_t)
802 // Suspect cannot cause things in the past
803 if (action_t < suspect_t) {
804 return 0; // 0 = cannot be
808 // Distance (1 node = -x points)
809 f -= POINTS_PER_NODE * intToFloat(suspect_p, 1).getDistanceFrom(intToFloat(action_p, 1));
810 // Time (1 second = -x points)
811 f -= 1 * (action_t - suspect_t);
812 // If is a guess, halve the points
824 void RollbackManager::reportAction(const RollbackAction &action_)
826 // Ignore if not important
827 if (!action_.isImportant(gamedef)) {
831 RollbackAction action = action_;
832 action.unix_time = time(0);
835 action.actor = current_actor;
836 action.actor_is_guess = current_actor_is_guess;
838 if (action.actor.empty()) { // If actor is not known, find out suspect or cancel
840 if (!action.getPosition(&p)) {
844 action.actor = getSuspect(p, 83, 1);
845 if (action.actor.empty()) {
849 action.actor_is_guess = true;
855 std::string RollbackManager::getActor()
857 return current_actor;
860 bool RollbackManager::isActorGuess()
862 return current_actor_is_guess;
865 void RollbackManager::setActor(const std::string & actor, bool is_guess)
867 current_actor = actor;
868 current_actor_is_guess = is_guess;
871 std::string RollbackManager::getSuspect(v3s16 p, float nearness_shortcut,
874 if (current_actor != "") {
875 return current_actor;
877 int cur_time = time(0);
878 time_t first_time = cur_time - (100 - min_nearness);
879 RollbackAction likely_suspect;
880 float likely_suspect_nearness = 0;
881 for (std::list<RollbackAction>::const_reverse_iterator
882 i = action_latest_buffer.rbegin();
883 i != action_latest_buffer.rend(); ++i) {
884 if (i->unix_time < first_time) {
887 if (i->actor == "") {
890 // Find position of suspect or continue
892 if (!i->getPosition(&suspect_p)) {
895 float f = getSuspectNearness(i->actor_is_guess, suspect_p,
896 i->unix_time, p, cur_time);
897 if (f >= min_nearness && f > likely_suspect_nearness) {
898 likely_suspect_nearness = f;
900 if (likely_suspect_nearness >= nearness_shortcut) {
905 // No likely suspect was found
906 if (likely_suspect_nearness == 0) {
909 // Likely suspect was found
910 return likely_suspect.actor;
914 void RollbackManager::flush()
916 sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
918 std::list<RollbackAction>::const_iterator iter;
920 for (iter = action_todisk_buffer.begin();
921 iter != action_todisk_buffer.end();
923 if (iter->actor == "") {
927 registerRow(actionRowFromRollbackAction(*iter));
930 sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
931 action_todisk_buffer.clear();
935 void RollbackManager::addAction(const RollbackAction & action)
937 action_todisk_buffer.push_back(action);
938 action_latest_buffer.push_back(action);
940 // Flush to disk sometimes
941 if (action_todisk_buffer.size() >= 500) {
946 std::list<RollbackAction> RollbackManager::getEntriesSince(time_t first_time)
949 return getActionsSince(first_time);
952 std::list<RollbackAction> RollbackManager::getNodeActors(v3s16 pos, int range,
953 time_t seconds, int limit)
956 time_t cur_time = time(0);
957 time_t first_time = cur_time - seconds;
959 return getActionsSince_range(first_time, pos, range, limit);
962 std::list<RollbackAction> RollbackManager::getRevertActions(
963 const std::string &actor_filter,
966 time_t cur_time = time(0);
967 time_t first_time = cur_time - seconds;
971 return getActionsSince(first_time, actor_filter);