bool cPrefabPiecePool::LoadFromString(const AString & a_Contents, const AString & a_FileName, bool a_LogWarnings)
{
	// If the contents start with GZip signature, ungzip and retry:
	if (a_Contents.substr(0, 3) == "\x1f\x8b\x08")
	{
		AString Uncompressed;
		auto res = UncompressStringGZIP(a_Contents.data(), a_Contents.size(), Uncompressed);
		if (res == Z_OK)
		{
			return LoadFromString(Uncompressed, a_FileName, a_LogWarnings);
		}
		else
		{
			CONDWARNING(a_LogWarnings, "Failed to decompress Gzip data in file %s: %d", a_FileName.c_str(), res);
			return false;
		}
	}

	// Search the first 8 KiB of the file for the format auto-detection string:
	auto Header = a_Contents.substr(0, 8192);
	if (Header.find("CubesetFormatVersion =") != AString::npos)
	{
		return LoadFromCubeset(a_Contents, a_FileName, a_LogWarnings);
	}
	CONDWARNING(a_LogWarnings, "Cannot load prefabs from file %s, unknown file format", a_FileName.c_str());
	return false;
}
Example #2
0
bool StringToItem(const AString & a_ItemTypeString, cItem & a_Item)
{
	AString ItemName = TrimString(a_ItemTypeString);
	if (ItemName.substr(0, 10) == "minecraft:")
	{
		ItemName = ItemName.substr(10);
	}
	return gsBlockIDMap.ResolveItem(ItemName, a_Item);
}
	Bool HawkOSOperator::CreateDir(const AString& sDir)
	{
		if (!sDir.size())
			return true;

		AString sSysDir = sDir;
		HawkStringUtil::Replace<AString>(sSysDir,"\\","/");

		Size_t iPos = sSysDir.rfind('/');
		if (iPos != AString::npos)
		{
			AString sParent = sSysDir.substr(0, iPos);
			if (sParent.size() && !ExistFolder(sParent))
				CreateDir(sParent);
		}

#ifdef PLATFORM_WINDOWS
		if (_mkdir(sSysDir.c_str()) != HAWK_OK && errno != EEXIST)
			return false;
#else
		mode_t iMask = umask(0);
		if (mkdir(sSysDir.c_str(), S_IRWXU|S_IRWXG|S_IXOTH|S_IROTH) != HAWK_OK && errno != EEXIST)
		{
			umask(iMask);
			return false;
		}
		umask(iMask);
#endif

		return true;
	}	
Example #4
0
cPiece::cVerticalStrategyPtr CreateVerticalStrategyFromString(const AString & a_StrategyDesc, bool a_LogWarnings)
{
	// Break apart the strategy class, the first parameter before the first pipe char:
	auto idxPipe = a_StrategyDesc.find('|');
	if (idxPipe == AString::npos)
	{
		idxPipe = a_StrategyDesc.length();
	}
	AString StrategyClass = a_StrategyDesc.substr(0, idxPipe);

	// Create a strategy class based on the class string:
	cPiece::cVerticalStrategyPtr Strategy;
	if (NoCaseCompare(StrategyClass, "Fixed") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyFixed>();
	}
	else if (NoCaseCompare(StrategyClass, "Range") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyRange>();
	}
	else if (NoCaseCompare(StrategyClass, "TerrainTop") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyTerrainTop>();
	}
	else if (NoCaseCompare(StrategyClass, "TerrainOrOceanTop") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyTerrainOrOceanTop>();
	}
	else
	{
		return nullptr;
	}

	// Initialize the strategy's parameters:
	AString Params;
	if (idxPipe < a_StrategyDesc.length())
	{
		Params = a_StrategyDesc.substr(idxPipe + 1);
	}
	if (!Strategy->InitializeFromString(Params, a_LogWarnings))
	{
		return nullptr;
	}

	return Strategy;
}
	AString HawkOSOperator::SplitFilePath(const AString& sFile)
	{
		AString sTmpFile = sFile;
		HawkStringUtil::Replace<AString>(sTmpFile,"\\","/");
		Int32 iPos = (Int32)sTmpFile.find_last_of('/');
		if (iPos > 0)
		{
			return sTmpFile.substr(0,iPos+1);
		}
		return sTmpFile;
	}
Example #6
0
void cHTTPRequest::OnHeaderLine(const AString & a_Key, const AString & a_Value)
{
	if (
		(NoCaseCompare(a_Key, "Authorization") == 0) &&
		(strncmp(a_Value.c_str(), "Basic ", 6) == 0)
	)
	{
		AString UserPass = Base64Decode(a_Value.substr(6));
		size_t idxCol = UserPass.find(':');
		if (idxCol != AString::npos)
		{
			m_AuthUsername = UserPass.substr(0, idxCol);
			m_AuthPassword = UserPass.substr(idxCol + 1);
			m_HasAuth = true;
		}
	}
	if ((a_Key == "Connection") && (NoCaseCompare(a_Value, "keep-alive") == 0))
	{
		m_AllowKeepAlive = true;
	}
	AddHeader(a_Key, a_Value);
}
Example #7
0
void cMonster::SetCustomName(const AString & a_CustomName)
{
	m_CustomName = a_CustomName;

	// The maximal length is 64
	if (a_CustomName.length() > 64)
	{
		m_CustomName = a_CustomName.substr(0, 64);
	}

	if (m_World != nullptr)
	{
		m_World->BroadcastEntityMetadata(*this);
	}
}
Example #8
0
void cIniFile::RemoveBom(AString & a_line) const
{
	// The BOM sequence for UTF-8 is 0xEF, 0xBB, 0xBF
	static unsigned const char BOM[] = { 0xEF, 0xBB, 0xBF };

	// The BOM sequence, if present, is always th e first three characters of the input.
	const AString ref = a_line.substr(0, 3);

	// If any of the first three chars do not match, return and do nothing.
	for (size_t i = 0; i < 3; ++i)
	{
		if (static_cast<unsigned char>(ref[i]) != BOM[i])
		{
			return;
		}
	}

	// First three characters match; erase them.
	a_line.erase(0, 3);
}
Example #9
0
bool cFile::CreateFolderRecursive(const AString & a_FolderPath)
{
	// Special case: Fail if the path is empty
	if (a_FolderPath.empty())
	{
		return false;
	}

	// Go through each path element and create the folder:
	auto len = a_FolderPath.length();
	auto PathSep = GetPathSeparator()[0];
	for (decltype(len) i = 0; i < len; i++)
	{
		if (a_FolderPath[i] == PathSep)
		{
			CreateFolder(a_FolderPath.substr(0, i));
		}
	}
	CreateFolder(a_FolderPath);

	// Check the result by querying whether the final path exists:
	return IsFolder(a_FolderPath);
}
Example #10
0
/** 查找指定的文件
@Param 存储文件信息的列表
@Param 用于匹配文件名的字符
@Param 遍历时是否递归
@Param 是否遍历目录
*/
void FZipFilePack::FindFiles( VFileInfoList& fileList,const AString& pattern,
                                bool bRecurse,bool bDir )
{
    VFileInfoList::iterator itr;
    VFileInfoList::iterator end = m_FileInfos.end();

    // 如果要查找的字符包含目录,则做一个全匹配
    bool full_match = (pattern.find('/') != AString::npos ||
        pattern.find('\\') != AString::npos );

    // 获取关键字的目录
    AString directory;
    if( full_match )
    {
        size_t pos1 = pattern.rfind( '/' );
        size_t pos2 = pattern.rfind( '\\' );

        if( pos1 == AString::npos || ((pos2 != AString::npos) && pos1 < pos2) )
            pos1 = pos2;

        if( pos1 != AString::npos )
            directory = pattern.substr( 0,pos1+1 );
    }

    // 遍历文件信息
    for( itr=m_FileInfos.begin();itr!=end;itr++ )
    {
        if( ((itr->nCompressedSize != (size_t)-1) ||
            (itr->nCompressedSize == (size_t)-1 && bDir)) &&
            (bRecurse || itr->sPath == directory) )
        {
            if( AStringUtil::Match(full_match ? itr->sFileName : itr->sBaseName,pattern) )
                fileList.push_back( *itr );
        }
    }
}
Example #11
0
void cCompositeChat::ParseText(const AString & a_ParseText)
{
	size_t len = a_ParseText.length();
	size_t first = 0;  // First character of the currently parsed block
	AString CurrentStyle;
	AString CurrentText;
	for (size_t i = 0; i < len; i++)
	{
		switch (a_ParseText[i])
		{
			case '@':
			{
				// Color code
				i++;
				if (i >= len)
				{
					// Not enough following text
					break;
				}
				if (a_ParseText[i] == '@')
				{
					// "@@" escape, just put a "@" into the current text and keep parsing as text
					if (i > first + 1)
					{
						CurrentText.append(a_ParseText.c_str() + first, i - first - 1);
					}
					first = i + 1;
					continue;
				}
				else
				{
					// True color code. Create a part for the CurrentText and start parsing anew:
					if (i >= first)
					{
						CurrentText.append(a_ParseText.c_str() + first, i - first - 1);
						first = i + 1;
					}
					if (!CurrentText.empty())
					{
						m_Parts.push_back(new cTextPart(CurrentText, CurrentStyle));
						CurrentText.clear();
					}
					AddStyle(CurrentStyle, a_ParseText.substr(i - 1, 2));
				}
				break;
			}

			case ':':
			{
				const char * LinkPrefixes[] =
				{
					"http",
					"https"
				};
				for (size_t Prefix = 0; Prefix < ARRAYCOUNT(LinkPrefixes); Prefix++)
				{
					size_t PrefixLen = strlen(LinkPrefixes[Prefix]);
					if (
						(i >= first + PrefixLen) &&  // There is enough space in front of the colon for the prefix
						(strncmp(a_ParseText.c_str() + i - PrefixLen, LinkPrefixes[Prefix], PrefixLen) == 0)  // the prefix matches
					)
					{
						// Add everything before this as a text part:
						if (i > first + PrefixLen)
						{
							CurrentText.append(a_ParseText.c_str() + first, i - first - PrefixLen);
							first = i - PrefixLen;
						}
						if (!CurrentText.empty())
						{
							AddTextPart(CurrentText, CurrentStyle);
							CurrentText.clear();
						}

						// Go till the last non-whitespace char in the text:
						for (; i < len; i++)
						{
							if (isspace(a_ParseText[i]))
							{
								break;
							}
						}
						AddUrlPart(a_ParseText.substr(first, i - first), a_ParseText.substr(first, i - first), CurrentStyle);
						first = i;
						break;
					}
				}  // for Prefix - LinkPrefix[]
				break;
			}  // case ':'
		}  // switch (a_ParseText[i])
	}  // for i - a_ParseText[]
	if (first < len)
	{
		AddTextPart(a_ParseText.substr(first, len - first), CurrentStyle);
	}
}
Example #12
0
bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect)
{

	m_Filename = a_FileName;

	// Normally you would use ifstream, but the SGI CC compiler has
	// a few bugs with ifstream. So ... fstream used.
	fstream f;
	AString   line;
	AString   keyname, valuename, value;
	AString::size_type pLeft, pRight;
	bool IsFromExampleRedirect = false;


	f.open((FILE_IO_PREFIX + a_FileName).c_str(), ios::in);
	if (f.fail())
	{
		f.clear();
		if (a_AllowExampleRedirect)
		{
			// Retry with the .example.ini file instead of .ini:
			AString ExPath(a_FileName.substr(0, a_FileName.length() - 4));
			ExPath.append(".example.ini");
			f.open((FILE_IO_PREFIX + ExPath).c_str(), ios::in);
			if (f.fail())
			{
				return false;
			}
			IsFromExampleRedirect = true;
		}
		else
		{
			return false;
		}
	}

	bool IsFirstLine = true;

	while (getline(f, line))
	{
		// To be compatible with Win32, check for existence of '\r'.
		// Win32 files have the '\r' and Unix files don't at the end of a line.
		// Note that the '\r' will be written to INI files from
		// Unix so that the created INI file can be read under Win32
		// without change.

		// Removes UTF-8 Byte Order Markers (BOM) if, present.
		if (IsFirstLine)
		{
			RemoveBom(line);
			IsFirstLine = false;
		}

		size_t lineLength = line.length();
		if (lineLength == 0)
		{
			continue;
		}
		if (line[lineLength - 1] == '\r')
		{
			line = line.substr(0, lineLength - 1);
		}

		if (line.length() == 0)
		{
			continue;
		}

		// Check that the user hasn't opened a binary file by checking the first
		// character of each line!
		if (!isprint(line[0]))
		{
			printf("%s: Binary-check failed on char %d\n", __FUNCTION__, line[0]);
			f.close();
			return false;
		}
		if ((pLeft = line.find_first_of(";#[=")) == AString::npos)
		{
			continue;
		}

		switch (line[pLeft])
		{
			case '[':
			{
				if (
					((pRight = line.find_last_of("]")) != AString::npos) &&
					(pRight > pLeft)
					)
				{
					keyname = line.substr(pLeft + 1, pRight - pLeft - 1);
					AddKeyName(keyname);
				}
				break;
			}

			case '=':
			{
				valuename = line.substr(0, pLeft);
				value = line.substr(pLeft + 1);
				AddValue(keyname, valuename, value);
				break;
			}

			case ';':
			case '#':
			{
				if (names.empty())
				{
					AddHeaderComment(line.substr(pLeft + 1));
				}
				else
				{
					AddKeyComment(keyname, line.substr(pLeft + 1));
				}
				break;
			}
		}  // switch (line[pLeft])
	}  // while (getline())

	f.close();
	if (keys.empty() && names.empty() && comments.empty())
	{
		// File be empty or unreadable, equivalent to nonexistant
		return false;
	}

	if (IsFromExampleRedirect)
	{
		WriteFile(FILE_IO_PREFIX + a_FileName);
	}

	return true;
}
Example #13
0
void cRoot::LoadWorlds(cSettingsRepositoryInterface & a_Settings, bool a_IsNewIniFile)
{
	if (a_IsNewIniFile)
	{
		a_Settings.AddValue("Worlds", "DefaultWorld", "world");
		a_Settings.AddValue("Worlds", "World", "world_nether");
		a_Settings.AddValue("Worlds", "World", "world_end");
		m_pDefaultWorld = new cWorld("world");
		m_WorldsByName["world"] = m_pDefaultWorld;
		m_WorldsByName["world_nether"] = new cWorld("world_nether", dimNether, "world");
		m_WorldsByName["world_end"] = new cWorld("world_end", dimEnd, "world");
		return;
	}

	// First get the default world
	AString DefaultWorldName = a_Settings.GetValueSet("Worlds", "DefaultWorld", "world");
	m_pDefaultWorld = new cWorld(DefaultWorldName.c_str());
	m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld;
	auto Worlds = a_Settings.GetValues("Worlds");

	// Fix servers that have default world configs created prior to #2815. See #2810.
	// This can probably be removed several years after 2016
	// We start by inspecting the world linkage and determining if it's the default one
	if ((DefaultWorldName == "world") && (Worlds.size() == 1))
	{
		auto DefaultWorldIniFile= cpp14::make_unique<cIniFile>();
		if (DefaultWorldIniFile->ReadFile("world/world.ini"))
		{
			AString NetherName = DefaultWorldIniFile->GetValue("LinkedWorlds", "NetherWorldName", "");
			AString EndName = DefaultWorldIniFile->GetValue("LinkedWorlds", "EndWorldName", "");
			if ((NetherName.compare("world_nether") == 0) && (EndName.compare("world_end") == 0))
			{
				// This is a default world linkage config, see if the nether and end are in settings.ini
				// If both of them are not in settings.ini, then this is a pre-#2815 default config
				// so we add them to settings.ini
				// Note that if only one of them is not in settings.ini, it's nondefault and we don't touch it

				bool NetherInSettings = false;
				bool EndInSettings = false;

				for (auto WorldNameValue : Worlds)
				{
					AString ValueName = WorldNameValue.first;
					if (ValueName.compare("World") != 0)
					{
						continue;
					}
					AString WorldName = WorldNameValue.second;
					if (WorldName.compare("world_nether") == 0)
					{
						NetherInSettings = true;
					}
					else if (WorldName.compare("world_end") == 0)
					{
						EndInSettings = true;
					}
				}

				if ((!NetherInSettings) && (!EndInSettings))
				{
					a_Settings.AddValue("Worlds", "World", "world_nether");
					a_Settings.AddValue("Worlds", "World", "world_end");
					Worlds = a_Settings.GetValues("Worlds");  // Refresh the Worlds list so that the rest of the function works as usual
					LOG("The server detected an old default config with bad world linkages. This has been autofixed by adding \"world_nether\" and \"world_end\" to settings.ini");
				}
			}
		}
	}

	// Then load the other worlds
	if (Worlds.size() <= 0)
	{
		return;
	}

	/* Here are the world creation rules. Note that these only apply for a world which is in settings.ini but has no world.ini file.
	If an ini file is present, it overrides the world linkages and the dimension type in cWorld::start()
	The creation rules are as follows:

	- If a world exists in settings.ini but has no world.ini, then:
		- If the world name is x_nether, create a world.ini with the dimension type "nether".
			- If a world called x exists, set it as x_nether's overworld.
			- Otherwise set the default world as x_nether's overworld.

		- If the world name is x_end, create a world.ini with the dimension type "end".
			- If a world called x exists, set it as x_end's overworld.
			- Otherwise set the default world as x_end's overworld.

		- If the world name is x (and doesn't end with _end or _nether)
			- Create a world.ini with a dimension type of "overworld".
			- If a world called x_nether exists, set it as x's nether world.
			- Otherwise set x's nether world to blank.h
			- If a world called x_end  exists, set it as x's end world.
			- Otherwise set x's nether world to blank.

	*/

	bool FoundAdditionalWorlds = false;
	for (auto WorldNameValue : Worlds)
	{
		AString ValueName = WorldNameValue.first;
		if (ValueName.compare("World") != 0)
		{
			continue;
		}
		AString WorldName = WorldNameValue.second;
		if (WorldName.empty())
		{
			continue;
		}
		FoundAdditionalWorlds = true;
		cWorld * NewWorld;
		AString LowercaseName = StrToLower(WorldName);
		AString NetherAppend="_nether";
		AString EndAppend="_end";

		// if the world is called x_nether
		if ((LowercaseName.size() > NetherAppend.size()) && (LowercaseName.substr(LowercaseName.size() - NetherAppend.size()) == NetherAppend))
		{
			// The world is called x_nether, see if a world called x exists. If yes, choose it as the linked world,
			// otherwise, choose the default world as the linked world.
			// As before, any ini settings will completely override this if an ini is already present.

			AString LinkTo = WorldName.substr(0, WorldName.size() - NetherAppend.size());
			if (GetWorld(LinkTo) == nullptr)
			{
				LinkTo = DefaultWorldName;
			}
			NewWorld = new cWorld(WorldName.c_str(), dimNether, LinkTo);
		}
		// if the world is called x_end
		else if ((LowercaseName.size() > EndAppend.size()) && (LowercaseName.substr(LowercaseName.size() - EndAppend.size()) == EndAppend))
		{
			// The world is called x_end, see if a world called x exists. If yes, choose it as the linked world,
			// otherwise, choose the default world as the linked world.
			// As before, any ini settings will completely override this if an ini is already present.

			AString LinkTo = WorldName.substr(0, WorldName.size() - EndAppend.size());
			if (GetWorld(LinkTo) == nullptr)
			{
				LinkTo = DefaultWorldName;
			}
			NewWorld = new cWorld(WorldName.c_str(), dimEnd, LinkTo);
		}
		else
		{
			NewWorld = new cWorld(WorldName.c_str());
		}
		m_WorldsByName[WorldName] = NewWorld;
	}  // for i - Worlds

	if (!FoundAdditionalWorlds)
	{
		if (a_Settings.GetKeyComment("Worlds", 0) != " World=secondworld")
		{
			a_Settings.DeleteKeyComment("Worlds", 0);
			a_Settings.AddKeyComment("Worlds", " World=secondworld");
		}
	}
}
Example #14
0
UniquePtr<cPrefab> cPrefabPiecePool::LoadPrefabFromCubesetVer1(
	const AString & a_FileName,
	cLuaState & a_LuaState,
	const AString & a_PieceName,
	bool a_LogWarnings
)
{
	// First try loading a referenced schematic file, if any:
	AString SchematicFileName;
	if (a_LuaState.GetNamedValue("SchematicFileName", SchematicFileName))
	{
		auto PathEnd = a_FileName.find_last_of("/\\");  // Find the last path separator
		if (PathEnd != AString::npos)
		{
			SchematicFileName = a_FileName.substr(0, PathEnd) + SchematicFileName;
		}
		cBlockArea area;
		if (!cSchematicFileSerializer::LoadFromSchematicFile(area, SchematicFileName))
		{
			CONDWARNING(a_LogWarnings, "Cannot load schematic file \"%s\" for piece %s in cubeset %s.",
				SchematicFileName.c_str(), a_PieceName.c_str(), a_FileName.c_str()
			);
			return nullptr;
		}
		return cpp14::make_unique<cPrefab>(area);
	}  // if (SchematicFileName)

	// There's no referenced schematic file, load from BlockDefinitions / BlockData.
	// Get references to the data and the table.concat function:
	cLuaState::cRef TableConcat, BlockDefinitions, BlockData;
	if (
		!a_LuaState.GetNamedGlobal("table.concat", TableConcat) ||
		!a_LuaState.GetNamedValue("BlockDefinitions", BlockDefinitions) ||
		!a_LuaState.GetNamedValue("BlockData", BlockData)
	)
	{
		CONDWARNING(a_LogWarnings, "Cannot parse block data for piece %s in cubeset %s", a_PieceName.c_str(), a_FileName.c_str());
		return nullptr;
	}

	// Call table.concat() on the BlockDefinitions:
	AString BlockDefStr;
	if (!a_LuaState.Call(TableConcat, BlockDefinitions, "\n", cLuaState::Return, BlockDefStr))
	{
		CONDWARNING(a_LogWarnings, "Cannot concat block definitions for piece %s in cubeset %s", a_PieceName.c_str(), a_FileName.c_str());
		return nullptr;
	}

	// Call table.concat() on the BlockData:
	AString BlockDataStr;
	if (!a_LuaState.Call(TableConcat, BlockData, "", cLuaState::Return, BlockDataStr))
	{
		CONDWARNING(a_LogWarnings, "Cannot concat block data for piece %s in cubeset %s", a_PieceName.c_str(), a_FileName.c_str());
		return nullptr;
	}

	// Read the size:
	int SizeX = 0, SizeY = 0, SizeZ = 0;
	if (
		!a_LuaState.GetNamedValue("Size.x", SizeX) ||
		!a_LuaState.GetNamedValue("Size.y", SizeY) ||
		!a_LuaState.GetNamedValue("Size.z", SizeZ)
	)
	{
		CONDWARNING(a_LogWarnings, "Cannot load piece %s from file %s, its size information is missing", a_PieceName.c_str(), a_FileName.c_str());
		return nullptr;
	}

	// Check that the size matches the data length:
	if (static_cast<size_t>(SizeX * SizeY * SizeZ) != BlockDataStr.size())
	{
		CONDWARNING(a_LogWarnings, "Cannot create piece %s from file %s, its size (%d) doesn't match the blockdata length (%u)",
			a_PieceName.c_str(), a_FileName.c_str(),
			SizeX * SizeY * SizeZ, static_cast<unsigned>(BlockDataStr.size())
		);
		return nullptr;
	}

	return cpp14::make_unique<cPrefab>(BlockDefStr, BlockDataStr, SizeX, SizeY, SizeZ);
}
Example #15
0
void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request)
{
	if (!a_Request.HasAuth())
	{
		a_Connection.SendNeedAuth("MCServer WebAdmin");
		return;
	}

	// Check auth:
	AString UserPassword = m_IniFile.GetValue("User:"******"Password", "");
	if ((UserPassword == "") || (a_Request.GetAuthPassword() != UserPassword))
	{
		a_Connection.SendNeedAuth("MCServer WebAdmin - bad username or password");
		return;
	}

	// Check if the contents should be wrapped in the template:
	AString URL = a_Request.GetBareURL();
	ASSERT(URL.length() > 0);
	bool ShouldWrapInTemplate = ((URL.length() > 1) && (URL[1] != '~'));

	// Retrieve the request data:
	cWebadminRequestData * Data = (cWebadminRequestData *)(a_Request.GetUserData());
	if (Data == NULL)
	{
		a_Connection.SendStatusAndReason(500, "Bad UserData");
		return;
	}

	// Wrap it all up for the Lua call:
	AString Template;
	HTTPTemplateRequest TemplateRequest;
	TemplateRequest.Request.Username = a_Request.GetAuthUsername();
	TemplateRequest.Request.Method = a_Request.GetMethod();
	TemplateRequest.Request.Path = URL.substr(1);

	if (Data->m_Form.Finish())
	{
		for (cHTTPFormParser::const_iterator itr = Data->m_Form.begin(), end = Data->m_Form.end(); itr != end; ++itr)
		{
			HTTPFormData HTTPfd;
			HTTPfd.Value = itr->second;
			HTTPfd.Type = "";
			HTTPfd.Name = itr->first;
			TemplateRequest.Request.FormData[itr->first] = HTTPfd;
			TemplateRequest.Request.PostParams[itr->first] = itr->second;
		}  // for itr - Data->m_Form[]

		// Parse the URL into individual params:
		size_t idxQM = a_Request.GetURL().find('?');
		if (idxQM != AString::npos)
		{
			cHTTPFormParser URLParams(cHTTPFormParser::fpkURL, a_Request.GetURL().c_str() + idxQM + 1, a_Request.GetURL().length() - idxQM - 1, *Data);
			URLParams.Finish();
			for (cHTTPFormParser::const_iterator itr = URLParams.begin(), end = URLParams.end(); itr != end; ++itr)
			{
				TemplateRequest.Request.Params[itr->first] = itr->second;
			}  // for itr - URLParams[]
		}
	}

	// Try to get the template from the Lua template script
	if (ShouldWrapInTemplate)
	{
		if (m_TemplateScript.Call("ShowPage", this, &TemplateRequest, cLuaState::Return, Template))
		{
			cHTTPResponse Resp;
			Resp.SetContentType("text/html");
			a_Connection.Send(Resp);
			a_Connection.Send(Template.c_str(), Template.length());
			return;
		}
		a_Connection.SendStatusAndReason(500, "m_TemplateScript failed");
		return;
	}

	AString BaseURL = GetBaseURL(URL);
	AString Menu;
	Template = "{CONTENT}";
	AString FoundPlugin;

	for (PluginList::iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); ++itr)
	{
		cWebPlugin * WebPlugin = *itr;
		std::list< std::pair<AString, AString> > NameList = WebPlugin->GetTabNames();
		for (std::list< std::pair<AString, AString> >::iterator Names = NameList.begin(); Names != NameList.end(); ++Names)
		{
			Menu += "<li><a href='" + BaseURL + WebPlugin->GetWebTitle().c_str() + "/" + (*Names).second + "'>" + (*Names).first + "</a></li>";
		}
	}

	sWebAdminPage Page = GetPage(TemplateRequest.Request);
	AString Content = Page.Content;
	FoundPlugin = Page.PluginName;
	if (!Page.TabName.empty())
	{
		FoundPlugin += " - " + Page.TabName;
	}

	if (FoundPlugin.empty())  // Default page
	{
		Content = GetDefaultPage();
	}

	if (ShouldWrapInTemplate && (URL.size() > 1))
	{
		Content += "\n<p><a href='" + BaseURL + "'>Go back</a></p>";
	}

	int MemUsageKiB = cRoot::GetPhysicalRAMUsage();
	if (MemUsageKiB > 0)
	{
		ReplaceString(Template, "{MEM}",       Printf("%.02f", (double)MemUsageKiB / 1024));
		ReplaceString(Template, "{MEMKIB}",    Printf("%d", MemUsageKiB));
	}
	else
	{
		ReplaceString(Template, "{MEM}",       "unknown");
		ReplaceString(Template, "{MEMKIB}",    "unknown");
	}
	ReplaceString(Template, "{USERNAME}",    a_Request.GetAuthUsername());
	ReplaceString(Template, "{MENU}",        Menu);
	ReplaceString(Template, "{PLUGIN_NAME}", FoundPlugin);
	ReplaceString(Template, "{CONTENT}",     Content);
	ReplaceString(Template, "{TITLE}",       "MCServer");

	AString NumChunks;
	Printf(NumChunks, "%d", cRoot::Get()->GetTotalChunkCount());
	ReplaceString(Template, "{NUMCHUNKS}", NumChunks);

	cHTTPResponse Resp;
	Resp.SetContentType("text/html");
	a_Connection.Send(Resp);
	a_Connection.Send(Template.c_str(), Template.length());
}
Example #16
0
void cRoot::LoadWorlds(cSettingsRepositoryInterface & a_Settings, bool a_IsNewIniFile)
{
	if (a_IsNewIniFile)
	{
		a_Settings.AddValue("Worlds", "DefaultWorld", "world");
		a_Settings.AddValue("Worlds", "World", "world_nether");
		a_Settings.AddValue("Worlds", "World", "world_end");
		m_pDefaultWorld = new cWorld("world");
		m_WorldsByName["world"] = m_pDefaultWorld;
		m_WorldsByName["world_nether"] = new cWorld("world_nether", dimNether, "world");
		m_WorldsByName["world_end"] = new cWorld("world_end", dimEnd, "world");
		return;
	}

	// First get the default world
	AString DefaultWorldName = a_Settings.GetValueSet("Worlds", "DefaultWorld", "world");
	m_pDefaultWorld = new cWorld(DefaultWorldName.c_str());
	m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld;
	auto Worlds = a_Settings.GetValues("Worlds");

	// Then load the other worlds
	if (Worlds.size() <= 0)
	{
		return;
	}

	/* Here are the world creation rules. Note that these only apply for a world which is in settings.ini but has no world.ini file.
	If an ini file is present, it overrides the world linkages and the dimension type in cWorld::start()
	The creation rules are as follows:

	- If a world exists in settings.ini but has no world.ini, then:
		- If the world name is x_nether, create a world.ini with the dimension type "nether".
			- If a world called x exists, set it as x_nether's overworld.
			- Otherwise set the default world as x_nether's overworld.

		- If the world name is x_end, create a world.ini with the dimension type "end".
			- If a world called x exists, set it as x_end's overworld.
			- Otherwise set the default world as x_end's overworld.

		- If the world name is x (and doesn't end with _end or _nether)
			- Create a world.ini with a dimension type of "overworld".
			- If a world called x_nether exists, set it as x's nether world.
			- Otherwise set x's nether world to blank.h
			- If a world called x_end  exists, set it as x's end world.
			- Otherwise set x's nether world to blank.

	*/

	bool FoundAdditionalWorlds = false;
	for (auto WorldNameValue : Worlds)
	{
		AString ValueName = WorldNameValue.first;
		if (ValueName.compare("World") != 0)
		{
			continue;
		}
		AString WorldName = WorldNameValue.second;
		if (WorldName.empty())
		{
			continue;
		}
		FoundAdditionalWorlds = true;
		cWorld * NewWorld;
		AString LowercaseName = StrToLower(WorldName);
		AString NetherAppend="_nether";
		AString EndAppend="_end";

		// if the world is called x_nether
		if ((LowercaseName.size() > NetherAppend.size()) && (LowercaseName.substr(LowercaseName.size() - NetherAppend.size()) == NetherAppend))
		{
			// The world is called x_nether, see if a world called x exists. If yes, choose it as the linked world,
			// otherwise, choose the default world as the linked world.
			// As before, any ini settings will completely override this if an ini is already present.

			AString LinkTo = WorldName.substr(0, WorldName.size() - NetherAppend.size());
			if (GetWorld(LinkTo) == nullptr)
			{
				LinkTo = DefaultWorldName;
			}
			NewWorld = new cWorld(WorldName.c_str(), dimNether, LinkTo);
		}
		// if the world is called x_end
		else if ((LowercaseName.size() > EndAppend.size()) && (LowercaseName.substr(LowercaseName.size() - EndAppend.size()) == EndAppend))
		{
			// The world is called x_end, see if a world called x exists. If yes, choose it as the linked world,
			// otherwise, choose the default world as the linked world.
			// As before, any ini settings will completely override this if an ini is already present.

			AString LinkTo = WorldName.substr(0, WorldName.size() - EndAppend.size());
			if (GetWorld(LinkTo) == nullptr)
			{
				LinkTo = DefaultWorldName;
			}
			NewWorld = new cWorld(WorldName.c_str(), dimEnd, LinkTo);
		}
		else
		{
			NewWorld = new cWorld(WorldName.c_str());
		}
		m_WorldsByName[WorldName] = NewWorld;
	}  // for i - Worlds

	if (!FoundAdditionalWorlds)
	{
		if (a_Settings.GetKeyComment("Worlds", 0) != " World=secondworld")
		{
			a_Settings.DeleteKeyComment("Worlds", 0);
			a_Settings.AddKeyComment("Worlds", " World=secondworld");
		}
	}
}