220 lines
		
	
	
		
			8.6 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			220 lines
		
	
	
		
			8.6 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | ||
| using System.Globalization;
 | ||
| using Firebase.Extensions;
 | ||
| using Firebase.RemoteConfig;
 | ||
| using UnityEngine;
 | ||
| 
 | ||
| namespace WZ
 | ||
| {
 | ||
|     public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigManager>
 | ||
|     {
 | ||
|         public void FetchRemoteConfig()
 | ||
|         {
 | ||
|             Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
 | ||
|             {
 | ||
|                 if (task.Result == Firebase.DependencyStatus.Available)
 | ||
|                 {
 | ||
|                     Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.FetchAsync(TimeSpan.Zero).ContinueWithOnMainThread(task =>
 | ||
|                     {
 | ||
|                         FirebaseRemoteConfig.DefaultInstance.ActivateAsync().ContinueWithOnMainThread(task =>
 | ||
|                         {
 | ||
|                             // adjust卸载监控
 | ||
| 
 | ||
|                             /* 执行到这时,表示firebase接入正常,能获取到远端在线参数 */
 | ||
| 
 | ||
|                             // 设置 firebase 初始化成功 flag
 | ||
| 
 | ||
|                             // 初始化广告
 | ||
| 
 | ||
|                             // 检查Adjust归因                   
 | ||
|                         });
 | ||
|                     });
 | ||
|                 }
 | ||
|             });
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         /// <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;
 | ||
|         }
 | ||
|     }
 | ||
| } |