//---------------------------------------------------------------------------------------
// 
void FSkookumScriptRuntime::tick_remote()
  {
  if (!IsRunningCommandlet())
    {
    // Request recompilation of binaries if script files changed
    if (m_freshen_binaries_requested)
      {
      m_remote_client.cmd_compiled_state(true);
      m_freshen_binaries_requested = false;
      }

    // Remote communication to and from SkookumScript IDE.
    // Needs to be called whether in editor or game and whether paused or not
    // $Revisit - CReis This is probably a hack. The remote client update should probably
    // live somewhere other than a tick method such as its own thread.
    m_remote_client.process_incoming();

    // Re-load compiled binaries?
    if (m_remote_client.is_load_compiled_binaries_requested())
      {
      // Load the Skookum class hierarchy scripts in compiled binary form
      #if WITH_EDITOR
        bool is_first_time = !is_skookum_initialized();
      #endif

      bool success_b = m_runtime.load_compiled_scripts();
      SK_ASSERTX(success_b, AErrMsg("Unable to load SkookumScript compiled binaries!", AErrLevel_notify));
      m_remote_client.clear_load_compiled_binaries_requested();

      #if WITH_EDITOR
        if (is_first_time && is_skookum_initialized())
          {
          // When we load the binaries for the very first time, try regenerating all generated class script files again,
          // as the editor might have tried to generate them before but skipped because SkookumScript was not initialized yet
          m_runtime.get_editor_interface()->generate_all_class_script_files();
          // Also recompile Blueprints in error state as such error state might have been due to SkookumScript not being initialized at the time of compile
          m_runtime.get_editor_interface()->recompile_blueprints_with_errors();
          // Set world pointer now
          SkUEClassBindingHelper::set_world(m_game_world_p);
          }
      #endif
      }
    }
  }
//---------------------------------------------------------------------------------------
// This code will execute after your module is loaded into memory (but after global
// variables are initialized, of course.)
void FSkookumScriptRuntime::StartupModule()
  {
  A_DPRINT(A_SOURCE_STR " Starting up SkookumScript plug-in modules\n");

  // Note that FWorldDelegates::OnPostWorldCreation has world_p->WorldType set to None
  // Note that FWorldDelegates::OnPreWorldFinishDestroy has world_p->GetName() set to "None"

  m_on_world_init_pre_handle  = FWorldDelegates::OnPreWorldInitialization.AddRaw(this, &FSkookumScriptRuntime::on_world_init_pre);
  m_on_world_init_post_handle = FWorldDelegates::OnPostWorldInitialization.AddRaw(this, &FSkookumScriptRuntime::on_world_init_post);
  m_on_world_cleanup_handle   = FWorldDelegates::OnWorldCleanup.AddRaw(this, &FSkookumScriptRuntime::on_world_cleanup);

  // Hook up Unreal memory allocator
  AMemory::override_functions(&Agog::malloc_func, &Agog::free_func, &Agog::req_byte_size_func);

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Start up SkookumScript
  #if !WITH_EDITOR
    // If no editor, initialize right away
    // otherwise wait until map is loaded
    m_runtime.startup();

    #ifndef SKOOKUM_REMOTE_UNREAL
      bool success_b = m_runtime.load_compiled_scripts();
      SK_ASSERTX(success_b, AErrMsg("Unable to load SkookumScript compiled binaries!", AErrLevel_notify));
    #endif
  #endif

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Send off connect request to IDE
  // Come back later to check on it
  #ifdef SKOOKUM_REMOTE_UNREAL
    if (!IsRunningCommandlet())
      {
      m_remote_client.set_mode(SkLocale_runtime);
      }
  #endif
  }
//---------------------------------------------------------------------------------------
// 
void FSkookumScriptRuntime::on_editor_map_opened()
  {
  // When editor is present, initialize Sk here
  if (!m_runtime.is_initialized())
    {
    m_runtime.startup();

    #ifdef SKOOKUM_REMOTE_UNREAL
      // At this point, have zero patience with the IDE and launch it if not connected
      if (!IsRunningCommandlet())
        {
        // At this point, wait if necessary to make sure we are connected
        m_remote_client.ensure_connected(0.0);
        // Kick off re-compilation of the binaries
        m_remote_client.cmd_compiled_state(true);
        m_freshen_binaries_requested = false; // Request satisfied
        }
    #else
      // If no remote connection, load binaries at this point
      bool success_b = m_runtime.load_compiled_scripts();
      SK_ASSERTX(success_b, AErrMsg("Unable to load SkookumScript compiled binaries!", AErrLevel_notify));
    #endif
    }
  }
void FSkookumScriptRuntime::compile_and_load_binaries()
  {
  #ifdef SKOOKUM_REMOTE_UNREAL
    // At this point, have zero patience with the IDE and launch it if not connected
    if (!IsRunningCommandlet())
      {
      // At this point, wait if necessary to make sure we are connected
      m_remote_client.ensure_connected(0.0);

      // Alert user in case we are still not connected - and allow for corrective measures
      bool load_binaries = true;
      #if WITH_EDITOR
        while (!m_remote_client.is_authenticated())
          {
          FText title = FText::FromString(TEXT("SkookumScript UE4 Plugin cannot connect to the SkookumIDE!"));
          EAppReturnType::Type decision = FMessageDialog::Open(
            EAppMsgType::CancelRetryContinue,
            FText::Format(FText::FromString(TEXT(
              "The SkookumScript UE4 Plugin cannot connect to the SkookumIDE. A connection to the SkookumIDE is required to properly work with SkookumScript.\n\n"
              "The connection problem could be caused by any of the following situations:\n"
              "- The SkookumIDE application is not running. If this is the case, your security software (Virus checker, VPN, Firewall) may have blocked or even removed it. If so, allow SkookumIDE.exe to run, then click 'Retry'. "
              "You can also try to launch the IDE manually. It should be located at the following path: {0}. Once running, click 'Retry'.\n"
              "- The SkookumIDE application is running, but stuck on an error. If so, try to resolve the error, and when the SkookumIDE is back up, click 'Retry'.\n"
              "- The SkookumIDE application is running and seems to be working fine. "
              "If so, the IP and port that the SkookumScript UE4 Plugin is trying to connect to ({1}) might be different from the IP and port that the SkookumIDE is listening to (see SkookumIDE log window), or blocked by a firewall. "
              "These problems could be due to your networking environment, such as a custom firewall, virtualization software such as VirtualBox, or multiple network adapters.\n\n"
              "For additional information including how to specify the SkookumIDE address for the runtime, please see http://skookumscript.com/docs/v3.0/ide/ip-addresses/ and ensure 'Settings'->'Remote runtimes' on the SkookumIDE is set properly.\n\n"
              "If you are having difficulties resolving this issue, please don't hesitate to ask us for help at the SkookumScript Forum (http://forum.skookumscript.com). We are here to make your experience skookum!\n")), 
              FText::FromString(FPaths::ConvertRelativePathToFull(IPluginManager::Get().FindPlugin(TEXT("SkookumScript"))->GetBaseDir() / TEXT("SkookumIDE") / TEXT("SkookumIDE.exe"))),
              FText::FromString(m_remote_client.get_ip_address_ide()->ToString(true))),
            &title);
          if (decision != EAppReturnType::Retry)
            {
            load_binaries = (decision == EAppReturnType::Continue);
            break;
            }
          m_remote_client.ensure_connected(10.0);
          }
      #endif

      if (load_binaries && m_remote_client.is_authenticated())
        {
      #if WITH_EDITOR
        RetryCompilation:
      #endif
        // Block while binaries are being recompiled
        m_remote_client.cmd_compiled_state(true);
        m_freshen_binaries_requested = false; // Request satisfied
        while (!m_remote_client.is_load_compiled_binaries_requested()
            && !m_remote_client.is_compiled_binaries_have_errors())
          {
          m_remote_client.wait_for_update();
          }
        #if WITH_EDITOR
          if (m_remote_client.is_compiled_binaries_have_errors())
            {
            FText title = FText::FromString(TEXT("Compilation errors!"));
            EAppReturnType::Type decision = FMessageDialog::Open(
              EAppMsgType::CancelRetryContinue,
              FText::FromString(TEXT(
                "The SkookumScript compiled binaries could not be generated because errors were found in the script files.\n\n")),
              &title);
            if (decision == EAppReturnType::Retry)
              {
              m_remote_client.clear_load_compiled_binaries_requested();
              goto RetryCompilation;
              }
            load_binaries = (decision == EAppReturnType::Continue);
            }
        #endif
        m_remote_client.clear_load_compiled_binaries_requested();
        }

      if (load_binaries)
        {
        // Attempt to load binaries at this point
        bool success_b = m_runtime.load_compiled_scripts();
        if (success_b)
          {
          // Inform the IDE about the version we got
          m_remote_client.cmd_incremental_update_reply(true, SkBrain::ms_session_guid, SkBrain::ms_revision);
          }
        else
          {
          // Something went wrong - let the user know
          FText title = FText::FromString(TEXT("Unable to load SkookumScript compiled binaries!"));
          FMessageDialog::Open(
            EAppMsgType::Ok,
            FText::FromString(TEXT(
              "Unable to load the compiled binaries. This is most likely caused by errors in the script files which prevented a successful compilation. The project will continue to load with SkookumScript temporarily disabled.")),
            &title);
          }
        }
      }
    else
  #endif
      {
      // If no remote connection, or commandlet mode, load binaries at this point
      bool success_b = m_runtime.load_compiled_scripts();
      SK_ASSERTX(success_b, AErrMsg("Unable to load SkookumScript compiled binaries!", AErrLevel_notify));
      }
  }