From d9434bf3b2f35d36fe65d018c3b3d5cfc9078efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20C=C3=BCrg=C3=BCl?= Date: Sun, 2 Dec 2018 23:08:52 +0100 Subject: [PATCH] Waypoint File saving stub --- Pongbot/Main.cpp | 3 ++ Pongbot/Pongbot.vcxproj | 2 + Pongbot/Pongbot.vcxproj.filters | 6 +++ Pongbot/WaypointFileManager.cpp | 85 +++++++++++++++++++++++++++++++++ Pongbot/WaypointFileManager.h | 23 +++++++++ 5 files changed, 119 insertions(+) create mode 100644 Pongbot/WaypointFileManager.cpp create mode 100644 Pongbot/WaypointFileManager.h diff --git a/Pongbot/Main.cpp b/Pongbot/Main.cpp index 6b565ba..28ed5bc 100644 --- a/Pongbot/Main.cpp +++ b/Pongbot/Main.cpp @@ -2,6 +2,7 @@ #include "Info.h" #include "BotManager.h" #include "WaypointManager.h" +#include "WaypointFileManager.h" #include "GameFramable.h" #include #include @@ -35,12 +36,14 @@ bool Main::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool lat BotManager::Init(); WaypointManager::Init(); + WaypointFileManager::Init(); return true; } bool Main::Unload(char *error, size_t len) { BotManager::Destroy(); WaypointManager::Destroy(); + WaypointFileManager::Destroy(); SH_REMOVE_HOOK(IServerGameDLL, GameFrame, Server, SH_MEMBER(this, &Main::_OnGameFrame), true); return true; diff --git a/Pongbot/Pongbot.vcxproj b/Pongbot/Pongbot.vcxproj index fd1d6ab..14ce389 100644 --- a/Pongbot/Pongbot.vcxproj +++ b/Pongbot/Pongbot.vcxproj @@ -153,6 +153,7 @@ + @@ -161,6 +162,7 @@ + diff --git a/Pongbot/Pongbot.vcxproj.filters b/Pongbot/Pongbot.vcxproj.filters index b1d7ffe..3a3a95a 100644 --- a/Pongbot/Pongbot.vcxproj.filters +++ b/Pongbot/Pongbot.vcxproj.filters @@ -57,6 +57,9 @@ Quelldateien + + Quelldateien\Waypoint + @@ -92,5 +95,8 @@ Headerdateien + + Headerdateien\Waypoint + \ No newline at end of file diff --git a/Pongbot/WaypointFileManager.cpp b/Pongbot/WaypointFileManager.cpp new file mode 100644 index 0000000..9d8b575 --- /dev/null +++ b/Pongbot/WaypointFileManager.cpp @@ -0,0 +1,85 @@ +#include "WaypointFileManager.h" +#include "Util.h" +#include +#include +#include +#include +#include + +#define FILE_DIR "tf/addons/pongbot/waypoints/" +#define FILE_EXTENSION "pbw" + +using namespace std; + +extern IServerGameDLL *Server; +extern SourceHook::ISourceHook *g_SHPtr; +extern PluginId g_PLID; + +WaypointFileManager *_WaypointFileManager; + +char _CurrentMapName[32]; + +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, + char const *, char const *, bool, bool); + +void WaypointFileManager::Init() { + Assert(!_WaypointFileManager); + _WaypointFileManager = new WaypointFileManager(); + SH_ADD_HOOK(IServerGameDLL, LevelInit, Server, + SH_MEMBER(_WaypointFileManager, &WaypointFileManager::_OnLevelInit), true); +} + +void WaypointFileManager::Destroy() { + Assert(_WaypointFileManager); + SH_REMOVE_HOOK(IServerGameDLL, LevelInit, Server, + SH_MEMBER(_WaypointFileManager, &WaypointFileManager::_OnLevelInit), true); + delete _WaypointFileManager; +} + +WaypointFileManager::WaypointFileManager() {} + +void WaypointFileManager::Read() { + if (_CheckFile()) + Util::Log("Waypoint loaded! (STUB!)"); +} + +void WaypointFileManager::Write() { + if (_CheckFile()) + Util::Log("Waypoint saved! (STUB!)"); +} + +bool WaypointFileManager::_CheckFile() { + // Check for dir first + struct stat info; + stat(FILE_DIR, &info); + if (~info.st_mode & S_IFDIR) { + // Create dir + bool created = CreateDirectory(FILE_DIR, nullptr); + if (!created) { + Util::Log("Error while creating directory!"); + return false; + } + } + + // Now check for file + char fileName[64]; + sprintf_s(fileName, "%s%s.%s", FILE_DIR, _CurrentMapName, FILE_EXTENSION); + ofstream file(fileName); + file.close(); + + return true; +} + +bool WaypointFileManager::_OnLevelInit(const char *pMapName, char const *pMapEntities, + char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background) { + strcpy(_CurrentMapName, pMapName); + return true; +} + +CON_COMMAND(pongbot_savewaypoint, "Saves current waypoint to file") { + _WaypointFileManager->Write(); +} + +CON_COMMAND(pongbot_loadwaypoint, "Loads waypoint from file") { + _WaypointFileManager->Read(); +} \ No newline at end of file diff --git a/Pongbot/WaypointFileManager.h b/Pongbot/WaypointFileManager.h new file mode 100644 index 0000000..1411b83 --- /dev/null +++ b/Pongbot/WaypointFileManager.h @@ -0,0 +1,23 @@ +#pragma once +#include "ConVarBase.h" + +class WaypointFileManager : public ConVarBase { +public: + static void Init(); + static void Destroy(); +private: + WaypointFileManager(); +public: + void Read(); + void Write(); +private: + char _CurrentMapName[32]; +private: + bool _CheckFile(); +// Hooks +private: + bool _OnLevelInit(const char *pMapName, char const *pMapEntities, char const *pOldLevel, + char const *pLandmarkName, bool loadGame, bool background); +}; + +extern WaypointFileManager *_WaypointFileManager; \ No newline at end of file