Skip to content

Commit

Permalink
wippppp
Browse files Browse the repository at this point in the history
  • Loading branch information
CassiusPacheco committed Sep 19, 2023
1 parent eb73b5b commit 11ee2ad
Show file tree
Hide file tree
Showing 25 changed files with 763 additions and 155 deletions.
3 changes: 3 additions & 0 deletions Plugins/Mac/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DerivedData
WebView.xcodeproj/project.xcworkspace/xcuserdata/
WebView.xcodeproj/xcuserdata/
28 changes: 28 additions & 0 deletions Plugins/Mac/Resources/Info-WebViewSeparated.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
28 changes: 28 additions & 0 deletions Plugins/Mac/Resources/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
2 changes: 2 additions & 0 deletions Plugins/Mac/Resources/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Localized versions of Info.plist keys */

3 changes: 3 additions & 0 deletions Plugins/Mac/Resources/Prefix.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,9 @@
* 3. This notice may not be removed or altered from any source distribution.
*/

#if canImport(UIKit)
#import <UIKit/UIKit.h>
#else
#import <AppKit/AppKit.h>
#endif

#import <WebKit/WebKit.h>

#if canImport(UIKit)
// NOTE: we need extern without "C" before unity 4.5
//extern UIViewController *UnityGetGLViewController();
extern "C" UIViewController *UnityGetGLViewController();
extern "C" void UnitySendMessage(const char *, const char *, const char *);
#endif
extern "C" typedef void (*DelegateCallbackFunction)(const char * key, const char * message);

DelegateCallbackFunction delegateCallback = NULL;
Expand Down Expand Up @@ -84,10 +73,10 @@ - (void)load:(NSURLRequest *)request
NSURL *url = [request URL];
if ([url.absoluteString hasPrefix:@"file:"]) {
// LoadFileURL is not loading the JS content due to thinking it's out of the sandbox
// NSURL *top = [NSURL URLWithString:[[url absoluteString] stringByDeletingLastPathComponent]];
// [webView loadFileURL:url allowingReadAccessToURL:top];
NSString *htmlContent = [NSString stringWithContentsOfFile:url.path encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlContent baseURL:url];
NSURL *top = [NSURL URLWithString:[[url absoluteString] stringByDeletingLastPathComponent]];
[webView loadFileURL:url allowingReadAccessToURL:top];
// NSString *htmlContent = [NSString stringWithContentsOfFile:url.path encoding:NSUTF8StringEncoding error:nil];
// [webView loadHTMLString:htmlContent baseURL:url];
} else {
[webView loadRequest:request];
}
Expand All @@ -97,7 +86,8 @@ - (void)load:(NSURLRequest *)request
@interface CWebViewPlugin : NSObject<WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
{
WKWebView *webView;
}
NSWindow *window;
NSWindowController *windowController;}
@end

@implementation CWebViewPlugin
Expand All @@ -110,7 +100,7 @@ - (id)initWithUa:(const char *)ua
{
self = [super init];

// UIView *view = UnityGetGLViewController().view;
CGRect frame = CGRectMake(0, 0, 500, 400);

if (_sharedProcessPool == NULL) {
_sharedProcessPool = [[WKProcessPool alloc] init];
Expand All @@ -132,15 +122,14 @@ - (id)initWithUa:(const char *)ua
";

WKUserScript *script
= [[WKUserScript alloc] initWithSource:str injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
= [[WKUserScript alloc] initWithSource:str injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
[controller addUserScript:script];
configuration.userContentController = controller;
configuration.allowsInlineMediaPlayback = true;
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
configuration.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
configuration.processPool = _sharedProcessPool;

WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
#if UNITYWEBVIEW_DEVELOPMENT
// enable Safari debugging if exists
if ([wkwebView respondsToSelector:@selector(setInspectable:)]) {
Expand All @@ -155,23 +144,17 @@ - (id)initWithUa:(const char *)ua
((WKWebView *)webView).customUserAgent = [[NSString alloc] initWithUTF8String:ua];
}

webView.opaque = NO;
webView.hidden = YES;
// webView.hidden = YES;

#if canImport(UIKit)
// cf. https://rick38yip.medium.com/wkwebview-weird-spacing-issue-in-ios-13-54a4fc686f72
// cf. https://stackoverflow.com/questions/44390971/automaticallyadjustsscrollviewinsets-was-deprecated-in-ios-11-0
((WKWebView *)webView).scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

webView.backgroundColor = [UIColor clearColor];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UIView *view = UnityGetGLViewController().view;
[view addSubview:webView];
#else
[webView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
#endif

window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered
defer:NO];
[window setContentView:webView];
[window orderFront:NSApp];
// [window setDelegate:self];
windowController = [[NSWindowController alloc] initWithWindow:window];
return self;
}

Expand Down Expand Up @@ -292,11 +275,7 @@ - (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNaviga
NSString *url = [nsurl absoluteString];

if ([url rangeOfString:@"//itunes.apple.com/"].location != NSNotFound) {
#if canImport(UIKit)
[[UIApplication sharedApplication] openURL:nsurl];
#else
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
#endif
decisionHandler(WKNavigationActionPolicyCancel);
return;
} else if ([url hasPrefix:@"unity:"]) {
Expand All @@ -307,13 +286,8 @@ - (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNaviga
&& ![url hasPrefix:@"file:"]
&& ![url hasPrefix:@"http:"]
&& ![url hasPrefix:@"https:"]) {
#if canImport(UIKit)
if([[UIApplication sharedApplication] canOpenURL:nsurl]) {
[[UIApplication sharedApplication] openURL:nsurl];
}
#else

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
#endif
decisionHandler(WKNavigationActionPolicyCancel);
return;
} else if (navigationAction.navigationType == WKNavigationTypeLinkActivated
Expand Down Expand Up @@ -352,7 +326,7 @@ - (void)loadURL:(const char *)url
{
if (webView == nil)
return;

WKWebView *_webView = (WKWebView *)webView;
NSString *urlStr = [NSString stringWithUTF8String:url];
NSURL *nsurl = [NSURL URLWithString:urlStr];
Expand All @@ -370,11 +344,11 @@ - (void)evaluateJS:(const char *)js
@end

extern "C" {
void *_CWebViewPlugin_Init(const char *ua);
void _CWebViewPlugin_Destroy(void *instance);
void _CWebViewPlugin_LoadURL(void *instance, const char *url);
void _CWebViewPlugin_EvaluateJS(void *instance, const char *url);
void _CWebViewPlugin_SetDelegate(DelegateCallbackFunction callback);
void *_CWebViewPlugin_Init(const char *ua);
void _CWebViewPlugin_Destroy(void *instance);
void _CWebViewPlugin_LoadURL(void *instance, const char *url);
void _CWebViewPlugin_EvaluateJS(void *instance, const char *url);
void _CWebViewPlugin_SetDelegate(DelegateCallbackFunction callback);
}

void _CWebViewPlugin_SetDelegate(DelegateCallbackFunction callback) {
Expand Down
Loading

0 comments on commit 11ee2ad

Please sign in to comment.