-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
496 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
Source/Private/SlateWidget/SVersionUpdater/SVersionUpdaterWidget.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
#include "SVersionUpdaterWidget.h" | ||
// engine header | ||
#include "Widgets/Input/SHyperlink.h" | ||
#include "Widgets/Layout/SBorder.h" | ||
#include "Widgets/Images/SImage.h" | ||
#include "Widgets/Layout/SBox.h" | ||
#include "Widgets/Layout/SScrollBox.h" | ||
#include "Widgets/SBoxPanel.h" | ||
#include "HttpModule.h" | ||
#include "Misc/FileHelper.h" | ||
#include "Json.h" | ||
#include "VersionUpdaterStyle.h" | ||
#include "Interfaces/IHttpRequest.h" | ||
#include "Interfaces/IHttpResponse.h" | ||
|
||
#define LOCTEXT_NAMESPACE "VersionUpdaterWidget" | ||
|
||
|
||
void SVersionUpdaterWidget::Construct(const FArguments& InArgs) | ||
{ | ||
static bool GBrushInited = false; | ||
if(!GBrushInited) | ||
{ | ||
FVersionUpdaterStyle::Initialize(); | ||
FVersionUpdaterStyle::ReloadTextures(); | ||
GBrushInited = true; | ||
} | ||
|
||
SetToolUpdateInfo( | ||
InArgs._ToolName.Get().ToString(), | ||
InArgs._DeveloperName.Get().ToString(), | ||
InArgs._DeveloperWebsite.Get().ToString(), | ||
InArgs._UpdateWebsite.Get().ToString() | ||
); | ||
CurrentVersion = InArgs._CurrentVersion.Get(); | ||
|
||
ChildSlot | ||
[ | ||
SNew(SBorder) | ||
.Padding(2) | ||
.ColorAndOpacity(FLinearColor{ 1.0,1.0,1.0,1.0 }) | ||
//.BorderImage(FVersionUpdaterStyle::GetBrush("Updater.GroupBorder")) | ||
[ | ||
SNew(SHorizontalBox) | ||
+ SHorizontalBox::Slot() | ||
.FillWidth(1.0f) | ||
.VAlign(VAlign_Center) | ||
.Padding(4.0f, 0.0f, 4.0f, 0.0f) | ||
[ | ||
SNew(SHorizontalBox) | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
.VAlign(VAlign_Center) | ||
[ | ||
SNew(SBox) | ||
.WidthOverride(40) | ||
.HeightOverride(40) | ||
[ | ||
SNew(SImage) | ||
.Image(FVersionUpdaterStyle::GetBrush("Updater.QuickLaunch")) | ||
] | ||
] | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
.Padding(10,0,10,0) | ||
.VAlign(VAlign_Center) | ||
[ | ||
SNew(SVerticalBox) | ||
+ SVerticalBox::Slot() | ||
.AutoHeight() | ||
.Padding(2, 4, 2, 4) | ||
[ | ||
SNew(SHorizontalBox) | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
[ | ||
SNew(SHyperlink) | ||
.Text_Raw(this,&SVersionUpdaterWidget::GetToolName) | ||
.OnNavigate(this, &SVersionUpdaterWidget::HyLinkClickEventOpenUpdateWebsite) | ||
] | ||
+ SHorizontalBox::Slot() | ||
.FillWidth(1.0) | ||
[ | ||
SNew(SOverlay) | ||
] | ||
] | ||
+ SVerticalBox::Slot() | ||
.AutoHeight() | ||
.Padding(2, 4, 2, 4) | ||
[ | ||
SNew(SHorizontalBox) | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
.Padding(0,0,4,0) | ||
[ | ||
SNew(STextBlock) | ||
.Text_Raw(this,&SVersionUpdaterWidget::GetCurrentVersionText) | ||
] | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
[ | ||
SAssignNew(UpdateInfoWidget,SHorizontalBox) | ||
.Visibility(EVisibility::Collapsed) | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
[ | ||
SNew(SBox) | ||
.WidthOverride(18) | ||
.HeightOverride(18) | ||
[ | ||
SNew(SImage) | ||
.Image(FVersionUpdaterStyle::GetBrush("Updater.SpawnableIconOverlay")) | ||
] | ||
] | ||
+ SHorizontalBox::Slot() | ||
.Padding(2,0,0,0) | ||
.AutoWidth() | ||
[ | ||
SNew(SHyperlink) | ||
.Text_Raw(this,&SVersionUpdaterWidget::GetLatstVersionText) | ||
.OnNavigate(this, &SVersionUpdaterWidget::HyLinkClickEventOpenUpdateWebsite) | ||
] | ||
] | ||
] | ||
] | ||
] | ||
+ SHorizontalBox::Slot() | ||
.FillWidth(1.0f) | ||
.VAlign(VAlign_Center) | ||
[ | ||
SNew(SOverlay) | ||
] | ||
+ SHorizontalBox::Slot() | ||
.AutoWidth() | ||
.Padding(10,0,10,0) | ||
.VAlign(VAlign_Center) | ||
[ | ||
SNew(SHyperlink) | ||
.Text_Raw(this,&SVersionUpdaterWidget::GetDeveloperDescrible) | ||
.OnNavigate(this, &SVersionUpdaterWidget::HyLinkClickEventOpenDeveloperWebsite) | ||
] | ||
] | ||
]; | ||
RequestVersion(REMOTE_VERSION_FILE); | ||
} | ||
|
||
void SVersionUpdaterWidget::HyLinkClickEventOpenUpdateWebsite() | ||
{ | ||
FPlatformProcess::LaunchURL(*UpdateWebsite, NULL, NULL); | ||
} | ||
|
||
void SVersionUpdaterWidget::HyLinkClickEventOpenDeveloperWebsite() | ||
{ | ||
FPlatformProcess::LaunchURL(*DeveloperWebsite, NULL, NULL); | ||
} | ||
|
||
void SVersionUpdaterWidget::SetToolUpdateInfo(const FString& InToolName, const FString& InDeveloperName, | ||
const FString& InDeveloperWebsite, const FString& InUpdateWebsite) | ||
{ | ||
ToolName = InToolName; | ||
DeveloperName = InDeveloperName; | ||
DeveloperWebsite = InDeveloperWebsite; | ||
UpdateWebsite = InUpdateWebsite; | ||
} | ||
|
||
DEFINE_LOG_CATEGORY_STATIC(LogVersionUpdater,All,All); | ||
|
||
void SVersionUpdaterWidget::OnRequestComplete(FHttpRequestPtr RequestPtr, FHttpResponsePtr ResponsePtr, bool bConnectedSuccessfully) | ||
{ | ||
FString Result = ResponsePtr->GetContentAsString();; | ||
// UE_LOG(LogVersionUpdater, Log, TEXT("%s"),*Result); | ||
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(Result); | ||
TSharedPtr<FJsonObject> JsonObject; | ||
if (FJsonSerializer::Deserialize(JsonReader, JsonObject)) | ||
{ | ||
TArray<FString> ToolNames; | ||
if(JsonObject->TryGetStringArrayField(TEXT("Tools"),ToolNames)) | ||
{ | ||
if(ToolNames.Contains(GetToolName().ToString())) | ||
{ | ||
const TSharedPtr<FJsonObject>* ToolJsonObject; | ||
if(JsonObject->TryGetObjectField(GetToolName().ToString(),ToolJsonObject)) | ||
{ | ||
int32 Version = ToolJsonObject->Get()->GetIntegerField(TEXT("Version")); | ||
FString Developer = ToolJsonObject->Get()->GetStringField(TEXT("Author")); | ||
FString UpdateURL = ToolJsonObject->Get()->GetStringField(TEXT("URL")); | ||
FString Website = ToolJsonObject->Get()->GetStringField(TEXT("Website")); | ||
SetToolUpdateInfo(GetToolName().ToString(),Developer,Website,UpdateURL); | ||
LatstVersion = Version; | ||
if(CurrentVersion < LatstVersion) | ||
{ | ||
UpdateInfoWidget->SetVisibility(EVisibility::Visible); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void SVersionUpdaterWidget::RequestVersion(const FString& URL) | ||
{ | ||
if(HttpHeadRequest.IsValid()) | ||
{ | ||
HttpHeadRequest->CancelRequest(); | ||
HttpHeadRequest.Reset(); | ||
} | ||
HttpHeadRequest = FHttpModule::Get().CreateRequest(); | ||
HttpHeadRequest->SetURL(URL); | ||
HttpHeadRequest->SetVerb(TEXT("GET")); | ||
HttpHeadRequest->OnProcessRequestComplete().BindRaw(this,&SVersionUpdaterWidget::OnRequestComplete); | ||
if (HttpHeadRequest->ProcessRequest()) | ||
{ | ||
UE_LOG(LogVersionUpdater, Log, TEXT("Request %s Version."),*GetToolName().ToString()); | ||
} | ||
} | ||
|
||
#undef LOCTEXT_NAMESPACE |
62 changes: 62 additions & 0 deletions
62
Source/Private/SlateWidget/SVersionUpdater/SVersionUpdaterWidget.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
#include "Widgets/DeclarativeSyntaxSupport.h" | ||
#include "Input/Reply.h" | ||
#include "Interfaces/IHttpRequest.h" | ||
#include "Widgets/SCompoundWidget.h" | ||
|
||
// #define CURRENT_VERSION_ID 68 | ||
// #define REMOTE_VERSION_FILE TEXT("https://imzlp.com/opensource/version.json") | ||
|
||
class SVersionUpdaterWidget : public SCompoundWidget | ||
{ | ||
|
||
public: | ||
SLATE_BEGIN_ARGS(SVersionUpdaterWidget): | ||
_CurrentVersion(), | ||
_ToolName(), | ||
_DeveloperName(), | ||
_DeveloperWebsite(), | ||
_UpdateWebsite() | ||
{} | ||
SLATE_ATTRIBUTE( int32, CurrentVersion ) | ||
SLATE_ATTRIBUTE( FText, ToolName ) | ||
SLATE_ATTRIBUTE( FText, DeveloperName ) | ||
SLATE_ATTRIBUTE( FText, DeveloperWebsite ) | ||
SLATE_ATTRIBUTE( FText, UpdateWebsite ) | ||
SLATE_END_ARGS() | ||
|
||
public: | ||
/** | ||
* Construct the widget | ||
* | ||
* @param InArgs A declaration from which to construct the widget | ||
*/ | ||
void Construct(const FArguments& InArgs); | ||
void HyLinkClickEventOpenUpdateWebsite(); | ||
void HyLinkClickEventOpenDeveloperWebsite(); | ||
|
||
FText GetCurrentVersionText() const {return FText::FromString(FString::Printf(TEXT("Current Version v%d."),GetCurrentVersion()));}; | ||
FText GetToolName() const {return FText::FromString(ToolName);}; | ||
FText GetDeveloperName() const {return FText::FromString(DeveloperName);}; | ||
FText GetUpdateWebsite() const {return FText::FromString(UpdateWebsite);}; | ||
FText GetDeveloperWebsite() const {return FText::FromString(DeveloperWebsite);}; | ||
FText GetDeveloperDescrible() const {return FText::FromString(FString::Printf(TEXT("Developed by %s"),*GetDeveloperName().ToString()));}; | ||
FText GetLatstVersionText() const {return FText::FromString(FString::Printf(TEXT("A new version v%d is avaliable"),LatstVersion));}; | ||
virtual void SetToolUpdateInfo(const FString& ToolName,const FString& DeveloperName,const FString& DeveloperWebsite,const FString& UpdateWebsite); | ||
int32 GetCurrentVersion()const { return CurrentVersion; } | ||
|
||
void OnRequestComplete(FHttpRequestPtr RequestPtr, FHttpResponsePtr ResponsePtr, bool bConnectedSuccessfully); | ||
void RequestVersion(const FString& URL); | ||
private: | ||
int32 CurrentVersion = 0; | ||
FString ToolName; | ||
FString UpdateWebsite; | ||
FString DeveloperWebsite; | ||
FString DeveloperName; | ||
TSharedPtr<SHorizontalBox> UpdateInfoWidget; | ||
int32 LatstVersion = 0; | ||
FHttpRequestPtr HttpHeadRequest; | ||
}; | ||
|
Oops, something went wrong.