246 lines
		
	
	
		
			9.4 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			246 lines
		
	
	
		
			9.4 KiB
		
	
	
	
		
			C#
		
	
	
	
| 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()
 | ||
|         {
 | ||
| 
 | ||
|             if (Application.isEditor)
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
|             
 | ||
|             // 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_bid_set"));
 | ||
|                     // 刷新广告位信息
 | ||
|                     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(Application.identifier, key)}" : key;
 | ||
|         }
 | ||
| 
 | ||
|         private string GetValueStr(string key, ConfigValue configValue)
 | ||
|         {
 | ||
|             return IsEncrypt(key) ? EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue) : configValue.StringValue;
 | ||
|         }
 | ||
| 
 | ||
|         private long GetValueLong(string key, ConfigValue configValue)
 | ||
|         {
 | ||
|             return IsEncrypt(key) ? long.Parse(EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue)) : configValue.LongValue;
 | ||
|         }
 | ||
| 
 | ||
|         private bool GetValueBool(string key, ConfigValue configValue)
 | ||
|         {
 | ||
|             return IsEncrypt(key) ? bool.Parse(EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue)) : configValue.BooleanValue;
 | ||
|         }
 | ||
| 
 | ||
|         private double GetValueDouble(string key, ConfigValue configValue)
 | ||
|         {
 | ||
|             if (IsEncrypt(key))
 | ||
|             {
 | ||
|                 string valueStr = EncryptionUtils.AesDecrypt(Application.identifier, configValue.StringValue);
 | ||
|                 if (float.TryParse(valueStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float result))
 | ||
|                 {
 | ||
|                     return result;
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             return configValue.DoubleValue;
 | ||
|         }
 | ||
|     }
 | ||
| } |