Skip to content

Instantly share code, notes, and snippets.

@MattiaPezzanoAiv
Last active February 17, 2018 09:24
Show Gist options
  • Save MattiaPezzanoAiv/2264f64a76856ce1e1162133d6fe6c0e to your computer and use it in GitHub Desktop.
Save MattiaPezzanoAiv/2264f64a76856ce1e1162133d6fe6c0e to your computer and use it in GitHub Desktop.
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "AivMemory.h"
#define LOCTEXT_NAMESPACE "FAivMemoryModule"
void FAivMemoryModule::StartupModule()
{
PossibleCommands.Add("-v"); //it's vector curve
PossibleCommands.Add("-f"); //it's float curve
Seconds = 0;
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
FConsoleCommandDelegate ConsoleDelegate;
ConsoleDelegate.BindRaw(this, &FAivMemoryModule::SpawnCanary);
FConsoleCommandDelegate KillCanarys;
KillCanarys.BindRaw(this, &FAivMemoryModule::KillCanarys);
FConsoleCommandDelegate RemoveTick;
RemoveTick.BindRaw(this, &FAivMemoryModule::RemoveTick);
FConsoleCommandWithArgsDelegate SetScale;
SetScale.BindRaw(this, &FAivMemoryModule::SetScale);
FConsoleCommandWithArgsDelegate CursorMove;
CursorMove.BindRaw(this, &FAivMemoryModule::ActiveCursorMovement);
IConsoleManager::Get().RegisterConsoleCommand(TEXT("canary"), TEXT(""), ConsoleDelegate);
IConsoleManager::Get().RegisterConsoleCommand(TEXT("killall"), TEXT(""), KillCanarys);
IConsoleManager::Get().RegisterConsoleCommand(TEXT("rmv"), TEXT(""), RemoveTick);
IConsoleManager::Get().RegisterConsoleCommand(TEXT("scale"), TEXT("things"), SetScale);
IConsoleManager::Get().RegisterConsoleCommand(TEXT("movecursor"), TEXT("things"), CursorMove);
//start fsilvestro
TSharedPtr<FSylvester> Sylvester = MakeShareable(new FSylvester(TEXT("boo")));
/*UCanary* c = NewObject<UCanary>();
c->AddToRoot();
c->SetSylvester(Sylvester);*/
FTickerDelegate ticker;
ticker.BindRaw(this, &FAivMemoryModule::PrintStuffs);
//handle = FTicker::GetCoreTicker().AddTicker(ticker, 0);
/*FTickerDelegate curveTicker;
curveTicker.BindRaw(this, &FAivMemoryModule::MoveCursorOnCurve);
handle = FTicker::GetCoreTicker().AddTicker(ticker, 0);*/
}
void FAivMemoryModule::ActiveCursorMovement(const TArray<FString> &args)
{
if (args.Num() <= 0) //there is no args, load default path
{
if(!ParseZeroArgs()) return;
}
else if (args.Num() == 1) //one arg must be a string (curve path)
{
if (!ParseOneArg(args)) return;
}
else if (args.Num() > 1) //parse n float and build a custom curve
{
int argc = args.Num();
if (!PossibleCommands.Contains(args[0])) //it's not valid type of curve
{
UE_LOG(LogTemp, Error, TEXT("%s is not a valid command"),*args[0]);
return;
}
isVector = (args[0] == "-v"); //is vector arg
//init uobject
if (!isVector)
{
FloatCurve = NewObject<UCurveFloat>();
FloatCurve->AddToRoot();
UE_LOG(LogTemp, Error, TEXT("Float curve added to root and never been destoryed"));
}
else
{
VectorCurve = NewObject<UCurveVector>();
VectorCurve->AddToRoot();
UE_LOG(LogTemp, Error, TEXT("Vector curve added to root and never been destoryed"));
}
FRichCurve xRichData;
FRichCurve yRichData;
FRichCurve zRichData;
for (int i = 1; i < argc; i++)
{
FString arg = *args[i]; //get current arg
TArray<FString> splitted; //declare buffer to contain splitted line
args[i].ParseIntoArray(splitted,TEXT(","), false); //finally split current line with ','
if (!isVector) //build float curve
{
//expected only 2 args (time and float value)
if (splitted.Num() != 2) //error
{
UE_LOG(LogTemp, Error, TEXT("Unable to parse float arguments"));
FloatCurve->RemoveFromRoot();
UE_LOG(LogTemp, Error, TEXT("Float curve will be destroyed at next GC cycle"));
return; //stop program
}
float time = FCString::Atof(*splitted[0]);
float xValue = FCString::Atof(*splitted[1]);
xRichData.AddKey(time, xValue);
}
else
{
//expected 4 args (time and 4 float values)
if (splitted.Num() != 4) //error
{
UE_LOG(LogTemp, Error, TEXT("Unable to parse arguments"));
VectorCurve->RemoveFromRoot();
UE_LOG(LogTemp, Error, TEXT("Vector curve will be destroyed at next GC cycle"));
return; //stop program
}
float time = FCString::Atof(*splitted[0]);
float xValue = FCString::Atof(*splitted[1]);
float yValue = FCString::Atof(*splitted[2]);
float zValue = FCString::Atof(*splitted[3]);
xRichData.AddKey(time, xValue);
yRichData.AddKey(time, yValue);
zRichData.AddKey(time, zValue);
}
}
//After that check what type of curve i builded and add rich curves
if (!isVector)
{
//build float curve
FloatCurve->FloatCurve = xRichData;
}
else
{
//build vector curve
VectorCurve->FloatCurves[0] = xRichData;
VectorCurve->FloatCurves[1] = yRichData;
VectorCurve->FloatCurves[2] = zRichData;
}
FTickerDelegate curveTicker;
curveTicker.BindRaw(this, &FAivMemoryModule::MoveCursorOnRuntimeCurve);
handle = FTicker::GetCoreTicker().AddTicker(curveTicker, 0);
Seconds = 0;
return;
}
FTickerDelegate curveTicker;
curveTicker.BindRaw(this, &FAivMemoryModule::MoveCursorOnCurve);
handle = FTicker::GetCoreTicker().AddTicker(curveTicker, 0);
Seconds = 0;
}
bool FAivMemoryModule::MoveCursorOnRuntimeCurve(float DeltaTime)
{
if (!isVector)
{
float min;
float max;
FloatCurve->GetTimeRange(min, max);
if (Seconds > max)
{
FTicker::GetCoreTicker().RemoveTicker(handle);
FloatCurve->RemoveFromRoot();
UE_LOG(LogTemp, Error, TEXT("Float curve will be destroyed at next GC cycle"));
}
Seconds += DeltaTime;
float curveValue = FloatCurve->GetFloatValue(Seconds);
FSlateApplication::Get().SetCursorPos(FVector2D(curveValue, curveValue));
}
else
{
float min;
float max;
VectorCurve->GetTimeRange(min, max);
if (Seconds > max)
{
FTicker::GetCoreTicker().RemoveTicker(handle);
VectorCurve->RemoveFromRoot();
UE_LOG(LogTemp, Error, TEXT("Vector curve will be destroyed at next GC cycle"));
}
Seconds += DeltaTime;
FVector curveValue = VectorCurve->GetVectorValue(Seconds);
FSlateApplication::Get().SetCursorPos(FVector2D(curveValue.X, curveValue.Y));
}
return true;
}
bool FAivMemoryModule::MoveCursorOnCurve(float DeltaTime)
{
float min;
float max;
FloatCurve->GetTimeRange(min, max);
if (Seconds > max)
FTicker::GetCoreTicker().RemoveTicker(handle);
Seconds += DeltaTime;
float curveValue = FloatCurve->GetFloatValue(Seconds);
FSlateApplication::Get().SetCursorPos(FVector2D(curveValue, curveValue));
return true;
}
void FAivMemoryModule::SetScale(const TArray<FString> &args)
{
if (args.Num() <= 0 || args.Num() > 1)
{
UE_LOG(LogTemp, Error, TEXT("Arguments is not valid"));
return;
}
int scale = FCString::Atoi(*args[0]);
FSlateApplication::Get().SetApplicationScale(scale);
}
void FAivMemoryModule::RemoveTick()
{
FTicker::GetCoreTicker().RemoveTicker(handle);
}
bool FAivMemoryModule::PrintStuffs(float DeltaTime)
{
/*float scale = FMath::Sin(DeltaTime*0.5) +2;
FSlateApplication::Get().SetApplicationScale(scale);
FSlateApplication::Get().SetCursorRadius(1000000);*/
return true;
}
void FAivMemoryModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
}
void FAivMemoryModule::SpawnCanary()
{
UCanary* canary = NewObject<UCanary>(); //born and put it in orphan list
canary->AddToRoot();
CanaryList.Add(canary);
//canary->RemoveFromRoot();
}
void FAivMemoryModule::KillCanarys()
{
UE_LOG(LogTemp, Warning, TEXT("CANARYS %d"),CanaryList.Num());
for (int i = (CanaryList.Num() - 1); i >= 0; i--)
{
if (CanaryList[i].IsValid())
{
CanaryList[i].Get()->RemoveFromRoot();
CanaryList.RemoveAt(i);
}
}
}
//refactoring
bool FAivMemoryModule::ParseZeroArgs()
{
UE_LOG(LogTemp, Warning, TEXT("No args passed, use default path"));
FloatCurve = LoadObject<UCurveFloat>(NULL, TEXT("CurveFloat'/Game/CurveFloat.CurveFloat'"));
if (!FloatCurve)
{
UE_LOG(LogTemp, Warning, TEXT("curve not found in default path"));
return false;
}
return true;
}
bool FAivMemoryModule::ParseOneArg(const TArray<FString> &args)
{
UE_LOG(LogTemp, Warning, TEXT("Passed one argument, parsed as string"));
FString path = args[0]; //assert that is a path and load object
//try load float curve
FloatCurve = LoadObject<UCurveFloat>(NULL, *path);
if (!FloatCurve)
{
UE_LOG(LogTemp, Warning, TEXT("curve not found in input path"));
return false;
}
return true;
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FAivMemoryModule, AivMemory)
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Canary.h"
#include "HAL/ConsoleManager.h"
#include "Containers/Ticker.h"
#include "Runtime/Engine/Classes/Curves/CurveFloat.h"
#include "Runtime/Engine/Classes/Curves/CurveVector.h"
#include "Runtime/Core/Public/Misc/App.h"
#include "Runtime/Slate/Public/Framework/Application/SlateApplication.h"
#include "ModuleManager.h"
//custom class managed from smartpointer, class must inherit from TsharedFromThis
class FSylvester : public TSharedFromThis<FSylvester>
{
public:
FString Name;
FSylvester(FString NewName) : Name(NewName) //this is an initializer
{
UE_LOG(LogTemp, Warning, TEXT("SYLVESTER BORN"));
}
~FSylvester()
{
UE_LOG(LogTemp, Warning, TEXT("SYLVESTER DEAD"));
}
};
class FAivMemoryModule : public IModuleInterface
{
public:
/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;
protected:
void SpawnCanary();
void KillCanarys();
bool PrintStuffs(float DeltaTime);
void RemoveTick();
void SetScale(const TArray<FString> &args);
bool MoveCursorOnCurve(float DeltaTime);
bool MoveCursorOnRuntimeCurve(float DeltaTime);
void ActiveCursorMovement(const TArray<FString> &args);
TArray<FWeakObjectPtr> CanaryList;
FDelegateHandle handle;
UCurveFloat* FloatCurve;
UCurveVector* VectorCurve;
bool isVector;
private:
float Seconds;
bool ParseZeroArgs();
bool ParseOneArg(const TArray<FString> &args);
TArray<FString> PossibleCommands;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment