RandomGFGoStop/Assets/Scripts/My/GPGSInfo.cs

129 lines
3.8 KiB
C#
Raw Normal View History

2025-09-07 01:22:33 +00:00
using CodeJay.Classes;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using Newtonsoft.Json;
using System;
using UnityEngine;
public class GPGSInfo : MonoBehaviour
{
public static GPGSInfo Ins;
private void Awake() { Ins = this; }
private ISavedGameClient savedGameClient;
string str_savedata;
Action<int> act_save, act_load;
string gamedataname = "mygamedata";
void Run_act(int status)
{
if (act_save != null) act_save(status);
act_save = null;
if (act_load != null) act_load(status);
act_load = null;
}
internal void ProcessAuthentication(SignInStatus status)
{
if (status == SignInStatus.Success)
{
// Continue with Play Games Services
savedGameClient = PlayGamesPlatform.Instance.SavedGame;
if (act_save != null) SaveGameAfterLogin();
else if (act_load != null) LoadGameAfterLogin();
}
else
{
// Disable your integration with Play Games Services or show a login button
// to ask users to sign-in. Clicking it should call
// PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication).
Run_act(-1);
}
}
public void SaveGame(string data, Action<int> act)
{
str_savedata = data;
act_save = act;
act_load = null;
PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication);
}
void SaveGameAfterLogin()
{
if (savedGameClient != null)
{
byte[] byteData = System.Text.Encoding.UTF8.GetBytes(str_savedata);
savedGameClient.OpenWithAutomaticConflictResolution(
gamedataname,
DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime,
(status, metadata) => OnSavedGameOpenedForSave(status, metadata, byteData));
}
else
Run_act(-1);
}
private void OnSavedGameOpenedForSave(SavedGameRequestStatus status, ISavedGameMetadata game, byte[] data)
{
if (status == SavedGameRequestStatus.Success)
savedGameClient.CommitUpdate(game, new SavedGameMetadataUpdate.Builder().Build(), data, OnGameSaved);
else
Run_act(-1);
}
private void OnGameSaved(SavedGameRequestStatus status, ISavedGameMetadata game)
{
Run_act(status == SavedGameRequestStatus.Success ? 0 : -1);
}
public void LoadGame(Action<int> act)
{
act_save = null;
act_load = act;
PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication);
}
void LoadGameAfterLogin()
{
if (savedGameClient != null)
{
savedGameClient.OpenWithAutomaticConflictResolution(
gamedataname,
DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime,
(status, metadata) => OnSavedGameOpenedForLoad(status, metadata));
}
else
Run_act(-1);
}
private void OnSavedGameOpenedForLoad(SavedGameRequestStatus status, ISavedGameMetadata game)
{
if (status == SavedGameRequestStatus.Success)
savedGameClient.ReadBinaryData(game, OnGameLoaded);
else
Run_act(-1);
}
private void OnGameLoaded(SavedGameRequestStatus status, byte[] data)
{
if (status == SavedGameRequestStatus.Success)
{
if (data != null && data.Length > 0)
{
string loadedData = System.Text.Encoding.UTF8.GetString(data);
GameManager.DB.Set_SaveData(JsonConvert.DeserializeObject<SaveData>(loadedData));
Run_act(0);
}
else
Run_act(-2);
}
else
Run_act(-1);
}
}