Skip to content

Commit

Permalink
Waypoint File saving stub
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Dec 2, 2018
1 parent dabef82 commit d9434bf
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Pongbot/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Info.h"
#include "BotManager.h"
#include "WaypointManager.h"
#include "WaypointFileManager.h"
#include "GameFramable.h"
#include <hlsdk/game/shared/IEffects.h>
#include <hlsdk/public/eiface.h>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Pongbot/Pongbot.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<ClCompile Include="ConVarBase.cpp" />
<ClCompile Include="GameFramable.cpp" />
<ClCompile Include="TF2Helper.cpp" />
<ClCompile Include="WaypointFileManager.cpp" />
<ClCompile Include="WaypointManager.cpp" />
<ClCompile Include="WaypointNode.cpp" />
<ClInclude Include="ConVarBase.h" />
Expand All @@ -161,6 +162,7 @@
<ClCompile Include="BotManager.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Util.cpp" />
<ClInclude Include="WaypointFileManager.h" />
<ClInclude Include="WaypointManager.h" />
<ClInclude Include="WaypointNode.h" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Pongbot/Pongbot.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<ClCompile Include="GameFramable.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="WaypointFileManager.cpp">
<Filter>Quelldateien\Waypoint</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Main.h">
Expand Down Expand Up @@ -92,5 +95,8 @@
<ClInclude Include="GameFramable.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="WaypointFileManager.h">
<Filter>Headerdateien\Waypoint</Filter>
</ClInclude>
</ItemGroup>
</Project>
85 changes: 85 additions & 0 deletions Pongbot/WaypointFileManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "WaypointFileManager.h"
#include "Util.h"
#include <metamod/ISmmAPI.h>
#include <metamod/sourcehook.h>
#include <windows.h>
#include <iostream>
#include <fstream>

#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();
}
23 changes: 23 additions & 0 deletions Pongbot/WaypointFileManager.h
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit d9434bf

Please sign in to comment.