| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- // Copyright ECG . All Rights Reserved.
- #include "BlueprintUtilityBPLibrary.h"
- #include "BlueprintUtility.h"
- #include "Runtime/Engine/Classes/Engine/Engine.h"
- #include "ImageUtils.h"
- #include "IImageWrapper.h"
- #include "IImageWrapperModule.h"
- //#include "ImageLoader.h"
- //#include "../Public/FileHelper.h"
- #include "SecureHash.h"
- #include "Engine/Texture2D.h"
- #include "HighResScreenshot.h"
- #include "Kismet/KismetRenderingLibrary.h"
- UBlueprintUtilityBPLibrary::UBlueprintUtilityBPLibrary(const FObjectInitializer& ObjectInitializer)
- : Super(ObjectInitializer)
- {
- }
- //Use ContentDir()
- FString UBlueprintUtilityBPLibrary::GetFullPath(FString FilePath)
- {
- //Check Relative Or absolute path
- FString FullFilePath;
- if (FilePath.StartsWith(".", ESearchCase::CaseSensitive))
- {
- FullFilePath = *FPaths::Combine(FPaths::ProjectContentDir(), FilePath);
- FullFilePath = *FPaths::ConvertRelativePathToFull(FullFilePath);
- }
- else
- {
- FullFilePath = FilePath;
- }
- return FullFilePath;
- }
- float UBlueprintUtilityBPLibrary::BlueprintUtilitySampleFunction(float Param)
- {
- return -1;
- }
- //Discern Texture Type
- static TSharedPtr<IImageWrapper> GetImageWrapperByExtention(const FString InImagePath)
- {
- IImageWrapperModule& ImageWrapperModule = FModuleManager::GetModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
-
- if (InImagePath.EndsWith(".png"))
- {
- return ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
- }
- else if (InImagePath.EndsWith(".jpg") || InImagePath.EndsWith(".jpeg"))
- {
- return ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
- }
- else if (InImagePath.EndsWith(".bmp"))
- {
- return ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);
- }
- else if (InImagePath.EndsWith(".ico"))
- {
- return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO);
- }
- else if (InImagePath.EndsWith(".exr"))
- {
- return ImageWrapperModule.CreateImageWrapper(EImageFormat::EXR);
- }
- else if (InImagePath.EndsWith(".icns"))
- {
- return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS);
- }
- return nullptr;
- }
- UTexture2D* UBlueprintUtilityBPLibrary::LoadTexture2DFromFile(const FString& FilePath,
- bool& IsValid, int32& Width, int32& Height)
- {
- FString FullFilePath = GetFullPath(FilePath);
- if (!IsVaildPath(FullFilePath))
- {
- return NULL;
- }
-
- IsValid = false;
- UTexture2D* LoadedT2D = NULL;
- TSharedPtr<IImageWrapper> ImageWrapper = GetImageWrapperByExtention(FullFilePath);
-
- TArray<uint8> RawFileData;
- if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath, 0)) return NULL ;
- if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
- {
- TArray<uint8> UncompressedBGRA ;
- if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
- {
- LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
- if (!LoadedT2D) return NULL;
- Width = ImageWrapper->GetWidth();
- Height = ImageWrapper->GetHeight();
- void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
- FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
- LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();
- LoadedT2D->UpdateResource();
- }
- }
- IsValid = true;
- return LoadedT2D;
- }
- UImageLoader* UBlueprintUtilityBPLibrary::LoadTexture2DFromFile_Async(const FString& FilePath,const FString& ID)
- {
- UImageLoader* Loader = NewObject<UImageLoader>();
- Loader->LoadImageAsync(FilePath,ID);
- return Loader;
- }
- UTexture2D* UBlueprintUtilityBPLibrary::BytesToTexture2d(const TArray<uint8> bytes)
- {
- UTexture2D* LoadedT2D = NULL;
- IImageWrapperModule& ImageWrapperModule = FModuleManager::Get().LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
- EImageFormat Format = ImageWrapperModule.DetectImageFormat(bytes.GetData(), bytes.Num());
- TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(Format);
- if (!ImageWrapper.IsValid())
- {
- return nullptr;
- }
- if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(bytes.GetData(), bytes.Num()))
- {
- TArray<uint8> UncompressedBGRA;
- if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
- {
- LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
- if (!LoadedT2D) return NULL;
- void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
- FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
- LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();
- LoadedT2D->UpdateResource();
- }
- }
- return LoadedT2D;
- }
- bool UBlueprintUtilityBPLibrary::ReadOggWaveData(class USoundWave* sw, TArray<uint8>* rawFile)
- {
- //FSoundQualityInfo info;
- //FVorbisAudioInfo vorbisObj ;
- //if (!vorbisObj.ReadCompressedInfo(rawFile->GetData(), rawFile->Num(), &info))
- //{
- // //Debug("Can't load header");
- // return true;
- //}
- //if (!sw) return true;
- //sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;
- //sw->NumChannels = info.NumChannels;
- //sw->Duration = info.Duration;
- //sw->RawPCMDataSize = info.SampleDataSize;
- //sw->SetSampleRate(info.SampleRate);
- //sw->RawData ;
- return false;
- }
- bool UBlueprintUtilityBPLibrary::ReadWavWaveData(class USoundWave* sw, TArray<uint8>* rawFile)
- {
- return false;
- }
- class USoundWave* UBlueprintUtilityBPLibrary::LoadWaveDataFromFile(const FString& FilePath)
- {
- USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());
- if (!sw)
- return nullptr;
- FString FullPath = GetFullPath(FilePath);
- TArray < uint8 > rawFile;
- FFileHelper::LoadFileToArray(rawFile, FullPath.GetCharArray().GetData());
- FWaveModInfo WaveInfo;
- if (WaveInfo.ReadWaveInfo(rawFile.GetData(), rawFile.Num()))
- {
- sw->InvalidateCompressedData();
- sw->RawData.Lock(LOCK_READ_WRITE);
- void* LockedData = sw->RawData.Realloc(rawFile.Num());
- FMemory::Memcpy(LockedData, rawFile.GetData(), rawFile.Num());
- sw->RawData.Unlock();
- int32 DurationDiv = *WaveInfo.pChannels * *WaveInfo.pBitsPerSample * *WaveInfo.pSamplesPerSec;
- if (DurationDiv)
- {
- sw->Duration = *WaveInfo.pWaveDataSize * 8.0f / DurationDiv;
- }
- else
- {
- sw->Duration = 0.0f;
- }
-
- sw->SetSampleRate(*WaveInfo.pSamplesPerSec);
- sw->NumChannels = *WaveInfo.pChannels;
- sw->RawPCMDataSize = WaveInfo.SampleDataSize;
- sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;
- }
- else {
- return nullptr;
- }
- return sw;
- }
- class USoundWave* UBlueprintUtilityBPLibrary::LoadOggDataFromFile(const FString& FilePath)
- {
- USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());
- if (!sw)
- return NULL;
- //* If true the song was successfully loaded
- bool loaded = false;
-
- FString FullPath = GetFullPath(FilePath);
- //* loaded song file (binary, encoded)
- TArray < uint8 > rawFile;
- loaded = FFileHelper::LoadFileToArray(rawFile, FullPath.GetCharArray().GetData());
- if (loaded)
- {
- FByteBulkData* bulkData = &sw->CompressedFormatData.GetFormat(TEXT("OGG"));
- //sw->RawData = sw->CompressedFormatData.GetFormat(TEXT("OGG"));
- bulkData->Lock(LOCK_READ_WRITE);
- FMemory::Memcpy(bulkData->Realloc(rawFile.Num()), rawFile.GetData(), rawFile.Num());
- bulkData->Unlock();
-
- sw->RawData = *bulkData;
- loaded = ReadOggWaveData(sw, &rawFile) == 0 ? true : false;
- }
- if (!loaded)
- return NULL;
- return sw;
- }
- void UBlueprintUtilityBPLibrary::ReadConfig(const FString& SectionName, const FString& ValueName, bool& succeed, FString &ReturnValue)
- {
- //GConfig->Flush(true, GGameIni);
- //bool succeed = false;
- succeed = GConfig->GetString(
- *SectionName,
- *ValueName,
- ReturnValue,
- GGameIni
- );
- UE_LOG(LogTemp, Warning, TEXT("Read Config %s "),succeed ? TEXT("Succeed") : TEXT("Fail"));
-
- }
- void UBlueprintUtilityBPLibrary::WriteConfig(const FString& SectionName, const FString& ValueName, const FString &ReturnValue)
- {
- //FString newSection = "/Script/CommunicationSetting";
- //FString TA = "DefaultMyConfig";
- GConfig->SetString(
- *SectionName,
- *ValueName,
- *ReturnValue,
- GGameIni
- );
- GConfig->Flush(false, GGameIni);
- /*
- FString log;
- ReadConfig(ReturnValue, ValueName, log);
- UE_LOG(LogTemp, Warning, TEXT("Set Config As %s "), *log);*/
- }
- bool UBlueprintUtilityBPLibrary::ReadCustomPathConfig(const FString&FilePath, const FString& SectionName, const FString& ValueName, FString &ReturnString)
- {
- FString FullPath = GetFullPath(FilePath);
- GConfig->Flush(true, FullPath);
- bool succeed = GConfig->GetString(*SectionName, *ValueName, ReturnString, FullPath);
- return succeed;
- }
- void UBlueprintUtilityBPLibrary::WriteCustomPathConfig(const FString&FilePath, const FString& SectionName, const FString& ValueName, const FString &WriteString)
- {
- FString FullPath = GetFullPath(FilePath);
- IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
- // Does the file exist?
- if (!PlatformFile.FileExists(*FullPath))
- {
- // File doesn't exist; (Attempt to) create a new one.
- FFileHelper::SaveStringToFile(TEXT(""), *FullPath);
- }
- GConfig->SetString(*SectionName, *ValueName, *WriteString, FullPath);
- GConfig->Flush(false, FullPath);
- }
- void UBlueprintUtilityBPLibrary::RefrashAllSkeletallAnimation()
- {
- for (TObjectIterator<AActor> iterator; iterator; ++iterator)
- {
- if (iterator)
- {
- for (auto actor_Component : iterator->GetComponents())
- {
- if (actor_Component->IsA(USkeletalMeshComponent::StaticClass()))
- {
- (Cast<USkeletalMeshComponent>(actor_Component))->TickAnimation(0.0f, false);
- (Cast<USkeletalMeshComponent>(actor_Component))->RefreshBoneTransforms();
- }
- }
- }
- }
- };
- bool UBlueprintUtilityBPLibrary::ReadFile(const FString FilePath, FString& ReturnString)
- {
- FString FullPath = GetFullPath(FilePath);
- FString Cache = "";
- bool Sucess = false;
- Sucess = FFileHelper::LoadFileToString(Cache, FullPath.GetCharArray().GetData());
- ReturnString = Cache;
- return Sucess;
- }
- bool UBlueprintUtilityBPLibrary::WriteFile(const FString FilePath, const FString ReturnString)
- {
- FString FullPath = GetFullPath(FilePath);
- bool Sucess;
- Sucess = FFileHelper::SaveStringToFile(ReturnString, *FullPath);
- return Sucess;
- }
- bool UBlueprintUtilityBPLibrary::WriteFileByte(TArray<uint8> bytes, const FString FilePath)
- {
- FString FullPath = GetFullPath(FilePath);
- bool Sucess;
- Sucess = FFileHelper::SaveArrayToFile(bytes, *FilePath);
-
- return Sucess;
- }
- bool UBlueprintUtilityBPLibrary::DeleteFile(const FString FilePath)
- {
- FString FullPath = GetFullPath(FilePath);
- IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
-
- if (PlatformFile.DeleteFile(*FullPath))
- {
- UE_LOG(LogTemp, Warning, TEXT("deleteFile: Delete the flie successfully!"));
-
- return true;
- }
- else
- {
- UE_LOG(LogTemp, Warning, TEXT("deleteFile: Not delete the flie!"));
-
- return false;
- }
- }
- bool UBlueprintUtilityBPLibrary::DeleteFiles(const FString FilePath)
- {
- FString FullPath = GetFullPath(FilePath);
- IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
- if (PlatformFile.DeleteDirectoryRecursively(*FullPath))
- {
- UE_LOG(LogTemp, Warning, TEXT("deleteFile: Delete the flie successfully!"));
- return true;
- }
- else
- {
- UE_LOG(LogTemp, Warning, TEXT("deleteFile: Not delete the flie!"));
- return false;
- }
- }
- bool UBlueprintUtilityBPLibrary::CopyFile(const FString FilePath, const FString ToPath)
- {
- IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
-
- return PlatformFile.CopyFile(*ToPath, *FilePath);
- }
- bool UBlueprintUtilityBPLibrary::Texture2d2PNG( UTextureRenderTarget2D* TextureRenderTarget, const FString& FilePath)
- {
- FTextureRenderTargetResource* rtResource = TextureRenderTarget->GameThread_GetRenderTargetResource();
- FReadSurfaceDataFlags readPixelFlags(RCM_UNorm);
- TArray<FColor> outBMP;
- for (FColor& color : outBMP)
- {
- color.A = 255;
- }
- outBMP.AddUninitialized(TextureRenderTarget->GetSurfaceWidth() * TextureRenderTarget->GetSurfaceHeight());
- rtResource->ReadPixels(outBMP, readPixelFlags);
- FIntPoint destSize(TextureRenderTarget->GetSurfaceWidth(), TextureRenderTarget->GetSurfaceHeight());
- TArray<uint8> CompressedBitmap;
- FImageUtils::CompressImageArray(destSize.X, destSize.Y, outBMP, CompressedBitmap);
- bool imageSavedOk = FFileHelper::SaveArrayToFile(CompressedBitmap, *FilePath);
- return imageSavedOk;
- }
- FString UBlueprintUtilityBPLibrary::GetGamePath(DirType E)
- {
- if (E == DirType::GameDir)
- {
- return FPaths::ProjectDir();
- }
- return FPaths::ProjectContentDir();
- }
- bool UBlueprintUtilityBPLibrary::IsVaildPath(const FString ImagePath)
- {
- if (!FPaths::FileExists(ImagePath))
- {
- UE_LOG(LogTemp, Warning, TEXT("File not found: %s"), *ImagePath);
- return false;
- }
- // Load the compressed byte data from the file
- TArray<uint8> FileData;
- if (!FFileHelper::LoadFileToArray(FileData, *ImagePath))
- {
- UE_LOG(LogTemp, Warning, TEXT("Failed to load file: %s"), *ImagePath);
- return false;
- }
- return true;
- }
- AExeActor* UBlueprintUtilityBPLibrary::OpenExe(UObject* SomeInWorldObject, const FString Path, const FString Args)
- {
- UWorld* world = SomeInWorldObject->GetWorld();
- FVector pos(150, 0, 20);
- AExeActor *Temp = world->SpawnActor<AExeActor>(pos, FRotator::ZeroRotator); ;
- //AExeActor *Temp = NewObject<AExeActor>();
- //EE->GetWorld()->AddNetworkActor(Temp);
- Temp->SetActorTickEnabled(true);
- Temp->ActorToWorld();
- Temp->CheckProc = FPlatformProcess::CreateProc(*Path, *Args, true, false, false, nullptr, 0, nullptr, nullptr);
- return Temp;
- }
- void UBlueprintUtilityBPLibrary::GenColors(int Length, const FColor color, TArray<FColor>& OuterColor)
- {
- TArray<FColor> setColor;
- for (int i=0;i<Length;i++)
- {
- setColor.Add(color);
-
- }
- OuterColor = setColor;
- }
- void UBlueprintUtilityBPLibrary::UVtimes(FVector2D tims, const TArray<FVector2D> inputUV, TArray<FVector2D>& OuterColor)
- {
- OuterColor.Reset(inputUV.Num());
- for (const FVector2D& tan : inputUV)
- {
- OuterColor.Add(tan*tims);
- }
-
- }
-
- bool UBlueprintUtilityBPLibrary::IsEditorMode()
- {
- #if WITH_EDITOR
- return true;
- #endif
-
- return false;
- }
- FString UBlueprintUtilityBPLibrary::GenMD5(FString inputstring)
- {
- FTCHARToUTF8 EchoStrUtf8(*inputstring);
- int32 DestLen = EchoStrUtf8.Length();
- FString RST = FMD5::HashBytes((unsigned char*)TCHAR_TO_UTF8(*inputstring), DestLen);
-
-
- return RST;
- }
- FString UBlueprintUtilityBPLibrary::GenSHA1(FString inputstring)
- {
- FSHAHash StringHash;
- FSHA1::HashBuffer(TCHAR_TO_ANSI(*inputstring), inputstring.Len(), StringHash.Hash);
- return *(StringHash.ToString());
- }
- FString UBlueprintUtilityBPLibrary::EnCryptoPPSHA1(FString inputstring,FString aes_key)
- {
- //std::string sKey = TCHAR_TO_UTF8(*aes_key);
- //const char* cipherText = TCHAR_TO_ANSI(*aes_content);
- //std::string outstr;
- //try
- //{
- // //填key
- // SecByteBlock key(AES::MAX_KEYLENGTH);
- // memset(key, 0x30, key.size());
- // sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
- // ECB_Mode<AES >::Decryption ecbDecryption((byte*)key, AES::MAX_KEYLENGTH);
- // HexDecoder decryptor(new StreamTransformationFilter(ecbDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::ZEROS_PADDING,
- // true
- // ));
- // decryptor.Put((byte*)cipherText, strlen(cipherText));
- // decryptor.MessageEnd();
- // _isSuc = true;
- //}
- //catch (...)
- //{
- // outstr = "error";
- // _isSuc = false;
- //}
- return "123";
- }
- bool UBlueprintUtilityBPLibrary::CheckActorInView(UPrimitiveComponent* actorComp,float checktime)
- {
-
- float c = actorComp->GetLastRenderTimeOnScreen();
- float d = actorComp->GetWorld()->GetTimeSeconds();
- //UE_LOG(LogTemp, Warning, TEXT("last render time :%f Game Time :%f"),c,d);
- if (d-c >=checktime)
- {
- return false;
- }
- return true;
- }
- void UBlueprintUtilityBPLibrary::GetProduralMeshInfo(UProceduralMeshComponent* InProcMesh, int32 SectionIndex, TArray<FVector>& Vertices, TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FProcMeshTangent>& Tangents, TArray<FColor>& VerticesColor)
- {
- if (InProcMesh && SectionIndex >= 0 && SectionIndex < InProcMesh->GetNumSections())
- {
- const FProcMeshSection* Section = InProcMesh->GetProcMeshSection(SectionIndex);
- const int32 NumOutputVerts = Section->ProcVertexBuffer.Num();
- // Allocate output buffers for vert data
- Vertices.SetNumUninitialized(NumOutputVerts);
- Normals.SetNumUninitialized(NumOutputVerts);
- UVs.SetNumUninitialized(NumOutputVerts);
- Tangents.SetNumUninitialized(NumOutputVerts);
- VerticesColor.SetNumUninitialized(NumOutputVerts);
- // copy data
- for (int32 VertIdx = 0; VertIdx < NumOutputVerts; VertIdx++)
- {
- const FProcMeshVertex& Vert = Section->ProcVertexBuffer[VertIdx];
- Vertices[VertIdx] = Vert.Position;
- Normals[VertIdx] = Vert.Normal;
- UVs[VertIdx] = Vert.UV0;
- Tangents[VertIdx] = Vert.Tangent;
- VerticesColor[VertIdx] = Vert.Color;
- }
- // Copy index buffer
- const int32 NumIndices = Section->ProcIndexBuffer.Num();
- Triangles.SetNumUninitialized(NumIndices);
- for (int32 IndexIdx = 0; IndexIdx < NumIndices; IndexIdx++)
- {
- Triangles[IndexIdx] = Section->ProcIndexBuffer[IndexIdx];
- }
- }
- }
|