1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include "LoginWidget.h" #include "HAL/PlatformFileManager.h"
ULoginWidget::ULoginWidget(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { LoginButton = nullptr; RegisterButton = nullptr; UsernameTextBox = nullptr; PasswordTextBox = nullptr; }
void ULoginWidget::NativeConstruct() { Super::NativeConstruct();
UE_LOG(LogTemp, Warning, TEXT("ULoginWidget::NativeConstruct()"));
if (UButton* SBtn = Cast<UButton>(GetWidgetFromName("LoginButton"))) { LoginButton = SBtn;
FScriptDelegate sgbDelegate; sgbDelegate.BindUFunction(this, "PlayerLogin"); LoginButton->OnClicked.Add(sgbDelegate); } if (UButton* SBtn = Cast<UButton>(GetWidgetFromName("RegisterButton"))) { RegisterButton = SBtn;
FScriptDelegate sgbDelegate; sgbDelegate.BindUFunction(this, "PlayerRegister"); RegisterButton->OnClicked.Add(sgbDelegate); } if (UEditableTextBox* SText = Cast<UEditableTextBox>(GetWidgetFromName("UsernameTextBox"))) { UsernameTextBox = SText; } if (UEditableTextBox* SText = Cast<UEditableTextBox>(GetWidgetFromName("PasswordTextBox"))) { PasswordTextBox = SText; } }
void ULoginWidget::PlayerLogin() { if (UsernameTextBox == nullptr || PasswordTextBox == nullptr) { UE_LOG(LogTemp, Warning, TEXT("Widget can't get")); return; }
FText UsernameText = UsernameTextBox->GetText(); FText PasswordText = PasswordTextBox->GetText(); FString UsernameStr = UsernameText.ToString(); FString PasswordStr = PasswordText.ToString(); if (UsernameStr.Len() <= 0 || PasswordStr.Len() <= 0) { UE_LOG(LogTemp, Warning, TEXT("Username or Password must not be empty")); return; }
FString FileName = TEXT("Player_") + UsernameStr + TEXT(".data");
if (!this->CheckTextFileExists(FileName)) { UE_LOG(LogTemp, Warning, TEXT("Player-[%s] Not Registered"), *UsernameStr); return; }
if (!this->LoadFromTextFile(FileName)) { UE_LOG(LogTemp, Warning, TEXT("Player-[%s] Login Failed. DataFile Wrong Occured"), *UsernameStr); return; }
if (this->Password != PasswordStr) { UE_LOG(LogTemp, Warning, TEXT("Player-[%s] Login Failed. Password Not Correct"), *UsernameStr); return; }
GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Red, TEXT("Login Successfully"), false, FVector2D(2.0, 2.0)); }
void ULoginWidget::PlayerRegister() { if (UsernameTextBox == nullptr || PasswordTextBox == nullptr) { UE_LOG(LogTemp, Warning, TEXT("Widget can't get")); return; }
FText UsernameText = UsernameTextBox->GetText(); FText PasswordText = PasswordTextBox->GetText(); FString UsernameStr = UsernameText.ToString(); FString PasswordStr = PasswordText.ToString(); if (UsernameStr.Len() <= 0 || PasswordStr.Len() <= 0) { UE_LOG(LogTemp, Warning, TEXT("Username or Password must not be empty")); return; }
FString FileName = TEXT("Player_") + UsernameStr + TEXT(".data");
if (this->CheckTextFileExists(FileName)) { UE_LOG(LogTemp, Warning, TEXT("Player-[%s] Already Registered"), *UsernameStr); return; }
this->Username = UsernameStr; this->Password = PasswordStr;
if (!this->SaveToTextFile(FileName)) { UE_LOG(LogTemp, Warning, TEXT("Player-[%s] Register Failed. DataFile Wrong Occured"), *UsernameStr); return; }
GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Red, TEXT("Register Successfully"), false, FVector2D(2.0, 2.0)); }
bool ULoginWidget::CheckTextFileExists(FString FileName) { IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile(); return PlatformFile.FileExists(*FileName); }
bool ULoginWidget::SaveToTextFile(FString FileName) { TArray<FString> SaveArray; SaveArray.Add(this->Username); SaveArray.Add(this->Password); if (!FFileHelper::SaveStringArrayToFile(SaveArray, *FileName, FFileHelper::EEncodingOptions::ForceUTF8)) { UE_LOG(LogTemp, Warning, TEXT("Save Text File-[%s] Failed"), *FileName); return false; } return true; }
bool ULoginWidget::LoadFromTextFile(FString FileName) { TArray<FString> SaveArray; if (!FFileHelper::LoadFileToStringArray(SaveArray, *FileName)) { UE_LOG(LogTemp, Warning, TEXT("Load Text File-[%s] Failed"), *FileName); return false; } if (SaveArray.Num() < 2) { UE_LOG(LogTemp, Warning, TEXT("Text File-[%s] Broken"), *FileName); return false; }
this->Username = SaveArray[0]; this->Password = SaveArray[1]; return true; }
|