aboutsummaryrefslogtreecommitdiff
path: root/old/changelog.txt
blob: b27fc506bb190735534bd5fe93cd743ac1dc693e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Minetest-c55 changelog
----------------------
This should contain all the major changes.
For minor stuff, refer to the commit log of the repository.

0.3.1: (released on 2011-11-09)
- Fix frustum culling (previous versions have rendered too much stuff that is not actually visible (about 180 degrees, while should have been more like 100.))
- Add occlusion culling (improves performance a lot)
- Add “3d clouds” on/off checkbox in main menu
- Add “opaque water” on/off checkbox
- Fix some random minor stuff
- Turn mipmapping off (This makes far-away textures a bit noisier but better looking)
- Add Command-line signal handler for Windows (contributed by SpeedProg)
- Fix network layer segmentation fault introduced in 0.3.dev-20111021
- Fix water-glass and water-lava and lava-glass surfaces

0.3.0: (released on 2011-11-01)
- Some small fixes
0.3.dev-20111021:
- Modify dungeon masters to only try to shoot players
- Fix object duplication bug at block load/unload bug
- Improve network layer
0.3.dev-20111016:
- Locked chest (contributed)
- Server user limit setting (max_users)
- Wielded tool is shown in HUD (contributed)
- View bobbing (contributed)
- Saplings that drop from leaf blocks and when placed on ground will grow to trees (contributed)
- Optimized map saving (does not re-save everything all the time)
- New mob system and new mob: dungeon master
- Death/respawn screen

0.2.20110922_3:
- Fix the build for MSVC2010; also released for windows using MSVC2010.

0.2.20110922_1:
- Make client report a newer version number to the server than 2011-07-31 does and make server disallow old clients

0.2.20110922:
- Map is saved in an SQLite database file (by Queatz)
- Ladders (MarkTraceur)
- Lava
- Apple trees (sfan5)
- Slightly better looking inventory with transparency
- /me chat command (Oblomov)
- Using chosen map seed possible through fixed_map_seed configuration option (kahrl)
- Fix the long-existed PeerNotFound loop bug
- Some translations and localization-related fixes
- Lots of small fixes

2011-07-31_3:
- Fixes a bug that made the server to deny non-empty passwords from players connecting the first time

2011-07-31_2:
- Fixes a bug that caused the server to always read an empty password from the client when a client connected.

2011-07-31:
- A number of small fixes, build system stuff and such (refer to version control log)
- Map generator no longer crashes at generation limit
- Fixed mapgen producing lots of cut-down trees
- Some minor tweaks in map generator (some contributed)
- Volumetric clouds (contributed)
- Icon added (graphic contributed)
- Key configuration menu (contributed)
- Decorative blocks and items: bookshelf, sandstone, cactus, clay, brick, papyrus, rail, paper, book (contributed)
- Jungles!
- Hotbar is a bit smaller
- Health is now enabled by default; You can now eat cooked rats to heal yourself.
- Finally added sword textures, altough sword is still of no use
- Creative mode now preserves normal mode inventory

2011-07-04:
- Many small fixes
- Code reorganizing to aid further development
- Renewed map generator

2011-06-02:
- Password crash on windows fixed
- Optimized server CPU usage a lot
- Furnaces now work also while players are not near to them

2011-05-29:
- Optimized smooth lighting
- A number of small fixes
- Added clouds and simple skyboxes
- The glass block added
- Added key configuration to config file
- Player privileges on server
- Slightly updated map format
- Player passwords
- All textures first searched from texture_path
- Map directory ("map") has been renamed to "world" (just rename it to load an old world)
- Mouse inversion (invert_mouse)
- Grass doesn't grow immediately anymore
- Fence added

2011-04-24:
- Smooth lighting with simple ambient occlusion
- Updated main menu

2011-04-23_0_test:
- Small bug fixes
- Item drop multiplication fixed
- HP added
- Added A simple monster which spawns to dark places at map generation time
- Some code refactoring and cleaning (possibly new bugs)

2011-04-11:
- Fixed crafting a bit

2011-04-10_0:
- Asynchronous map generation
- New object system

2011-04-06:
- Mesh update of node addition/removal is now done asynchronously on client, removing frametime spike
- Node addition/removal is sent directly only to clients that are closer than 100 nodes to the modification. For the others, the modified blocks are set unsent. (and are re-sent when applicable)

2011-04-05:
- Made furnace usable
- Added cobblestone
- Added wood, stone and steel tools: pickaxes, shovels and axes
- Incremented to version 0.0.2

2011-04-04:
- Cleaned client to be completely synchronous, except for the mesh calculation, which is now done with queues in a separate thread.
- Added node metadata support
- Added chests

2011-02-17:
- Added better handling of textures. Now many file extensions are searched. Also too large textures are not put on the texture atlas, and the construction of the texture atlas is stopped when it is full.

2011-02-16:
- Better handling of Ctrl-C on POSIX systems

2011-02-15:
- Fixed a problem of not saving and loading the "lighting expired" value of MapBlocks properly. This caused high server CPU usage.
- Ctrl-C handling on POSIX systems
- Added simple command support to server
- Added settings enable_texture_atlas and texture_path

2011-02-14:
- Created changelog.txt
- Added sneaking/crouching
- Modified the looks of the hotbar and cleaned code
- Added code to allow generating 3D cube images for inventory

9 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "rollback.h"
#include <fstream>
#include <list>
#include <sstream>
#include "log.h"
#include "mapnode.h"
#include "gamedef.h"
#include "nodedef.h"
#include "util/serialize.h"
#include "util/string.h"
#include "util/numeric.h"
#include "inventorymanager.h" // deserializing InventoryLocations
#include "sqlite3.h"
#include "filesys.h"

#define POINTS_PER_NODE (16.0)

#define SQLRES(f, good) \
	if ((f) != (good)) {\
		throw FileNotGoodException(std::string("RollbackManager: " \
			"SQLite3 error (" __FILE__ ":" TOSTRING(__LINE__) \
			"): ") + sqlite3_errmsg(db)); \
	}
#define SQLOK(f) SQLRES(f, SQLITE_OK)

#define SQLOK_ERRSTREAM(s, m)                             \
	if ((s) != SQLITE_OK) {                               \
		errorstream << "RollbackManager: " << (m) << ": " \
			<< sqlite3_errmsg(db) << std::endl;           \
	}

#define FINALIZE_STATEMENT(statement) \
	SQLOK_ERRSTREAM(sqlite3_finalize(statement), "Failed to finalize " #statement)

class ItemStackRow : public ItemStack {
public:
	ItemStackRow & operator = (const ItemStack & other)
	{
		*static_cast<ItemStack *>(this) = other;
		return *this;
	}

	int id;
};

struct ActionRow {
	int          id;
	int          actor;
	time_t       timestamp;
	int          type;
	std::string  location, list;
	int          index, add;
	ItemStackRow stack;
	int          nodeMeta;
	int          x, y, z;
	int          oldNode;
	int          oldParam1, oldParam2;
	std::string  oldMeta;
	int          newNode;
	int          newParam1, newParam2;
	std::string  newMeta;
	int          guessed;
};


struct Entity {
	int         id;
	std::string name;
};



RollbackManager::RollbackManager(const std::string & world_path,
		IGameDef * gamedef_) :
	gamedef(gamedef_)
{
	verbosestream << "RollbackManager::RollbackManager(" << world_path
		<< ")" << std::endl;

	std::string txt_filename = world_path + DIR_DELIM "rollback.txt";
	std::string migrating_flag = txt_filename + ".migrating";
	database_path = world_path + DIR_DELIM "rollback.sqlite";

	bool created = initDatabase();

	if (fs::PathExists(txt_filename) && (created ||
			fs::PathExists(migrating_flag))) {
		std::ofstream of(migrating_flag.c_str());
		of.close();
		migrate(txt_filename);
		fs::DeleteSingleFileOrEmptyDirectory(migrating_flag);
	}
}


RollbackManager::~RollbackManager()
{
	flush();

	FINALIZE_STATEMENT(stmt_insert);
	FINALIZE_STATEMENT(stmt_replace);
	FINALIZE_STATEMENT(stmt_select);
	FINALIZE_STATEMENT(stmt_select_range);
	FINALIZE_STATEMENT(stmt_select_withActor);
	FINALIZE_STATEMENT(stmt_knownActor_select);
	FINALIZE_STATEMENT(stmt_knownActor_insert);
	FINALIZE_STATEMENT(stmt_knownNode_select);
	FINALIZE_STATEMENT(stmt_knownNode_insert);

	SQLOK_ERRSTREAM(sqlite3_close(db), "Could not close db");
}


void RollbackManager::registerNewActor(const int id, const std::string &name)
{
	Entity actor = {id, name};
	knownActors.push_back(actor);
}


void RollbackManager::registerNewNode(const int id, const std::string &name)
{
	Entity node = {id, name};
	knownNodes.push_back(node);
}


int RollbackManager::getActorId(const std::string &name)
{
	for (std::vector<Entity>::const_iterator iter = knownActors.begin();
			iter != knownActors.end(); ++iter) {
		if (iter->name == name) {
			return iter->id;
		}
	}

	SQLOK(sqlite3_bind_text(stmt_knownActor_insert, 1, name.c_str(), name.size(), NULL));
	SQLRES(sqlite3_step(stmt_knownActor_insert), SQLITE_DONE);
	SQLOK(sqlite3_reset(stmt_knownActor_insert));

	int id = sqlite3_last_insert_rowid(db);
	registerNewActor(id, name);

	return id;
}


int RollbackManager::getNodeId(const std::string &name)
{
	for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
			iter != knownNodes.end(); ++iter) {
		if (iter->name == name) {
			return iter->id;
		}
	}

	SQLOK(sqlite3_bind_text(stmt_knownNode_insert, 1, name.c_str(), name.size(), NULL));
	SQLRES(sqlite3_step(stmt_knownNode_insert), SQLITE_DONE);
	SQLOK(sqlite3_reset(stmt_knownNode_insert));

	int id = sqlite3_last_insert_rowid(db);
	registerNewNode(id, name);

	return id;
}


const char * RollbackManager::getActorName(const int id)
{
	for (std::vector<Entity>::const_iterator iter = knownActors.begin();
			iter != knownActors.end(); ++iter) {
		if (iter->id == id) {
			return iter->name.c_str();
		}
	}

	return "";
}


const char * RollbackManager::getNodeName(const int id)
{
	for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
			iter != knownNodes.end(); ++iter) {
		if (iter->id == id) {
			return iter->name.c_str();
		}
	}

	return "";
}


bool RollbackManager::createTables()
{
	SQLOK(sqlite3_exec(db,
		"CREATE TABLE IF NOT EXISTS `actor` (\n"
		"	`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
		"	`name` TEXT NOT NULL\n"
		");\n"
		"CREATE TABLE IF NOT EXISTS `node` (\n"
		"	`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
		"	`name` TEXT NOT NULL\n"
		");\n"
		"CREATE TABLE IF NOT EXISTS `action` (\n"
		"	`id` INTEGER PRIMARY KEY AUTOINCREMENT,\n"
		"	`actor` INTEGER NOT NULL,\n"
		"	`timestamp` TIMESTAMP NOT NULL,\n"
		"	`type` INTEGER NOT NULL,\n"
		"	`list` TEXT,\n"
		"	`index` INTEGER,\n"
		"	`add` INTEGER,\n"
		"	`stackNode` INTEGER,\n"
		"	`stackQuantity` INTEGER,\n"
		"	`nodeMeta` INTEGER,\n"
		"	`x` INT,\n"
		"	`y` INT,\n"
		"	`z` INT,\n"
		"	`oldNode` INTEGER,\n"
		"	`oldParam1` INTEGER,\n"
		"	`oldParam2` INTEGER,\n"
		"	`oldMeta` TEXT,\n"
		"	`newNode` INTEGER,\n"
		"	`newParam1` INTEGER,\n"
		"	`newParam2` INTEGER,\n"
		"	`newMeta` TEXT,\n"
		"	`guessedActor` INTEGER,\n"
		"	FOREIGN KEY (`actor`) REFERENCES `actor`(`id`),\n"
		"	FOREIGN KEY (`stackNode`) REFERENCES `node`(`id`),\n"
		"	FOREIGN KEY (`oldNode`)   REFERENCES `node`(`id`),\n"
		"	FOREIGN KEY (`newNode`)   REFERENCES `node`(`id`)\n"
		");\n"
		"CREATE INDEX IF NOT EXISTS `actionIndex` ON `action`(`x`,`y`,`z`,`timestamp`,`actor`);\n",
		NULL, NULL, NULL));
	verbosestream << "SQL Rollback: SQLite3 database structure was created" << std::endl;

	return true;
}


bool RollbackManager::initDatabase()
{
	verbosestream << "RollbackManager: Database connection setup" << std::endl;

	bool needs_create = !fs::PathExists(database_path);
	SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
			SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));

	if (needs_create) {
		createTables();
	}

	SQLOK(sqlite3_prepare_v2(db,
		"INSERT INTO `action` (\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		") VALUES (\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?, ?, ?,\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?"
		");",
		-1, &stmt_insert, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"REPLACE INTO `action` (\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`, `id`\n"
		") VALUES (\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?, ?, ?,\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?, ?\n"
		");",
		-1, &stmt_replace, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"SELECT\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		" FROM `action`\n"
		" WHERE `timestamp` >= ?\n"
		" ORDER BY `timestamp` DESC, `id` DESC",
		-1, &stmt_select, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"SELECT\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		"FROM `action`\n"
		"WHERE `timestamp` >= ?\n"
		"	AND `x` IS NOT NULL\n"
		"	AND `y` IS NOT NULL\n"
		"	AND `z` IS NOT NULL\n"
		"	AND `x` BETWEEN ? AND ?\n"
		"	AND `y` BETWEEN ? AND ?\n"
		"	AND `z` BETWEEN ? AND ?\n"
		"ORDER BY `timestamp` DESC, `id` DESC\n"
		"LIMIT 0,?",
		-1, &stmt_select_range, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"SELECT\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		"FROM `action`\n"
		"WHERE `timestamp` >= ?\n"
		"	AND `actor` = ?\n"
		"ORDER BY `timestamp` DESC, `id` DESC\n",
		-1, &stmt_select_withActor, NULL));

	SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `actor`",
			-1, &stmt_knownActor_select, NULL));

	SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `actor` (`name`) VALUES (?)",
			-1, &stmt_knownActor_insert, NULL));

	SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `node`",
			-1, &stmt_knownNode_select, NULL));

	SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `node` (`name`) VALUES (?)",
			-1, &stmt_knownNode_insert, NULL));

	verbosestream << "SQL prepared statements setup correctly" << std::endl;

	while (sqlite3_step(stmt_knownActor_select) == SQLITE_ROW) {
		registerNewActor(
		        sqlite3_column_int(stmt_knownActor_select, 0),
		        reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownActor_select, 1))
		);
	}
	SQLOK(sqlite3_reset(stmt_knownActor_select));

	while (sqlite3_step(stmt_knownNode_select) == SQLITE_ROW) {
		registerNewNode(
		        sqlite3_column_int(stmt_knownNode_select, 0),
		        reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownNode_select, 1))
		);
	}
	SQLOK(sqlite3_reset(stmt_knownNode_select));

	return needs_create;
}


bool RollbackManager::registerRow(const ActionRow & row)
{
	sqlite3_stmt * stmt_do = (row.id) ? stmt_replace : stmt_insert;

	bool nodeMeta = false;

	SQLOK(sqlite3_bind_int  (stmt_do, 1, row.actor));
	SQLOK(sqlite3_bind_int64(stmt_do, 2, row.timestamp));
	SQLOK(sqlite3_bind_int  (stmt_do, 3, row.type));

	if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
		const std::string & loc = row.location;
		nodeMeta = (loc.substr(0, 9) == "nodemeta:");

		SQLOK(sqlite3_bind_text(stmt_do, 4, row.list.c_str(), row.list.size(), NULL));
		SQLOK(sqlite3_bind_int (stmt_do, 5, row.index));
		SQLOK(sqlite3_bind_int (stmt_do, 6, row.add));
		SQLOK(sqlite3_bind_int (stmt_do, 7, row.stack.id));
		SQLOK(sqlite3_bind_int (stmt_do, 8, row.stack.count));
		SQLOK(sqlite3_bind_int (stmt_do, 9, (int) nodeMeta));

		if (nodeMeta) {
			std::string::size_type p1, p2;
			p1 = loc.find(':') + 1;
			p2 = loc.find(',');
			std::string x = loc.substr(p1, p2 - p1);
			p1 = p2 + 1;
			p2 = loc.find(',', p1);
			std::string y = loc.substr(p1, p2 - p1);
			std::string z = loc.substr(p2 + 1);
			SQLOK(sqlite3_bind_int(stmt_do, 10, atoi(x.c_str())));
			SQLOK(sqlite3_bind_int(stmt_do, 11, atoi(y.c_str())));
			SQLOK(sqlite3_bind_int(stmt_do, 12, atoi(z.c_str())));
		}
	} else {
		SQLOK(sqlite3_bind_null(stmt_do, 4));
		SQLOK(sqlite3_bind_null(stmt_do, 5));
		SQLOK(sqlite3_bind_null(stmt_do, 6));
		SQLOK(sqlite3_bind_null(stmt_do, 7));
		SQLOK(sqlite3_bind_null(stmt_do, 8));
		SQLOK(sqlite3_bind_null(stmt_do, 9));
	}

	if (row.type == RollbackAction::TYPE_SET_NODE) {
		SQLOK(sqlite3_bind_int (stmt_do, 10, row.x));
		SQLOK(sqlite3_bind_int (stmt_do, 11, row.y));
		SQLOK(sqlite3_bind_int (stmt_do, 12, row.z));
		SQLOK(sqlite3_bind_int (stmt_do, 13, row.oldNode));
		SQLOK(sqlite3_bind_int (stmt_do, 14, row.oldParam1));
		SQLOK(sqlite3_bind_int (stmt_do, 15, row.oldParam2));
		SQLOK(sqlite3_bind_text(stmt_do, 16, row.oldMeta.c_str(), row.oldMeta.size(), NULL));
		SQLOK(sqlite3_bind_int (stmt_do, 17, row.newNode));
		SQLOK(sqlite3_bind_int (stmt_do, 18, row.newParam1));
		SQLOK(sqlite3_bind_int (stmt_do, 19, row.newParam2));
		SQLOK(sqlite3_bind_text(stmt_do, 20, row.newMeta.c_str(), row.newMeta.size(), NULL));
		SQLOK(sqlite3_bind_int (stmt_do, 21, row.guessed ? 1 : 0));
	} else {
		if (!nodeMeta) {
			SQLOK(sqlite3_bind_null(stmt_do, 10));
			SQLOK(sqlite3_bind_null(stmt_do, 11));
			SQLOK(sqlite3_bind_null(stmt_do, 12));
		}
		SQLOK(sqlite3_bind_null(stmt_do, 13));
		SQLOK(sqlite3_bind_null(stmt_do, 14));
		SQLOK(sqlite3_bind_null(stmt_do, 15));
		SQLOK(sqlite3_bind_null(stmt_do, 16));
		SQLOK(sqlite3_bind_null(stmt_do, 17));
		SQLOK(sqlite3_bind_null(stmt_do, 18));
		SQLOK(sqlite3_bind_null(stmt_do, 19));
		SQLOK(sqlite3_bind_null(stmt_do, 20));
		SQLOK(sqlite3_bind_null(stmt_do, 21));
	}

	if (row.id) {
		SQLOK(sqlite3_bind_int(stmt_do, 22, row.id));
	}

	int written = sqlite3_step(stmt_do);

	SQLOK(sqlite3_reset(stmt_do));

	return written == SQLITE_DONE;
}


const std::list<ActionRow> RollbackManager::actionRowsFromSelect(sqlite3_stmt* stmt)
{
	std::list<ActionRow> rows;
	const unsigned char * text;
	size_t size;

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		ActionRow row;

		row.actor     = sqlite3_column_int  (stmt, 0);
		row.timestamp = sqlite3_column_int64(stmt, 1);
		row.type      = sqlite3_column_int  (stmt, 2);

		if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
			text = sqlite3_column_text (stmt, 3);
			size = sqlite3_column_bytes(stmt, 3);
			row.list        = std::string(reinterpret_cast<const char*>(text), size);
			row.index       = sqlite3_column_int(stmt, 4);
			row.add         = sqlite3_column_int(stmt, 5);
			row.stack.id    = sqlite3_column_int(stmt, 6);
			row.stack.count = sqlite3_column_int(stmt, 7);
			row.nodeMeta    = sqlite3_column_int(stmt, 8);
		}

		if (row.type == RollbackAction::TYPE_SET_NODE || row.nodeMeta) {
			row.x = sqlite3_column_int(stmt,  9);
			row.y = sqlite3_column_int(stmt, 10);
			row.z = sqlite3_column_int(stmt, 11);
		}

		if (row.type == RollbackAction::TYPE_SET_NODE) {
			row.oldNode   = sqlite3_column_int(stmt, 12);
			row.oldParam1 = sqlite3_column_int(stmt, 13);
			row.oldParam2 = sqlite3_column_int(stmt, 14);
			text = sqlite3_column_text (stmt, 15);
			size = sqlite3_column_bytes(stmt, 15);
			row.oldMeta   = std::string(reinterpret_cast<const char*>(text), size);
			row.newNode   = sqlite3_column_int(stmt, 16);
			row.newParam1 = sqlite3_column_int(stmt, 17);
			row.newParam2 = sqlite3_column_int(stmt, 18);
			text = sqlite3_column_text(stmt, 19);
			size = sqlite3_column_bytes(stmt, 19);
			row.newMeta   = std::string(reinterpret_cast<const char*>(text), size);
			row.guessed   = sqlite3_column_int(stmt, 20);
		}

		if (row.nodeMeta) {
			row.location = "nodemeta:";
			row.location += itos(row.x);
			row.location += ',';
			row.location += itos(row.y);
			row.location += ',';
			row.location += itos(row.z);
		} else {
			row.location = getActorName(row.actor);
		}

		rows.push_back(row);
	}

	SQLOK(sqlite3_reset(stmt));

	return rows;
}


ActionRow RollbackManager::actionRowFromRollbackAction(const RollbackAction & action)
{
	ActionRow row;

	row.id        = 0;
	row.actor     = getActorId(action.actor);
	row.timestamp = action.unix_time;
	row.type      = action.type;

	if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
		row.location = action.inventory_location;
		row.list     = action.inventory_list;
		row.index    = action.inventory_index;
		row.add      = action.inventory_add;
		row.stack    = action.inventory_stack;
		row.stack.id = getNodeId(row.stack.name);
	} else {
		row.x         = action.p.X;
		row.y         = action.p.Y;
		row.z         = action.p.Z;
		row.oldNode   = getNodeId(action.n_old.name);
		row.oldParam1 = action.n_old.param1;
		row.oldParam2 = action.n_old.param2;
		row.oldMeta   = action.n_old.meta;
		row.newNode   = getNodeId(action.n_new.name);
		row.newParam1 = action.n_new.param1;
		row.newParam2 = action.n_new.param2;
		row.newMeta   = action.n_new.meta;
		row.guessed   = action.actor_is_guess;
	}

	return row;
}


const std::list<RollbackAction> RollbackManager::rollbackActionsFromActionRows(
		const std::list<ActionRow> & rows)
{
	std::list<RollbackAction> actions;

	for (const ActionRow &row : rows) {
		RollbackAction action;
		action.actor     = (row.actor) ? getActorName(row.actor) : "";
		action.unix_time = row.timestamp;
		action.type      = static_cast<RollbackAction::Type>(row.type);

		switch (action.type) {
		case RollbackAction::TYPE_MODIFY_INVENTORY_STACK:
			action.inventory_location = row.location;
			action.inventory_list     = row.list;
			action.inventory_index    = row.index;
			action.inventory_add      = row.add;
			action.inventory_stack    = row.stack;
			if (action.inventory_stack.name.empty()) {
				action.inventory_stack.name = getNodeName(row.stack.id);
			}
			break;

		case RollbackAction::TYPE_SET_NODE:
			action.p            = v3s16(row.x, row.y, row.z);
			action.n_old.name   = getNodeName(row.oldNode);
			action.n_old.param1 = row.oldParam1;
			action.n_old.param2 = row.oldParam2;
			action.n_old.meta   = row.oldMeta;
			action.n_new.name   = getNodeName(row.newNode);
			action.n_new.param1 = row.newParam1;
			action.n_new.param2 = row.newParam2;
			action.n_new.meta   = row.newMeta;
			break;

		default:
			throw ("W.T.F.");
			break;
		}

		actions.push_back(action);
	}

	return actions;
}


const std::list<ActionRow> RollbackManager::getRowsSince(time_t firstTime, const std::string & actor)
{
	sqlite3_stmt *stmt_stmt = actor.empty() ? stmt_select : stmt_select_withActor;
	sqlite3_bind_int64(stmt_stmt, 1, firstTime);

	if (!actor.empty()) {
		sqlite3_bind_int(stmt_stmt, 2, getActorId(actor));
	}

	const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_stmt);
	sqlite3_reset(stmt_stmt);

	return rows;
}


const std::list<ActionRow> RollbackManager::getRowsSince_range(
		time_t start_time, v3s16 p, int range, int limit)
{

	sqlite3_bind_int64(stmt_select_range, 1, start_time);
	sqlite3_bind_int  (stmt_select_range, 2, static_cast<int>(p.X - range));
	sqlite3_bind_int  (stmt_select_range, 3, static_cast<int>(p.X + range));
	sqlite3_bind_int  (stmt_select_range, 4, static_cast<int>(p.Y - range));
	sqlite3_bind_int  (stmt_select_range, 5, static_cast<int>(p.Y + range));
	sqlite3_bind_int  (stmt_select_range, 6, static_cast<int>(p.Z - range));
	sqlite3_bind_int  (stmt_select_range, 7, static_cast<int>(p.Z + range));
	sqlite3_bind_int  (stmt_select_range, 8, limit);

	const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_select_range);
	sqlite3_reset(stmt_select_range);

	return rows;
}


const std::list<RollbackAction> RollbackManager::getActionsSince_range(
		time_t start_time, v3s16 p, int range, int limit)
{
	return rollbackActionsFromActionRows(getRowsSince_range(start_time, p, range, limit));
}


const std::list<RollbackAction> RollbackManager::getActionsSince(
		time_t start_time, const std::string & actor)
{
	return rollbackActionsFromActionRows(getRowsSince(start_time, actor));
}


void RollbackManager::migrate(const std::string & file_path)
{
	std::cout << "Migrating from rollback.txt to rollback.sqlite." << std::endl;

	std::ifstream fh(file_path.c_str(), std::ios::in | std::ios::ate);
	if (!fh.good()) {
		throw FileNotGoodException("Unable to open rollback.txt");
	}

	std::streampos file_size = fh.tellg();

	if (file_size < 10) {
		errorstream << "Empty rollback log." << std::endl;
		return;
	}

	fh.seekg(0);

	sqlite3_stmt *stmt_begin;
	sqlite3_stmt *stmt_commit;
	SQLOK(sqlite3_prepare_v2(db, "BEGIN", -1, &stmt_begin, NULL));
	SQLOK(sqlite3_prepare_v2(db, "COMMIT", -1, &stmt_commit, NULL));

	std::string bit;
	int i = 0;
	time_t start = time(0);
	time_t t = start;
	SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
	sqlite3_reset(stmt_begin);
	do {
		ActionRow row;
		row.id = 0;

		// Get the timestamp
		std::getline(fh, bit, ' ');
		bit = trim(bit);
		if (!atoi(bit.c_str())) {
			std::getline(fh, bit);
			continue;
		}
		row.timestamp = atoi(bit.c_str());

		// Get the actor
		row.actor = getActorId(deSerializeJsonString(fh));

		// Get the action type
		std::getline(fh, bit, '[');
		std::getline(fh, bit, ' ');

		if (bit == "modify_inventory_stack") {
			row.type = RollbackAction::TYPE_MODIFY_INVENTORY_STACK;
			row.location = trim(deSerializeJsonString(fh));
			std::getline(fh, bit, ' ');
			row.list     = trim(deSerializeJsonString(fh));
			std::getline(fh, bit, ' ');
			std::getline(fh, bit, ' ');
			row.index    = atoi(trim(bit).c_str());
			std::getline(fh, bit, ' ');
			row.add      = (int)(trim(bit) == "add");
			row.stack.deSerialize(deSerializeJsonString(fh));
			row.stack.id = getNodeId(row.stack.name);
			std::getline(fh, bit);
		} else if (bit == "set_node") {
			row.type = RollbackAction::TYPE_SET_NODE;
			std::getline(fh, bit, '(');
			std::getline(fh, bit, ',');
			row.x       = atoi(trim(bit).c_str());
			std::getline(fh, bit, ',');
			row.y       = atoi(trim(bit).c_str());
			std::getline(fh, bit, ')');
			row.z       = atoi(trim(bit).c_str());
			std::getline(fh, bit, ' ');
			row.oldNode = getNodeId(trim(deSerializeJsonString(fh)));
			std::getline(fh, bit, ' ');
			std::getline(fh, bit, ' ');
			row.oldParam1 = atoi(trim(bit).c_str());
			std::getline(fh, bit, ' ');
			row.oldParam2 = atoi(trim(bit).c_str());
			row.oldMeta   = trim(deSerializeJsonString(fh));
			std::getline(fh, bit, ' ');
			row.newNode   = getNodeId(trim(deSerializeJsonString(fh)));
			std::getline(fh, bit, ' ');
			std::getline(fh, bit, ' ');
			row.newParam1 = atoi(trim(bit).c_str());
			std::getline(fh, bit, ' ');
			row.newParam2 = atoi(trim(bit).c_str());
			row.newMeta   = trim(deSerializeJsonString(fh));
			std::getline(fh, bit, ' ');
			std::getline(fh, bit, ' ');
			std::getline(fh, bit);
			row.guessed = (int)(trim(bit) == "actor_is_guess");
		} else {
			errorstream << "Unrecognized rollback action type \""
				<< bit << "\"!" << std::endl;
			continue;
		}

		registerRow(row);
		++i;

		if (time(0) - t >= 1) {
			SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE);
			sqlite3_reset(stmt_commit);
			t = time(0);
			std::cout
				<< " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%"
				<< " Speed: " << i / (t - start) << "/second     \r" << std::flush;
			SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
			sqlite3_reset(stmt_begin);
		}
	} while (fh.good());
	SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE);
	sqlite3_reset(stmt_commit);

	SQLOK(sqlite3_finalize(stmt_begin));
	SQLOK(sqlite3_finalize(stmt_commit));

	std::cout
		<< " Done: 100%                                  " << std::endl
		<< "Now you can delete the old rollback.txt file." << std::endl;
}