summaryrefslogtreecommitdiff
path: root/src/input/InputSource.cpp
blob: 73b79ec7e5a678dd6ac7be5a4af6f8bf7b25ee72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "InputSource.h"
#include "N64RomInput.h"

#include <fstream>
#include <stdexcept>

#ifdef GAMECUBE_SUPPORT
#include "gc/GameCubeInput.h"
#endif

std::shared_ptr<InputSource> InputSource::Create(const std::optional<std::filesystem::path>& path,
                                                 std::vector<uint8_t>& data) {
#ifdef GAMECUBE_SUPPORT
    if (path.has_value()) {
        if (auto gc = GC::GameCubeInput::FromPath(path.value())) {
            return gc;
        }
    } else if (auto gc = GC::GameCubeInput::FromMemory(data)) {
        return gc;
    }
#endif

    if (path.has_value()) {
        std::error_code ec;
        if (std::filesystem::is_directory(path.value(), ec)) {
            throw std::runtime_error(path->string() + " is a directory, not a ROM or an extracted GameCube disc");
        }
        std::ifstream input(path.value(), std::ios::binary);
        data = std::vector<uint8_t>(std::istreambuf_iterator(input), {});
        input.close();
    }

    return std::make_shared<N64RomInput>(std::move(data));
}