From 7e5f22141f72fd1ad0ec7982df03f126e9c11244 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 31 Aug 2014 12:59:04 +0200 Subject: WebAdmin: Added "files" folder and load the login template from login_template.html --- src/WebAdmin.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/WebAdmin.h | 6 ++++ 2 files changed, 102 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 23eedbd14..0646c9d1c 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -135,6 +135,20 @@ bool cWebAdmin::Start(void) m_TemplateScript.Close(); } + if (!LoadLoginTemplate()) + { + LOGWARN("Could not load WebAdmin login template \"%s\", using fallback template.", FILE_IO_PREFIX "webadmin/login_template.html"); + + // Sets the fallback template: + m_LoginTemplate = \ + "

MCServer WebAdmin

" \ + "
" \ + "
" \ + "" \ + "
" \ + "
"; + } + m_IsRunning = m_HTTPServer.Start(*this); return m_IsRunning; } @@ -159,6 +173,28 @@ void cWebAdmin::Stop(void) +bool cWebAdmin::LoadLoginTemplate(void) +{ + cFile File(FILE_IO_PREFIX "webadmin/login_template.html", cFile::fmRead); + if (!File.IsOpen()) + { + return false; + } + + AString TemplateContent; + if (File.ReadRestOfFile(TemplateContent) == -1) + { + return false; + } + + m_LoginTemplate = TemplateContent; + return true; +} + + + + + void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) { if (!a_Request.HasAuth()) @@ -298,17 +334,11 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque void cWebAdmin::HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) { UNUSED(a_Request); - static const char LoginForm[] = \ - "

MCServer WebAdmin

" \ - "
" \ - "
" \ - "" \ - "
" \ - "
"; + cHTTPResponse Resp; Resp.SetContentType("text/html"); a_Connection.Send(Resp); - a_Connection.Send(LoginForm, sizeof(LoginForm) - 1); + a_Connection.Send(m_LoginTemplate); a_Connection.FinishResponse(); } @@ -528,7 +558,64 @@ void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & } else { - // TODO: Handle other requests + AString FileURL = URL; + std::replace(FileURL.begin(), FileURL.end(), '\\', '/'); + + // Remove all backsplashes on the first place: + if (FileURL[0] == '/') + { + size_t FirstCharToRead = FileURL.find_first_not_of('/'); + if (FirstCharToRead != AString::npos) + { + FileURL = FileURL.substr(FirstCharToRead); + } + } + + // Remove all "../" strings: + ReplaceString(FileURL, "../", ""); + + bool LoadedSuccessfull = false; + AString Content = "

404 Not Found

"; + AString Path = Printf(FILE_IO_PREFIX "webadmin/files/%s", FileURL.c_str()); + if (cFile::IsFile(Path)) + { + cFile File(Path, cFile::fmRead); + AString FileContent; + if (File.IsOpen() && (File.ReadRestOfFile(FileContent) != -1)) + { + LoadedSuccessfull = true; + Content = FileContent; + } + } + + // Find content type (The currently method is very bad. We should change it later) + AString ContentType = "text/html"; + size_t LastPointPosition = Path.find_last_of('.'); + if (LoadedSuccessfull && (LastPointPosition != AString::npos) && (LastPointPosition < Path.length())) + { + const AString & FileExtension = StrToLower(Path.substr(LastPointPosition + 1)); + if (FileExtension == "png") ContentType = "image/png"; + if (FileExtension == "fif") ContentType = "image/fif"; + if (FileExtension == "gif") ContentType = "image/gif"; + if (FileExtension == "jpeg") ContentType = "image/jpeg"; + if (FileExtension == "jpg") ContentType = "image/jpeg"; + if (FileExtension == "jpe") ContentType = "image/jpeg"; + if (FileExtension == "tiff") ContentType = "image/tiff"; + if (FileExtension == "ico") ContentType = "image/ico"; + if (FileExtension == "csv") ContentType = "text/comma-separated-values"; + if (FileExtension == "css") ContentType = "text/css"; + if (FileExtension == "js") ContentType = "text/javascript"; + if (FileExtension == "txt") ContentType = "text/plain"; + if (FileExtension == "rtx") ContentType = "text/richtext"; + if (FileExtension == "xml") ContentType = "text/xml"; + } + + // Send the response: + cHTTPResponse Resp; + Resp.SetContentType(ContentType); + a_Connection.Send(Resp); + a_Connection.Send(Content); + a_Connection.FinishResponse(); } // Delete any request data assigned to the request: @@ -551,4 +638,3 @@ void cWebAdmin::cWebadminRequestData::OnBody(const char * a_Data, size_t a_Size) - diff --git a/src/WebAdmin.h b/src/WebAdmin.h index aefc1d145..a59c69096 100644 --- a/src/WebAdmin.h +++ b/src/WebAdmin.h @@ -116,6 +116,9 @@ public: /** Stops the HTTP server, if it was started. */ void Stop(void); + /** Loads the login template. Returns true if the loading success, false if not. */ + bool LoadLoginTemplate(void); + void AddPlugin(cWebPlugin * a_Plugin); void RemovePlugin(cWebPlugin * a_Plugin); @@ -205,6 +208,9 @@ protected: /** The Lua template script to provide templates: */ cLuaState m_TemplateScript; + /** The template who provide the login side: */ + AString m_LoginTemplate; + /** The HTTP server which provides the underlying HTTP parsing, serialization and events */ cHTTPServer m_HTTPServer; -- cgit v1.2.3 From e67bb7f431e275b5313ff412266905dc1499294a Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 31 Aug 2014 15:03:23 +0200 Subject: WebAdmin: Stop webadmin if template can't load. --- src/WebAdmin.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 0646c9d1c..35a6d401c 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -131,8 +131,10 @@ bool cWebAdmin::Start(void) m_TemplateScript.RegisterAPILibs(); if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua")) { - LOGWARN("Could not load WebAdmin template \"%s\", using default template.", FILE_IO_PREFIX "webadmin/template.lua"); + LOGERROR("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua"); m_TemplateScript.Close(); + m_HTTPServer.Stop(); + return false; } if (!LoadLoginTemplate()) -- cgit v1.2.3 From 017235e15ccdfca901d63793c5544626e730ec48 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 2 Sep 2014 20:02:52 +0200 Subject: [WebAdmin] Code improvements. --- src/WebAdmin.cpp | 150 +++++++++++++++++++++++++++++++++---------------------- src/WebAdmin.h | 10 +++- 2 files changed, 99 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 35a6d401c..341c64236 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -131,7 +131,7 @@ bool cWebAdmin::Start(void) m_TemplateScript.RegisterAPILibs(); if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua")) { - LOGERROR("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua"); + LOGWARN("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua"); m_TemplateScript.Close(); m_HTTPServer.Stop(); return false; @@ -348,6 +348,94 @@ void cWebAdmin::HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & +void cWebAdmin::HandleFileRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) +{ + AString FileURL = a_Request.GetURL(); + std::replace(FileURL.begin(), FileURL.end(), '\\', '/'); + + // Remove all leading backslashes: + if (FileURL[0] == '/') + { + size_t FirstCharToRead = FileURL.find_first_not_of('/'); + if (FirstCharToRead != AString::npos) + { + FileURL = FileURL.substr(FirstCharToRead); + } + } + + // Remove all "../" strings: + ReplaceString(FileURL, "../", ""); + + bool LoadedSuccessfull = false; + AString Content = "

404 Not Found

"; + AString Path = Printf(FILE_IO_PREFIX "webadmin/files/%s", FileURL.c_str()); + if (cFile::IsFile(Path)) + { + cFile File(Path, cFile::fmRead); + AString FileContent; + if (File.IsOpen() && (File.ReadRestOfFile(FileContent) != -1)) + { + LoadedSuccessfull = true; + Content = FileContent; + } + } + + // Find content type (The currently method is very bad. We should change it later) + AString ContentType = "text/html"; + size_t LastPointPosition = Path.find_last_of('.'); + if (LoadedSuccessfull && (LastPointPosition != AString::npos) && (LastPointPosition < Path.length())) + { + AString FileExtension = Path.substr(LastPointPosition + 1); + ContentType = GetContentTypeFromFileExt(FileExtension); + } + + // Send the response: + cHTTPResponse Resp; + Resp.SetContentType(ContentType); + a_Connection.Send(Resp); + a_Connection.Send(Content); + a_Connection.FinishResponse(); +} + + + + + +AString cWebAdmin::GetContentTypeFromFileExt(const AString & a_FileExtension) +{ + static bool IsInitialized = false; + static std::map ContentTypeMap; + if (!IsInitialized) + { + // Initialize the ContentTypeMap: + ContentTypeMap["png"] = "image/png"; + ContentTypeMap["fif"] = "image/fif"; + ContentTypeMap["gif"] = "image/gif"; + ContentTypeMap["jpeg"] = "image/jpeg"; + ContentTypeMap["jpg"] = "image/jpeg"; + ContentTypeMap["jpe"] = "image/jpeg"; + ContentTypeMap["tiff"] = "image/tiff"; + ContentTypeMap["ico"] = "image/ico"; + ContentTypeMap["csv"] = "image/comma-separated-values"; + ContentTypeMap["css"] = "text/css"; + ContentTypeMap["js"] = "text/javascript"; + ContentTypeMap["txt"] = "text/plain"; + ContentTypeMap["rtx"] = "text/richtext"; + ContentTypeMap["xml"] = "text/xml"; + } + + AString FileExtension = StrToLower(a_FileExtension); + if (ContentTypeMap.find(a_FileExtension) == ContentTypeMap.end()) + { + return "text/html"; + } + return ContentTypeMap[FileExtension]; +} + + + + + sWebAdminPage cWebAdmin::GetPage(const HTTPRequest & a_Request) { sWebAdminPage Page; @@ -414,6 +502,7 @@ AString cWebAdmin::GetDefaultPage(void) + AString cWebAdmin::GetBaseURL( const AString& a_URL) { return GetBaseURL(StringSplit(a_URL, "/")); @@ -560,64 +649,7 @@ void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & } else { - AString FileURL = URL; - std::replace(FileURL.begin(), FileURL.end(), '\\', '/'); - - // Remove all backsplashes on the first place: - if (FileURL[0] == '/') - { - size_t FirstCharToRead = FileURL.find_first_not_of('/'); - if (FirstCharToRead != AString::npos) - { - FileURL = FileURL.substr(FirstCharToRead); - } - } - - // Remove all "../" strings: - ReplaceString(FileURL, "../", ""); - - bool LoadedSuccessfull = false; - AString Content = "

404 Not Found

"; - AString Path = Printf(FILE_IO_PREFIX "webadmin/files/%s", FileURL.c_str()); - if (cFile::IsFile(Path)) - { - cFile File(Path, cFile::fmRead); - AString FileContent; - if (File.IsOpen() && (File.ReadRestOfFile(FileContent) != -1)) - { - LoadedSuccessfull = true; - Content = FileContent; - } - } - - // Find content type (The currently method is very bad. We should change it later) - AString ContentType = "text/html"; - size_t LastPointPosition = Path.find_last_of('.'); - if (LoadedSuccessfull && (LastPointPosition != AString::npos) && (LastPointPosition < Path.length())) - { - const AString & FileExtension = StrToLower(Path.substr(LastPointPosition + 1)); - if (FileExtension == "png") ContentType = "image/png"; - if (FileExtension == "fif") ContentType = "image/fif"; - if (FileExtension == "gif") ContentType = "image/gif"; - if (FileExtension == "jpeg") ContentType = "image/jpeg"; - if (FileExtension == "jpg") ContentType = "image/jpeg"; - if (FileExtension == "jpe") ContentType = "image/jpeg"; - if (FileExtension == "tiff") ContentType = "image/tiff"; - if (FileExtension == "ico") ContentType = "image/ico"; - if (FileExtension == "csv") ContentType = "text/comma-separated-values"; - if (FileExtension == "css") ContentType = "text/css"; - if (FileExtension == "js") ContentType = "text/javascript"; - if (FileExtension == "txt") ContentType = "text/plain"; - if (FileExtension == "rtx") ContentType = "text/richtext"; - if (FileExtension == "xml") ContentType = "text/xml"; - } - - // Send the response: - cHTTPResponse Resp; - Resp.SetContentType(ContentType); - a_Connection.Send(Resp); - a_Connection.Send(Content); - a_Connection.FinishResponse(); + HandleFileRequest(a_Connection, a_Request); } // Delete any request data assigned to the request: diff --git a/src/WebAdmin.h b/src/WebAdmin.h index a59c69096..d6baa2ef9 100644 --- a/src/WebAdmin.h +++ b/src/WebAdmin.h @@ -116,7 +116,7 @@ public: /** Stops the HTTP server, if it was started. */ void Stop(void); - /** Loads the login template. Returns true if the loading success, false if not. */ + /** Loads the login template. Returns true if the loading succeeds, false if not. */ bool LoadLoginTemplate(void); void AddPlugin(cWebPlugin * a_Plugin); @@ -149,6 +149,9 @@ public: /** Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style) */ static AString GetBaseURL(const AStringVector & a_URLSplit); + /** Returns the content type from the file extension. If the extension isn't in the list, the function returns "text/html" */ + AString GetContentTypeFromFileExt(const AString & a_FileExtension); + protected: /** Common base class for request body data handlers */ class cRequestData @@ -208,7 +211,7 @@ protected: /** The Lua template script to provide templates: */ cLuaState m_TemplateScript; - /** The template who provide the login side: */ + /** The template that provides the login site: */ AString m_LoginTemplate; /** The HTTP server which provides the underlying HTTP parsing, serialization and events */ @@ -220,6 +223,9 @@ protected: /** Handles requests for the root page */ void HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request); + /** Handles requests for a file */ + void HandleFileRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request); + // cHTTPServer::cCallbacks overrides: virtual void OnRequestBegun (cHTTPConnection & a_Connection, cHTTPRequest & a_Request) override; virtual void OnRequestBody (cHTTPConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size) override; -- cgit v1.2.3 From 254c8c9154e8355f139e43cf482366bb4855a4f8 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 16:40:03 +0300 Subject: Anvil: Fixed loading block entities with invalid Y coord. --- src/WorldStorage/WSSAnvil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 4d2f92173..b8f8ba019 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -589,7 +589,7 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntities, con // Get the BlockEntity's position int x, y, z; - if (!GetBlockEntityNBTPos(a_NBT, Child, x, y, z)) + if (!GetBlockEntityNBTPos(a_NBT, Child, x, y, z) || (y < 0) || (y >= cChunkDef::Height)) { LOGWARNING("Bad block entity, missing the coords. Will be ignored."); continue; -- cgit v1.2.3 From 6444bc25ba326b20f2d6bd2e64992e7d7b8e7231 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 16:55:16 +0200 Subject: Fixed security: Player cannot spawn in a disabled world. --- src/Entities/Player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 756410989..51a1f8699 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -1670,7 +1670,7 @@ bool cPlayer::LoadFromFile(const AString & a_FileName, cWorldPtr & a_World) cEnderChestEntity::LoadFromJson(root["enderchestinventory"], m_EnderChestContents); m_LoadedWorldName = root.get("world", "world").asString(); - a_World = cRoot::Get()->GetWorld(GetLoadedWorldName(), true); + a_World = cRoot::Get()->GetWorld(GetLoadedWorldName(), false); m_LastBedPos.x = root.get("SpawnX", a_World->GetSpawnX()).asInt(); m_LastBedPos.y = root.get("SpawnY", a_World->GetSpawnY()).asInt(); -- cgit v1.2.3 From 60e235362c9662ad01b68141b32f5aae28a2fdfe Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 22:07:13 +0200 Subject: Fixed style. --- src/Generating/BioGen.cpp | 4 ++-- src/Generating/BioGen.h | 14 +++++++++----- src/Generating/ComposableGenerator.cpp | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Generating/BioGen.cpp b/src/Generating/BioGen.cpp index 175e4ef33..60ad4e3eb 100644 --- a/src/Generating/BioGen.cpp +++ b/src/Generating/BioGen.cpp @@ -227,7 +227,7 @@ cBioGenMulticache::cBioGenMulticache(cBiomeGen * a_BioGenToCache, size_t a_Cache cBioGenMulticache::~cBioGenMulticache() { - for (std::vector::iterator it = m_Caches.begin(); it != m_Caches.end(); it++) + for (cBiomeGens::iterator it = m_Caches.begin(); it != m_Caches.end(); it++) { delete *it; } @@ -251,7 +251,7 @@ void cBioGenMulticache::GenBiomes(int a_ChunkX, int a_ChunkZ, cChunkDef::BiomeMa void cBioGenMulticache::InitializeBiomeGen(cIniFile & a_IniFile) { - for (std::vector::iterator it = m_Caches.begin(); it != m_Caches.end(); it++) + for (cBiomeGens::iterator it = m_Caches.begin(); it != m_Caches.end(); it++) { cBiomeGen * tmp = *it; tmp->InitializeBiomeGen(a_IniFile); diff --git a/src/Generating/BioGen.h b/src/Generating/BioGen.h index a4cf95a72..20d199611 100644 --- a/src/Generating/BioGen.h +++ b/src/Generating/BioGen.h @@ -87,16 +87,20 @@ class cBioGenMulticache : typedef cBiomeGen super; public: - /* - a_CacheSize defines the size of each singular cache - a_CachesLength defines how many caches are used for the multicache + /* + a_CacheSize defines the size of each singular cache + a_CachesLength defines how many caches are used for the multicache */ cBioGenMulticache(cBiomeGen * a_BioGenToCache, size_t a_CacheSize, size_t a_CachesLength); // Doesn't take ownership of a_BioGenToCache ~cBioGenMulticache(); protected: - size_t m_CachesLength; - std::vector m_Caches; + typedef std::vector cBiomeGens; + + + size_t m_CachesLength; + cBiomeGens m_Caches; + virtual void GenBiomes(int a_ChunkX, int a_ChunkZ, cChunkDef::BiomeMap & a_BiomeMap) override; virtual void InitializeBiomeGen(cIniFile & a_IniFile) override; diff --git a/src/Generating/ComposableGenerator.cpp b/src/Generating/ComposableGenerator.cpp index d70438bf3..d98153e22 100644 --- a/src/Generating/ComposableGenerator.cpp +++ b/src/Generating/ComposableGenerator.cpp @@ -243,12 +243,12 @@ void cComposableGenerator::InitBiomeGen(cIniFile & a_IniFile) } LOGD("Using a cache for biomegen of size %d.", CacheSize); m_UnderlyingBiomeGen = m_BiomeGen; - if (MultiCacheLength > 0) + if (MultiCacheLength > 0) { LOGD("Enabling multicache for biomegen of length %d.", MultiCacheLength); m_BiomeGen = new cBioGenMulticache(m_UnderlyingBiomeGen, CacheSize, MultiCacheLength); } - else + else { m_BiomeGen = new cBioGenCache(m_UnderlyingBiomeGen, CacheSize); } -- cgit v1.2.3 From e15b8600a6dcc16fcfb406ee18eb6568d0de7994 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 22:08:00 +0200 Subject: BiomeMultiCache is not used for simple generators. --- src/Generating/ComposableGenerator.cpp | 43 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/Generating/ComposableGenerator.cpp b/src/Generating/ComposableGenerator.cpp index d98153e22..69068d231 100644 --- a/src/Generating/ComposableGenerator.cpp +++ b/src/Generating/ComposableGenerator.cpp @@ -230,29 +230,30 @@ void cComposableGenerator::InitBiomeGen(cIniFile & a_IniFile) // Add a cache, if requested: int CacheSize = a_IniFile.GetValueSetI("Generator", "BiomeGenCacheSize", CacheOffByDefault ? 0 : 64); - int MultiCacheLength = a_IniFile.GetValueSetI("Generator", "BiomeGenMultiCacheLength", 4); - if (CacheSize > 0) + if (CacheSize <= 0) { - if (CacheSize < 4) - { - LOGWARNING("Biomegen cache size set too low, would hurt performance instead of helping. Increasing from %d to %d", - CacheSize, 4 - ); - CacheSize = 4; - } - LOGD("Using a cache for biomegen of size %d.", CacheSize); - m_UnderlyingBiomeGen = m_BiomeGen; - if (MultiCacheLength > 0) - { - LOGD("Enabling multicache for biomegen of length %d.", MultiCacheLength); - m_BiomeGen = new cBioGenMulticache(m_UnderlyingBiomeGen, CacheSize, MultiCacheLength); - } - else - { - m_BiomeGen = new cBioGenCache(m_UnderlyingBiomeGen, CacheSize); - } - + return; + } + + int MultiCacheLength = a_IniFile.GetValueSetI("Generator", "BiomeGenMultiCacheLength", 4); + if (CacheSize < 4) + { + LOGWARNING("Biomegen cache size set too low, would hurt performance instead of helping. Increasing from %d to %d", + CacheSize, 4 + ); + CacheSize = 4; + } + LOGD("Using a cache for biomegen of size %d.", CacheSize); + m_UnderlyingBiomeGen = m_BiomeGen; + if (MultiCacheLength > 0) + { + LOGD("Enabling multicache for biomegen of length %d.", MultiCacheLength); + m_BiomeGen = new cBioGenMulticache(m_UnderlyingBiomeGen, CacheSize, MultiCacheLength); + } + else + { + m_BiomeGen = new cBioGenCache(m_UnderlyingBiomeGen, CacheSize); } } -- cgit v1.2.3 From 137b021d26d47b11fc27df1c0b52f408f0ef5257 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 22:16:48 +0200 Subject: Rewritten chunk status to specify whether the chunk is in queue. This fixes #1370. --- src/Chunk.cpp | 50 +++++++++++++++++++------- src/Chunk.h | 44 +++++++++++++++++++---- src/ChunkMap.cpp | 75 +++++++++++++-------------------------- src/ChunkMap.h | 9 ++--- src/Generating/ChunkGenerator.cpp | 3 +- src/Generating/ChunkGenerator.h | 4 +++ src/World.cpp | 38 ++++++++++---------- src/World.h | 14 ++++---- src/WorldStorage/WorldStorage.cpp | 11 +++--- 9 files changed, 141 insertions(+), 107 deletions(-) (limited to 'src') diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 9fb178bc3..caace5024 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -71,7 +71,7 @@ cChunk::cChunk( cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP, cAllocationPool & a_Pool ) : - m_IsValid(false), + m_Presence(cpInvalid), m_IsLightValid(false), m_IsDirty(false), m_IsSaving(false), @@ -165,11 +165,22 @@ cChunk::~cChunk() -void cChunk::SetValid(void) +void cChunk::SetPresence(cChunk::ePresence a_Presence) { - m_IsValid = true; - - m_World->GetChunkMap()->ChunkValidated(); + m_Presence = a_Presence; + if (a_Presence == cpPresent) + { + m_World->GetChunkMap()->ChunkValidated(); + } +} + + + + + +void cChunk::SetShouldGenerateIfLoadFailed(bool a_ShouldGenerateIfLoadFailed) +{ + m_ShouldGenerateIfLoadFailed = a_ShouldGenerateIfLoadFailed; } @@ -178,6 +189,9 @@ void cChunk::SetValid(void) void cChunk::MarkRegenerating(void) { + // Set as queued again: + SetPresence(cpQueued); + // Tell all clients attached to this chunk that they want this chunk: for (cClientHandleList::iterator itr = m_LoadedByClient.begin(); itr != m_LoadedByClient.end(); ++itr) { @@ -191,7 +205,11 @@ void cChunk::MarkRegenerating(void) bool cChunk::CanUnload(void) { - return m_LoadedByClient.empty() && !m_IsDirty && (m_StayCount == 0); + return + m_LoadedByClient.empty() && // The chunk is not used by any client + !m_IsDirty && // The chunk has been saved properly or hasn't been touched since the load / gen + (m_StayCount == 0) && // The chunk is not in a ChunkStay + (m_Presence != cpQueued) ; // The chunk is not queued for loading / generating (otherwise multi-load / multi-gen could occur) } @@ -223,7 +241,7 @@ void cChunk::MarkSaved(void) void cChunk::MarkLoaded(void) { m_IsDirty = false; - SetValid(); + SetPresence(cpPresent); } @@ -232,12 +250,17 @@ void cChunk::MarkLoaded(void) void cChunk::MarkLoadFailed(void) { - if (m_IsValid) + ASSERT(m_Presence == cpQueued); + + // If the chunk is marked as needed, generate it: + if (m_ShouldGenerateIfLoadFailed) { - return; + m_World->GetGenerator().QueueGenerateChunk(m_PosX, m_PosZ, false); + } + else + { + m_Presence = cpInvalid; } - - m_HasLoadFailed = true; } @@ -246,6 +269,8 @@ void cChunk::MarkLoadFailed(void) void cChunk::GetAllData(cChunkDataCallback & a_Callback) { + ASSERT(m_Presence == cpPresent); + a_Callback.HeightMap(&m_HeightMap); a_Callback.BiomeData(&m_BiomeMap); @@ -272,6 +297,7 @@ void cChunk::SetAllData(cSetChunkData & a_SetChunkData) { ASSERT(a_SetChunkData.IsHeightMapValid()); ASSERT(a_SetChunkData.AreBiomesValid()); + ASSERT(IsQueued()); memcpy(m_BiomeMap, a_SetChunkData.GetBiomes(), sizeof(m_BiomeMap)); memcpy(m_HeightMap, a_SetChunkData.GetHeightMap(), sizeof(m_HeightMap)); @@ -317,7 +343,7 @@ void cChunk::SetAllData(cSetChunkData & a_SetChunkData) CreateBlockEntities(); // Set the chunk data as valid. This may be needed for some simulators that perform actions upon block adding (Vaporize) - SetValid(); + SetPresence(cpPresent); // Wake up all simulators for their respective blocks: WakeUpSimulators(); diff --git a/src/Chunk.h b/src/Chunk.h index 0f4928b90..81862f2e7 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -66,6 +66,14 @@ class cChunk : public cChunkDef // The inheritance is "misused" here only to inherit the functions and constants defined in cChunkDef { public: + /** Represents the presence state of the chunk */ + enum ePresence + { + cpInvalid, /**< The chunk is not present at all and is not queued in the loader / generator */ + cpQueued, /**< The chunk is not present, but is queued for loading / generation */ + cpPresent, /**< The chunk is present */ + }; + cChunk( int a_ChunkX, int a_ChunkZ, // Chunk coords cChunkMap * a_ChunkMap, cWorld * a_World, // Parent objects @@ -75,11 +83,25 @@ public: cChunk(cChunk & other); ~cChunk(); - bool IsValid(void) const {return m_IsValid; } // Returns true if the chunk block data is valid (loaded / generated) - void SetValid(void); // Also wakes up any calls to cChunkMap::GetHeight() - void MarkRegenerating(void); // Marks all clients attached to this chunk as wanting this chunk - bool IsDirty(void) const {return m_IsDirty; } // Returns true if the chunk has changed since it was last saved - bool HasLoadFailed(void) const {return m_HasLoadFailed; } // Returns true if the chunk failed to load and hasn't been generated since then + /** Returns true iff the chunk block data is valid (loaded / generated) */ + bool IsValid(void) const {return (m_Presence == cpPresent); } + + /** Returns true iff the chunk is in the queue for loading / generating */ + bool IsQueued(void) const {return (m_Presence == cpQueued); } + + /** Sets the chunk's presence. + Wakes up any calls to cChunkMap::GetHeight() when setting to cpPresent. */ + void SetPresence(ePresence a_Presence); + + /** Called to indicate whether the chunk should be queued in the generator if it fails to load. Set by cChunkMap::GetChunk(). */ + void SetShouldGenerateIfLoadFailed(bool a_ShouldGenerateIfLoadFailed); + + /** Marks all clients attached to this chunk as wanting this chunk. Also sets presence to cpQueued. */ + void MarkRegenerating(void); + + /** Returns true iff the chunk has changed since it was last saved. */ + bool IsDirty(void) const {return m_IsDirty; } + bool CanUnload(void); bool IsLightValid(void) const {return m_IsLightValid; } @@ -94,7 +116,10 @@ public: void MarkSaving(void); // Marks the chunk as being saved. void MarkSaved(void); // Marks the chunk as saved, if it didn't change from the last call to MarkSaving() void MarkLoaded(void); // Marks the chunk as freshly loaded. Fails if the chunk is already valid - void MarkLoadFailed(void); // Marks the chunk as failed to load. Ignored is the chunk is already valid + + /** Marks the chunk as failed to load. + If m_ShouldGenerateIfLoadFailed is set, queues the chunk for generating. */ + void MarkLoadFailed(void); /** Gets all chunk data, calls the a_Callback's methods for each data type */ void GetAllData(cChunkDataCallback & a_Callback); @@ -434,7 +459,12 @@ private: typedef std::vector sSetBlockQueueVector; - bool m_IsValid; // True if the chunk is loaded / generated + /** Holds the presence status of the chunk - if it is present, or in the loader / generator queue, or unloaded */ + ePresence m_Presence; + + /** If the chunk fails to load, should it be queued in the generator or reset back to invalid? */ + bool m_ShouldGenerateIfLoadFailed; + bool m_IsLightValid; // True if the blocklight and skylight are calculated bool m_IsDirty; // True if the chunk has changed since it was last saved bool m_IsSaving; // True if the chunk is being saved diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 8c765c8c9..50066e539 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -145,10 +145,9 @@ cChunkMap::cChunkLayer * cChunkMap::GetLayerForChunk(int a_ChunkX, int a_ChunkZ) cChunkPtr cChunkMap::GetChunk(int a_ChunkX, int a_ChunkZ) { - // No need to lock m_CSLayers, since it's already locked by the operation that called us - ASSERT(m_CSLayers.IsLockedByCurrentThread()); + ASSERT(m_CSLayers.IsLockedByCurrentThread()); // m_CSLayers should already be locked by the operation that called us - cChunkLayer * Layer = GetLayerForChunk( a_ChunkX, a_ChunkZ); + cChunkLayer * Layer = GetLayerForChunk(a_ChunkX, a_ChunkZ); if (Layer == NULL) { // An error must have occurred, since layers are automatically created if they don't exist @@ -160,8 +159,10 @@ cChunkPtr cChunkMap::GetChunk(int a_ChunkX, int a_ChunkZ) { return NULL; } - if (!(Chunk->IsValid())) + if (!Chunk->IsValid() && !Chunk->IsQueued()) { + Chunk->SetPresence(cChunk::cpQueued); + Chunk->SetShouldGenerateIfLoadFailed(true); m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ, true); } return Chunk; @@ -171,10 +172,11 @@ cChunkPtr cChunkMap::GetChunk(int a_ChunkX, int a_ChunkZ) -cChunkPtr cChunkMap::GetChunkNoGen( int a_ChunkX, int a_ChunkZ) +cChunkPtr cChunkMap::GetChunkNoGen(int a_ChunkX, int a_ChunkZ) { - // No need to lock m_CSLayers, since it's already locked by the operation that called us - cChunkLayer * Layer = GetLayerForChunk( a_ChunkX, a_ChunkZ); + ASSERT(m_CSLayers.IsLockedByCurrentThread()); // m_CSLayers should already be locked by the operation that called us + + cChunkLayer * Layer = GetLayerForChunk(a_ChunkX, a_ChunkZ); if (Layer == NULL) { // An error must have occurred, since layers are automatically created if they don't exist @@ -186,8 +188,9 @@ cChunkPtr cChunkMap::GetChunkNoGen( int a_ChunkX, int a_ChunkZ) { return NULL; } - if (!(Chunk->IsValid())) + if (!Chunk->IsValid() && !Chunk->IsQueued()) { + Chunk->SetPresence(cChunk::cpQueued); m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ, false); } @@ -200,7 +203,8 @@ cChunkPtr cChunkMap::GetChunkNoGen( int a_ChunkX, int a_ChunkZ) cChunkPtr cChunkMap::GetChunkNoLoad( int a_ChunkX, int a_ChunkZ) { - // No need to lock m_CSLayers, since it's already locked by the operation that called us + ASSERT(m_CSLayers.IsLockedByCurrentThread()); // m_CSLayers should already be locked by the operation that called us + cChunkLayer * Layer = GetLayerForChunk( a_ChunkX, a_ChunkZ); if (Layer == NULL) { @@ -1009,6 +1013,17 @@ bool cChunkMap::GetChunkBlockTypes(int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_Blo +bool cChunkMap::IsChunkQueued(int a_ChunkX, int a_ChunkZ) +{ + cCSLock Lock(m_CSLayers); + cChunkPtr Chunk = GetChunkNoLoad(a_ChunkX, a_ChunkZ); + return (Chunk != NULL) && Chunk->IsQueued(); +} + + + + + bool cChunkMap::IsChunkValid(int a_ChunkX, int a_ChunkZ) { cCSLock Lock(m_CSLayers); @@ -2332,48 +2347,6 @@ void cChunkMap::TouchChunk(int a_ChunkX, int a_ChunkZ) -/// Loads the chunk synchronously, if not already loaded. Doesn't generate. Returns true if chunk valid (even if already loaded before) -bool cChunkMap::LoadChunk(int a_ChunkX, int a_ChunkZ) -{ - { - cCSLock Lock(m_CSLayers); - cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ); - if (Chunk == NULL) - { - // Internal error - return false; - } - if (Chunk->IsValid()) - { - // Already loaded - return true; - } - if (Chunk->HasLoadFailed()) - { - // Already tried loading and it failed - return false; - } - } - return m_World->GetStorage().LoadChunk(a_ChunkX, a_ChunkZ); -} - - - - - -/// Loads the chunks specified. Doesn't report failure, other than chunks being !IsValid() -void cChunkMap::LoadChunks(const cChunkCoordsList & a_Chunks) -{ - for (cChunkCoordsList::const_iterator itr = a_Chunks.begin(); itr != a_Chunks.end(); ++itr) - { - LoadChunk(itr->m_ChunkX, itr->m_ChunkZ); - } // for itr - a_Chunks[] -} - - - - - void cChunkMap::ChunkLoadFailed(int a_ChunkX, int a_ChunkZ) { cCSLock Lock(m_CSLayers); diff --git a/src/ChunkMap.h b/src/ChunkMap.h index 8c564d0de..7354536d4 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -133,6 +133,9 @@ public: /** Copies the chunk's blocktypes into a_Blocks; returns true if successful */ bool GetChunkBlockTypes (int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_Blocks); + /** Returns true iff the chunk is in the loader / generator queue. */ + bool IsChunkQueued(int a_ChunkX, int a_ChunkZ); + bool IsChunkValid (int a_ChunkX, int a_ChunkZ); bool HasChunkAnyClients (int a_ChunkX, int a_ChunkZ); int GetHeight (int a_BlockX, int a_BlockZ); // Waits for the chunk to get loaded / generated @@ -278,12 +281,6 @@ public: /** Touches the chunk, causing it to be loaded or generated */ void TouchChunk(int a_ChunkX, int a_ChunkZ); - /** Loads the chunk, if not already loaded. Doesn't generate. Returns true if chunk valid (even if already loaded before) */ - bool LoadChunk(int a_ChunkX, int a_ChunkZ); - - /** Loads the chunks specified. Doesn't report failure, other than chunks being !IsValid() */ - void LoadChunks(const cChunkCoordsList & a_Chunks); - /** Marks the chunk as failed-to-load */ void ChunkLoadFailed(int a_ChunkX, int a_ChunkZ); diff --git a/src/Generating/ChunkGenerator.cpp b/src/Generating/ChunkGenerator.cpp index 4fa9729ec..d8dbb4a3a 100644 --- a/src/Generating/ChunkGenerator.cpp +++ b/src/Generating/ChunkGenerator.cpp @@ -283,7 +283,8 @@ void cChunkGenerator::DoGenerate(int a_ChunkX, int a_ChunkZ) { ASSERT(m_PluginInterface != NULL); ASSERT(m_ChunkSink != NULL); - + ASSERT(m_ChunkSink->IsChunkQueued(a_ChunkX, a_ChunkZ)); + cChunkDesc ChunkDesc(a_ChunkX, a_ChunkZ); m_PluginInterface->CallHookChunkGenerating(ChunkDesc); m_Generator->DoGenerate(a_ChunkX, a_ChunkZ, ChunkDesc); diff --git a/src/Generating/ChunkGenerator.h b/src/Generating/ChunkGenerator.h index e880a6766..190d9e616 100644 --- a/src/Generating/ChunkGenerator.h +++ b/src/Generating/ChunkGenerator.h @@ -106,6 +106,10 @@ public: If this callback returns false, the chunk is not generated. */ virtual bool HasChunkAnyClients(int a_ChunkX, int a_ChunkZ) = 0; + + /** Called to check whether the specified chunk is in the queued state. + Currently used only in Debug-mode asserts. */ + virtual bool IsChunkQueued(int a_ChunkX, int a_ChunkZ) = 0; } ; diff --git a/src/World.cpp b/src/World.cpp index 2a3336dee..e669f6fa0 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2374,6 +2374,8 @@ void cWorld::MarkChunkSaved (int a_ChunkX, int a_ChunkZ) void cWorld::QueueSetChunkData(const cSetChunkDataPtr & a_SetChunkData) { + ASSERT(IsChunkQueued(a_SetChunkData->GetChunkX(), a_SetChunkData->GetChunkZ())); + // Validate biomes, if needed: if (!a_SetChunkData->AreBiomesValid()) { @@ -2463,6 +2465,15 @@ bool cWorld::GetChunkBlockTypes(int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_BlockT +bool cWorld::IsChunkQueued(int a_ChunkX, int a_ChunkZ) const +{ + return m_ChunkMap->IsChunkQueued(a_ChunkX, a_ChunkZ); +} + + + + + bool cWorld::IsChunkValid(int a_ChunkX, int a_ChunkZ) const { return m_ChunkMap->IsChunkValid(a_ChunkX, a_ChunkZ); @@ -2787,24 +2798,6 @@ void cWorld::TouchChunk(int a_ChunkX, int a_ChunkZ) -bool cWorld::LoadChunk(int a_ChunkX, int a_ChunkZ) -{ - return m_ChunkMap->LoadChunk(a_ChunkX, a_ChunkZ); -} - - - - - -void cWorld::LoadChunks(const cChunkCoordsList & a_Chunks) -{ - m_ChunkMap->LoadChunks(a_Chunks); -} - - - - - void cWorld::ChunkLoadFailed(int a_ChunkX, int a_ChunkZ) { m_ChunkMap->ChunkLoadFailed(a_ChunkX, a_ChunkZ); @@ -3520,6 +3513,15 @@ bool cWorld::cChunkGeneratorCallbacks::IsChunkValid(int a_ChunkX, int a_ChunkZ) +bool cWorld::cChunkGeneratorCallbacks::IsChunkQueued(int a_ChunkX, int a_ChunkZ) +{ + return m_World->IsChunkQueued(a_ChunkX, a_ChunkZ); +} + + + + + bool cWorld::cChunkGeneratorCallbacks::HasChunkAnyClients(int a_ChunkX, int a_ChunkZ) { return m_World->HasChunkAnyClients(a_ChunkX, a_ChunkZ); diff --git a/src/World.h b/src/World.h index 49932ac9d..6316c6811 100644 --- a/src/World.h +++ b/src/World.h @@ -279,7 +279,12 @@ public: /** Gets the chunk's blocks, only the block types */ bool GetChunkBlockTypes(int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_BlockTypes); - bool IsChunkValid (int a_ChunkX, int a_ChunkZ) const; + /** Returns true iff the chunk is in the loader / generator queue. */ + bool IsChunkQueued(int a_ChunkX, int a_ChunkZ) const; + + /** Returns true iff the chunk is present and valid. */ + bool IsChunkValid(int a_ChunkX, int a_ChunkZ) const; + bool HasChunkAnyClients(int a_ChunkX, int a_ChunkZ) const; /** Queues a task to unload unused chunks onto the tick thread. The prefferred way of unloading*/ @@ -358,12 +363,6 @@ public: /** Touches the chunk, causing it to be loaded or generated */ void TouchChunk(int a_ChunkX, int a_ChunkZ); - /** Loads the chunk, if not already loaded. Doesn't generate. Returns true if chunk valid (even if already loaded before) */ - bool LoadChunk(int a_ChunkX, int a_ChunkZ); - - /** Loads the chunks specified. Doesn't report failure, other than chunks being !IsValid() */ - void LoadChunks(const cChunkCoordsList & a_Chunks); - /** Marks the chunk as failed-to-load: */ void ChunkLoadFailed(int a_ChunkX, int a_ChunkZ); @@ -822,6 +821,7 @@ private: virtual void OnChunkGenerated (cChunkDesc & a_ChunkDesc) override; virtual bool IsChunkValid (int a_ChunkX, int a_ChunkZ) override; virtual bool HasChunkAnyClients(int a_ChunkX, int a_ChunkZ) override; + virtual bool IsChunkQueued (int a_ChunkX, int a_ChunkZ) override; // cPluginInterface overrides: virtual void CallHookChunkGenerating(cChunkDesc & a_ChunkDesc) override; diff --git a/src/WorldStorage/WorldStorage.cpp b/src/WorldStorage/WorldStorage.cpp index 667a28470..899b0beb3 100644 --- a/src/WorldStorage/WorldStorage.cpp +++ b/src/WorldStorage/WorldStorage.cpp @@ -143,6 +143,8 @@ size_t cWorldStorage::GetSaveQueueLength(void) void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkZ, bool a_Generate) { + ASSERT(m_World->IsChunkQueued(a_ChunkX, a_ChunkZ)); + m_LoadQueue.EnqueueItem(sChunkLoad(a_ChunkX, a_ChunkZ, a_Generate)); m_Event.Set(); } @@ -153,6 +155,8 @@ void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkZ, bool a_Generate) void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkZ) { + ASSERT(m_World->IsChunkValid(a_ChunkX, a_ChunkZ)); + m_SaveQueue.EnqueueItemIfNotPresent(cChunkCoords(a_ChunkX, a_ChunkZ)); m_Event.Set(); } @@ -244,6 +248,7 @@ bool cWorldStorage::LoadOneChunk(void) { sChunkLoad ToLoad(0, 0, false); bool ShouldLoad = m_LoadQueue.TryDequeueItem(ToLoad); + if (ShouldLoad && !LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkZ)) { if (ToLoad.m_Generate) @@ -285,11 +290,7 @@ bool cWorldStorage::SaveOneChunk(void) bool cWorldStorage::LoadChunk(int a_ChunkX, int a_ChunkZ) { - if (m_World->IsChunkValid(a_ChunkX, a_ChunkZ)) - { - // Already loaded (can happen, since the queue is async) - return true; - } + ASSERT(m_World->IsChunkQueued(a_ChunkX, a_ChunkZ)); cChunkCoords Coords(a_ChunkX, a_ChunkZ); -- cgit v1.2.3 From 4230eb3d924a2230a20e5044dfffa118ad090429 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 22:55:39 +0200 Subject: Fixed loading empty chunks. Reported on the Dropper map in #1307. --- src/SetChunkData.cpp | 1 - src/WorldStorage/WSSAnvil.cpp | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/SetChunkData.cpp b/src/SetChunkData.cpp index bfe59fbcb..707dfb9e8 100644 --- a/src/SetChunkData.cpp +++ b/src/SetChunkData.cpp @@ -44,7 +44,6 @@ cSetChunkData::cSetChunkData( // Check the params' validity: ASSERT(a_BlockTypes != NULL); ASSERT(a_BlockMetas != NULL); - ASSERT(a_Biomes != NULL); // Copy block types and metas: memcpy(m_BlockTypes, a_BlockTypes, sizeof(cChunkDef::BlockTypes)); diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index b8f8ba019..f78ee405b 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -323,7 +323,13 @@ bool cWSSAnvil::LoadChunkFromNBT(const cChunkCoords & a_Chunk, const cParsedNBT return false; } int Sections = a_NBT.FindChildByName(Level, "Sections"); - if ((Sections < 0) || (a_NBT.GetType(Sections) != TAG_List) || (a_NBT.GetChildrenType(Sections) != TAG_Compound)) + if ((Sections < 0) || (a_NBT.GetType(Sections) != TAG_List)) + { + LOAD_FAILED(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ); + return false; + } + eTagType SectionsType = a_NBT.GetChildrenType(Sections); + if ((SectionsType != TAG_Compound) && (SectionsType != TAG_End)) { LOAD_FAILED(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ); return false; -- cgit v1.2.3 From 103fa8812d6bb0fcd996d1d75817d657a0a2691c Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 5 Sep 2014 23:26:00 +0200 Subject: WorldStorage no longer queues chunks into generator. --- src/ChunkMap.cpp | 4 ++-- src/Generating/ChunkGenerator.cpp | 2 ++ src/WorldStorage/WorldStorage.cpp | 23 +++++++---------------- src/WorldStorage/WorldStorage.h | 39 ++++++--------------------------------- 4 files changed, 17 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 50066e539..9c105c5af 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -163,7 +163,7 @@ cChunkPtr cChunkMap::GetChunk(int a_ChunkX, int a_ChunkZ) { Chunk->SetPresence(cChunk::cpQueued); Chunk->SetShouldGenerateIfLoadFailed(true); - m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ, true); + m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ); } return Chunk; } @@ -191,7 +191,7 @@ cChunkPtr cChunkMap::GetChunkNoGen(int a_ChunkX, int a_ChunkZ) if (!Chunk->IsValid() && !Chunk->IsQueued()) { Chunk->SetPresence(cChunk::cpQueued); - m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ, false); + m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ); } return Chunk; diff --git a/src/Generating/ChunkGenerator.cpp b/src/Generating/ChunkGenerator.cpp index d8dbb4a3a..d615456c1 100644 --- a/src/Generating/ChunkGenerator.cpp +++ b/src/Generating/ChunkGenerator.cpp @@ -112,6 +112,8 @@ void cChunkGenerator::Stop(void) void cChunkGenerator::QueueGenerateChunk(int a_ChunkX, int a_ChunkZ, bool a_ForceGenerate) { + ASSERT(m_ChunkSink->IsChunkQueued(a_ChunkX, a_ChunkZ)); + { cCSLock Lock(m_CS); diff --git a/src/WorldStorage/WorldStorage.cpp b/src/WorldStorage/WorldStorage.cpp index 899b0beb3..179cf9393 100644 --- a/src/WorldStorage/WorldStorage.cpp +++ b/src/WorldStorage/WorldStorage.cpp @@ -141,11 +141,11 @@ size_t cWorldStorage::GetSaveQueueLength(void) -void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkZ, bool a_Generate) +void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkZ) { ASSERT(m_World->IsChunkQueued(a_ChunkX, a_ChunkZ)); - m_LoadQueue.EnqueueItem(sChunkLoad(a_ChunkX, a_ChunkZ, a_Generate)); + m_LoadQueue.EnqueueItem(cChunkCoords(a_ChunkX, a_ChunkZ)); m_Event.Set(); } @@ -167,7 +167,7 @@ void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkZ) void cWorldStorage::UnqueueLoad(int a_ChunkX, int a_ChunkZ) { - m_LoadQueue.Remove(sChunkLoad(a_ChunkX, a_ChunkZ, true)); + m_LoadQueue.Remove(cChunkCoords(a_ChunkX, a_ChunkZ)); } @@ -246,23 +246,14 @@ void cWorldStorage::Execute(void) bool cWorldStorage::LoadOneChunk(void) { - sChunkLoad ToLoad(0, 0, false); + cChunkCoords ToLoad(0, 0); bool ShouldLoad = m_LoadQueue.TryDequeueItem(ToLoad); - if (ShouldLoad && !LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkZ)) + if (ShouldLoad) { - if (ToLoad.m_Generate) - { - // The chunk couldn't be loaded, generate it: - m_World->GetGenerator().QueueGenerateChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkZ, false); - } - else - { - // TODO: Notify the world that the load has failed: - // m_World->ChunkLoadFailed(ToLoad.m_ChunkX, ToLoad.m_ChunkZ); - } + return LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkZ); } - return ShouldLoad; + return false; } diff --git a/src/WorldStorage/WorldStorage.h b/src/WorldStorage/WorldStorage.h index 5f89ead53..fc7e9f84c 100644 --- a/src/WorldStorage/WorldStorage.h +++ b/src/WorldStorage/WorldStorage.h @@ -64,12 +64,9 @@ public: cWorldStorage(void); ~cWorldStorage(); - void QueueLoadChunk(int a_ChunkX, int a_ChunkZ, bool a_Generate); // Queues the chunk for loading; if not loaded, the chunk will be generated if a_Generate is true + void QueueLoadChunk(int a_ChunkX, int a_ChunkZ); void QueueSaveChunk(int a_ChunkX, int a_ChunkZ); - /// Loads the chunk specified; returns true on success, false on failure - bool LoadChunk(int a_ChunkX, int a_ChunkZ); - void UnqueueLoad(int a_ChunkX, int a_ChunkZ); void UnqueueSave(const cChunkCoords & a_Chunk); @@ -84,38 +81,10 @@ public: protected: - struct sChunkLoad - { - int m_ChunkX; - int m_ChunkZ; - bool m_Generate; // If true, the chunk will be generated if it cannot be loaded - - sChunkLoad(int a_ChunkX, int a_ChunkZ, bool a_Generate) : m_ChunkX(a_ChunkX), m_ChunkZ(a_ChunkZ), m_Generate(a_Generate) {} - - bool operator ==(const sChunkLoad other) const - { - return ( - (this->m_ChunkX == other.m_ChunkX) && - (this->m_ChunkZ == other.m_ChunkZ) - ); - } - } ; - - struct FuncTable - { - static void Delete(sChunkLoad) {} - static void Combine(sChunkLoad & a_orig, const sChunkLoad a_new) - { - a_orig.m_Generate |= a_new.m_Generate; - } - }; - - typedef cQueue sChunkLoadQueue; - cWorld * m_World; AString m_StorageSchemaName; - sChunkLoadQueue m_LoadQueue; + cChunkCoordsQueue m_LoadQueue; cChunkCoordsQueue m_SaveQueue; /// All the storage schemas (all used for loading) @@ -123,7 +92,11 @@ protected: /// The one storage schema used for saving cWSSchema * m_SaveSchema; + + /// Loads the chunk specified; returns true on success, false on failure + bool LoadChunk(int a_ChunkX, int a_ChunkZ); + void InitSchemas(int a_StorageCompressionFactor); virtual void Execute(void) override; -- cgit v1.2.3 From 15d4e543b1bcc56923b2f94bf0a63b6ed8cc8edc Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sat, 6 Sep 2014 09:56:22 +0100 Subject: Xoft fixes. --- src/WebAdmin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/WebAdmin.h b/src/WebAdmin.h index d6baa2ef9..94b95dbcf 100644 --- a/src/WebAdmin.h +++ b/src/WebAdmin.h @@ -150,7 +150,7 @@ public: static AString GetBaseURL(const AStringVector & a_URLSplit); /** Returns the content type from the file extension. If the extension isn't in the list, the function returns "text/html" */ - AString GetContentTypeFromFileExt(const AString & a_FileExtension); + static AString GetContentTypeFromFileExt(const AString & a_FileExtension); protected: /** Common base class for request body data handlers */ -- cgit v1.2.3 From dd0aa22d2e5720aac5df473382c3ddaa1ca7e484 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 6 Sep 2014 13:11:08 +0200 Subject: Fixed scoreboard loader type checks. Fixes scoreboard loading error reported on the Dropper map in #1307. --- src/WorldStorage/ScoreboardSerializer.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/WorldStorage/ScoreboardSerializer.cpp b/src/WorldStorage/ScoreboardSerializer.cpp index da8236e0d..e30eecf67 100644 --- a/src/WorldStorage/ScoreboardSerializer.cpp +++ b/src/WorldStorage/ScoreboardSerializer.cpp @@ -283,37 +283,37 @@ bool cScoreboardSerializer::LoadScoreboardFromNBT(const cParsedNBT & a_NBT) bool AllowsFriendlyFire = true, CanSeeFriendlyInvisible = false; int CurrLine = a_NBT.FindChildByName(Child, "Name"); - if (CurrLine >= 0) + if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String)) { - Name = a_NBT.GetInt(CurrLine); + Name = a_NBT.GetString(CurrLine); } CurrLine = a_NBT.FindChildByName(Child, "DisplayName"); - if (CurrLine >= 0) + if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String)) { - DisplayName = a_NBT.GetInt(CurrLine); + DisplayName = a_NBT.GetString(CurrLine); } CurrLine = a_NBT.FindChildByName(Child, "Prefix"); - if (CurrLine >= 0) + if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String)) { - Prefix = a_NBT.GetInt(CurrLine); + Prefix = a_NBT.GetString(CurrLine); } CurrLine = a_NBT.FindChildByName(Child, "Suffix"); - if (CurrLine >= 0) + if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String)) { - Suffix = a_NBT.GetInt(CurrLine); + Suffix = a_NBT.GetString(CurrLine); } CurrLine = a_NBT.FindChildByName(Child, "AllowFriendlyFire"); - if (CurrLine >= 0) + if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Int)) { AllowsFriendlyFire = (a_NBT.GetInt(CurrLine) != 0); } CurrLine = a_NBT.FindChildByName(Child, "SeeFriendlyInvisibles"); - if (CurrLine >= 0) + if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Int)) { CanSeeFriendlyInvisible = (a_NBT.GetInt(CurrLine) != 0); } -- cgit v1.2.3 From 9f9302f470d2adc1ef4018474c4f03a72ca723ed Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 6 Sep 2014 13:32:16 +0200 Subject: Anvil: Fixed an off-by-one error in the loader. Fixes #1307. --- src/WorldStorage/WSSAnvil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index f78ee405b..2500b5dea 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -2821,7 +2821,7 @@ bool cWSSAnvil::cMCAFile::GetChunkData(const cChunkCoords & a_Chunk, AString & a } unsigned ChunkLocation = ntohl(m_Header[LocalX + 32 * LocalZ]); unsigned ChunkOffset = ChunkLocation >> 8; - if (ChunkOffset <= 2) + if (ChunkOffset < 2) { return false; } -- cgit v1.2.3 From 9e1f67932b32241312f25279d924c235d4cff66d Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 6 Sep 2014 15:26:20 +0200 Subject: Fixed player spawning in unknown world. This may happen if the admin removes a world. --- src/Entities/Player.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 51a1f8699..b0da6965a 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -1671,6 +1671,10 @@ bool cPlayer::LoadFromFile(const AString & a_FileName, cWorldPtr & a_World) m_LoadedWorldName = root.get("world", "world").asString(); a_World = cRoot::Get()->GetWorld(GetLoadedWorldName(), false); + if (a_World == NULL) + { + a_World = cRoot::Get()->GetDefaultWorld(); + } m_LastBedPos.x = root.get("SpawnX", a_World->GetSpawnX()).asInt(); m_LastBedPos.y = root.get("SpawnY", a_World->GetSpawnY()).asInt(); -- cgit v1.2.3 From 868723ce507eb7092fea4852c3a8f78d7391ed7c Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 6 Sep 2014 18:57:57 +0200 Subject: Alpha-sorted protocol 1.7 senders. --- src/Protocol/Protocol17x.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index ccfa19eb6..d3100d0fb 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -71,7 +71,9 @@ public: virtual void SendCollectEntity (const cEntity & a_Entity, const cPlayer & a_Player) override; virtual void SendDestroyEntity (const cEntity & a_Entity) override; virtual void SendDisconnect (const AString & a_Reason) override; + virtual void SendDisplayObjective (const AString & a_Objective, cScoreboard::eDisplaySlot a_Display) override; virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) override; ///< Request the client to open up the sign editor for the sign (1.6+) + virtual void SendEntityAnimation (const cEntity & a_Entity, char a_Animation) override; virtual void SendEntityEffect (const cEntity & a_Entity, int a_EffectID, int a_Amplifier, short a_Duration) override; virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) override; virtual void SendEntityHeadLook (const cEntity & a_Entity) override; @@ -82,6 +84,8 @@ public: virtual void SendEntityRelMoveLook (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; virtual void SendEntityStatus (const cEntity & a_Entity, char a_Status) override; virtual void SendEntityVelocity (const cEntity & a_Entity) override; + virtual void SendExperience (void) override; + virtual void SendExperienceOrb (const cExpOrb & a_ExpOrb) override; virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override; virtual void SendGameMode (eGameMode a_GameMode) override; virtual void SendHealth (void) override; @@ -93,10 +97,9 @@ public: virtual void SendMapDecorators (int a_ID, const cMapDecoratorList & a_Decorators) override; virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override; virtual void SendPaintingSpawn (const cPainting & a_Painting) override; + virtual void SendParticleEffect (const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount) override; virtual void SendPickupSpawn (const cPickup & a_Pickup) override; virtual void SendPlayerAbilities (void) override; - virtual void SendEntityAnimation (const cEntity & a_Entity, char a_Animation) override; - virtual void SendParticleEffect (const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount) override; virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; virtual void SendPlayerMaxSpeed (void) override; virtual void SendPlayerMoveLook (void) override; @@ -105,12 +108,9 @@ public: virtual void SendPluginMessage (const AString & a_Channel, const AString & a_Message) override; virtual void SendRemoveEntityEffect (const cEntity & a_Entity, int a_EffectID) override; virtual void SendRespawn (eDimension a_Dimension, bool a_ShouldIgnoreDimensionChecks) override; - virtual void SendSoundEffect (const AString & a_SoundName, double a_X, double a_Y, double a_Z, float a_Volume, float a_Pitch) override; - virtual void SendExperience (void) override; - virtual void SendExperienceOrb (const cExpOrb & a_ExpOrb) override; - virtual void SendScoreboardObjective (const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) override; virtual void SendScoreUpdate (const AString & a_Objective, const AString & a_Player, cObjective::Score a_Score, Byte a_Mode) override; - virtual void SendDisplayObjective (const AString & a_Objective, cScoreboard::eDisplaySlot a_Display) override; + virtual void SendScoreboardObjective (const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) override; + virtual void SendSoundEffect (const AString & a_SoundName, double a_X, double a_Y, double a_Z, float a_Volume, float a_Pitch) override; virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; virtual void SendSpawnMob (const cMonster & a_Mob) override; -- cgit v1.2.3 From 0a651b9fd83f078a73429f02603144e80a05148d Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 6 Sep 2014 18:59:17 +0200 Subject: Added Y-wise asserts to signs. This should help detect #1313's second case. --- src/BlockEntities/SignEntity.cpp | 1 + src/Protocol/Protocol17x.cpp | 1 + src/WorldStorage/WSSAnvil.cpp | 2 ++ 3 files changed, 4 insertions(+) (limited to 'src') diff --git a/src/BlockEntities/SignEntity.cpp b/src/BlockEntities/SignEntity.cpp index 97fed0f04..23d9ef926 100644 --- a/src/BlockEntities/SignEntity.cpp +++ b/src/BlockEntities/SignEntity.cpp @@ -15,6 +15,7 @@ cSignEntity::cSignEntity(BLOCKTYPE a_BlockType, int a_X, int a_Y, int a_Z, cWorld * a_World) : super(a_BlockType, a_X, a_Y, a_Z, a_World) { + ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height)); } diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 1091b877f..f24ef320d 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -1355,6 +1355,7 @@ void cProtocol172::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) void cProtocol172::SendUpdateSign(int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_BlockY >= 0) && (a_BlockY < cChunkDef::Height)); cPacketizer Pkt(*this, 0x33); Pkt.WriteInt(a_BlockX); diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 2500b5dea..fe309ce4e 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -623,6 +623,8 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntities, con cBlockEntity * cWSSAnvil::LoadBlockEntityFromNBT(const cParsedNBT & a_NBT, int a_Tag, int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { + ASSERT((a_BlockY >= 0) && (a_BlockY < cChunkDef::Height)); + // Load the specific BlockEntity type: switch (a_BlockType) { -- cgit v1.2.3 From e1a9a6291b98126dad993009bb184c963bd9765b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 6 Sep 2014 19:01:30 +0200 Subject: Removed chunk's unused Y coord. This fixes the second issue of #1313. --- src/Chunk.cpp | 6 +++--- src/Chunk.h | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Chunk.cpp b/src/Chunk.cpp index caace5024..99e48df95 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -1345,11 +1345,11 @@ void cChunk::CreateBlockEntities(void) case E_BLOCK_JUKEBOX: case E_BLOCK_FLOWER_POT: { - if (!HasBlockEntityAt(x + m_PosX * Width, y + m_PosY * Height, z + m_PosZ * Width)) + if (!HasBlockEntityAt(x + m_PosX * Width, y, z + m_PosZ * Width)) { m_BlockEntities.push_back(cBlockEntity::CreateByBlockType( BlockType, GetMeta(x, y, z), - x + m_PosX * Width, y + m_PosY * Height, z + m_PosZ * Width, m_World + x + m_PosX * Width, y, z + m_PosZ * Width, m_World )); } break; @@ -3178,7 +3178,7 @@ void cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ, int & a Vector3i cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ) { - return Vector3i(m_PosX * Width + a_RelX, m_PosY * Height + a_RelY, m_PosZ * Width + a_RelZ); + return Vector3i(m_PosX * Width + a_RelX, a_RelY, m_PosZ * Width + a_RelZ); } diff --git a/src/Chunk.h b/src/Chunk.h index 81862f2e7..f282694c2 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -160,7 +160,6 @@ public: void TickBlock(int a_RelX, int a_RelY, int a_RelZ); int GetPosX(void) const { return m_PosX; } - int GetPosY(void) const { return m_PosY; } int GetPosZ(void) const { return m_PosZ; } cWorld * GetWorld(void) const { return m_World; } @@ -483,7 +482,7 @@ private: /** Number of times the chunk has been requested to stay (by various cChunkStay objects); if zero, the chunk can be unloaded */ int m_StayCount; - int m_PosX, m_PosY, m_PosZ; + int m_PosX, m_PosZ; cWorld * m_World; cChunkMap * m_ChunkMap; -- cgit v1.2.3