mirror of
https://github.com/hedge-dev/UnleashedRecomp
synced 2026-09-01 10:12:56 -04:00
Implement installer with support for ISO, STFS and SVOD. Also implement XEX Patcher. (#5)
This commit is contained in:
Vendored
+3
-2
@@ -1,3 +1,4 @@
|
||||
add_subdirectory(${SWA_THIRDPARTY_ROOT}/PowerRecomp)
|
||||
add_subdirectory(${SWA_THIRDPARTY_ROOT}/ShaderRecomp)
|
||||
add_subdirectory(${SWA_THIRDPARTY_ROOT}/o1heap)
|
||||
add_subdirectory(${SWA_THIRDPARTY_ROOT}/ShaderRecomp)
|
||||
add_subdirectory(${SWA_THIRDPARTY_ROOT}/o1heap)
|
||||
add_subdirectory(${SWA_THIRDPARTY_ROOT}/fshasher)
|
||||
|
||||
Vendored
+1
-1
Submodule thirdparty/PowerRecomp updated: 7dd4f91ac6...675b482ec4
Vendored
+1
-1
Submodule thirdparty/ShaderRecomp updated: f936ed2212...30f5986047
Vendored
+223
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
*
|
||||
* TinySHA1 - a header only implementation of the SHA1 algorithm in C++. Based
|
||||
* on the implementation in boost::uuid::details.
|
||||
*
|
||||
* SHA1 Wikipedia Page: http://en.wikipedia.org/wiki/SHA-1
|
||||
*
|
||||
* Copyright (c) 2012-22 SAURAV MOHAPATRA <mohaps@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* Taken from https://github.com/mohaps/TinySHA1
|
||||
* Modified for use by Xenia
|
||||
*/
|
||||
#ifndef _TINY_SHA1_HPP_
|
||||
#define _TINY_SHA1_HPP_
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace sha1 {
|
||||
class SHA1 {
|
||||
public:
|
||||
typedef uint32_t digest32_t[5];
|
||||
typedef uint8_t digest8_t[20];
|
||||
inline static uint32_t LeftRotate(uint32_t value, size_t count) {
|
||||
return (value << count) ^ (value >> (32 - count));
|
||||
}
|
||||
SHA1() { reset(); }
|
||||
virtual ~SHA1() {}
|
||||
SHA1(const SHA1& s) { *this = s; }
|
||||
const SHA1& operator=(const SHA1& s) {
|
||||
memcpy(m_digest, s.m_digest, 5 * sizeof(uint32_t));
|
||||
memcpy(m_block, s.m_block, 64);
|
||||
m_blockByteIndex = s.m_blockByteIndex;
|
||||
m_byteCount = s.m_byteCount;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SHA1& init(const uint32_t digest[5], const uint8_t block[64],
|
||||
uint32_t count) {
|
||||
std::memcpy(m_digest, digest, 20);
|
||||
std::memcpy(m_block, block, count % 64);
|
||||
m_byteCount = count;
|
||||
m_blockByteIndex = count % 64;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const uint32_t* getDigest() const { return m_digest; }
|
||||
const uint8_t* getBlock() const { return m_block; }
|
||||
size_t getBlockByteIndex() const { return m_blockByteIndex; }
|
||||
size_t getByteCount() const { return m_byteCount; }
|
||||
|
||||
SHA1& reset() {
|
||||
m_digest[0] = 0x67452301;
|
||||
m_digest[1] = 0xEFCDAB89;
|
||||
m_digest[2] = 0x98BADCFE;
|
||||
m_digest[3] = 0x10325476;
|
||||
m_digest[4] = 0xC3D2E1F0;
|
||||
m_blockByteIndex = 0;
|
||||
m_byteCount = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SHA1& processByte(uint8_t octet) {
|
||||
this->m_block[this->m_blockByteIndex++] = octet;
|
||||
++this->m_byteCount;
|
||||
if (m_blockByteIndex == 64) {
|
||||
this->m_blockByteIndex = 0;
|
||||
processBlock();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SHA1& processBlock(const void* const start, const void* const end) {
|
||||
const uint8_t* begin = static_cast<const uint8_t*>(start);
|
||||
const uint8_t* finish = static_cast<const uint8_t*>(end);
|
||||
while (begin != finish) {
|
||||
processByte(*begin);
|
||||
begin++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
SHA1& processBytes(const void* const data, size_t len) {
|
||||
const uint8_t* block = static_cast<const uint8_t*>(data);
|
||||
processBlock(block, block + len);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const uint32_t* finalize(digest32_t digest) {
|
||||
size_t bitCount = this->m_byteCount * 8;
|
||||
processByte(0x80);
|
||||
if (this->m_blockByteIndex > 56) {
|
||||
while (m_blockByteIndex != 0) {
|
||||
processByte(0);
|
||||
}
|
||||
while (m_blockByteIndex < 56) {
|
||||
processByte(0);
|
||||
}
|
||||
} else {
|
||||
while (m_blockByteIndex < 56) {
|
||||
processByte(0);
|
||||
}
|
||||
}
|
||||
processByte(0);
|
||||
processByte(0);
|
||||
processByte(0);
|
||||
processByte(0);
|
||||
processByte(static_cast<unsigned char>((bitCount >> 24) & 0xFF));
|
||||
processByte(static_cast<unsigned char>((bitCount >> 16) & 0xFF));
|
||||
processByte(static_cast<unsigned char>((bitCount >> 8) & 0xFF));
|
||||
processByte(static_cast<unsigned char>((bitCount)&0xFF));
|
||||
|
||||
memcpy(digest, m_digest, 5 * sizeof(uint32_t));
|
||||
return digest;
|
||||
}
|
||||
|
||||
const uint8_t* finalize(digest8_t digest) {
|
||||
digest32_t d32;
|
||||
finalize(d32);
|
||||
size_t di = 0;
|
||||
digest[di++] = ((d32[0] >> 24) & 0xFF);
|
||||
digest[di++] = ((d32[0] >> 16) & 0xFF);
|
||||
digest[di++] = ((d32[0] >> 8) & 0xFF);
|
||||
digest[di++] = ((d32[0]) & 0xFF);
|
||||
|
||||
digest[di++] = ((d32[1] >> 24) & 0xFF);
|
||||
digest[di++] = ((d32[1] >> 16) & 0xFF);
|
||||
digest[di++] = ((d32[1] >> 8) & 0xFF);
|
||||
digest[di++] = ((d32[1]) & 0xFF);
|
||||
|
||||
digest[di++] = ((d32[2] >> 24) & 0xFF);
|
||||
digest[di++] = ((d32[2] >> 16) & 0xFF);
|
||||
digest[di++] = ((d32[2] >> 8) & 0xFF);
|
||||
digest[di++] = ((d32[2]) & 0xFF);
|
||||
|
||||
digest[di++] = ((d32[3] >> 24) & 0xFF);
|
||||
digest[di++] = ((d32[3] >> 16) & 0xFF);
|
||||
digest[di++] = ((d32[3] >> 8) & 0xFF);
|
||||
digest[di++] = ((d32[3]) & 0xFF);
|
||||
|
||||
digest[di++] = ((d32[4] >> 24) & 0xFF);
|
||||
digest[di++] = ((d32[4] >> 16) & 0xFF);
|
||||
digest[di++] = ((d32[4] >> 8) & 0xFF);
|
||||
digest[di++] = ((d32[4]) & 0xFF);
|
||||
return digest;
|
||||
}
|
||||
|
||||
protected:
|
||||
void processBlock() {
|
||||
uint32_t w[80];
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
w[i] = (m_block[i * 4 + 0] << 24);
|
||||
w[i] |= (m_block[i * 4 + 1] << 16);
|
||||
w[i] |= (m_block[i * 4 + 2] << 8);
|
||||
w[i] |= (m_block[i * 4 + 3]);
|
||||
}
|
||||
for (size_t i = 16; i < 80; i++) {
|
||||
w[i] = LeftRotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1);
|
||||
}
|
||||
|
||||
uint32_t a = m_digest[0];
|
||||
uint32_t b = m_digest[1];
|
||||
uint32_t c = m_digest[2];
|
||||
uint32_t d = m_digest[3];
|
||||
uint32_t e = m_digest[4];
|
||||
|
||||
for (std::size_t i = 0; i < 80; ++i) {
|
||||
uint32_t f = 0;
|
||||
uint32_t k = 0;
|
||||
|
||||
if (i < 20) {
|
||||
f = (b & c) | (~b & d);
|
||||
k = 0x5A827999;
|
||||
} else if (i < 40) {
|
||||
f = b ^ c ^ d;
|
||||
k = 0x6ED9EBA1;
|
||||
} else if (i < 60) {
|
||||
f = (b & c) | (b & d) | (c & d);
|
||||
k = 0x8F1BBCDC;
|
||||
} else {
|
||||
f = b ^ c ^ d;
|
||||
k = 0xCA62C1D6;
|
||||
}
|
||||
uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i];
|
||||
e = d;
|
||||
d = c;
|
||||
c = LeftRotate(b, 30);
|
||||
b = a;
|
||||
a = temp;
|
||||
}
|
||||
|
||||
m_digest[0] += a;
|
||||
m_digest[1] += b;
|
||||
m_digest[2] += c;
|
||||
m_digest[3] += d;
|
||||
m_digest[4] += e;
|
||||
}
|
||||
|
||||
private:
|
||||
digest32_t m_digest;
|
||||
uint8_t m_block[64];
|
||||
size_t m_blockByteIndex;
|
||||
size_t m_byteCount;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
project("fshasher")
|
||||
|
||||
add_executable(fshasher "fshasher.cpp")
|
||||
|
||||
find_package(xxhash CONFIG REQUIRED)
|
||||
|
||||
target_link_libraries(fshasher PRIVATE xxHash::xxhash)
|
||||
Vendored
+203
@@ -0,0 +1,203 @@
|
||||
//
|
||||
// fshasher - CLI tool to generate a hash map from a file system.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
//
|
||||
// Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
// distribute this software, either in source code form or as a compiled
|
||||
// binary, for any purpose, commercial or non-commercial, and by any
|
||||
// means.
|
||||
//
|
||||
// In jurisdictions that recognize copyright laws, the author or authors
|
||||
// of this software dedicate any and all copyright interest in the
|
||||
// software to the public domain. We make this dedication for the benefit
|
||||
// of the public at large and to the detriment of our heirs and
|
||||
// successors. We intend this dedication to be an overt act of
|
||||
// relinquishment in perpetuity of all present and future rights to this
|
||||
// software under copyright law.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include <xxh3.h>
|
||||
|
||||
#include "plainargs.h"
|
||||
|
||||
void showHelp() {
|
||||
std::cout << "fshasher --directory <directory1 directory2 ...> --source <source file> --header <header file> --variable <variable name>" << std::endl;
|
||||
}
|
||||
|
||||
int process(const std::list<std::filesystem::path> &searchDirectories, std::ofstream &outputSourceStream, std::ofstream &outputHeaderStream, const std::string &variableName) {
|
||||
auto writeExterns = [&](std::ofstream &outputStream)
|
||||
{
|
||||
outputStream << "extern const uint64_t " << variableName << "Hashes[];" << std::endl;
|
||||
outputStream << "extern const std::pair<const char *, uint32_t> " << variableName << "Files[];" << std::endl;
|
||||
outputStream << "extern const size_t " << variableName << "FilesSize;" << std::endl << std::endl;
|
||||
};
|
||||
|
||||
// Generate header.
|
||||
outputHeaderStream << "// File automatically generated by fshasher" << std::endl << std::endl;
|
||||
outputHeaderStream << "#pragma once" << std::endl << std::endl;
|
||||
outputHeaderStream << "#include <utility>" << std::endl << std::endl;
|
||||
writeExterns(outputHeaderStream);
|
||||
|
||||
if (outputHeaderStream.bad())
|
||||
{
|
||||
std::cerr << "Failed to write to output header." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
outputSourceStream << "// File automatically generated by fshasher" << std::endl << std::endl;
|
||||
outputSourceStream << "#include <utility>" << std::endl << std::endl;
|
||||
writeExterns(outputSourceStream);
|
||||
|
||||
std::map<std::u8string, std::set<uint64_t>> fileHashSets;
|
||||
char fileData[65536];
|
||||
XXH3_state_t xxh3;
|
||||
for (const std::filesystem::path &searchDirectory : searchDirectories)
|
||||
{
|
||||
if (!std::filesystem::is_directory(searchDirectory))
|
||||
{
|
||||
std::cerr << "Specified directory " << searchDirectory << " does not exist." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (const std::filesystem::directory_entry &entry : std::filesystem::recursive_directory_iterator(searchDirectory))
|
||||
{
|
||||
if (!entry.is_regular_file())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
std::filesystem::path entryPath = entry.path();
|
||||
std::filesystem::path entryRelative = std::filesystem::relative(entryPath, searchDirectory);
|
||||
std::ifstream entryStream(entryPath, std::ios::binary);
|
||||
if (!entryStream.is_open())
|
||||
{
|
||||
std::cerr << "Could not open " << entryPath << " for reading." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Reading " << entryRelative << "." << std::endl;
|
||||
XXH3_64bits_reset(&xxh3);
|
||||
while (!entryStream.eof() && !entryStream.bad())
|
||||
{
|
||||
entryStream.read(fileData, sizeof(fileData));
|
||||
XXH3_64bits_update(&xxh3, fileData, entryStream.gcount());
|
||||
}
|
||||
|
||||
if (entryStream.bad())
|
||||
{
|
||||
std::cerr << "Could not read " << entryPath << " successfully." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::u8string entryRelativeU8 = entryRelative.u8string();
|
||||
std::replace(entryRelativeU8.begin(), entryRelativeU8.end(), '\\', '/');
|
||||
fileHashSets[entryRelativeU8].insert(XXH3_64bits_digest(&xxh3));
|
||||
}
|
||||
}
|
||||
|
||||
outputSourceStream << "const uint64_t " << variableName << "Hashes[] = {" << std::endl;
|
||||
|
||||
for (auto &it : fileHashSets)
|
||||
{
|
||||
for (uint64_t hash : it.second)
|
||||
{
|
||||
outputSourceStream << " " << hash << "ULL," << std::endl;
|
||||
}
|
||||
|
||||
if (outputSourceStream.bad())
|
||||
{
|
||||
std::cerr << "Failed to write to output source." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
outputSourceStream << "};" << std::endl << std::endl;
|
||||
outputSourceStream << "const std::pair<const char *, uint32_t> " << variableName << "Files[] = {" << std::endl;
|
||||
|
||||
for (const auto &it : fileHashSets)
|
||||
{
|
||||
outputSourceStream << " { \"" << (const char *)(it.first.c_str()) << "\", " << it.second.size() << " }," << std::endl;
|
||||
if (outputSourceStream.bad())
|
||||
{
|
||||
std::cerr << "Failed to write to output source." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
outputSourceStream << "};" << std::endl << std::endl;
|
||||
outputSourceStream << "const size_t " << variableName << "FilesSize = std::size(" << variableName << "Files);" << std::endl;
|
||||
|
||||
if (outputSourceStream.bad())
|
||||
{
|
||||
std::cerr << "Failed to write to output source." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
plainargs::Result argsResult = plainargs::parse(argc, argv);
|
||||
std::vector<std::string> directories = argsResult.getValues("directory", "d");
|
||||
std::string variable = argsResult.getValue("variable", "v");
|
||||
std::string source = argsResult.getValue("source", "s");
|
||||
std::string header = argsResult.getValue("header", "h");
|
||||
if (directories.empty() || variable.empty() || source.empty() || header.empty())
|
||||
{
|
||||
showHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::filesystem::path sourcePath(source);
|
||||
std::ofstream sourceStream(sourcePath);
|
||||
if (!sourceStream.is_open())
|
||||
{
|
||||
std::cerr << "Could not open " << sourcePath << " for writing." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::filesystem::path headerPath(header);
|
||||
std::ofstream headerStream(headerPath);
|
||||
if (!headerStream.is_open())
|
||||
{
|
||||
std::cerr << "Could not open " << headerPath << " for writing." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::list<std::filesystem::path> searchDirectories;
|
||||
for (std::string &directory : directories)
|
||||
{
|
||||
searchDirectories.emplace_back(directory);
|
||||
}
|
||||
|
||||
int resultCode = process(searchDirectories, sourceStream, headerStream, variable);
|
||||
sourceStream.close();
|
||||
headerStream.close();
|
||||
|
||||
if (resultCode != 0)
|
||||
{
|
||||
std::cerr << "Failed to generate " << sourcePath << "and" << headerPath << "." << std::endl;
|
||||
std::filesystem::remove(sourcePath);
|
||||
std::filesystem::remove(headerPath);
|
||||
}
|
||||
|
||||
return resultCode;
|
||||
}
|
||||
Vendored
+147
@@ -0,0 +1,147 @@
|
||||
//
|
||||
// plainargs - A very plain CLI arguments parsing header-only library.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
//
|
||||
// Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
// distribute this software, either in source code form or as a compiled
|
||||
// binary, for any purpose, commercial or non-commercial, and by any
|
||||
// means.
|
||||
//
|
||||
// In jurisdictions that recognize copyright laws, the author or authors
|
||||
// of this software dedicate any and all copyright interest in the
|
||||
// software to the public domain. We make this dedication for the benefit
|
||||
// of the public at large and to the detriment of our heirs and
|
||||
// successors. We intend this dedication to be an overt act of
|
||||
// relinquishment in perpetuity of all present and future rights to this
|
||||
// software under copyright law.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace plainargs {
|
||||
class Result {
|
||||
private:
|
||||
struct Option {
|
||||
uint32_t keyIndex;
|
||||
uint32_t valueCount;
|
||||
};
|
||||
|
||||
std::string directory;
|
||||
std::vector<std::string> arguments;
|
||||
std::vector<Option> options;
|
||||
std::unordered_map<std::string, uint32_t> shortKeyMap;
|
||||
std::unordered_map<std::string, uint32_t> longKeyMap;
|
||||
public:
|
||||
// Arguments are the same as main().
|
||||
Result(int argc, char *argv[]) {
|
||||
if (argc < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
directory = argv[0];
|
||||
|
||||
arguments.resize(size_t(argc - 1));
|
||||
for (uint32_t i = 1; i < uint32_t(argc); i++) {
|
||||
std::string &argument = arguments[i - 1];
|
||||
argument = std::string(argv[i]);
|
||||
|
||||
if (!argument.empty()) {
|
||||
bool shortKey = (argument.size() > 1) && (argument[0] == '-');
|
||||
bool longKey = (argument.size() > 2) && (argument[0] == '-') && (argument[1] == '-');
|
||||
if (longKey) {
|
||||
longKeyMap[argument.substr(2)] = uint32_t(options.size());
|
||||
options.emplace_back(Option{ i - 1, 0 });
|
||||
}
|
||||
else if (shortKey) {
|
||||
shortKeyMap[argument.substr(1)] = uint32_t(options.size());
|
||||
options.emplace_back(Option{ i - 1, 0 });
|
||||
}
|
||||
else if (!options.empty()) {
|
||||
options.back().valueCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return all the values associated to the long key or the short key in order.
|
||||
std::vector<std::string> getValues(const std::string &longKey, const std::string &shortKey = "", uint32_t maxValues = 0) const {
|
||||
std::vector<std::string> values;
|
||||
auto optionIt = options.end();
|
||||
if (!longKey.empty()) {
|
||||
auto it = longKeyMap.find(longKey);
|
||||
if (it != longKeyMap.end()) {
|
||||
optionIt = options.begin() + it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if ((optionIt == options.end()) && !shortKey.empty()) {
|
||||
auto it = shortKeyMap.find(shortKey);
|
||||
if (it != shortKeyMap.end()) {
|
||||
optionIt = options.begin() + it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if (optionIt != options.end()) {
|
||||
uint32_t valueCount = optionIt->valueCount;
|
||||
if ((maxValues > 0) && (valueCount > maxValues)) {
|
||||
valueCount = maxValues;
|
||||
}
|
||||
|
||||
values.resize(valueCount);
|
||||
for (uint32_t i = 0; i < valueCount; i++) {
|
||||
values[i] = arguments[optionIt->keyIndex + i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
std::string getValue(const std::string &longKey, const std::string &shortKey = "") const {
|
||||
std::vector<std::string> values = getValues(longKey, shortKey, 1);
|
||||
return !values.empty() ? values.front() : std::string();
|
||||
}
|
||||
|
||||
// Return whether an option with the long key or short key was specified.
|
||||
bool hasOption(const std::string &longKey, const std::string &shortKey = "") const {
|
||||
if (!longKey.empty() && (longKeyMap.find(longKey) != longKeyMap.end())) {
|
||||
return true;
|
||||
}
|
||||
else if (!shortKey.empty() && (shortKeyMap.find(shortKey) != shortKeyMap.end())) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Corresponds to argv[0].
|
||||
const std::string &getDirectory() const {
|
||||
return directory;
|
||||
}
|
||||
|
||||
// No bounds checking, must be a valid index.
|
||||
const std::string getArgument(uint32_t index) const {
|
||||
return arguments[index];
|
||||
}
|
||||
|
||||
// Will be one less than argc.
|
||||
uint32_t getArgumentCount() const {
|
||||
return arguments.size();
|
||||
}
|
||||
};
|
||||
|
||||
// Parse and return the arguments in a structure that can be queried easily. Does not perform any validation of the arguments.
|
||||
Result parse(int argc, char *argv[]) {
|
||||
return Result(argc, argv);
|
||||
}
|
||||
};
|
||||
+1
Submodule thirdparty/libmspack added at 305907723a
Reference in New Issue
Block a user