summaryrefslogtreecommitdiff
path: root/mm/2s2h/Rando/Spoiler/HandleFileDropped.cpp
blob: f0658df951b53eaecdb57557b00e73c20e83bd18 (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
#include "Spoiler.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <filesystem>
#include <spdlog/spdlog.h>
#include "BenPort.h"
#include <libultraship/bridge/consolevariablebridge.h>
#include <ship/window/Window.h>
#include "2s2h/BenGui/Notification.h"

extern "C" {
#include "sfx.h"
}

bool Rando::Spoiler::HandleFileDropped(char* filePath) {
    try {
        std::ifstream fileStream(filePath);

        if (!fileStream.is_open()) {
            return false;
        }

        // Check if first byte is "{"
        if (fileStream.peek() != '{') {
            return false;
        }

        nlohmann::json j;

        // Attempt to parse the file
        try {
            fileStream >> j;
        } catch (nlohmann::json::exception& e) { return false; }

        // Check if the file is a spoiler file
        if (!j.contains("type") || j["type"] != "2S2H_RANDO_SPOILER") {
            return false;
        }

        // Save the spoiler file to the randomizer folder
        std::string spoilerFile = std::filesystem::path(filePath).filename().string();
        std::string spoilerFilePath =
            Ship::Context::GetPathRelativeToAppDirectory("randomizer/" + spoilerFile, appShortName);
        std::filesystem::copy_file(filePath, spoilerFilePath, std::filesystem::copy_options::overwrite_existing);

        // Set the spoiler file to the new file
        CVarSetString("gRando.SpoilerFile", spoilerFile.c_str());
        // Update the spoiler file options
        Rando::Spoiler::RefreshOptions();

        Audio_PlaySfx(NA_SE_SY_QUIZ_CORRECT);
        Notification::Emit({ .message = "Spoiler file loaded" });
        return true;
    } catch (std::exception& e) {
        SPDLOG_ERROR("Failed to load file: {}", e.what());
        Notification::Emit({ .message = "Failed to load file" });
        return false;
    } catch (...) {
        SPDLOG_ERROR("Failed to load file");
        Notification::Emit({ .message = "Failed to load file" });
        return false;
    }
}