Files
OpenRCT2-Web/src/openrct2/network/NetworkUser.cpp
T

241 lines
5.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2016-05-28 22:27:08 +01:00
#ifndef DISABLE_NETWORK
2018-07-21 16:17:06 +02:00
# include "NetworkUser.h"
2018-07-21 16:17:06 +02:00
# include "../core/Console.hpp"
# include "../core/Guard.hpp"
2018-07-21 16:17:06 +02:00
# include "../core/Path.hpp"
# include "../core/String.hpp"
# include "../platform/Platform2.h"
2018-07-21 16:17:06 +02:00
# include <unordered_set>
2018-06-22 23:02:47 +02:00
constexpr const utf8* USER_STORE_FILENAME = "users.json";
NetworkUser* NetworkUser::FromJson(json_t& jsonData)
{
Guard::Assert(jsonData.is_object(), "NetworkUser::FromJson expects parameter jsonData to be object");
const std::string hash = Json::GetString(jsonData["hash"]);
const std::string name = Json::GetString(jsonData["name"]);
json_t jsonGroupId = jsonData["groupId"];
2018-06-22 23:02:47 +02:00
NetworkUser* user = nullptr;
if (!hash.empty() && !name.empty())
2018-06-22 23:02:47 +02:00
{
user = new NetworkUser();
user->Hash = hash;
user->Name = name;
if (jsonGroupId.is_number_integer())
2018-06-22 23:02:47 +02:00
{
user->GroupId = Json::GetNumber<uint8_t>(jsonGroupId);
}
user->Remove = false;
}
return user;
}
json_t NetworkUser::ToJson() const
{
json_t jsonData;
jsonData["hash"] = Hash;
jsonData["name"] = Name;
json_t jsonGroupId;
2018-06-22 23:02:47 +02:00
if (GroupId.HasValue())
{
jsonGroupId = GroupId.GetValue();
2018-06-22 23:02:47 +02:00
}
jsonData["groupId"] = jsonGroupId;
return jsonData;
}
NetworkUserManager::~NetworkUserManager()
{
DisposeUsers();
}
void NetworkUserManager::DisposeUsers()
{
2018-06-22 23:02:47 +02:00
for (const auto& kvp : _usersByHash)
{
delete kvp.second;
}
_usersByHash.clear();
}
void NetworkUserManager::Load()
{
utf8 path[MAX_PATH];
GetStorePath(path, sizeof(path));
if (Platform::FileExists(path))
{
DisposeUsers();
try
{
json_t jsonUsers = Json::ReadFromFile(path);
for (auto& jsonUser : jsonUsers)
{
if (jsonUser.is_object())
{
auto networkUser = NetworkUser::FromJson(jsonUser);
if (networkUser != nullptr)
{
_usersByHash[networkUser->Hash] = networkUser;
}
}
}
}
2018-06-22 23:02:47 +02:00
catch (const std::exception& ex)
{
2018-01-02 19:23:22 +00:00
Console::Error::WriteLine("Failed to read %s as JSON. %s", path, ex.what());
}
}
}
void NetworkUserManager::Save()
{
utf8 path[MAX_PATH];
GetStorePath(path, sizeof(path));
json_t jsonUsers;
try
{
if (Platform::FileExists(path))
{
jsonUsers = Json::ReadFromFile(path);
}
}
2018-06-22 23:02:47 +02:00
catch (const std::exception&)
2016-11-30 23:32:47 +01:00
{
}
// Update existing users
std::unordered_set<std::string> savedHashes;
for (auto it = jsonUsers.begin(); it != jsonUsers.end();)
{
json_t jsonUser = *it;
if (!jsonUser.is_object())
{
continue;
}
std::string hashString = Json::GetString(jsonUser["hash"]);
const auto networkUser = GetUserByHash(hashString);
if (networkUser != nullptr)
{
if (networkUser->Remove)
{
it = jsonUsers.erase(it);
// erase advances the iterator so make sure we don't do it again
continue;
}
else
{
// replace the existing element in jsonUsers
*it = networkUser->ToJson();
savedHashes.insert(hashString);
}
}
it++;
}
// Add new users
2018-06-22 23:02:47 +02:00
for (const auto& kvp : _usersByHash)
{
2018-06-22 23:02:47 +02:00
const NetworkUser* networkUser = kvp.second;
if (!networkUser->Remove && savedHashes.find(networkUser->Hash) == savedHashes.end())
{
jsonUsers.push_back(networkUser->ToJson());
}
}
Json::WriteToFile(path, jsonUsers);
}
void NetworkUserManager::UnsetUsersOfGroup(uint8_t groupId)
{
2018-06-22 23:02:47 +02:00
for (const auto& kvp : _usersByHash)
{
2018-06-22 23:02:47 +02:00
NetworkUser* networkUser = kvp.second;
if (networkUser->GroupId.HasValue() && networkUser->GroupId.GetValue() == groupId)
{
networkUser->GroupId = nullptr;
}
}
}
2018-06-22 23:02:47 +02:00
void NetworkUserManager::RemoveUser(const std::string& hash)
{
2018-06-22 23:02:47 +02:00
NetworkUser* networkUser = GetUserByHash(hash);
2016-05-29 10:56:36 +01:00
if (networkUser != nullptr)
{
networkUser->Remove = true;
}
}
2018-06-22 23:02:47 +02:00
NetworkUser* NetworkUserManager::GetUserByHash(const std::string& hash)
{
auto it = _usersByHash.find(hash);
if (it != _usersByHash.end())
{
return it->second;
}
return nullptr;
}
2018-06-22 23:02:47 +02:00
const NetworkUser* NetworkUserManager::GetUserByHash(const std::string& hash) const
{
auto it = _usersByHash.find(hash);
if (it != _usersByHash.end())
{
return it->second;
}
return nullptr;
}
2018-06-22 23:02:47 +02:00
const NetworkUser* NetworkUserManager::GetUserByName(const std::string& name) const
{
for (auto kvp : _usersByHash)
{
2018-06-22 23:02:47 +02:00
const NetworkUser* networkUser = kvp.second;
if (String::Equals(name.c_str(), networkUser->Name.c_str(), true))
{
return networkUser;
}
}
return nullptr;
}
2018-06-22 23:02:47 +02:00
NetworkUser* NetworkUserManager::GetOrAddUser(const std::string& hash)
{
2018-06-22 23:02:47 +02:00
NetworkUser* networkUser = GetUserByHash(hash);
if (networkUser == nullptr)
{
networkUser = new NetworkUser();
networkUser->Hash = hash;
_usersByHash[hash] = networkUser;
}
return networkUser;
}
2018-06-22 23:02:47 +02:00
void NetworkUserManager::GetStorePath(utf8* buffer, size_t bufferSize)
{
2016-09-25 21:24:29 -05:00
platform_get_user_directory(buffer, nullptr, bufferSize);
Path::Append(buffer, bufferSize, USER_STORE_FILENAME);
}
2016-05-28 22:27:08 +01:00
#endif