Skip to content

Commit

Permalink
support version update
Browse files Browse the repository at this point in the history
  • Loading branch information
hxhb committed Nov 16, 2021
1 parent af1a4c9 commit c9c49ea
Show file tree
Hide file tree
Showing 8 changed files with 496 additions and 84 deletions.
39 changes: 28 additions & 11 deletions Source/Private/RealExecutionMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ int RealExecutionMain(const TCHAR* pCmdLine)


#include "SlateWidget/SConfigListPanel.h"

#include "SlateWidget/SVersionUpdater/SVersionUpdaterWidget.h"
namespace WindowManager
{
static TSharedPtr < SConfPanel > LauncherPanel;
static TSharedPtr < SConfigListPanel > LauncherConfListPanel;
static TSharedPtr < SVersionUpdaterWidget > VersionUpdaterWidget;
void OnConfigSelected(FLaunchConf Config)
{
if(LauncherPanel.IsValid())
Expand Down Expand Up @@ -154,20 +155,36 @@ namespace WindowManager
.MinWidth(1200)
.IsTopmostWindow(false)
[
SNew(SHorizontalBox)
+SHorizontalBox::Slot()
.FillWidth(0.3)
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.AutoHeight()
[
SNew(SBox)
[
SAssignNew(LauncherConfListPanel,SConfigListPanel)
]
SAssignNew(VersionUpdaterWidget,SVersionUpdaterWidget)
.ToolName(FText::FromString(TOOL_NAME))
.DeveloperName(FText::FromString(TEXT("lipengzha")))
.DeveloperWebsite(FText::FromString(TEXT("https://imzlp.com")))
.UpdateWebsite(FText::FromString(TEXT("https://imzlp.com/posts/11750/")))
.CurrentVersion(CURRENT_VERSION_ID)
]
+SHorizontalBox::Slot()
.FillWidth(0.7)
+SVerticalBox::Slot()
.FillHeight(1.0)
[
SAssignNew(LauncherPanel, SConfPanel)
SNew(SHorizontalBox)
+ SHorizontalBox::Slot()
.FillWidth(0.3)
[
SNew(SBox)
[
SAssignNew(LauncherConfListPanel, SConfigListPanel)
]
]
+ SHorizontalBox::Slot()
.FillWidth(0.7)
[
SAssignNew(LauncherPanel, SConfPanel)
]
]

//.OnOpenedFileEvent.BindStatic(&WindowManager::OnOpenFileChangeWindowTitle)
];
LauncherConfListPanel->OnConfigFileSelected.BindStatic(&WindowManager::OnConfigSelected);
Expand Down
17 changes: 8 additions & 9 deletions Source/Private/SlateWidget/SConfListPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "Widgets/SCanvas.h"
#include "Tools/SerializationTools.h"
#include "Tools/EngineLaunchTools.h"

#define LOCTEXT_NAMESPACE "WidgetConfigListPanel"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
Expand Down Expand Up @@ -157,14 +156,14 @@ void SConfigListPanel::Construct(const FArguments& InArgs)
[
SNew(SOverlay)
]
+SHorizontalBox::Slot()
.AutoWidth()
.HAlign(HAlign_Center)
[
SNew(SHyperlink)
.Text(LOCTEXT("LauncherVersion", LAUNCHER_VERSION))
.OnNavigate(this, &SConfigListPanel::HyLinkClickEventOpenVersionWebsite)
]
//+SHorizontalBox::Slot()
//.AutoWidth()
//.HAlign(HAlign_Center)
//[
// SNew(SHyperlink)
// .Text(LOCTEXT("LauncherVersion", *UKismetStringLibrary::Conv_IntToString(CURRENT_VERSION_ID)))
// .OnNavigate(this, &SConfigListPanel::HyLinkClickEventOpenVersionWebsite)
//]
]
+SVerticalBox::Slot()
.AutoHeight()
Expand Down
16 changes: 8 additions & 8 deletions Source/Private/SlateWidget/SConfPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ void SConfPanel::Construct(const FArguments& InArgs)
[
SNew(SOverlay)
]
+SHorizontalBox::Slot()
.AutoWidth()
.HAlign(HAlign_Center)
[
SNew(SHyperlink)
.Text(LOCTEXT("Developer", "Developed by lipengzha"))
.OnNavigate(this, &SConfPanel::HyLinkClickEventOpenDeveloperWebsite)
]
//+SHorizontalBox::Slot()
//.AutoWidth()
//.HAlign(HAlign_Center)
//[
// SNew(SHyperlink)
// .Text(LOCTEXT("Developer", "Developed by lipengzha"))
// .OnNavigate(this, &SConfPanel::HyLinkClickEventOpenDeveloperWebsite)
//]
]
]
+SVerticalBox::Slot()
Expand Down
217 changes: 217 additions & 0 deletions Source/Private/SlateWidget/SVersionUpdater/SVersionUpdaterWidget.cpp
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 Source/Private/SlateWidget/SVersionUpdater/SVersionUpdaterWidget.h
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;
};

Loading

0 comments on commit c9c49ea

Please sign in to comment.