Skip to content

Commit

Permalink
Update deprecated code inside jslib plugins (#234)
Browse files Browse the repository at this point in the history
* Update deprecated code inside jslib plugins

* Fix SolanaWalletAdapterWebGL SignAllTransaction bug

* Rename callback parameter to reflect full transaction return
  • Loading branch information
sadegh-askari authored Oct 19, 2024
1 parent a6ef21b commit 90edc61
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 deletions.
16 changes: 9 additions & 7 deletions Runtime/Plugins/NativeWebSocket/NativeWebSocket.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ var LibraryWebSocket = {
if (webSocketState.debug)
console.log("[JSLIB WebSocket] Connected.");

if (webSocketState.onOpen)
Module.dynCall_vi(webSocketState.onOpen, instanceId);
if (webSocketState.onOpen) {
{{{ makeDynCall('vi', 'webSocketState.onOpen') }}}(instanceId);
}

};

Expand All @@ -170,7 +171,7 @@ var LibraryWebSocket = {
HEAPU8.set(dataBuffer, buffer);

try {
Module.dynCall_viii(webSocketState.onMessage, instanceId, buffer, dataBuffer.length);
{{{ makeDynCall('viii', 'webSocketState.onMessage') }}}(instanceId, buffer, dataBuffer.length);
} finally {
_free(buffer);
}
Expand All @@ -182,7 +183,7 @@ var LibraryWebSocket = {
HEAPU8.set(dataBuffer, buffer);

try {
Module.dynCall_viii(webSocketState.onMessage, instanceId, buffer, dataBuffer.length);
{{{ makeDynCall('viii', 'webSocketState.onMessage') }}}(instanceId, buffer, dataBuffer.length);
} finally {
_free(buffer);
}
Expand All @@ -204,7 +205,7 @@ var LibraryWebSocket = {
stringToUTF8(msg, buffer, length);

try {
Module.dynCall_vii(webSocketState.onError, instanceId, buffer);
{{{ makeDynCall('vii', 'webSocketState.onError') }}}(instanceId, buffer);
} finally {
_free(buffer);
}
Expand All @@ -218,8 +219,9 @@ var LibraryWebSocket = {
if (webSocketState.debug)
console.log("[JSLIB WebSocket] Closed.");

if (webSocketState.onClose)
Module.dynCall_vii(webSocketState.onClose, instanceId, ev.code);
if (webSocketState.onClose) {
{{{ makeDynCall('vii', 'webSocketState.onClose') }}}(instanceId, ev.code);
}

delete instance.ws;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ mergeInto(LibraryManager.library, {
"https://cdn.jsdelivr.net/npm/@magicblock-labs/unity-wallet-adapter@1.2.1";
document.head.appendChild(script);
script.onload = function () {
Module.dynCall_vi(callback, isXnft);
{{{ makeDynCall('vi', 'callback') }}}(isXnft);
};
} else {
window.walletAdapterLib.refreshWalletAdapters();
Module.dynCall_vi(callback, isXnft);
{{{ makeDynCall('vi', 'callback') }}}(isXnft);
}
},
ExternGetWallets: async function (callback) {
Expand All @@ -27,10 +27,10 @@ mergeInto(LibraryManager.library, {
var bufferSize = lengthBytesUTF8(wallets) + 1;
var walletsPtr = _malloc(bufferSize);
stringToUTF8(wallets, walletsPtr, bufferSize);
Module.dynCall_vi(callback, walletsPtr);
{{{ makeDynCall('vi', 'callback') }}}(walletsPtr);
} catch (err) {
console.error(err.message);
Module.dynCall_vi(callback, null);
{{{ makeDynCall('vi', 'callback') }}}(null);
}
},
ExternConnectWallet: async function (walletNamePtr, callback) {
Expand All @@ -48,10 +48,10 @@ mergeInto(LibraryManager.library, {
var bufferSize = lengthBytesUTF8(pubKey) + 1;
var pubKeyPtr = _malloc(bufferSize);
stringToUTF8(pubKey, pubKeyPtr, bufferSize);
Module.dynCall_vi(callback, pubKeyPtr);
{{{ makeDynCall('vi', 'callback') }}}(pubKeyPtr);
} catch (err) {
console.error(err.message);
Module.dynCall_vi(callback, null);
{{{ makeDynCall('vi', 'callback') }}}(null);
}
},
ExternSignTransactionWallet: async function (
Expand Down Expand Up @@ -79,10 +79,10 @@ mergeInto(LibraryManager.library, {
var bufferSize = lengthBytesUTF8(txStr) + 1;
var txPtr = _malloc(bufferSize);
stringToUTF8(txStr, txPtr, bufferSize);
Module.dynCall_vi(callback, txPtr);
{{{ makeDynCall('vi', 'callback') }}}(txPtr);
} catch (err) {
console.error(err.message);
Module.dynCall_vi(callback, null);
{{{ makeDynCall('vi', 'callback') }}}(null);
}
},
ExternSignMessageWallet: async function (
Expand Down Expand Up @@ -119,10 +119,10 @@ mergeInto(LibraryManager.library, {
var bufferSize = lengthBytesUTF8(signatureStr) + 1;
var signaturePtr = _malloc(bufferSize);
stringToUTF8(signatureStr, signaturePtr, bufferSize);
Module.dynCall_vi(callback, signaturePtr);
{{{ makeDynCall('vi', 'callback') }}}(signaturePtr);
} catch (err) {
console.error(err.message);
Module.dynCall_vi(callback, null);
{{{ makeDynCall('vi', 'callback') }}}(null);
}
},
ExternSignAllTransactionsWallet: async function (
Expand Down Expand Up @@ -162,10 +162,10 @@ mergeInto(LibraryManager.library, {
var bufferSize = lengthBytesUTF8(txsStr) + 1;
var txsPtr = _malloc(bufferSize);
stringToUTF8(txsStr, txsPtr, bufferSize);
Module.dynCall_vi(callback, txsPtr);
{{{ makeDynCall('vi', 'callback') }}}(txsPtr);
} catch (err) {
console.error(err.message);
Module.dynCall_vi(callback, null);
{{{ makeDynCall('vi', 'callback') }}}(null);
}
},
});
24 changes: 12 additions & 12 deletions Runtime/Plugins/WebGLSupport/WebGLInput/WebGLInput.jslib
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var WebGLInput = {
$instances: [],
WebGLInputInit : function() {
// Remove the `Runtime` object from "v1.37.27: 12/24/2017"
// if Runtime not defined. create and add functon!!
if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
// Initialization if needed
},
WebGLInputCreate: function (canvasId, x, y, width, height, fontsize, text, placeholder, isMultiLine, isPassword, isHidden, isMobile) {
var container = document.getElementById(UTF8ToString(canvasId));
Expand Down Expand Up @@ -100,7 +98,7 @@ var WebGLInput = {
input.setSelectionRange(start + 1, start + 1);
input.oninput(); // call oninput to exe ValueChange function!!
} else {
Runtime.dynCall("vii", cb, [id, e.shiftKey ? -1 : 1]);
{{{ makeDynCall('vii', 'cb') }}}(id, e.shiftKey ? -1 : 1);
}
}
});
Expand All @@ -112,13 +110,13 @@ var WebGLInput = {
WebGLInputOnFocus: function (id, cb) {
var input = instances[id];
input.onfocus = function () {
Runtime.dynCall("vi", cb, [id]);
{{{ makeDynCall('vi', 'cb') }}}(id);
};
},
WebGLInputOnBlur: function (id, cb) {
var input = instances[id];
input.onblur = function () {
Runtime.dynCall("vi", cb, [id]);
{{{ makeDynCall('vi', 'cb') }}}(id);
};
},
WebGLInputIsFocus: function (id) {
Expand All @@ -127,17 +125,19 @@ var WebGLInput = {
WebGLInputOnValueChange:function(id, cb){
var input = instances[id];
input.oninput = function () {
var intArray = intArrayFromString(input.value);
var value = (allocate.length <= 2) ? allocate(intArray, ALLOC_NORMAL):allocate(intArray, 'i8', ALLOC_NORMAL);
Runtime.dynCall("vii", cb, [id,value]);
var lengthBytes = lengthBytesUTF8(input.value) + 1;
var stringOnHeap = _malloc(lengthBytes);
stringToUTF8(input.value, stringOnHeap, lengthBytes);
{{{ makeDynCall('vii', 'cb') }}}(id, stringOnHeap);
};
},
WebGLInputOnEditEnd:function(id, cb){
var input = instances[id];
input.onchange = function () {
var intArray = intArrayFromString(input.value);
var value = (allocate.length <= 2) ? allocate(intArray, ALLOC_NORMAL):allocate(intArray, 'i8', ALLOC_NORMAL);
Runtime.dynCall("vii", cb, [id,value]);
var lengthBytes = lengthBytesUTF8(input.value) + 1;
var stringOnHeap = _malloc(lengthBytes);
stringToUTF8(input.value, stringOnHeap, lengthBytes);
{{{ makeDynCall('vii', 'cb') }}}(id, stringOnHeap);
};
},
WebGLInputSelectionStart:function(id){
Expand Down
10 changes: 4 additions & 6 deletions Runtime/Plugins/WebGLSupport/WebGLWindow/WebGLWindow.jslib
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
var WebGLWindow = {
WebGLWindowInit : function() {
// Remove the `Runtime` object from "v1.37.27: 12/24/2017"
// if Runtime not defined. create and add functon!!
if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
// Initialization if needed
},
WebGLWindowOnFocus: function (cb) {
window.addEventListener('focus', function () {
Runtime.dynCall("v", cb, []);
{{{ makeDynCall('v', 'cb') }}}();
});
},
WebGLWindowOnBlur: function (cb) {
window.addEventListener('blur', function () {
Runtime.dynCall("v", cb, []);
{{{ makeDynCall('v', 'cb') }}}();
});
},
WebGLWindowOnResize: function(cb) {
window.addEventListener('resize', function () {
Runtime.dynCall("v", cb, []);
{{{ makeDynCall('v', 'cb') }}}();
});
},
WebGLWindowInjectFullscreen : function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class SolanaWalletAdapterWebGL: WalletBase
private static TaskCompletionSource<Transaction> _signedTransactionTaskCompletionSource;
private static TaskCompletionSource<Transaction[]> _signedAllTransactionsTaskCompletionSource;
private static TaskCompletionSource<byte[]> _signedMessageTaskCompletionSource;
private static Transaction[] _currentTransactions;
private static Account _account;
public static GameObject WalletAdapterUI { get; private set; }

Expand Down Expand Up @@ -176,7 +175,6 @@ protected override Task<Account> _CreateAccount(string mnemonic = null, string p
protected override Task<Transaction[]> _SignAllTransactions(Transaction[] transactions)
{
_signedAllTransactionsTaskCompletionSource = new TaskCompletionSource<Transaction[]>();
_currentTransactions = transactions;
string[] base64Transactions = new string[transactions.Length];
for (int i = 0; i < transactions.Length; i++)
{
Expand Down Expand Up @@ -224,28 +222,26 @@ public static void OnTransactionSigned(string transaction)
}

/// <summary>
/// Called from javascript when the wallet signed all transactions and return the signature
/// that we then need to put into the transaction before we send it out.
/// Called from javascript when the wallet signs all transactions and returns the signed transactions,
/// which we then need to send out.
/// </summary>
[MonoPInvokeCallback(typeof(Action<string>))]
public static void OnAllTransactionsSigned(string signatures)
public static void OnAllTransactionsSigned(string transactions)
{
if (signatures == null)
if (transactions == null)
{
_signedAllTransactionsTaskCompletionSource.TrySetException(new Exception("Transactions signing cancelled"));
_signedAllTransactionsTaskCompletionSource.TrySetResult(null);
return;
}
string[] signaturesList = signatures.Split(',');
for (int i = 0; i < signaturesList.Length; i++)
string[] transactionsList = transactions.Split(',');
var txList = new Transaction[transactionsList.Length];

for (int i = 0; i < transactionsList.Length; i++)
{
_currentTransactions[i].Signatures.Add(new SignaturePubKeyPair()
{
PublicKey = _account.PublicKey,
Signature = Convert.FromBase64String(signaturesList[i])
});
txList[i] = Transaction.Deserialize(transactionsList[i]);
}
_signedAllTransactionsTaskCompletionSource.SetResult(_currentTransactions);
_signedAllTransactionsTaskCompletionSource.SetResult(txList);
}


Expand Down

0 comments on commit 90edc61

Please sign in to comment.