Match the Java layer: 12 ms touch throttle, video in its view rect with stop/destroy semantics, ShouldLoadURL and Android URLs for the web view

This commit is contained in:
CRTkafa 2026-10-15 22:48:40 +08:00
parent d998c2efd1
commit dc575f7e5f
5 changed files with 63 additions and 21 deletions

View file

@ -9,6 +9,21 @@
namespace sm { namespace sm {
uint32_t ui_call_native(const char* name, std::vector<uint32_t> args); // posts to the UI thread uint32_t ui_call_native(const char* name, std::vector<uint32_t> args); // posts to the UI thread
// runs a native on the UI thread and hands its return value to `done` on the main thread
void ui_call_native_then(const char* name, std::vector<uint32_t> args, std::function<void(uint32_t)> done);
// The game sees Android URLs: map host file URLs back to the /sdcard path it asked for.
static std::string guest_url(NSURL* url) {
if (!url) return "";
std::string u = url.absoluteString.UTF8String;
if (url.isFileURL) {
std::string path = url.path.UTF8String;
std::string root = g_game_dir + "/data";
if (path.compare(0, root.size(), root) == 0)
return "file:///sdcard/Android/data/com.ea.spymouse_carrier/files" + path.substr(root.size());
}
return u;
}
} }
using namespace sm; using namespace sm;
@ -19,7 +34,7 @@ using namespace sm;
@implementation SMWebDelegate @implementation SMWebDelegate
- (void)report:(const char*)native url:(NSURL*)url { - (void)report:(const char*)native url:(NSURL*)url {
std::string u = url ? url.absoluteString.UTF8String : ""; std::string u = guest_url(url);
uint32_t b = self.browser, c = self.client; uint32_t b = self.browser, c = self.client;
ui_call_native(native, {jni_env(), c, jstring(u), b}); ui_call_native(native, {jni_env(), c, jstring(u), b});
} }
@ -32,7 +47,8 @@ using namespace sm;
} }
- (void)webView:(WKWebView*)wv didFailProvisionalNavigation:(WKNavigation*)nav withError:(NSError*)err { - (void)webView:(WKWebView*)wv didFailProvisionalNavigation:(WKNavigation*)nav withError:(NSError*)err {
logf("web page failed: %s", err.localizedDescription.UTF8String); logf("web page failed: %s", err.localizedDescription.UTF8String);
std::string msg = std::string("Loading Error. URL: ") + (wv.URL ? wv.URL.absoluteString.UTF8String : "") + // same text as InAppWebBrowserWebViewClient.onReceivedError
std::string msg = "Loading Error. URL: " + guest_url(wv.URL) + " ErrorCode: " + std::to_string((long)err.code) +
" Description: " + err.localizedDescription.UTF8String; " Description: " + err.localizedDescription.UTF8String;
uint32_t b = self.browser, c = self.client; uint32_t b = self.browser, c = self.client;
ui_call_native("Java_com_ea_InAppWebBrowser_InAppWebBrowserWebViewClient_OnLoadError", {jni_env(), c, jstring(msg), b}); ui_call_native("Java_com_ea_InAppWebBrowser_InAppWebBrowserWebViewClient_OnLoadError", {jni_env(), c, jstring(msg), b});
@ -40,15 +56,18 @@ using namespace sm;
- (void)webView:(WKWebView*)wv - (void)webView:(WKWebView*)wv
decidePolicyForNavigationAction:(WKNavigationAction*)action decidePolicyForNavigationAction:(WKNavigationAction*)action
decisionHandler:(void (^)(WKNavigationActionPolicy))handler { decisionHandler:(void (^)(WKNavigationActionPolicy))handler {
// External links open in the default browser instead of inside the game. // Android: loadUrl() itself is not offered to shouldOverrideUrlLoading; later navigations are, and
NSURL* url = action.request.URL; // the page loads in place unless the game's ShouldLoadURL returns false.
if (action.navigationType == WKNavigationTypeLinkActivated && ![url.scheme isEqualToString:@"file"]) { if (action.navigationType == WKNavigationTypeOther || !action.targetFrame.isMainFrame) {
logf("opening external link %s", url.absoluteString.UTF8String); handler(WKNavigationActionPolicyAllow);
[[NSWorkspace sharedWorkspace] openURL:url];
handler(WKNavigationActionPolicyCancel);
return; return;
} }
handler(WKNavigationActionPolicyAllow); std::string u = guest_url(action.request.URL);
ui_call_native_then("Java_com_ea_InAppWebBrowser_InAppWebBrowserWebViewClient_ShouldLoadURL",
{jni_env(), self.client, jstring(u), self.browser}, [handler, u](uint32_t ok) {
logf("ShouldLoadURL(%s) -> %u", u.c_str(), ok & 0xff);
handler((ok & 0xff) ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel);
});
} }
@end @end

View file

@ -589,13 +589,13 @@ static void register_java() {
java_impl(VP, "init", [](JCall& c) { java_impl(VP, "init", [](JCall& c) {
std::string path = jget(c.args[0].i)->str; std::string path = jget(c.args[0].i)->str;
logf("video init %s at %d,%d %dx%d", path.c_str(), c.args[1].i, c.args[2].i, c.args[3].i, c.args[4].i); logf("video init %s at %d,%d %dx%d", path.c_str(), c.args[1].i, c.args[2].i, c.args[3].i, c.args[4].i);
video_open(c.thiz, path); video_open(c.thiz, path, c.args[1].i, c.args[2].i, c.args[3].i, c.args[4].i);
return JVal{}; return JVal{};
}); });
java_impl(VP, "play", [](JCall& c) { video_play(c.thiz); return JVal{}; }); java_impl(VP, "play", [](JCall& c) { video_play(c.thiz); return JVal{}; });
java_impl(VP, "pause", [](JCall& c) { video_pause(c.thiz); return JVal{}; }); java_impl(VP, "pause", [](JCall& c) { video_pause(c.thiz); return JVal{}; });
java_impl(VP, "stop", [](JCall& c) { video_stop(c.thiz); return JVal{}; }); java_impl(VP, "stop", [](JCall& c) { video_stop(c.thiz); return JVal{}; });
java_impl(VP, "destroy", [](JCall& c) { video_stop(c.thiz); return JVal{}; }); java_impl(VP, "destroy", [](JCall& c) { video_destroy(c.thiz); return JVal{}; });
java_impl(VP, "isPlaying", [](JCall& c) { return JVal::I(video_is_playing(c.thiz)); }); java_impl(VP, "isPlaying", [](JCall& c) { return JVal::I(video_is_playing(c.thiz)); });
} }

View file

@ -51,7 +51,8 @@ void jni_set_audio_stream(SDL_AudioStream* s);
void jni_audio_config(int rate, int channels, int frames); void jni_audio_config(int rate, int channels, int frames);
// video.mm // video.mm
void video_open(uint32_t player, const std::string& path); void video_open(uint32_t player, const std::string& path, int x, int y, int w, int h);
void video_destroy(uint32_t player);
void video_play(uint32_t player); void video_play(uint32_t player);
void video_pause(uint32_t player); void video_pause(uint32_t player);
void video_stop(uint32_t player); void video_stop(uint32_t player);

View file

@ -3,6 +3,7 @@
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
#include <OpenGL/glext.h> #include <OpenGL/glext.h>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <dispatch/dispatch.h>
#include <fcntl.h> #include <fcntl.h>
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#include <signal.h> #include <signal.h>
@ -107,6 +108,13 @@ uint32_t ui_call_native(const char* name, std::vector<uint32_t> args) {
ui.post([n, args] { call_native(n.c_str(), args); }); ui.post([n, args] { call_native(n.c_str(), args); });
return 0; return 0;
} }
void ui_call_native_then(const char* name, std::vector<uint32_t> args, std::function<void(uint32_t)> done) {
std::string n = name;
ui.post([n, args, done] {
uint32_t r = call_native(n.c_str(), args);
dispatch_async(dispatch_get_main_queue(), ^{ done(r); });
});
}
void browser_attach(SDL_Window* win, int game_w, int game_h); void browser_attach(SDL_Window* win, int game_w, int game_h);
void browser_layout(); void browser_layout();
void browser_snapshot(const std::string& png_base); void browser_snapshot(const std::string& png_base);
@ -327,7 +335,10 @@ int main(int argc, char** argv) {
uint32_t ptr_event = native("Java_com_ea_blast_TouchSurfaceAndroid_NativeOnPointerEvent"); uint32_t ptr_event = native("Java_com_ea_blast_TouchSurfaceAndroid_NativeOnPointerEvent");
auto send_touch = [&](uint32_t kind, float x, float y) { auto send_touch = [&](uint32_t kind, float x, float y) {
ui.post([=] { call_guest(ptr_event, {env, touchCls, kind, kTouch, 0, fbits(x), fbits(y)}); }); ui.post([=] {
call_guest(ptr_event, {env, touchCls, kind, kTouch, 0, fbits(x), fbits(y)});
std::this_thread::sleep_for(std::chrono::milliseconds(12)); // TouchSurfaceAndroid sleeps 12 ms per event
});
}; };
uint32_t draw = native("Java_com_ea_blast_AndroidRenderer_NativeOnDrawFrame"); uint32_t draw = native("Java_com_ea_blast_AndroidRenderer_NativeOnDrawFrame");
@ -465,7 +476,6 @@ int main(int argc, char** argv) {
if (!done && opt.skip_video && video_is_playing(0)) done = video_current(); if (!done && opt.skip_video && video_is_playing(0)) done = video_current();
if (done) { if (done) {
logf("video finished"); logf("video finished");
video_stop(done);
ui.post([=] { call_native("Java_com_ea_VideoPlayer_PlayerAndroid_OnCompletionNativeImpl", {env, done}); }); ui.post([=] { call_native("Java_com_ea_VideoPlayer_PlayerAndroid_OnCompletionNativeImpl", {env, done}); });
} }
frame++; frame++;

View file

@ -17,6 +17,7 @@ static uint32_t g_player_obj = 0;
static std::atomic<uint32_t> g_completed{0}; static std::atomic<uint32_t> g_completed{0};
static std::atomic<bool> g_playing{false}; static std::atomic<bool> g_playing{false};
static bool g_muted = false; static bool g_muted = false;
static int g_rx = 0, g_ry = 0, g_rw = 960, g_rh = 640; // the VideoView's layout rect
static GLuint g_tex = 0; static GLuint g_tex = 0;
static int g_tex_w = 0, g_tex_h = 0; static int g_tex_w = 0, g_tex_h = 0;
@ -27,9 +28,10 @@ static std::string resolve(const std::string& path) {
return host_path(path.c_str()); return host_path(path.c_str());
} }
void video_open(uint32_t player, const std::string& path) { void video_open(uint32_t player, const std::string& path, int x, int y, int w, int h) {
@autoreleasepool { @autoreleasepool {
video_stop(g_player_obj); video_destroy(g_player_obj);
g_rx = x; g_ry = y; g_rw = w; g_rh = h;
std::string hp = resolve(path); std::string hp = resolve(path);
NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:hp.c_str()]]; NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:hp.c_str()]];
if (![[NSFileManager defaultManager] fileExistsAtPath:url.path]) { if (![[NSFileManager defaultManager] fileExistsAtPath:url.path]) {
@ -64,8 +66,19 @@ void video_play(uint32_t player) {
g_playing = true; g_playing = true;
[g_player play]; [g_player play];
} }
void video_pause(uint32_t) { if (g_player) [g_player pause]; } // PlayerAndroidHelper.pause(): only when playing
void video_pause(uint32_t) {
if (g_player && g_playing) { [g_player pause]; g_playing = false; }
}
// PlayerAndroidHelper.stop(): seekTo(0), start, pause - the player stays alive
void video_stop(uint32_t) { void video_stop(uint32_t) {
if (!g_player) return;
[g_player seekToTime:kCMTimeZero];
[g_player pause];
g_playing = false;
}
// PlayerAndroidHelper.destroy(): reset and release
void video_destroy(uint32_t) {
@autoreleasepool { @autoreleasepool {
if (g_player) [g_player pause]; if (g_player) [g_player pause];
if (g_end_observer) [[NSNotificationCenter defaultCenter] removeObserver:g_end_observer]; if (g_end_observer) [[NSNotificationCenter defaultCenter] removeObserver:g_end_observer];
@ -73,6 +86,7 @@ void video_stop(uint32_t) {
g_player = nil; g_player = nil;
g_output = nil; g_output = nil;
g_playing = false; g_playing = false;
if (g_tex) { glDeleteTextures(1, &g_tex); g_tex = 0; }
} }
} }
bool video_is_playing(uint32_t) { return g_playing; } bool video_is_playing(uint32_t) { return g_playing; }
@ -114,14 +128,12 @@ bool video_draw(int w, int h) {
glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE); glDisable(GL_SCISSOR_TEST); glDisable(GL_CULL_FACE); glDisable(GL_SCISSOR_TEST);
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_tex); glBindTexture(GL_TEXTURE_2D, g_tex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
float s = std::min(w / (float)g_tex_w, h / (float)g_tex_h); // the surface buffer has the video's size and is scaled to fill the view (no letterbox)
float dw = g_tex_w * s, dh = g_tex_h * s, x0 = (w - dw) / 2, y0 = (h - dh) / 2; float x0 = g_rx, y0 = g_ry, dw = g_rw, dh = g_rh;
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(x0, y0); glTexCoord2f(0, 0); glVertex2f(x0, y0);
glTexCoord2f(1, 0); glVertex2f(x0 + dw, y0); glTexCoord2f(1, 0); glVertex2f(x0 + dw, y0);