Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Commit

Permalink
Adds UGameInstance::Init
Browse files Browse the repository at this point in the history
  • Loading branch information
iainmckay committed Aug 2, 2019
1 parent c630298 commit e7467cd
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ internal virtual void GetActorEyesViewPointInternal(out FVector OutLocation, out
OutRotation = default(FRotator);
}

internal virtual void InitInternal()
{
// This is eventually implemented in injected classes
}

/// <summary>
/// Looks for a given function name
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using UnrealEngine.Engine;
using UnrealEngine.Runtime;
using UnrealEngine.Runtime.Native;

namespace UnrealEngine.Engine
{
public partial class UGameInstance : UObject
{
private VTableHacks.CachedFunctionRedirect<VTableHacks.GameInstanceInitDel_ThisCall> initializeRedirect;
internal override void InitInternal()
{
Init();
}

/// <summary>
/// Allow custom GameInstances an opportunity to set up what it needs.
/// </summary>
public virtual void Init()
{
initializeRedirect
.Resolve(VTableHacks.GameInstanceInit, this)
.Invoke(Address);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private static void AddVTableRedirects()
IntPtr actorClass = Runtime.Classes.AActor;
IntPtr actorComponentClass = Runtime.Classes.UActorComponent;
IntPtr playerControllerClass = Runtime.Classes.APlayerController;
IntPtr gameInstanceClass = Runtime.Classes.UGameInstance;

GetLifetimeReplicatedProps = AddVTableRedirect(objectClass, "DummyRepProps", new GetLifetimeReplicatedPropsDel(OnGetLifetimeReplicatedProps));
PawnSetupPlayerInputComponent = AddVTableRedirect(pawnClass, "DummySetupPlayerInput", new PawnSetupPlayerInputComponentDel(OnPawnSetupPlayerInputComponent));
Expand All @@ -29,6 +30,7 @@ private static void AddVTableRedirects()
ActorComponentEndPlay = AddVTableRedirect(actorComponentClass, "DummyActorComponentEndPlay", new EndPlayDel(OnActorComponentEndPlay));
PlayerControllerSetupInputComponent = AddVTableRedirect(playerControllerClass, "DummyPlayerControllerSetupInputComponent", new PlayerControllerSetupInputComponentDel(OnPlayerControllerSetupInputComponent));
PlayerControllerUpdateRotation = AddVTableRedirect(playerControllerClass, "DummyPlayerControllerUpdateRotation", new PlayerControllerUpdateRotationDel(OnPlayerControllerUpdateRotation));
GameInstanceInit = AddVTableRedirect(gameInstanceClass, "DummyGameInstanceInit", new GameInstanceInitDel(OnGameInstanceInit));
}

private static void LogCallbackException(string functionName, Exception e)
Expand Down Expand Up @@ -186,6 +188,23 @@ private static void OnPlayerControllerUpdateRotation(IntPtr address, float delta
}
}

public static FunctionRedirect GameInstanceInit { get; private set; }
delegate void GameInstanceInitDel(IntPtr address);
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate void GameInstanceInitDel_ThisCall(IntPtr address);
private static void OnGameInstanceInit(IntPtr address)
{
try
{
UObject obj = GCHelper.Find(address);
obj.InitInternal();
}
catch (Exception e)
{
LogCallbackException(nameof(OnGameInstanceInit), e);
}
}

////////////////////////////////////////////////////////////////////////////////////////
// Add vtable redirects above this line
////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ActorComponentBeginPlayCallbackSig ActorComponentBeginPlayCallback = nullptr;
ActorComponentEndPlayCallbackSig ActorComponentEndPlayCallback = nullptr;
PlayerControllerSetupInputComponentCallbackSig PlayerControllerSetupInputComponentCallback = nullptr;
PlayerControllerUpdateRotationCallbackSig PlayerControllerUpdateRotationCallback = nullptr;
GameInstanceInitCallbackSig GameInstanceInitCallback = nullptr;

TMap<FString, void**> DummyNames;
CSEXPORT void CSCONV Export_VTableHacks_Set_VTableCallback(const FString& DummyName, void* Callback)
Expand All @@ -25,6 +26,7 @@ CSEXPORT void CSCONV Export_VTableHacks_Set_VTableCallback(const FString& DummyN
DummyNames.Add(TEXT("DummyActorComponentEndPlay"), (void**)&ActorComponentEndPlayCallback);
DummyNames.Add(TEXT("DummyPlayerControllerSetupInputComponent"), (void**)&PlayerControllerSetupInputComponentCallback);
DummyNames.Add(TEXT("DummyPlayerControllerUpdateRotation"), (void**)&PlayerControllerUpdateRotationCallback);
DummyNames.Add(TEXT("DummyGameInstanceInit"), (void**)&GameInstanceInitCallback);
}

void*** Element = DummyNames.Find(DummyName);
Expand Down
40 changes: 40 additions & 0 deletions Source/USharp/Private/VTableHacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,44 @@ class USHARP_API ADummyPlayerControllerUpdateRotation3 : public ADummyPlayerCont
{
FMsg::Logf("", 0, FName(TEXT("USharp")), ELogVerbosity::Log, TEXT("ADummyPlayerControllerUpdateRotation3-UpdateRotation"));
}
};

/////////////////////////////////////////////////////////////////////////////
// UGameInstance::Init
/////////////////////////////////////////////////////////////////////////////

typedef void(CSCONV *GameInstanceInitCallbackSig)(UGameInstance* Obj);
extern GameInstanceInitCallbackSig GameInstanceInitCallback;

UCLASS(NotBlueprintable, NotBlueprintType)
class USHARP_API UDummyGameInstanceInit1 : public UGameInstance
{
GENERATED_BODY()

public:
virtual void Init() override
{
if (GameInstanceInitCallback != nullptr)
{
GameInstanceInitCallback(this);
}
}
};

UCLASS(NotBlueprintable, NotBlueprintType)
class USHARP_API UDummyGameInstanceInit2 : public UDummyGameInstanceInit1
{
GENERATED_BODY()
};

UCLASS(NotBlueprintable, NotBlueprintType)
class USHARP_API UDummyGameInstanceInit3 : public UDummyGameInstanceInit2
{
GENERATED_BODY()

public:
virtual void Init() override
{
FMsg::Logf("", 0, FName(TEXT("USharp")), ELogVerbosity::Log, TEXT("UDummyGameInstanceInit3-Init"));
}
};

0 comments on commit e7467cd

Please sign in to comment.