Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/Build
/Build
.DS_Store
13 changes: 2 additions & 11 deletions Apps/Playground/iOS/LibNativeBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <Babylon/ScriptLoader.h>
#import <Babylon/XMLHttpRequest.h>
#import <Shared/InputManager.h>
#import "Babylon/XMLHttpRequestApple.h"

std::unique_ptr<Babylon::AppRuntime> runtime{};
std::unique_ptr<InputManager::InputBuffer> inputBuffer{};
Expand Down Expand Up @@ -35,15 +36,6 @@ - (void)init:(void*)CALayerPtr width:(int)inWidth height:(int)inHeight
runtime = std::make_unique<Babylon::AppRuntime>(std::move(rootUrl));
}

// Initialize console plugin
runtime->Dispatch([](Napi::Env env)
{
Babylon::Console::CreateInstance(env, [](const char* message, auto)
{
NSLog(@"%s", message);
});
});

// Initialize NativeWindow plugin
float width = inWidth;
float height = inHeight;
Expand All @@ -55,8 +47,7 @@ - (void)init:(void*)CALayerPtr width:(int)inWidth height:(int)inHeight

Babylon::InitializeNativeEngine(*runtime, windowPtr, width, height);

// Initialize XMLHttpRequest plugin.
Babylon::InitializeXMLHttpRequest(*runtime, runtime->RootUrl());
InitializeXMLHttpRequest(*runtime);

inputBuffer = std::make_unique<InputManager::InputBuffer>(*runtime);
InputManager::Initialize(*runtime, *inputBuffer);
Expand Down
13 changes: 2 additions & 11 deletions Apps/Playground/macOS/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <Babylon/ScriptLoader.h>
#import <Babylon/XMLHttpRequest.h>
#import <Shared/InputManager.h>
#import "Babylon/XMLHttpRequestApple.h"

std::unique_ptr<Babylon::AppRuntime> runtime{};
std::unique_ptr<InputManager::InputBuffer> inputBuffer{};
Expand All @@ -15,7 +16,6 @@ @implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

}

- (void)viewDidAppear {
Expand All @@ -30,15 +30,6 @@ - (void)viewDidAppear {
runtime = std::make_unique<Babylon::AppRuntime>(std::move(rootUrl));
}

// Initialize console plugin
runtime->Dispatch([](Napi::Env env)
{
Babylon::Console::CreateInstance(env, [](const char* message, auto)
{
NSLog(@"%s", message);
});
});

// Initialize NativeWindow plugin
NSSize size = [self view].frame.size;
float width = size.width;
Expand All @@ -53,7 +44,7 @@ - (void)viewDidAppear {
Babylon::InitializeNativeEngine(*runtime, windowPtr, width, height);

// Initialize XMLHttpRequest plugin.
Babylon::InitializeXMLHttpRequest(*runtime, runtime->RootUrl());
InitializeXMLHttpRequest(*runtime);

inputBuffer = std::make_unique<InputManager::InputBuffer>(*runtime);
InputManager::Initialize(*runtime, *inputBuffer);
Expand Down
26 changes: 19 additions & 7 deletions Dependencies/BabylonNativeUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
set(SOURCES
"Include/Babylon/NetworkUtils.h"
"Include/Babylon/TicketedCollection.h"
"Source/NetworkUtils.cpp")
if(APPLE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm a little iffy about the platform-specific guards we have in here. I know they're inevitable in certain cases, but we really ended up with a ton of them before the plugin refactor (many of which we still have), and for me it felt pretty messy. Can we look for alternatives for how to minimize these, at least within scenarios where we have duplicated code, as below?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best alternative is to have specific plugin/project per platform IMHO. instead of guards we would have more cmakelists.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with that. Is this just a temporary workaround, then? If so, can we associate an issue with it?

set(SOURCES
"Include/Babylon/NetworkUtils.h"
"Include/Babylon/TicketedCollection.h"
"Source/NetworkUtils.mm")
else()
set(SOURCES
"Include/Babylon/NetworkUtils.h"
"Include/Babylon/TicketedCollection.h"
"Source/NetworkUtils.cpp")
endif()

add_library(BabylonNativeUtils ${SOURCES})

target_include_directories(BabylonNativeUtils PRIVATE "Include/Babylon")
target_include_directories(BabylonNativeUtils INTERFACE "Include")

target_link_libraries(BabylonNativeUtils
PUBLIC arcana
PRIVATE libcurl)
if(APPLE)
target_link_libraries(BabylonNativeUtils
PUBLIC arcana)
else()
target_link_libraries(BabylonNativeUtils
PUBLIC arcana
PRIVATE libcurl)
endif()
54 changes: 54 additions & 0 deletions Dependencies/BabylonNativeUtils/Source/NetworkUtils.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "NetworkUtils.h"
#import <Foundation/Foundation.h>

namespace Babylon
{
std::string GetAbsoluteUrl(const std::string& url, const std::string& rootUrl)
{
NSString *urlStr = [NSString stringWithCString:url.c_str() encoding:[NSString defaultCStringEncoding]];
Comment thread
bghgary marked this conversation as resolved.
NSString *rootUrlStr = [NSString stringWithCString:rootUrl.c_str() encoding:[NSString defaultCStringEncoding]];
NSString *completeURL = [NSString stringWithFormat:@"%@/%@",rootUrlStr,urlStr];
NSURL *baseUrl = [NSURL URLWithString:completeURL];
NSError *error;
BOOL reachable = [baseUrl checkResourceIsReachableAndReturnError:&error];
if (reachable)
{
return std::string([completeURL UTF8String]);
}
return url;
}

template<typename DataT>
arcana::task<DataT, std::exception_ptr> LoadUrlAsync(std::string url)
{
__block arcana::task_completion_source<DataT, std::exception_ptr> taskCompletionSource{};
NSString *urlStr = [NSString stringWithCString:url.c_str() encoding:[NSString defaultCStringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
DataT dataT{};
dataT.resize(data.length);
[data getBytes:dataT.data()
length:data.length];
Comment thread
bghgary marked this conversation as resolved.
taskCompletionSource.complete(std::move(dataT));
}
else
{
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
[task resume];
return taskCompletionSource.as_task();
}

arcana::task<std::string, std::exception_ptr> LoadTextAsync(std::string url)
{
return LoadUrlAsync<std::string>(std::move(url));
}

arcana::task<std::vector<uint8_t>, std::exception_ptr> LoadBinaryAsync(std::string url)
{
return LoadUrlAsync<std::vector<uint8_t>>(std::move(url));
}
}
45 changes: 23 additions & 22 deletions Dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,31 @@ endif()

# -------------------------------- curl --------------------------------
# Dependencies: openssl (indirect),
if(APPLE OR ANDROID)
set(CURL_CA_BUNDLE "none" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
set(CURL_CA_PATH "none" CACHE PATH "Path to SSL CA Certificate Directory")
endif()

if(APPLE)
set(CMAKE_USE_SECTRANSP ON CACHE BOOL "enable Apple OS native SSL/TLS")
elseif(WIN32 OR WINDOWS_STORE)
set(CMAKE_USE_WINSSL ON CACHE BOOL "Set cURL to use WinSSL by default.")
else()
set(CMAKE_USE_WINSSL OFF CACHE BOOL "Set cURL to use WinSSL by default.")
endif()

add_subdirectory(curl)
set_property(TARGET libcurl PROPERTY FOLDER Dependencies)

# TODO: Certain parts of cURL's functionality are gated behind WINAPI checks
# that cause the functionality to become unavailable in UWP. Find a better way
# to ensure that functionality is enabled, then remove the following workaround.
if(WINDOWS_STORE)
target_compile_definitions(libcurl PRIVATE "WINAPI_PARTITION_DESKTOP=1")
if(NOT APPLE)
if(ANDROID)
set(CURL_CA_BUNDLE "none" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
set(CURL_CA_PATH "none" CACHE PATH "Path to SSL CA Certificate Directory")
endif()

if(WIN32 OR WINDOWS_STORE)
set(CMAKE_USE_WINSSL ON CACHE BOOL "Set cURL to use WinSSL by default.")
else()
set(CMAKE_USE_WINSSL OFF CACHE BOOL "Set cURL to use WinSSL by default.")
endif()

add_subdirectory(curl)
set_property(TARGET libcurl PROPERTY FOLDER Dependencies)

# TODO: Certain parts of cURL's functionality are gated behind WINAPI checks
# that cause the functionality to become unavailable in UWP. Find a better way
# to ensure that functionality is enabled, then remove the following workaround.
if(WINDOWS_STORE)
target_compile_definitions(libcurl PRIVATE "WINAPI_PARTITION_DESKTOP=1")
endif()
endif()

# -------------------------------- BabylonNativeUtils --------------------------------
# Dependencies: arcana, curl
# Dependencies: arcana, curl (not for Apple)

add_subdirectory(BabylonNativeUtils)
set_property(TARGET BabylonNativeUtils PROPERTY FOLDER Dependencies)
4 changes: 3 additions & 1 deletion Dependencies/napi/include/napi/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ namespace Napi
void Detach(Napi::Env);

Napi::Value Eval(Napi::Env env, const char* source, const char* sourceUrl);
}

template<typename T> T GetContext(Napi::Env env);
}
7 changes: 7 additions & 0 deletions Dependencies/napi/source/env_JavaScriptCore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ namespace Napi
{
delete env.operator napi_env();
}

template<> JSGlobalContextRef GetContext(Napi::Env env)
{
napi_env napienv = env;
return napienv->m_globalContext;
}
}

26 changes: 26 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2181,3 +2181,29 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```

## XMLHttpRequest

````
The MIT License (MIT)

Copyright (c) 2015 Lukas Stührk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```
2 changes: 1 addition & 1 deletion Plugins/NativeEngine/Source/NativeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ namespace Babylon
const spirv_cross::ShaderResources resources = compiler.get_shader_resources();
assert(resources.uniform_buffers.size() == 1);
const spirv_cross::Resource& uniformBuffer = resources.uniform_buffers[0];
#if (BGFX_CONFIG_RENDERER_METAL)
#if __APPLE__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? The comment below indicates that the guarded behavior is specific to Metal, not Apple; is that incorrect?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BGFX_CONFIG_RENDERER_METAL not defined. APPLE is the only define that I've found to make the guard.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should probably associate this with the rendering engine somewhere down the line, since this could cause problems if somebody doesn't use the default runtime for the platform (i.e., GL or MoltenVK or whatever).

// with metal, we bind images and not samplers
const spirv_cross::SmallVector<spirv_cross::Resource>& samplers = resources.separate_images;
#else
Expand Down
1 change: 0 additions & 1 deletion Plugins/ScriptLoader/Source/ScriptLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ScriptLoader.h"

#include <Babylon/NetworkUtils.h>

#include <arcana/threading/task.h>

namespace Babylon
Expand Down
23 changes: 18 additions & 5 deletions Plugins/XMLHttpRequest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
set(SOURCES
"Include/Babylon/XMLHttpRequest.h"
"Source/XMLHttpRequest.cpp"
"Source/XMLHttpRequest.h"
"Source/XMLHttpRequest${BABYLON_NATIVE_PLATFORM}.cpp")
if(APPLE)
Comment thread
syntheticmagus marked this conversation as resolved.
set(SOURCES
"Include/Babylon/XMLHttpRequestApple.h"
"Source/XMLHttpRequestApple.mm")

# CLANG_ENABLE_OBJC_WEAK = YES
set_source_files_properties(
${SOURCES}
PROPERTIES
COMPILE_FLAGS -fobjc-weak
)
else()
set(SOURCES
"Include/Babylon/XMLHttpRequest.h"
"Source/XMLHttpRequest.cpp"
"Source/XMLHttpRequest.h"
"Source/XMLHttpRequest${BABYLON_NATIVE_PLATFORM}.cpp")
endif()

add_library(XMLHttpRequest ${SOURCES})

Expand Down
54 changes: 54 additions & 0 deletions Plugins/XMLHttpRequest/Include/Babylon/XMLHttpRequestApple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// XMLHttpRequest from https://github.com/Lukas-Stuehrk/XMLHTTPRequest
// with modifications (addEventListener, arraybuffer)
// MIT License
Comment thread
bghgary marked this conversation as resolved.
#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>
#include <napi/napi.h>
#include <Babylon/JsRuntime.h>

typedef void (^ CompletionHandlerFunction)();
typedef void (^ CompletionHandler)(CompletionHandlerFunction);

void InitializeXMLHttpRequest(Babylon::JsRuntime& runtime);

typedef NS_ENUM(NSUInteger , ReadyState) {
XMLHttpRequestUNSENT =0, // open()has not been called yet.
XMLHttpRequestOPENED, // send()has not been called yet.
XMLHttpRequestHEADERS, // RECEIVED send() has been called, and headers and status are available.
XMLHttpRequestLOADING, // Downloading; responseText holds partial data.
XMLHttpRequestDONE // The operation is complete.
};

@protocol XMLHttpRequest <JSExport>
@property (nonatomic, retain) JSValue *response;
@property (nonatomic) NSString *responseText;
@property (nonatomic, copy) NSString *responseType;

@property (nonatomic) NSNumber *readyState;
@property (nonatomic) JSValue *onload;
@property (nonatomic) JSValue *onerror;
@property (nonatomic) NSNumber *status;
@property (nonatomic) NSString *statusText;

-(void)open:(NSString *)httpMethod :(NSString *)url :(bool)async;
-(void)send:(id)data;
-(void)setRequestHeader:(NSString *)name :(NSString *)value;
-(void)addEventListener:(NSString *)event :(JSValue *)callback;
-(void)removeEventListener:(NSString *)event :(JSValue *)callback;
-(NSString *)getAllResponseHeaders;
-(NSString *)getResponseHeader:(NSString *)name;
@end

@interface XMLHttpRequest : NSObject <XMLHttpRequest>
- (instancetype)initWithURLSession: (NSURLSession *)urlSession;
- (void)extend:(JSGlobalContextRef)globalContextRef:(Babylon::JsRuntime *)runtime;
@property (nonatomic) NSMutableDictionary *_eventHandlers;
@property (atomic, copy) NSURLSession *_urlSession;
@property (atomic, copy) NSString *_httpMethod;
@property (atomic, copy) NSURL *_url;
@property (atomic) bool _async;
@property (nonatomic) NSMutableDictionary *_requestHeaders;
@property (atomic, copy) NSDictionary *_responseHeaders;
//@property (atomic, copy) NSString *_urlString;
@property (nonatomic, retain) JSValue *_onreadystatechange;
@end
9 changes: 0 additions & 9 deletions Plugins/XMLHttpRequest/Source/XMLHttpRequestApple.cpp

This file was deleted.

Loading