Skip to content

Commit

Permalink
Add mraid Lua module
Browse files Browse the repository at this point in the history
  • Loading branch information
aglitchman committed Jan 29, 2024
1 parent 4c60cad commit 83766aa
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .defignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/playable_ad/build
/playable_ad/node_modules
/playable_ad/package.json
/playable_ad/package-lock.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ npm install --global gulp-cli

## Usage

Copy the `playable_ad` folder into the root of your project + the `.defignore` file. Then, in the command line:
Copy the `playable_ad` folder into the root of your project and the `.defignore` file. Then, in the command line:

```
cd your_project_folder
Expand Down
File renamed without changes.
23 changes: 9 additions & 14 deletions example/main.script
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ local function toggle_music(self, enabled)
end
end

local function window_callback(self, event, data)
if event == window.WINDOW_EVENT_FOCUS_LOST then
print("window.WINDOW_EVENT_FOCUS_LOST")
toggle_music(self, false)
elseif event == window.WINDOW_EVENT_FOCUS_GAINED then
print("window.WINDOW_EVENT_FOCUS_GAINED")
toggle_music(self, true)
elseif event == window.WINDOW_EVENT_ICONIFIED then
print("window.WINDOW_EVENT_ICONIFIED")
elseif event == window.WINDOW_EVENT_DEICONIFIED then
print("window.WINDOW_EVENT_DEICONIFIED")
elseif event == window.WINDOW_EVENT_RESIZED then
local function mraid_callback(self, event, data)
if event == mraid.EVENT_VIEWABLE_CHANGE then
print("mraid.EVENT_VIEWABLE_CHANGE:", data.viewable)
toggle_music(self, data.viewable)
end
end

Expand All @@ -36,7 +28,10 @@ end
--

function init(self)
window.set_listener(window_callback)
-- If `mraid` is available, the game is ran in the webview of Unity Ads or other mraid-based ad system.
if mraid then
mraid.set_listener(mraid_callback)
end

toggle_music(self, true)
go.set("/music", "euler.z", 20)
Expand All @@ -53,4 +48,4 @@ function init(self)
end

function on_input(self, action_id, action)
end
end
14 changes: 11 additions & 3 deletions example/play_now.gui
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
script: "/example/play_now.gui_script"
fonts {
name: "vera_large"
font: "/example/vera_large.font"
font: "/example/assets/fonts/vera_large.font"
}
textures {
name: "main"
Expand Down Expand Up @@ -33,8 +33,8 @@ nodes {
w: 1.0
}
size {
x: 329.0
y: 93.0
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
Expand Down Expand Up @@ -66,6 +66,10 @@ nodes {
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_AUTO
custom_type: 0
enabled: true
visible: true
material: ""
}
nodes {
position {
Expand Down Expand Up @@ -129,6 +133,10 @@ nodes {
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
custom_type: 0
enabled: true
visible: true
material: ""
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
Expand Down
2 changes: 2 additions & 0 deletions playable_ad/build.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[playable_ad]
enabled = 1
4 changes: 3 additions & 1 deletion playable_ad/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const projectDir = "..";
const buildDir = "build";
const archiveDir = "archive";
const bundleJsWebPath = buildDir + "/output_js-web";
const buildSettingsPath = "build.settings";

const bobJarVersionInfoUrl = "https://d.defold.com/stable/info.json";
let bobJarVersionInfo = null; // filled by fetchBobVersionInfo()
Expand Down Expand Up @@ -240,8 +241,9 @@ function buildGame(cb) {
"--build-server", options["build-server"],
"--variant", options.variant,
"--texture-compression", options["texture-compression"],
"--settings", playableAdDir + "/" + buildSettingsPath,
],
(options.settings ? ["--settings", options.settings] : []),
(options.settings ? [options.settings] : []),
[
"--bundle-output", playableAdDir + "/" + bundleJsWebPath,
"--platform", "js-web",
Expand Down
19 changes: 19 additions & 0 deletions playable_ad/lib/web/library_mraid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var LibraryDefMraid = {
$DefMraid: {
viewableChangeFunc: null,

onViewableChange: function (viewable) {
if (DefMraid.viewableChangeFunc) {
{{{ makeDynCall('vi', 'DefMraid.viewableChangeFunc') }}}(viewable);
}
},
},

DefMraid_SetCallback: function (cbfun) {
DefMraid.viewableChangeFunc = cbfun;
return 1;
},
};

autoAddDeps(LibraryDefMraid, '$DefMraid');
mergeInto(LibraryManager.library, LibraryDefMraid);
91 changes: 42 additions & 49 deletions playable_ad/manifests/web/engine_template.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<html>
<head>
{{#playable_ad.enabled}}
<script data-src="fzstd.js" embed></script>
<script>
// Base64 decoder. RFC 4648, but it doesn't support ending "=" (it is stripped by the gulp script)
Expand Down Expand Up @@ -229,8 +230,10 @@
}
};
</script>
{{/playable_ad.enabled}}
</head>
<body>
{{#playable_ad.enabled}}
<div id="running-from-file-warning" merge="keep"></div>
<script id="engine-loader" src="dmloader.js" embed="compress"></script>
<script data-src="{{exe-name}}_archive.js" embed></script>
Expand Down Expand Up @@ -264,67 +267,57 @@
// The main point is that mraid is injected by their webview and we can just use the `mraid.*` methods.
// If `mraid` is available, then it's Unity Ads or other mraid-based ad system.
if (window.mraid) {
// Defold does not currently have a public API to hook the game startup.
// Therefore, we override this function to figure that:
const old_preloadAndCallMain = Module._preloadAndCallMain;
Module._preloadAndCallMain = function () {
if (!Module._archiveLoaded) {
old_preloadAndCallMain();
return;
}
(function() {
// Defold does not currently have a public API to hook the game startup.
// Therefore, we override this function to figure that:
var old_preloadAndCallMain = Module._preloadAndCallMain;
Module._preloadAndCallMain = function () {
if (!Module._archiveLoaded) {
old_preloadAndCallMain();
return;
}

// Wait for the SDK to become ready
if (mraid.getState() === "loading") {
mraid.addEventListener("ready", onSdkReady);
} else {
onSdkReady();
}
};
// Wait for the SDK to become ready
if (mraid.getState() === "loading") {
mraid.addEventListener("ready", onSdkReady);
} else {
onSdkReady();
}
};

let started = false;
let paused = false;
function startGame() {
console.log("mraid support: start game.");
if (!started) {
started = true;
old_preloadAndCallMain();
} else {
if (paused) {
paused = false;
GLFW.onFocusChanged(1);
var started = false;
function startGame() {
if (!started) {
started = true;
console.log("mraid support: start game.");
old_preloadAndCallMain();
}
}
}

function pauseGame() {
console.log("mraid support: pause game.");
if (!paused) {
paused = true;
GLFW.onFocusChanged(0);
}
}
function viewableChangeHandler(viewable) {
if (viewable) {
startGame();
}

function viewableChangeHandler(viewable) {
// start/pause/resume gameplay, stop/play sounds
if(viewable) {
startGame();
} else {
pauseGame();
if (typeof DefMraid !== 'undefined') {
DefMraid.onViewableChange(viewable);
}
}
}

function onSdkReady() {
console.log("mraid support: SDK is ready.");
mraid.addEventListener("viewableChange", viewableChangeHandler);
function onSdkReady() {
console.log("mraid support: SDK is ready.");
mraid.addEventListener("viewableChange", viewableChangeHandler);

// Wait for the ad to become viewable for the first time
if (mraid.isViewable()) {
startGame();
// Wait for the ad to become viewable for the first time
if (mraid.isViewable()) {
startGame();
}
}
}

console.log("mraid support: hook installed.");
console.log("mraid support: hook installed.");
})();
}
</script>
{{/playable_ad.enabled}}
</body>
</html>
Loading

0 comments on commit 83766aa

Please sign in to comment.