SDK_UnityMoney/Assets/Script/SDKManager/FirebaseManager/FireBaseRemoteConfigManager.cs

239 lines
9.3 KiB
C#
Raw Normal View History

2025-08-30 08:47:09 +00:00
using System;
2025-08-31 14:59:14 +00:00
using System.Globalization;
2025-08-31 08:42:48 +00:00
using Firebase.Extensions;
2025-08-30 08:47:09 +00:00
using Firebase.RemoteConfig;
using UnityEngine;
2025-09-01 10:32:50 +00:00
namespace WZ
2025-08-30 08:47:09 +00:00
{
2025-09-02 09:14:49 +00:00
public class FireBaseRemoteConfigManager : D_MonoSingleton<FireBaseRemoteConfigManager>
2025-08-31 08:42:48 +00:00
{
2025-09-02 10:20:43 +00:00
public bool IsInitialized { get; private set; } = false;
2025-09-01 10:32:50 +00:00
public void FetchRemoteConfig()
2025-08-31 08:42:48 +00:00
{
2025-09-02 10:08:26 +00:00
// Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
// {
// if (task.Result == Firebase.DependencyStatus.Available)
// {
2025-09-02 10:20:43 +00:00
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.FetchAsync(TimeSpan.Zero).ContinueWithOnMainThread(task =>
{
FirebaseRemoteConfig.DefaultInstance.ActivateAsync().ContinueWithOnMainThread(task =>
{
IsInitialized = true;
AdjustTrackEvent.Instance.UpdateEventToken();
// 获取广告位信息
AdConfigParser.Parse(GetRemoteConfigString("ad_config"));
// 刷新广告位信息
AdsSDKManager.Instance.RefreshAdsData();
//AB测试分组参数
GroupSet();
2025-09-02 10:20:43 +00:00
// adjust卸载监控
2025-09-01 10:32:50 +00:00
2025-09-02 10:20:43 +00:00
/* 执行到这时表示firebase接入正常能获取到远端在线参数 */
2025-09-01 10:32:50 +00:00
2025-09-02 10:20:43 +00:00
// 设置 firebase 初始化成功 flag
2025-09-01 10:32:50 +00:00
2025-09-02 10:20:43 +00:00
// 初始化广告
2025-09-01 10:32:50 +00:00
2025-09-02 10:20:43 +00:00
// 检查Adjust归因
});
});
2025-09-02 10:08:26 +00:00
// }
// });
2025-09-01 10:32:50 +00:00
}
private void GroupSet()
{
string value = GetRemoteConfigString("group_set");
//数数
ShuShuEvent.Instance.Track($"group_set_{value}");
//firebase
FireBaseAnalyticsManager.Instance.LogEvent($"group_set_{value}");
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 获取int参数
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public int GetRemoteConfigInt(string key, int defaultValue = 0, bool IsEncrypt = false)
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
try
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
string newKey = GetKey(key, IsEncrypt);
// 获取值
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
if (configValue.Source == ValueSource.RemoteValue)
{
int value = (int)GetValueLong(newKey, configValue);
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {value}, No Is defaultValue");
return value;
}
else
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
return defaultValue;
}
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
catch (Exception e)
2025-08-30 08:47:09 +00:00
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
2025-09-01 10:32:50 +00:00
Debug.LogError($"Failed to get Remote Config value for key '{key}': {e.Message}");
2025-08-30 08:47:09 +00:00
return defaultValue;
}
2025-09-01 10:32:50 +00:00
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 获取string参数
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public string GetRemoteConfigString(string key, string defaultValue = "", bool IsEncrypt = false)
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
try
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
string newKey = GetKey(key, IsEncrypt);
// 获取值
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
if (configValue.Source == ValueSource.RemoteValue)
{
string valueStr = GetValueStr(newKey, configValue);
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {valueStr}, No Is defaultValue");
return valueStr;
}
else
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
return defaultValue;
}
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
catch (Exception e)
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
Debug.LogError($"Failed to get Remote Config value for key '{key}': {e.Message}");
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
2025-08-30 08:47:09 +00:00
return defaultValue;
}
2025-09-01 10:32:50 +00:00
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 获取bool参数
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public bool GetRemoteConfigBool(string key, bool defaultValue = false, bool IsEncrypt = false)
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
try
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
string newKey = GetKey(key, IsEncrypt);
// 获取值
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
if (configValue.Source == ValueSource.RemoteValue)
{
bool valueBool = GetValueBool(newKey, configValue);
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {valueBool}, No Is defaultValue");
return valueBool;
}
else
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
return defaultValue;
}
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
catch (Exception e)
2025-08-30 08:47:09 +00:00
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
2025-09-01 10:32:50 +00:00
Debug.LogError($"Failed to get Remote Config value for key '{key}': {e.Message}");
2025-08-30 08:47:09 +00:00
return defaultValue;
}
2025-09-01 10:32:50 +00:00
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 获取float参数
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public float GetRemoteConfigFloat(string key, float defaultValue = 0, bool IsEncrypt = false)
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
try
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
string newKey = GetKey(key, IsEncrypt);
// 获取值
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
if (configValue.Source == ValueSource.RemoteValue)
{
float valueFloat = (float)GetValueDouble(newKey, configValue);
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {valueFloat}, No Is defaultValue");
return valueFloat;
}
else
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
return defaultValue;
}
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
catch (Exception e)
2025-08-30 08:47:09 +00:00
{
LoggerUtils.Debug($"[RemoteConfig] key is {key}, defaultValue is {defaultValue}, value is {defaultValue}, Is defaultValue");
2025-09-01 10:32:50 +00:00
Debug.LogError($"Failed to get Remote Config value for key '{key}': {e.Message}");
2025-08-30 08:47:09 +00:00
return defaultValue;
}
2025-09-01 10:32:50 +00:00
2025-08-30 08:47:09 +00:00
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 是否是加密
/// </summary>
/// <returns></returns>
private bool IsEncrypt(string key)
2025-08-30 08:47:09 +00:00
{
2025-09-01 10:32:50 +00:00
return key.StartsWith("CCB", StringComparison.OrdinalIgnoreCase);
2025-08-30 08:47:09 +00:00
}
2025-08-31 14:59:14 +00:00
2025-09-01 10:32:50 +00:00
private string GetKey(string key, bool isEncrypt)
{
//ccb开头 忽略大小写
return isEncrypt ? $"ccb{EncryptionUtils.AesEncrypt(AppSDKManager.PackageName, key)}" : key;
}
2025-08-31 14:59:14 +00:00
2025-09-01 10:32:50 +00:00
private string GetValueStr(string key, ConfigValue configValue)
{
return IsEncrypt(key) ? EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue) : configValue.StringValue;
}
2025-08-31 14:59:14 +00:00
2025-09-01 10:32:50 +00:00
private long GetValueLong(string key, ConfigValue configValue)
2025-08-31 14:59:14 +00:00
{
2025-09-01 10:32:50 +00:00
return IsEncrypt(key) ? long.Parse(EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue)) : configValue.LongValue;
}
private bool GetValueBool(string key, ConfigValue configValue)
{
return IsEncrypt(key) ? bool.Parse(EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue)) : configValue.BooleanValue;
}
private double GetValueDouble(string key, ConfigValue configValue)
{
if (IsEncrypt(key))
2025-08-31 14:59:14 +00:00
{
2025-09-01 10:32:50 +00:00
string valueStr = EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue);
if (float.TryParse(valueStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float result))
{
return result;
}
2025-08-31 14:59:14 +00:00
}
2025-09-01 10:32:50 +00:00
return configValue.DoubleValue;
2025-08-31 14:59:14 +00:00
}
}
2025-08-30 08:47:09 +00:00
}