~redstrate/libxiv

libxiv/src/installextract.cpp -rw-r--r-- 4.6 KiB
f3feece1Joshua Goins Add deprecation notice 7 months ago
                                                                                
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "installextract.h"

#ifdef UNSHIELD_SUPPORTED
#include <cstdio>
#include <array>
#include <stdexcept>
#include <libunshield.h>
#include <fmt/format.h>
#include <filesystem>

const std::array filesToExtract = {"data1.cab", "data1.hdr", "data2.cab"};

const std::array bootComponent = {
        "cef_license.txt",
        "FFXIV.ico",
        "ffxivboot.exe",
        "ffxivboot.ver",
        "ffxivboot64.exe",
        "ffxivconfig.exe",
        "ffxivconfig64.exe",
        "ffxivlauncher.exe",
        "ffxivlauncher64.exe",
        "ffxivsysinfo.exe",
        "ffxivsysinfo64.exe",
        "ffxivupdater.exe",
        "ffxivupdater64.exe",
        "FFXIV_sysinfo.ico",
        "icudt.dll",
        "libcef.dll",
        "license.txt",
        "locales/reserved.txt"
};

const std::array gameComponent = {
        "ffxivgame.ver"
};

void extractBootstrapFiles(const std::string_view installer, const std::string_view directory) {
    FILE* file = fopen(installer.data(), "rb");
    if(file == nullptr) {
        throw std::runtime_error("Failed to open installer {}" + std::string(installer));
    }

    fseek(file, 0, SEEK_END);
    size_t fileSize = ftell(file);
    rewind(file);

    auto buffer = static_cast<char*>(malloc(sizeof(char) * fileSize));

    fread(buffer, 1, fileSize, file);

    // some really, really bad code to basically read the cab files which are put right next to each other in the exe file
    char* lastNeedle = nullptr;
    std::string lastFilename;
    for(auto fileName : filesToExtract) {
        // all files are located in Disk1
        const std::string realFileName = fmt::format("Disk1\\{}", fileName);
        const char* needle = realFileName.c_str();

        // make unshield shut up
        unshield_set_log_level(0);

        // TODO: is this while really needed? it should only find it once
        while(true) {
            char* p = static_cast<char*>(memmem(buffer, fileSize, needle, realFileName.length()));
            if(!p)
                break;

            if(lastNeedle != nullptr) {
                FILE* newFile = fopen(lastFilename.data(), "wb");
                fmt::print("Wrote {}!\n", lastFilename);

                if(lastFilename == "data1.hdr") {
                    fwrite(lastNeedle + 30, p - lastNeedle - 42, 1, newFile);
                } else {
                    fwrite(lastNeedle + 32, p - lastNeedle - 42, 1, newFile);
                }

                // actual cab files start 32 bytes and we need to peel 42 bytes off of the end
                fclose(newFile);
            }

            lastFilename = fileName;
            lastNeedle = p;
            fileSize -= (p + 4) - buffer;
            buffer = p + realFileName.length();
        }

        // write final file
        FILE* newFile = fopen(lastFilename.data(), "wb");
        fmt::print("Wrote {}!\n", lastFilename);
        fwrite(lastNeedle + 33, fileSize - 42, 1, newFile);

        // actual cab files start 32 bytes and we need to peel 42 bytes off of the end
        fclose(newFile);
    }

    if(!std::filesystem::exists(directory)) {
        fmt::print("Game install directory doesn't exist yet, creating...\n");
        std::filesystem::create_directories(directory);
    }

    if(!std::filesystem::exists(std::filesystem::path(directory) / "boot")) {
        fmt::print("Boot directory doesn't exist yet, creating...\n");
        std::filesystem::create_directory(std::filesystem::path(directory) / "boot");
    }

    if(!std::filesystem::exists(std::filesystem::path(directory) / "game")) {
        fmt::print("Game directory doesn't exist yet, creating...\n");
        std::filesystem::create_directory(std::filesystem::path(directory) / "game");
    }

    auto unshield = unshield_open("data1.cab");
    const int fileCount = unshield_file_count(unshield);

    for(int i = 0 ; i < fileCount; i++) {
        const char* filename = unshield_file_name(unshield, i);

        for(auto bootName : bootComponent) {
            fmt::print("Extracted {}\n", bootName);

            if(strcmp(filename, bootName) == 0) {
                std::string saveFilename = fmt::format("{}/boot/{}", directory, bootName);
                unshield_file_save(unshield, i, saveFilename.data());
            }
        }

        for(auto gameName : gameComponent) {
            fmt::print("Extracted {}\n", gameName);

            if(strcmp(filename, gameName) == 0) {
                std::string saveFilename = fmt::format("{}/game/{}", directory, gameName);
                unshield_file_save(unshield, i, saveFilename.data());
            }
        }
    }

    fclose(file);
}

#else

void extractBootstrapFiles(const std::string_view installer, const std::string_view directory) {
    // no op
}

#endif