Skip to content

Commit

Permalink
Cross platform copy utility
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Apr 5, 2024
1 parent 4400c69 commit 76fb878
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
20 changes: 20 additions & 0 deletions Assets/Thirdweb/Core/Plugin/thirdweb.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,26 @@ var plugin = {
dynCall_viii(cb, idPtr, null, buffer);
});
},
ThirdwebCopyBuffer: async function (taskId, text, cb) {
// convert taskId from pointer to str and allocate it to keep in memory
var id = UTF8ToString(taskId);
var idSize = lengthBytesUTF8(id) + 1;
var idPtr = _malloc(idSize);
stringToUTF8(id, idPtr, idSize);
// execute bridge call
window.bridge
.copyBuffer(UTF8ToString(text))
.then(() => {
dynCall_viii(cb, idPtr, idPtr, null);
})
.catch((err) => {
var msg = err.message;
var bufferSize = lengthBytesUTF8(msg) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(msg, buffer, bufferSize);
dynCall_viii(cb, idPtr, null, buffer);
});
},
};

mergeInto(LibraryManager.library, plugin);
18 changes: 18 additions & 0 deletions Assets/Thirdweb/Core/Scripts/Bridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ public static async Task<string> ResolveAddressFromENS(string ens)
return JsonConvert.DeserializeObject<Result<string>>(result).result;
}

public static async Task CopyBuffer(string text)
{
if (!Utils.IsWebGLBuild())
{
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
return;
}
string taskId = Guid.NewGuid().ToString();
var task = new TaskCompletionSource<string>();
taskMap[taskId] = task;
#if UNITY_WEBGL
ThirdwebCopyBuffer(taskId, text, jsCallback);
#endif
await task.Task;
}

#if UNITY_WEBGL
[DllImport("__Internal")]
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
Expand Down Expand Up @@ -495,6 +511,8 @@ public static async Task<string> ResolveAddressFromENS(string ens)
private static extern string ThirdwebResolveENSFromAddress(string taskId, string address, Action<string, string, string> cb);
[DllImport("__Internal")]
private static extern string ThirdwebResolveAddressFromENS(string taskId, string ens, Action<string, string, string> cb);
[DllImport("__Internal")]
private static extern string ThirdwebCopyBuffer(string taskId, string text, Action<string, string, string> cb);
#endif
}
}
17 changes: 17 additions & 0 deletions Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -739,5 +739,22 @@ public static string GenerateRandomString(int v)
var result = new string(Enumerable.Repeat(chars, v).Select(s => s[random.Next(s.Length)]).ToArray());
return result;
}

public static async Task<bool> CopyToClipboard(string text)
{
try
{
if (IsWebGLBuild())
await Bridge.CopyBuffer(text);
else
GUIUtility.systemCopyBuffer = text;
return true;
}
catch (Exception e)
{
ThirdwebDebug.LogWarning($"Failed to copy to clipboard: {e}");
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public void ConnectEmail()
email: emailInput.text,
authOptions: new AuthOptions(AuthProvider.EmailOTP)
);
Connect(wc);
}
else
{
Expand Down Expand Up @@ -272,13 +271,13 @@ public async void ExportWallet()
{
ThirdwebDebug.Log("Exporting wallet...");
string json = await ThirdwebManager.Instance.SDK.Wallet.Export(_password);
GUIUtility.systemCopyBuffer = json;
await Utils.CopyToClipboard(json);
ThirdwebDebug.Log($"Copied wallet to clipboard: {json}");
}

public void CopyAddress()
public async void CopyAddress()
{
GUIUtility.systemCopyBuffer = _address;
await Utils.CopyToClipboard(_address);
ThirdwebDebug.Log($"Copied address to clipboard: {_address}");
}

Expand Down
7 changes: 6 additions & 1 deletion Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -446041,7 +446041,7 @@ Code: ${s}`;
}
(globalThis.X_SDK_NAME = "UnitySDK_WebGL"),
(globalThis.X_SDK_PLATFORM = "unity"),
(globalThis.X_SDK_VERSION = "4.9.2"),
(globalThis.X_SDK_VERSION = "4.10.0"),
(globalThis.X_SDK_OS = s?.os ?? "unknown");
}
this.initializedChain = e;
Expand Down Expand Up @@ -446528,6 +446528,11 @@ Code: ${s}`;
}).resolveName(e);
return JSON.stringify({ result: r });
}
async copyBuffer(e) {
navigator.clipboard.writeText(e).catch(function (t) {
console.error("Could not copy text: ", t);
});
}
openPopupWindow() {
let e = window.open("", void 0, "width=350, height=500");
return (
Expand Down

0 comments on commit 76fb878

Please sign in to comment.