перенос на 25 версию, создание с++

This commit is contained in:
Andron666
2021-09-15 13:28:27 +05:00
parent 577f8b099a
commit d2e74a40f8
115 changed files with 529 additions and 5 deletions
+15
View File
@@ -0,0 +1,15 @@
// Fill out your copyright notice in the Description page of Project Settings.
using UnrealBuildTool;
using System.Collections.Generic;
public class TEM2Target : TargetRules
{
public TEM2Target(TargetInfo Target) : base(Target)
{
Type = TargetType.Game;
DefaultBuildSettings = BuildSettingsVersion.V2;
ExtraModuleNames.AddRange( new string[] { "TEM2" } );
}
}
+368
View File
@@ -0,0 +1,368 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CPPGameInstance.h"
#include "Networking/Public/Interfaces/IPv4/IPv4Address.h"
#include "SocketSubsystem.h"
#include "Engine/Engine.h"
#include "Serialization/JsonReader.h"
#include "Serialization/JsonSerializer.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
/*
* creates a map of all commands that come from the server
*/
void UCPPGameInstance::CreateMapCommand()
{
CommandMap.Add(TEXT("waggonvelocity"), 0);
CommandMap.Add(TEXT("switchstate"), 1);
CommandMap.Add(TEXT("lightstate"), 2);
CommandMap.Add(TEXT("createwaggon"), 3);
CommandMap.Add(TEXT("teleportwaggon"), 4);
CommandMap.Add(TEXT("connectwagon"), 5);
CommandMap.Add(TEXT("destroywaggon"), 6);
CommandMap.Add(TEXT("init"), 99);
}
int32 UCPPGameInstance::getNumberFunction(FString Command)
{
auto numberComand = CommandMap.Find(Command);
return *numberComand;
}
void UCPPGameInstance::createwaggon_Implementation(const FString& value, int32 id)
{
}
void UCPPGameInstance::waggonvelocity_Implementation(int32 id, float speed, int32 idspline,
float offset)
{
}
void UCPPGameInstance::teleportwaggon_Implementation(int32 id, int32 idspline, float offset)
{
}
void UCPPGameInstance::destroywaggon_Implementation(int32 id)
{
}
void UCPPGameInstance::connectwagon_Implementation(int32 id, int32 idspline)
{
}
bool UCPPGameInstance::StartWorkingAtSocket()
{
MySocket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
MySocket->SetNonBlocking(true);
// set address ad port socket
//FString address = TEXT("192.168.1.123");
FIPv4Address ip;
//int32 port = 6000;
FIPv4Address::Parse(adress, ip);
//FIPv4Address ip(192, 168, 1, 165);
TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
addr->SetIp(ip.Value);
addr->SetPort(port);
bool connected = MySocket->Connect(*addr);
/*FIPv4Address ip(127, 0, 0, 1);
TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
addr->SetIp(ip.Value);
addr->SetPort(6000);
bool connected = MySocket->Connect(*addr);*/
if (connected)
{
// if connected = true make 1 byte and send to socket
UE_LOG(LogTemp, Warning, TEXT("CONNCET"));
uint8 b1 = 1;
int32 BytesSent = 0;
int32 size = 1;
bool successful = MySocket->Send(&b1, size, BytesSent);
if (successful)
{
UE_LOG(LogTemp, Warning, TEXT("PUSHED 1 byte"));
int32 BufferSize = 1;
int32 BytesRead = 0;
uint8 Response = NULL;
// wait response from server
bool read = false;
while (!read)
{
MySocket->Recv(&Response, BufferSize, BytesRead);
if (Response)
{
read = true;
}
}
// if response = 3 server is running
if (Response == 3)
{
size = 8;
// revers bytes at number 4
int64 next_size = (int64)4 << 56;
bool successful2 = MySocket->Send((uint8*)&next_size, size, BytesSent);
if (successful2)
{
UE_LOG(LogTemp, Warning, TEXT("PUSHED 4 INT 64"));
size = 4;
int32 b15 = 15;
bool successful3 = MySocket->Send((uint8*)&b15, size, BytesSent);
if (successful3)
{
UE_LOG(LogTemp, Warning, TEXT("PUSHED 15 INT 32"));
return true;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("NO PUSHED 15 INT 32"));
return false;
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("NO PUSHED 4 INT 64"));
return false;
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Response != 3"));
//return false;
return false;
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT(" NO PUSHED 1 byte"));
return false;
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("NOT Connecting"));
return false;
}
}
void UCPPGameInstance::cnctwagon_Implementation(int32 ideven, int32 idodd)
{
}
bool UCPPGameInstance::SaveTxt(FString SaveTextB, FString FileNameB)
{
return FFileHelper::SaveStringToFile(SaveTextB, *(FPaths::ProjectDir() + FileNameB));
}
bool UCPPGameInstance::LoadTxt(FString FileNameA, FString& SaveTextA)
{
return FFileHelper::LoadFileToString(SaveTextA, *(FPaths::ProjectDir() + FileNameA));
}
void UCPPGameInstance::SetWinPos(int posX, int posY)
{
SetWindowPos(
GetActiveWindow(),
HWND_TOP,
posX,
posY,
0,
0,
SWP_NOSIZE
);
}
void UCPPGameInstance::SetWinSize(int sizeX, int sizeY)
{
SetWindowPos(
GetActiveWindow(),
HWND_TOP,
0,
0,
sizeX,
sizeY,
SWP_NOMOVE
);
}
/*
* the function listening socket server
* Parameters
* @param NumStatus status number that will be sent to the server
* 0 - wait initializing
* 1 - initialize client
* 2 - initializing at all clients
* 3 - in process working
* 4 - end working
*/
void UCPPGameInstance::listening(int32 NumberStatus,FString command)
{
// push char 105
char i = '\151';
int32 size = 1;
int32 BytesSent = 0;
MySocket->Send((uint8*)&i, size, BytesSent);
int32 BufferSize = 1;
int32 BytesRead = 0;
uint8 Response = NULL;
bool read = false;
while (!read)
{
MySocket->Recv(&Response, BufferSize, BytesRead);
if (Response == 3)
{
//LAstServResp = Response;
break;
}
}
SendStatus(NumberStatus,command);
}
void UCPPGameInstance::SendStatus(int32 NumberStatus,FString command)
{
int32 BytesSentStatus = 0;
FString status = FString::Printf(TEXT("{\"status\":%d%s}"), NumberStatus,*command); // create string status client
// FString status = FString::Printf(TEXT("%s"), *command); // create string status client
if (!command.IsEmpty()) {
UE_LOG(LogTemp, Warning, TEXT("command sent"));
}
MySocket->Send((uint8*)TCHAR_TO_UTF8(*status), status.Len(), BytesSentStatus);
int32 BytesRead = 0;
TArray<uint8> Response;
uint32 BufferSize = 0;
bool read = false;
FString jsonString;
// get json from server and make string json
while (!read)
{
read = MySocket->HasPendingData(BufferSize);
if (read)
{
Response.SetNumUninitialized(BufferSize);
MySocket->Recv(Response.GetData(), Response.Num(), BytesRead);
for (int i = 0; i < Response.Num(); i++)
{
//const TCHAR c = Response[i];
jsonString.AppendChar(Response[i]);
}
//GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Yellow, jsonString);
break;
}
}
// make json object and parsing
JsonObject = MakeShareable(new FJsonObject);
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(jsonString);
bool b1 = FJsonSerializer::Deserialize(Reader, JsonObject);
bool b2 = JsonObject.IsValid();
SaveTxt(jsonString, "JSON.json");
/*if ( JsonObject.IsValid() && NumberStatus == 3)
{
// get array all commands
TArray<TSharedPtr<FJsonValue>> JsonObjectArray = JsonObject->GetArrayField(TEXT("comandarray"));
for (int32 indx = 0; indx < JsonObjectArray.Num(); indx++)
{
TSharedPtr<FJsonValue> value = JsonObjectArray[indx];
TSharedPtr<FJsonObject> ObjectComand = value->AsObject();
//get name command
FString NameComand = ObjectComand->GetStringField(TEXT("comand"));
int32 NumberComand = getNumberFunction(NameComand);
switch (NumberComand)
{
case 0://waggonvelocity
{
waggonvelocity(ObjectComand->GetNumberField(TEXT("id")),
ObjectComand->GetNumberField(TEXT("speed")),
ObjectComand->GetNumberField(TEXT("idspline")),
ObjectComand->GetNumberField(TEXT("offset")));
break;
}
case 1://switchstate
break;
case 2://lightstate
break;
case 3://createwaggon
{
FString typeTrain = ObjectComand->GetStringField(TEXT("value"));
createwaggon(typeTrain,ObjectComand->GetNumberField(TEXT("id")));
break;
}
case 4://teleportwaggon
{
teleportwaggon(ObjectComand->GetNumberField(TEXT("id")),
ObjectComand->GetNumberField(TEXT("idspline")),
ObjectComand->GetNumberField(TEXT("offset")));
break;
}
case 5://connectwagon
{
cnctwagon(ObjectComand->GetNumberField(TEXT("ideven")),
ObjectComand->GetNumberField(TEXT("idodd")));
//cnctwagon(ObjectComand->GetNumberField(TEXT("ideven")),
//ObjectComand->GetNumberField(TEXT("idodd")));
break;
}
case 6:
{
destroywaggon(ObjectComand->GetNumberField(TEXT("id")));
break;
}
default:
{
break;
}
}
}
}else
{
return;
}*/
}
void UCPPGameInstance::runSelf(FString argument)
{
FPlatformProcess::CreateProc(FPlatformProcess::ExecutableName(false), *argument, true, false, false, NULL, 0, NULL, NULL);
}
+83
View File
@@ -0,0 +1,83 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "Sockets.h"
#include "TimerManager.h"
#include "Dom/JsonObject.h"
#include "CPPGameInstance.generated.h"
/**
*
*/
UCLASS()
class TEM2_API UCPPGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
FSocket* MySocket;
FTimerHandle timer;
UPROPERTY(BlueprintReadOnly,Category="Server responses")
FString LAstServResp;
UPROPERTY(BlueprintReadOnly, Category = "Server responses")
FString strjsonTest;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Server responses")
FString adress;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Server responses")
int32 port;
protected:
// map all commands
TMap<FString, int32> CommandMap;
// json object
TSharedPtr<FJsonObject> JsonObject;
UFUNCTION(BlueprintCallable, Category = "Data")
void CreateMapCommand();
UFUNCTION(BlueprintCallable, Category = "Data")
int32 getNumberFunction(FString Command);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Command server")
void waggonvelocity(int32 id, float speed, int32 idspline, float offset);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Command server")
void createwaggon(const FString& value, int32 id);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Command server")
void teleportwaggon(int32 id, int32 idspline, float offset);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Command server")
void connectwagon(int32 ideven, int32 idodd);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Command server")
void cnctwagon(int32 ideven, int32 idodd);
//UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = "Command server")
//void testWgn(int32 id,int32 idd);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Command server")
void destroywaggon(int32 id);
public:
UFUNCTION(BlueprintCallable, Category = "Status client")
void listening(int32 NumberStatus, FString command);
UFUNCTION(BlueprintCallable, Category = "Status client")
void SendStatus(int32 NumberStatus, FString command);
UFUNCTION(BlueprintCallable, Category = "Status client")
bool StartWorkingAtSocket();
UFUNCTION(BlueprintCallable, Category = "Custom", meta = (Keywords = "LoadTxt"))
static bool LoadTxt(FString FileNameA, FString& SaveTextA);
UFUNCTION(BlueprintCallable, Category = "Custom", meta = (Keywords = "SaveTxt"))
static bool SaveTxt(FString SaveTextB, FString FileNameB);
UFUNCTION(BlueprintCallable, Category = "Custom")
static void SetWinPos(int posX, int posY);
UFUNCTION(BlueprintCallable, Category = "Custom")
static void SetWinSize(int sizeX, int sizeY);
UFUNCTION(BlueprintCallable, Category = "Custom",meta=(ToolTip="execute this app again with argument"))
static void runSelf(FString argument);
};
+23
View File
@@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.
using UnrealBuildTool;
public class TEM2 : ModuleRules
{
public TEM2(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Sockets", "Networking", "Json", "JsonUtilities" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
+6
View File
@@ -0,0 +1,6 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "TEM2.h"
#include "Modules/ModuleManager.h"
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, TEM2, "TEM2" );
+6
View File
@@ -0,0 +1,6 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
+15
View File
@@ -0,0 +1,15 @@
// Fill out your copyright notice in the Description page of Project Settings.
using UnrealBuildTool;
using System.Collections.Generic;
public class TEM2EditorTarget : TargetRules
{
public TEM2EditorTarget(TargetInfo Target) : base(Target)
{
Type = TargetType.Editor;
DefaultBuildSettings = BuildSettingsVersion.V2;
ExtraModuleNames.AddRange( new string[] { "TEM2" } );
}
}