diff --git a/Packages/io.chainsafe.web3-unity/Editor/WebGLThreadPatcherInstaller.cs b/Packages/io.chainsafe.web3-unity/Editor/WebGLThreadPatcherInstaller.cs new file mode 100644 index 000000000..8463e17de --- /dev/null +++ b/Packages/io.chainsafe.web3-unity/Editor/WebGLThreadPatcherInstaller.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Newtonsoft.Json; +using UnityEditor; +using UnityEngine; + +[InitializeOnLoad] +public static class WebGLThreadPatcherInstaller +{ + private const string WebGLThreadPatchedInstalled = "IsWebGLThreadPatcherInstalled"; + + private const string ManifestPath = "Packages/manifest.json"; + + private const string WebGLThreadPatcherPackageName = "com.tools.webglthreadingpatcher"; + + private const string WebGLThreadPatcherPackageLink = "https://github.com/VolodymyrBS/WebGLThreadingPatcher.git"; + + static WebGLThreadPatcherInstaller() + { +#if UNITY_WEBGL + if (SessionState.GetBool(WebGLThreadPatchedInstalled, false)) + { + return; + } + + TryInstallThreadPatcher(); + + SessionState.SetBool(WebGLThreadPatchedInstalled, true); +#else + // reset. + SessionState.SetBool(WebGLThreadPatchedInstalled, false); +#endif + } + + [MenuItem("Window/ChainSafe SDK/Install WebGLThreadingPatcher")] + public static void TryInstallThreadPatcher() + { + Manifest manifest = JsonConvert.DeserializeObject(File.ReadAllText(ManifestPath)); + + // check if ThreadPatcher is already installed. + if (manifest.Dependencies.ContainsKey(WebGLThreadPatcherPackageName)) + { + return; + } + + if (EditorUtility.DisplayDialog("Web3.Unity", "For Web3.Unity to fully work on a WebGL build you need to install a WebGLThreadingPatcher, this will make sure async operations can run to completion.\nInstall WebGLThreadingPatcher?", "Yes", "No")) + { + // Add the package as a dependency. + manifest.Dependencies.Add(WebGLThreadPatcherPackageName, WebGLThreadPatcherPackageLink); + + File.WriteAllText(ManifestPath, JsonConvert.SerializeObject(manifest, Formatting.Indented)); + } + } + + private struct Manifest + { + [JsonProperty("dependencies")] + public Dictionary Dependencies { get; private set; } + } +} \ No newline at end of file diff --git a/Packages/io.chainsafe.web3-unity/Editor/WebGLThreadPatcherInstaller.cs.meta b/Packages/io.chainsafe.web3-unity/Editor/WebGLThreadPatcherInstaller.cs.meta new file mode 100644 index 000000000..c20e586af --- /dev/null +++ b/Packages/io.chainsafe.web3-unity/Editor/WebGLThreadPatcherInstaller.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3324985ba731c20459fcc27adef8426f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/UnitySampleProject/Packages/manifest.json b/src/UnitySampleProject/Packages/manifest.json index 38ca12ddc..377e19650 100644 --- a/src/UnitySampleProject/Packages/manifest.json +++ b/src/UnitySampleProject/Packages/manifest.json @@ -1,5 +1,6 @@ { "dependencies": { + "com.tools.webglthreadingpatcher": "https://github.com/VolodymyrBS/WebGLThreadingPatcher.git", "com.unity.collab-proxy": "2.0.7", "com.unity.feature.2d": "1.0.0", "com.unity.ide.rider": "3.0.25",