// AUTO-GENERATED FILE, MOVE TO 'gh_fix' FOLDER PREVENT OVERWRITING!!!!!

#include <r3/binders/auto.h>
#include <gh_global.h>
#include <r3/config/static.hpp>

extern "C" {
undefined FUN_00470db0(void);    // 00470db0 // FUN_00470db0
undefined r3_checkDisc(void);    // 004464f0 // r3_checkDisc
undefined FUN_00503710(void);    // 00503710 // FUN_00503710
undefined r3_initAllModules(void); // 00401000 // r3_initAllModules
undefined FUN_00472150(void);    // 00472150 // FUN_00472150
undefined r3_wait_for_dvd(char *param_1, char *param_2,
                          undefined4 param_3); // 0043e540 // r3_wait_for_dvd
undefined FUN_004725a0(void);                  // 004725a0 // FUN_004725a0
int r3_get_gli_width1(void);                   // 0047baf0 // r3_get_gli_width1
undefined gfx_init2(void);                     // 00470be0 // gfx_init2
void g_setInitVar0(void);                      // 00401310 // g_setInitVar0
int r3_get_gli_height1(void);                  // 0047bb00 // r3_get_gli_height1
undefined FUN_004010b0(void);                  // 004010b0 // FUN_004010b0
undefined FUN_00401320(void);                  // 00401320 // FUN_00401320
void r3_noop(void *p_cTxt1, void *p_cTxt2);    // 00401100 // r3_noop
undefined r3_initEngine(void);                 // 00401220 // r3_initEngine
undefined FUN_005038e0(void);                  // 005038e0 // FUN_005038e0
undefined FUN_004fb300(void);                  // 004fb300 // FUN_004fb300
undefined spawnThread(void);                   // 004477d0 // spawnThread
undefined
r3_setupWindow(HINSTANCE hInstance, undefined4 param_2,
               undefined4 maximizeWindow); // 00402140 // r3_setupWindow

// 00401630
int r3_main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR *cmdline,
            int showCmd)

{
  undefined4 uVar1;
  undefined2 uVar3;
  LPSTR pCVar3;
  char *pcVar4;
  FILE *pFVar5;
  LONG LVar6;
  HMODULE hLibModule;
  DWORD DVar7;
  HANDLE hTargetProcessHandle;
  HANDLE hSourceHandle;
  HANDLE hSourceProcessHandle;
  LPSTR *ppCVar8;
  uint32_t uVar9;
  HWND hWnd;
  int iVar10;
  int iVar11;
  int iVar12;
  int iVar13;
  uint uVar14;
  void *unaff_ESI;
  undefined4 *puVar15;
  void *unaff_EDI;
  undefined4 *puVar16;
  char *pcVar17;
  undefined4 uStack_83c;
  HKEY pHStack_838;
  HANDLE pvStack_834;
  UINT UStack_830;
  BYTE aBStack_82c[4];
  DWORD DStack_828;
  DWORD DStack_824;
  tagRECT tStack_820;
  char acStack_810[260];
  CHAR languageStr[256];
  CHAR pathToUbi_ini[260];
  char acStack_508[260];
  CHAR windowsDir[260];
  char acStack_300[256];
  char acStack_200[256];
  char acStack_100[256];
  BOOL BVar18;
  char cVar1;
  undefined4 chars;
  DWORD dwOptions;
  tagRECT *lpRect;
  HANDLE *lpTargetHandle;
  undefined2 uVar2;
  undefined4 uVar22;

  pCVar3 = GetCommandLineA();

  R3Config &gameConfig = getDefaultConfig();

  // Store and change to game directory
  chdir(gameConfig.gameRootDir.c_str());
  strcpy(r3_main_data_005d28b6.gameDataDir, gameConfig.gameRootDir.c_str());

  strcpy(languageStr, gameConfig.language.c_str());
  sprintf(g_mutexName_Rayman3, s_percents, lpAppName_005b68f0);
  sprintf(g_windowTitle, s_percents, s_windowTitle);

  /* Setup localized quiting/restoring strings */
  iVar13 = strcmpi(languageStr, s_French_005b6828);
  if (iVar13 == 0) {
    sprintf(s_windowTitleRestoring, s_Restauration_fmt, s_windowTitle);
    chars = s_QUITTER + Field<4, 4>();
    puVar15 = (undefined4 *)s_ou_appuyez_sur_Echap_pour_quitte_005b67d0;
    puVar16 = (undefined4 *)s_quitting1;
    for (iVar13 = 0xb; uVar22 = s_QUITTER + Field<4, 4>(), iVar13 != 0;
         iVar13 = iVar13 + -1) {
      *puVar16 = *puVar15;
      puVar15 = puVar15 + 1;
      puVar16 = puVar16 + 1;
    }
    s_wndStrQuiting[0] = s_QUITTER[0];
    s_wndStrQuiting[1] = s_QUITTER[1];
    s_wndStrQuiting[2] = s_QUITTER[2];
    s_wndStrQuiting[3] = s_QUITTER[3];
    /* Wrong decompilation? */
    s_QUITTER[4] = (char)chars;
    s_QUITTER[5] = SUB41(chars, 1);
    s_QUITTER[6] = SUB41(chars, 2);
    s_QUITTER[7] = SUB41(chars, 3);
    s_wndStrQuiting[4] = s_QUITTER[4];
    s_wndStrQuiting[5] = s_QUITTER[5];
    s_wndStrQuiting[6] = s_QUITTER[6];
    s_wndStrQuiting[7] = s_QUITTER[7];
    s_QUITTER + Field<4, 4>() = uVar22;
    sprintf(lpString_0077d2c0, s__s___Pause_005b67c4, s_windowTitle);
    puVar15 = (undefined4 *)s_Restauration;
    puVar16 = (undefined4 *)s_wndStrRestoring;
    for (iVar13 = 7; iVar13 != 0; iVar13 = iVar13 + -1) {
      *puVar16 = *puVar15;
      puVar15 = puVar15 + 1;
      puVar16 = puVar16 + 1;
    }
  } else {
    iVar13 = strcmpi(languageStr, s_Spanish_005b67a0);
    if (iVar13 == 0) {
      sprintf(s_windowTitleRestoring, s__s___Restablecer_datos____005b6784,
              s_windowTitle);
      uVar2 = s_SALIR_005b675c + Field<4, 2>();
      puVar15 = (undefined4 *)s_Pulsa_ESC_para_salir_Rayman_3__005b6764;
      puVar16 = (undefined4 *)s_quitting1;
      for (iVar13 = 7; chars = s_SALIR_005b675c + Field<0, 4>(), iVar13 != 0;
           iVar13 = iVar13 + -1) {
        *puVar16 = *puVar15;
        puVar15 = puVar15 + 1;
        puVar16 = puVar16 + 1;
      }
      *(undefined2 *)puVar16 = *(undefined2 *)puVar15;
      *(undefined *)((int)puVar16 + 2) = *(undefined *)((int)puVar15 + 2);
      uVar3 = s_SALIR_005b675c + Field<4, 2>();
      uVar22 = s_SALIR_005b675c + Field<0, 4>();
      s_SALIR_005b675c[0] = (char)chars;
      s_SALIR_005b675c[1] = SUB41(chars, 1);
      s_SALIR_005b675c[2] = SUB41(chars, 2);
      s_SALIR_005b675c[3] = SUB41(chars, 3);
      s_wndStrQuiting[0] = s_SALIR_005b675c[0];
      s_wndStrQuiting[1] = s_SALIR_005b675c[1];
      s_wndStrQuiting[2] = s_SALIR_005b675c[2];
      s_wndStrQuiting[3] = s_SALIR_005b675c[3];
      s_SALIR_005b675c[4] = (char)uVar2;
      s_SALIR_005b675c[5] = SUB21(uVar2, 1);
      s_wndStrQuiting[4] = s_SALIR_005b675c[4];
      s_wndStrQuiting[5] = s_SALIR_005b675c[5];
      s_SALIR_005b675c + Field<0, 4>() = uVar22;
      s_SALIR_005b675c + Field<4, 2>() = uVar3;
      sprintf(lpString_0077d2c0, s__s___Rotura_005b6750, s_windowTitle);
      puVar15 = (undefined4 *)s_Restablecer_datos____005b6738;
      puVar16 = (undefined4 *)s_wndStrRestoring;
      for (iVar13 = 5; iVar13 != 0; iVar13 = iVar13 + -1) {
        *puVar16 = *puVar15;
        puVar15 = puVar15 + 1;
        puVar16 = puVar16 + 1;
      }
      *(undefined *)puVar16 = *(undefined *)puVar15;
    } else {
      iVar13 = strcmpi(languageStr, s_Italian_005b6730);
      if (iVar13 == 0) {
        sprintf(s_windowTitleRestoring, s__s___Ripristino_dati____005b6718,
                s_windowTitle);
        uVar2 = s_USCIRE_005b66ec + Field<4, 2>();
        puVar15 = (undefined4 *)s_Premi_ESC_per_uscire_da_Rayman_3_005b66f4;
        puVar16 = (undefined4 *)s_quitting1;
        for (iVar13 = 8; cVar1 = s_USCIRE_005b66ec[6], iVar13 != 0;
             iVar13 = iVar13 + -1) {
          *puVar16 = *puVar15;
          puVar15 = puVar15 + 1;
          puVar16 = puVar16 + 1;
        }
        s_wndStrQuiting[0] = s_USCIRE_005b66ec[0];
        s_wndStrQuiting[1] = s_USCIRE_005b66ec[1];
        s_wndStrQuiting[2] = s_USCIRE_005b66ec[2];
        s_wndStrQuiting[3] = s_USCIRE_005b66ec[3];
        *(undefined2 *)puVar16 = *(undefined2 *)puVar15;
        s_wndStrQuiting + Field<4, 3>() = CONCAT12(cVar1, uVar2);
        sprintf(lpString_0077d2c0, s__s___Pausa_005b66e0, s_windowTitle);
        s_wndStrRestoring[0] = s_Ripristino_dati____005b66cc[0];
        s_wndStrRestoring[1] = s_Ripristino_dati____005b66cc[1];
        s_wndStrRestoring[2] = s_Ripristino_dati____005b66cc[2];
        s_wndStrRestoring[3] = s_Ripristino_dati____005b66cc[3];
        s_wndStrRestoring[4] = s_Ripristino_dati____005b66cc[4];
        s_wndStrRestoring[5] = s_Ripristino_dati____005b66cc[5];
        s_wndStrRestoring[6] = s_Ripristino_dati____005b66cc[6];
        s_wndStrRestoring[7] = s_Ripristino_dati____005b66cc[7];
        s_wndStrRestoring[8] = s_Ripristino_dati____005b66cc[8];
        s_wndStrRestoring[9] = s_Ripristino_dati____005b66cc[9];
        s_wndStrRestoring[10] = s_Ripristino_dati____005b66cc[10];
        s_wndStrRestoring[0xb] = s_Ripristino_dati____005b66cc[0xb];
        s_wndStrRestoring + Field<12, 4>() =
            s_Ripristino_dati____005b66cc + Field<12, 4>();
        s_wndStrRestoring + Field<16, 2>() =
            s_Ripristino_dati____005b66cc + Field<16, 2>();
        s_wndStrRestoring[0x12] = s_Ripristino_dati____005b66cc[0x12];
      } else {
        iVar13 = strcmpi(languageStr, s_German_005b66c4);
        if (iVar13 == 0) {
          sprintf(s_windowTitleRestoring, s__s___Daten_Reparatur____005b66ac,
                  s_windowTitle);
          chars = s_BEENDIGEN_005b6678 + Field<0, 4>();
          puVar15 = (undefined4 *)&CHAR_E_005b6684;
          puVar16 = (undefined4 *)s_quitting1;
          for (iVar13 = 9; uVar2 = s_BEENDIGEN_005b6678 + Field<8, 2>(),
              uVar22 = s_BEENDIGEN_005b6678 + Field<4, 4>(), iVar13 != 0;
               iVar13 = iVar13 + -1) {
            *puVar16 = *puVar15;
            puVar15 = puVar15 + 1;
            puVar16 = puVar16 + 1;
          }
          *(undefined2 *)puVar16 = *(undefined2 *)puVar15;
          uVar1 = s_BEENDIGEN_005b6678 + Field<0, 4>();
          s_BEENDIGEN_005b6678[0] = (char)chars;
          s_BEENDIGEN_005b6678[1] = SUB41(chars, 1);
          s_BEENDIGEN_005b6678[2] = SUB41(chars, 2);
          s_BEENDIGEN_005b6678[3] = SUB41(chars, 3);
          s_wndStrQuiting[0] = s_BEENDIGEN_005b6678[0];
          s_wndStrQuiting[1] = s_BEENDIGEN_005b6678[1];
          s_wndStrQuiting[2] = s_BEENDIGEN_005b6678[2];
          s_wndStrQuiting[3] = s_BEENDIGEN_005b6678[3];
          s_BEENDIGEN_005b6678 + Field<0, 4>() = uVar1;
          *(undefined *)((int)puVar16 + 2) = *(undefined *)((int)puVar15 + 2);
          uVar3 = s_BEENDIGEN_005b6678 + Field<8, 2>();
          chars = s_BEENDIGEN_005b6678 + Field<4, 4>();
          s_BEENDIGEN_005b6678[4] = (char)uVar22;
          s_BEENDIGEN_005b6678[5] = SUB41(uVar22, 1);
          s_BEENDIGEN_005b6678[6] = SUB41(uVar22, 2);
          s_BEENDIGEN_005b6678[7] = SUB41(uVar22, 3);
          s_wndStrQuiting[4] = s_BEENDIGEN_005b6678[4];
          s_wndStrQuiting[5] = s_BEENDIGEN_005b6678[5];
          s_wndStrQuiting[6] = s_BEENDIGEN_005b6678[6];
          s_wndStrQuiting[7] = s_BEENDIGEN_005b6678[7];
          s_BEENDIGEN_005b6678[8] = (char)uVar2;
          s_BEENDIGEN_005b6678[9] = SUB21(uVar2, 1);
          s_wndStrQuiting[8] = s_BEENDIGEN_005b6678[8];
          s_wndStrQuiting[9] = s_BEENDIGEN_005b6678[9];
          s_BEENDIGEN_005b6678 + Field<4, 4>() = chars;
          s_BEENDIGEN_005b6678 + Field<8, 2>() = uVar3;
          sprintf(lpString_0077d2c0, s__s___Pause_005b67c4, s_windowTitle);
          s_wndStrRestoring[0] = s_Daten_Reparatur____005b6664[0];
          s_wndStrRestoring[1] = s_Daten_Reparatur____005b6664[1];
          s_wndStrRestoring[2] = s_Daten_Reparatur____005b6664[2];
          s_wndStrRestoring[3] = s_Daten_Reparatur____005b6664[3];
          s_wndStrRestoring[4] = s_Daten_Reparatur____005b6664[4];
          s_wndStrRestoring[5] = s_Daten_Reparatur____005b6664[5];
          s_wndStrRestoring[6] = s_Daten_Reparatur____005b6664[6];
          s_wndStrRestoring[7] = s_Daten_Reparatur____005b6664[7];
          s_wndStrRestoring[8] = s_Daten_Reparatur____005b6664[8];
          s_wndStrRestoring[9] = s_Daten_Reparatur____005b6664[9];
          s_wndStrRestoring[10] = s_Daten_Reparatur____005b6664[10];
          s_wndStrRestoring[0xb] = s_Daten_Reparatur____005b6664[0xb];
          s_wndStrRestoring[0xc] = s_Daten_Reparatur____005b6664[0xc];
          s_wndStrRestoring[0xd] = s_Daten_Reparatur____005b6664[0xd];
          s_wndStrRestoring[0xe] = s_Daten_Reparatur____005b6664[0xe];
          s_wndStrRestoring[0xf] = s_Daten_Reparatur____005b6664[0xf];
          s_wndStrRestoring[0x10] = s_Daten_Reparatur____005b6664[0x10];
          s_wndStrRestoring[0x11] = s_Daten_Reparatur____005b6664[0x11];
          s_wndStrRestoring[0x12] = s_Daten_Reparatur____005b6664[0x12];
        } else {
          sprintf(s_windowTitleRestoring, s__s___Restoring_data____005b664c,
                  s_windowTitle);
          chars = s_QUIT + Field<0, 4>();
          puVar15 = (undefined4 *)s_or_press_ESC_to_quit_Rayman_3__005b662c;
          puVar16 = (undefined4 *)s_quitting1;
          for (iVar13 = 7; cVar1 = s_QUIT[4], iVar13 != 0;
               iVar13 = iVar13 + -1) {
            *puVar16 = *puVar15;
            puVar15 = puVar15 + 1;
            puVar16 = puVar16 + 1;
          }
          *(undefined2 *)puVar16 = *(undefined2 *)puVar15;
          *(undefined *)((int)puVar16 + 2) = *(undefined *)((int)puVar15 + 2);
          uVar22 = s_QUIT + Field<0, 4>();
          s_QUIT[0] = (char)chars;
          s_QUIT[1] = SUB41(chars, 1);
          s_QUIT[2] = SUB41(chars, 2);
          s_QUIT[3] = SUB41(chars, 3);
          s_wndStrQuiting[0] = s_QUIT[0];
          s_wndStrQuiting[1] = s_QUIT[1];
          s_wndStrQuiting[2] = s_QUIT[2];
          s_wndStrQuiting[3] = s_QUIT[3];
          s_wndStrQuiting[4] = cVar1;
          s_QUIT + Field<0, 4>() = uVar22;
          sprintf(lpString_0077d2c0, s__s___Pause_005b67c4, s_windowTitle);
          s_wndStrRestoring[0] = s_Restoring_data_____005b6610[0];
          s_wndStrRestoring[1] = s_Restoring_data_____005b6610[1];
          s_wndStrRestoring[2] = s_Restoring_data_____005b6610[2];
          s_wndStrRestoring[3] = s_Restoring_data_____005b6610[3];
          s_wndStrRestoring[4] = s_Restoring_data_____005b6610[4];
          s_wndStrRestoring[5] = s_Restoring_data_____005b6610[5];
          s_wndStrRestoring[6] = s_Restoring_data_____005b6610[6];
          s_wndStrRestoring[7] = s_Restoring_data_____005b6610[7];
          s_wndStrRestoring[8] = s_Restoring_data_____005b6610[8];
          s_wndStrRestoring[9] = s_Restoring_data_____005b6610[9];
          s_wndStrRestoring[10] = s_Restoring_data_____005b6610[10];
          s_wndStrRestoring[0xb] = s_Restoring_data_____005b6610[0xb];
          s_wndStrRestoring + Field<12, 4>() =
              s_Restoring_data_____005b6610 + Field<12, 4>();
          s_wndStrRestoring + Field<16, 2>() =
              s_Restoring_data_____005b6610 + Field<16, 2>();
          s_wndStrRestoring[0x12] = s_Restoring_data_____005b6610[0x12];
        }
      }
    }
  }

  /* Create draw semaphore
     Initial count = 1
     Maximum count = 1 */
  g_drawSemaphore =
      CreateSemaphoreA((LPSECURITY_ATTRIBUTES)0x0, 1, 1, s_DRAWSEM_005b6608);
  if (hPrevInstance != (HINSTANCE)0x0) {
    return -1;
  }

  /* Load DirectPlay probably unused */
  // hLibModule = LoadLibraryA(s_dpnhpast_dll_005b65f8);
  // if (hLibModule == (HMODULE)0x0) {
  //   iVar13 = strcmpi(languageStr, s_French_005b6828);
  //   if (iVar13 == 0) {
  //     MessageBoxA((HWND)0x0, lpText_005b6598, s_Erreur_Rayman_3_005b65e4,
  //     0x10); return -1;
  //   }
  //   iVar13 = strcmpi(languageStr, s_Spanish_005b67a0);
  //   if (iVar13 == 0) {
  //     MessageBoxA((HWND)0x0, lpText_005b6540, s_Rayman_3_005b6588, 0x10);
  //     return -1;
  //   }
  //   iVar13 = strcmpi(languageStr, s_Italian_005b6730);
  //   if (iVar13 == 0) {
  //     MessageBoxA((HWND)0x0, lpText_005b64f0, s_Rayman_3_005b6588, 0x10);
  //     return -1;
  //   }
  //   iVar13 = strcmpi(languageStr, s_German_005b66c4);
  //   if (iVar13 != 0) {
  //     MessageBoxA((HWND)0x0, s_Check_your_version_of_DirectX__V_005b6430,
  //                 s_Rayman_3_Error_005b68bc, 0x10);
  //     return -1;
  //   }
  //   MessageBoxA((HWND)0x0, lpText_005b6478, s_Rayman_3_005b6588, 0x10);
  //   return -1;
  // }
  // FreeLibrary(hLibModule);

  /* For GLIMode
      1 = maximize
      0 = windowed */
  g_runMaximized = (int)(acStack_200[0] != '0');
  // r3_checkDisc();
  dwOptions = 0;
  BVar18 = 0;
  lpTargetHandle = &pvStack_834;
  DVar7 = 0x1f03ff;
  g_hinstance = hInstance;
  hTargetProcessHandle = GetCurrentProcess();
  hSourceHandle = GetCurrentThread();
  hSourceProcessHandle = GetCurrentProcess();
  DuplicateHandle(hSourceProcessHandle, hSourceHandle, hTargetProcessHandle,
                  lpTargetHandle, DVar7, BVar18, dwOptions);
  g_mainThreadHandle = pvStack_834;
  /* Copy cmdline */
  strcpy(g_appCmdLine, GetCommandLineA());

  // SetErrorMode(1);

  r3_initAllModules();
  iVar13 = r3_setupWindow(hInstance, showCmd, g_runMaximized);
  if (iVar13 == 0) {
    return -1;
  }
  g_setInitVar0();
  FUN_00401320();
  spawnThread();
  gfx_init2();
  lpRect = &tStack_820;
  hWnd = GetDesktopWindow();
  GetWindowRect(hWnd, lpRect);
  BVar18 = 1;
  iVar13 = r3_get_gli_height1();
  iVar13 = iVar13 + 0x20;
  iVar10 = r3_get_gli_width1();
  iVar10 = iVar10 + 0xc;
  iVar11 = r3_get_gli_height1();
  iVar11 = (tStack_820.bottom - iVar11) / 2;
  iVar12 = r3_get_gli_width1();
  MoveWindow(g_gameHWND, (tStack_820.right - iVar12) / 2, iVar11, iVar10,
             iVar13, BVar18);
  FUN_004010b0();
  SystemParametersInfoA(0x10, 0, &UStack_830, 0);
  SystemParametersInfoA(0x11, 0, (PVOID)0x0, 0);
  ShowCursor(0);
  uStack_83c = 0;
  SystemParametersInfoA(0x61, 1, &uStack_83c, 0);
  g_engineRunning = 1;
  r3_initEngine();
  FUN_004725a0();
  FUN_005038e0();
  FUN_00503710();
  FUN_004fb300();
  FUN_00472150();
  FUN_00470db0();
  uStack_83c = 0;
  SystemParametersInfoA(0x61, 0, &uStack_83c, 0);
  ShowCursor(1);
  SystemParametersInfoA(0x11, UStack_830, (PVOID)0x0, 0);
  CloseHandle(pvStack_834);

  return 0;
}
}
