Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreal Engine 4 Plugin #647

Merged
merged 5 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdks/UEAgones/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Binaries/
Intermediate/
Empty file added sdks/UEAgones/README.md
Empty file.
Binary file added sdks/UEAgones/Resources/Icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions sdks/UEAgones/Source/Agones/Agones.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using UnrealBuildTool;

public class Agones : ModuleRules
{
public Agones(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

PublicIncludePaths.AddRange(
new string[] {
});

PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
});

PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Http",
});
}
}
80 changes: 80 additions & 0 deletions sdks/UEAgones/Source/Agones/Agones.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "Agones.h"
#include "AgonesHook.h"

#if WITH_EDITOR
#include "AgonesSettings.h"
#include "ISettingsModule.h"
#include "ISettingsSection.h"
#include "UObject/Class.h"
#include "UObject/WeakObjectPtr.h"
#endif

#define LOCTEXT_NAMESPACE "AgonesModule"

void FAgonesModule::StartupModule()
{
FWorldDelegates::OnPostWorldInitialization.AddRaw(this, &FAgonesModule::OnWorldInitialized);


#if WITH_EDITOR
// register Agones settings
ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings");

if (SettingsModule != nullptr)
{
ISettingsSectionPtr SettingsSection = SettingsModule->RegisterSettings("Project", "Plugins", "Agones",
LOCTEXT("AgonesSettingsName", "Agones"),
LOCTEXT("AgonesSettingsDescription", "Configure the Agones plug-in."),
GetMutableDefault<UAgonesSettings>()
);
}
#endif //WITH_EDITOR
}

void FAgonesModule::ShutdownModule()
{
FWorldDelegates::OnPostWorldInitialization.RemoveAll(this);

if (HookPtr.IsValid())
{
HookPtr->Shutdown();
}

#if WITH_EDITOR
// unregister Agones settings
ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings");

if (SettingsModule != nullptr)
{
SettingsModule->UnregisterSettings("Project", "Plugins", "Agones");
}
#endif //WITH_EDITOR
}

void FAgonesModule::OnWorldInitialized(UWorld* World, const UWorld::InitializationValues IVS)
{
// Only start the agones hook if this is a dedicated server.
if (World != nullptr && World->GetNetMode() == ENetMode::NM_DedicatedServer)
{
HookPtr = MakeShareable(new FAgonesHook());
HookPtr->Ready();
}
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FAgonesModule, Agones)
115 changes: 115 additions & 0 deletions sdks/UEAgones/Source/Agones/AgonesHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "AgonesHook.h"
#include "AgonesSettings.h"
#include "Runtime/Online/HTTP/Public/Http.h"

#define LOCTEXT_NAMESPACE "AgonesHook"
DEFINE_LOG_CATEGORY(LogAgonesHook);

FAgonesHook::FAgonesHook()
: FTickableGameObject()
, CurrentHealthTime(0.0f)
, Settings(nullptr)
, ReadySuffix(FString(TEXT("/ready")))
, HealthSuffix(FString(TEXT("/health")))
, ShutdownSuffix(FString(TEXT("/shutdown")))
{
Settings = GetDefault<UAgonesSettings>();
check(Settings != nullptr);

UE_LOG(LogAgonesHook, Log, TEXT("Initialized Agones Hook, Sidecar address: %s, Health Enabled: %s, Health Ping: %f, Debug: %s")
, *Settings->AgonesSidecarAddress
, (Settings->bHealthPingEnabled ? TEXT("True") : TEXT("False"))
, Settings->HealthPingSeconds
, (Settings->bDebugLogEnabled ? TEXT("True") : TEXT("False")));
}

FAgonesHook::~FAgonesHook()
{
Settings = nullptr;
}

void FAgonesHook::Tick(float DeltaTime)
{
CurrentHealthTime += DeltaTime;
if (CurrentHealthTime >= Settings->HealthPingSeconds)
{
Health();
CurrentHealthTime = 0.0f;
}
}

bool FAgonesHook::IsTickable() const
{
return Settings->bHealthPingEnabled;
}

TStatId FAgonesHook::GetStatId() const
{
RETURN_QUICK_DECLARE_CYCLE_STAT(FAgonesHook, STATGROUP_Tickables);
}

bool FAgonesHook::IsTickableWhenPaused() const
{
return true;
}

static TSharedRef<IHttpRequest> MakeRequest(const FString& URL)
{
FHttpModule* http = &FHttpModule::Get();
TSharedRef<IHttpRequest> req = http->CreateRequest();
req->SetURL(URL);
req->SetVerb("POST");
req->SetHeader("Content-Type", "application/json");
req->SetContentAsString("{}");
return req;
}

void FAgonesHook::Ready()
{
SendRequest(Settings->AgonesSidecarAddress + ReadySuffix);
}

void FAgonesHook::Health()
{
SendRequest(Settings->AgonesSidecarAddress + HealthSuffix);
}

void FAgonesHook::Shutdown()
{
SendRequest(Settings->AgonesSidecarAddress + ShutdownSuffix);
}


bool FAgonesHook::SendRequest(const FString& URL)
{
TSharedRef<IHttpRequest> req = MakeRequest(URL);
bool bSuccess = req->ProcessRequest();
if (Settings->bDebugLogEnabled)
{
if (bSuccess)
{
UE_LOG(LogAgonesHook, Log, TEXT("Send: %s"), *URL);
}
else
{
UE_LOG(LogAgonesHook, Error, TEXT("ERROR - failed sending: %s"), *URL);
}
}
return bSuccess;
}

#undef LOCTEXT_NAMESPACE
63 changes: 63 additions & 0 deletions sdks/UEAgones/Source/Agones/AgonesHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "CoreMinimal.h"
#include "Tickable.h"

DECLARE_LOG_CATEGORY_EXTERN(LogAgonesHook, Verbose, All);

class FAgonesHook : public FTickableGameObject
{
public:

/** Default constructor */
FAgonesHook();

/** Deconstructor */
~FAgonesHook();

// FTickableObjectBase interface
virtual void Tick(float DeltaTime) override;
virtual bool IsTickable() const override;
virtual TStatId GetStatId() const override;
// End FTickableObjectBase interface

// FTickableGameObject interface
virtual bool IsTickableWhenPaused() const override;
// End FTickableGameObject interface

/** Sends ready request to sidecar **/
void Ready();
/** Sends health ping request to sidecar **/
void Health();
/** Sends shutdown request to sidecar **/
void Shutdown();

private:

/** Helper function to send requests with default debug output */
bool SendRequest(const FString& URL);

/** Time since last health ping */
float CurrentHealthTime;

/** Agones settings */
const class UAgonesSettings* Settings;

const FString ReadySuffix;
const FString HealthSuffix;
const FString ShutdownSuffix;
};
26 changes: 26 additions & 0 deletions sdks/UEAgones/Source/Agones/AgonesSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "AgonesSettings.h"

UAgonesSettings::UAgonesSettings()
: Super()
, AgonesSidecarAddress("http://localhost:59358")
, bHealthPingEnabled(false)
, HealthPingSeconds(5.0f)
, bDebugLogEnabled(false)
{
}


34 changes: 34 additions & 0 deletions sdks/UEAgones/Source/Agones/Public/Agones.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "CoreMinimal.h"
#include "ModuleManager.h"
#include "Engine/World.h"

class FAgonesModule : public IModuleInterface
{
public:

/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;

private:
void OnWorldInitialized(UWorld* World, UWorld::InitializationValues IVS);

/** Communicates with the Agones sidecar. */
TSharedPtr<class FAgonesHook> HookPtr;
};
Loading