From 9a3e5015ab151a16e4697e27d180772e2aa76ef4 Mon Sep 17 00:00:00 2001 From: monsieurgustav Date: Thu, 5 Feb 2015 00:41:09 +0100 Subject: [PATCH] in editor, listen port only during play/simulate --- OSC/Source/OSC/OSC.Build.cs | 11 +++++ OSC/Source/OSC/Private/Osc.h | 4 ++ OSC/Source/OSC/Private/OscModule.cpp | 62 ++++++++++++++++++++++---- OSC/Source/OSC/Private/OscSettings.cpp | 4 +- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/OSC/Source/OSC/OSC.Build.cs b/OSC/Source/OSC/OSC.Build.cs index c23fa8e..0d9a9fe 100644 --- a/OSC/Source/OSC/OSC.Build.cs +++ b/OSC/Source/OSC/OSC.Build.cs @@ -39,6 +39,17 @@ namespace UnrealBuildTool.Rules ); bEnableExceptions = true; + + if (Target.Type.HasValue && TargetRules.IsEditorType(Target.Type.Value)) + { + Definitions.Add("OSC_EDITOR_BUILD=1"); + + PrivateDependencyModuleNames.Add("UnrealEd"); + } + else + { + Definitions.Add("OSC_EDITOR_BUILD=0"); + } } } } diff --git a/OSC/Source/OSC/Private/Osc.h b/OSC/Source/OSC/Private/Osc.h index 3a248ad..dca36ed 100644 --- a/OSC/Source/OSC/Private/Osc.h +++ b/OSC/Source/OSC/Private/Osc.h @@ -4,6 +4,10 @@ // Target engine version. Used to enable compatibility code. #define OSC_ENGINE_VERSION 40600 +#ifndef OSC_EDITOR_BUILD +#error OSC_EDITOR_BUILD must be set by the OSC.Build.cs +#endif + /* Public Dependencies *****************************************************************************/ diff --git a/OSC/Source/OSC/Private/OscModule.cpp b/OSC/Source/OSC/Private/OscModule.cpp index 5a3e4f5..6a062e5 100644 --- a/OSC/Source/OSC/Private/OscModule.cpp +++ b/OSC/Source/OSC/Private/OscModule.cpp @@ -1,6 +1,10 @@ #include "OscPrivatePCH.h" #include "OscDispatcher.h" +#if OSC_EDITOR_BUILD +#include "Editor.h" +#endif + #define LOCTEXT_NAMESPACE "FOscModule" @@ -16,6 +20,12 @@ public: return; } +#if OSC_EDITOR_BUILD + _isPlayingInEditor = false; + FEditorDelegates::BeginPIE.AddRaw(this, &FOscModule::OnBeginPIE); + FEditorDelegates::EndPIE.AddRaw(this, &FOscModule::OnEndPIE); +#endif + _dispatcher = UOscDispatcher::Get(); HandleSettingsSaved(); @@ -53,7 +63,9 @@ public: } else { +#if OSC_EDITOR_BUILD UE_LOG(LogOSC, Warning, TEXT("Settings changed registration failed")); +#endif } UE_LOG(LogOSC, Display, TEXT("Startup succeed")); } @@ -84,17 +96,15 @@ public: auto settings = GetMutableDefault(); // receive settings - FIPv4Address receiveAddress(0); - uint32_t receivePort; - if(UOscSettings::Parse(settings->ReceiveFrom, &receiveAddress, &receivePort)) +#if OSC_EDITOR_BUILD + if(_isPlayingInEditor) { - _dispatcher->Listen(receiveAddress, receivePort); + Listen(settings); } - else - { - UE_LOG(LogOSC, Warning, TEXT("Fail to parse receive address: %s"), *settings->ReceiveFrom); - } - +#else + Listen(settings); +#endif + // send settings settings->UpdateSendAddresses(); @@ -104,6 +114,40 @@ public: return true; } + void Listen(UOscSettings * settings) + { + FIPv4Address receiveAddress(0); + uint32_t receivePort; + if(UOscSettings::Parse(settings->ReceiveFrom, &receiveAddress, &receivePort)) + { + _dispatcher->Listen(receiveAddress, receivePort); + } + else + { + UE_LOG(LogOSC, Error, TEXT("Fail to parse receive address: %s"), *settings->ReceiveFrom); + } + } + +private: +#if OSC_EDITOR_BUILD + void OnBeginPIE(bool isSimulating) + { + _isPlayingInEditor = true; + + check(_dispatcher.IsValid()) + auto settings = GetMutableDefault(); + Listen(settings); + } + + void OnEndPIE(bool isSimulating) + { + _isPlayingInEditor = false; + _dispatcher->Stop(); + } + + bool _isPlayingInEditor; +#endif + private: TWeakObjectPtr _dispatcher; diff --git a/OSC/Source/OSC/Private/OscSettings.cpp b/OSC/Source/OSC/Private/OscSettings.cpp index f25200e..f1d6a72 100644 --- a/OSC/Source/OSC/Private/OscSettings.cpp +++ b/OSC/Source/OSC/Private/OscSettings.cpp @@ -35,7 +35,7 @@ void UOscSettings::UpdateSendAddresses() } else { - UE_LOG(LogOSC, Warning, TEXT("Fail to parse or invalid send address: %s"), *SendTargets[i]); + UE_LOG(LogOSC, Error, TEXT("Fail to parse or invalid send address: %s"), *SendTargets[i]); } } } @@ -69,7 +69,7 @@ void UOscSettings::Send(const uint8 *buffer, int32 length, int32 targetIndex) } else { - UE_LOG(LogOSC, Warning, TEXT("Cannot send OSC: invalid targetIndex %d"), targetIndex); + UE_LOG(LogOSC, Error, TEXT("Cannot send OSC: invalid targetIndex %d"), targetIndex); } }