|
|
|
@ -1,48 +1,73 @@
|
|
|
|
|
using System;
|
|
|
|
|
using Firebase.Auth;
|
|
|
|
|
using Google;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Firebase.Extensions;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using Google;
|
|
|
|
|
using PlayFab;
|
|
|
|
|
using PlayFab.ClientModels;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using OneSignalSDK;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using OneSignalSDK.Debug.Models;
|
|
|
|
|
|
|
|
|
|
public class GoogleSignInHandler : MonoBehaviour
|
|
|
|
|
public class GoogleSignInManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private FirebaseAuth auth;
|
|
|
|
|
private GoogleSignInConfiguration configuration;
|
|
|
|
|
[SerializeField] private Button googleSignInButton;
|
|
|
|
|
[SerializeField] private Button guestLoginButton;
|
|
|
|
|
[SerializeField] private Bootstrapper bootstrapper;
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
googleSignInButton.onClick.AddListener(SignInWithGoogle);
|
|
|
|
|
guestLoginButton.onClick.AddListener(GuestLogin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
googleSignInButton.onClick.RemoveListener(SignInWithGoogle);
|
|
|
|
|
guestLoginButton.onClick.RemoveListener(GuestLogin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
public Bootstrapper bootstrapper;
|
|
|
|
|
public Button googleSignInButton;
|
|
|
|
|
public Button guestLoginButton;
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
PlayFabSettings.staticSettings.TitleId = GameConstants.PlayfabTitleId;
|
|
|
|
|
auth = FirebaseAuth.DefaultInstance;
|
|
|
|
|
configuration = new GoogleSignInConfiguration
|
|
|
|
|
{
|
|
|
|
|
WebClientId = GameConstants.WebClientID,
|
|
|
|
|
RequestEmail = true,
|
|
|
|
|
RequestAuthCode = true,
|
|
|
|
|
RequestIdToken = true
|
|
|
|
|
RequestAuthCode = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GoogleSignIn.Configuration = configuration;
|
|
|
|
|
GoogleSignIn.DefaultInstance.EnableDebugLogging(true);
|
|
|
|
|
Application.quitting += SyncPlayerPrefsToPlayFabOnQuit;
|
|
|
|
|
string ons_id = "";
|
|
|
|
|
#if UNITY_ANDROID
|
|
|
|
|
ons_id = GameConstants.OSN_Android;
|
|
|
|
|
#elif UNITY_IOS
|
|
|
|
|
ons_id = GameConstants.OSN_iOS;
|
|
|
|
|
#endif
|
|
|
|
|
Debug.Log($"Initializing with appId <b>{ons_id}</b>");
|
|
|
|
|
OneSignal.Debug.LogLevel = LogLevel.Debug;
|
|
|
|
|
OneSignal.Initialize(ons_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
googleSignInButton.onClick.RemoveAllListeners();
|
|
|
|
|
googleSignInButton.onClick.AddListener(SignInWithGoogle);
|
|
|
|
|
|
|
|
|
|
guestLoginButton.onClick.RemoveAllListeners();
|
|
|
|
|
guestLoginButton.onClick.AddListener(GuestLogin);
|
|
|
|
|
|
|
|
|
|
if (PlayerPrefs.HasKey("PlayFabID") && !PlayerPrefs.HasKey("GuestMode"))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("User previously signed in with Google. Attempting silent login...");
|
|
|
|
|
googleSignInButton.gameObject.SetActive(false);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(false);
|
|
|
|
|
SignInSilently();
|
|
|
|
|
}
|
|
|
|
|
else if (PlayerPrefs.HasKey("GuestMode"))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Guest mode previously selected. Letting user choose again.");
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("No login info found. Showing login options.");
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void GuestLogin()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Starting game in Guest Mode...");
|
|
|
|
@ -64,85 +89,240 @@ public class GoogleSignInHandler : MonoBehaviour
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PlayFabClientAPI.LoginWithCustomID(request, result =>
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("✅ Guest Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
|
SafePlayerPrefs.SetString("PlayFabID", result.PlayFabId);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
if (result.NewlyCreated)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("✅ Guest Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
|
SafePlayerPrefs.SetString("PlayFabID", result.PlayFabId);
|
|
|
|
|
SafePlayerPrefs.SetInt("NeedsDisplayNameUpload", 1);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.NewlyCreated)
|
|
|
|
|
{
|
|
|
|
|
SafePlayerPrefs.SetInt("NeedsDisplayNameUpload", 1);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
|
},
|
|
|
|
|
error =>
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("❌ Guest PlayFab login failed: " + error.GenerateErrorReport());
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
});
|
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
|
},
|
|
|
|
|
error =>
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("❌ Guest PlayFab login failed: " + error.GenerateErrorReport());
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SignInWithGoogle()
|
|
|
|
|
|
|
|
|
|
public void SignInWithGoogle()
|
|
|
|
|
{
|
|
|
|
|
if (GoogleSignIn.Configuration == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("Google Sign-In Configuration is not set.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
PlayerPrefs.DeleteKey("GuestMode");
|
|
|
|
|
PlayerPrefs.DeleteAll();
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
googleSignInButton.interactable = false;
|
|
|
|
|
guestLoginButton.interactable = false;
|
|
|
|
|
|
|
|
|
|
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnGoogleSignIn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnGoogleSignIn(Task<GoogleSignInUser> task)
|
|
|
|
|
{
|
|
|
|
|
if (task.IsFaulted || task.IsCanceled || task.Result == null)
|
|
|
|
|
if (task.IsFaulted || task.IsCanceled)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Google Sign-In failed.");
|
|
|
|
|
Debug.LogError("Google Sign-In failed: " + task.Exception);
|
|
|
|
|
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
googleSignInButton.interactable = true;
|
|
|
|
|
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.interactable = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PlayFab uses ServerAuthCode
|
|
|
|
|
GoogleSignInUser user = task.Result;
|
|
|
|
|
string authCode = user.AuthCode;
|
|
|
|
|
SafePlayerPrefs.SetString("GoogleAuthCode", authCode);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
LoginToPlayFab(authCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SignInSilently()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Attempting Google Silent Sign-In...");
|
|
|
|
|
|
|
|
|
|
GoogleSignIn.DefaultInstance.SignInSilently().ContinueWith(task =>
|
|
|
|
|
{
|
|
|
|
|
if (task.IsFaulted || task.IsCanceled)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("Silent Sign-In failed. Showing login buttons.");
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
googleSignInButton.interactable = true;
|
|
|
|
|
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.interactable = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GoogleSignInUser user = task.Result;
|
|
|
|
|
string authCode = user.AuthCode;
|
|
|
|
|
SafePlayerPrefs.SetString("GoogleAuthCode", authCode);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
LoginToPlayFab(authCode);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoginToPlayFab(string authCode)
|
|
|
|
|
{
|
|
|
|
|
PlayFabSettings.staticSettings.TitleId = GameConstants.PlayfabTitleId;
|
|
|
|
|
var request = new LoginWithGoogleAccountRequest
|
|
|
|
|
{
|
|
|
|
|
TitleId = PlayFabSettings.staticSettings.TitleId,
|
|
|
|
|
ServerAuthCode = task.Result.AuthCode,
|
|
|
|
|
TitleId = GameConstants.PlayfabTitleId,
|
|
|
|
|
ServerAuthCode = authCode,
|
|
|
|
|
CreateAccount = true
|
|
|
|
|
};
|
|
|
|
|
Debug.Log($"LoginWithGoogleAccount Title {request.TitleId} AuthCode {request.ServerAuthCode}");
|
|
|
|
|
|
|
|
|
|
PlayFabClientAPI.LoginWithGoogleAccount(request, OnPlayFabLoginSuccess, OnPlayFabLoginFailure);
|
|
|
|
|
// // Firebase uses ID token
|
|
|
|
|
// var firebaseCredential = GoogleAuthProvider.GetCredential(task.Result.IdToken, null);
|
|
|
|
|
// FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(firebaseCredential).ContinueWith(authTask =>
|
|
|
|
|
// {
|
|
|
|
|
// if (authTask.IsCompleted && !authTask.IsFaulted)
|
|
|
|
|
// {
|
|
|
|
|
// Debug.Log("Firebase sign-in successful");
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// Debug.LogError("Firebase sign-in failed: " + authTask.Exception);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayFabLoginSuccess(LoginResult result)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("PlayFab Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
|
Debug.Log("✅ PlayFab Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
|
|
|
|
|
|
SafePlayerPrefs.SetString("PlayFabID", result.PlayFabId);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
|
|
|
|
|
|
OneSignal.Login(result.PlayFabId);
|
|
|
|
|
StartCoroutine(SaveOneSignalPlayerIdToPlayFab());
|
|
|
|
|
|
|
|
|
|
LoadPlayerPrefsFromPlayFab(() =>
|
|
|
|
|
{
|
|
|
|
|
googleSignInButton.gameObject.SetActive(false);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(false);
|
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator SaveOneSignalPlayerIdToPlayFab()
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(1f);
|
|
|
|
|
|
|
|
|
|
string playerId = OneSignal.User.PushSubscription.Id;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(playerId))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("✅ Got OneSignal Player ID: " + playerId);
|
|
|
|
|
|
|
|
|
|
var request = new UpdateUserDataRequest
|
|
|
|
|
{
|
|
|
|
|
Data = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "OneSignalPlayerId", playerId }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PlayFabClientAPI.UpdateUserData(request,
|
|
|
|
|
result => Debug.Log("✅ Saved OneSignal Player ID to PlayFab"),
|
|
|
|
|
error => Debug.LogError("❌ Failed to save OneSignal Player ID: " + error.GenerateErrorReport()));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("⚠️ OneSignal Player ID not available yet.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayFabLoginFailure(PlayFabError error)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("PlayFab Login Failed: " + error.GenerateErrorReport());
|
|
|
|
|
Debug.LogError("❌ PlayFab Login Failed: " + error.GenerateErrorReport());
|
|
|
|
|
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
googleSignInButton.interactable = true;
|
|
|
|
|
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.interactable = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadPlayerPrefsFromPlayFab(Action onComplete)
|
|
|
|
|
{
|
|
|
|
|
PlayFabClientAPI.GetUserData(new GetUserDataRequest(), result =>
|
|
|
|
|
{
|
|
|
|
|
if (result.Data != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var entry in result.Data)
|
|
|
|
|
{
|
|
|
|
|
string key = entry.Key;
|
|
|
|
|
string rawValue = entry.Value.Value;
|
|
|
|
|
|
|
|
|
|
if (rawValue.StartsWith("int:") && int.TryParse(rawValue.Substring(4), out int i))
|
|
|
|
|
SafePlayerPrefs.SetInt(key, i);
|
|
|
|
|
else if (rawValue.StartsWith("float:") && float.TryParse(rawValue.Substring(6), out float f))
|
|
|
|
|
SafePlayerPrefs.SetFloat(key, f);
|
|
|
|
|
else if (rawValue.StartsWith("string:"))
|
|
|
|
|
SafePlayerPrefs.SetString(key, rawValue.Substring(7));
|
|
|
|
|
else
|
|
|
|
|
SafePlayerPrefs.SetString(key, rawValue);
|
|
|
|
|
|
|
|
|
|
PlayerPrefsKeys.RegisterKey(key);
|
|
|
|
|
}
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onComplete?.Invoke();
|
|
|
|
|
},
|
|
|
|
|
error =>
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("❌ Failed to load SafePlayerPrefs from PlayFab: " + error.GenerateErrorReport());
|
|
|
|
|
onComplete?.Invoke();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SyncPlayerPrefsToPlayFabOnQuit()
|
|
|
|
|
{
|
|
|
|
|
if (PlayerPrefs.GetInt("GuestMode", 0) == 1) return;
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> allPrefs = new Dictionary<string, string>();
|
|
|
|
|
foreach (var key in PlayerPrefsKeys.GetAllKeys())
|
|
|
|
|
{
|
|
|
|
|
allPrefs[key] = PlayerPrefs.GetString(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var request = new UpdateUserDataRequest
|
|
|
|
|
{
|
|
|
|
|
Data = allPrefs
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (PlayFabClientAPI.IsClientLoggedIn())
|
|
|
|
|
{
|
|
|
|
|
PlayFabClientAPI.UpdateUserData(request,
|
|
|
|
|
result => Debug.Log("✅ Synced SafePlayerPrefs to PlayFab on quit."),
|
|
|
|
|
error => Debug.LogError("❌ Failed to sync SafePlayerPrefs: " + error.GenerateErrorReport()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SignOut()
|
|
|
|
|
{
|
|
|
|
|
// 1) Clear PlayFab session in memory
|
|
|
|
|
PlayFabClientAPI.ForgetAllCredentials();
|
|
|
|
|
|
|
|
|
|
// 2) Delete every PlayerPref we’ve registered
|
|
|
|
|
foreach (var key in PlayerPrefsKeys.GetAllKeys())
|
|
|
|
|
PlayerPrefs.DeleteKey(key);
|
|
|
|
|
// Also remove any extra cruft
|
|
|
|
|
PlayerPrefs.DeleteKey("GuestMode");
|
|
|
|
|
PlayerPrefs.DeleteKey("GuestCustomID");
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
// 3) Sign out of Google
|
|
|
|
|
GoogleSignIn.DefaultInstance.SignOut();
|
|
|
|
|
|
|
|
|
|
// 4) Unlink OneSignal from this user
|
|
|
|
|
OneSignal.Logout();
|
|
|
|
|
|
|
|
|
|
// 5) Bring back your login buttons
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
|
googleSignInButton.interactable = true;
|
|
|
|
|
guestLoginButton.interactable = true;
|
|
|
|
|
|
|
|
|
|
Debug.Log("✅ User fully signed out.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|