Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* Resource Download Helpers
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/Globals.h"
#include "CommonFramework/Logging/Logger.h"
// #include "CommonFramework/Tools/GlobalThreadPools.h"
#include "CommonFramework/Tools/FileDownloader.h"
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Cpp/Filesystem.h"
#include "ResourceDownloadHelpers.h"

// #include <filesystem>
// #include <thread>
// #include <unordered_set>

#include <iostream>
using std::cout;
using std::endl;

namespace PokemonAutomation{


std::vector<DownloadedResourceMetadata> deserialize_resource_list_json(const JsonValue& json){
std::vector<DownloadedResourceMetadata> resources;

try{
const JsonObject& obj = json.to_object_throw();
const JsonArray& resource_list = obj.get_array_throw("ResourceList");
for (const JsonValue& resource_val : resource_list){
const JsonObject& resource_obj = resource_val.to_object_throw();

std::string resource_name = resource_obj.get_string_throw("ResourceName");
std::optional<uint16_t> version_num = (uint16_t)resource_obj.get_integer_throw("Version");
size_t compressed_bytes = (size_t)resource_obj.get_integer_throw("CompressedBytes");
size_t decompressed_bytes = (size_t)resource_obj.get_integer_throw("DecompressedBytes");
std::string url = resource_obj.get_string_throw("URL");
std::string sha256 = resource_obj.get_string_throw("SHA256");

DownloadedResourceMetadata resource = {
resource_name,
version_num,
compressed_bytes,
decompressed_bytes,
url,
sha256
};

resources.emplace_back(std::move(resource));

}

}catch (ParseException&){
std::cerr << "JSON parsing error. Given JSON file doesn't match the expected format." << endl;
// throw ParseException("JSON parsing error. Given JSON file doesn't match the expected format.");
return std::vector<DownloadedResourceMetadata>();
}

return resources;
}


const std::vector<DownloadedResourceMetadata>& local_resource_download_list(){
// cout << "local_resource_download_list" << endl;
static std::vector<DownloadedResourceMetadata> local_resources = deserialize_resource_list_json(load_json_file(RESOURCE_PATH() + "ResourceDownloadList.json"));

return local_resources;
}


JsonValue fetch_resource_download_list_json_from_remote(){
Logger& logger = global_logger_tagged();
JsonValue json =
FileDownloader::download_json_file(
logger,
"https://raw.githubusercontent.com/jw098/Packages/refs/heads/download/Resources/ResourceDownloadList.json"
);

return json;
}

const JsonValue& remote_resource_download_list_json(){
static const JsonValue json = fetch_resource_download_list_json_from_remote();

return json;
}

const std::vector<DownloadedResourceMetadata>& remote_resource_download_list(){
// cout << "remote_resource_download_list" << endl;
static std::vector<DownloadedResourceMetadata> remote_resources = deserialize_resource_list_json(remote_resource_download_list_json());

return remote_resources;
}

std::optional<uint16_t> get_resource_version_num(Filesystem::Path folder_path){
try{
std::string file_name = folder_path.string() + "/version.json";
const JsonValue& json = load_json_file(file_name);

const JsonObject& obj = json.to_object_throw();
uint16_t version_num = (uint16_t)obj.get_integer_throw("version");
return version_num;
}catch(...){
std::cerr << "Unable to determine the version number from version.json." << endl;
return std::nullopt;
}

}

ResourceVersionStatus get_version_status(uint16_t expected_version_num, std::optional<uint16_t> current_version_num){
if (!current_version_num.has_value()){
return ResourceVersionStatus::NOT_APPLICABLE;
}

if (current_version_num < expected_version_num){
return ResourceVersionStatus::OUTDATED;
}else if (current_version_num == expected_version_num){
return ResourceVersionStatus::CURRENT;
}else{ // current > expected
return ResourceVersionStatus::FUTURE_VERSION;
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Resource Download Helpers
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_ResourceDownloadHelpers_H
#define PokemonAutomation_ResourceDownloadHelpers_H

#include <string>
#include <optional>


namespace PokemonAutomation{

namespace Filesystem{
class Path;
}



struct DownloadedResourceMetadata{
std::string resource_name;
std::optional<uint16_t> version_num;
size_t size_compressed_bytes;
size_t size_decompressed_bytes;
std::string url;
std::string sha256;
};

enum class ResourceVersionStatus{
CURRENT,
OUTDATED, // still used, but newer version available
FUTURE_VERSION, // current version number is greater than the expected version number
NOT_APPLICABLE, // resource not downloaded locally, so can't get its version
// RETIRED, // no longer used
};

enum class RemoteMetadataStatus{
UNINITIALIZED,
NOT_AVAILABLE,
AVAILABLE,
};
struct RemoteMetadata {
RemoteMetadataStatus status = RemoteMetadataStatus::UNINITIALIZED;
DownloadedResourceMetadata metadata;
};


const std::vector<DownloadedResourceMetadata>& local_resource_download_list();
const std::vector<DownloadedResourceMetadata>& remote_resource_download_list();
std::optional<uint16_t> get_resource_version_num(Filesystem::Path folder_path);
ResourceVersionStatus get_version_status(uint16_t expected_version_num, std::optional<uint16_t> current_version_num);

}
#endif
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ file(GLOB LIBRARY_SOURCES
Source/CommonFramework/Recording/StreamHistoryTracker_SaveFrames.h
Source/CommonFramework/Recording/StreamRecorder.cpp
Source/CommonFramework/Recording/StreamRecorder.h
Source/CommonFramework/ResourceDownload/ResourceDownloadHelpers.cpp
Source/CommonFramework/ResourceDownload/ResourceDownloadHelpers.h
Source/CommonFramework/Startup/NewVersionCheck.cpp
Source/CommonFramework/Startup/NewVersionCheck.h
Source/CommonFramework/Startup/SetupSettings.cpp
Expand Down