aboutsummaryrefslogtreecommitdiff
path: root/src/network/connection.cpp
Commit message (Collapse)AuthorAge
* Sapier's fix for the RESEND RELIABLE problem (#4170)Robert Kiraly2016-06-03
|
* Tolerate packet reordering in the early init processest312016-05-22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes a bug where packet reordering made the server give the client two peer ids instead of one. This in turn confused reliable packet sending and made connecting to the server fail. The client usually sends three packets at init: one "dummy" packet consisting of two 0 bytes, and the init packet as well as its legacy counterpart. The last one can be turned off since commit af30183124d40a969040d7de4b3a487feec466e4, but this is of lower relevance for the bug. The relevant part here is that network packet reorder (which is a normal occurence) can make the packets reach the server in different order. If reorder puts the dummy packet further behind, the following would happen before the patch: 1. The server will get one of the init packets on channel 1 and assign the client a peer id, as the packet will have zero as peer id. 2. The server sends a CONTROLTYPE_SET_PEER_ID packet to inform the client of the peer id. 3. The next packet from the client will contain the peer id set by the server. 4. The server sets the m_has_sent_with_id member for the client's peer structure to true. 5. Now the dummy packet arrives. It has a peer id of zero, therefore the server searches whether it already has a peer id for the address the packet was sent from. The search fails because m_has_sent_with_id was set to true and the server only searched for peers with m_has_sent_with_id set to false. 6. In a working setup, the server would assign the dummy packet to the correct peer id. However the server instead now assigns a second peer id and peer structure to the peer, and assign the packet to that new peer. 7. In order to inform the peer of its peer id, the server sends a CONTROLTYPE_SET_PEER_ID command packet, reliably, to the peer. This packet uses the new peer id. 8. The client sends an ack to that packet, not with the new peer id but with the peer id sent in 2. 9. This packet reaches the server, but it drops the ACK as the peer id does not map to any un-ACK-ed packets with that seqnum. The same time, the server still waits for an ACK with the new peer id, which of course won't come. This causes the server to periodically re-try sending that packet, and the client ACKing it each time. Steps 7-9 cause annoyances and erroneous output, but don't cause the connection failure itself. The actual mistake that causes the connection failure happens in 6: The server does not assign the dummy packet to the correct peer, but to a newly created one. Therefore, all further packets sent by the client on channel 0 are now buffered by the server as it waits for the dummy packet to reach the peer, which of course doesn't happen as the server assigned that packet to the second peer it created for the client. This makes the connection code indefinitely buffer the TOSERVER_CLIENT_READY packet, not passing it to higher level code, which stalls the continuation of the further init process indefinitely and causes the actual bug. Maybe this can be caused by reordered init packets as well, the only studied case was where network has reliably reordered the dummy packet to get sent after the init packets. The patch fixes the bug by not ignoring peers where m_has_sent_with_id has been set anymore. The other changes of the patch are just cleanups of unused methods and fields and additional explanatory comments. One could think of alternate ways to fix the bug: * The client could simply take the new peer id and continue communicating with that. This is however worse than the fix as it requires the peer id set command to be sent reliably (which currently happens, but it cant be changed anymore). Also, such a change would require both server and client to be patched in order for the bug to be fixed, as right now the client ignores peer id set commands after the peer id is different from PEER_ID_INEXISTENT and the server requires modification too to change the peer id internally. And, most importantly, right now we guarantee higher level server code that the peer id for a certain peer does not change. This guarantee would have to be broken, and it would require much larger changes to the server than this patch means. * One could stop sending the dummy packet. One may be unsure whether this is a good idea, as the meaning of the dummy packet is not known (it might be there for something important), and as it is possible that the init packets may cause this problem as well (although it may be possible too that they can't cause this). Thanks to @auouymous who had originally reported this bug and who has helped patiently in finding its cause.
* Always use errorstream for DEBUG_EXCEPTION_HANDLERShadowNinja2015-10-14
|
* Use warningstream for log messages with WARNINGShadowNinja2015-10-14
| | | | Remove DTIME macro and its uses, too
* Change i++ to ++iDavid Jones2015-08-25
|
* Clean up threadingShadowNinja2015-08-23
| | | | | | | | | | | | | | | | | | | | * Rename everything. * Strip J prefix. * Change UpperCamelCase functions to lowerCamelCase. * Remove global (!) semaphore count mutex on OSX. * Remove semaphore count getter (unused, unsafe, depended on internal API functions on Windows, and used a hack on OSX). * Add `Atomic<type>`. * Make `Thread` handle thread names. * Add support for C++11 multi-threading. * Combine pthread and win32 sources. * Remove `ThreadStarted` (unused, unneeded). * Move some includes from the headers to the sources. * Move all of `Event` into its header (allows inlining with no new includes). * Make `Event` use `Semaphore` (except on Windows). * Move some porting functions into `Thread`. * Integrate logging with `Thread`. * Add threading test.
* Remove unused function from connection.{cpp,h}nerzhul2015-08-15
|
* connection: Make assertions non-fatal for received datakwolekr2015-08-05
|
* connection.cpp: remove unused constructorLoic Blot2015-07-14
|
* Move globals from main.cpp to more sane locationsCraig Robbins2015-04-01
| | | | | | | | | | | | Move debug streams to log.cpp|h Move GUI-related globals to clientlauncher Move g_settings and g_settings_path to settings.cpp|h Move g_menuclouds to clouds.cpp|h Move g_profiler to profiler.cpp|h
* Connection::Receive(): receive Network Packet instead of SharedBuffer<u8>.Loic Blot2015-03-31
| | | | | | | Because we get a Buffer<u8> from ConnectionEvent, don't convert it to SharedBuffer<u8> and return it to Server/Client::Receive which will convert it to NetworkPacket Instead, put the Buffer<u8> directly to NetworkPacket and return it to packet processing This remove a long existing memory copy Also check the packet size directly into Connection::Receive instead of packet processing
* RunCommandQueues: little optimizationLoic Blot2015-03-31
| | | | remove command from queue on success, else do nothing, instead of remove command from queue and push it front on success
* Typo in getOutgoingSequenceNumber => successfull to successfulLoic Blot2015-03-31
|
* queued_commands must be a std::deque. RunCommandQueues needs to push packet ↵Loic Blot2015-03-29
| | | | on front, not back
* Remove unused ConnectionCommand::sendToAll function. ↵Loic Blot2015-03-22
| | | | | | NetworkPacket::oldForgePacket returns Buffer instead of SharedBuffer and is used in ConnectionCommand instead of Connection::Send This remove the NetworkPacket buffer => SharedBuffer => Buffer copy. Now NetworkPacket => Buffer
* Replace NetworkPacket pointers to referencesLoic Blot2015-03-13
|
* For usages of assert() that are meant to persist in Release builds (when ↵Craig Robbins2015-03-07
| | | | NDEBUG is defined), replace those usages with persistent alternatives
* Remove Queue class which uses std::list and use native std::queueLoic Blot2015-03-05
|
* Fix some memory leaks on packet sending.Loic Blot2015-02-21
|
* [Patch 2/4] Network rework: packet writing, sending and cleanupsLoic Blot2015-02-16
NetworkPacket.cpp: * Remove some deprecated functions, we must use streaming interface * m_data converted from u8* to std::vector<u8> * Add an exporter to forge packet to Connection object * implement operator << std::wstring. n * implement operator << std::string * dynamic resize when write packet content. * fix string writing and performances. * create ServerCommandFactory, used by client to get useful informations about packet processing (sending). * Reliability * Transmit channel * Implement putRawString for some ugly char (_INIT packet), and use it. * Many packet read and write migrated * Implement oldForgePacket to interface writing with current connection * fix U8/char/bool writing * fix string writing and performances. * add some missing functions * Use v3s16 read instead of reading x,y,z separately * Add irr::video::SColor support into packets * Add some missing handlers * Add a template function to increase offset * Throw a serialization error on packet reading (must be improved) PacketFactories: * Create ServerCommandFactory, used by client to get useful informations about packet processing (sending). * Create ClientCommandFactory, used by server to get useful informations about packet processing (sending). Client.cpp: * implement NetworkPacket ::Send interface. * Move packet handlers to a dedicated file * Remove Client::Send(SharedBuffer) Server.cpp: * implement NetworkPacket ::Send interface. * Rewrite all packets using NetworkPacket * Move packet handlers to a dedicated file * Remove Server::Send(SharedBuffer) ClientIface.cpp: * Remove sendToAll(SharedBuffer<u8>) Connection.hpp rework: * Remove duplicate include * Remove duplicate negation * Remove a useless variable * Improve code performance by using a m_peers_list instead of scanning m_peers map * Remove Connection::Send(SharedBuffer) * Fix useafterfree into NetworkPacket Sending * Remove unused Connection::sendToAll Test.cpp: * Remove dead code * Update tests to use NetworkPackets Misc: * add new wrappers to Send packets in client, using NetworkPacket * Add NetworkPacket methods for Connection * coding style fix * dead code since changes cleanup * Use v3s16 read instead of reading x,y,z separately in some packets * Use different files to handle packets received by client and server * Cleanup: Remove useless includes ok @Zeno- Tested by @Zeno- @VanessaE and @nerzhul on running servers
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 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
/*
Minetest
Copyright (C) 2010-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 "inventory.h"
#include "serialization.h"
#include "debug.h"
#include <algorithm>
#include <sstream>
#include "log.h"
#include "itemdef.h"
#include "util/strfnd.h"
#include "content_mapnode.h" // For loading legacy MaterialItems
#include "nameidmapping.h" // For loading legacy MaterialItems
#include "util/serialize.h"
#include "util/string.h"

/*
	ItemStack
*/

static content_t content_translate_from_19_to_internal(content_t c_from)
{
	for (const auto &tt : trans_table_19) {
		if(tt[1] == c_from) {
			return tt[0];
		}
	}
	return c_from;
}

ItemStack::ItemStack(const std::string &name_, u16 count_,
		u16 wear_, IItemDefManager *itemdef) :
	name(itemdef->getAlias(name_)),
	count(count_),
	wear(wear_)
{
	if (name.empty() || count == 0)
		clear();
	else if (itemdef->get(name).type == ITEM_TOOL)
		count = 1;
}

void ItemStack::serialize(std::ostream &os, bool serialize_meta) const
{
	if (empty())
		return;

	// Check how many parts of the itemstring are needed
	int parts = 1;
	if (!metadata.empty())
		parts = 4;
	else if (wear != 0)
		parts = 3;
	else if (count != 1)
		parts = 2;

	os << serializeJsonStringIfNeeded(name);
	if (parts >= 2)
		os << " " << count;
	if (parts >= 3)
		os << " " << wear;
	if (parts >= 4) {
		os << " ";
		if (serialize_meta)
			metadata.serialize(os);
		else
			os << "<metadata size=" << metadata.size() << ">";
	}
}

void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
{
	clear();

	// Read name
	name = deSerializeJsonStringIfNeeded(is);

	// Skip space
	std::string tmp;
	std::getline(is, tmp, ' ');
	if(!tmp.empty())
		throw SerializationError("Unexpected text after item name");

	if(name == "MaterialItem")
	{
		// Obsoleted on 2011-07-30

		u16 material;
		is>>material;
		u16 materialcount;
		is>>materialcount;
		// Convert old materials
		if(material <= 0xff)
			material = content_translate_from_19_to_internal(material);
		if(material > 0xfff)
			throw SerializationError("Too large material number");
		// Convert old id to name
		NameIdMapping legacy_nimap;
		content_mapnode_get_name_id_mapping(&legacy_nimap);
		legacy_nimap.getName(material, name);
		if(name.empty())
			name = "unknown_block";
		if (itemdef)
			name = itemdef->getAlias(name);
		count = materialcount;
	}
	else if(name == "MaterialItem2")
	{
		// Obsoleted on 2011-11-16

		u16 material;
		is>>material;
		u16 materialcount;
		is>>materialcount;
		if(material > 0xfff)
			throw SerializationError("Too large material number");
		// Convert old id to name
		NameIdMapping legacy_nimap;
		content_mapnode_get_name_id_mapping(&legacy_nimap);
		legacy_nimap.getName(material, name);
		if(name.empty())
			name = "unknown_block";
		if (itemdef)
			name = itemdef->getAlias(name);
		count = materialcount;
	}
	else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
			|| name == "craft" || name == "CraftItem")
	{
		// Obsoleted on 2012-01-07

		std::string all;
		std::getline(is, all, '\n');
		// First attempt to read inside ""
		Strfnd fnd(all);
		fnd.next("\"");
		// If didn't skip to end, we have ""s
		if(!fnd.at_end()){
			name = fnd.next("\"");
		} else { // No luck, just read a word then
			fnd.start(all);
			name = fnd.next(" ");
		}
		fnd.skip_over(" ");
		if (itemdef)
			name = itemdef->getAlias(name);
		count = stoi(trim(fnd.next("")));
		if(count == 0)
			count = 1;
	}
	else if(name == "MBOItem")
	{
		// Obsoleted on 2011-10-14
		throw SerializationError("MBOItem not supported anymore");
	}
	else if(name == "tool" || name == "ToolItem")
	{
		// Obsoleted on 2012-01-07

		std::string all;
		std::getline(is, all, '\n');
		// First attempt to read inside ""
		Strfnd fnd(all);
		fnd.next("\"");
		// If didn't skip to end, we have ""s
		if(!fnd.at_end()){
			name = fnd.next("\"");
		} else { // No luck, just read a word then
			fnd.start(all);
			name = fnd.next(" ");
		}
		count = 1;
		// Then read wear
		fnd.skip_over(" ");
		if (itemdef)
			name = itemdef->getAlias(name);
		wear = stoi(trim(fnd.next("")));
	}
	else
	{
		do  // This loop is just to allow "break;"
		{
			// The real thing

			// Apply item aliases
			if (itemdef)
				name = itemdef->getAlias(name);

			// Read the count
			std::string count_str;
			std::getline(is, count_str, ' ');
			if (count_str.empty()) {
				count = 1;
				break;
			}

			count = stoi(count_str);

			// Read the wear
			std::string wear_str;
			std::getline(is, wear_str, ' ');
			if(wear_str.empty())
				break;

			wear = stoi(wear_str);

			// Read metadata
			metadata.deSerialize(is);

			// In case fields are added after metadata, skip space here:
			//std::getline(is, tmp, ' ');
			//if(!tmp.empty())
			//	throw SerializationError("Unexpected text after metadata");

		} while(false);
	}

	if (name.empty() || count == 0)
		clear();
	else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
		count = 1;
}

void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
{
	std::istringstream is(str, std::ios::binary);
	deSerialize(is, itemdef);
}

std::string ItemStack::getItemString(bool include_meta) const
{
	std::ostringstream os(std::ios::binary);
	serialize(os, include_meta);
	return os.str();
}

std::string ItemStack::getDescription(IItemDefManager *itemdef) const
{
	std::string desc = metadata.getString("description");
	if (desc.empty())
		desc = getDefinition(itemdef).description;
	return desc.empty() ? name : desc;
}

std::string ItemStack::getShortDescription(IItemDefManager *itemdef) const
{
	std::string desc = metadata.getString("short_description");
	if (desc.empty())
		desc = getDefinition(itemdef).short_description;
	if (!desc.empty())
		return desc;
	// no short_description because of old server version or modified builtin
	// return first line of description
	std::stringstream sstr(getDescription(itemdef));
	std::getline(sstr, desc, '\n');
	return desc;
}


ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
{
	// If the item is empty or the position invalid, bail out
	if(newitem.empty())
	{
		// nothing can be added trivially
	}
	// If this is an empty item, it's an easy job.
	else if(empty())
	{
		*this = newitem;
		newitem.clear();
	}
	// If item name or metadata differs, bail out
	else if (name != newitem.name
		|| metadata != newitem.metadata)
	{
		// cannot be added
	}
	// If the item fits fully, add counter and delete it
	else if(newitem.count <= freeSpace(itemdef))
	{
		add(newitem.count);
		newitem.clear();
	}
	// Else the item does not fit fully. Add all that fits and return
	// the rest.
	else
	{
		u16 freespace = freeSpace(itemdef);
		add(freespace);
		newitem.remove(freespace);
	}

	return newitem;
}

bool ItemStack::itemFits(ItemStack newitem,
		ItemStack *restitem,
		IItemDefManager *itemdef) const
{

	// If the item is empty or the position invalid, bail out
	if(newitem.empty())
	{
		// nothing can be added trivially
	}
	// If this is an empty item, it's an easy job.
	else if(empty())
	{
		newitem.clear();
	}
	// If item name or metadata differs, bail out
	else if (name != newitem.name
		|| metadata != newitem.metadata)
	{
		// cannot be added
	}
	// If the item fits fully, delete it
	else if(newitem.count <= freeSpace(itemdef))
	{
		newitem.clear();
	}
	// Else the item does not fit fully. Return the rest.
	else
	{
		u16 freespace = freeSpace(itemdef);
		newitem.remove(freespace);
	}

	if(restitem)
		*restitem = newitem;

	return newitem.empty();
}

ItemStack ItemStack::takeItem(u32 takecount)
{
	if(takecount == 0 || count == 0)
		return ItemStack();

	ItemStack result = *this;
	if(takecount >= count)
	{
		// Take all
		clear();
	}
	else
	{
		// Take part
		remove(takecount);
		result.count = takecount;
	}
	return result;
}

ItemStack ItemStack::peekItem(u32 peekcount) const
{
	if(peekcount == 0 || count == 0)
		return ItemStack();

	ItemStack result = *this;
	if(peekcount < count)
		result.count = peekcount;
	return result;
}

/*
	Inventory
*/

InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
	m_name(name),
	m_size(size),
	m_itemdef(itemdef)
{
	clearItems();
}

void InventoryList::clearItems()
{
	m_items.clear();

	for (u32 i=0; i < m_size; i++) {
		m_items.emplace_back();
	}

	setModified();
}

void InventoryList::setSize(u32 newsize)
{
	if (newsize == m_items.size())
		return;

	m_items.resize(newsize);
	m_size = newsize;
	setModified();
}

void InventoryList::setWidth(u32 newwidth)
{
	m_width = newwidth;
	setModified();
}

void InventoryList::setName(const std::string &name)
{
	m_name = name;
	setModified();
}

void InventoryList::serialize(std::ostream &os, bool incremental) const
{
	//os.imbue(std::locale("C"));

	os<<"Width "<<m_width<<"\n";

	for (const auto &item : m_items) {
		if (item.empty()) {
			os<<"Empty";
		} else {
			os<<"Item ";
			item.serialize(os);
		}
		// TODO: Implement this:
		// if (!incremental || item.checkModified())
		// os << "Keep";
		os<<"\n";
	}

	os<<"EndInventoryList\n";
}

void InventoryList::deSerialize(std::istream &is)
{
	//is.imbue(std::locale("C"));
	setModified();

	u32 item_i = 0;
	m_width = 0;

	while (is.good()) {
		std::string line;
		std::getline(is, line, '\n');

		std::istringstream iss(line);
		//iss.imbue(std::locale("C"));

		std::string name;
		std::getline(iss, name, ' ');

		if (name == "EndInventoryList" || name == "end") {
			// If partial incremental: Clear leftover items (should not happen!)
			for (size_t i = item_i; i < m_items.size(); ++i)
				m_items[i].clear();
			return;
		}

		if (name == "Width") {
			iss >> m_width;
			if (iss.fail())
				throw SerializationError("incorrect width property");
		}
		else if(name == "Item")
		{
			if(item_i > getSize() - 1)
				throw SerializationError("too many items");
			ItemStack item;
			item.deSerialize(iss, m_itemdef);
			m_items[item_i++] = item;
		}
		else if(name == "Empty")
		{
			if(item_i > getSize() - 1)
				throw SerializationError("too many items");
			m_items[item_i++].clear();
		} else if (name == "Keep") {
			++item_i; // Unmodified item
		}
	}

	// Contents given to deSerialize() were not terminated properly: throw error.

	std::ostringstream ss;
	ss << "Malformatted inventory list. list="
		<< m_name << ", read " << item_i << " of " << getSize()
		<< " ItemStacks." << std::endl;
	throw SerializationError(ss.str());
}

InventoryList::InventoryList(const InventoryList &other)
{
	*this = other;
}

InventoryList & InventoryList::operator = (const InventoryList &other)
{
	m_items = other.m_items;
	m_size = other.m_size;
	m_width = other.m_width;
	m_name = other.m_name;
	m_itemdef = other.m_itemdef;
	//setDirty(true);

	return *this;
}

bool InventoryList::operator == (const InventoryList &other) const
{
	if(m_size != other.m_size)
		return false;
	if(m_width != other.m_width)
		return false;
	if(m_name != other.m_name)
		return false;
	for (u32 i = 0; i < m_items.size(); i++)
		if (m_items[i] != other.m_items[i])
			return false;

	return true;
}

const std::string &InventoryList::getName() const
{
	return m_name;
}

u32 InventoryList::getSize() const
{
	return m_items.size();
}

u32 InventoryList::getWidth() const
{
	return m_width;
}

u32 InventoryList::getUsedSlots() const
{
	u32 num = 0;
	for (const auto &m_item : m_items) {
		if (!m_item.empty())
			num++;
	}
	return num;
}

u32 InventoryList::getFreeSlots() const
{
	return getSize() - getUsedSlots();
}

const ItemStack& InventoryList::getItem(u32 i) const
{
	assert(i < m_size); // Pre-condition
	return m_items[i];
}

ItemStack& InventoryList::getItem(u32 i)
{
	assert(i < m_size); // Pre-condition
	return m_items[i];
}

ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
{
	if(i >= m_items.size())
		return newitem;

	ItemStack olditem = m_items[i];
	m_items[i] = newitem;
	setModified();
	return olditem;
}

void InventoryList::deleteItem(u32 i)
{
	assert(i < m_items.size()); // Pre-condition
	m_items[i].clear();
	setModified();
}

ItemStack InventoryList::addItem(const ItemStack &newitem_)
{
	ItemStack newitem = newitem_;

	if(newitem.empty())
		return newitem;

	/*
		First try to find if it could be added to some existing items
	*/
	for(u32 i=0; i<m_items.size(); i++)
	{
		// Ignore empty slots
		if(m_items[i].empty())
			continue;
		// Try adding
		newitem = addItem(i, newitem);
		if(newitem.empty())
			return newitem; // All was eaten
	}

	/*
		Then try to add it to empty slots
	*/
	for(u32 i=0; i<m_items.size(); i++)
	{
		// Ignore unempty slots
		if(!m_items[i].empty())
			continue;
		// Try adding
		newitem = addItem(i, newitem);
		if(newitem.empty())
			return newitem; // All was eaten
	}

	// Return leftover
	return newitem;
}

ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
{
	if(i >= m_items.size())
		return newitem;

	ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
	if (leftover != newitem)
		setModified();
	return leftover;
}

bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
		ItemStack *restitem) const
{
	if(i >= m_items.size())
	{
		if(restitem)
			*restitem = newitem;
		return false;
	}

	return m_items[i].itemFits(newitem, restitem, m_itemdef);
}

bool InventoryList::roomForItem(const ItemStack &item_) const
{
	ItemStack item = item_;
	ItemStack leftover;
	for(u32 i=0; i<m_items.size(); i++)
	{
		if(itemFits(i, item, &leftover))
			return true;
		item = leftover;
	}
	return false;
}

bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
{
	u32 count = item.count;
	if (count == 0)
		return true;

	for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
		if (count == 0)
			break;
		if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
			if (i->count >= count)
				return true;

			count -= i->count;
		}
	}
	return false;
}

ItemStack InventoryList::removeItem(const ItemStack &item)
{
	ItemStack removed;
	for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
		if (i->name == item.name) {
			u32 still_to_remove = item.count - removed.count;
			ItemStack leftover = removed.addItem(i->takeItem(still_to_remove),
					m_itemdef);
			// Allow oversized stacks
			removed.count += leftover.count;

			if (removed.count == item.count)
				break;
		}
	}
	if (!removed.empty())
		setModified();
	return removed;
}

ItemStack InventoryList::takeItem(u32 i, u32 takecount)
{
	if(i >= m_items.size())
		return ItemStack();

	ItemStack taken = m_items[i].takeItem(takecount);
	if (!taken.empty())
		setModified();
	return taken;
}

void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
{
	// Take item from source list
	ItemStack item1;
	if (count == 0)
		item1 = changeItem(i, ItemStack());
	else
		item1 = takeItem(i, count);

	if (item1.empty())
		return;

	ItemStack leftover;
	leftover = dest->addItem(item1);

	if (!leftover.empty()) {
		// Add the remaining part back to the source item
		addItem(i, leftover);
	}
}

u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
		u32 count, bool swap_if_needed, bool *did_swap)
{
	if (this == dest && i == dest_i)
		return count;

	// Take item from source list
	ItemStack item1;
	if (count == 0)
		item1 = changeItem(i, ItemStack());
	else
		item1 = takeItem(i, count);

	if (item1.empty())
		return 0;

	// Try to add the item to destination list
	u32 oldcount = item1.count;
	item1 = dest->addItem(dest_i, item1);

	// If something is returned, the item was not fully added
	if (!item1.empty()) {
		// If olditem is returned, nothing was added.
		bool nothing_added = (item1.count == oldcount);

		// If something else is returned, part of the item was left unadded.
		// Add the other part back to the source item
		addItem(i, item1);

		// If olditem is returned, nothing was added.
		// Swap the items
		if (nothing_added && swap_if_needed) {
			// Tell that we swapped
			if (did_swap != NULL) {
				*did_swap = true;
			}
			// Take item from source list
			item1 = changeItem(i, ItemStack());
			// Adding was not possible, swap the items.
			ItemStack item2 = dest->changeItem(dest_i, item1);
			// Put item from destination list to the source list
			changeItem(i, item2);
		}
	}
	return (oldcount - item1.count);
}

/*
	Inventory
*/

Inventory::~Inventory()
{
	clear();
}

void Inventory::clear()
{
	for (auto &m_list : m_lists) {
		delete m_list;
	}
	m_lists.clear();
	setModified();
}

Inventory::Inventory(IItemDefManager *itemdef)
{
	m_itemdef = itemdef;
	setModified();
}

Inventory::Inventory(const Inventory &other)
{
	*this = other;
}

Inventory & Inventory::operator = (const Inventory &other)
{
	// Gracefully handle self assignment
	if(this != &other)
	{
		clear();
		m_itemdef = other.m_itemdef;
		for (InventoryList *list : other.m_lists) {
			m_lists.push_back(new InventoryList(*list));
		}
		setModified();
	}
	return *this;
}

bool Inventory::operator == (const Inventory &other) const
{
	if(m_lists.size() != other.m_lists.size())
		return false;

	for(u32 i=0; i<m_lists.size(); i++)
	{
		if(*m_lists[i] != *other.m_lists[i])
			return false;
	}
	return true;
}

void Inventory::serialize(std::ostream &os, bool incremental) const
{
	//std::cout << "Serialize " << (int)incremental << ", n=" << m_lists.size() << std::endl;
	for (const InventoryList *list : m_lists) {
		if (!incremental || list->checkModified()) {
			os << "List " << list->getName() << " " << list->getSize() << "\n";
			list->serialize(os, incremental);
		} else {
			os << "KeepList " << list->getName() << "\n";
		}
	}

	os<<"EndInventory\n";
}

void Inventory::deSerialize(std::istream &is)
{
	std::vector<InventoryList *> new_lists;
	new_lists.reserve(m_lists.size());

	while (is.good()) {
		std::string line;
		std::getline(is, line, '\n');

		std::istringstream iss(line);

		std::string name;
		std::getline(iss, name, ' ');

		if (name == "EndInventory" || name == "end") {
			// Remove all lists that were not sent
			for (auto &list : m_lists) {
				if (std::find(new_lists.begin(), new_lists.end(), list) != new_lists.end())
					continue;

				delete list;
				list = nullptr;
				setModified();
			}
			m_lists.erase(std::remove(m_lists.begin(), m_lists.end(),
					nullptr), m_lists.end());
			return;
		}

		if (name == "List") {
			std::string listname;
			u32 listsize;

			std::getline(iss, listname, ' ');
			iss>>listsize;

			InventoryList *list = getList(listname);
			bool create_new = !list;
			if (create_new)
				list = new InventoryList(listname, listsize, m_itemdef);
			else
				list->setSize(listsize);
			list->deSerialize(is);

			new_lists.push_back(list);
			if (create_new)
				m_lists.push_back(list);

		} else if (name == "KeepList") {
			// Incrementally sent list
			std::string listname;
			std::getline(iss, listname, ' ');

			InventoryList *list = getList(listname);
			if (list) {
				new_lists.push_back(list);
			} else {
				errorstream << "Inventory::deSerialize(): Tried to keep list '" <<
					listname << "' which is non-existent." << std::endl;
			}
		}
		// Any additional fields will throw errors when received by a client
		// older than PROTOCOL_VERSION 38
	}

	// Contents given to deSerialize() were not terminated properly: throw error.

	std::ostringstream ss;
	ss << "Malformatted inventory (damaged?). "
		<< m_lists.size() << " lists read." << std::endl;
	throw SerializationError(ss.str());
}

InventoryList * Inventory::addList(const std::string &name, u32 size)
{
	setModified();
	s32 i = getListIndex(name);
	if(i != -1)
	{
		if(m_lists[i]->getSize() != size)
		{
			delete m_lists[i];
			m_lists[i] = new InventoryList(name, size, m_itemdef);
			m_lists[i]->setModified();
		}
		return m_lists[i];
	}


	//don't create list with invalid name
	if (name.find(' ') != std::string::npos)
		return nullptr;

	InventoryList *list = new InventoryList(name, size, m_itemdef);
	list->setModified();
	m_lists.push_back(list);
	return list;
}

InventoryList * Inventory::getList(const std::string &name)
{
	s32 i = getListIndex(name);
	if(i == -1)
		return NULL;
	return m_lists[i];
}

std::vector<const InventoryList*> Inventory::getLists()
{
	std::vector<const InventoryList*> lists;
	for (auto list : m_lists) {
		lists.push_back(list);
	}
	return lists;
}

bool Inventory::deleteList(const std::string &name)
{
	s32 i = getListIndex(name);
	if(i == -1)
		return false;

	setModified();
	delete m_lists[i];
	m_lists.erase(m_lists.begin() + i);
	return true;
}

const InventoryList * Inventory::getList(const std::string &name) const
{
	s32 i = getListIndex(name);
	if(i == -1)
		return NULL;
	return m_lists[i];
}

const s32 Inventory::getListIndex(const std::string &name) const
{
	for(u32 i=0; i<m_lists.size(); i++)
	{
		if(m_lists[i]->getName() == name)
			return i;
	}
	return -1;
}

//END