//---------------------------------------------------------------------------------------
// 
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
      }
    }
  }
//---------------------------------------------------------------------------------------
// 
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 the game is currently running, delay until it's not
    if (m_remote_client.is_load_compiled_binaries_requested() 
     && SkookumScript::get_initialization_level() < SkookumScript::InitializationLevel_gameplay)
      {
      // Makes sure the SkookumScript runtime object is initialized at this point
      ensure_runtime_initialized();

      // Load the Skookum class hierarchy scripts in compiled binary form
      bool is_first_time = !is_skookum_initialized();

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

      // After reloading, re-resolve the raw data of all dynamic classes
      #if WITH_EDITORONLY_DATA
        TArray<UObject*> blueprint_array;
        GetObjectsOfClass(UBlueprint::StaticClass(), blueprint_array, true, RF_ClassDefaultObject);
        for (UObject * obj_p : blueprint_array)
          {
          UBlueprint * blueprint_p = static_cast<UBlueprint *>(obj_p);
          if (blueprint_p->GeneratedClass)
            {
            SkClass * sk_class_p = SkUEClassBindingHelper::get_sk_class_from_ue_class(blueprint_p->GeneratedClass);
            if (sk_class_p)
              {
              SkUEClassBindingHelper::resolve_raw_data(sk_class_p, blueprint_p->GeneratedClass);
              }
            }
          }
      #endif

      if (is_first_time && is_skookum_initialized())
        {
        #if WITH_EDITOR
          // Recompile Blueprints in error state as such error state might have been due to SkookumScript not being initialized at the time of compile
          if (m_runtime.get_editor_interface())
            {
            m_runtime.get_editor_interface()->recompile_blueprints_with_errors();
            }
        #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));
      }
  }