//
// Created by Иван_Архипов on 23.11.2017.
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
#include <algorithm>

#ifdef WIN32
#include <direct.h>
#define mkdir(dir, mode) _mkdir(dir)
#endif

#include "../LotroDatPatcher.h"
#include "../Subfile.h"
using namespace LOTRO_DAT;

// Change these 2 variables to your path and name of .dat file
const std::string path = "";//"E:\\SteamLibrary\\steamapps\\common\\";//Lord Of The Rings Online\\";
const std::string filename = "client_local_English.dat";

int main() {
    const clock_t begin_time = clock();
    std::cout << "Gi1dor's LotRO .dat patcher ver. 1.0.3" << std::endl;
    freopen("errors.log", "w", stderr);

    setbuf(stdout, NULL);
    setbuf(stderr, NULL);

    setvbuf (stdout, NULL, _IONBF, BUFSIZ);
    setvbuf (stderr, NULL, _IONBF, BUFSIZ);

    Database db;
    db.InitDatabase("patch.db");

    try {
        DatFile *a = new DatFile((path + filename).c_str(), 0);
        std::cout << "Starting magic...\n";
        std::cout << "Files number: " << a->files_number() << std::endl;
        a->PatchAllDatabase(&db);
        delete a;
    } catch (std::exception &e) {
        fprintf(stderr, "Caught %s exception.", e.what());
        printf("Caught %s exception.", e.what());
        fflush(stdout);

        printf("Some critical errors occured. Need to stop execution. See information in errors.log file\n");
        fprintf(stderr, "Some critical errors occured. Need to stop execution now...\n");
    }

    fprintf(stdout, "Spent %f seconds on performing magic! Thank you for your patience!\n",
            float(clock() - begin_time) / CLOCKS_PER_SEC);
    system("pause");
    return 0;
}

