Skip to content

feat: ios support for ue 5.2 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Source/Immutable/Private/Immutable/ImmutableSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ void UImmutableSubsystem::Initialize(FSubsystemCollectionBase& Collection)
#if PLATFORM_ANDROID
// Enable DOM storage so we can use localStorage in the Android webview
GConfig->SetBool(TEXT("/Script/AndroidRuntimeSettings.AndroidRuntimeSettings"), TEXT("bEnableDomStorage"), true, GEngineIni);
#endif

#if PLATFORM_ANDROID | PLATFORM_IOS
EngineInitCompleteHandle = FCoreDelegates::OnFEngineLoopInitComplete.AddUObject(this, &UImmutableSubsystem::OnViewportCreated);
#else
ViewportCreatedHandle = UGameViewportClient::OnViewportCreated().AddUObject(this, &UImmutableSubsystem::OnViewportCreated);
Expand All @@ -40,8 +42,11 @@ void UImmutableSubsystem::Deinitialize()
BrowserWidget = nullptr;
ImtblBlui = nullptr;
Passport = nullptr;

#if PLATFORM_ANDROID | PLATFORM_IOS
UGameViewportClient::OnViewportCreated().Remove(EngineInitCompleteHandle);
#else
UGameViewportClient::OnViewportCreated().Remove(ViewportCreatedHandle);
#endif
FWorldDelegates::OnWorldTickStart.Remove(WorldTickHandle);

Super::Deinitialize();
Expand Down
6 changes: 3 additions & 3 deletions Source/Immutable/Private/Immutable/ImtblBrowserUserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TSharedRef<SWidget> UImtblBrowserUserWidget::RebuildWidget()
ScaleBox->AddChild(Browser);
if (UCanvasPanelSlot* RootWidgetSlot = Cast<UCanvasPanelSlot>(ScaleBox->Slot))
{
#if PLATFORM_ANDROID
#if PLATFORM_ANDROID | PLATFORM_IOS
// Android webview needs to be at least 1px to 1px big to work
// but it can be off screen
RootWidgetSlot->SetAnchors(FAnchors(0, 0, 0, 0));
Expand Down Expand Up @@ -85,8 +85,8 @@ void UImtblBrowserUserWidget::RemoveFromParent()

void UImtblBrowserUserWidget::OnWidgetRebuilt()
{
#if PLATFORM_ANDROID
// Android webview needs to be visible to work
#if PLATFORM_ANDROID | PLATFORM_IOS
// Mobile webview needs to be visible to work
#else
SetVisibility(ESlateVisibility::Collapsed);
#endif
Expand Down
132 changes: 69 additions & 63 deletions Source/Immutable/Private/Immutable/ImtblBrowserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

UImtblBrowserWidget::UImtblBrowserWidget()
{
IMTBL_LOG_FUNCSIG

JSConnector = NewObject<UImtblJSConnector>(this, "JSConnector");
JSConnector->ExecuteJs = UImtblJSConnector::FOnExecuteJsDelegate::CreateUObject(this, &UImtblBrowserWidget::ExecuteJS);

// WebBrowserWidget->LoadString("<html><head><title>Test</title></head><body><h1>Test</h1></body></html>", TEXT("http://www.google.com"));
// InitialURL = TEXT("http://www.google.com");
// InitialURL = TEXT("chrome://version");
// IPluginManager& PluginManager = IPluginManager::Get();
// if (const TSharedPtr<IPlugin> Plugin = PluginManager.FindPlugin("Immutable"))
// {
// InitialURL = FString::Printf(TEXT("%s%s"), TEXT("file:///"), *FPaths::ConvertRelativePathToFull(FPaths::Combine(Plugin->GetContentDir(), TEXT("index.html"))));
// IMTBL_LOG("Loading initial url: %s", *InitialURL)
// }
InitialURL = TEXT("about:blank");
IMTBL_LOG_FUNCSIG

JSConnector = NewObject<UImtblJSConnector>(this, "JSConnector");
JSConnector->ExecuteJs = UImtblJSConnector::FOnExecuteJsDelegate::CreateUObject(this, &UImtblBrowserWidget::ExecuteJS);

// WebBrowserWidget->LoadString("<html><head><title>Test</title></head><body><h1>Test</h1></body></html>", TEXT("http://www.google.com"));
// InitialURL = TEXT("http://www.google.com");
// InitialURL = TEXT("chrome://version");
// IPluginManager& PluginManager = IPluginManager::Get();
// if (const TSharedPtr<IPlugin> Plugin = PluginManager.FindPlugin("Immutable"))
// {
// InitialURL = FString::Printf(TEXT("%s%s"), TEXT("file:///"), *FPaths::ConvertRelativePathToFull(FPaths::Combine(Plugin->GetContentDir(), TEXT("index.html"))));
// IMTBL_LOG("Loading initial url: %s", *InitialURL)
// }
InitialURL = TEXT("about:blank");
}

void UImtblBrowserWidget::BindConnector()
Expand All @@ -41,10 +41,10 @@ void UImtblBrowserWidget::BindConnector()

if (JSConnector)
{
if (BindUObject(UImtblJSConnector::JSObjectName(), JSConnector))
{
JSConnector->Init(IsPageLoaded());
}
if (BindUObject(UImtblJSConnector::JSObjectName(), JSConnector))
{
JSConnector->Init(IsPageLoaded());
}
}
}

Expand All @@ -60,7 +60,7 @@ bool UImtblBrowserWidget::IsPageLoaded() const
#if USING_BUNDLED_CEF
return WebBrowserWidget.IsValid() && WebBrowserWidget->IsLoaded();
#endif
return false;
return false;
}


Expand All @@ -78,7 +78,7 @@ void UImtblBrowserWidget::ExecuteJS(const FString& ScriptText) const
void UImtblBrowserWidget::SetBrowserContent()
{
#if USING_BUNDLED_CEF
FSoftObjectPath AssetRef(TEXT("/Script/Immutable.ImtblSDKResource'/Immutable/PackagedResources/index.index'"));
FSoftObjectPath AssetRef(TEXT("/Script/Immutable.ImtblSDKResource'/Immutable/PackagedResources/index.index'"));
if (UObject* LoadedAsset = AssetRef.TryLoad())
{
if (auto Resource = Cast<UImtblSDKResource>(LoadedAsset))
Expand All @@ -89,10 +89,10 @@ void UImtblBrowserWidget::SetBrowserContent()
return;
}

const FString IndexHtml = FString("<!doctype html><html lang='en'><head><meta charset='utf-8'><title>GameSDK Bridge</title><script>")
+ Resource->Js
+ FString("</script></head><body><h1>Bridge Running</h1></body></html>");
const FString IndexHtml = FString("<!doctype html><html lang='en'><head><meta charset='utf-8'><title>GameSDK Bridge</title><script>")
+ Resource->Js
+ FString("</script></head><body><h1>Bridge Running</h1></body></html>");
// IMTBL_LOG("Loaded resource: %s", *Resource->GetName())
WebBrowserWidget->LoadString(IndexHtml, TEXT("file://immutable/index.html"));
// WebBrowserWidget->LoadURL(FString::Printf(TEXT("%s%s"), TEXT("file:///"), *FPaths::ConvertRelativePathToFull(FPaths::Combine(FPaths::ProjectContentDir(), TEXT("html"), TEXT("index.html")))));
Expand All @@ -119,62 +119,68 @@ bool UImtblBrowserWidget::BindUObject(const FString& Name, UObject* Object, cons

void UImtblBrowserWidget::ReleaseSlateResources(bool bReleaseChildren)
{
Super::ReleaseSlateResources(bReleaseChildren);
Super::ReleaseSlateResources(bReleaseChildren);
#if USING_BUNDLED_CEF
WebBrowserWidget.Reset();
WebBrowserWidget.Reset();
#endif
}


TSharedRef<SWidget> UImtblBrowserWidget::RebuildWidget()
{
if (IsDesignTime())
{
return SNew(SBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
[
SNew(STextBlock)
.Text(NSLOCTEXT("Immutable", "Immutable Web Browser", "Immutable Web Browser"))
];
}
else
{
if (IsDesignTime())
{
return SNew(SBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
[
SNew(STextBlock)
.Text(NSLOCTEXT("Immutable", "Immutable Web Browser", "Immutable Web Browser"))
];
}
else
{
#if USING_BUNDLED_CEF
WebBrowserWidget = SNew(SWebBrowser)
.InitialURL(InitialURL)
.ShowControls(false)
.SupportsTransparency(bSupportsTransparency)
.ShowInitialThrobber(bShowInitialThrobber)
#if PLATFORM_ANDROID
.OnLoadCompleted(BIND_UOBJECT_DELEGATE(FSimpleDelegate, HandleOnLoadCompleted))
WebBrowserWidget = SNew(SWebBrowser)
.InitialURL(InitialURL)
.ShowControls(false)
.SupportsTransparency(bSupportsTransparency)
.ShowInitialThrobber(bShowInitialThrobber)
#if PLATFORM_ANDROID | PLATFORM_IOS
.OnLoadCompleted(BIND_UOBJECT_DELEGATE(FSimpleDelegate, HandleOnLoadCompleted))
#endif
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 1
.OnConsoleMessage(BIND_UOBJECT_DELEGATE(FOnConsoleMessageDelegate, HandleOnConsoleMessage))
.OnConsoleMessage(BIND_UOBJECT_DELEGATE(FOnConsoleMessageDelegate, HandleOnConsoleMessage))
#endif
;
;

return WebBrowserWidget.ToSharedRef();
return WebBrowserWidget.ToSharedRef();
#else
return SNew(SBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
[
SNew(STextBlock)
.Text(NSLOCTEXT("Immutable", "Immutable Web Browser", "Immutable Web Browser"))
];
return SNew(SBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
[
SNew(STextBlock)
.Text(NSLOCTEXT("Immutable", "Immutable Web Browser", "Immutable Web Browser"))
];
#endif
}
}
}


#if PLATFORM_ANDROID
#if PLATFORM_ANDROID | PLATFORM_IOS
void UImtblBrowserWidget::HandleOnLoadCompleted()
{
if (WebBrowserWidget->GetUrl() == "file://immutable/index.html")
{
JSConnector->SetAndroidBridgeReady();
}
#if PLATFORM_ANDROID
FString indexUrl = "file://immutable/index.html";
#else
FString indexUrl = "file:///index.html";
#endif

if (WebBrowserWidget->GetUrl() == indexUrl)
{
JSConnector->SetMobileBridgeReady();
}
}
#endif

Expand All @@ -192,7 +198,7 @@ void UImtblBrowserWidget::HandleOnConsoleMessage(const FString& Message, const F
{
// TODO: add severity to log and callback
IMTBL_LOG("Browser console message: %s, Source: %s, Line: %d", *Message, *Source, Line);
OnConsoleMessage.Broadcast(Message, Source, Line);
OnConsoleMessage.Broadcast(Message, Source, Line);
}
#endif

2 changes: 1 addition & 1 deletion Source/Immutable/Private/Immutable/ImtblBrowserWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IMMUTABLE_API UImtblBrowserWidget : public UWidget
// Bind the JSConnector to the browser window
void BindConnector();

#if PLATFORM_ANDROID
#if PLATFORM_ANDROID | PLATFORM_IOS
void HandleOnLoadCompleted();
#endif

Expand Down
4 changes: 2 additions & 2 deletions Source/Immutable/Private/Immutable/ImtblJSConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ void UImtblJSConnector::SendToGame(FString Message)
}


#if PLATFORM_ANDROID
void UImtblJSConnector::SetAndroidBridgeReady()
#if PLATFORM_ANDROID | PLATFORM_IOS
void UImtblJSConnector::SetMobileBridgeReady()
{
IMTBL_LOG_FUNCSIG
bIsBridgeReady = true;
Expand Down
4 changes: 2 additions & 2 deletions Source/Immutable/Private/Immutable/ImtblJSConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class IMMUTABLE_API UImtblJSConnector : public UObject
// Bind the func to be called for executing JS. Typically by the BrowserWidget (UE5) or Blui for UE4
FOnExecuteJsDelegate ExecuteJs;

#if PLATFORM_ANDROID
void SetAndroidBridgeReady();
#if PLATFORM_ANDROID | PLATFORM_IOS
void SetMobileBridgeReady();
#endif

protected:
Expand Down
2 changes: 1 addition & 1 deletion Source/Immutable/Public/Immutable/ImmutableSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class IMMUTABLE_API UImmutableSubsystem : public UGameInstanceSubsystem

FDelegateHandle WorldTickHandle;
FDelegateHandle ViewportCreatedHandle;
#if PLATFORM_ANDROID
#if PLATFORM_ANDROID | PLATFORM_IOS
FDelegateHandle EngineInitCompleteHandle;
#endif

Expand Down