shaoxiadiablo/Assets/AGame/Scripts/KRemoteConfig.cs
2025-05-18 01:04:31 +08:00

474 lines
12 KiB
C#

// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-12-07
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "KRemoteConfig" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
#if !UNITY_EDITOR
#define PRODUCTION_MY
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace G
{
/// <summary>
/// 远程配置
/// </summary>
public class KRemoteConfig : MonoBehaviour
{
[System.Serializable]
public sealed class ConfigInfo
{
/// <summary>
///
/// </summary>
public string url;
/// <summary>
/// 当前版本
/// </summary>
public string version;
/// <summary>
/// 最低版本
/// </summary>
public string min_version;
/// <summary>
/// 群地址
/// </summary>
public string group_url;
/// <summary>
/// 公告
/// </summary>
public string notice;
/// <summary>
/// 公告时间
/// </summary>
public string notice_time;
/// <summary>
///
/// </summary>
public string review;
/// <summary>
///
/// </summary>
public string gongzhonghao;
/// <summary>
///
/// </summary>
public bool show_login;
/// <summary>
///
/// </summary>
public bool support_pay;
/// <summary>
///
/// </summary>
public bool support_pay_ios;
/// <summary>
/// 支持分享游戏
/// </summary>
public bool support_share_game;
/// <summary>
///
/// </summary>
[SerializeField]
private ShareGameInfo[] ShareGames;
public ShareGameInfo[] shareGameInfos => ShareGames;
[System.NonSerialized]
public int error = -1;
}
[System.Serializable]
public sealed class ShareGameInfo
{
[SerializeField]
string GameName;
[SerializeField]
string AppID;
[SerializeField]
string Icon;
[SerializeField]
bool IsOpen;
[SerializeField]
string SendData;
public string gameName => GameName;
public string appId => AppID;
public string iconName => Icon;
public string extraData => SendData;
public bool isValid => IsOpen && AppID != KPlatform.Instance.appId;
public string GetIconURL()
{
return "https://cdn-fcds.lanfeitech.com/online/share/game/Icons/" + Icon;
}
private Texture2D _texture;
private Sprite _sprite;
public void GetSprite(Callback3 callback)
{
if (_sprite)
{
callback(0, "", _sprite);
}
else
{
KRemoteConfig.Instance.StartCoroutine(SetIconSprite(callback));
}
}
public IEnumerator SetIconSprite(Callback3 callback)
{
var request = UnityWebRequestTexture.GetTexture(GetIconURL(), true);
yield return request.SendWebRequest();
if (string.IsNullOrEmpty(request.error))
{
_texture = DownloadHandlerTexture.GetContent(request);
_sprite = Sprite.Create(_texture, new Rect(0, 0, _texture.width, _texture.height), Vector2.one * 0.5f);
callback(0, "", _sprite);
}
else
{
callback(1, "", null);
}
}
}
#region Property
/// <summary>
///
/// </summary>
public ConfigInfo remoteConfig
{
get;
private set;
}
/// <summary>
///
/// </summary>
public bool initSuccess
{
get { return remoteConfig != null && remoteConfig.error == 0; }
}
/// <summary>
///
/// </summary>
public bool isMinVersion
{
get
{
#if PRODUCTION_MY
if (remoteConfig != null && !string.IsNullOrEmpty(remoteConfig.min_version))
{
if (System.Version.TryParse(remoteConfig.min_version, out var remoteAppV))
{
System.Version.TryParse(Application.version, out var localAppV);
return remoteAppV > localAppV;
}
}
#endif
return false;
}
}
/// <summary>
///
/// </summary>
public bool findNewVersion
{
get
{
#if PRODUCTION_MY
if (remoteConfig != null && !string.IsNullOrEmpty(remoteConfig.version))
{
if (System.Version.TryParse(remoteConfig.version, out var remoteAppV))
{
System.Version.TryParse(Application.version, out var localAppV);
return remoteAppV > localAppV;
}
}
#endif
return false;
}
}
/// <summary>
///
/// </summary>
public bool isReview
{
get
{
#if PRODUCTION_MY
if (remoteConfig != null && !string.IsNullOrEmpty(remoteConfig.review))
{
return remoteConfig.review == Application.version;
}
return true;
#else
return false;
#endif
}
}
/// <summary>
/// 群地址
/// </summary>
public string groupURL
{
get { return remoteConfig != null ? remoteConfig.group_url : default; }
}
/// <summary>
///
/// </summary>
public string gongzhonghao
{
get { return remoteConfig != null ? remoteConfig.gongzhonghao : default; }
}
/// <summary>
///
/// </summary>
public string notice
{
get { return remoteConfig != null ? remoteConfig.notice : default; }
}
/// <summary>
///
/// </summary>
public string noticeTime
{
get { return remoteConfig != null ? remoteConfig.notice_time : default; }
}
/// <summary>
/// 有新的
/// </summary>
public bool newNotice
{
get
{
var remoteStr = this.noticeTime;
return string.IsNullOrEmpty(remoteStr) ? false : remoteStr.CompareTo(PlayerPrefs.GetString("notice_time", "")) > 0;
}
set
{
var remoteStr = this.noticeTime;
PlayerPrefs.SetString("notice_time", remoteStr);
PlayerPrefs.Save();
}
}
public bool newVersionNote
{
get
{
var remoteStr = this.remoteConfig.version;
return string.IsNullOrEmpty(remoteStr) ? false : remoteStr.CompareTo(PlayerPrefs.GetString("ver_note", "")) > 0;
}
set
{
var remoteStr = this.remoteConfig.version;
PlayerPrefs.SetString("ver_note", remoteStr);
PlayerPrefs.Save();
}
}
/// <summary>
///
/// </summary>
public bool showLoginBtn
{
get { return !isReview ? remoteConfig.show_login : false; }
}
/// <summary>
/// 支持支付
/// </summary>
public bool supportPay
{
get
{
#if UNITY_EDITOR
return true;
#else
return remoteConfig != null ? remoteConfig.support_pay : false;
#endif
}
}
public bool supportPayiOS
{
get
{
return !isReview && remoteConfig.support_pay_ios;
}
}
/// <summary>
/// 支持分享游戏
/// </summary>
public bool supportShareGame
{
get
{
return remoteConfig != null ? remoteConfig.support_share_game : false;
}
}
/// <summary>
///
/// </summary>
public string shareImageUrl
{
get { return $"https://cdn-shaoxia.lanfeitech.com/minigame/dxbk_share{Random.Range(1, 2)}"; }
}
#endregion
#region Request
Callback2 _callback;
public void GetConfig(Callback2 callback)
{
if (remoteConfig != null)
{
if (remoteConfig.error == 0)
callback?.Invoke(0, default);
else
{
_callback = callback;
StartCoroutine(RequestWebConfigCO());
}
}
else
{
_callback = callback;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private IEnumerator RequestWebConfigCO()
{
#if SDK_WECHAT_WASM
var httpUrl = "https://dev-cdn-shaoxia.lanfeitech.com/prod/v2/config/wasm.json";
var request = UnityWebRequest.Get(httpUrl);
#else
#if PRODUCTION_MY
var httpUrl = "https://dev-cdn-shaoxia.lanfeitech.com/prod/v2/config/";
#else
var httpUrl = "https://dev-cdn-shaoxia.lanfeitech.com/prod/v2/config/";
#endif
//int minute = Launch.Timestamp / 600;
#if UNITY_IOS
var request = UnityWebRequest.Get(httpUrl + $"ios.json");
#elif UNITY_ANDROID
var request = UnityWebRequest.Get(httpUrl + $"android.json");
#else
var request = UnityWebRequest.Get(httpUrl + $"android.json");
#endif
#endif
yield return request.SendWebRequest();
if (string.IsNullOrEmpty(request.error))
{
var json = request.downloadHandler.text;
SetText(json);
_callback?.Invoke(0, default);
}
else
{
remoteConfig = new ConfigInfo
{
error = 1,
};
_callback?.Invoke(1, default);
}
_callback = null;
}
private IEnumerator RequestShareGameConfigCO()
{
int minute = Launch.Timestamp / 600;
var request = UnityWebRequest.Get($"https://cdn-fcds.lanfeitech.com/online/share/game/game.json?key={minute}");
yield return request.SendWebRequest();
if (string.IsNullOrEmpty(request.error))
{
var json = request.downloadHandler.text;
JsonUtility.FromJsonOverwrite(json, remoteConfig);
}
else
{
}
}
private void SetText(string jsonText)
{
try
{
#if UNITY_EDITOR || DEBUG_MY
Debug.Log(jsonText);
#endif
remoteConfig = JsonUtility.FromJson<ConfigInfo>(jsonText);
remoteConfig.error = 0;
}
catch (System.Exception ex)
{
#if UNITY_EDITOR || DEBUG_MY
Debug.Log(ex.Message);
#endif
remoteConfig = new ConfigInfo
{
error = 1,
};
}
}
#endregion
#region Unity && Instance
public static KRemoteConfig Instance;
private void Awake()
{
Instance = this;
}
private IEnumerator Start()
{
yield return RequestWebConfigCO();
//yield return RequestShareGameConfigCO();
}
#endregion
}
}