mirror of
https://github.com/DarkStore-3DS/DarkStore.git
synced 2026-07-02 16:49:05 +00:00
UniStore list and screenshots (#54)
* Initial push. * Fix png loading * Remove unneeded casts * Push my progress. * Improve screenshot display * Hopefully last commit here before merge? Co-authored-by: StackZ <47382115+SuperSaiyajinStackZ@users.noreply.github.com>
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include "files.hpp"
|
||||
#include "json.hpp"
|
||||
#include "lang.hpp"
|
||||
#include "screenshot.hpp"
|
||||
#include "scriptUtils.hpp"
|
||||
#include "stringutils.hpp"
|
||||
|
||||
@@ -846,4 +847,144 @@ void UpdateAction() {
|
||||
Msg::waitMsg(Lang::get("UPDATE_DONE"));
|
||||
exiting = true;
|
||||
}
|
||||
}
|
||||
|
||||
static StoreList fetch(const std::string &entry, nlohmann::json &js) {
|
||||
StoreList store = { "", "", "", "" };
|
||||
if (!js.contains(entry)) return store;
|
||||
|
||||
if (js[entry].contains("title") && js[entry]["title"].is_string()) store.Title = js[entry]["title"];
|
||||
if (js[entry].contains("author") && js[entry]["author"].is_string()) store.Author = js[entry]["author"];
|
||||
if (js[entry].contains("url") && js[entry]["url"].is_string()) store.URL = js[entry]["url"];
|
||||
if (js[entry].contains("description") && js[entry]["description"].is_string()) store.Description = js[entry]["description"];
|
||||
|
||||
return store;
|
||||
}
|
||||
/*
|
||||
Fetch Store list for available UniStores.
|
||||
*/
|
||||
std::vector<StoreList> FetchStores() {
|
||||
Msg::DisplayMsg(Lang::get("FETCHING_AVAILABLE_UNISTORES"));
|
||||
std::vector<StoreList> stores = { };
|
||||
|
||||
Result ret = 0;
|
||||
void *socubuf = memalign(0x1000, 0x100000);
|
||||
if (!socubuf) return stores;
|
||||
|
||||
ret = socInit((u32 *)socubuf, 0x100000);
|
||||
|
||||
if (R_FAILED(ret)) {
|
||||
free(socubuf);
|
||||
return stores;
|
||||
}
|
||||
|
||||
CURL *hnd = curl_easy_init();
|
||||
|
||||
ret = setupContext(hnd, "https://github.com/Universal-Team/Universal-Updater/raw/master/resources/UniStores.json");
|
||||
if (ret != 0) {
|
||||
socExit();
|
||||
free(result_buf);
|
||||
free(socubuf);
|
||||
result_buf = nullptr;
|
||||
result_sz = 0;
|
||||
result_written = 0;
|
||||
return stores;
|
||||
}
|
||||
|
||||
CURLcode cres = curl_easy_perform(hnd);
|
||||
curl_easy_cleanup(hnd);
|
||||
char *newbuf = (char *)realloc(result_buf, result_written + 1);
|
||||
result_buf = newbuf;
|
||||
result_buf[result_written] = 0; // nullbyte to end it as a proper C style string.
|
||||
|
||||
if (cres != CURLE_OK) {
|
||||
printf("Error in:\ncurl\n");
|
||||
socExit();
|
||||
free(result_buf);
|
||||
free(socubuf);
|
||||
result_buf = nullptr;
|
||||
result_sz = 0;
|
||||
result_written = 0;
|
||||
return stores;
|
||||
}
|
||||
|
||||
if (nlohmann::json::accept(result_buf)) {
|
||||
nlohmann::json parsedAPI = nlohmann::json::parse(result_buf);
|
||||
|
||||
for(auto it = parsedAPI.begin(); it != parsedAPI.end(); ++it) {
|
||||
stores.push_back( fetch(it.key(), parsedAPI) );
|
||||
}
|
||||
}
|
||||
|
||||
socExit();
|
||||
free(result_buf);
|
||||
free(socubuf);
|
||||
result_buf = nullptr;
|
||||
result_sz = 0;
|
||||
result_written = 0;
|
||||
|
||||
return stores;
|
||||
}
|
||||
|
||||
C2D_Image FetchScreenshot(const std::string &URL) {
|
||||
if (URL == "") return { };
|
||||
|
||||
C2D_Image img = { };
|
||||
|
||||
Result ret = 0;
|
||||
void *socubuf = memalign(0x1000, 0x100000);
|
||||
if (!socubuf) return img;
|
||||
|
||||
ret = socInit((u32 *)socubuf, 0x100000);
|
||||
|
||||
if (R_FAILED(ret)) {
|
||||
free(socubuf);
|
||||
return img;
|
||||
}
|
||||
|
||||
CURL *hnd = curl_easy_init();
|
||||
|
||||
ret = setupContext(hnd, URL.c_str());
|
||||
if (ret != 0) {
|
||||
socExit();
|
||||
free(result_buf);
|
||||
free(socubuf);
|
||||
result_buf = nullptr;
|
||||
result_sz = 0;
|
||||
result_written = 0;
|
||||
return img;
|
||||
}
|
||||
|
||||
CURLcode cres = curl_easy_perform(hnd);
|
||||
curl_easy_cleanup(hnd);
|
||||
char *newbuf = (char *)realloc(result_buf, result_written + 1);
|
||||
result_buf = newbuf;
|
||||
result_buf[result_written] = 0; // nullbyte to end it as a proper C style string.
|
||||
|
||||
if (cres != CURLE_OK) {
|
||||
printf("Error in:\ncurl\n");
|
||||
socExit();
|
||||
free(result_buf);
|
||||
free(socubuf);
|
||||
result_buf = nullptr;
|
||||
result_sz = 0;
|
||||
result_written = 0;
|
||||
return img;
|
||||
}
|
||||
|
||||
std::vector<u8> buffer;
|
||||
for (int i = 0; i < (int)result_written; i++) {
|
||||
buffer.push_back( result_buf[i] );
|
||||
}
|
||||
|
||||
img = Screenshot::ConvertFromBuffer(buffer);
|
||||
|
||||
socExit();
|
||||
free(result_buf);
|
||||
free(socubuf);
|
||||
result_buf = nullptr;
|
||||
result_sz = 0;
|
||||
result_written = 0;
|
||||
|
||||
return img;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* This file is part of Universal-Updater
|
||||
* Copyright (C) 2019-2020 Universal-Team
|
||||
*
|
||||
* 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, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
|
||||
* * Requiring preservation of specified reasonable legal notices or
|
||||
* author attributions in that material or in the Appropriate Legal
|
||||
* Notices displayed by works containing it.
|
||||
* * Prohibiting misrepresentation of the origin of that material,
|
||||
* or requiring that modified versions of such material be marked in
|
||||
* reasonable ways as different from the original version.
|
||||
*/
|
||||
|
||||
#include "lodepng.h"
|
||||
#include "msg.hpp"
|
||||
#include "screenshot.hpp"
|
||||
|
||||
C2D_Image Screenshot::Convert(const std::string &filename) {
|
||||
std::vector<u8> ImageBuffer;
|
||||
unsigned width, height;
|
||||
C2D_Image img;
|
||||
lodepng::decode(ImageBuffer, width, height, filename.c_str());
|
||||
|
||||
img.tex = new C3D_Tex;
|
||||
img.subtex = new Tex3DS_SubTexture({(u16)width, (u16)height, 0.0f, 1.0f, width / 512.0f, 1.0f - (height / 512.0f)});
|
||||
|
||||
C3D_TexInit(img.tex, 512, 512, GPU_RGBA8);
|
||||
C3D_TexSetFilter(img.tex, GPU_LINEAR, GPU_LINEAR);
|
||||
img.tex->border = 0xFFFFFFFF;
|
||||
C3D_TexSetWrap(img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
|
||||
|
||||
for (u32 x = 0; x < width && x < 512; x++) {
|
||||
for (u32 y = 0; y < height && y < 512; y++) {
|
||||
const u32 dstPos = ((((y >> 3) * (512 >> 3) + (x >> 3)) << 6) +
|
||||
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
|
||||
((x & 4) << 2) | ((y & 4) << 3))) * 4;
|
||||
|
||||
const u32 srcPos = (y * width + x) * 4;
|
||||
((uint8_t *)img.tex->data)[dstPos + 1] = ImageBuffer.data()[srcPos + 2];
|
||||
((uint8_t *)img.tex->data)[dstPos + 2] = ImageBuffer.data()[srcPos + 1];
|
||||
((uint8_t *)img.tex->data)[dstPos + 3] = ImageBuffer.data()[srcPos + 0];
|
||||
((uint8_t *)img.tex->data)[dstPos + 4] = ImageBuffer.data()[srcPos + 3];
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
C2D_Image Screenshot::ConvertFromBuffer(const std::vector<u8> &buffer) {
|
||||
std::vector<u8> ImageBuffer;
|
||||
unsigned width, height;
|
||||
C2D_Image img;
|
||||
lodepng::decode(ImageBuffer, width, height, buffer);
|
||||
|
||||
img.tex = new C3D_Tex;
|
||||
img.subtex = new Tex3DS_SubTexture({(u16)width, (u16)height, 0.0f, 1.0f, width / 512.0f, 1.0f - (height / 512.0f)});
|
||||
|
||||
C3D_TexInit(img.tex, 512, 512, GPU_RGBA8);
|
||||
C3D_TexSetFilter(img.tex, GPU_LINEAR, GPU_LINEAR);
|
||||
img.tex->border = 0xFFFFFFFF;
|
||||
C3D_TexSetWrap(img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
|
||||
|
||||
for (u32 x = 0; x < width && x < 512; x++) {
|
||||
for (u32 y = 0; y < height && y < 512; y++) {
|
||||
const u32 dstPos = ((((y >> 3) * (512 >> 3) + (x >> 3)) << 6) +
|
||||
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
|
||||
((x & 4) << 2) | ((y & 4) << 3))) * 4;
|
||||
|
||||
const u32 srcPos = (y * width + x) * 4;
|
||||
((uint8_t *)img.tex->data)[dstPos + 1] = ImageBuffer.data()[srcPos + 2];
|
||||
((uint8_t *)img.tex->data)[dstPos + 2] = ImageBuffer.data()[srcPos + 1];
|
||||
((uint8_t *)img.tex->data)[dstPos + 3] = ImageBuffer.data()[srcPos + 0];
|
||||
((uint8_t *)img.tex->data)[dstPos + 4] = ImageBuffer.data()[srcPos + 3];
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
Reference in New Issue
Block a user