2017-06-06 14:34:30 +01:00
|
|
|
/*****************************************************************************
|
2026-01-05 14:42:59 +01:00
|
|
|
* Copyright (c) 2014-2026 OpenRCT2 developers
|
2017-06-06 14:34:30 +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
|
2017-06-06 14:34:30 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2017-06-06 14:34:30 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2019-02-04 18:37:49 +00:00
|
|
|
#include <mutex>
|
2018-01-19 12:46:13 +00:00
|
|
|
#include <openrct2-ui/interface/Widget.h>
|
2025-01-19 15:50:54 +01:00
|
|
|
#include <openrct2-ui/windows/Windows.h>
|
2024-07-09 16:23:31 +02:00
|
|
|
#include <openrct2/Diagnostic.h>
|
2021-02-13 00:15:27 +00:00
|
|
|
#include <openrct2/core/Console.hpp>
|
2020-02-03 23:11:38 +00:00
|
|
|
#include <openrct2/core/Http.h>
|
2019-02-02 17:49:34 +00:00
|
|
|
#include <openrct2/core/Json.hpp>
|
|
|
|
|
#include <openrct2/core/String.hpp>
|
2026-01-13 23:41:59 +01:00
|
|
|
#include <openrct2/drawing/ColourMap.h>
|
2026-01-05 20:09:34 +01:00
|
|
|
#include <openrct2/drawing/Drawing.h>
|
2025-10-23 18:44:45 +02:00
|
|
|
#include <openrct2/drawing/Rectangle.h>
|
2021-12-11 18:06:06 -05:00
|
|
|
#include <openrct2/localisation/Formatter.h>
|
2023-01-17 21:56:30 +01:00
|
|
|
#include <openrct2/localisation/Formatting.h>
|
2024-07-12 14:59:00 +02:00
|
|
|
#include <openrct2/localisation/StringIds.h>
|
2018-02-08 20:34:05 +01:00
|
|
|
#include <openrct2/object/ObjectList.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/object/ObjectManager.h>
|
2019-02-02 17:49:34 +00:00
|
|
|
#include <openrct2/object/ObjectRepository.h>
|
2022-02-18 21:57:00 +01:00
|
|
|
#include <openrct2/platform/Platform.h>
|
2021-09-12 21:25:22 +02:00
|
|
|
#include <openrct2/ui/UiContext.h>
|
2025-01-19 10:58:28 +01:00
|
|
|
#include <openrct2/ui/WindowManager.h>
|
2019-02-03 22:42:25 +00:00
|
|
|
#include <openrct2/windows/Intent.h>
|
2021-09-12 21:25:22 +02:00
|
|
|
#include <sstream>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2024-07-12 14:59:00 +02:00
|
|
|
|
2025-10-21 12:20:00 +02:00
|
|
|
using namespace OpenRCT2::Drawing;
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
namespace OpenRCT2::Ui::Windows
|
|
|
|
|
{
|
2019-02-06 22:51:20 +00:00
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
|
|
2025-06-07 23:02:14 +02:00
|
|
|
// TODO: move to its own compilation unit
|
2024-03-10 13:52:35 +00:00
|
|
|
class ObjectDownloader
|
2019-08-13 18:32:32 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
private:
|
2025-06-16 23:10:47 +02:00
|
|
|
static constexpr auto kOpenRCT2ApiLegacyObjectURL = "https://api.openrct2.io/objects/legacy/";
|
2019-08-13 18:32:32 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
struct DownloadStatusInfo
|
2019-08-13 18:32:32 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
std::string Name;
|
|
|
|
|
std::string Source;
|
|
|
|
|
size_t Count{};
|
|
|
|
|
size_t Total{};
|
|
|
|
|
|
|
|
|
|
bool operator==(const DownloadStatusInfo& rhs) const
|
|
|
|
|
{
|
|
|
|
|
return Name == rhs.Name && Source == rhs.Source && Count == rhs.Count && Total == rhs.Total;
|
|
|
|
|
}
|
|
|
|
|
bool operator!=(const DownloadStatusInfo& rhs) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == rhs);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<ObjectEntryDescriptor> _entries;
|
|
|
|
|
std::vector<ObjectEntryDescriptor> _downloadedEntries;
|
|
|
|
|
size_t _currentDownloadIndex{};
|
|
|
|
|
std::mutex _downloadedEntriesMutex;
|
|
|
|
|
std::mutex _queueMutex;
|
|
|
|
|
bool _nextDownloadQueued{};
|
|
|
|
|
|
|
|
|
|
DownloadStatusInfo _lastDownloadStatusInfo;
|
|
|
|
|
DownloadStatusInfo _downloadStatusInfo;
|
|
|
|
|
std::mutex _downloadStatusInfoMutex;
|
|
|
|
|
std::string _lastDownloadSource;
|
|
|
|
|
|
|
|
|
|
// TODO static due to INTENT_EXTRA_CALLBACK not allowing a std::function
|
|
|
|
|
inline static bool _downloadingObjects;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void Begin(const std::vector<ObjectEntryDescriptor>& entries)
|
|
|
|
|
{
|
|
|
|
|
_lastDownloadStatusInfo = {};
|
|
|
|
|
_downloadStatusInfo = {};
|
|
|
|
|
_lastDownloadSource = {};
|
|
|
|
|
_entries = entries;
|
|
|
|
|
_currentDownloadIndex = 0;
|
|
|
|
|
_downloadingObjects = true;
|
|
|
|
|
QueueNextDownload();
|
2019-08-13 18:32:32 +01:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
bool IsDownloading() const
|
2019-08-13 18:32:32 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
return _downloadingObjects;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<ObjectEntryDescriptor> GetDownloadedEntries()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> guard(_downloadedEntriesMutex);
|
|
|
|
|
return _downloadedEntries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard guard(_queueMutex);
|
|
|
|
|
if (_nextDownloadQueued)
|
|
|
|
|
{
|
|
|
|
|
_nextDownloadQueued = false;
|
|
|
|
|
NextDownload();
|
|
|
|
|
}
|
|
|
|
|
UpdateStatusBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void UpdateStatusBox()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> guard(_downloadStatusInfoMutex);
|
|
|
|
|
if (_lastDownloadStatusInfo != _downloadStatusInfo)
|
|
|
|
|
{
|
|
|
|
|
_lastDownloadStatusInfo = _downloadStatusInfo;
|
|
|
|
|
|
|
|
|
|
if (_downloadStatusInfo == DownloadStatusInfo())
|
|
|
|
|
{
|
2025-09-17 23:18:52 +02:00
|
|
|
ContextForceCloseWindowByClass(WindowClass::networkStatus);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char str_downloading_objects[256]{};
|
|
|
|
|
Formatter ft;
|
|
|
|
|
if (_downloadStatusInfo.Source.empty())
|
|
|
|
|
{
|
|
|
|
|
ft.Add<int16_t>(static_cast<int16_t>(_downloadStatusInfo.Count));
|
|
|
|
|
ft.Add<int16_t>(static_cast<int16_t>(_downloadStatusInfo.Total));
|
|
|
|
|
ft.Add<char*>(_downloadStatusInfo.Name.c_str());
|
2026-01-16 04:38:20 -03:00
|
|
|
FormatStringLegacy(
|
2024-03-10 13:52:35 +00:00
|
|
|
str_downloading_objects, sizeof(str_downloading_objects), STR_DOWNLOADING_OBJECTS, ft.Data());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ft.Add<char*>(_downloadStatusInfo.Name.c_str());
|
|
|
|
|
ft.Add<char*>(_downloadStatusInfo.Source.c_str());
|
|
|
|
|
ft.Add<int16_t>(static_cast<int16_t>(_downloadStatusInfo.Count));
|
|
|
|
|
ft.Add<int16_t>(static_cast<int16_t>(_downloadStatusInfo.Total));
|
2026-01-16 04:38:20 -03:00
|
|
|
FormatStringLegacy(
|
2024-03-10 13:52:35 +00:00
|
|
|
str_downloading_objects, sizeof(str_downloading_objects), STR_DOWNLOADING_OBJECTS_FROM, ft.Data());
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 23:18:52 +02:00
|
|
|
auto intent = Intent(WindowClass::networkStatus);
|
2024-03-10 13:52:35 +00:00
|
|
|
intent.PutExtra(INTENT_EXTRA_MESSAGE, std::string(str_downloading_objects));
|
|
|
|
|
intent.PutExtra(INTENT_EXTRA_CALLBACK, []() -> void { _downloadingObjects = false; });
|
|
|
|
|
ContextOpenIntent(&intent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateProgress(const DownloadStatusInfo& info)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> guard(_downloadStatusInfoMutex);
|
|
|
|
|
_downloadStatusInfo = info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QueueNextDownload()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard guard(_queueMutex);
|
|
|
|
|
_nextDownloadQueued = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DownloadObject(const ObjectEntryDescriptor& entry, const std::string& name, const std::string& url)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Console::WriteLine("Downloading %s", url.c_str());
|
|
|
|
|
Http::Request req;
|
|
|
|
|
req.method = Http::Method::GET;
|
|
|
|
|
req.url = url;
|
|
|
|
|
Http::DoAsync(req, [this, entry, name](Http::Response response) {
|
|
|
|
|
if (response.status == Http::Status::Ok)
|
|
|
|
|
{
|
|
|
|
|
// Check that download operation hasn't been cancelled
|
|
|
|
|
if (_downloadingObjects)
|
|
|
|
|
{
|
|
|
|
|
auto data = reinterpret_cast<uint8_t*>(response.body.data());
|
|
|
|
|
auto dataLen = response.body.size();
|
|
|
|
|
|
2026-01-16 04:38:20 -03:00
|
|
|
auto& objRepo = GetContext()->GetObjectRepository();
|
2024-03-10 13:52:35 +00:00
|
|
|
objRepo.AddObjectFromFile(ObjectGeneration::DAT, name, data, dataLen);
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(_downloadedEntriesMutex);
|
|
|
|
|
_downloadedEntries.push_back(entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine(" Failed to download %s", name.c_str());
|
|
|
|
|
}
|
|
|
|
|
QueueNextDownload();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception&)
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine(" Failed to download %s", name.c_str());
|
|
|
|
|
QueueNextDownload();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NextDownload()
|
|
|
|
|
{
|
|
|
|
|
if (!_downloadingObjects || _currentDownloadIndex >= _entries.size())
|
|
|
|
|
{
|
|
|
|
|
// Finished...
|
|
|
|
|
_downloadingObjects = false;
|
|
|
|
|
UpdateProgress({});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& entry = _entries[_currentDownloadIndex];
|
2024-12-11 22:28:31 +01:00
|
|
|
auto name = String::trim(std::string(entry.GetName()));
|
2024-03-10 13:52:35 +00:00
|
|
|
LOG_VERBOSE("Downloading object: [%s]:", name.c_str());
|
|
|
|
|
_currentDownloadIndex++;
|
|
|
|
|
UpdateProgress({ name, _lastDownloadSource, _currentDownloadIndex, _entries.size() });
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Http::Request req;
|
|
|
|
|
req.method = Http::Method::GET;
|
2025-06-16 23:10:47 +02:00
|
|
|
req.url = kOpenRCT2ApiLegacyObjectURL + name;
|
2024-03-10 13:52:35 +00:00
|
|
|
Http::DoAsync(req, [this, entry, name](Http::Response response) {
|
|
|
|
|
if (response.status == Http::Status::Ok)
|
|
|
|
|
{
|
|
|
|
|
auto jresponse = Json::FromString(response.body);
|
|
|
|
|
if (jresponse.is_object())
|
|
|
|
|
{
|
|
|
|
|
auto objName = Json::GetString(jresponse["name"]);
|
|
|
|
|
auto source = Json::GetString(jresponse["source"]);
|
|
|
|
|
auto downloadLink = Json::GetString(jresponse["download"]);
|
|
|
|
|
if (!downloadLink.empty())
|
|
|
|
|
{
|
|
|
|
|
_lastDownloadSource = source;
|
|
|
|
|
UpdateProgress({ name, source, _currentDownloadIndex, _entries.size() });
|
|
|
|
|
DownloadObject(entry, objName, downloadLink);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (response.status == Http::Status::NotFound)
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine(" %s not found", name.c_str());
|
|
|
|
|
QueueNextDownload();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine(
|
|
|
|
|
" %s query failed (status %d)", name.c_str(), static_cast<int32_t>(response.status));
|
|
|
|
|
QueueNextDownload();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception&)
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine(" Failed to query %s", name.c_str());
|
|
|
|
|
}
|
2019-08-13 18:32:32 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-06 22:51:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
2024-09-04 21:11:26 +02:00
|
|
|
enum WindowObjectLoadErrorWidgetIdx
|
|
|
|
|
{
|
|
|
|
|
WIDX_BACKGROUND,
|
|
|
|
|
WIDX_TITLE,
|
|
|
|
|
WIDX_CLOSE,
|
|
|
|
|
WIDX_COLUMN_OBJECT_NAME,
|
|
|
|
|
WIDX_COLUMN_OBJECT_SOURCE,
|
|
|
|
|
WIDX_COLUMN_OBJECT_TYPE,
|
|
|
|
|
WIDX_SCROLL,
|
|
|
|
|
WIDX_COPY_CURRENT,
|
|
|
|
|
WIDX_COPY_ALL,
|
|
|
|
|
WIDX_DOWNLOAD_ALL
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-16 23:10:47 +02:00
|
|
|
static constexpr StringId kWindowTitle = STR_OBJECT_LOAD_ERROR_TITLE;
|
|
|
|
|
static constexpr ScreenSize kWindowSize = { 450, 400 };
|
|
|
|
|
static constexpr int32_t kWindowWidthLessPadding = kWindowSize.width - 5;
|
|
|
|
|
constexpr int32_t kNameColLeft = 4;
|
|
|
|
|
constexpr int32_t kSourceColLeft = (kWindowWidthLessPadding / 4) + 1;
|
|
|
|
|
constexpr int32_t kTypeColLeft = 5 * kWindowWidthLessPadding / 8 + 1;
|
2024-09-04 21:11:26 +02:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
// clang-format off
|
2025-06-07 23:02:14 +02:00
|
|
|
static constexpr auto window_object_load_error_widgets = makeWidgets(
|
2025-06-16 23:10:47 +02:00
|
|
|
makeWindowShim(kWindowTitle, kWindowSize),
|
|
|
|
|
makeWidget({ kNameColLeft, 57}, {108, 14}, WidgetType::tableHeader, WindowColour::primary, STR_OBJECT_NAME ), // 'Object name' header
|
|
|
|
|
makeWidget({kSourceColLeft, 57}, {166, 14}, WidgetType::tableHeader, WindowColour::primary, STR_OBJECT_SOURCE ), // 'Object source' header
|
|
|
|
|
makeWidget({ kTypeColLeft, 57}, {166, 14}, WidgetType::tableHeader, WindowColour::primary, STR_OBJECT_TYPE ), // 'Object type' header
|
|
|
|
|
makeWidget({ kNameColLeft, 70}, {442, 298}, WidgetType::scroll, WindowColour::primary, SCROLL_VERTICAL ), // Scrollable list area
|
|
|
|
|
makeWidget({ kNameColLeft, 377}, {145, 14}, WidgetType::button, WindowColour::primary, STR_COPY_SELECTED, STR_COPY_SELECTED_TIP), // Copy selected button
|
|
|
|
|
makeWidget({ 152, 377}, {145, 14}, WidgetType::button, WindowColour::primary, STR_COPY_ALL, STR_COPY_ALL_TIP ) // Copy all button
|
2024-09-04 21:11:26 +02:00
|
|
|
#ifndef DISABLE_HTTP
|
2025-06-16 23:10:47 +02:00
|
|
|
, makeWidget({ 300, 377}, {146, 14}, WidgetType::button, WindowColour::primary, STR_DOWNLOAD_ALL, STR_DOWNLOAD_ALL_TIP ) // Download all button
|
2024-09-04 21:11:26 +02:00
|
|
|
#endif
|
2025-06-07 23:02:14 +02:00
|
|
|
);
|
2024-03-10 13:52:35 +00:00
|
|
|
// clang-format on
|
2021-10-14 16:53:48 +01:00
|
|
|
|
|
|
|
|
/**
|
2024-03-10 13:52:35 +00:00
|
|
|
* Returns an StringId that represents an RCTObjectEntry's type.
|
|
|
|
|
*
|
|
|
|
|
* Could possibly be moved out of the window file if other
|
|
|
|
|
* uses exist and a suitable location is found.
|
2021-10-14 16:53:48 +01:00
|
|
|
*/
|
2024-03-10 13:52:35 +00:00
|
|
|
static constexpr StringId GetStringFromObjectType(const ObjectType type)
|
2021-10-14 16:53:48 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
switch (type)
|
2021-10-14 16:53:48 +01:00
|
|
|
{
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::ride:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_RIDE_VEHICLES_ATTRACTIONS;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::smallScenery:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_SMALL_SCENERY;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::largeScenery:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_LARGE_SCENERY;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::walls:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_WALLS_FENCES;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::banners:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_PATH_SIGNS;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::paths:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_FOOTPATHS;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::pathAdditions:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_PATH_EXTRAS;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::sceneryGroup:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_SCENERY_GROUPS;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::parkEntrance:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_PARK_ENTRANCE;
|
2025-02-13 23:07:55 +01:00
|
|
|
case ObjectType::water:
|
2024-03-10 13:52:35 +00:00
|
|
|
return STR_OBJECT_SELECTION_WATER;
|
|
|
|
|
default:
|
|
|
|
|
return STR_UNKNOWN_OBJECT_TYPE;
|
2021-10-14 16:53:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-22 02:51:41 +01:00
|
|
|
class ObjectLoadErrorWindow final : public Window
|
2021-10-14 16:53:48 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
private:
|
|
|
|
|
std::vector<ObjectEntryDescriptor> _invalidEntries;
|
|
|
|
|
int32_t _highlightedIndex = -1;
|
|
|
|
|
std::string _filePath;
|
2021-10-14 16:53:48 +01:00
|
|
|
#ifndef DISABLE_HTTP
|
2024-03-10 13:52:35 +00:00
|
|
|
ObjectDownloader _objDownloader;
|
|
|
|
|
bool _updatedListAfterDownload{};
|
2021-10-14 16:53:48 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void DownloadAllObjects()
|
2021-10-14 16:53:48 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
if (!_objDownloader.IsDownloading())
|
|
|
|
|
{
|
|
|
|
|
_updatedListAfterDownload = false;
|
|
|
|
|
_objDownloader.Begin(_invalidEntries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateObjectList()
|
|
|
|
|
{
|
|
|
|
|
const auto entries = _objDownloader.GetDownloadedEntries();
|
|
|
|
|
for (auto& de : entries)
|
|
|
|
|
{
|
|
|
|
|
_invalidEntries.erase(
|
|
|
|
|
std::remove_if(
|
|
|
|
|
_invalidEntries.begin(), _invalidEntries.end(),
|
|
|
|
|
[de](const ObjectEntryDescriptor& e) { return de.GetName() == e.GetName(); }),
|
|
|
|
|
_invalidEntries.end());
|
|
|
|
|
}
|
2025-09-17 19:03:29 +02:00
|
|
|
numListItems = static_cast<uint16_t>(_invalidEntries.size());
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a newline-separated string listing all object names.
|
|
|
|
|
* Used for placing all names on the clipboard.
|
|
|
|
|
*/
|
|
|
|
|
void CopyObjectNamesToClipboard()
|
|
|
|
|
{
|
|
|
|
|
std::stringstream stream;
|
2025-09-17 19:03:29 +02:00
|
|
|
for (uint16_t i = 0; i < numListItems; i++)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
const auto& entry = _invalidEntries[i];
|
|
|
|
|
stream << entry.GetName();
|
|
|
|
|
stream << PLATFORM_NEWLINE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto clip = stream.str();
|
2026-01-16 04:38:20 -03:00
|
|
|
GetContext()->GetUiContext().SetClipboardText(clip.c_str());
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SelectObjectFromList(const int32_t index)
|
|
|
|
|
{
|
2025-09-17 19:03:29 +02:00
|
|
|
if (index < 0 || index > numListItems)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-09-17 19:03:29 +02:00
|
|
|
selectedListItem = -1;
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-17 19:03:29 +02:00
|
|
|
selectedListItem = index;
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
2025-09-17 20:10:06 +02:00
|
|
|
invalidateWidget(WIDX_SCROLL);
|
2021-10-14 16:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
public:
|
2025-09-17 20:10:06 +02:00
|
|
|
void onOpen() override
|
2021-10-14 16:53:48 +01:00
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
setWidgets(window_object_load_error_widgets);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
WindowInitScrollWidgets(*this);
|
2026-01-28 10:06:45 +01:00
|
|
|
colours[0] = Drawing::Colour::lightBlue;
|
|
|
|
|
colours[1] = Drawing::Colour::lightBlue;
|
|
|
|
|
colours[2] = Drawing::Colour::lightBlue;
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onClose() override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
_invalidEntries.clear();
|
|
|
|
|
_invalidEntries.shrink_to_fit();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onMouseUp(const WidgetIndex widgetIndex) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
switch (widgetIndex)
|
|
|
|
|
{
|
|
|
|
|
case WIDX_CLOSE:
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-03-10 13:52:35 +00:00
|
|
|
return;
|
|
|
|
|
case WIDX_COPY_CURRENT:
|
2025-09-17 19:03:29 +02:00
|
|
|
if (selectedListItem > -1 && selectedListItem < numListItems)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-09-17 19:03:29 +02:00
|
|
|
const auto name = std::string(_invalidEntries[selectedListItem].GetName());
|
2026-01-16 04:38:20 -03:00
|
|
|
GetContext()->GetUiContext().SetClipboardText(name.c_str());
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case WIDX_COPY_ALL:
|
|
|
|
|
CopyObjectNamesToClipboard();
|
|
|
|
|
break;
|
|
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
|
case WIDX_DOWNLOAD_ALL:
|
|
|
|
|
DownloadAllObjects();
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onUpdate() override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-09-17 19:03:29 +02:00
|
|
|
currentFrame++;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
// Check if the mouse is hovering over the list
|
2025-06-10 21:57:53 +02:00
|
|
|
if (!widgetIsHighlighted(*this, WIDX_SCROLL))
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
_highlightedIndex = -1;
|
2025-09-17 20:10:06 +02:00
|
|
|
invalidateWidget(WIDX_SCROLL);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
|
_objDownloader.Update();
|
|
|
|
|
|
|
|
|
|
// Remove downloaded objects from our invalid entry list
|
|
|
|
|
if (_objDownloader.IsDownloading())
|
|
|
|
|
{
|
|
|
|
|
// Don't do this too often as it isn't particularly efficient
|
2025-09-17 19:03:29 +02:00
|
|
|
if (currentFrame % 64 == 0)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
UpdateObjectList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!_updatedListAfterDownload)
|
2021-10-14 16:53:48 +01:00
|
|
|
{
|
|
|
|
|
UpdateObjectList();
|
2024-03-10 13:52:35 +00:00
|
|
|
_updatedListAfterDownload = true;
|
2021-10-14 16:53:48 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
ScreenSize onScrollGetSize(const int32_t scrollIndex) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-09-17 19:03:29 +02:00
|
|
|
return ScreenSize(0, numListItems * kScrollableRowHeight);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onScrollMouseDown(const int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2024-05-09 13:59:03 +02:00
|
|
|
const auto selectedItem = screenCoords.y / kScrollableRowHeight;
|
2024-03-10 13:52:35 +00:00
|
|
|
SelectObjectFromList(selectedItem);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onScrollMouseOver(const int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
// Highlight item that the cursor is over, or remove highlighting if none
|
2024-05-09 13:59:03 +02:00
|
|
|
const auto selectedItem = screenCoords.y / kScrollableRowHeight;
|
2025-09-17 19:03:29 +02:00
|
|
|
if (selectedItem < 0 || selectedItem >= numListItems)
|
2024-03-10 13:52:35 +00:00
|
|
|
_highlightedIndex = -1;
|
|
|
|
|
else
|
|
|
|
|
_highlightedIndex = selectedItem;
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
invalidateWidget(WIDX_SCROLL);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 04:38:20 -03:00
|
|
|
void onDraw(RenderTarget& rt) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
WindowDrawWidgets(*this, rt);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-05-19 22:15:32 +02:00
|
|
|
auto screenPos = windowPos + ScreenCoordsXY{ 5, widgets[WIDX_TITLE].bottom };
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
// Draw explanatory message
|
|
|
|
|
auto ft = Formatter();
|
|
|
|
|
ft.Add<StringId>(STR_OBJECT_ERROR_WINDOW_EXPLANATION);
|
2025-06-16 23:10:47 +02:00
|
|
|
DrawTextWrapped(rt, screenPos + ScreenCoordsXY{ 0, 4 }, kWindowSize.width - 10, STR_BLACK_STRING, ft);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
// Draw file name
|
|
|
|
|
ft = Formatter();
|
|
|
|
|
ft.Add<StringId>(STR_OBJECT_ERROR_WINDOW_FILE);
|
|
|
|
|
ft.Add<utf8*>(_filePath.c_str());
|
2025-06-16 23:10:47 +02:00
|
|
|
DrawTextEllipsised(rt, screenPos + ScreenCoordsXY{ 0, 29 }, kWindowSize.width - 5, STR_BLACK_STRING, ft);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 04:38:20 -03:00
|
|
|
void onScrollDraw(const int32_t scrollIndex, RenderTarget& rt) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
auto rtCoords = ScreenCoordsXY{ rt.x, rt.y };
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::fill(
|
2025-05-05 01:06:46 +03:00
|
|
|
rt, { rtCoords, rtCoords + ScreenCoordsXY{ rt.width - 1, rt.height - 1 } },
|
2026-01-13 23:41:59 +01:00
|
|
|
getColourMap(colours[1].colour).midLight);
|
2025-11-08 10:27:58 +01:00
|
|
|
const int32_t listWidth = widgets[WIDX_SCROLL].width() - 1;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-09-17 19:03:29 +02:00
|
|
|
for (int32_t i = 0; i < numListItems; i++)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
ScreenCoordsXY screenCoords;
|
2024-05-09 13:59:03 +02:00
|
|
|
screenCoords.y = i * kScrollableRowHeight;
|
2025-05-05 01:06:46 +03:00
|
|
|
if (screenCoords.y > rt.y + rt.height)
|
2024-03-10 13:52:35 +00:00
|
|
|
break;
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
if (screenCoords.y + kScrollableRowHeight < rt.y)
|
2024-03-10 13:52:35 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
const auto screenRect = ScreenRect{ { 0, screenCoords.y },
|
2024-05-09 13:59:03 +02:00
|
|
|
{ listWidth, screenCoords.y + kScrollableRowHeight - 1 } };
|
2024-03-10 13:52:35 +00:00
|
|
|
// If hovering over item, change the color and fill the backdrop.
|
2025-09-17 19:03:29 +02:00
|
|
|
if (i == selectedListItem)
|
2026-01-13 23:41:59 +01:00
|
|
|
Rectangle::fill(rt, screenRect, getColourMap(colours[1].colour).darker);
|
2024-03-10 13:52:35 +00:00
|
|
|
else if (i == _highlightedIndex)
|
2026-01-13 23:41:59 +01:00
|
|
|
Rectangle::fill(rt, screenRect, getColourMap(colours[1].colour).midDark);
|
2024-03-10 13:52:35 +00:00
|
|
|
else if ((i & 1) != 0) // odd / even check
|
2026-01-13 23:41:59 +01:00
|
|
|
Rectangle::fill(rt, screenRect, getColourMap(colours[1].colour).light);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
// Draw the actual object entry's name...
|
2025-06-16 23:10:47 +02:00
|
|
|
screenCoords.x = kNameColLeft - 3;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
const auto& entry = _invalidEntries[i];
|
|
|
|
|
|
2026-03-22 18:58:24 +01:00
|
|
|
DrawText(rt, screenCoords, entry.GetName(), { Colour::darkGreen });
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
if (entry.Generation == ObjectGeneration::DAT)
|
|
|
|
|
{
|
|
|
|
|
// ... source game ...
|
|
|
|
|
const auto sourceStringId = ObjectManagerGetSourceGameString(entry.Entry.GetSourceGame());
|
2026-03-22 18:58:24 +01:00
|
|
|
DrawText(rt, { kSourceColLeft - 3, screenCoords.y }, sourceStringId, {}, { Drawing::Colour::darkGreen });
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ... and type
|
|
|
|
|
const auto type = GetStringFromObjectType(entry.GetType());
|
2026-03-22 18:58:24 +01:00
|
|
|
DrawText(rt, { kTypeColLeft - 3, screenCoords.y }, type, {}, { Drawing::Colour::darkGreen });
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void initialise(utf8* path, const size_t numMissingObjects, const ObjectEntryDescriptor* missingObjects)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
_invalidEntries = std::vector<ObjectEntryDescriptor>(missingObjects, missingObjects + numMissingObjects);
|
|
|
|
|
|
|
|
|
|
// Refresh list items and path
|
2025-09-17 19:03:29 +02:00
|
|
|
numListItems = static_cast<uint16_t>(numMissingObjects);
|
2024-03-10 13:52:35 +00:00
|
|
|
_filePath = path;
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
invalidate();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-17 02:37:46 -04:00
|
|
|
WindowBase* ObjectLoadErrorOpen(utf8* path, size_t numMissingObjects, const ObjectEntryDescriptor* missingObjects)
|
2023-05-19 10:17:38 +02:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
// Check if window is already open
|
2026-01-16 04:38:20 -03:00
|
|
|
auto* windowMgr = GetWindowManager();
|
2025-09-17 23:18:52 +02:00
|
|
|
auto* window = windowMgr->BringToFrontByClass(WindowClass::objectLoadError);
|
2024-03-10 13:52:35 +00:00
|
|
|
if (window == nullptr)
|
|
|
|
|
{
|
2025-09-20 21:46:42 +02:00
|
|
|
window = windowMgr->Create<ObjectLoadErrorWindow>(WindowClass::objectLoadError, kWindowSize, {});
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
static_cast<ObjectLoadErrorWindow*>(window)->initialise(path, numMissingObjects, missingObjects);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
return window;
|
2023-05-19 10:17:38 +02:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
} // namespace OpenRCT2::Ui::Windows
|