Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions Assets/Talo Game Services/Talo/Runtime/APIs/GameConfigAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,49 @@ public GameConfigAPI() : base("v1/game-config") {
if (response.GetResponseType() == "v1.live-config.updated")
{
var data = response.GetData<GameConfigResponse>();
OnLiveConfigUpdated?.Invoke(new LiveConfig(data.config));
SetLiveConfig(new LiveConfig(data.config), true);
}
};
}

public async Task<LiveConfig> Get()
{
if (Talo.IsOffline())
{
return GetOfflineConfig();
}

var uri = new Uri(baseUrl);
var json = await Call(uri, "GET");

var res = JsonUtility.FromJson<GameConfigResponse>(json);
Talo.LiveConfig = new LiveConfig(res.config);
OnLiveConfigLoaded?.Invoke(Talo.LiveConfig);

SetLiveConfig(new LiveConfig(res.config));
return Talo.LiveConfig;
}

public LiveConfig GetOfflineConfig()
{
var offlineConfig = LiveConfig.GetOfflineConfig();
if (offlineConfig != null)
{
SetLiveConfig(offlineConfig);
}
return offlineConfig;
}

private void SetLiveConfig(LiveConfig liveConfig, bool isUpdate = false)
{
Talo.LiveConfig = liveConfig;
liveConfig.WriteOfflineConfig();

if (isUpdate)
{
OnLiveConfigUpdated?.Invoke(liveConfig);
}
else
{
OnLiveConfigLoaded?.Invoke(liveConfig);
}
}
}
}
10 changes: 5 additions & 5 deletions Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PlayersAPI : BaseAPI
public event Action OnIdentificationStarted;
public event Action OnIdentificationFailed;

private readonly string _offlineDataPath = Application.persistentDataPath + "/ta.bin";
private readonly string offlineDataPath = Application.persistentDataPath + "/ta.bin";

public PlayersAPI() : base("v1/players") { }

Expand Down Expand Up @@ -123,7 +123,7 @@ private async Task<Player> IdentifyOffline(string service, string identifier)
{
try
{
File.Delete(_offlineDataPath);
File.Delete(offlineDataPath);
}
finally
{
Expand All @@ -137,13 +137,13 @@ private void WriteOfflineAlias(PlayerAlias alias)
{
if (!Talo.Settings.cachePlayerOnIdentify) return;
var content = JsonUtility.ToJson(alias);
Talo.Crypto.WriteFileContent(_offlineDataPath, content);
Talo.Crypto.WriteFileContent(offlineDataPath, content);
}

private PlayerAlias GetOfflineAlias()
{
if (!Talo.Settings.cachePlayerOnIdentify || !File.Exists(_offlineDataPath)) return null;
return JsonUtility.FromJson<PlayerAlias>(Talo.Crypto.ReadFileContent(_offlineDataPath));
if (!Talo.Settings.cachePlayerOnIdentify || !File.Exists(offlineDataPath)) return null;
return JsonUtility.FromJson<PlayerAlias>(Talo.Crypto.ReadFileContent(offlineDataPath));
}

public async Task<PlayersSearchResponse> Search(string query)
Expand Down
59 changes: 57 additions & 2 deletions Assets/Talo Game Services/Talo/Runtime/Entities/LiveConfig.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace TaloGameServices
{
public class LiveConfig
{
private Prop[] props;
private static readonly string offlineDataPath = Application.persistentDataPath + "/tlc.bin";
private readonly Prop[] props;

public LiveConfig(Prop[] props)
{
this.props = props;
}

public T GetProp<T>(string key, T fallback = default(T))
public T GetProp<T>(string key, T fallback = default)
{
try
{
Expand All @@ -24,5 +27,57 @@ public LiveConfig(Prop[] props)
return fallback;
}
}

public void WriteOfflineConfig()
{
try
{
var json = JsonUtility.ToJson(new GameConfigResponse { config = props });
Talo.Crypto.WriteFileContent(offlineDataPath, json);
}
catch (Exception e)
{
Debug.LogWarning($"Failed to write offline config: {e.Message}");
}
}

public static LiveConfig GetOfflineConfig()
{
try
{
if (!File.Exists(offlineDataPath))
{
return null;
}

var json = Talo.Crypto.ReadFileContent(offlineDataPath);
var response = JsonUtility.FromJson<GameConfigResponse>(json);
return new LiveConfig(response.config);
}
catch (Exception e)
{
Debug.LogWarning($"Failed to read offline config: {e.Message}");
return null;
}
}

public static long GetOfflineConfigLastModified()
{
try
{
if (!File.Exists(offlineDataPath))
{
return 0;
}

var fileInfo = new FileInfo(offlineDataPath);
return new DateTimeOffset(fileInfo.LastWriteTimeUtc).ToUnixTimeSeconds();
}
catch (Exception e)
{
Debug.LogWarning($"Failed to get offline config last modified time: {e.Message}");
return 0;
}
}
}
}