2015-07-02 10:37:13 +00:00
|
|
|
// Copyright (c) 2014- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2016-10-12 09:32:24 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/UI/Screen.h"
|
|
|
|
#include "Common/UI/Context.h"
|
|
|
|
#include "Common/UI/ViewGroup.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/Render/DrawBuffer.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2020-09-29 10:44:47 +00:00
|
|
|
#include "Common/Common.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
#include "Common/Log.h"
|
2020-10-04 07:29:36 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
|
|
|
#include "Common/Data/Format/JSONReader.h"
|
2015-07-02 10:37:13 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
#include "Core/Config.h"
|
2015-07-04 16:05:17 +00:00
|
|
|
#include "Core/System.h"
|
2016-03-06 21:16:50 +00:00
|
|
|
#include "Core/Util/GameManager.h"
|
2015-07-04 16:05:17 +00:00
|
|
|
#include "UI/EmuScreen.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
#include "UI/Store.h"
|
2016-12-27 21:26:49 +00:00
|
|
|
#include "UI/TextureUtil.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
|
|
|
|
const std::string storeBaseUrl = "http://store.ppsspp.org/";
|
|
|
|
|
2015-07-02 10:37:13 +00:00
|
|
|
// baseUrl is assumed to have a trailing slash, and not contain any subdirectories.
|
|
|
|
std::string ResolveUrl(std::string baseUrl, std::string url) {
|
|
|
|
if (url.empty()) {
|
|
|
|
return baseUrl;
|
|
|
|
} else if (url[0] == '/') {
|
|
|
|
return baseUrl + url.substr(1);
|
2019-10-06 15:30:39 +00:00
|
|
|
} else if (startsWith(url, "http://") || startsWith(url, "https://")) {
|
2015-07-02 10:37:13 +00:00
|
|
|
return url;
|
|
|
|
} else {
|
|
|
|
// Huh.
|
|
|
|
return baseUrl + url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class HttpImageFileView : public UI::View {
|
|
|
|
public:
|
|
|
|
HttpImageFileView(http::Downloader *downloader, const std::string &path, UI::ImageSizeMode sizeMode = UI::IS_DEFAULT, UI::LayoutParams *layoutParams = 0)
|
2017-05-18 10:52:03 +00:00
|
|
|
: UI::View(layoutParams), path_(path), sizeMode_(sizeMode), downloader_(downloader) {}
|
2015-07-02 10:37:13 +00:00
|
|
|
|
|
|
|
~HttpImageFileView() {
|
2015-10-06 17:07:09 +00:00
|
|
|
if (download_)
|
|
|
|
download_->Cancel();
|
2015-07-02 10:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
|
|
|
|
void Draw(UIContext &dc) override;
|
2021-02-22 00:38:02 +00:00
|
|
|
std::string DescribeText() const override { return ""; }
|
2015-07-02 10:37:13 +00:00
|
|
|
|
|
|
|
void SetFilename(std::string filename);
|
|
|
|
void SetColor(uint32_t color) { color_ = color; }
|
|
|
|
void SetFixedSize(float fixW, float fixH) { fixedSizeW_ = fixW; fixedSizeH_ = fixH; }
|
|
|
|
void SetCanBeFocused(bool can) { canFocus_ = can; }
|
|
|
|
|
|
|
|
bool CanBeFocused() const override { return false; }
|
|
|
|
|
|
|
|
const std::string &GetFilename() const { return path_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DownloadCompletedCallback(http::Download &download);
|
|
|
|
|
2017-05-18 10:52:03 +00:00
|
|
|
bool canFocus_ = false;
|
2015-07-02 10:37:13 +00:00
|
|
|
std::string path_;
|
2017-05-18 10:52:03 +00:00
|
|
|
uint32_t color_ = 0xFFFFFFFF;
|
2015-07-02 10:37:13 +00:00
|
|
|
UI::ImageSizeMode sizeMode_;
|
|
|
|
http::Downloader *downloader_;
|
|
|
|
std::shared_ptr<http::Download> download_;
|
|
|
|
|
|
|
|
std::string textureData_;
|
2017-05-18 10:52:03 +00:00
|
|
|
std::unique_ptr<ManagedTexture> texture_;
|
|
|
|
bool textureFailed_ = false;
|
|
|
|
float fixedSizeW_ = 0.0f;
|
|
|
|
float fixedSizeH_ = 0.0f;
|
2015-07-02 10:37:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void HttpImageFileView::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
|
|
|
|
switch (sizeMode_) {
|
|
|
|
case UI::IS_FIXED:
|
|
|
|
w = fixedSizeW_;
|
|
|
|
h = fixedSizeH_;
|
|
|
|
break;
|
|
|
|
case UI::IS_DEFAULT:
|
|
|
|
default:
|
|
|
|
if (texture_) {
|
|
|
|
float texw = (float)texture_->Width();
|
|
|
|
float texh = (float)texture_->Height();
|
|
|
|
w = texw;
|
|
|
|
h = texh;
|
|
|
|
} else {
|
|
|
|
w = 16;
|
|
|
|
h = 16;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpImageFileView::SetFilename(std::string filename) {
|
|
|
|
if (path_ != filename) {
|
|
|
|
textureFailed_ = false;
|
|
|
|
path_ = filename;
|
2017-05-18 10:52:03 +00:00
|
|
|
texture_.reset(nullptr);
|
2015-07-02 10:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpImageFileView::DownloadCompletedCallback(http::Download &download) {
|
2015-10-06 17:07:09 +00:00
|
|
|
if (download.IsCancelled()) {
|
|
|
|
// We were probably destroyed. Can't touch "this" (heh).
|
|
|
|
return;
|
|
|
|
}
|
2015-07-02 10:37:13 +00:00
|
|
|
if (download.ResultCode() == 200) {
|
|
|
|
download.buffer().TakeAll(&textureData_);
|
|
|
|
} else {
|
|
|
|
textureFailed_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpImageFileView::Draw(UIContext &dc) {
|
2016-12-25 17:18:19 +00:00
|
|
|
using namespace Draw;
|
2015-07-02 10:37:13 +00:00
|
|
|
if (!texture_ && !textureFailed_ && !path_.empty() && !download_) {
|
2021-08-22 15:29:48 +00:00
|
|
|
auto cb = std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1);
|
|
|
|
const char *acceptMime = "image/png, image/jpeg, image/*; q=0.9, */*; q=0.8";
|
|
|
|
download_ = downloader_->StartDownloadWithCallback(path_, Path(), cb, acceptMime);
|
2015-07-02 10:37:13 +00:00
|
|
|
download_->SetHidden(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!textureData_.empty()) {
|
2020-08-09 07:35:56 +00:00
|
|
|
texture_ = CreateTextureFromFileData(dc.GetDrawContext(), (const uint8_t *)(textureData_.data()), (int)textureData_.size(), DETECT, false, "store_icon");
|
2015-07-02 10:37:13 +00:00
|
|
|
if (!texture_)
|
|
|
|
textureFailed_ = true;
|
|
|
|
textureData_.clear();
|
2015-07-02 15:59:47 +00:00
|
|
|
download_.reset();
|
2015-07-02 10:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasFocus()) {
|
|
|
|
dc.FillRect(dc.theme->itemFocusedStyle.background, bounds_.Expand(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: involve sizemode
|
|
|
|
if (texture_) {
|
2017-12-10 17:30:31 +00:00
|
|
|
float tw = texture_->Width();
|
|
|
|
float th = texture_->Height();
|
|
|
|
|
|
|
|
float x = bounds_.x;
|
|
|
|
float y = bounds_.y;
|
|
|
|
float w = bounds_.w;
|
|
|
|
float h = bounds_.h;
|
|
|
|
|
|
|
|
if (tw / th < w / h) {
|
|
|
|
float nw = h * tw / th;
|
|
|
|
x += (w - nw) / 2.0f;
|
|
|
|
w = nw;
|
|
|
|
} else {
|
|
|
|
float nh = w * th / tw;
|
|
|
|
y += (h - nh) / 2.0f;
|
|
|
|
h = nh;
|
|
|
|
}
|
|
|
|
|
2015-07-02 10:37:13 +00:00
|
|
|
dc.Flush();
|
2017-01-30 13:33:38 +00:00
|
|
|
dc.GetDrawContext()->BindTexture(0, texture_->GetTexture());
|
2017-12-10 17:30:31 +00:00
|
|
|
dc.Draw()->Rect(x, y, w, h, color_);
|
2015-07-02 10:37:13 +00:00
|
|
|
dc.Flush();
|
|
|
|
dc.RebindTexture();
|
|
|
|
} else {
|
|
|
|
// draw a black rectangle to represent the missing image.
|
2017-12-10 17:30:31 +00:00
|
|
|
dc.FillRect(UI::Drawable(0x7F000000), GetBounds());
|
2015-07-02 10:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
// This is the entry in a list. Does not have install buttons and so on.
|
2017-12-10 08:44:44 +00:00
|
|
|
class ProductItemView : public UI::StickyChoice {
|
2013-11-20 13:42:48 +00:00
|
|
|
public:
|
|
|
|
ProductItemView(const StoreEntry &entry, UI::LayoutParams *layoutParams = 0)
|
2017-12-10 08:44:44 +00:00
|
|
|
: UI::StickyChoice(entry.name, "", layoutParams), entry_(entry) {}
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2017-03-15 05:01:18 +00:00
|
|
|
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override {
|
2013-11-20 13:42:48 +00:00
|
|
|
w = 300;
|
|
|
|
h = 164;
|
|
|
|
}
|
|
|
|
|
|
|
|
StoreEntry GetEntry() const { return entry_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const StoreEntry &entry_;
|
|
|
|
};
|
|
|
|
|
2015-07-04 15:01:32 +00:00
|
|
|
// This is a "details" view of a game. Lets you install it.
|
2013-11-20 13:42:48 +00:00
|
|
|
class ProductView : public UI::LinearLayout {
|
|
|
|
public:
|
2015-07-04 15:40:40 +00:00
|
|
|
ProductView(const StoreEntry &entry)
|
2017-03-06 14:43:38 +00:00
|
|
|
: LinearLayout(UI::ORIENT_VERTICAL), entry_(entry) {
|
2013-11-20 13:42:48 +00:00
|
|
|
CreateViews();
|
|
|
|
}
|
|
|
|
|
2017-03-15 05:01:18 +00:00
|
|
|
void Update() override;
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2015-07-04 16:05:17 +00:00
|
|
|
UI::Event OnClickLaunch;
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
private:
|
|
|
|
void CreateViews();
|
|
|
|
UI::EventReturn OnInstall(UI::EventParams &e);
|
2017-03-06 14:43:38 +00:00
|
|
|
UI::EventReturn OnCancel(UI::EventParams &e);
|
2013-11-20 13:42:48 +00:00
|
|
|
UI::EventReturn OnUninstall(UI::EventParams &e);
|
2015-07-04 16:05:17 +00:00
|
|
|
UI::EventReturn OnLaunchClick(UI::EventParams &e);
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2015-07-04 15:40:40 +00:00
|
|
|
bool IsGameInstalled() {
|
|
|
|
return g_GameManager.IsGameInstalled(entry_.file);
|
|
|
|
}
|
2021-05-01 17:49:44 +00:00
|
|
|
std::string DownloadURL();
|
2015-07-04 15:40:40 +00:00
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
StoreEntry entry_;
|
2017-03-06 14:43:38 +00:00
|
|
|
UI::Button *installButton_ = nullptr;
|
2017-05-18 12:21:13 +00:00
|
|
|
UI::Button *launchButton_ = nullptr;
|
2017-03-06 14:43:38 +00:00
|
|
|
UI::Button *cancelButton_ = nullptr;
|
2021-05-01 17:40:34 +00:00
|
|
|
UI::TextView *speedView_ = nullptr;
|
2017-03-06 14:43:38 +00:00
|
|
|
bool wasInstalled_ = false;
|
2013-11-20 13:42:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void ProductView::CreateViews() {
|
|
|
|
using namespace UI;
|
|
|
|
Clear();
|
|
|
|
|
2015-07-02 10:37:13 +00:00
|
|
|
if (!entry_.iconURL.empty()) {
|
|
|
|
Add(new HttpImageFileView(&g_DownloadManager, ResolveUrl(storeBaseUrl, entry_.iconURL), IS_FIXED))->SetFixedSize(144, 88);
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
Add(new TextView(entry_.name));
|
|
|
|
Add(new TextView(entry_.author));
|
|
|
|
|
2020-01-26 18:43:18 +00:00
|
|
|
auto st = GetI18NCategory("Store");
|
|
|
|
auto di = GetI18NCategory("Dialog");
|
2015-07-04 15:40:40 +00:00
|
|
|
wasInstalled_ = IsGameInstalled();
|
2021-05-01 17:49:44 +00:00
|
|
|
bool isDownloading = g_GameManager.IsDownloading(DownloadURL());
|
2015-07-04 15:40:40 +00:00
|
|
|
if (!wasInstalled_) {
|
2020-05-17 12:44:11 +00:00
|
|
|
launchButton_ = nullptr;
|
2021-05-01 17:40:34 +00:00
|
|
|
LinearLayout *progressDisplay = new LinearLayout(ORIENT_HORIZONTAL);
|
|
|
|
installButton_ = progressDisplay->Add(new Button(st->T("Install")));
|
2013-11-20 18:51:54 +00:00
|
|
|
installButton_->OnClick.Handle(this, &ProductView::OnInstall);
|
2021-05-01 17:40:34 +00:00
|
|
|
|
|
|
|
speedView_ = progressDisplay->Add(new TextView(""));
|
2021-05-01 17:49:44 +00:00
|
|
|
speedView_->SetVisibility(isDownloading ? V_VISIBLE : V_GONE);
|
2021-05-01 17:40:34 +00:00
|
|
|
Add(progressDisplay);
|
2013-11-20 13:42:48 +00:00
|
|
|
} else {
|
2015-07-04 15:40:40 +00:00
|
|
|
installButton_ = nullptr;
|
2021-05-13 09:08:38 +00:00
|
|
|
speedView_ = nullptr;
|
2013-12-14 13:50:18 +00:00
|
|
|
Add(new TextView(st->T("Already Installed")));
|
|
|
|
Add(new Button(st->T("Uninstall")))->OnClick.Handle(this, &ProductView::OnUninstall);
|
2017-05-18 12:21:13 +00:00
|
|
|
launchButton_ = new Button(st->T("Launch Game"));
|
|
|
|
launchButton_->OnClick.Handle(this, &ProductView::OnLaunchClick);
|
|
|
|
Add(launchButton_);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
2013-11-20 15:36:58 +00:00
|
|
|
|
2017-03-06 14:43:38 +00:00
|
|
|
cancelButton_ = Add(new Button(di->T("Cancel")));
|
|
|
|
cancelButton_->OnClick.Handle(this, &ProductView::OnCancel);
|
2021-05-01 17:49:44 +00:00
|
|
|
cancelButton_->SetVisibility(isDownloading ? V_VISIBLE : V_GONE);
|
2017-03-06 14:43:38 +00:00
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
// Add star rating, comments etc?
|
|
|
|
|
2021-08-14 03:45:31 +00:00
|
|
|
// Draw each line separately so focusing can scroll.
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
SplitString(entry_.description, '\n', lines);
|
|
|
|
for (auto &line : lines) {
|
|
|
|
Add(new TextView(line, ALIGN_LEFT | FLAG_WRAP_TEXT, false))->SetFocusable(true);
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2021-08-14 03:45:31 +00:00
|
|
|
float size = entry_.size / (1024.f * 1024.f);
|
|
|
|
Add(new TextView(StringFromFormat("%s: %.2f %s", st->T("Size"), size, st->T("MB"))));
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 05:01:18 +00:00
|
|
|
void ProductView::Update() {
|
2015-07-04 15:40:40 +00:00
|
|
|
if (wasInstalled_ != IsGameInstalled()) {
|
|
|
|
CreateViews();
|
|
|
|
}
|
|
|
|
if (installButton_) {
|
2017-03-06 14:43:38 +00:00
|
|
|
installButton_->SetEnabled(g_GameManager.GetState() == GameManagerState::IDLE);
|
2015-07-04 15:40:40 +00:00
|
|
|
}
|
2021-05-01 17:40:34 +00:00
|
|
|
if (g_GameManager.GetState() == GameManagerState::DOWNLOADING) {
|
|
|
|
if (speedView_) {
|
|
|
|
float speed = g_GameManager.DownloadSpeedKBps();
|
|
|
|
speedView_->SetText(StringFromFormat("%0.1f KB/s", speed));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cancelButton_)
|
|
|
|
cancelButton_->SetVisibility(UI::V_GONE);
|
|
|
|
if (speedView_)
|
|
|
|
speedView_->SetVisibility(UI::V_GONE);
|
|
|
|
}
|
2017-05-18 12:21:13 +00:00
|
|
|
if (launchButton_)
|
|
|
|
launchButton_->SetEnabled(g_GameManager.GetState() == GameManagerState::IDLE);
|
2017-03-15 05:01:18 +00:00
|
|
|
View::Update();
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 17:49:44 +00:00
|
|
|
std::string ProductView::DownloadURL() {
|
2013-11-29 16:34:21 +00:00
|
|
|
if (entry_.downloadURL.empty()) {
|
2021-07-12 09:09:39 +00:00
|
|
|
// Construct the URL.
|
|
|
|
return storeBaseUrl + "files/" + entry_.file + ".zip";
|
2013-11-29 16:34:21 +00:00
|
|
|
} else {
|
|
|
|
// Use the provided URL, for external hosting.
|
2021-05-01 17:49:44 +00:00
|
|
|
return entry_.downloadURL;
|
2013-11-29 16:34:21 +00:00
|
|
|
}
|
2021-05-01 17:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ProductView::OnInstall(UI::EventParams &e) {
|
|
|
|
std::string fileUrl = DownloadURL();
|
2013-11-20 13:42:48 +00:00
|
|
|
if (installButton_) {
|
2013-11-20 18:51:54 +00:00
|
|
|
installButton_->SetEnabled(false);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
2017-03-06 14:43:38 +00:00
|
|
|
if (cancelButton_) {
|
|
|
|
cancelButton_->SetVisibility(UI::V_VISIBLE);
|
|
|
|
}
|
2021-05-01 17:40:34 +00:00
|
|
|
if (speedView_) {
|
|
|
|
speedView_->SetVisibility(UI::V_VISIBLE);
|
|
|
|
speedView_->SetText("");
|
|
|
|
}
|
2019-07-10 21:37:10 +00:00
|
|
|
INFO_LOG(SYSTEM, "Triggering install of '%s'", fileUrl.c_str());
|
|
|
|
g_GameManager.DownloadAndInstall(fileUrl);
|
2013-11-20 13:42:48 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2017-03-06 14:43:38 +00:00
|
|
|
UI::EventReturn ProductView::OnCancel(UI::EventParams &e) {
|
|
|
|
g_GameManager.CancelDownload();
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
UI::EventReturn ProductView::OnUninstall(UI::EventParams &e) {
|
|
|
|
g_GameManager.Uninstall(entry_.file);
|
2013-11-20 15:36:58 +00:00
|
|
|
CreateViews();
|
2013-11-20 13:42:48 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2015-07-04 16:05:17 +00:00
|
|
|
UI::EventReturn ProductView::OnLaunchClick(UI::EventParams &e) {
|
2017-05-18 12:21:13 +00:00
|
|
|
if (g_GameManager.GetState() != GameManagerState::IDLE) {
|
|
|
|
// Button should have been disabled. Just a safety check.
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
Path pspGame = GetSysDirectory(DIRECTORY_GAME);
|
|
|
|
Path path = pspGame / entry_.file;
|
2017-03-22 01:34:52 +00:00
|
|
|
UI::EventParams e2{};
|
|
|
|
e2.v = e.v;
|
2021-05-05 23:31:38 +00:00
|
|
|
e2.s = path.ToString();
|
2015-07-04 16:05:17 +00:00
|
|
|
// Insta-update - here we know we are already on the right thread.
|
|
|
|
OnClickLaunch.Trigger(e2);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2017-12-10 08:44:44 +00:00
|
|
|
StoreScreen::StoreScreen() {
|
2013-11-20 13:42:48 +00:00
|
|
|
StoreFilter noFilter;
|
|
|
|
SetFilter(noFilter);
|
|
|
|
lang_ = g_Config.sLanguageIni;
|
|
|
|
loading_ = true;
|
|
|
|
|
|
|
|
std::string indexPath = storeBaseUrl + "index.json";
|
2021-08-22 15:29:48 +00:00
|
|
|
const char *acceptMime = "application/json, */*; q=0.8";
|
|
|
|
listing_ = g_DownloadManager.StartDownload(indexPath, Path(), acceptMime);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StoreScreen::~StoreScreen() {
|
2013-11-29 15:33:17 +00:00
|
|
|
g_DownloadManager.CancelAll();
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle async download tasks
|
2017-03-15 05:01:18 +00:00
|
|
|
void StoreScreen::update() {
|
|
|
|
UIDialogScreenWithBackground::update();
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2013-11-29 15:33:17 +00:00
|
|
|
g_DownloadManager.Update();
|
2013-11-20 13:42:48 +00:00
|
|
|
|
|
|
|
if (listing_.get() != 0 && listing_->Done()) {
|
2021-08-22 10:21:44 +00:00
|
|
|
resultCode_ = listing_->ResultCode();
|
2013-11-20 13:42:48 +00:00
|
|
|
if (listing_->ResultCode() == 200) {
|
|
|
|
std::string listingJson;
|
|
|
|
listing_->buffer().TakeAll(&listingJson);
|
2013-12-05 11:42:42 +00:00
|
|
|
// printf("%s\n", listingJson.c_str());
|
2013-11-20 13:42:48 +00:00
|
|
|
loading_ = false;
|
2015-07-19 15:33:38 +00:00
|
|
|
connectionError_ = false;
|
2013-11-20 13:42:48 +00:00
|
|
|
|
|
|
|
ParseListing(listingJson);
|
|
|
|
RecreateViews();
|
|
|
|
} else {
|
|
|
|
// Failed to contact store. Don't do anything.
|
2021-08-22 10:21:44 +00:00
|
|
|
ERROR_LOG(IO, "Download failed : error code %d", resultCode_);
|
2013-11-20 13:42:48 +00:00
|
|
|
connectionError_ = true;
|
2015-07-19 15:33:38 +00:00
|
|
|
loading_ = false;
|
2013-11-20 13:42:48 +00:00
|
|
|
RecreateViews();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forget the listing.
|
|
|
|
listing_.reset();
|
|
|
|
}
|
2017-03-06 14:43:38 +00:00
|
|
|
|
|
|
|
const char *storeName = "PPSSPP Homebrew Store";
|
|
|
|
switch (g_GameManager.GetState()) {
|
|
|
|
case GameManagerState::DOWNLOADING:
|
|
|
|
titleText_->SetText(std::string(storeName) + " - downloading");
|
|
|
|
break;
|
|
|
|
case GameManagerState::INSTALLING:
|
|
|
|
titleText_->SetText(std::string(storeName) + " - installing");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
titleText_->SetText(storeName);
|
|
|
|
break;
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StoreScreen::ParseListing(std::string json) {
|
2018-08-12 22:05:00 +00:00
|
|
|
using namespace json;
|
2013-11-20 13:42:48 +00:00
|
|
|
JsonReader reader(json.c_str(), json.size());
|
2018-04-15 18:24:10 +00:00
|
|
|
if (!reader.ok() || !reader.root()) {
|
2020-08-15 14:13:24 +00:00
|
|
|
ERROR_LOG(IO, "Error parsing JSON from store");
|
2013-11-20 13:42:48 +00:00
|
|
|
connectionError_ = true;
|
|
|
|
RecreateViews();
|
|
|
|
return;
|
|
|
|
}
|
2018-04-15 18:24:10 +00:00
|
|
|
const JsonGet root = reader.root();
|
|
|
|
const JsonNode *entries = root.getArray("entries");
|
2013-11-20 13:42:48 +00:00
|
|
|
if (entries) {
|
|
|
|
entries_.clear();
|
2018-04-15 18:24:10 +00:00
|
|
|
for (const JsonNode *pgame : entries->value) {
|
|
|
|
JsonGet game = pgame->value;
|
2013-11-20 13:42:48 +00:00
|
|
|
StoreEntry e;
|
|
|
|
e.type = ENTRY_PBPZIP;
|
|
|
|
e.name = GetTranslatedString(game, "name");
|
|
|
|
e.description = GetTranslatedString(game, "description", "");
|
2018-04-15 18:24:10 +00:00
|
|
|
e.author = game.getString("author", "?");
|
|
|
|
e.size = game.getInt("size");
|
|
|
|
e.downloadURL = game.getString("download-url", "");
|
|
|
|
e.iconURL = game.getString("icon-url", "");
|
|
|
|
e.hidden = game.getBool("hidden", false);
|
|
|
|
const char *file = game.getString("file", nullptr);
|
2013-11-20 13:42:48 +00:00
|
|
|
if (!file)
|
|
|
|
continue;
|
|
|
|
e.file = file;
|
|
|
|
entries_.push_back(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StoreScreen::CreateViews() {
|
|
|
|
using namespace UI;
|
2013-11-29 12:02:08 +00:00
|
|
|
|
|
|
|
root_ = new LinearLayout(ORIENT_VERTICAL);
|
2013-12-14 13:50:18 +00:00
|
|
|
|
2020-01-26 18:43:18 +00:00
|
|
|
auto di = GetI18NCategory("Dialog");
|
|
|
|
auto st = GetI18NCategory("Store");
|
2013-11-29 12:02:08 +00:00
|
|
|
|
|
|
|
// Top bar
|
|
|
|
LinearLayout *topBar = root_->Add(new LinearLayout(ORIENT_HORIZONTAL));
|
2013-12-14 13:50:18 +00:00
|
|
|
topBar->Add(new Button(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
|
2017-03-06 14:43:38 +00:00
|
|
|
titleText_ = new TextView("PPSSPP Homebrew Store");
|
|
|
|
topBar->Add(titleText_);
|
2013-11-29 12:02:08 +00:00
|
|
|
UI::Drawable solid(0xFFbd9939);
|
|
|
|
topBar->SetBG(solid);
|
|
|
|
|
|
|
|
LinearLayout *content;
|
2013-11-20 13:42:48 +00:00
|
|
|
if (connectionError_ || loading_) {
|
2013-12-05 16:08:20 +00:00
|
|
|
content = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 1.0f));
|
2021-08-22 10:21:44 +00:00
|
|
|
content->Add(new TextView(loading_ ? std::string(st->T("Loading...")) : StringFromFormat("%s: %d", st->T("Connection Error"), resultCode_)));
|
2013-12-14 13:50:18 +00:00
|
|
|
content->Add(new Button(di->T("Retry")))->OnClick.Handle(this, &StoreScreen::OnRetry);
|
|
|
|
content->Add(new Button(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
|
2017-12-10 08:44:44 +00:00
|
|
|
|
|
|
|
scrollItemView_ = nullptr;
|
|
|
|
productPanel_ = nullptr;
|
2013-11-20 13:42:48 +00:00
|
|
|
} else {
|
2013-12-05 16:08:20 +00:00
|
|
|
content = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 1.0f));
|
|
|
|
ScrollView *leftScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(WRAP_CONTENT, FILL_PARENT, 0.4f));
|
2016-01-23 06:52:13 +00:00
|
|
|
leftScroll->SetTag("StoreMainList");
|
2013-11-29 12:02:08 +00:00
|
|
|
content->Add(leftScroll);
|
2021-02-22 02:41:08 +00:00
|
|
|
scrollItemView_ = new LinearLayoutList(ORIENT_VERTICAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
|
2017-12-10 08:44:44 +00:00
|
|
|
leftScroll->Add(scrollItemView_);
|
2013-12-05 16:08:20 +00:00
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
std::vector<StoreEntry> entries = FilterEntries();
|
|
|
|
for (size_t i = 0; i < entries.size(); i++) {
|
2017-12-10 08:44:44 +00:00
|
|
|
scrollItemView_->Add(new ProductItemView(entries_[i]))->OnClick.Handle(this, &StoreScreen::OnGameSelected);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Similar apps, etc etc
|
|
|
|
productPanel_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(0.5f));
|
2016-01-23 06:52:13 +00:00
|
|
|
leftScroll->SetTag("StoreMainProduct");
|
2013-11-29 12:02:08 +00:00
|
|
|
content->Add(productPanel_);
|
2017-12-10 08:44:44 +00:00
|
|
|
|
|
|
|
ProductItemView *selectedItem = GetSelectedItem();
|
|
|
|
if (selectedItem) {
|
|
|
|
ProductView *productView = new ProductView(selectedItem->GetEntry());
|
|
|
|
productView->OnClickLaunch.Handle(this, &StoreScreen::OnGameLaunch);
|
|
|
|
productPanel_->Add(productView);
|
|
|
|
|
|
|
|
selectedItem->Press();
|
|
|
|
} else {
|
|
|
|
lastSelectedName_ = "";
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
2013-11-29 12:02:08 +00:00
|
|
|
root_->Add(content);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StoreEntry> StoreScreen::FilterEntries() {
|
|
|
|
std::vector<StoreEntry> filtered;
|
|
|
|
for (size_t i = 0; i < entries_.size(); i++) {
|
|
|
|
// TODO: Actually filter by category etc.
|
2015-10-01 10:37:16 +00:00
|
|
|
if (!entries_[i].hidden)
|
|
|
|
filtered.push_back(entries_[i]);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
2017-12-10 08:44:44 +00:00
|
|
|
ProductItemView *StoreScreen::GetSelectedItem() {
|
|
|
|
for (int i = 0; i < scrollItemView_->GetNumSubviews(); ++i) {
|
|
|
|
ProductItemView *item = static_cast<ProductItemView *>(scrollItemView_->GetViewByIndex(i));
|
|
|
|
if (item->GetEntry().name == lastSelectedName_)
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
UI::EventReturn StoreScreen::OnGameSelected(UI::EventParams &e) {
|
|
|
|
ProductItemView *item = static_cast<ProductItemView *>(e.v);
|
|
|
|
if (!item)
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
|
|
|
|
productPanel_->Clear();
|
2015-07-04 16:05:17 +00:00
|
|
|
ProductView *productView = new ProductView(item->GetEntry());
|
|
|
|
productView->OnClickLaunch.Handle(this, &StoreScreen::OnGameLaunch);
|
|
|
|
productPanel_->Add(productView);
|
2017-12-10 08:44:44 +00:00
|
|
|
|
|
|
|
ProductItemView *previousItem = GetSelectedItem();
|
|
|
|
if (previousItem && previousItem != item)
|
|
|
|
previousItem->Release();
|
|
|
|
lastSelectedName_ = item->GetEntry().name;
|
2015-07-04 16:05:17 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn StoreScreen::OnGameLaunch(UI::EventParams &e) {
|
|
|
|
std::string path = e.s;
|
2021-05-05 23:31:38 +00:00
|
|
|
screenManager()->switchScreen(new EmuScreen(Path(path)));
|
2013-11-20 13:42:48 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StoreScreen::SetFilter(const StoreFilter &filter) {
|
|
|
|
filter_ = filter;
|
|
|
|
RecreateViews();
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn StoreScreen::OnRetry(UI::EventParams &e) {
|
|
|
|
SetFilter(filter_);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:05:00 +00:00
|
|
|
std::string StoreScreen::GetTranslatedString(const json::JsonGet json, std::string key, const char *fallback) const {
|
|
|
|
json::JsonGet dict = json.getDict("en_US");
|
2018-04-15 18:24:10 +00:00
|
|
|
if (dict && json.hasChild(lang_.c_str(), JSON_OBJECT)) {
|
|
|
|
if (json.getDict(lang_.c_str()).hasChild(key.c_str(), JSON_STRING)) {
|
|
|
|
dict = json.getDict(lang_.c_str());
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-15 18:24:10 +00:00
|
|
|
const char *str = nullptr;
|
2013-11-20 13:42:48 +00:00
|
|
|
if (dict) {
|
2018-04-15 18:24:10 +00:00
|
|
|
str = dict.getString(key.c_str(), nullptr);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
if (str) {
|
|
|
|
return std::string(str);
|
|
|
|
} else {
|
|
|
|
return fallback ? fallback : "(error)";
|
|
|
|
}
|
|
|
|
}
|