Skip to content
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

Add web support #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Plugins/JS.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Plugins/JS/AdPlayer.jslib
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mergeInto(LibraryManager.library, {
////////////////////////////////////////////////////////////////
// Player API
////////////////////////////////////////////////////////////////
Ada_Player_InitializePublisher: function (pubId, tagId) {
pubId = UTF8ToString(pubId);
tagId = UTF8ToString(tagId);
return runAdPlayer((ada) => ada.player.initializePublisher(pubId, tagId));
},

////////////////////////////////////////////////////////////////
// Placement API
////////////////////////////////////////////////////////////////
Ada_Placement_Alloc: function () {
return runAdPlayer((ada) => ada.placement.alloc());
},

////////////////////////////////////////////////////////////////
Ada_Placement_Dispose: function (id) {
id = UTF8ToString(id);
return runAdPlayer((ada) => ada.placement.dispose(id));
},

////////////////////////////////////////////////////////////////
Ada_Placement_UpdatePosition: function (id, x, y, width, height) {
id = UTF8ToString(id);
return runAdPlayer((ada) => ada.placement.updatePosition(id, x, y, width, height));
},

////////////////////////////////////////////////////////////////
Ada_Placement_AttachTag: function (id, tagId) {
id = UTF8ToString(id);
tagId = UTF8ToString(tagId);
return runAdPlayer((ada) => ada.placement.attachTag(id, tagId));
},
});
32 changes: 32 additions & 0 deletions Plugins/JS/AdPlayer.jslib.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

167 changes: 167 additions & 0 deletions Plugins/JS/AdPlayer.jspre
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
const AdPlayer = new function () {
var tag2pub = {};
var placementCounter = 0;
var placementsById = {};

////////////////////////////////////////////////////////////////
// Player API
////////////////////////////////////////////////////////////////
this.player = new function () {
////////////////////////////////////////////////////////////////
this.initializePublisher = function (pubId, tagId) {
console.log("player.initializePublisher: pubId = " + pubId + ", tagId = " + tagId);
tag2pub[tagId] = pubId;
};
};

////////////////////////////////////////////////////////////////
// Placement API
////////////////////////////////////////////////////////////////
this.placement = new function () {
////////////////////////////////////////////////////////////////
this.alloc = function () {
console.log("placement.alloc");

const unityContainer = Module.canvas.parentElement;
if (!unityContainer) {
return console.error("unity container not found");
}

const playerContainer = document.createElement("div");
playerContainer.id = "av_player_" + (++placementCounter) + "_container";
playerContainer.style = "background-color: red;";
playerContainer.style.position = "absolute";
playerContainer.style.top = "0px";
playerContainer.style.left = "0px";
playerContainer.style.width = "0px";
playerContainer.style.height = "0px";

placementsById[playerContainer.id] = playerContainer;
unityContainer.appendChild(playerContainer);

return playerContainer.id;
};

////////////////////////////////////////////////////////////////
this.dispose = function (id) {
console.log("placement.dispose: id = " + id);

const playerContainer = placementsById[id];
if (!playerContainer) {
return;
}

playerContainer.remove();
// TODO dispose player instance
};

////////////////////////////////////////////////////////////////
this.updatePosition = function (id, x, y, width, height) {
console.log("placement.updatePosition: id = " + id + ", x = " + x + ", y = " + y + ", w = " + width + ", h = " + height);

const playerContainer = placementsById[id];
if (!playerContainer) {
return;
}

const bounds = Module.canvas.getBoundingClientRect();
if (bounds) {
const clientWidth = bounds.width;
const clientHeight = bounds.height;
const viewportWidth = Module.canvas.width;
const viewportHeight = Module.canvas.height;

if (clientWidth > 0 && clientHeight > 0 && viewportWidth > 0 && viewportHeight > 0) {
const scaleX = clientWidth / viewportWidth;
const scaleY = clientHeight / viewportHeight;

x = x * scaleX;
y = y * scaleY;
width = width * scaleX;
height = height * scaleY;
}
}

const player = playerContainer.avPlayer;
if (player) {
player.resize(width, height);
}

playerContainer.style.top = y + "px";
playerContainer.style.left = x + "px";
playerContainer.style.width = width + "px";
playerContainer.style.height = height + "px";
};

////////////////////////////////////////////////////////////////
this.attachTag = function (id, tagId) {
console.log("placement.attachTag: id = " + id + ", tagId = " + tagId);

const pubId = tag2pub[tagId];
if (!pubId) {
return console.error("tag " + tagId + " was not initialized");
}

const playerContainer = placementsById[id];
if (!playerContainer) {
return console.error("player " + id + " was not found");
}

const loader = async () => {
const url = "https://tg1.aniview.com/api/adserver/sdk/spt?AV_PUBLISHERID=" + pubId + "&AV_TAGID=" + tagId;
const response = await fetch(url);
const json = await response.json();
const channelId = json.config.channelId;

if (!channelId) {
return console.error("failed to fetch channel id");
}

console.log("placement.attachTag: id = " + id + ", channelId = " + channelId);

const playerConfig = {
publisherId: pubId,
channelId: channelId,
tagId: tagId,
width: playerContainer.clientWidth,
height: playerContainer.clientHeight,
position: playerContainer.id
};

const playerScript = document.createElement("script");
playerScript.src = "https://player.aniview.com/script/6.1/player.js?v=1&type=s&pid=" + pubId;
playerScript.crossOrigin = "anonymous"; // TOOD remove when fixed
playerScript.onload = function () {
if (!playerContainer.parentElement) {
return;
}
const player = new avPlayer(playerConfig);
player.play(playerConfig);
playerContainer.avPlayer = player;
};
playerScript.onerror = function (error) {
console.error("placement.attachTag: id = " + id + ", error = " + error);
// TODO handle error
};
playerContainer.appendChild(playerScript);
};

loader().catch((error) => {
console.error(error); // TODO properly handle error
});
};
};
};

function runAdPlayer(closure) {
try {
var result = closure(AdPlayer);
if (typeof result == "string") {
result = stringToNewUTF8(result);
}
return result;
} catch (e) {
console.error(e);
return null;
}
}
32 changes: 32 additions & 0 deletions Plugins/JS/AdPlayer.jspre.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Runtime/AdPlacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ public static IAdPlacement Alloc()
{
return Application.platform switch
{
#if UNITY_ANDROID
RuntimePlatform.Android => new AdPlacementAndroid(),
#endif
#if UNITY_IOS
RuntimePlatform.IPhonePlayer => new AdPlacementIOS(),
#endif
#if UNITY_WEBGL
RuntimePlatform.WebGLPlayer => new AdPlacementJs(),
#endif
_ => throw new Exception("unsupported platform"),
};
}
Expand Down
4 changes: 4 additions & 0 deletions Runtime/AdPlacementAndroid.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading;
using UnityEngine;

#if UNITY_ANDROID

namespace AdPlayer
{
internal class AdPlacementAndroid : IAdPlacement
Expand Down Expand Up @@ -33,3 +35,5 @@ public void UpdatePosition(int x, int y, int width, int height)
}
}
}

#endif
9 changes: 7 additions & 2 deletions Runtime/AdPlacementIOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Runtime.InteropServices;
using UnityEngine;

#if UNITY_IOS

namespace AdPlayer
{
internal class AdPlacementIOS : IAdPlacement
Expand All @@ -18,7 +20,8 @@ internal class AdPlacementIOS : IAdPlacement

private readonly string placementId = Guid.NewGuid().ToString();

internal AdPlacementIOS() {
internal AdPlacementIOS()
{
_createAdPlacement(placementId);
}

Expand All @@ -34,7 +37,7 @@ public void Dispose()

public void AttachTag(string tagId)
{
_attachTag(placementId, tagId);
_attachTag(placementId, tagId);
}

public void UpdatePosition(int x, int y, int width, int height)
Expand All @@ -43,3 +46,5 @@ public void UpdatePosition(int x, int y, int width, int height)
}
}
}

#endif
47 changes: 47 additions & 0 deletions Runtime/AdPlacementJs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Threading;
using System.Runtime.InteropServices;
using UnityEngine;

namespace AdPlayer
{
internal class AdPlacementJs : IAdPlacement
{
[DllImport("__Internal")]
private static extern string Ada_Placement_Alloc();

[DllImport("__Internal")]
private static extern void Ada_Placement_Dispose(string id);

[DllImport("__Internal")]
private static extern void Ada_Placement_AttachTag(string id, string tagId);

[DllImport("__Internal")]
private static extern void Ada_Placement_UpdatePosition(string id, int x, int y, int width, int height);

private string id = Ada_Placement_Alloc();
private int disposed = 0;

~AdPlacementJs()
{
Dispose();
}

public void Dispose()
{
if (Interlocked.Exchange(ref disposed, 1) == 0)
{
Ada_Placement_Dispose(id);
}
}

public void AttachTag(string tagId)
{
Ada_Placement_AttachTag(id, tagId);
}

public void UpdatePosition(int x, int y, int width, int height)
{
Ada_Placement_UpdatePosition(id, x, y, width, height);
}
}
}
Loading