Files
Webview_SgConfig_Unity_IOS/Assets/Scripts/ConfigSystem_sdk.cs
T

281 lines
9.1 KiB
C#
Raw Normal View History

2026-07-06 16:31:08 +08:00
using System;
using System.IO;
using BingoBrain.Core;
using UnityEngine;
using BingoBrain.Asset;
using BingoBrain.HotFix;
using System.Collections.Generic;
using Newtonsoft.Json;
using BingoBrain;
using System.Linq;
using System.Text;
2026-07-08 17:47:04 +08:00
using DontConfuse;
2026-07-10 10:22:32 +08:00
using UnityEngine.Networking;
using UnityEngine.Events;
using System.Collections;
2026-07-06 16:31:08 +08:00
namespace BingoBrain
{
public class ConfigSystem_sdk
{
private ConfigSystem_sdk()
{
}
private static readonly ConfigSystem_sdk _instance = new ConfigSystem_sdk();
public static ConfigSystem_sdk Instance
{
get { return _instance; }
}
private static Dictionary<Type, object> configData = new();
public void OnRequestGetConfig()
{
GameHelper.PostFunnelLogin("loadBegin");
var loginData = LoginModel_sdk.Instance;
var configFileName = "Json";
var configFileSavePath = $"{Application.persistentDataPath}/sdkConfig/";
var assetHotFixFilePath = $"{configFileSavePath}{configFileName}.txt";
var configFileNameKey = "sdkconfigFileName";
var CDNConfigFileName = loginData.setting;
string savedCfgName = PlayerPrefs.GetString(configFileNameKey);
bool needDownloadConfigFile = false;
if (!string.IsNullOrEmpty(CDNConfigFileName))
{
//如果本地Player Prefs里没有保存配置文件名
if (string.IsNullOrEmpty(savedCfgName))
{
// Debug.Log("[UNITY] No config file name saved.");
needDownloadConfigFile = true;
}
else
{
// Debug.Log($"[UNITY] Saved config name: {savedCfgName}, CDN config name: {CDNConfigFileName}");
//与CDN上的对比名称
if (!savedCfgName.Equals(CDNConfigFileName))
{
needDownloadConfigFile = true;
}
}
}
//Debug.Log($"[UNITY] needDownloadConfigFile: {needDownloadConfigFile}");
//默默地拉去新配置
// Debug.Log("kkkkkkkkkkkkkkkkkkkkkk" + needDownloadConfigFile);
// Debug.Log("kkkkkkkkkkkkkkkkkkkkkk" + savedCfgName);
if (needDownloadConfigFile || !File.Exists(assetHotFixFilePath))
{
2026-07-10 10:22:32 +08:00
SdkManager.Instance.StartCoroutine(GetTextFromUrl($"{loginData.cdn_url}/config/{CDNConfigFileName}",
2026-07-06 16:31:08 +08:00
configFileName, (content) =>
{
PlayerPrefs.SetString(configFileNameKey, CDNConfigFileName);
ParseConfig(content);
//CtrlDispatcher.Instance.Dispatch(CtrlMsg.NewConfigRead);
}, configFileSavePath));
}
2026-07-10 15:29:36 +08:00
else if (File.Exists(assetHotFixFilePath))
2026-07-06 16:31:08 +08:00
{
// Debug.Log($"[UNITY] Load config from datapath: {assetHotFixFilePath}");
ParseConfig(File.ReadAllText(assetHotFixFilePath));
// UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NetLoadingUI_Close);
AppDispatcher.Instance.Dispatch(CsjInfoC.LoginInit);
GameHelper.PostFunnelLogin("loadFinish");
}
2026-07-10 15:29:36 +08:00
//检查设备本地是否有配置文件
2026-07-06 16:31:08 +08:00
}
2026-07-10 10:22:32 +08:00
public static IEnumerator GetTextFromUrl(string url, string fileName, UnityAction<string> action = null,
string savePath = null)
{
var unityWebRequest = UnityWebRequest.Get(url);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.result is UnityWebRequest.Result.ConnectionError
or UnityWebRequest.Result.ProtocolError)
{
Debug.LogError($"加载 [ {fileName} ] 文本文件失败 {unityWebRequest.error} [ url: {url} ]");
action?.Invoke(default);
yield break;
}
var content = unityWebRequest.downloadHandler.text;
SaveTextFile(content, savePath, fileName);
action?.Invoke(content);
}
private static void SaveTextFile(string content, string filePath, string fileName)
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
File.WriteAllText($"{filePath}{fileName}.txt", content);
}
2026-07-06 16:31:08 +08:00
private void ParseConfig(string json)
{
Debug.Log("sdk--------json" + json);
if (json == null)
{
return;
}
if (!json.StartsWith("{"))
{
json = Encoding.UTF8.GetString(Base64Kit_sdk.Decrypt(Encoding.UTF8.GetBytes(json),
"com.matchgame.captaindicedubloons"));
}
Debug.Log("转化后的json" + json);
SetConfig(json);
2026-07-08 17:47:04 +08:00
Debug.Log(LoginModel_sdk.Instance.reg_time);
Debug.Log(GetRegisteredCalendarDaysByUTC(LoginModel_sdk.Instance.reg_time));
2026-07-10 15:29:36 +08:00
SdkManager.Instance.OpenWv();
// int flyswitch = ConfigSystem.GetConfig<CommonModel>().flyswitch;
// int propswitch = ConfigSystem.GetConfig<CommonModel>().propswitch;
// float top_offset = 150;//fgui中的顶部信息的高度
// float buttom_offset = 0;
// if (Screen.safeArea.y != 0)
// {//刘海屏
// top_offset += Screen.safeArea.y;
// }
// SdkManager.Instance.setFlyBtnTag(flyswitch == 1);
// SdkManager.Instance.setRewardBtnTag(propswitch == 1);
// SdkManager.Instance.SetBtn(ConfigSystem.GetConfig<CommonModel>().propCoord[0], ConfigSystem.GetConfig<CommonModel>().propCoord[1], 60, 60);
// SdkManager.Instance.SetPadding(0, top_offset / GRoot.inst.height, 0, buttom_offset / GRoot.inst.height);
SdkManager.Instance.SetDarkThough(true);
SdkManager.Instance.RefreshUrl();
SdkManager.Instance.ShowH5View(false);
2026-07-08 17:47:04 +08:00
}
public int GetRegisteredCalendarDaysByUTC(long regTimeTimestamp)
{
// 1. 转换为 0时区 的日期部分(抹去具体时分秒,变成 00:00:00)
DateTime regDateUtc = DateTimeOffset.FromUnixTimeSeconds(regTimeTimestamp).UtcDateTime.Date;
// 2. 获取当前 0时区 的日期部分
DateTime nowDateUtc = DateTimeOffset.UtcNow.UtcDateTime.Date;
// 3. 两个 0时区的日期直接相减
TimeSpan difference = nowDateUtc - regDateUtc;
2026-07-06 16:31:08 +08:00
2026-07-08 17:47:04 +08:00
int days = difference.Days;
return days < 0 ? 0 : days;
2026-07-06 16:31:08 +08:00
}
2026-07-07 15:58:59 +08:00
public RootObject SDKConfig;
2026-07-06 16:31:08 +08:00
private void SetConfig(string json_)
{
SDKConfig = JsonConvert.DeserializeObject<RootObject>(json_);
2026-07-08 17:47:04 +08:00
for (int i = 0; i < SDKConfig.h5Conf.links.Count; i++)
{
if (SDKConfig.h5Conf.links[i].maxF5Times < 0)
{
SDKConfig.h5Conf.links[i].maxF5Times = 99999;
}
}
for (int i = 0; i < SDKConfig.h6Conf.layerConfList.Count; i++)
{
for (int j = 0; j < SDKConfig.h6Conf.layerConfList[i].links.Count; j++)
{
if (SDKConfig.h6Conf.layerConfList[i].links[j].maxF5Times < 0)
{
SDKConfig.h6Conf.layerConfList[i].links[j].maxF5Times = 99999;
}
}
}
2026-07-06 16:31:08 +08:00
}
}
public class RootObject
{
public string bid;
public H5Conf h5Conf;
public H6Conf h6Conf;
}
public class H5Conf
{
public F5Interval f5Interval;//自动刷新间隔
public List<H5Link> links; // H5 的 Link
}
public class F5Interval
{
public int min;
public int max;
}
public class H5Link
{
public int id;
public string url;
public int weight;
public int maxF5Times;
}
public class H6Conf
{
public int layers;//层数总数
public ADClickF5Delay ADClickF5Delay;//延长时间
public SwitchCondition switchCondition;//开启条件
public List<LayerConfList> layerConfList;
}
public class ADClickF5Delay
{
public int min;
public int max;
}
public class SwitchCondition//开启条件
{
public List<RetentionConf> retentionConf; // 已经实例化为具体类型
}
public class RetentionConf//开启条件
{
public Days days;
public int prob;
}
public class Days
{
public int min;
public int max;
}
public class LayerConfList
{
public int layerId;//第几层
public int weight;//显示权重
public F5Interval f5Interval;//自动刷新间隔
public int offset;
public List<H6Link> links; // H6 内部图层的 Link
}
public class H6Link
{
public int id;
public string url;
public int weight;
public int maxF5Times;//刷新上限
public int ctProb;
public int ctProb2;
}
}