172 lines
4.5 KiB
C++
172 lines
4.5 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
#include "CPPFunctionLibrary.h"
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <array>
|
|
|
|
void UCPPFunctionLibrary::CreateTxt(FString Path, FString FileName, FString text)
|
|
{
|
|
if (Path == "") {
|
|
Path = FPaths::ProjectSavedDir();
|
|
}
|
|
FString fullpath(Path + FileName);
|
|
FFileHelper::SaveStringToFile(text, *fullpath, FFileHelper::EEncodingOptions::ForceUTF8);
|
|
|
|
}
|
|
|
|
bool UCPPFunctionLibrary::AppendStringToFile(FString DirPath, FString FileName, FString Data)
|
|
{
|
|
// get file path
|
|
FString FilePath;
|
|
if (!DirPath.Len())
|
|
{
|
|
// default documents directory path + FileName
|
|
FilePath = FPlatformProcess::UserDir() + FileName;
|
|
}
|
|
else
|
|
{
|
|
FilePath = DirPath + FileName;
|
|
}
|
|
UE_LOG(LogTemp, Log, TEXT("File path: %s"), *FilePath);
|
|
|
|
// check file existence and write data to a file
|
|
FString OldData;
|
|
bool append_result;
|
|
if (FPaths::FileExists(FilePath))
|
|
{
|
|
UE_LOG(LogTemp, Log, TEXT("File exists: true"));
|
|
FFileHelper::LoadFileToString(OldData, *FilePath);
|
|
OldData += Data;
|
|
append_result = FFileHelper::SaveStringToFile(Data, *FilePath, FFileHelper::EEncodingOptions::ForceUTF8, &IFileManager::Get(), FILEWRITE_Append);
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("File exists: false"));
|
|
append_result = FFileHelper::SaveStringToFile(Data, *FilePath, FFileHelper::EEncodingOptions::ForceUTF8);
|
|
}
|
|
|
|
// return append result
|
|
if (append_result)
|
|
{
|
|
UE_LOG(LogTemp, Log, TEXT("Append: complete"));
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Error, TEXT("Append: not complete"));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void UCPPFunctionLibrary::RunSystemCommand(FString Command)
|
|
{
|
|
system(TCHAR_TO_ANSI(*Command));
|
|
}
|
|
|
|
void UCPPFunctionLibrary::GetExcelData(FString Command)
|
|
{
|
|
}
|
|
|
|
void UCPPFunctionLibrary::GetContentHTTP(FString Verb, FString Payload, FString Url)
|
|
{
|
|
/*TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> Request = FHttpModule::Get().CreateRequest();
|
|
Request->SetURL(Url);
|
|
if (Payload.Len() > 0)
|
|
{
|
|
Request->SetContentAsString(Payload);
|
|
}
|
|
Request->SetVerb(Verb);
|
|
Request->ProcessRequest();*/
|
|
}
|
|
|
|
int32 UCPPFunctionLibrary::GetUnixTimestamp()
|
|
{
|
|
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
|
}
|
|
|
|
void UCPPFunctionLibrary::CreateProc(FString PathToExec, TArray<FString> Args, bool Detach, bool Hidden, int32 Priority, FString OptionalWorkDir)
|
|
{
|
|
FString StrArgs("");
|
|
/*if (Args.Num())
|
|
for (auto arg : Args)
|
|
StrArgs += " " + arg;*/
|
|
|
|
if (Args.Num() > 1)
|
|
{
|
|
StrArgs = Args[0];
|
|
for (int32 v = 1; v < Args.Num(); v++)
|
|
{
|
|
StrArgs += " " + Args[v];
|
|
}
|
|
}
|
|
else if (Args.Num() > 0)
|
|
{
|
|
StrArgs = Args[0];
|
|
}
|
|
|
|
FProcHandle PHandle = FPlatformProcess::CreateProc(
|
|
*PathToExec,
|
|
*StrArgs,
|
|
Detach,
|
|
Hidden,
|
|
Hidden,
|
|
nullptr,
|
|
Priority,
|
|
(OptionalWorkDir != "") ? *OptionalWorkDir : nullptr,
|
|
nullptr
|
|
);
|
|
|
|
if (PHandle.IsValid())
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("ProcInfo: Valid"))
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("ProcInfo: NotValid"))
|
|
}
|
|
}
|
|
|
|
FString UCPPFunctionLibrary::GetDataFromFile(FString FileName)
|
|
{
|
|
FString FileData("");
|
|
|
|
if (FPaths::FileExists(FileName))
|
|
FFileHelper::LoadFileToString(FileData, *FileName);
|
|
|
|
return FileData;
|
|
}
|
|
|
|
TArray<FString> UCPPFunctionLibrary::GetHardDisksSerialNumbers()
|
|
{
|
|
TArray<FString> lines;
|
|
std::string executeCommand = "wmic path win32_physicalmedia get SerialNumber";
|
|
std::array<char, 128> buffer;
|
|
std::string result;
|
|
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(executeCommand.c_str(), "r"), _pclose);
|
|
if (!pipe) {
|
|
throw std::runtime_error("popen() failed!");
|
|
}
|
|
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
|
result += buffer.data();
|
|
}
|
|
std::cout << result << std::endl;
|
|
std::istringstream istream(result);
|
|
std::string line;
|
|
int lineNum = 0;
|
|
while (std::getline(istream, line))
|
|
{
|
|
if (!lineNum)
|
|
{
|
|
++lineNum;
|
|
continue;
|
|
}
|
|
lines.Push(line.c_str());
|
|
}
|
|
return lines;
|
|
} |