#include <windows.h>
#include <spdlog/spdlog.h>
#include <stdexcept>
#include <stdio.h>
#include <r3/binders/stub.h>

#if RE_DBG_INJECTED
#include <r3/binders/dbg_mem.h>
extern "C" {
// This is the part of Rayman3.exe main CRT setup that runs before main, but
// doesn't call it
void gh_pre_main(void);
}
#else
#include <r3/binders/static_mem.h>
#endif

extern "C" int r3_main(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR *cmdline, int showCmd);

int main(int argc, char **argv) {
  try {
#if RE_DBG_INJECTED
    gh_init_dbg_loader();
    gh_pre_main();
#else
    gh_init_data_segment();
#endif

    r3_main(GetModuleHandle(NULL), NULL, argv, SW_SHOW);
  } catch (const std::exception &e) {
    SPDLOG_ERROR("Unhandled exception: {}", e.what());
  }
  return 0;
}