c1_unity/Assets/Scripts/Common/DeepLink/DeepLinkManager.cs
2025-11-03 10:59:33 +08:00

255 lines
8.3 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.Collections.Generic;
using System;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;
namespace BF
{
public class DeepLinkManager : MonoBehaviour
{
// 深度链接参数
public static string ENTRANCE = "entrance"; // Lan/Wan 内外网
public const string ENTRANCE_LAN = "lan";
public static string ENV = "env"; // 正式环境/白名单等
public static string GM = "gm"; // gm
public static string LOG = "log"; // 日志
public static string PACKAGE = "package"; // 包名
public const string TRUE_FLAG = "true";
public const string FALSE_FLAG = "false";
private const string SIGN_FLAG = "sign"; // 签名
// 缓存上一次的地址 与 解析后的参数
private string DeepLinkURL = string.Empty;
private Dictionary<string, string> DeepLinkParams = new Dictionary<string, string>();
// 因事件触发的callback,冷启动会在login中处理
public Action<string, string> luaDeepLinkActiveCallback;
private static char[] NOTHING_CHAR_RESULT = new char[32];
private static char[] NOTHING_CHAR = new char[32] {
(char)('y' ^ (0x37 - 0)),
(char)('r' ^ (0x37 - 1)),
(char)('u' ^ (0x37 - 2)),
(char)('8' ^ (0x37 - 3)),
(char)('c' ^ (0x37 - 4)),
(char)('z' ^ (0x37 - 5)),
(char)('P' ^ (0x37 - 6)),
(char)('i' ^ (0x37 - 7)),
(char)('s' ^ (0x37 - 8)),
(char)('o' ^ (0x37 - 9)),
(char)('O' ^ (0x37 - 10)),
(char)('0' ^ (0x37 - 11)),
(char)('x' ^ (0x37 - 12)),
(char)('W' ^ (0x37 - 13)),
(char)('L' ^ (0x37 - 14)),
(char)('e' ^ (0x37 - 15)),
(char)('k' ^ (0x37 - 16)),
(char)('B' ^ (0x37 - 17)),
(char)('q' ^ (0x37 - 18)),
(char)('2' ^ (0x37 - 19)),
(char)('H' ^ (0x37 - 20)),
(char)('f' ^ (0x37 - 21)),
(char)('Y' ^ (0x37 - 22)),
(char)('n' ^ (0x37 - 23)),
(char)('X' ^ (0x37 - 24)),
(char)('4' ^ (0x37 - 25)),
(char)('A' ^ (0x37 - 26)),
(char)('9' ^ (0x37 - 27)),
(char)('N' ^ (0x37 - 28)),
(char)('a' ^ (0x37 - 29)),
(char)('U' ^ (0x37 - 30)),
(char)('G' ^ (0x37 - 31)),
};
private static bool NothingFlag = false;
private static char[] GetNothingChar()
{
if (!NothingFlag)
{
NothingFlag = true;
for (int i = 0; i < 32; ++i)
{
NOTHING_CHAR_RESULT[i] = (char)(NOTHING_CHAR[i] ^ (0x37 - i));
}
}
return NOTHING_CHAR_RESULT;
}
void Start()
{
Init();
}
public void Init()
{
// 注册监听
Application.deepLinkActivated += OnDeepLinkActivated;
if (!string.IsNullOrEmpty(Application.absoluteURL))
{
// 冷启动而且 Application.absoluteURL 不为 null因此处理深层链接。
OnDeepLinkActivated(Application.absoluteURL);
}
// 初始化 DeepLink Manager 全局变量。
else
{
DeepLinkURL = string.Empty;
}
}
private void OnDeepLinkActivated(string url)
{
DeepLinkURL = url;
UpdateMapData(DeepLinkURL);
// 不考虑中途打开 仅处理冷启动
// if (luaDeepLinkActiveCallback != null)
// {
// // 延迟的回调
// luaDeepLinkActiveCallback(DeepLinkURL, GetDeepLinkParamsJson());
// }
HandleLaunchLinkParams();
}
private void HandleLaunchLinkParams()
{
if (!DeepLinkParams.ContainsKey(SIGN_FLAG))
{
return;
}
string serverSign = DeepLinkParams[SIGN_FLAG];
if (string.IsNullOrEmpty(serverSign))
{
return;
}
string signStr = string.Empty;
foreach (var kvp in DeepLinkParams.OrderBy(kv => kv.Key, StringComparer.Ordinal))
{
if (!kvp.Key.Equals(SIGN_FLAG))
{
if (string.IsNullOrEmpty(signStr))
{
signStr += kvp.Key + "=" + kvp.Value;
}
else
{
signStr += "&" + kvp.Key + "=" + kvp.Value;
}
}
}
signStr += string.Concat(GetNothingChar());
if (!Md5Encrypt.VerifyMd5Hash(signStr, serverSign))
{
return;
}
// 冷启动 更新参数
if (DeepLinkParams.ContainsKey(ENTRANCE))
{
if (!string.IsNullOrEmpty(DeepLinkParams[ENTRANCE]))
{
BFMain.DPEntrance = DeepLinkParams[ENTRANCE];
if (BFMain.DPEntrance == ENTRANCE_LAN)
{
BFMain.DPIsLan = true;
}
Debug.Log("深度链接 更新 ENTRANCE:" + BFMain.DPEntrance + " 内网:" + BFMain.DPIsLan); // 测试日志 TODOJ
}
}
if (DeepLinkParams.ContainsKey(ENV))
{
if (!string.IsNullOrEmpty(DeepLinkParams[ENV]))
{
BFMain.DPEnv = DeepLinkParams[ENV];
BFMain.DPForceEnv = true;
Debug.Log("深度链接 更新 ENV:" + BFMain.DPEnv); // 测试日志 TODOJ
}
}
if (DeepLinkParams.ContainsKey(GM))
{
var support = DeepLinkParams[GM] == TRUE_FLAG;
BFMain.DPSupportGM = support;
Debug.Log("深度链接 更新 GM:" + BFMain.DPSupportGM); // 测试日志 TODOJ
}
if (DeepLinkParams.ContainsKey(LOG))
{
var support = DeepLinkParams[LOG] == TRUE_FLAG;
BFMain.DPSupportLog = support;
Debug.Log("深度链接 更新 LOG:" + BFMain.DPSupportLog); // 测试日志 TODOJ
}
if (DeepLinkParams.ContainsKey(PACKAGE))
{
if (!string.IsNullOrEmpty(DeepLinkParams[PACKAGE]))
{
BFMain.DPPackageName = DeepLinkParams[PACKAGE];
Debug.Log("深度链接 更新 ENV:" + BFMain.DPPackageName);
}
}
}
private void UpdateMapData(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return;
}
var splitUrl = url.Split('?', 2);
if (splitUrl.Length < 2)
{
return;
}
var parsedUrl = splitUrl[1];
if (string.IsNullOrWhiteSpace(parsedUrl))
{
return;
}
var strs = parsedUrl.Split('&');
foreach (var str in strs)
{
var keyValueStr = str.Split('=', 2);
if (keyValueStr.Length == 2)
{
var key = keyValueStr[0];
var value = keyValueStr[1];
// 更新
DeepLinkParams[key] = value;
}
}
}
// 获取缓存的深度链接
public string GetDeepLinkURL()
{
return DeepLinkURL;
}
public Dictionary<string, string> GetDeepLinkParamsDic()
{
return DeepLinkParams;
}
public string GetDeepLinkParamsJson()
{
return JsonConvert.SerializeObject(DeepLinkParams);
}
public string GetDeepLinkValueByKey(string key)
{
if (DeepLinkParams.ContainsKey(key))
{
return DeepLinkParams[key];
}
return string.Empty;
}
// 设置回调
public void SetLuaDeepLinkActiveCallback(Action<string, string> callback)
{
luaDeepLinkActiveCallback = callback;
}
}
}