| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/ResourcePack/Manifest.h" |
| 5 | |
| 6 | #include <picojson.h> |
| 7 | |
| 8 | namespace ResourcePack |
| 9 | { |
| 10 | Manifest::Manifest(const std::string& json) |
| 11 | { |
| 12 | picojson::value out; |
| 13 | auto error = picojson::parse(out, json); |
| 14 | |
| 15 | if (!error.empty()) |
| 16 | { |
| 17 | m_error = "Failed to parse manifest."; |
| 18 | m_valid = false; |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | // Required fields |
| 23 | picojson::value& name = out.get("name"); |
| 24 | picojson::value& version = out.get("version"); |
| 25 | picojson::value& id = out.get("id"); |
| 26 | |
| 27 | // Optional fields |
| 28 | picojson::value& authors = out.get("authors"); |
| 29 | picojson::value& description = out.get("description"); |
| 30 | picojson::value& website = out.get("website"); |
| 31 | picojson::value& compressed = out.get("compressed"); |
| 32 | |
| 33 | if (!name.is<std::string>() || !id.is<std::string>() || !version.is<std::string>()) |
| 34 | { |
| 35 | m_error = "Some objects have a bad type."; |
| 36 | m_valid = false; |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | m_name = name.to_str(); |
| 41 | m_version = version.to_str(); |
| 42 | m_id = id.to_str(); |
| 43 | |
| 44 | if (authors.is<picojson::array>()) |
| 45 | { |
| 46 | std::string author_list; |
| 47 | for (const auto& o : authors.get<picojson::array>()) |
| 48 | { |
| 49 | author_list += o.to_str() + ", "; |
| 50 | } |
| 51 | |
| 52 | if (!author_list.empty()) |
| 53 | m_authors = author_list.substr(0, author_list.size() - 2); |
| 54 | } |
| 55 | |
| 56 | if (description.is<std::string>()) |
| 57 | m_description = description.to_str(); |
| 58 | |
| 59 | if (website.is<std::string>()) |
| 60 | m_website = website.to_str(); |
| 61 | |
| 62 | if (compressed.is<bool>()) |
| 63 | m_compressed = compressed.get<bool>(); |
| 64 | } |
| 65 | |
| 66 | bool Manifest::IsValid() const |
| 67 | { |
| 68 | return m_valid; |
| 69 | } |
| 70 | |
| 71 | const std::string& Manifest::GetName() const |
| 72 | { |
| 73 | return m_name; |
| 74 | } |
| 75 | |
| 76 | const std::string& Manifest::GetVersion() const |
| 77 | { |
| 78 | return m_version; |
| 79 | } |
| 80 | |
| 81 | const std::string& Manifest::GetID() const |
| 82 | { |
| 83 | return m_id; |
| 84 | } |
| 85 | |
| 86 | const std::string& Manifest::GetError() const |
| 87 | { |
| 88 | return m_error; |
| 89 | } |
| 90 | |
| 91 | const std::optional<std::string>& Manifest::GetAuthors() const |
| 92 | { |
| 93 | return m_authors; |
| 94 | } |
| 95 | |
| 96 | const std::optional<std::string>& Manifest::GetDescription() const |
| 97 | { |
| 98 | return m_description; |
| 99 | } |
| 100 | |
| 101 | const std::optional<std::string>& Manifest::GetWebsite() const |
| 102 | { |
| 103 | return m_website; |
| 104 | } |
| 105 | |
| 106 | bool Manifest::IsCompressed() const |
| 107 | { |
| 108 | return m_compressed; |
| 109 | } |
| 110 | |
| 111 | } // namespace ResourcePack |