Skip to content

Commit

Permalink
Do not preserve previous sessions, fix multiple callbacks OnSend
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Apr 7, 2023
1 parent 7370866 commit 77c1b52
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 77 deletions.
148 changes: 72 additions & 76 deletions Assets/Thirdweb/Plugins/WalletConnectSharp.Unity/WalletConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class WalletConnect : BindableMonoBehavior

[BindComponent]
private NativeWebSocketTransport _transport;
private bool initialized;
private bool eventsSetup;

public static WalletConnect Instance;

Expand All @@ -63,6 +65,10 @@ protected override void Awake()
return;
}

ClearSession();
if (Session != null)
Session = null;

base.Awake();
}

Expand Down Expand Up @@ -103,90 +109,85 @@ public void SetMode(bool active)

public async Task<WCSessionData> Connect(int chainId)
{
while (true)
SavedSession savedSession = null;
if (PlayerPrefs.HasKey(SessionKey))
{
try
{
SavedSession savedSession = null;
if (PlayerPrefs.HasKey(SessionKey))
{
var json = PlayerPrefs.GetString(SessionKey);
savedSession = JsonConvert.DeserializeObject<SavedSession>(json);
}
var json = PlayerPrefs.GetString(SessionKey);
savedSession = JsonConvert.DeserializeObject<SavedSession>(json);
}

if (Session != null)
if (Session != null)
{
if (savedSession != null)
{
if (Session.KeyData != savedSession.Key)
{
if (savedSession != null)
{
if (Session.KeyData != savedSession.Key)
{
if (Session.SessionConnected)
await Session.Disconnect();
else if (Session.TransportConnected)
await Session.Transport.Close();
}
else if (!Session.Connected && !Session.Connecting)
{
StartCoroutine(SetupDefaultWallet());
SetupEvents();
return await CompleteConnect();
}
else
{
Debug.LogWarning("Already Connected");
return null;
}
}
else if (Session.SessionConnected)
{
if (Session.SessionConnected)
await Session.Disconnect();
}
else if (Session.TransportConnected)
{
await Session.Transport.Close();
}
// else if (Session.Connecting)
// {
// Debug.LogWarning("Session connecting...");
// return null;
// }
}

if (savedSession != null)
else if (!Session.Connected && !Session.Connecting)
{
Session = new WalletConnectUnitySession(savedSession, this, _transport);
StartCoroutine(SetupDefaultWallet());
SetupEvents();
return await CompleteConnect();
}
else
{
Session = new WalletConnectUnitySession(
new ClientMeta()
{
Name = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appName,
Description = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appDescription,
URL = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appUrl,
Icons = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appIcons,
},
this,
null,
_transport,
null,
chainId
);
Debug.LogWarning("Already Connected");
return null;
}

StartCoroutine(SetupDefaultWallet());
SetupEvents();
return await CompleteConnect();
}
catch (System.Exception e)
else if (Session.SessionConnected)
{
await Session.Disconnect();
}
else if (Session.TransportConnected)
{
Debug.LogWarning("WalletConnect.Connect Error, Regeneratinge | " + e.Message);
await Session.Transport.Close();
}
// else if (Session.Connecting)
// {
// Debug.LogWarning("Session connecting...");
// return null;
// }
}

if (savedSession != null)
{
Session = new WalletConnectUnitySession(savedSession, this, _transport);
}
else
{
Session = new WalletConnectUnitySession(
new ClientMeta()
{
Name = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appName,
Description = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appDescription,
URL = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appUrl,
Icons = ThirdwebManager.Instance.SDK.nativeSession.options.wallet?.appIcons,
},
this,
null,
_transport,
null,
chainId
);
}

StartCoroutine(SetupDefaultWallet());
SetupEvents();
return await CompleteConnect();
}

private void SetupEvents()
{
if (eventsSetup)
return;

eventsSetup = true;

Session.OnSessionConnect += (sender, session) =>
{
Debug.LogWarning("[WalletConnect] Session Connected");
Expand All @@ -212,8 +213,7 @@ private async Task<WCSessionData> CompleteConnect()
{
try
{
var session = await Session.SourceConnectSession();
return session;
return await Session.SourceConnectSession();
}
catch (IOException e)
{
Expand Down Expand Up @@ -309,18 +309,14 @@ private IEnumerator DownloadImagesFor(string id, string[] sizes = null)
}
}

private void OnDestroy()
{
SaveSession();
}

private void OnApplicationQuit()
{
SaveSession();
}

private async void OnApplicationPause(bool pauseStatus)
{
if (!initialized)
{
initialized = true;
return;
}

if (pauseStatus)
SaveSession();
else if (PlayerPrefs.HasKey(SessionKey))
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 2.0.2
bundleVersion: 2.0.3
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down

0 comments on commit 77c1b52

Please sign in to comment.