Guest User

MusicalRangeInstance CPP

a guest
Feb 28th, 2017
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "MusicalRange.h"
  4. #include "MusicalRangeInstance.h"
  5. //#include "OnlineSubsystemSteam.h"
  6. //#include "ThirdParty/Steamworks/Steamv132/sdk/public/steam/steam_api.h"
  7.  
  8. //UE_LOG(LogSteamworks, Log, TEXT("Message!"));
  9.  
  10.  
  11. //Musical Range Game Instance Elements
  12. UMusicalRangeInstance::UMusicalRangeInstance() {
  13.  
  14. }
  15.  
  16. UMusicalRangeInstance::~UMusicalRangeInstance() {
  17.     if (CallbackAsyncVariable != nullptr)
  18.     {
  19.         UE_LOG(LogSteamworks, Log, TEXT("Deleting CallbackAsyncPointer"));
  20.         delete CallbackAsyncVariable;
  21.     }
  22. }
  23.  
  24. bool UMusicalRangeInstance::InitializeCPPElements() {
  25.     if (SteamAPI_Init())
  26.     {
  27.         UE_LOG(LogSteamworks, Log, TEXT("CPP Elements Init Called"));
  28.         CallbackAsyncVariable = new FSteamworksCallbackAsync();
  29.         UE_LOG(LogSteamworks, Log, TEXT("Assigning Game Instance object to CallbackAsyncVariable"));
  30.         CallbackAsyncVariable->AssignGameInstance(this);
  31.         return true;
  32.     }
  33.     else
  34.     {
  35.         UE_LOG(LogSteamworks, Warning, TEXT("CPP Elements Init FAILED! No Steam Log on"));
  36.     }
  37.     return false;
  38. }
  39.  
  40. bool UMusicalRangeInstance::InterLevelPersitentValuePONE() {
  41.     InterLevelPersitentValue++;
  42.     return true;
  43. }
  44.  
  45. bool UMusicalRangeInstance::RunSteamCallbacks() {
  46.     SteamAPI_RunCallbacks();
  47.     return true;
  48. }
  49.  
  50. void UMusicalRangeInstance::SteamOverlayActive() {
  51.     SteamAPICall_t hSteamAPICall = SteamUtils()->IsOverlayEnabled();
  52.     m_callResultGameOverlayActive.Set(hSteamAPICall, this, &UMusicalRangeInstance::OnSteamOverlayEnabled);
  53.     return;
  54. }
  55.  
  56. void UMusicalRangeInstance::OnSteamOverlayEnabled(GameOverlayActivated_t *pResult, bool bIOFailure) {
  57.     IsSteamOverlayActive = (pResult->m_bActive != 0);
  58.     if (IsSteamOverlayActive)
  59.     {
  60.         UE_LOG(LogSteamworks, Log, TEXT("Overlay is ACTIVE"));
  61.     }
  62.     else
  63.     {
  64.         UE_LOG(LogSteamworks, Log, TEXT("Overlay is OFF"));
  65.     }  
  66.     OnSteamOverlayIsActive(IsSteamOverlayActive);
  67.     return;
  68. }
  69.  
  70. //FSteamworksCallbackAsync elements
  71. FSteamworksCallbackAsync::FSteamworksCallbackAsync() :
  72.     OnSteamOverlayActiveCallback(this, &FSteamworksCallbackAsync::OnSteamOverlayActive)
  73. {
  74.     UE_LOG(LogSteamworks, Log, TEXT("Initializing FSteamworksCallbackAsync"));
  75.     if (GEngine && GEngine->GetWorld() != nullptr)
  76.     {
  77.         UE_LOG(LogSteamworks, Log, TEXT("Creating new LiveSteamSubsystem"));
  78.         LiveSteamSubsystem = new FOnlineSubsystemSteam();
  79.         UE_LOG(LogSteamworks, Log, TEXT("Initializing LiveSteamSubsystem"));
  80.         if (LiveSteamSubsystem->Init())
  81.         {
  82.             UE_LOG(LogSteamworks, Log, TEXT("LiveSteamSubsystem Succesfully Activated!"));
  83.         }
  84.         else {
  85.             UE_LOG(LogSteamworks, Error, TEXT("LiveSteamSubsystem FAILED!"));
  86.         }
  87.     }
  88.     UE_LOG(LogSteamworks, Warning, TEXT("LiveSteamSubsystem Not Created. GEngine OR GetWorld() not valid!"));
  89. }
  90.  
  91. FSteamworksCallbackAsync::~FSteamworksCallbackAsync() {
  92.     if (LiveSteamSubsystem != nullptr)
  93.     {
  94.         UE_LOG(LogSteamworks, Log, TEXT("Shutting Down LiveSteamSubsystem"));
  95.         LiveSteamSubsystem->Shutdown();
  96.         UE_LOG(LogSteamworks, Log, TEXT("Deleting LiveSteamSubsystem"));
  97.         delete LiveSteamSubsystem;
  98.     }
  99. }
  100.  
  101. void FSteamworksCallbackAsync::AssignGameInstance(UMusicalRangeInstance *NewMusicalRangeGameInstance) {
  102.     MusicalRangeGameInstance = NewMusicalRangeGameInstance;
  103.     UE_LOG(LogSteamworks, Log, TEXT("New Musical Range Game Instance Assigned to CallbackAsyncVariable"));
  104. }
  105.  
  106. void FSteamworksCallbackAsync::OnSteamOverlayActive(GameOverlayActivated_t *CallbackData) {
  107.     UE_LOG(LogSteamworks, Log, TEXT("Check CallbackData"));
  108.     check(CallbackData != nullptr);
  109.    
  110.     UE_LOG(LogSteamworks, Log, TEXT("Copy Callback Data"));
  111.     //Copy the CallbacData as it may be deleted to quickly https://answers.unrealengine.com/questions/569160/game-crash-runnable-thread-onlineasynctaskthreadst.html
  112.     const bool IsOverlayActive = (CallbackData->m_bActive != 0);
  113.  
  114.     UE_LOG(LogSteamworks, Log, TEXT("Execute Callback in Next Tick"));
  115.     LiveSteamSubsystem->ExecuteNextTick([IsOverlayActive, this]() {
  116.         if (IsOverlayActive)
  117.         {
  118.             UE_LOG(LogSteamworks, Log, TEXT("Overlay is ACTIVE"));
  119.         }
  120.         else
  121.         {
  122.             UE_LOG(LogSteamworks, Log, TEXT("Overlay is OFF"));
  123.         }
  124.         if (MusicalRangeGameInstance != nullptr)
  125.         {
  126.             UE_LOG(LogSteamworks, Log, TEXT("Calling Event OnSteamOverlayIsActive"));
  127.             MusicalRangeGameInstance->OnSteamOverlayIsActive(IsOverlayActive);
  128.             UE_LOG(LogSteamworks, Log, TEXT("Music Should be paused now"));
  129.         }
  130.         else
  131.         {
  132.             UE_LOG(LogSteamworks, Error, TEXT("Error with Musical Range Game Instance Pointer!"));
  133.             UE_LOG(LogSteamworks, Error, TEXT("OnSteamOverlayActive WILL NOT FIRING!"));
  134.         }
  135.     });
  136. }
RAW Paste Data