Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: get rid of global game variables #292

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Common/Game/IGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "IGame.h"

#include "IW3/GameIW3.h"
#include "IW4/GameIW4.h"
#include "IW5/GameIW5.h"
#include "T5/GameT5.h"
#include "T6/GameT6.h"

#include <cassert>

IGame* IGame::GetGameById(GameId gameId)
{
static IGame* games[static_cast<unsigned>(GameId::COUNT)]{
new IW3::Game(),
new IW4::Game(),
new IW5::Game(),
new T5::Game(),
new T6::Game(),
};

assert(static_cast<unsigned>(gameId) < static_cast<unsigned>(GameId::COUNT));
auto* result = games[static_cast<unsigned>(gameId)];
assert(result);

return result;
}
12 changes: 7 additions & 5 deletions src/Common/Game/IGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class IGame
IGame& operator=(const IGame& other) = default;
IGame& operator=(IGame&& other) noexcept = default;

virtual GameId GetId() = 0;
virtual std::string GetFullName() = 0;
virtual std::string GetShortName() = 0;
[[nodiscard]] virtual GameId GetId() const = 0;
[[nodiscard]] virtual const std::string& GetFullName() const = 0;
[[nodiscard]] virtual const std::string& GetShortName() const = 0;
virtual void AddZone(Zone* zone) = 0;
virtual void RemoveZone(Zone* zone) = 0;
virtual std::vector<Zone*> GetZones() = 0;
virtual std::vector<GameLanguagePrefix> GetLanguagePrefixes() = 0;
[[nodiscard]] virtual const std::vector<Zone*>& GetZones() const = 0;
[[nodiscard]] virtual const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const = 0;

static IGame* GetGameById(GameId gameId);
};
26 changes: 12 additions & 14 deletions src/Common/Game/IW3/GameIW3.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
#include "GameIW3.h"

#include "IW3.h"

#include <algorithm>

using namespace IW3;

GameIW3 g_GameIW3;

GameId GameIW3::GetId()
GameId Game::GetId() const
{
return GameId::IW3;
}

std::string GameIW3::GetFullName()
const std::string& Game::GetFullName() const
{
return "Call Of Duty 4: Modern Warfare";
static std::string fullName = "Call Of Duty 4: Modern Warfare";
return fullName;
}

std::string GameIW3::GetShortName()
const std::string& Game::GetShortName() const
{
return "IW3";
static std::string shortName = "IW3";
return shortName;
}

void GameIW3::AddZone(Zone* zone)
void Game::AddZone(Zone* zone)
{
m_zones.push_back(zone);
}

void GameIW3::RemoveZone(Zone* zone)
void Game::RemoveZone(Zone* zone)
{
const auto foundEntry = std::ranges::find(m_zones, zone);

if (foundEntry != m_zones.end())
m_zones.erase(foundEntry);
}

std::vector<Zone*> GameIW3::GetZones()
const std::vector<Zone*>& Game::GetZones() const
{
return m_zones;
}

std::vector<GameLanguagePrefix> GameIW3::GetLanguagePrefixes()
const std::vector<GameLanguagePrefix>& Game::GetLanguagePrefixes() const
{
std::vector<GameLanguagePrefix> prefixes;
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
29 changes: 15 additions & 14 deletions src/Common/Game/IW3/GameIW3.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once
#include "Game/IGame.h"

class GameIW3 final : public IGame
namespace IW3
{
public:
GameId GetId() override;
std::string GetFullName() override;
std::string GetShortName() override;
void AddZone(Zone* zone) override;
void RemoveZone(Zone* zone) override;
std::vector<Zone*> GetZones() override;
std::vector<GameLanguagePrefix> GetLanguagePrefixes() override;
class Game final : public IGame
{
public:
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
void AddZone(Zone* zone) override;
void RemoveZone(Zone* zone) override;
[[nodiscard]] const std::vector<Zone*>& GetZones() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;

private:
std::vector<Zone*> m_zones;
};

extern GameIW3 g_GameIW3;
private:
std::vector<Zone*> m_zones;
};
} // namespace IW3
26 changes: 12 additions & 14 deletions src/Common/Game/IW4/GameIW4.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
#include "GameIW4.h"

#include "IW4.h"

#include <algorithm>

using namespace IW4;

GameIW4 g_GameIW4;

GameId GameIW4::GetId()
GameId Game::GetId() const
{
return GameId::IW4;
}

std::string GameIW4::GetFullName()
const std::string& Game::GetFullName() const
{
return "Call Of Duty: Modern Warfare 2";
static std::string fullName = "Call Of Duty: Modern Warfare 2";
return fullName;
}

std::string GameIW4::GetShortName()
const std::string& Game::GetShortName() const
{
return "IW4";
static std::string shortName = "IW4";
return shortName;
}

void GameIW4::AddZone(Zone* zone)
void Game::AddZone(Zone* zone)
{
m_zones.push_back(zone);
}

void GameIW4::RemoveZone(Zone* zone)
void Game::RemoveZone(Zone* zone)
{
const auto foundEntry = std::ranges::find(m_zones, zone);

if (foundEntry != m_zones.end())
m_zones.erase(foundEntry);
}

std::vector<Zone*> GameIW4::GetZones()
const std::vector<Zone*>& Game::GetZones() const
{
return m_zones;
}

std::vector<GameLanguagePrefix> GameIW4::GetLanguagePrefixes()
const std::vector<GameLanguagePrefix>& Game::GetLanguagePrefixes() const
{
std::vector<GameLanguagePrefix> prefixes;
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
29 changes: 15 additions & 14 deletions src/Common/Game/IW4/GameIW4.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once
#include "Game/IGame.h"

class GameIW4 final : public IGame
namespace IW4
{
public:
GameId GetId() override;
std::string GetFullName() override;
std::string GetShortName() override;
void AddZone(Zone* zone) override;
void RemoveZone(Zone* zone) override;
std::vector<Zone*> GetZones() override;
std::vector<GameLanguagePrefix> GetLanguagePrefixes() override;
class Game final : public IGame
{
public:
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
void AddZone(Zone* zone) override;
void RemoveZone(Zone* zone) override;
[[nodiscard]] const std::vector<Zone*>& GetZones() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;

private:
std::vector<Zone*> m_zones;
};

extern GameIW4 g_GameIW4;
private:
std::vector<Zone*> m_zones;
};
} // namespace IW4
26 changes: 12 additions & 14 deletions src/Common/Game/IW5/GameIW5.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
#include "GameIW5.h"

#include "IW5.h"

#include <algorithm>

using namespace IW5;

GameIW5 g_GameIW5;

GameId GameIW5::GetId()
GameId Game::GetId() const
{
return GameId::IW5;
}

std::string GameIW5::GetFullName()
const std::string& Game::GetFullName() const
{
return "Call Of Duty: Modern Warfare 3";
static std::string fullName = "Call Of Duty: Modern Warfare 3";
return fullName;
}

std::string GameIW5::GetShortName()
const std::string& Game::GetShortName() const
{
return "IW5";
static std::string shortName = "IW5";
return shortName;
}

void GameIW5::AddZone(Zone* zone)
void Game::AddZone(Zone* zone)
{
m_zones.push_back(zone);
}

void GameIW5::RemoveZone(Zone* zone)
void Game::RemoveZone(Zone* zone)
{
const auto foundEntry = std::ranges::find(m_zones, zone);

if (foundEntry != m_zones.end())
m_zones.erase(foundEntry);
}

std::vector<Zone*> GameIW5::GetZones()
const std::vector<Zone*>& Game::GetZones() const
{
return m_zones;
}

std::vector<GameLanguagePrefix> GameIW5::GetLanguagePrefixes()
const std::vector<GameLanguagePrefix>& Game::GetLanguagePrefixes() const
{
std::vector<GameLanguagePrefix> prefixes;
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
29 changes: 15 additions & 14 deletions src/Common/Game/IW5/GameIW5.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once
#include "Game/IGame.h"

class GameIW5 final : public IGame
namespace IW5
{
public:
GameId GetId() override;
std::string GetFullName() override;
std::string GetShortName() override;
void AddZone(Zone* zone) override;
void RemoveZone(Zone* zone) override;
std::vector<Zone*> GetZones() override;
std::vector<GameLanguagePrefix> GetLanguagePrefixes() override;
class Game final : public IGame
{
public:
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
void AddZone(Zone* zone) override;
void RemoveZone(Zone* zone) override;
[[nodiscard]] const std::vector<Zone*>& GetZones() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;

private:
std::vector<Zone*> m_zones;
};

extern GameIW5 g_GameIW5;
private:
std::vector<Zone*> m_zones;
};
} // namespace IW5
Loading