diff options
52 files changed, 1563 insertions, 247 deletions
diff --git a/.gitmodules b/.gitmodules index 93fac9d1f..20f8ea103 100644 --- a/.gitmodules +++ b/.gitmodules @@ -28,3 +28,6 @@ [submodule "lib/libevent"] path = lib/libevent url = https://github.com/mc-server/libevent.git +[submodule "lib/TCLAP"] + path = lib/TCLAP + url = https://github.com/mc-server/TCLAP.git diff --git a/.travis.yml b/.travis.yml index 2fd3bf991..6dde5b62b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: cpp compiler: clang before_install: - - if [ "$TRAVIS_MCSERVER_BUILD_TYPE" == "COVERAGE" ]; then sudo pip install cpp_coveralls; fi +# - if [ "$TRAVIS_MCSERVER_BUILD_TYPE" == "COVERAGE" ]; then sudo pip install cpp_coveralls; fi # g++4.8 - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test @@ -21,17 +21,17 @@ install: # Build MCServer script: ./CIbuild.sh -after_success: - - ./uploadCoverage.sh +#after_success: +# - ./uploadCoverage.sh env: - TRAVIS_MCSERVER_BUILD_TYPE=RELEASE MCSERVER_PATH=./MCServer - TRAVIS_MCSERVER_BUILD_TYPE=DEBUG MCSERVER_PATH=./MCServer_debug -matrix: - include: - - compiler: gcc - env: TRAVIS_MCSERVER_BUILD_TYPE=COVERAGE MCSERVER_PATH=./MCServer +#matrix: +# include: +# - compiler: gcc +# env: TRAVIS_MCSERVER_BUILD_TYPE=COVERAGE MCSERVER_PATH=./MCServer # Notification Settings notifications: diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 3ca929b08..9cc7f19fc 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -8,6 +8,7 @@ Diusrex Duralex FakeTruth (founder) Howaner +jasperarmstrong keyboard Lapayo Luksor diff --git a/MCServer/Plugins/APIDump/Hooks/OnEntityChangedWorld.lua b/MCServer/Plugins/APIDump/Hooks/OnEntityChangedWorld.lua new file mode 100644 index 000000000..6675fdbe0 --- /dev/null +++ b/MCServer/Plugins/APIDump/Hooks/OnEntityChangedWorld.lua @@ -0,0 +1,28 @@ +return +{ + HOOK_ENTITY_CHANGED_WORLD = + { + CalledWhen = "After a entity has changed the world.", + DefaultFnName = "OnEntityChangedWorld", -- also used as pagename + Desc = [[ + This hook is called after the server has moved the {{cEntity|entity}} to the given world. This is an information-only + callback, the entity is already in the new world.<p> + See also the {{OnEntityChangingWorld|HOOK_ENTITY_CHANGING_WORLD}} hook for a similar hook called before the + entity is moved to the new world. + ]], + Params = + { + { Name = "Entity", Type = "{{cEntity}}", Notes = "The entity that has changed the world" }, + { Name = "World", Type = "{{cWorld}}", Notes = "The world from which the entity has come" }, + }, + Returns = [[ + If the function returns false or no value, the next plugin's callback is called. If the function + returns true, no other callback is called for this event. + ]], + }, -- HOOK_ENTITY_CHANGED_WORLD +} + + + + + diff --git a/MCServer/Plugins/APIDump/Hooks/OnEntityChangingWorld.lua b/MCServer/Plugins/APIDump/Hooks/OnEntityChangingWorld.lua new file mode 100644 index 000000000..521f829c7 --- /dev/null +++ b/MCServer/Plugins/APIDump/Hooks/OnEntityChangingWorld.lua @@ -0,0 +1,29 @@ +return +{ + HOOK_ENTITY_CHANGING_WORLD = + { + CalledWhen = "Before a entity is changing the world.", + DefaultFnName = "OnEntityChangingWorld", -- also used as pagename + Desc = [[ + This hook is called before the server moves the {{cEntity|entity}} to the given world. Plugins may + refuse the changing of the entity to the new world.<p> + See also the {{OnEntityChangedWorld|HOOK_ENTITY_CHANGED_WORLD}} hook for a similar hook is called after the + entity has been moved to the world. + ]], + Params = + { + { Name = "Entity", Type = "{{cEntity}}", Notes = "The entity that wants to change the world" }, + { Name = "World", Type = "{{cWorld}}", Notes = "The world to which the entity wants to change" }, + }, + Returns = [[ + If the function returns false or no value, the next plugin's callback is called. If the function + returns true, no other callback is called for this event and the change of the entity to the world is + cancelled. + ]], + }, -- HOOK_ENTITY_CHANGING_WORLD +} + + + + + diff --git a/MCServer/Plugins/APIDump/main_APIDump.lua b/MCServer/Plugins/APIDump/main_APIDump.lua index 4ca06b974..e841922b6 100644 --- a/MCServer/Plugins/APIDump/main_APIDump.lua +++ b/MCServer/Plugins/APIDump/main_APIDump.lua @@ -971,7 +971,7 @@ end --- Writes a list of undocumented objects into a file local function ListUndocumentedObjects(API, UndocumentedHooks) - f = io.open("API/_undocumented.lua", "w"); + local f = io.open("API/_undocumented.lua", "w"); if (f ~= nil) then f:write("\n-- This is the list of undocumented API objects, automatically generated by APIDump\n\n"); f:write("g_APIDesc =\n{\n\tClasses =\n\t{\n"); diff --git a/MCServer/Plugins/Core b/MCServer/Plugins/Core -Subproject ea0ab964d568630fd4f2b52954186f2851a769e +Subproject 5171b43807ff699a6b239ad4969520730b3748a @@ -36,9 +36,7 @@ Check out the [CONTRIBUTING.md](https://github.com/mc-server/MCServer/blob/maste Other Stuff ----------- -For other stuff, including plugins and discussion, check the [forums](http://forum.mc-server.org) and [Plugin API](http://mc-server.xoft.cz/LuaAPI/). - -Earn bitcoins for commits or donate to reward the MCServer developers: [![tip for next commit](http://tip4commit.com/projects/74.svg)](http://tip4commit.com/projects/74) +For other stuff, including plugins and discussion, check out the [forums](http://forum.mc-server.org) and [Plugin API](http://mc-server.xoft.cz/LuaAPI/). Support Us on Gratipay: [![Support via Gratipay](http://img.shields.io/gittip/cuberite_team.svg)](https://www.gratipay.com/cuberite_team) diff --git a/lib/TCLAP b/lib/TCLAP new file mode 160000 +Subproject 12cee38782897cfe60a1611615c200c45cd99ea diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index ccf812417..08c7e19d7 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -927,6 +927,9 @@ bool cLuaState::CheckParamTable(int a_StartParam, int a_EndParam) VERIFY(lua_getstack(m_LuaState, 0, &entry)); VERIFY(lua_getinfo (m_LuaState, "n", &entry)); AString ErrMsg = Printf("#ferror in function '%s'.", (entry.name != nullptr) ? entry.name : "?"); + + BreakIntoDebugger(m_LuaState); + tolua_error(m_LuaState, ErrMsg.c_str(), &tolua_err); return false; } // for i - Param @@ -1366,6 +1369,7 @@ int cLuaState::ReportFnCallErrors(lua_State * a_LuaState) { LOGWARNING("LUA: %s", lua_tostring(a_LuaState, -1)); LogStackTrace(a_LuaState, 1); + BreakIntoDebugger(a_LuaState); return 1; // We left the error message on the stack as the return value } @@ -1373,6 +1377,28 @@ int cLuaState::ReportFnCallErrors(lua_State * a_LuaState) +int cLuaState::BreakIntoDebugger(lua_State * a_LuaState) +{ + // Call the BreakIntoDebugger function, if available: + lua_getglobal(a_LuaState, "BreakIntoDebugger"); + if (!lua_isfunction(a_LuaState, -1)) + { + LOGD("LUA: BreakIntoDebugger() not found / not a function"); + lua_pop(a_LuaState, 1); + return 1; + } + lua_insert(a_LuaState, -2); // Copy the string that has been passed to us + LOGD("Calling BreakIntoDebugger()..."); + lua_call(a_LuaState, 1, 0); + LOGD("Returned from BreakIntoDebugger()."); + + return 0; +} + + + + + //////////////////////////////////////////////////////////////////////////////// // cLuaState::cRef: diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index b38401fd8..5b4ec3ae4 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -389,6 +389,9 @@ protected: /** Used as the error reporting function for function calls */ static int ReportFnCallErrors(lua_State * a_LuaState); + + /** Tries to break into the MobDebug debugger, if it is installed. */ + static int BreakIntoDebugger(lua_State * a_LuaState); } ; diff --git a/src/Bindings/ManualBindings_World.cpp b/src/Bindings/ManualBindings_World.cpp index 3ee5ca498..5becbba92 100644 --- a/src/Bindings/ManualBindings_World.cpp +++ b/src/Bindings/ManualBindings_World.cpp @@ -530,7 +530,7 @@ static int tolua_cWorld_TryGetHeight(lua_State * tolua_S) // Call the implementation: int Height = 0; bool res = self->TryGetHeight(BlockX, BlockZ, Height); - L.Push(res ? 1 : 0); + L.Push(res); if (res) { L.Push(Height); diff --git a/src/Bindings/Plugin.h b/src/Bindings/Plugin.h index d0c2bcefa..1330bca0d 100644 --- a/src/Bindings/Plugin.h +++ b/src/Bindings/Plugin.h @@ -56,6 +56,8 @@ public: virtual bool OnDisconnect (cClientHandle & a_Client, const AString & a_Reason) = 0; virtual bool OnEntityAddEffect (cEntity & a_Entity, int a_EffectType, int a_EffectDurationTicks, int a_EffectIntensity, double a_DistanceModifier) = 0; virtual bool OnEntityTeleport (cEntity & a_Entity, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition) = 0; + virtual bool OnEntityChangingWorld (cEntity & a_Entity, cWorld & a_World) = 0; + virtual bool OnEntityChangedWorld (cEntity & a_Entity, cWorld & a_World) = 0; virtual bool OnExecuteCommand (cPlayer * a_Player, const AStringVector & a_Split, const AString & a_EntireCommand, cPluginManager::CommandResult & a_Result) = 0; virtual bool OnExploded (cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) = 0; virtual bool OnExploding (cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) = 0; diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp index 76d3557a4..234bf579b 100644 --- a/src/Bindings/PluginLua.cpp +++ b/src/Bindings/PluginLua.cpp @@ -534,6 +534,54 @@ bool cPluginLua::OnEntityAddEffect(cEntity & a_Entity, int a_EffectType, int a_E +bool cPluginLua::OnEntityChangingWorld(cEntity & a_Entity, cWorld & a_World) +{ + cCSLock Lock(m_CriticalSection); + if (!m_LuaState.IsValid()) + { + return false; + } + bool res = false; + cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_ENTITY_CHANGING_WORLD]; + for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr) + { + m_LuaState.Call((int)(**itr), &a_Entity, &a_World, cLuaState::Return, res); + if (res) + { + return true; + } + } + return false; +} + + + + + +bool cPluginLua::OnEntityChangedWorld(cEntity & a_Entity, cWorld & a_World) +{ + cCSLock Lock(m_CriticalSection); + if (!m_LuaState.IsValid()) + { + return false; + } + bool res = false; + cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_ENTITY_CHANGED_WORLD]; + for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr) + { + m_LuaState.Call((int)(**itr), &a_Entity, &a_World, res); + if (res) + { + return true; + } + } + return false; +} + + + + + bool cPluginLua::OnExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split, const AString & a_EntireCommand, cPluginManager::CommandResult & a_Result) { cCSLock Lock(m_CriticalSection); @@ -1884,6 +1932,8 @@ const char * cPluginLua::GetHookFnName(int a_HookType) case cPluginManager::HOOK_DISCONNECT: return "OnDisconnect"; case cPluginManager::HOOK_PLAYER_ANIMATION: return "OnPlayerAnimation"; case cPluginManager::HOOK_ENTITY_ADD_EFFECT: return "OnEntityAddEffect"; + case cPluginManager::HOOK_ENTITY_CHANGING_WORLD: return "OnEntityChangingWorld"; + case cPluginManager::HOOK_ENTITY_CHANGED_WORLD: return "OnEntityChangedWorld"; case cPluginManager::HOOK_ENTITY_TELEPORT: return "OnEntityTeleport"; case cPluginManager::HOOK_EXECUTE_COMMAND: return "OnExecuteCommand"; case cPluginManager::HOOK_HANDSHAKE: return "OnHandshake"; diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index 524c249b0..29f0319ab 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -115,6 +115,8 @@ public: virtual bool OnCraftingNoRecipe (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe) override; virtual bool OnDisconnect (cClientHandle & a_Client, const AString & a_Reason) override; virtual bool OnEntityAddEffect (cEntity & a_Entity, int a_EffectType, int a_EffectDurationTicks, int a_EffectIntensity, double a_DistanceModifier) override; + virtual bool OnEntityChangingWorld (cEntity & a_Entity, cWorld & a_World) override; + virtual bool OnEntityChangedWorld (cEntity & a_Entity, cWorld & a_World) override; virtual bool OnExecuteCommand (cPlayer * a_Player, const AStringVector & a_Split, const AString & a_EntireCommand, cPluginManager::CommandResult & a_Result) override; virtual bool OnExploded (cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) override; virtual bool OnExploding (cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) override; diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp index db2493955..4dc3e20d3 100644 --- a/src/Bindings/PluginManager.cpp +++ b/src/Bindings/PluginManager.cpp @@ -118,7 +118,7 @@ void cPluginManager::ReloadPluginsNow(void) -void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni) +void cPluginManager::ReloadPluginsNow(cSettingsRepositoryInterface & a_Settings) { LOG("-- Loading Plugins --"); @@ -130,7 +130,7 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni) RefreshPluginList(); // Load the plugins: - AStringVector ToLoad = GetFoldersToLoad(a_SettingsIni); + AStringVector ToLoad = GetFoldersToLoad(a_Settings); for (auto & pluginFolder: ToLoad) { LoadPlugin(pluginFolder); @@ -157,16 +157,16 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni) -void cPluginManager::InsertDefaultPlugins(cIniFile & a_SettingsIni) +void cPluginManager::InsertDefaultPlugins(cSettingsRepositoryInterface & a_Settings) { - a_SettingsIni.AddKeyName("Plugins"); - a_SettingsIni.AddKeyComment("Plugins", " Plugin=Debuggers"); - a_SettingsIni.AddKeyComment("Plugins", " Plugin=HookNotify"); - a_SettingsIni.AddKeyComment("Plugins", " Plugin=ChunkWorx"); - a_SettingsIni.AddKeyComment("Plugins", " Plugin=APIDump"); - a_SettingsIni.AddValue("Plugins", "Plugin", "Core"); - a_SettingsIni.AddValue("Plugins", "Plugin", "TransAPI"); - a_SettingsIni.AddValue("Plugins", "Plugin", "ChatLog"); + a_Settings.AddKeyName("Plugins"); + a_Settings.AddKeyComment("Plugins", " Plugin=Debuggers"); + a_Settings.AddKeyComment("Plugins", " Plugin=HookNotify"); + a_Settings.AddKeyComment("Plugins", " Plugin=ChunkWorx"); + a_Settings.AddKeyComment("Plugins", " Plugin=APIDump"); + a_Settings.AddValue("Plugins", "Plugin", "Core"); + a_Settings.AddValue("Plugins", "Plugin", "TransAPI"); + a_Settings.AddValue("Plugins", "Plugin", "ChatLog"); } @@ -525,6 +525,42 @@ bool cPluginManager::CallHookEntityTeleport(cEntity & a_Entity, const Vector3d & +bool cPluginManager::CallHookEntityChangingWorld(cEntity & a_Entity, cWorld & a_World) +{ + FIND_HOOK(HOOK_ENTITY_CHANGING_WORLD); + VERIFY_HOOK; + + for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr) + { + if ((*itr)->OnEntityChangingWorld(a_Entity, a_World)) + { + return true; + } + } + return false; +} + + + + +bool cPluginManager::CallHookEntityChangedWorld(cEntity & a_Entity, cWorld & a_World) +{ + FIND_HOOK(HOOK_ENTITY_CHANGED_WORLD); + VERIFY_HOOK; + + for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr) + { + if ((*itr)->OnEntityChangedWorld(a_Entity, a_World)) + { + return true; + } + } + return false; +} + + + + bool cPluginManager::CallHookExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split, const AString & a_EntireCommand, CommandResult & a_Result) { FIND_HOOK(HOOK_EXECUTE_COMMAND); @@ -1896,25 +1932,23 @@ size_t cPluginManager::GetNumLoadedPlugins(void) const -AStringVector cPluginManager::GetFoldersToLoad(cIniFile & a_SettingsIni) +AStringVector cPluginManager::GetFoldersToLoad(cSettingsRepositoryInterface & a_Settings) { // Check if the Plugins section exists. - int KeyNum = a_SettingsIni.FindKey("Plugins"); - if (KeyNum == -1) + if (a_Settings.KeyExists("Plugins")) { - InsertDefaultPlugins(a_SettingsIni); - KeyNum = a_SettingsIni.FindKey("Plugins"); + InsertDefaultPlugins(a_Settings); } // Get the list of plugins to load: AStringVector res; - int NumPlugins = a_SettingsIni.GetNumValues(KeyNum); - for (int i = 0; i < NumPlugins; i++) + auto Values = a_Settings.GetValues("Plugins"); + for (auto NameValue : Values) { - AString ValueName = a_SettingsIni.GetValueName(KeyNum, i); + AString ValueName = NameValue.first; if (ValueName.compare("Plugin") == 0) { - AString PluginFile = a_SettingsIni.GetValue(KeyNum, i); + AString PluginFile = NameValue.second; if (!PluginFile.empty()) { res.push_back(PluginFile); diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h index d8c886b62..6bcef87bf 100644 --- a/src/Bindings/PluginManager.h +++ b/src/Bindings/PluginManager.h @@ -24,6 +24,7 @@ class cPlayer; class cPlugin; class cProjectileEntity; class cWorld; +class cSettingsRepositoryInterface; struct TakeDamageInfo; typedef SharedPtr<cPlugin> cPluginPtr; @@ -85,6 +86,8 @@ public: HOOK_DISCONNECT, HOOK_PLAYER_ANIMATION, HOOK_ENTITY_ADD_EFFECT, + HOOK_ENTITY_CHANGING_WORLD, + HOOK_ENTITY_CHANGED_WORLD, HOOK_EXECUTE_COMMAND, HOOK_EXPLODED, HOOK_EXPLODING, @@ -200,6 +203,8 @@ public: bool CallHookDisconnect (cClientHandle & a_Client, const AString & a_Reason); bool CallHookEntityAddEffect (cEntity & a_Entity, int a_EffectType, int a_EffectDurationTicks, int a_EffectIntensity, double a_DistanceModifier); bool CallHookEntityTeleport (cEntity & a_Entity, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition); + bool CallHookEntityChangingWorld (cEntity & a_Entity, cWorld & a_World); + bool CallHookEntityChangedWorld (cEntity & a_Entity, cWorld & a_World); bool CallHookExecuteCommand (cPlayer * a_Player, const AStringVector & a_Split, const AString & a_EntireCommand, CommandResult & a_Result); // If a_Player == nullptr, it is a console cmd bool CallHookExploded (cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData); bool CallHookExploding (cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData); @@ -364,20 +369,20 @@ private: /** Reloads all plugins, defaulting to settings.ini for settings location */ void ReloadPluginsNow(void); - /** Reloads all plugins with a cIniFile object expected to be initialised to settings.ini */ - void ReloadPluginsNow(cIniFile & a_SettingsIni); + /** Reloads all plugins with a settings repo expected to be initialised to settings.ini */ + void ReloadPluginsNow(cSettingsRepositoryInterface & a_Settings); /** Unloads all plugins */ void UnloadPluginsNow(void); - /** Handles writing default plugins if 'Plugins' key not found using a cIniFile object expected to be intialised to settings.ini */ - void InsertDefaultPlugins(cIniFile & a_SettingsIni); + /** Handles writing default plugins if 'Plugins' key not found using a settings repo expected to be intialised to settings.ini */ + void InsertDefaultPlugins(cSettingsRepositoryInterface & a_Settings); /** Tries to match a_Command to the internal table of commands, if a match is found, the corresponding plugin is called. Returns crExecuted if the command is executed. */ CommandResult HandleCommand(cPlayer & a_Player, const AString & a_Command, bool a_ShouldCheckPermissions); /** Returns the folders that are specified in the settings ini to load plugins from. */ - AStringVector GetFoldersToLoad(cIniFile & a_SettingsIni); + AStringVector GetFoldersToLoad(cSettingsRepositoryInterface & a_Settings); } ; // tolua_export diff --git a/src/Blocks/BlockBed.cpp b/src/Blocks/BlockBed.cpp index e56f4bfe0..dfa392d9b 100644 --- a/src/Blocks/BlockBed.cpp +++ b/src/Blocks/BlockBed.cpp @@ -14,7 +14,7 @@ void cBlockBedHandler::OnDestroyed(cChunkInterface & a_ChunkInterface, cWorldInt NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); Vector3i ThisPos( a_BlockX, a_BlockY, a_BlockZ); - Vector3i Direction = MetaDataToDirection( OldMeta & 0x7); + Vector3i Direction = MetaDataToDirection( OldMeta & 0x3); if (OldMeta & 0x8) { // Was pillow @@ -111,7 +111,7 @@ void cBlockBedHandler::OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface // Is foot end VERIFY((Meta & 0x4) != 0x4); // Occupied flag should never be set, else our compilator (intended) is broken - PillowDirection = MetaDataToDirection(Meta & 0x7); + PillowDirection = MetaDataToDirection(Meta & 0x3); if (a_ChunkInterface.GetBlock(a_BlockX + PillowDirection.x, a_BlockY, a_BlockZ + PillowDirection.z) == E_BLOCK_BED) // Must always use pillow location for sleeping { a_WorldInterface.GetBroadcastManager().BroadcastUseBed(*a_Player, a_BlockX + PillowDirection.x, a_BlockY, a_BlockZ + PillowDirection.z); diff --git a/src/Blocks/BlockLeaves.h b/src/Blocks/BlockLeaves.h index bd9a7414e..4d4610fd8 100644 --- a/src/Blocks/BlockLeaves.h +++ b/src/Blocks/BlockLeaves.h @@ -40,29 +40,41 @@ public: { cFastRandom rand; - // Old leaves - 3 bits contain display; new leaves - 1st bit, shifted left two for saplings to understand - if (rand.NextInt(6) == 0) + // There is a chance to drop a sapling that varies depending on the type of leaf broken. + // TODO: Take into account fortune for sapling drops. + int chance; + if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_JUNGLE)) + { + // Jungle leaves have a 2.5% chance of dropping a sapling. + chance = rand.NextInt(40); + } + else + { + // Other leaves have a 5% chance of dropping a sapling. + chance = rand.NextInt(20); + } + if (chance == 0) { a_Pickups.push_back( cItem( E_BLOCK_SAPLING, 1, - (m_BlockType == E_BLOCK_LEAVES) ? (a_BlockMeta & 0x03) : (2 << (a_BlockMeta & 0x01)) + (m_BlockType == E_BLOCK_LEAVES) ? (a_BlockMeta & 0x03) : (4 + (a_BlockMeta & 0x01)) ) ); } - - // 1 % chance of dropping an apple, if the leaves' type is Apple Leaves + + // 0.5 % chance of dropping an apple, if the leaves' type is Apple Leaves if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_APPLE)) { - if (rand.NextInt(101) == 0) + if (rand.NextInt(200) == 0) { a_Pickups.push_back(cItem(E_ITEM_RED_APPLE, 1, 0)); } } } - - + + virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, int a_BlockX, int a_BlockY, int a_BlockZ) override { NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); diff --git a/src/Blocks/BlockTorch.h b/src/Blocks/BlockTorch.h index d63df94cf..3abc572f3 100644 --- a/src/Blocks/BlockTorch.h +++ b/src/Blocks/BlockTorch.h @@ -103,6 +103,11 @@ public: case E_BLOCK_STAINED_GLASS: case E_BLOCK_FENCE: case E_BLOCK_NETHER_BRICK_FENCE: + case E_BLOCK_SPRUCE_FENCE: + case E_BLOCK_BIRCH_FENCE: + case E_BLOCK_JUNGLE_FENCE: + case E_BLOCK_DARK_OAK_FENCE: + case E_BLOCK_ACACIA_FENCE: case E_BLOCK_COBBLESTONE_WALL: { // Torches can only be placed on top of these blocks diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2e367bcf5..25de24c91 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,11 +48,13 @@ SET (SRCS Logger.cpp Map.cpp MapManager.cpp + MemorySettingsRepository.cpp MobCensus.cpp MobFamilyCollecter.cpp MobProximityCounter.cpp MobSpawner.cpp MonsterConfig.cpp + OverridesSettingsRepository.cpp ProbabDistrib.cpp RankManager.cpp RCONServer.cpp @@ -116,11 +118,13 @@ SET (HDRS Map.h MapManager.h Matrix4.h + MemorySettingsRepository.h MobCensus.h MobFamilyCollecter.h MobProximityCounter.h MobSpawner.h MonsterConfig.h + OverridesSettingsRepository.h ProbabDistrib.h RankManager.h RCONServer.h @@ -128,6 +132,7 @@ SET (HDRS Scoreboard.h Server.h SetChunkData.h + SettingsRepositoryInterface.h Statistics.h StringCompression.h StringUtils.h @@ -142,6 +147,7 @@ SET (HDRS include_directories(".") include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/../lib/sqlite") include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/../lib/SQLiteCpp/include") +include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/TCLAP/include") configure_file("BuildInfo.h.cmake" "${CMAKE_CURRENT_SOURCE_DIR}/BuildInfo.h") diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 4a909a1fd..dca44488b 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1403,14 +1403,25 @@ bool cEntity::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn) return false; } + // Ask the plugins if the entity is allowed to changing the world + if (cRoot::Get()->GetPluginManager()->CallHookEntityChangingWorld(*this, *a_World)) + { + // A Plugin doesn't allow the entity to changing the world + return false; + } + // Remove all links to the old world SetWorldTravellingFrom(GetWorld()); // cChunk::Tick() handles entity removal GetWorld()->BroadcastDestroyEntity(*this); // Queue add to new world a_World->AddEntity(this); + cWorld * OldWorld = cRoot::Get()->GetWorld(GetWorld()->GetName()); // Required for the hook HOOK_ENTITY_CHANGED_WORLD SetWorld(a_World); + // Entity changed the world, call the hook + cRoot::Get()->GetPluginManager()->CallHookEntityChangedWorld(*this, *OldWorld); + return true; } diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 1dab952c5..b5a48d9bf 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -128,6 +128,13 @@ cPlayer::cPlayer(cClientHandlePtr a_Client, const AString & a_PlayerName) : m_IsFlying = true; } } + + if (m_GameMode == gmSpectator) // If player is reconnecting to the server in spectator mode + { + m_CanFly = true; + m_IsFlying = true; + m_bVisible = false; + } cRoot::Get()->GetServer()->PlayerCreated(this); } @@ -1615,6 +1622,13 @@ bool cPlayer::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn) return false; } + // Ask the plugins if the player is allowed to changing the world + if (cRoot::Get()->GetPluginManager()->CallHookEntityChangingWorld(*this, *a_World)) + { + // A Plugin doesn't allow the player to changing the world + return false; + } + // Send the respawn packet: if (a_ShouldSendRespawn && (m_ClientHandle != nullptr)) { @@ -1630,6 +1644,7 @@ bool cPlayer::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn) // Queue adding player to the new world, including all the necessary adjustments to the object a_World->AddPlayer(this); + cWorld * OldWorld = cRoot::Get()->GetWorld(GetWorld()->GetName()); // Required for the hook HOOK_ENTITY_CHANGED_WORLD SetWorld(a_World); // Chunks may be streamed before cWorld::AddPlayer() sets the world to the new value // Update the view distance. @@ -1644,6 +1659,9 @@ bool cPlayer::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn) // Broadcast the player into the new world. a_World->BroadcastSpawnEntity(*this); + // Player changed the world, call the hook + cRoot::Get()->GetPluginManager()->CallHookEntityChangedWorld(*this, *OldWorld); + return true; } diff --git a/src/FastRandom.cpp b/src/FastRandom.cpp index 737b13535..c1716f026 100644 --- a/src/FastRandom.cpp +++ b/src/FastRandom.cpp @@ -6,12 +6,28 @@ #include "Globals.h" #include "FastRandom.h" +#include <random> + #ifdef _WIN32 - #define thread_local __declspec(thread) + #define thread_local static __declspec(thread) +#elif defined __APPLE__ + #define thread_local static __thread #endif -thread_local unsigned int m_Counter = 0; - +static unsigned int GetRandomSeed() +{ + thread_local bool SeedCounterInitialized = 0; + thread_local unsigned int SeedCounter = 0; + + if (!SeedCounterInitialized) + { + std::random_device rd; + std::uniform_int_distribution<unsigned int> dist; + SeedCounter = dist(rd); + SeedCounterInitialized = true; + } + return ++SeedCounter; +} @@ -92,7 +108,7 @@ public: cFastRandom::cFastRandom(void) : - m_LinearRand(m_Counter++) + m_LinearRand(GetRandomSeed()) { } @@ -136,7 +152,7 @@ int cFastRandom::GenerateRandomInteger(int a_Begin, int a_End) // MTRand: MTRand::MTRand() : - m_MersenneRand(m_Counter++) + m_MersenneRand(GetRandomSeed()) { } diff --git a/src/Generating/FinishGen.cpp b/src/Generating/FinishGen.cpp index 00b34bddd..69ae59c18 100644 --- a/src/Generating/FinishGen.cpp +++ b/src/Generating/FinishGen.cpp @@ -1274,6 +1274,7 @@ bool cFinishGenPassiveMobs::TrySpawnAnimals(cChunkDesc & a_ChunkDesc, int a_RelX double AnimalZ = static_cast<double>(a_ChunkDesc.GetChunkZ() * cChunkDef::Width + a_RelZ + 0.5); cMonster * NewMob = cMonster::NewMonsterFromType(AnimalToSpawn); + NewMob->SetHealth(NewMob->GetMaxHealth()); NewMob->SetPosition(AnimalX, AnimalY, AnimalZ); a_ChunkDesc.GetEntities().push_back(NewMob); LOGD("Spawning %s #%i at {%.02f, %.02f, %.02f}", NewMob->GetClass(), NewMob->GetUniqueID(), AnimalX, AnimalY, AnimalZ); diff --git a/src/Globals.h b/src/Globals.h index 1f354ae77..27d944fcc 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -433,10 +433,14 @@ typename std::enable_if<std::is_arithmetic<T>::value, C>::type CeilC(T a_Value) //temporary replacement for std::make_unique until we get c++14 -template <class T, class... Args> -std::unique_ptr<T> make_unique(Args&&... args) + +namespace cpp14 { - return std::unique_ptr<T>(new T(args...)); + template <class T, class... Args> + std::unique_ptr<T> make_unique(Args&&... args) + { + return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); + } } // a tick is 50 ms diff --git a/src/IniFile.cpp b/src/IniFile.cpp index 99c0a5f0f..cd98cce57 100644 --- a/src/IniFile.cpp +++ b/src/IniFile.cpp @@ -49,6 +49,9 @@ cIniFile::cIniFile(void) : 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; @@ -56,7 +59,8 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect) 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()) { @@ -650,7 +654,7 @@ void cIniFile::Clear(void) -bool cIniFile::HasValue(const AString & a_KeyName, const AString & a_ValueName) +bool cIniFile::HasValue(const AString & a_KeyName, const AString & a_ValueName) const { // Find the key: int keyID = FindKey(a_KeyName); @@ -889,8 +893,36 @@ void cIniFile::RemoveBom(AString & a_line) const +bool cIniFile::KeyExists(AString a_keyname) const +{ + return FindKey(a_keyname) != noID; +} + + + + + +std::vector<std::pair<AString, AString>> cIniFile::GetValues(AString a_keyName) +{ + std::vector<std::pair<AString, AString>> ret; + int keyID = FindKey(a_keyName); + if (keyID == noID) + { + return ret; + } + for (size_t valueID = 0; valueID < keys[keyID].names.size(); ++valueID) + { + ret.emplace_back(keys[keyID].names[valueID], keys[keyID].values[valueID]); + } + return ret; +} + + + + + AStringVector ReadUpgradeIniPorts( - cIniFile & a_IniFile, + cSettingsRepositoryInterface & a_Settings, const AString & a_KeyName, const AString & a_PortsValueName, const AString & a_OldIPv4ValueName, @@ -899,23 +931,34 @@ AStringVector ReadUpgradeIniPorts( ) { // Read the regular value, but don't use the default (in order to detect missing value for upgrade): - AStringVector Ports = StringSplitAndTrim(a_IniFile.GetValue(a_KeyName, a_PortsValueName), ";,"); + + AStringVector Ports; + + for (auto pair : a_Settings.GetValues(a_KeyName)) + { + if (pair.first != a_PortsValueName) + { + continue; + } + AStringVector temp = StringSplitAndTrim(pair.second, ";,"); + Ports.insert(Ports.end(), temp.begin(), temp.end()); + } if (Ports.empty()) { // Historically there were two separate entries for IPv4 and IPv6, merge them and migrate: - AString Ports4 = a_IniFile.GetValue(a_KeyName, a_OldIPv4ValueName, a_DefaultValue); - AString Ports6 = a_IniFile.GetValue(a_KeyName, a_OldIPv6ValueName); + AString Ports4 = a_Settings.GetValue(a_KeyName, a_OldIPv4ValueName, a_DefaultValue); + AString Ports6 = a_Settings.GetValue(a_KeyName, a_OldIPv6ValueName); Ports = MergeStringVectors(StringSplitAndTrim(Ports4, ";,"), StringSplitAndTrim(Ports6, ";,")); - a_IniFile.DeleteValue(a_KeyName, a_OldIPv4ValueName); - a_IniFile.DeleteValue(a_KeyName, a_OldIPv6ValueName); + a_Settings.DeleteValue(a_KeyName, a_OldIPv4ValueName); + a_Settings.DeleteValue(a_KeyName, a_OldIPv6ValueName); // If those weren't present or were empty, use the default:" if (Ports.empty()) { Ports = StringSplitAndTrim(a_DefaultValue, ";,"); } - a_IniFile.SetValue(a_KeyName, a_PortsValueName, StringsConcat(Ports, ',')); + a_Settings.SetValue(a_KeyName, a_PortsValueName, StringsConcat(Ports, ',')); } return Ports; @@ -923,4 +966,3 @@ AStringVector ReadUpgradeIniPorts( - diff --git a/src/IniFile.h b/src/IniFile.h index 71fea3a00..861be3800 100644 --- a/src/IniFile.h +++ b/src/IniFile.h @@ -18,7 +18,7 @@ #pragma once - +#include "SettingsRepositoryInterface.h" #define MAX_KEYNAME 128 #define MAX_VALUENAME 128 @@ -30,10 +30,12 @@ // tolua_begin -class cIniFile +class cIniFile : public cSettingsRepositoryInterface { private: bool m_IsCaseInsensitive; + + AString m_Filename; struct key { @@ -53,15 +55,19 @@ private: void RemoveBom(AString & a_line) const; public: - - enum errors - { - noID = -1, - }; /// Creates a new instance with no data cIniFile(void); +// tolua_end + virtual ~cIniFile() = default; + + virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override; + + virtual bool KeyExists(const AString a_keyName) const override; + +// tolua_begin + // Sets whether or not keynames and valuenames should be case sensitive. // The default is case insensitive. void CaseSensitive (void) { m_IsCaseInsensitive = false; } @@ -77,11 +83,13 @@ public: /// Writes data stored in class to the specified ini file bool WriteFile(const AString & a_FileName) const; + virtual bool Flush() override { return WriteFile(m_Filename); } + /// Deletes all stored ini data (but doesn't touch the file) void Clear(void); /** Returns true iff the specified value exists. */ - bool HasValue(const AString & a_KeyName, const AString & a_ValueName); + bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const; /// Returns index of specified key, or noID if not found int FindKey(const AString & keyname) const; @@ -222,7 +230,7 @@ Reads the list of ports from a_PortsValueName. If that value doesn't exist or is in a_OldIPv4ValueName and a_OldIPv6ValueName; in this case the old values are removed from the INI file. If there is none of the three values or they are all empty, the default is used and stored in the Ports value. */ AStringVector ReadUpgradeIniPorts( - cIniFile & a_IniFile, + cSettingsRepositoryInterface & a_Settings, const AString & a_KeyName, const AString & a_PortsValueName, const AString & a_OldIPv4ValueName, diff --git a/src/MemorySettingsRepository.cpp b/src/MemorySettingsRepository.cpp new file mode 100644 index 000000000..21b4c769c --- /dev/null +++ b/src/MemorySettingsRepository.cpp @@ -0,0 +1,312 @@ + +#include "Globals.h" + +#include "MemorySettingsRepository.h" + + + + +bool cMemorySettingsRepository::KeyExists(const AString keyname) const +{ + return m_Map.count(keyname) != 0; +} + + + + + +bool cMemorySettingsRepository::HasValue(const AString & a_KeyName, const AString & a_ValueName) const +{ + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + return false; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + return false; + } + return true; +} + + + + +int cMemorySettingsRepository::AddKeyName(const AString & a_keyname) +{ + m_Map.emplace(a_keyname, std::unordered_multimap<AString, sValue>{}); + return 0; +} + + + + + +bool cMemorySettingsRepository::AddKeyComment(const AString & keyname, const AString & comment) +{ + return false; +} + + + + + +AString cMemorySettingsRepository::GetKeyComment(const AString & keyname, const int commentID) const +{ + return ""; +} + + + + + +bool cMemorySettingsRepository::DeleteKeyComment(const AString & keyname, const int commentID) +{ + return false; +} + + + + + + +void cMemorySettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) +{ + if (m_Writable) + { + m_Map[a_KeyName].emplace(a_ValueName, sValue(a_Value)); + } +} + + + + +void cMemorySettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, Int64 a_Value) +{ + if (m_Writable) + { + m_Map[a_KeyName].emplace(a_ValueName, sValue(a_Value)); + } +} + + + + + +void cMemorySettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, bool a_Value) +{ + if (m_Writable) + { + m_Map[a_KeyName].emplace(a_ValueName, sValue(a_Value)); + } +} + + + + + +std::vector<std::pair<AString, AString>> cMemorySettingsRepository::GetValues(AString a_keyName) +{ + std::vector<std::pair<AString, AString>> ret; + for (auto pair : m_Map[a_keyName]) + { + ret.emplace_back(pair.first, pair.second.getStringValue()); + } + return ret; +} + + + + + +AString cMemorySettingsRepository::GetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & defValue) const +{ + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + return defValue; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + return defValue; + } + return iter->second.getStringValue(); +} + + + + + +AString cMemorySettingsRepository::GetValueSet (const AString & a_KeyName, const AString & a_ValueName, const AString & defValue) +{ + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + AddValue(a_KeyName, a_ValueName, defValue); + return defValue; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + AddValue(a_KeyName, a_ValueName, defValue); + return defValue; + } + return iter->second.getStringValue(); +} + + + + + +int cMemorySettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const int defValue) +{ + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + AddValue(a_KeyName, a_ValueName, static_cast<Int64>(defValue)); + return defValue; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + AddValue(a_KeyName, a_ValueName, static_cast<Int64>(defValue)); + return defValue; + } + return static_cast<int>(iter->second.getIntValue()); +} + + + + + +Int64 cMemorySettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const Int64 defValue) +{ + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + AddValue(a_KeyName, a_ValueName, defValue); + return defValue; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + AddValue(a_KeyName, a_ValueName, defValue); + return defValue; + } + return iter->second.getIntValue(); +} + + + + +bool cMemorySettingsRepository::GetValueSetB(const AString & a_KeyName, const AString & a_ValueName, const bool defValue) +{ + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + AddValue(a_KeyName, a_ValueName, defValue); + return defValue; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + AddValue(a_KeyName, a_ValueName, defValue); + return defValue; + } + return iter->second.getBoolValue(); +} + + + + + +bool cMemorySettingsRepository::SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists) +{ + if (!m_Writable) + { + return false; + } + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + if (a_CreateIfNotExists) + { + AddValue(a_KeyName, a_ValueName, a_Value); + } + return a_CreateIfNotExists; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + if (a_CreateIfNotExists) + { + AddValue(a_KeyName, a_ValueName, a_Value); + } + return a_CreateIfNotExists; + } + iter->second = sValue(a_Value); + return true; +} + + + + +bool cMemorySettingsRepository::SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists) +{ + if (!m_Writable) + { + return false; + } + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + if (a_CreateIfNotExists) + { + AddValue(a_KeyName, a_ValueName, static_cast<Int64>(a_Value)); + } + return a_CreateIfNotExists; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + if (a_CreateIfNotExists) + { + AddValue(a_KeyName, a_ValueName, static_cast<Int64>(a_Value)); + } + return a_CreateIfNotExists; + } + iter->second = sValue(static_cast<Int64>(a_Value)); + return true; +} + + + + + +bool cMemorySettingsRepository::DeleteValue(const AString & a_KeyName, const AString & a_ValueName) +{ + if (!m_Writable) + { + return false; + } + auto outerIter = m_Map.find(a_KeyName); + if (outerIter == m_Map.end()) + { + return false; + } + auto iter = outerIter->second.find(a_ValueName); + if (iter == outerIter->second.end()) + { + return false; + } + outerIter->second.erase(iter); + return true; +} + +bool cMemorySettingsRepository::Flush() +{ + return true; +} + diff --git a/src/MemorySettingsRepository.h b/src/MemorySettingsRepository.h new file mode 100644 index 000000000..335bc4513 --- /dev/null +++ b/src/MemorySettingsRepository.h @@ -0,0 +1,80 @@ + +#pragma once + +#include "SettingsRepositoryInterface.h" + +#include <unordered_map> + +class cMemorySettingsRepository : public cSettingsRepositoryInterface +{ +public: + + virtual bool KeyExists(const AString keyname) const override; + + virtual bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const override; + + virtual int AddKeyName(const AString & keyname) override; + + virtual bool AddKeyComment(const AString & keyname, const AString & comment) override; + + virtual AString GetKeyComment(const AString & keyname, const int commentID) const override; + + virtual bool DeleteKeyComment(const AString & keyname, const int commentID) override; + + virtual void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) override; + void AddValue (const AString & a_KeyName, const AString & a_ValueName, const Int64 a_Value); + void AddValue (const AString & a_KeyName, const AString & a_ValueName, const bool a_Value); + + virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override; + + virtual AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const override; + + + virtual AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") override; + virtual int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) override; + virtual Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) override; + virtual bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) override; + + virtual bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) override; + virtual bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) override; + + virtual bool DeleteValue(const AString & keyname, const AString & valuename) override; + + virtual bool Flush() override; + + void SetReadOnly() + { + m_Writable = false; + } + +private: + + bool m_Writable = true; + + struct sValue + { + sValue(AString value) : m_Type(eType::String), m_stringValue (value) {} + sValue(Int64 value) : m_Type(eType::Int64), m_intValue(value) {} + sValue(bool value) : m_Type(eType::Bool), m_boolValue(value) {} + + AString getStringValue() const { ASSERT(m_Type == eType::String); return m_stringValue; } + Int64 getIntValue() const { ASSERT(m_Type == eType::Int64); return m_intValue; } + bool getBoolValue() const { ASSERT(m_Type == eType::Bool); return m_boolValue; } + private: + enum class eType + { + String, + Int64, + Bool + } m_Type; + AString m_stringValue; + union + { + Int64 m_intValue; + bool m_boolValue; + }; + }; + + std::unordered_map<AString, std::unordered_multimap<AString, sValue>> m_Map{}; +}; + diff --git a/src/Mobs/AggressiveMonster.cpp b/src/Mobs/AggressiveMonster.cpp index d0fb79f6d..648599999 100644 --- a/src/Mobs/AggressiveMonster.cpp +++ b/src/Mobs/AggressiveMonster.cpp @@ -36,7 +36,6 @@ void cAggressiveMonster::InStateChasing(std::chrono::milliseconds a_Dt) return; } } - MoveToPosition(m_Target->GetPosition()); } } @@ -77,9 +76,11 @@ void cAggressiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } cTracer LineOfSight(GetWorld()); - Vector3d AttackDirection(m_Target->GetPosition() - GetPosition()); + Vector3d MyHeadPosition = GetPosition() + Vector3d(0, GetHeight(), 0); + Vector3d AttackDirection(m_Target->GetPosition() + Vector3d(0, m_Target->GetHeight(), 0) - MyHeadPosition); + - if (ReachedFinalDestination() && !LineOfSight.Trace(GetPosition(), AttackDirection, (int)AttackDirection.Length())) + if (ReachedFinalDestination() && !LineOfSight.Trace(MyHeadPosition, AttackDirection, static_cast<int>(AttackDirection.Length()))) { // Attack if reached destination, target isn't null, and have a clear line of sight to target (so won't attack through walls) Attack(a_Dt); diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index f3f8c6b24..f5d961096 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -89,7 +89,7 @@ cMonster::cMonster(const AString & a_ConfigName, eMonsterType a_MobType, const A , m_SoundDeath(a_SoundDeath) , m_AttackRate(3) , m_AttackDamage(1) - , m_AttackRange(2) + , m_AttackRange(1) , m_AttackInterval(0) , m_SightDistance(25) , m_DropChanceWeapon(0.085f) @@ -147,10 +147,15 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) (Recalculate lots when close, calculate rarely when far) */ if ( ((GetPosition() - m_PathFinderDestination).Length() < 0.25) || - ((m_TicksSinceLastPathReset > 10) && (m_TicksSinceLastPathReset > (0.15 * (m_FinalDestination - GetPosition()).SqrLength()))) + ((m_TicksSinceLastPathReset > 10) && (m_TicksSinceLastPathReset > (0.4 * (m_FinalDestination - GetPosition()).SqrLength()))) ) { - ResetPathFinding(); + /* Re-calculating is expensive when there's no path to target, and it results in mobs freezing very often as a result of always recalculating. + This is a workaround till we get better path recalculation. */ + if (!m_NoPathToTarget) + { + ResetPathFinding(); + } } } @@ -161,15 +166,25 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) StopMovingToPosition(); // Invalid chunks, probably world is loading or something, cancel movement. return false; } + m_NoPathToTarget = false; + m_NoMoreWayPoints = false; m_PathFinderDestination = m_FinalDestination; m_Path = new cPath(a_Chunk, GetPosition().Floor(), m_PathFinderDestination.Floor(), 20); } switch (m_Path->Step(a_Chunk)) { + case ePathFinderStatus::NEARBY_FOUND: + { + m_NoPathToTarget = true; + m_PathFinderDestination = m_Path->AcceptNearbyPath(); + break; + } + case ePathFinderStatus::PATH_NOT_FOUND: { - StopMovingToPosition(); // Give up pathfinding to that destination. + ResetPathFinding(); // Try to calculate a path again. + // Note that the next time may succeed, e.g. if a player breaks a barrier. break; } case ePathFinderStatus::CALCULATING: @@ -179,15 +194,22 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) } case ePathFinderStatus::PATH_FOUND: { - if (--m_GiveUpCounter == 0) + if (m_NoMoreWayPoints || (--m_GiveUpCounter == 0)) { ResetPathFinding(); // Try to calculate a path again. return false; } - else if (!m_Path->IsLastPoint() && (m_Path->IsFirstPoint() || ReachedNextWaypoint())) // Have we arrived at the next cell, as denoted by m_NextWayPointPosition? + else if (!m_Path->IsLastPoint()) // Have we arrived at the next cell, as denoted by m_NextWayPointPosition? + { + if ((m_Path->IsFirstPoint() || ReachedNextWaypoint())) + { + m_NextWayPointPosition = Vector3d(0.5, 0, 0.5) + m_Path->GetNextPoint(); + m_GiveUpCounter = 40; // Give up after 40 ticks (2 seconds) if failed to reach m_NextWayPointPosition. + } + } + else { - m_NextWayPointPosition = Vector3d(0.5, 0, 0.5) + m_Path->GetNextPoint(); - m_GiveUpCounter = 40; // Give up after 40 ticks (2 seconds) if failed to reach m_NextWayPointPosition. + m_NoMoreWayPoints = true; } return true; } @@ -269,21 +291,59 @@ bool cMonster::EnsureProperDestination(cChunk & a_Chunk) { return false; } - + int RelX = FloorC(m_FinalDestination.x) - Chunk->GetPosX() * cChunkDef::Width; int RelZ = FloorC(m_FinalDestination.z) - Chunk->GetPosZ() * cChunkDef::Width; - // If destination in the air, go down to the lowest air block. - while (m_FinalDestination.y > 0) + // If destination in the air, first try to go 1 block north, or east, or west. + // This fixes the player leaning issue. + // If that failed, we instead go down to the lowest air block. + Chunk->GetBlockTypeMeta(RelX, FloorC(m_FinalDestination.y) - 1, RelZ, BlockType, BlockMeta); + if (!cBlockInfo::IsSolid(BlockType)) { - Chunk->GetBlockTypeMeta(RelX, FloorC(m_FinalDestination.y) - 1, RelZ, BlockType, BlockMeta); - if (cBlockInfo::IsSolid(BlockType)) + bool InTheAir = true; + int x, z; + for (z = -1; z <= 1; ++z) { - break; + for (x = -1; x <= 1; ++x) + { + if ((x==0) && (z==0)) + { + continue; + } + Chunk = a_Chunk.GetNeighborChunk(FloorC(m_FinalDestination.x+x), FloorC(m_FinalDestination.z+z)); + if ((Chunk == nullptr) || !Chunk->IsValid()) + { + return false; + } + RelX = FloorC(m_FinalDestination.x+x) - Chunk->GetPosX() * cChunkDef::Width; + RelZ = FloorC(m_FinalDestination.z+z) - Chunk->GetPosZ() * cChunkDef::Width; + Chunk->GetBlockTypeMeta(RelX, FloorC(m_FinalDestination.y) - 1, RelZ, BlockType, BlockMeta); + if (cBlockInfo::IsSolid(BlockType)) + { + m_FinalDestination.x += x; + m_FinalDestination.z += z; + InTheAir = false; + goto breakBothLoops; + } + } } - m_FinalDestination.y -= 1; - } + breakBothLoops: + // Go down to the lowest air block. + if (InTheAir) + { + while (m_FinalDestination.y > 0) + { + Chunk->GetBlockTypeMeta(RelX, FloorC(m_FinalDestination.y) - 1, RelZ, BlockType, BlockMeta); + if (cBlockInfo::IsSolid(BlockType)) + { + break; + } + m_FinalDestination.y -= 1; + } + } + } // If destination in water, go up to the highest water block. // If destination in solid, go up to first air block. @@ -447,21 +507,29 @@ void cMonster::SetPitchAndYawFromDestination() } } + + + Vector3d BodyDistance = m_NextWayPointPosition - GetPosition(); + double BodyRotation, BodyPitch; + BodyDistance.Normalize(); + VectorToEuler(BodyDistance.x, BodyDistance.y, BodyDistance.z, BodyRotation, BodyPitch); + SetYaw(BodyRotation); + Vector3d Distance = FinalDestination - GetPosition(); { - double Rotation, Pitch; + double HeadRotation, HeadPitch; Distance.Normalize(); - VectorToEuler(Distance.x, Distance.y, Distance.z, Rotation, Pitch); - SetHeadYaw(Rotation); - SetPitch(-Pitch); - } - - { - Vector3d BodyDistance = m_NextWayPointPosition - GetPosition(); - double Rotation, Pitch; - BodyDistance.Normalize(); - VectorToEuler(BodyDistance.x, BodyDistance.y, BodyDistance.z, Rotation, Pitch); - SetYaw(Rotation); + VectorToEuler(Distance.x, Distance.y, Distance.z, HeadRotation, HeadPitch); + if (std::abs(BodyRotation - HeadRotation) < 120) + { + SetHeadYaw(HeadRotation); + SetPitch(-HeadPitch); + } + else // We're not an owl. If it's more than 120, don't look behind and instead look at where you're walking. + { + SetHeadYaw(BodyRotation); + SetPitch(-BodyPitch); + } } } diff --git a/src/Mobs/Monster.h b/src/Mobs/Monster.h index 5d20ba810..c4043b0e5 100644 --- a/src/Mobs/Monster.h +++ b/src/Mobs/Monster.h @@ -180,6 +180,13 @@ protected: /** Coordinates for the ultimate, final destination last given to the pathfinder. */ Vector3d m_PathFinderDestination; + /** True if there's no path to target and we're walking to an approximated location. */ + bool m_NoPathToTarget; + + /** Whether The mob has finished their path, note that this does not imply reaching the destination, + the destination may sometimes differ from the current path. */ + bool m_NoMoreWayPoints; + /** Finds the lowest non-air block position (not the highest, as cWorld::GetHeight does) If current Y is nonsolid, goes down to try to find a solid block, then returns that + 1 If current Y is solid, goes up to find first nonsolid block, and returns that. diff --git a/src/Mobs/Path.cpp b/src/Mobs/Path.cpp index ba7d615ae..eba29be7e 100644 --- a/src/Mobs/Path.cpp +++ b/src/Mobs/Path.cpp @@ -6,20 +6,14 @@ #include "Path.h" #include "../Chunk.h" + #define DISTANCE_MANHATTAN 0 // 1: More speed, a bit less accuracy 0: Max accuracy, less speed. #define HEURISTICS_ONLY 0 // 1: Much more speed, much less accurate. -#define CALCULATIONS_PER_STEP 5 // Higher means more CPU load but faster path calculations. +#define CALCULATIONS_PER_STEP 10 // Higher means more CPU load but faster path calculations. // The only version which guarantees the shortest path is 0, 0. -enum class eCellStatus {OPENLIST, CLOSEDLIST, NOLIST}; -struct cPathCell -{ - Vector3i m_Location; // Location of the cell in the world. - int m_F, m_G, m_H; // F, G, H as defined in regular A*. - eCellStatus m_Status; // Which list is the cell in? Either non, open, or closed. - cPathCell * m_Parent; // Cell's parent, as defined in regular A*. - bool m_IsSolid; // Is the cell an air or a solid? Partial solids are currently considered solids. -}; + + @@ -44,7 +38,8 @@ cPath::cPath( m_Destination(a_EndingPoint.Floor()), m_Source(a_StartingPoint.Floor()), m_CurrentPoint(0), // GetNextPoint increments this to 1, but that's fine, since the first cell is always a_StartingPoint - m_Chunk(&a_Chunk) + m_Chunk(&a_Chunk), + m_BadChunkFound(false) { // TODO: if src not walkable OR dest not walkable, then abort. // Borrow a new "isWalkable" from ProcessIfWalkable, make ProcessIfWalkable also call isWalkable @@ -55,6 +50,7 @@ cPath::cPath( return; } + m_NearestPointToTarget = GetCell(m_Source); m_Status = ePathFinderStatus::CALCULATING; m_StepsLeft = a_MaxSteps; @@ -81,15 +77,20 @@ cPath::~cPath() ePathFinderStatus cPath::Step(cChunk & a_Chunk) { m_Chunk = &a_Chunk; - if (m_Status != ePathFinderStatus::CALCULATING) { return m_Status; } - if (m_StepsLeft == 0) + if (m_BadChunkFound) { FinishCalculation(ePathFinderStatus::PATH_NOT_FOUND); + return m_Status; + } + + if (m_StepsLeft == 0) + { + AttemptToFindAlternative(); } else { @@ -102,9 +103,9 @@ ePathFinderStatus cPath::Step(cChunk & a_Chunk) break; // if we're here, m_Status must have changed either to PATH_FOUND or PATH_NOT_FOUND. } } - } - m_Chunk = nullptr; + m_Chunk = nullptr; + } return m_Status; } @@ -112,6 +113,17 @@ ePathFinderStatus cPath::Step(cChunk & a_Chunk) +Vector3i cPath::AcceptNearbyPath() +{ + ASSERT(m_Status == ePathFinderStatus::NEARBY_FOUND); + m_Status = ePathFinderStatus::PATH_FOUND; + return m_Destination; +} + + + + + bool cPath::IsSolid(const Vector3i & a_Location) { ASSERT(m_Chunk != nullptr); @@ -119,6 +131,7 @@ bool cPath::IsSolid(const Vector3i & a_Location) auto Chunk = m_Chunk->GetNeighborChunk(a_Location.x, a_Location.z); if ((Chunk == nullptr) || !Chunk->IsValid()) { + m_BadChunkFound = true; return true; } m_Chunk = Chunk; @@ -149,34 +162,36 @@ bool cPath::Step_Internal() { cPathCell * CurrentCell = OpenListPop(); - // Path not reachable, open list exauhsted. + // Path not reachable. if (CurrentCell == nullptr) { - FinishCalculation(ePathFinderStatus::PATH_NOT_FOUND); - ASSERT(m_Status == ePathFinderStatus::PATH_NOT_FOUND); + AttemptToFindAlternative(); return true; } // Path found. - if ( - (CurrentCell->m_Location == m_Destination + Vector3i(0, 0, 1)) || - (CurrentCell->m_Location == m_Destination + Vector3i(1, 0, 0)) || - (CurrentCell->m_Location == m_Destination + Vector3i(-1, 0, 0)) || - (CurrentCell->m_Location == m_Destination + Vector3i(0, 0, -1)) || - (CurrentCell->m_Location == m_Destination + Vector3i(0, -1, 0)) - ) + if (CurrentCell->m_Location == m_Destination) { - do - { - m_PathPoints.push_back(CurrentCell->m_Location); // Populate the cPath with points. - CurrentCell = CurrentCell->m_Parent; - } while (CurrentCell != nullptr); - + BuildPath(); FinishCalculation(ePathFinderStatus::PATH_FOUND); return true; } - // Calculation not finished yet, process a currentCell by inspecting all neighbors. + // Calculation not finished yet. + // Check if we have a new NearestPoint. + + if ((m_Destination - CurrentCell->m_Location).Length() < 5) + { + if (m_Rand.NextInt(4) == 0) + { + m_NearestPointToTarget = CurrentCell; + } + } + else if (CurrentCell->m_H < m_NearestPointToTarget->m_H) + { + m_NearestPointToTarget = CurrentCell; + } + // process a currentCell by inspecting all neighbors. // Check North, South, East, West on all 3 different heights. int i; @@ -213,13 +228,40 @@ bool cPath::Step_Internal() -void cPath::FinishCalculation() +void cPath::AttemptToFindAlternative() { - for (auto && pair : m_Map) + if (m_NearestPointToTarget == GetCell(m_Source)) { - delete pair.second; + FinishCalculation(ePathFinderStatus::PATH_NOT_FOUND); } + else + { + m_Destination = m_NearestPointToTarget->m_Location; + BuildPath(); + FinishCalculation(ePathFinderStatus::NEARBY_FOUND); + } +} + + + + +void cPath::BuildPath() +{ + cPathCell * CurrentCell = GetCell(m_Destination); + do + { + m_PathPoints.push_back(CurrentCell->m_Location); // Populate the cPath with points. + CurrentCell = CurrentCell->m_Parent; + } while (CurrentCell != nullptr); +} + + + + + +void cPath::FinishCalculation() +{ m_Map.clear(); m_OpenList = std::priority_queue<cPathCell *, std::vector<cPathCell *>, compareHeuristics>{}; } @@ -230,6 +272,10 @@ void cPath::FinishCalculation() void cPath::FinishCalculation(ePathFinderStatus a_NewStatus) { + if (m_BadChunkFound) + { + a_NewStatus = ePathFinderStatus::PATH_NOT_FOUND; + } m_Status = a_NewStatus; FinishCalculation(); } @@ -255,7 +301,7 @@ cPathCell * cPath::OpenListPop() // Popping from the open list also means addin { if (m_OpenList.size() == 0) { - return nullptr; // We've exhausted the search space and nothing was found, this will trigger a PATH_NOT_FOUND status. + return nullptr; // We've exhausted the search space and nothing was found, this will trigger a PATH_NOT_FOUND or NEARBY_FOUND status. } cPathCell * Ret = m_OpenList.top(); @@ -343,23 +389,20 @@ void cPath::ProcessCell(cPathCell * a_Cell, cPathCell * a_Caller, int a_GDelta) cPathCell * cPath::GetCell(const Vector3i & a_Location) { // Create the cell in the hash table if it's not already there. - cPathCell * Cell; if (m_Map.count(a_Location) == 0) // Case 1: Cell is not on any list. We've never checked this cell before. { - Cell = new cPathCell(); - Cell->m_Location = a_Location; - m_Map[a_Location] = Cell; - Cell->m_IsSolid = IsSolid(a_Location); - Cell->m_Status = eCellStatus::NOLIST; + m_Map[a_Location].m_Location = a_Location; + m_Map[a_Location].m_IsSolid = IsSolid(a_Location); + m_Map[a_Location].m_Status = eCellStatus::NOLIST; #ifdef COMPILING_PATHFIND_DEBUGGER #ifdef COMPILING_PATHFIND_DEBUGGER_MARK_UNCHECKED si::setBlock(a_Location.x, a_Location.y, a_Location.z, debug_unchecked, Cell->m_IsSolid ? NORMAL : MINI); #endif #endif - return Cell; + return &m_Map[a_Location]; } else { - return m_Map[a_Location]; + return &m_Map[a_Location]; } } diff --git a/src/Mobs/Path.h b/src/Mobs/Path.h index adae77984..b296bbdf5 100644 --- a/src/Mobs/Path.h +++ b/src/Mobs/Path.h @@ -1,16 +1,13 @@ #pragma once -/* Wanna use the pathfinder? Put this in your header file: - -// Fwd: cPath +/* +// Needed Fwds: cPath enum class ePathFinderStatus; class cPath; - -Put this in your .cpp: -#include "...Path.h" */ +#include "../FastRandom.h" #ifdef COMPILING_PATHFIND_DEBUGGER /* Note: the COMPILING_PATHFIND_DEBUGGER flag is used by Native / WiseOldMan95 to debug this class outside of MCServer. This preprocessor flag is never set when compiling MCServer. */ @@ -23,14 +20,31 @@ Put this in your .cpp: class cChunk; /* Various little structs and classes */ -enum class ePathFinderStatus {CALCULATING, PATH_FOUND, PATH_NOT_FOUND}; -struct cPathCell; // Defined inside Path.cpp +enum class ePathFinderStatus {CALCULATING, PATH_FOUND, PATH_NOT_FOUND, NEARBY_FOUND}; +enum class eCellStatus {OPENLIST, CLOSEDLIST, NOLIST}; +struct cPathCell +{ + Vector3i m_Location; // Location of the cell in the world. + int m_F, m_G, m_H; // F, G, H as defined in regular A*. + eCellStatus m_Status; // Which list is the cell in? Either non, open, or closed. + cPathCell * m_Parent; // Cell's parent, as defined in regular A*. + bool m_IsSolid; // Is the cell an air or a solid? Partial solids are currently considered solids. +}; + + + + + class compareHeuristics { public: bool operator()(cPathCell * & a_V1, cPathCell * & a_V2); }; + + + + class cPath { public: @@ -62,9 +76,17 @@ public: /** Destroys the path and frees its memory. */ ~cPath(); - /** Performs part of the path calculation and returns true if the path computation has finished. */ + /** Performs part of the path calculation and returns the appropriate status. + If NEARBY_FOUND is returned, it means that the destination is not reachable, but a nearby destination + is reachable. If the user likes the alternative destination, they can call AcceptNearbyPath to treat the path as found, + and to make consequent calls to step return PATH_FOUND*/ ePathFinderStatus Step(cChunk & a_Chunk); + /** Called after the PathFinder's step returns NEARBY_FOUND. + Changes the PathFinder status from NEARBY_FOUND to PATH_FOUND, returns the nearby destination that + the PathFinder found a path to. */ + Vector3i AcceptNearbyPath(); + /* Point retrieval functions, inlined for performance. */ /** Returns the next point in the path. */ inline Vector3i GetNextPoint() @@ -93,7 +115,10 @@ public: /** Returns the total number of points this path has. */ inline int GetPointCount() { - ASSERT(m_Status == ePathFinderStatus::PATH_FOUND); + if (m_Status != ePathFinderStatus::PATH_FOUND) + { + return 0; + } return m_PathPoints.size(); } @@ -119,6 +144,8 @@ private: bool Step_Internal(); // The public version just calls this version * CALCULATIONS_PER_CALL times. void FinishCalculation(); // Clears the memory used for calculating the path. void FinishCalculation(ePathFinderStatus a_NewStatus); // Clears the memory used for calculating the path and changes the status. + void AttemptToFindAlternative(); + void BuildPath(); /* Openlist and closedlist management */ void OpenListAdd(cPathCell * a_Cell); @@ -131,10 +158,12 @@ private: /* Pathfinding fields */ std::priority_queue<cPathCell *, std::vector<cPathCell *>, compareHeuristics> m_OpenList; - std::unordered_map<Vector3i, cPathCell *, VectorHasher> m_Map; + std::unordered_map<Vector3i, cPathCell, VectorHasher> m_Map; Vector3i m_Destination; Vector3i m_Source; int m_StepsLeft; + cPathCell * m_NearestPointToTarget; + cFastRandom m_Rand; /* Control fields */ ePathFinderStatus m_Status; @@ -145,6 +174,7 @@ private: /* Interfacing with the world */ cChunk * m_Chunk; // Only valid inside Step()! + bool m_BadChunkFound; #ifdef COMPILING_PATHFIND_DEBUGGER #include "../path_irrlicht.cpp" #endif diff --git a/src/OverridesSettingsRepository.cpp b/src/OverridesSettingsRepository.cpp new file mode 100644 index 000000000..e63f2c44c --- /dev/null +++ b/src/OverridesSettingsRepository.cpp @@ -0,0 +1,273 @@ + +#include "Globals.h" +#include "OverridesSettingsRepository.h" + +cOverridesSettingsRepository::cOverridesSettingsRepository(std::unique_ptr<cSettingsRepositoryInterface> a_Main, std::unique_ptr<cSettingsRepositoryInterface> a_Overrides) : + m_Main(std::move(a_Main)), + m_Overrides(std::move(a_Overrides)) +{ +} + + + + + +bool cOverridesSettingsRepository::KeyExists(const AString a_keyName) const +{ + return m_Overrides->KeyExists(a_keyName) || m_Main->KeyExists(a_keyName); +} + + + + +bool cOverridesSettingsRepository::HasValue(const AString & a_KeyName, const AString & a_ValueName) const +{ + return m_Overrides->HasValue(a_KeyName, a_ValueName) || m_Main->HasValue(a_KeyName, a_ValueName); +} + + + + + +int cOverridesSettingsRepository::AddKeyName(const AString & a_keyname) +{ + + if (m_Overrides->KeyExists(a_keyname)) + { + m_Overrides->AddKeyName(a_keyname); + return 0; + } + + return m_Main->AddKeyName(a_keyname); +} + + + + + +bool cOverridesSettingsRepository::AddKeyComment(const AString & a_keyname, const AString & a_comment) +{ + if (m_Overrides->KeyExists(a_keyname)) + { + return m_Overrides->AddKeyComment(a_keyname, a_comment); + } + + return m_Main->AddKeyComment(a_keyname, a_comment); +} + + + + + +AString cOverridesSettingsRepository::GetKeyComment(const AString & a_keyname, const int a_commentID) const +{ + + if (m_Overrides->KeyExists(a_keyname)) + { + return m_Overrides->GetKeyComment(a_keyname, a_commentID); + } + + return m_Main->GetKeyComment(a_keyname, a_commentID); +} + + + + + +bool cOverridesSettingsRepository::DeleteKeyComment(const AString & a_keyname, const int a_commentID) +{ + if (m_Overrides->KeyExists(a_keyname)) + { + return m_Overrides->DeleteKeyComment(a_keyname, a_commentID); + } + + return m_Main->DeleteKeyComment(a_keyname, a_commentID); +} + + + + + +void cOverridesSettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + m_Overrides->AddValue(a_KeyName, a_ValueName, a_Value); + } + else + { + m_Main->AddValue(a_KeyName, a_ValueName, a_Value); + } +} + + + + + +std::vector<std::pair<AString, AString>> cOverridesSettingsRepository::GetValues(AString a_keyName) +{ + auto overrides = m_Overrides->GetValues(a_keyName); + auto main = m_Main->GetValues(a_keyName); + std::sort(overrides.begin(), overrides.end(), [](std::pair<AString, AString> a, std::pair<AString, AString> b) -> bool { return a < b ;}); + std::sort(main.begin(), main.end(), [](std::pair<AString, AString> a, std::pair<AString, AString> b) -> bool { return a < b ;}); + + std::vector<std::pair<AString, AString>> ret; + + + size_t overridesIndex = 0; + for (auto pair : main) + { + if (overridesIndex >= overrides.size()) + { + ret.push_back(pair); + continue; + } + if (pair.first == overrides[overridesIndex].first) + { + continue; + } + while (pair.first > overrides[overridesIndex].first) + { + ret.push_back(overrides[overridesIndex]); + overridesIndex++; + } + ret.push_back(pair); + } + return ret; +} + + + + + +AString cOverridesSettingsRepository::GetValue(const AString & a_KeyName, const AString & a_ValueName, const AString & defValue) const +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->GetValue(a_KeyName, a_ValueName, defValue); + } + else + { + return m_Main->GetValue(a_KeyName, a_ValueName, defValue); + } +} + + + + + +AString cOverridesSettingsRepository::GetValueSet (const AString & a_KeyName, const AString & a_ValueName, const AString & defValue) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->GetValueSet(a_KeyName, a_ValueName, defValue); + } + else + { + return m_Main->GetValueSet(a_KeyName, a_ValueName, defValue); + } +} + + + + + +int cOverridesSettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const int defValue) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->GetValueSetI(a_KeyName, a_ValueName, defValue); + } + else + { + return m_Main->GetValueSetI(a_KeyName, a_ValueName, defValue); + } +} + + + + + +Int64 cOverridesSettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const Int64 defValue) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->GetValueSetI(a_KeyName, a_ValueName, defValue); + } + else + { + return m_Main->GetValueSetI(a_KeyName, a_ValueName, defValue); + } +} + + + + + +bool cOverridesSettingsRepository::GetValueSetB(const AString & a_KeyName, const AString & a_ValueName, const bool defValue) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->GetValueSetB(a_KeyName, a_ValueName, defValue); + } + else + { + return m_Main->GetValueSetB(a_KeyName, a_ValueName, defValue); + } +} + + + + + +bool cOverridesSettingsRepository::SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->SetValue(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists); + } + else + { + return m_Main->SetValue(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists); + } +} + + + + + +bool cOverridesSettingsRepository::SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->SetValueI(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists); + } + else + { + return m_Main->SetValueI(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists); + } +} + + + + + +bool cOverridesSettingsRepository::DeleteValue(const AString & a_KeyName, const AString & a_ValueName) +{ + if (m_Overrides->HasValue(a_KeyName, a_ValueName)) + { + return m_Overrides->DeleteValue(a_KeyName, a_ValueName); + } + else + { + return m_Main->DeleteValue(a_KeyName, a_ValueName); + } +} + + + +bool cOverridesSettingsRepository::Flush() +{ + return m_Overrides->Flush() && m_Main->Flush(); +} + diff --git a/src/OverridesSettingsRepository.h b/src/OverridesSettingsRepository.h new file mode 100644 index 000000000..04a53997f --- /dev/null +++ b/src/OverridesSettingsRepository.h @@ -0,0 +1,52 @@ + +#pragma once + +#include "SettingsRepositoryInterface.h" + +#include <unordered_map> + +class cOverridesSettingsRepository : public cSettingsRepositoryInterface +{ + +public: + cOverridesSettingsRepository(std::unique_ptr<cSettingsRepositoryInterface> a_Main, std::unique_ptr<cSettingsRepositoryInterface> a_Overrides); + + virtual ~cOverridesSettingsRepository() = default; + + virtual bool KeyExists(const AString keyname) const override; + + virtual bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const override; + + virtual int AddKeyName(const AString & keyname) override; + + virtual bool AddKeyComment(const AString & keyname, const AString & comment) override; + + virtual AString GetKeyComment(const AString & keyname, const int commentID) const override; + + virtual bool DeleteKeyComment(const AString & keyname, const int commentID) override; + + virtual void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) override; + + virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override; + + virtual AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const override; + + virtual AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") override; + virtual int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) override; + virtual Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) override; + virtual bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) override; + + virtual bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) override; + virtual bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) override; + + virtual bool DeleteValue(const AString & keyname, const AString & valuename) override; + + virtual bool Flush() override; + +private: + + std::unique_ptr<cSettingsRepositoryInterface> m_Main; + std::unique_ptr<cSettingsRepositoryInterface> m_Overrides; + +}; + diff --git a/src/PolarSSL++/BlockingSslClientSocket.h b/src/PolarSSL++/BlockingSslClientSocket.h index 319e82bf2..462ee95a7 100644 --- a/src/PolarSSL++/BlockingSslClientSocket.h +++ b/src/PolarSSL++/BlockingSslClientSocket.h @@ -21,6 +21,11 @@ class cBlockingSslClientSocket : { public: cBlockingSslClientSocket(void); + + ~cBlockingSslClientSocket(void) + { + Disconnect(); + } /** Connects to the specified server and performs SSL handshake. Returns true if successful, false on failure. Sets internal error text on failure. */ diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp index c9e4296a2..bfbe5028d 100644 --- a/src/Protocol/Authenticator.cpp +++ b/src/Protocol/Authenticator.cpp @@ -19,6 +19,10 @@ #define DEFAULT_AUTH_SERVER "sessionserver.mojang.com" #define DEFAULT_AUTH_ADDRESS "/session/minecraft/hasJoined?username=%USERNAME%&serverId=%SERVERID%" + + + + cAuthenticator::cAuthenticator(void) : super("cAuthenticator"), m_Server(DEFAULT_AUTH_SERVER), @@ -40,11 +44,11 @@ cAuthenticator::~cAuthenticator() -void cAuthenticator::ReadINI(cIniFile & IniFile) +void cAuthenticator::ReadSettings(cSettingsRepositoryInterface & a_Settings) { - m_Server = IniFile.GetValueSet ("Authentication", "Server", DEFAULT_AUTH_SERVER); - m_Address = IniFile.GetValueSet ("Authentication", "Address", DEFAULT_AUTH_ADDRESS); - m_ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true); + m_Server = a_Settings.GetValueSet ("Authentication", "Server", DEFAULT_AUTH_SERVER); + m_Address = a_Settings.GetValueSet ("Authentication", "Address", DEFAULT_AUTH_ADDRESS); + m_ShouldAuthenticate = a_Settings.GetValueSetB("Authentication", "Authenticate", true); } @@ -69,9 +73,9 @@ void cAuthenticator::Authenticate(int a_ClientID, const AString & a_UserName, co -void cAuthenticator::Start(cIniFile & IniFile) +void cAuthenticator::Start(cSettingsRepositoryInterface & a_Settings) { - ReadINI(IniFile); + ReadSettings(a_Settings); m_ShouldTerminate = false; super::Start(); } @@ -267,3 +271,7 @@ bool cAuthenticator::GetPlayerProperties(const AString & a_UUID, Json::Value & a return true; } */ + + + + diff --git a/src/Protocol/Authenticator.h b/src/Protocol/Authenticator.h index 853eff535..02b349256 100644 --- a/src/Protocol/Authenticator.h +++ b/src/Protocol/Authenticator.h @@ -14,7 +14,7 @@ #include "../OSSupport/IsThread.h" - +class cSettingsRepositoryInterface; @@ -40,13 +40,13 @@ public: ~cAuthenticator(); /** (Re-)read server and address from INI: */ - void ReadINI(cIniFile & IniFile); + void ReadSettings(cSettingsRepositoryInterface & a_Settings); /** Queues a request for authenticating a user. If the auth fails, the user will be kicked */ void Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash); /** Starts the authenticator thread. The thread may be started and stopped repeatedly */ - void Start(cIniFile & IniFile); + void Start(cSettingsRepositoryInterface & a_Settings); /** Stops the authenticator thread. The thread may be started and stopped repeatedly */ void Stop(void); diff --git a/src/Protocol/MojangAPI.cpp b/src/Protocol/MojangAPI.cpp index 0d1441500..110590359 100644 --- a/src/Protocol/MojangAPI.cpp +++ b/src/Protocol/MojangAPI.cpp @@ -38,12 +38,36 @@ const int MAX_PER_QUERY = 100; -/** This is the data of the root certs for Starfield Technologies, the CA that signed sessionserver.mojang.com's cert: -Downloaded from http://certs.starfieldtech.com/repository/ */ -static const AString & StarfieldCACert(void) +/** Returns the CA certificates that should be trusted for Mojang-related connections. */ +static const AString & GetCACerts(void) { static const AString Cert( - // G2 cert + // Equifax root CA cert + // Currently used for signing *.mojang.com's cert + // Exported from Mozilla Firefox's built-in CA repository + "-----BEGIN CERTIFICATE-----\n" + "MIIDIDCCAomgAwIBAgIENd70zzANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJV\n" + "UzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2Vy\n" + "dGlmaWNhdGUgQXV0aG9yaXR5MB4XDTk4MDgyMjE2NDE1MVoXDTE4MDgyMjE2NDE1\n" + "MVowTjELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0VxdWlmYXgxLTArBgNVBAsTJEVx\n" + "dWlmYXggU2VjdXJlIENlcnRpZmljYXRlIEF1dGhvcml0eTCBnzANBgkqhkiG9w0B\n" + "AQEFAAOBjQAwgYkCgYEAwV2xWGcIYu6gmi0fCG2RFGiYCh7+2gRvE4RiIcPRfM6f\n" + "BeC4AfBONOziipUEZKzxa1NfBbPLZ4C/QgKO/t0BCezhABRP/PvwDN1Dulsr4R+A\n" + "cJkVV5MW8Q+XarfCaCMczE1ZMKxRHjuvK9buY0V7xdlfUNLjUA86iOe/FP3gx7kC\n" + "AwEAAaOCAQkwggEFMHAGA1UdHwRpMGcwZaBjoGGkXzBdMQswCQYDVQQGEwJVUzEQ\n" + "MA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2VydGlm\n" + "aWNhdGUgQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMBoGA1UdEAQTMBGBDzIwMTgw\n" + "ODIyMTY0MTUxWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gj\n" + "IBBPM5iQn9QwHQYDVR0OBBYEFEjmaPkr0rKV10fYIyAQTzOYkJ/UMAwGA1UdEwQF\n" + "MAMBAf8wGgYJKoZIhvZ9B0EABA0wCxsFVjMuMGMDAgbAMA0GCSqGSIb3DQEBBQUA\n" + "A4GBAFjOKer89961zgK5F7WF0bnj4JXMJTENAKaSbn+2kmOeUJXRmm/kEd5jhW6Y\n" + "7qj/WsjTVbJmcVfewCHrPSqnI0kBBIZCe/zuf6IWUrVnZ9NA2zsmWLIodz2uFHdh\n" + "1voqZiegDfqnc1zqcPGUIWVEX/r87yloqaKHee9570+sB3c4\n" + "-----END CERTIFICATE-----\n\n" + + // Starfield G2 cert + // This is the data of the root certs for Starfield Technologies, the CA that used to sign sessionserver.mojang.com's cert + // Downloaded from http://certs.starfieldtech.com/repository/ "-----BEGIN CERTIFICATE-----\n" "MIID3TCCAsWgAwIBAgIBADANBgkqhkiG9w0BAQsFADCBjzELMAkGA1UEBhMCVVMx\n" "EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxJTAjBgNVBAoT\n" @@ -67,7 +91,8 @@ static const AString & StarfieldCACert(void) "pL/QlwVKvOoYKAKQvVR4CSFx09F9HdkWsKlhPdAKACL8x3vLCWRFCztAgfd9fDL1\n" "mMpYjn0q7pBZc2T5NnReJaH1ZgUufzkVqSr7UIuOhWn0\n" "-----END CERTIFICATE-----\n\n" - // Original (G1) cert: + + // Starfield original (G1) cert: "-----BEGIN CERTIFICATE-----\n" "MIIEDzCCAvegAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswCQYDVQQGEwJVUzEl\n" "MCMGA1UEChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMp\n" @@ -226,12 +251,12 @@ cMojangAPI::~cMojangAPI() -void cMojangAPI::Start(cIniFile & a_SettingsIni, bool a_ShouldAuth) +void cMojangAPI::Start(cSettingsRepositoryInterface & a_Settings, bool a_ShouldAuth) { - m_NameToUUIDServer = a_SettingsIni.GetValueSet("MojangAPI", "NameToUUIDServer", DEFAULT_NAME_TO_UUID_SERVER); - m_NameToUUIDAddress = a_SettingsIni.GetValueSet("MojangAPI", "NameToUUIDAddress", DEFAULT_NAME_TO_UUID_ADDRESS); - m_UUIDToProfileServer = a_SettingsIni.GetValueSet("MojangAPI", "UUIDToProfileServer", DEFAULT_UUID_TO_PROFILE_SERVER); - m_UUIDToProfileAddress = a_SettingsIni.GetValueSet("MojangAPI", "UUIDToProfileAddress", DEFAULT_UUID_TO_PROFILE_ADDRESS); + m_NameToUUIDServer = a_Settings.GetValueSet("MojangAPI", "NameToUUIDServer", DEFAULT_NAME_TO_UUID_SERVER); + m_NameToUUIDAddress = a_Settings.GetValueSet("MojangAPI", "NameToUUIDAddress", DEFAULT_NAME_TO_UUID_ADDRESS); + m_UUIDToProfileServer = a_Settings.GetValueSet("MojangAPI", "UUIDToProfileServer", DEFAULT_UUID_TO_PROFILE_SERVER); + m_UUIDToProfileAddress = a_Settings.GetValueSet("MojangAPI", "UUIDToProfileAddress", DEFAULT_UUID_TO_PROFILE_ADDRESS); LoadCachesFromDisk(); if (a_ShouldAuth) { @@ -390,7 +415,7 @@ bool cMojangAPI::SecureRequest(const AString & a_ServerName, const AString & a_R { // Connect the socket: cBlockingSslClientSocket Socket; - Socket.SetTrustedRootCertsFromString(StarfieldCACert(), a_ServerName); + Socket.SetTrustedRootCertsFromString(GetCACerts(), a_ServerName); if (!Socket.Connect(a_ServerName, 443)) { LOGWARNING("%s: Can't connect to %s: %s", __FUNCTION__, a_ServerName.c_str(), Socket.GetLastErrorText().c_str()); @@ -434,7 +459,6 @@ bool cMojangAPI::SecureRequest(const AString & a_ServerName, const AString & a_R a_Response.append((const char *)buf, (size_t)ret); } - Socket.Disconnect(); return true; } diff --git a/src/Protocol/MojangAPI.h b/src/Protocol/MojangAPI.h index 0dc2617b6..bea950740 100644 --- a/src/Protocol/MojangAPI.h +++ b/src/Protocol/MojangAPI.h @@ -25,7 +25,7 @@ namespace Json - +class cSettingsRepositoryInterface; // tolua_begin class cMojangAPI @@ -38,7 +38,7 @@ public: /** Initializes the API; reads the settings from the specified ini file. Loads cached results from disk. */ - void Start(cIniFile & a_SettingsIni, bool a_ShouldAuth); + void Start(cSettingsRepositoryInterface & a_Settings, bool a_ShouldAuth); /** Connects to the specified server using SSL, sends the given request and receives the response. Checks Mojang certificates using the hard-coded Starfield root CA certificate. diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index 17faca27e..4612af4a5 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -51,7 +51,7 @@ Implements the 1.8.x protocol classes: /** The slot number that the client uses to indicate "outside the window". */ -static const Int16 SLOT_NUM_OUTSIDE = -1; +static const Int16 SLOT_NUM_OUTSIDE = -999; @@ -2265,7 +2265,7 @@ void cProtocol180::HandlePacketCreativeInventoryAction(cByteBuffer & a_ByteBuffe { return; } - m_Client->HandleCreativeInventory(SlotNum, Item, (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftClickOutside : caLeftClick); + m_Client->HandleCreativeInventory(SlotNum, Item, (SlotNum == -1) ? caLeftClickOutside : caLeftClick); } diff --git a/src/RCONServer.cpp b/src/RCONServer.cpp index 685bd92f5..c5dc9b69b 100644 --- a/src/RCONServer.cpp +++ b/src/RCONServer.cpp @@ -134,15 +134,15 @@ cRCONServer::~cRCONServer() -void cRCONServer::Initialize(cIniFile & a_IniFile) +void cRCONServer::Initialize(cSettingsRepositoryInterface & a_Settings) { - if (!a_IniFile.GetValueSetB("RCON", "Enabled", false)) + if (!a_Settings.GetValueSetB("RCON", "Enabled", false)) { return; } // Read the password, don't allow an empty one: - m_Password = a_IniFile.GetValueSet("RCON", "Password", ""); + m_Password = a_Settings.GetValueSet("RCON", "Password", ""); if (m_Password.empty()) { LOGWARNING("RCON is requested, but the password is not set. RCON is now disabled."); @@ -150,7 +150,7 @@ void cRCONServer::Initialize(cIniFile & a_IniFile) } // Read the listening ports for RCON from config: - AStringVector Ports = ReadUpgradeIniPorts(a_IniFile, "RCON", "Ports", "PortsIPv4", "PortsIPv6", "25575"); + AStringVector Ports = ReadUpgradeIniPorts(a_Settings, "RCON", "Ports", "PortsIPv4", "PortsIPv6", "25575"); // Start listening on each specified port: for (auto port: Ports) diff --git a/src/RCONServer.h b/src/RCONServer.h index 352fa7b50..81b019516 100644 --- a/src/RCONServer.h +++ b/src/RCONServer.h @@ -17,7 +17,7 @@ // fwd: class cServer; -class cIniFile; +class cSettingsRepositoryInterface; @@ -29,7 +29,7 @@ public: cRCONServer(cServer & a_Server); virtual ~cRCONServer(); - void Initialize(cIniFile & a_IniFile); + void Initialize(cSettingsRepositoryInterface & a_Settings); protected: friend class cRCONCommandOutput; diff --git a/src/Root.cpp b/src/Root.cpp index 349608e8d..c3c880e73 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -19,6 +19,8 @@ #include "LoggerListeners.h" #include "BuildInfo.h" #include "IniFile.h" +#include "SettingsRepositoryInterface.h" +#include "OverridesSettingsRepository.h" #ifdef _WIN32 #include <conio.h> @@ -96,7 +98,7 @@ void cRoot::InputThread(cRoot & a_Params) -void cRoot::Start(void) +void cRoot::Start(std::unique_ptr<cSettingsRepositoryInterface> overridesRepo) { #ifdef _WIN32 HWND hwnd = GetConsoleWindow(); @@ -130,22 +132,24 @@ void cRoot::Start(void) m_Server = new cServer(); LOG("Reading server config..."); - cIniFile IniFile; - if (!IniFile.ReadFile("settings.ini")) + + auto IniFile = cpp14::make_unique<cIniFile>(); + if (!IniFile->ReadFile("settings.ini")) { LOGWARN("Regenerating settings.ini, all settings will be reset"); - IniFile.AddHeaderComment(" This is the main server configuration"); - IniFile.AddHeaderComment(" Most of the settings here can be configured using the webadmin interface, if enabled in webadmin.ini"); - IniFile.AddHeaderComment(" See: http://wiki.mc-server.org/doku.php?id=configure:settings.ini for further configuration help"); + IniFile->AddHeaderComment(" This is the main server configuration"); + IniFile->AddHeaderComment(" Most of the settings here can be configured using the webadmin interface, if enabled in webadmin.ini"); + IniFile->AddHeaderComment(" See: http://wiki.mc-server.org/doku.php?id=configure:settings.ini for further configuration help"); } + auto settingsRepo = cpp14::make_unique<cOverridesSettingsRepository>(std::move(IniFile), std::move(overridesRepo)); LOG("Starting server..."); m_MojangAPI = new cMojangAPI; - bool ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true); - m_MojangAPI->Start(IniFile, ShouldAuthenticate); // Mojang API needs to be started before plugins, so that plugins may use it for DB upgrades on server init - if (!m_Server->InitServer(IniFile, ShouldAuthenticate)) + bool ShouldAuthenticate = settingsRepo->GetValueSetB("Authentication", "Authenticate", true); + m_MojangAPI->Start(*settingsRepo, ShouldAuthenticate); // Mojang API needs to be started before plugins, so that plugins may use it for DB upgrades on server init + if (!m_Server->InitServer(*settingsRepo, ShouldAuthenticate)) { - IniFile.WriteFile("settings.ini"); + settingsRepo->Flush(); LOGERROR("Failure starting server, aborting..."); return; } @@ -160,29 +164,29 @@ void cRoot::Start(void) m_FurnaceRecipe = new cFurnaceRecipe(); LOGD("Loading worlds..."); - LoadWorlds(IniFile); + LoadWorlds(*settingsRepo); LOGD("Loading plugin manager..."); m_PluginManager = new cPluginManager(); - m_PluginManager->ReloadPluginsNow(IniFile); + m_PluginManager->ReloadPluginsNow(*settingsRepo); LOGD("Loading MonsterConfig..."); m_MonsterConfig = new cMonsterConfig; // This sets stuff in motion LOGD("Starting Authenticator..."); - m_Authenticator.Start(IniFile); + m_Authenticator.Start(*settingsRepo); LOGD("Starting worlds..."); StartWorlds(); - if (IniFile.GetValueSetB("DeadlockDetect", "Enabled", true)) + if (settingsRepo->GetValueSetB("DeadlockDetect", "Enabled", true)) { LOGD("Starting deadlock detector..."); - dd.Start(IniFile.GetValueSetI("DeadlockDetect", "IntervalSec", 20)); + dd.Start(settingsRepo->GetValueSetI("DeadlockDetect", "IntervalSec", 20)); } - IniFile.WriteFile("settings.ini"); + settingsRepo->Flush(); LOGD("Finalising startup..."); if (m_Server->Start()) @@ -282,30 +286,29 @@ void cRoot::LoadGlobalSettings() -void cRoot::LoadWorlds(cIniFile & IniFile) +void cRoot::LoadWorlds(cSettingsRepositoryInterface & a_Settings) { // First get the default world - AString DefaultWorldName = IniFile.GetValueSet("Worlds", "DefaultWorld", "world"); + AString DefaultWorldName = a_Settings.GetValueSet("Worlds", "DefaultWorld", "world"); m_pDefaultWorld = new cWorld(DefaultWorldName.c_str()); m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld; // Then load the other worlds - int KeyNum = IniFile.FindKey("Worlds"); - int NumWorlds = IniFile.GetNumValues(KeyNum); - if (NumWorlds <= 0) + auto Worlds = a_Settings.GetValues("Worlds"); + if (Worlds.size() <= 0) { return; } bool FoundAdditionalWorlds = false; - for (int i = 0; i < NumWorlds; i++) + for (auto WorldNameValue : Worlds) { - AString ValueName = IniFile.GetValueName(KeyNum, i); + AString ValueName = WorldNameValue.first; if (ValueName.compare("World") != 0) { continue; } - AString WorldName = IniFile.GetValue(KeyNum, i); + AString WorldName = WorldNameValue.second; if (WorldName.empty()) { continue; @@ -317,10 +320,10 @@ void cRoot::LoadWorlds(cIniFile & IniFile) if (!FoundAdditionalWorlds) { - if (IniFile.GetKeyComment("Worlds", 0) != " World=secondworld") + if (a_Settings.GetKeyComment("Worlds", 0) != " World=secondworld") { - IniFile.DeleteKeyComment("Worlds", 0); - IniFile.AddKeyComment("Worlds", " World=secondworld"); + a_Settings.DeleteKeyComment("Worlds", 0); + a_Settings.AddKeyComment("Worlds", " World=secondworld"); } } } diff --git a/src/Root.h b/src/Root.h index e0b6cf26c..2b30afaff 100644 --- a/src/Root.h +++ b/src/Root.h @@ -24,6 +24,7 @@ class cWorld; class cPlayer; class cCommandOutputCallback; class cCompositeChat; +class cSettingsRepositoryInterface; typedef cItemCallback<cPlayer> cPlayerListCallback; typedef cItemCallback<cWorld> cWorldListCallback; @@ -53,7 +54,7 @@ public: cRoot(void); ~cRoot(); - void Start(void); + void Start(std::unique_ptr<cSettingsRepositoryInterface> overridesRepo); // tolua_begin cServer * GetServer(void) { return m_Server; } @@ -204,7 +205,7 @@ private: void LoadGlobalSettings(); /// Loads the worlds from settings.ini, creates the worldmap - void LoadWorlds(cIniFile & IniFile); + void LoadWorlds(cSettingsRepositoryInterface & a_Settings); /// Starts each world's life void StartWorlds(void); diff --git a/src/Server.cpp b/src/Server.cpp index 996de2695..01d5a176a 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -185,12 +185,12 @@ void cServer::PlayerDestroying(const cPlayer * a_Player) -bool cServer::InitServer(cIniFile & a_SettingsIni, bool a_ShouldAuth) +bool cServer::InitServer(cSettingsRepositoryInterface & a_Settings, bool a_ShouldAuth) { - m_Description = a_SettingsIni.GetValueSet("Server", "Description", "MCServer - in C++!"); - m_MaxPlayers = a_SettingsIni.GetValueSetI("Server", "MaxPlayers", 100); - m_bIsHardcore = a_SettingsIni.GetValueSetB("Server", "HardcoreEnabled", false); - m_bAllowMultiLogin = a_SettingsIni.GetValueSetB("Server", "AllowMultiLogin", false); + m_Description = a_Settings.GetValueSet("Server", "Description", "MCServer - in C++!"); + m_MaxPlayers = a_Settings.GetValueSetI("Server", "MaxPlayers", 100); + m_bIsHardcore = a_Settings.GetValueSetB("Server", "HardcoreEnabled", false); + m_bAllowMultiLogin = a_Settings.GetValueSetB("Server", "AllowMultiLogin", false); m_PlayerCount = 0; m_PlayerCountDiff = 0; @@ -205,9 +205,9 @@ bool cServer::InitServer(cIniFile & a_SettingsIni, bool a_ShouldAuth) LOGINFO("Compatible clients: %s", MCS_CLIENT_VERSIONS); LOGINFO("Compatible protocol versions %s", MCS_PROTOCOL_VERSIONS); - m_Ports = ReadUpgradeIniPorts(a_SettingsIni, "Server", "Ports", "Port", "PortsIPv6", "25565"); + m_Ports = ReadUpgradeIniPorts(a_Settings, "Server", "Ports", "Port", "PortsIPv6", "25565"); - m_RCONServer.Initialize(a_SettingsIni); + m_RCONServer.Initialize(a_Settings); m_bIsConnected = true; @@ -226,16 +226,16 @@ bool cServer::InitServer(cIniFile & a_SettingsIni, bool a_ShouldAuth) } // Check if both BungeeCord and online mode are on, if so, warn the admin: - m_ShouldAllowBungeeCord = a_SettingsIni.GetValueSetB("Authentication", "AllowBungeeCord", false); + m_ShouldAllowBungeeCord = a_Settings.GetValueSetB("Authentication", "AllowBungeeCord", false); if (m_ShouldAllowBungeeCord && m_ShouldAuthenticate) { LOGWARNING("WARNING: BungeeCord is allowed and server set to online mode. This is unsafe and will not work properly. Disable either authentication or BungeeCord in settings.ini."); } - m_ShouldLoadOfflinePlayerData = a_SettingsIni.GetValueSetB("PlayerData", "LoadOfflinePlayerData", false); - m_ShouldLoadNamedPlayerData = a_SettingsIni.GetValueSetB("PlayerData", "LoadNamedPlayerData", true); + m_ShouldLoadOfflinePlayerData = a_Settings.GetValueSetB("PlayerData", "LoadOfflinePlayerData", false); + m_ShouldLoadNamedPlayerData = a_Settings.GetValueSetB("PlayerData", "LoadNamedPlayerData", true); - m_ClientViewDistance = a_SettingsIni.GetValueSetI("Server", "DefaultViewDistance", cClientHandle::DEFAULT_VIEW_DISTANCE); + m_ClientViewDistance = a_Settings.GetValueSetI("Server", "DefaultViewDistance", cClientHandle::DEFAULT_VIEW_DISTANCE); if (m_ClientViewDistance < cClientHandle::MIN_VIEW_DISTANCE) { m_ClientViewDistance = cClientHandle::MIN_VIEW_DISTANCE; diff --git a/src/Server.h b/src/Server.h index 1f30295b7..4d0bc1c18 100644 --- a/src/Server.h +++ b/src/Server.h @@ -38,8 +38,8 @@ class cClientHandle; typedef SharedPtr<cClientHandle> cClientHandlePtr; typedef std::list<cClientHandlePtr> cClientHandlePtrs; typedef std::list<cClientHandle *> cClientHandles; -class cIniFile; class cCommandOutputCallback; +class cSettingsRepositoryInterface; namespace Json @@ -58,7 +58,7 @@ public: // tolua_end virtual ~cServer() {} - bool InitServer(cIniFile & a_SettingsIni, bool a_ShouldAuth); + bool InitServer(cSettingsRepositoryInterface & a_Settings, bool a_ShouldAuth); // tolua_begin diff --git a/src/SettingsRepositoryInterface.h b/src/SettingsRepositoryInterface.h new file mode 100644 index 000000000..443b90ff5 --- /dev/null +++ b/src/SettingsRepositoryInterface.h @@ -0,0 +1,61 @@ + +#pragma once + +class cSettingsRepositoryInterface +{ +public: + + enum errors + { + noID = -1, + }; + + virtual ~cSettingsRepositoryInterface() = default; + + /** Returns true iff the specified key exists */ + virtual bool KeyExists(const AString keyname) const = 0; + + /** Returns true iff the specified value exists. */ + virtual bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const = 0; + + /** Add a key name. Return value is not required to mean anything **/ + virtual int AddKeyName(const AString & keyname) = 0; + + /** Add a key comment, will always fail if the repository does not support comments **/ + virtual bool AddKeyComment(const AString & keyname, const AString & comment) = 0; + + /** Return a key comment, returns "" for repositories that do not return comments **/ + virtual AString GetKeyComment(const AString & keyname, const int commentID) const = 0; + + /** Delete a key comment, will always fail if the repository does not support comments **/ + virtual bool DeleteKeyComment(const AString & keyname, const int commentID) = 0; + + /** Adds a new value to the specified key. + If a value of the same name already exists, creates another one **/ + virtual void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) = 0; + + /** returns a vector containing a name, value pair for each value under the key **/ + virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) = 0; + + /** Get the value at the specified key and value, returns defValue on failure **/ + virtual AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const = 0; + + /** Gets the value; if not found, write the default to the repository **/ + virtual AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") = 0; + virtual int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) = 0; + virtual Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) = 0; + virtual bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) = 0; + + /** Overwrites the value of the key, value pair + Specify the optional parameter as false if you do not want the value created if it doesn't exist. + Returns true if value set, false otherwise. **/ + virtual bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) = 0; + virtual bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) = 0; + + /** Deletes the specified key, value pair **/ + virtual bool DeleteValue(const AString & keyname, const AString & valuename) = 0; + + + /** Writes the changes to the backing store, if the repository has one **/ + virtual bool Flush() = 0; +}; diff --git a/src/World.cpp b/src/World.cpp index 8f8665ef5..7f0e3070a 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -622,18 +622,18 @@ void cWorld::Start(void) InitialiseAndLoadMobSpawningValues(IniFile); SetTimeOfDay(IniFile.GetValueSetI("General", "TimeInTicks", GetTimeOfDay())); - m_ChunkMap = make_unique<cChunkMap>(this); + m_ChunkMap = cpp14::make_unique<cChunkMap>(this); // preallocate some memory for ticking blocks so we don't need to allocate that often m_BlockTickQueue.reserve(1000); m_BlockTickQueueCopy.reserve(1000); // Simulators: - m_SimulatorManager = make_unique<cSimulatorManager>(*this); + m_SimulatorManager = cpp14::make_unique<cSimulatorManager>(*this); m_WaterSimulator = InitializeFluidSimulator(IniFile, "Water", E_BLOCK_WATER, E_BLOCK_STATIONARY_WATER); m_LavaSimulator = InitializeFluidSimulator(IniFile, "Lava", E_BLOCK_LAVA, E_BLOCK_STATIONARY_LAVA); - m_SandSimulator = make_unique<cSandSimulator>(*this, IniFile); - m_FireSimulator = make_unique<cFireSimulator>(*this, IniFile); + m_SandSimulator = cpp14::make_unique<cSandSimulator>(*this, IniFile); + m_FireSimulator = cpp14::make_unique<cFireSimulator>(*this, IniFile); m_RedstoneSimulator = InitializeRedstoneSimulator(IniFile); // Water, Lava and Redstone simulators get registered in their initialize function. @@ -801,7 +801,7 @@ void cWorld::InitialiseGeneratorDefaults(cIniFile & a_IniFile) a_IniFile.GetValueSet("Generator", "BiomeGen", "Grown"); a_IniFile.GetValueSet("Generator", "ShapeGen", "BiomalNoise3D"); a_IniFile.GetValueSet("Generator", "CompositionGen", "Biomal"); - a_IniFile.GetValueSet("Generator", "Finishers", "Ravines, WormNestCaves, WaterLakes, WaterSprings, LavaLakes, LavaSprings, OreNests, Mineshafts, Trees, Villages, SprinkleFoliage, Ice, Snow, Lilypads, BottomLava, DeadBushes, NaturalPatches, PreSimulator, Animals"); + a_IniFile.GetValueSet("Generator", "Finishers", "RoughRavines, WormNestCaves, WaterLakes, WaterSprings, LavaLakes, LavaSprings, OreNests, Mineshafts, Trees, Villages, TallGrass, SprinkleFoliage, Ice, Snow, Lilypads, BottomLava, DeadBushes, NaturalPatches, PreSimulator, Animals"); break; } case dimNether: @@ -2681,7 +2681,7 @@ void cWorld::UnloadUnusedChunks(void) void cWorld::QueueUnloadUnusedChunks(void) { - QueueTask(make_unique<cWorld::cTaskUnloadUnusedChunks>()); + QueueTask(cpp14::make_unique<cWorld::cTaskUnloadUnusedChunks>()); } diff --git a/src/main.cpp b/src/main.cpp index 1c34b8f61..8a237b8ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Root.h" +#include "tclap/CmdLine.h" #include <exception> #include <csignal> @@ -14,7 +15,7 @@ #include "OSSupport/NetworkSingleton.h" #include "BuildInfo.h" - +#include "MemorySettingsRepository.h" @@ -206,7 +207,7 @@ BOOL CtrlHandler(DWORD fdwCtrlType) //////////////////////////////////////////////////////////////////////////////// // universalMain - Main startup logic for both standard running and as a service -void universalMain() +void universalMain(std::unique_ptr<cSettingsRepositoryInterface> overridesRepo) { #ifdef _WIN32 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE)) @@ -226,7 +227,7 @@ void universalMain() #endif { cRoot Root; - Root.Start(); + Root.Start(std::move(overridesRepo)); } #if !defined(ANDROID_NDK) catch (std::exception & e) @@ -257,7 +258,7 @@ DWORD WINAPI serviceWorkerThread(LPVOID lpParam) UNREFERENCED_PARAMETER(lpParam); // Do the normal startup - universalMain(); + universalMain(cpp14::make_unique<cMemorySettingsRepository>()); return ERROR_SUCCESS; } @@ -363,14 +364,55 @@ void WINAPI serviceMain(DWORD argc, TCHAR *argv[]) +std::unique_ptr<cMemorySettingsRepository> parseArguments(int argc, char **argv) +{ + try + { + TCLAP::CmdLine cmd("MCServer"); + + TCLAP::ValueArg<int> slotsArg("s", "max-players", "Maximum number of slots for the server to use, overrides setting in setting.ini", false, -1, "number", cmd); + + TCLAP::MultiArg<int> portsArg("p", "port", "The port number the server should listen to", false, "port", cmd); + + cmd.parse(argc, argv); + + auto repo = cpp14::make_unique<cMemorySettingsRepository>(); + + if (slotsArg.isSet()) + { + + int slots = slotsArg.getValue(); + + repo->AddValue("Server", "MaxPlayers", static_cast<Int64>(slots)); + + } + + if (portsArg.isSet()) + { + std::vector<int> ports = portsArg.getValue(); + for (auto port : ports) + { + repo->AddValue("Server", "Port", static_cast<Int64>(port)); + } + } + + repo->SetReadOnly(); + + return repo; + } + catch (TCLAP::ArgException &e) + { + printf("error reading command line %s for arg %s", e.error().c_str(), e.argId().c_str()); + return cpp14::make_unique<cMemorySettingsRepository>(); + } +} + //////////////////////////////////////////////////////////////////////////////// // main: -int main( int argc, char **argv) +int main(int argc, char **argv) { - UNUSED(argc); - UNUSED(argv); #if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER) InitLeakFinder(); @@ -425,6 +467,8 @@ int main( int argc, char **argv) // DEBUG: test the dumpfile creation: // *((int *)0) = 0; + auto argsRepo = parseArguments(argc, argv); + // Check if comm logging is to be enabled: for (int i = 0; i < argc; i++) { @@ -483,7 +527,7 @@ int main( int argc, char **argv) #endif { // Not running as a service, do normal startup - universalMain(); + universalMain(std::move(argsRepo)); } #if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER) |