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

240 lines
9.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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