summaryrefslogtreecommitdiff
path: root/src/input/gc/GcmDisc.h
blob: 9a7cee6e7db5e9695cf624f36e6bc5cb8294571c (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once

#include "BlockReader.h"

#include <filesystem>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace GC {

struct FstEntry {
    uint64_t offset;
    uint32_t size;
};

class IDiscFileSystem {
public:
    virtual ~IDiscFileSystem() = default;
    virtual bool Exists(const std::string& path) = 0;
    virtual std::vector<uint8_t> ReadFile(const std::string& path) = 0;
    virtual std::string GetGameId() = 0;
    virtual uint8_t GetRevision() = 0;
};

class GcmDisc : public IDiscFileSystem {
public:
    static constexpr uint32_t MAGIC = 0xC2339F3D;
    static constexpr uint32_t BOOT_SIZE = 0x440;

    explicit GcmDisc(std::shared_ptr<IBlockReader> reader);

    bool Exists(const std::string& path) override;
    std::vector<uint8_t> ReadFile(const std::string& path) override;
    std::string GetGameId() override { return mGameId; }
    uint8_t GetRevision() override { return mRevision; }

    const std::map<std::string, FstEntry>& GetFiles() const { return mFiles; }

    static bool Probe(IBlockReader& reader);

private:
    void ParseFst(const std::vector<uint8_t>& fst);
    uint32_t GetDolSize();

    std::shared_ptr<IBlockReader> mReader;
    std::string mGameId;
    uint8_t mRevision;
    uint32_t mDolOffset;
    uint32_t mFstOffset;
    uint32_t mFstSize;
    std::map<std::string, FstEntry> mFiles;
};

class FolderDisc : public IDiscFileSystem {
public:
    explicit FolderDisc(std::filesystem::path root);

    bool Exists(const std::string& path) override;
    std::vector<uint8_t> ReadFile(const std::string& path) override;
    std::string GetGameId() override;
    uint8_t GetRevision() override;

    static bool Probe(const std::filesystem::path& root);

private:
    std::filesystem::path mRoot;
    std::optional<std::vector<uint8_t>> mBoot;
    void LoadBoot();
};

uint32_t DolFileSize(const uint8_t* header);

} // namespace GC