2016-05-26 23:52:40 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-05-26 23:52:40 +01:00
|
|
|
*
|
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
|
2016-05-26 23:52:40 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-26 23:52:40 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2016-05-28 22:27:08 +01:00
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "NetworkUser.h"
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
# include "../Context.h"
|
|
|
|
|
# include "../PlatformEnvironment.h"
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "../core/Console.hpp"
|
2022-01-08 18:38:09 +01:00
|
|
|
# include "../core/File.h"
|
2020-08-12 14:38:31 +01:00
|
|
|
# include "../core/Guard.hpp"
|
2020-10-02 11:31:20 +01:00
|
|
|
# include "../core/Json.hpp"
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "../core/Path.hpp"
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <unordered_set>
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
constexpr const utf8* USER_STORE_FILENAME = "users.json";
|
|
|
|
|
|
2020-08-12 14:38:31 +01:00
|
|
|
NetworkUser* NetworkUser::FromJson(json_t& jsonData)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
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"];
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUser* user = nullptr;
|
2020-08-12 14:38:31 +01:00
|
|
|
if (!hash.empty() && !name.empty())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2016-05-26 23:52:40 +01:00
|
|
|
user = new NetworkUser();
|
2020-08-12 14:38:31 +01:00
|
|
|
user->Hash = hash;
|
|
|
|
|
user->Name = name;
|
|
|
|
|
if (jsonGroupId.is_number_integer())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
user->GroupId = Json::GetNumber<uint8_t>(jsonGroupId);
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
2016-05-27 19:23:51 +01:00
|
|
|
user->Remove = false;
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 14:38:31 +01:00
|
|
|
json_t NetworkUser::ToJson() const
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
json_t jsonData;
|
|
|
|
|
jsonData["hash"] = Hash;
|
|
|
|
|
jsonData["name"] = Name;
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2020-08-12 14:38:31 +01:00
|
|
|
json_t jsonGroupId;
|
2021-09-12 15:44:28 +03:00
|
|
|
if (GroupId.has_value())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2021-09-12 15:44:28 +03:00
|
|
|
jsonGroupId = *GroupId;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2020-08-12 14:38:31 +01:00
|
|
|
jsonData["groupId"] = jsonGroupId;
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2020-08-12 14:38:31 +01:00
|
|
|
return jsonData;
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkUserManager::~NetworkUserManager()
|
|
|
|
|
{
|
|
|
|
|
DisposeUsers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkUserManager::DisposeUsers()
|
|
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
for (const auto& kvp : _usersByHash)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
|
|
|
|
delete kvp.second;
|
|
|
|
|
}
|
|
|
|
|
_usersByHash.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkUserManager::Load()
|
|
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
const auto path = GetStorePath();
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2022-01-08 18:38:09 +01:00
|
|
|
if (File::Exists(path))
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
|
|
|
|
DisposeUsers();
|
|
|
|
|
|
2016-08-16 21:24:03 +01:00
|
|
|
try
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
json_t jsonUsers = Json::ReadFromFile(path);
|
|
|
|
|
for (auto& jsonUser : jsonUsers)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
if (jsonUser.is_object())
|
2016-08-16 21:24:03 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
auto networkUser = NetworkUser::FromJson(jsonUser);
|
|
|
|
|
if (networkUser != nullptr)
|
|
|
|
|
{
|
|
|
|
|
_usersByHash[networkUser->Hash] = networkUser;
|
|
|
|
|
}
|
2016-08-16 21:24:03 +01:00
|
|
|
}
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
2016-08-16 21:24:03 +01:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception& ex)
|
2016-08-16 21:24:03 +01:00
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
Console::Error::WriteLine("Failed to read %s as JSON. %s", path.c_str(), ex.what());
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkUserManager::Save()
|
|
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
const auto path = GetStorePath();
|
2016-05-26 23:52:40 +01:00
|
|
|
|
2020-08-12 14:38:31 +01:00
|
|
|
json_t jsonUsers;
|
2016-05-26 23:52:40 +01:00
|
|
|
try
|
|
|
|
|
{
|
2022-01-08 18:38:09 +01:00
|
|
|
if (File::Exists(path))
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
|
|
|
|
jsonUsers = Json::ReadFromFile(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2016-11-30 23:32:47 +01:00
|
|
|
{
|
|
|
|
|
}
|
2016-05-26 23:52:40 +01:00
|
|
|
|
|
|
|
|
// Update existing users
|
|
|
|
|
std::unordered_set<std::string> savedHashes;
|
2020-08-12 14:38:31 +01:00
|
|
|
for (auto it = jsonUsers.begin(); it != jsonUsers.end();)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
json_t jsonUser = *it;
|
|
|
|
|
if (!jsonUser.is_object())
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
std::string hashString = Json::GetString(jsonUser["hash"]);
|
|
|
|
|
|
|
|
|
|
const auto networkUser = GetUserByHash(hashString);
|
|
|
|
|
if (networkUser != nullptr)
|
|
|
|
|
{
|
|
|
|
|
if (networkUser->Remove)
|
2016-05-27 19:23:51 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
it = jsonUsers.erase(it);
|
|
|
|
|
// erase advances the iterator so make sure we don't do it again
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
|
|
|
|
// replace the existing element in jsonUsers
|
|
|
|
|
*it = networkUser->ToJson();
|
|
|
|
|
savedHashes.insert(hashString);
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
2020-08-12 14:38:31 +01:00
|
|
|
|
|
|
|
|
it++;
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add new users
|
2018-06-22 23:02:47 +02:00
|
|
|
for (const auto& kvp : _usersByHash)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
const NetworkUser* networkUser = kvp.second;
|
2016-05-27 19:23:51 +01:00
|
|
|
if (!networkUser->Remove && savedHashes.find(networkUser->Hash) == savedHashes.end())
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2020-08-12 14:38:31 +01:00
|
|
|
jsonUsers.push_back(networkUser->ToJson());
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 14:38:31 +01:00
|
|
|
Json::WriteToFile(path, jsonUsers);
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void NetworkUserManager::UnsetUsersOfGroup(uint8_t groupId)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
for (const auto& kvp : _usersByHash)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUser* networkUser = kvp.second;
|
2021-09-12 15:44:28 +03:00
|
|
|
if (networkUser->GroupId.has_value() && *networkUser->GroupId == groupId)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2021-09-12 15:44:28 +03:00
|
|
|
networkUser->GroupId = std::nullopt;
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void NetworkUserManager::RemoveUser(const std::string& hash)
|
2016-05-27 19:23:51 +01:00
|
|
|
{
|
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;
|
|
|
|
|
}
|
2016-05-27 19:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUser* NetworkUserManager::GetUserByHash(const std::string& hash)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
|
|
|
|
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
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
|
|
|
|
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
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2021-01-12 06:14:15 +09:00
|
|
|
for (const auto& kvp : _usersByHash)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
const NetworkUser* networkUser = kvp.second;
|
2016-05-26 23:52:40 +01:00
|
|
|
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)
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUser* networkUser = GetUserByHash(hash);
|
2016-05-26 23:52:40 +01:00
|
|
|
if (networkUser == nullptr)
|
|
|
|
|
{
|
|
|
|
|
networkUser = new NetworkUser();
|
|
|
|
|
networkUser->Hash = hash;
|
|
|
|
|
_usersByHash[hash] = networkUser;
|
|
|
|
|
}
|
|
|
|
|
return networkUser;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string NetworkUserManager::GetStorePath()
|
2016-05-26 23:52:40 +01:00
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
|
|
|
|
|
return Path::Combine(env->GetDirectoryPath(OpenRCT2::DIRBASE::USER), USER_STORE_FILENAME);
|
2016-05-26 23:52:40 +01:00
|
|
|
}
|
2016-05-28 22:27:08 +01:00
|
|
|
|
|
|
|
|
#endif
|