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

261 lines
10 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-09-10 06:53:21 +00:00
using AdjustSdk;
2025-09-10 10:07:22 +00:00
using EFSDK;
2025-08-31 08:42:48 +00:00
using Firebase.Extensions;
2025-09-10 06:53:21 +00:00
using Firebase.Messaging;
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
{
if (Application.isEditor)
{
return;
}
2025-09-10 06:53:21 +00:00
2025-09-02 10:08:26 +00:00
// Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
// {
// if (task.Result == Firebase.DependencyStatus.Available)
// {
2025-09-10 06:53:21 +00:00
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
2025-09-02 10:20:43 +00:00
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.FetchAsync(TimeSpan.Zero).ContinueWithOnMainThread(task =>
{
FirebaseRemoteConfig.DefaultInstance.ActivateAsync().ContinueWithOnMainThread(task =>
{
IsInitialized = true;
2025-09-10 06:53:21 +00:00
FirebaseMessaging.TokenReceived += OnTokenReceived;
2025-09-02 10:20:43 +00:00
AdjustTrackEvent.Instance.UpdateEventToken();
2025-09-02 13:42:07 +00:00
AdsKeyEvents.Instance.InitData();
2025-09-02 10:20:43 +00:00
// 获取广告位信息
AdConfigParser.Parse(GetRemoteConfigString("ad_bid_set"));
2025-09-15 07:42:30 +00:00
// 获取kwai广告位信息
KwaiAdsConfigParser.Parse(GetRemoteConfigString("kwai_rv_floor"),true);
KwaiAdsConfigParser.Parse(GetRemoteConfigString("kwai_iv_floor"),false);
2025-10-17 11:31:25 +00:00
2025-09-02 10:20:43 +00:00
//AB测试分组参数
GroupSet();
2025-09-10 10:07:22 +00:00
//推送事件
EFSdk.get().SetGameActive(true);
2025-10-17 11:31:25 +00:00
// 刷新广告位信息
AdsSDKManager.Instance.RefreshAdsData();
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
}
2025-09-10 06:53:21 +00:00
public static void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
Adjust.SetPushToken(token?.Token);
}
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(Application.identifier, key)}" : key;
2025-09-01 10:32:50 +00:00
}
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(Application.identifier, configValue.StringValue) : configValue.StringValue;
2025-09-01 10:32:50 +00:00
}
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
{
return IsEncrypt(key) ? long.Parse(EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue)) : configValue.LongValue;
2025-09-01 10:32:50 +00:00
}
private bool GetValueBool(string key, ConfigValue configValue)
{
return IsEncrypt(key) ? bool.Parse(EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue)) : configValue.BooleanValue;
2025-09-01 10:32:50 +00:00
}
private double GetValueDouble(string key, ConfigValue configValue)
{
if (IsEncrypt(key))
2025-08-31 14:59:14 +00:00
{
string valueStr = EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue);
2025-09-01 10:32:50 +00:00
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
}