// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-12-07 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** #if !UNITY_EDITOR #define PRODUCTION_MY #endif using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; namespace G { /// /// 远程配置 /// public class KRemoteConfig : MonoBehaviour { [System.Serializable] public sealed class ConfigInfo { /// /// /// public string url; /// /// 当前版本 /// public string version; /// /// 最低版本 /// public string min_version; /// /// 群地址 /// public string group_url; /// /// 公告 /// public string notice; /// /// 公告时间 /// public string notice_time; /// /// /// public string review; /// /// /// public string gongzhonghao; /// /// /// public bool show_login; /// /// /// public bool support_pay; /// /// /// public bool support_pay_ios; /// /// 支持分享游戏 /// public bool support_share_game; /// /// /// [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 /// /// /// public ConfigInfo remoteConfig { get; private set; } /// /// /// public bool initSuccess { get { return remoteConfig != null && remoteConfig.error == 0; } } /// /// /// 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; } } /// /// /// 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; } } /// /// /// 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 } } /// /// 群地址 /// public string groupURL { get { return remoteConfig != null ? remoteConfig.group_url : default; } } /// /// /// public string gongzhonghao { get { return remoteConfig != null ? remoteConfig.gongzhonghao : default; } } /// /// /// public string notice { get { return remoteConfig != null ? remoteConfig.notice : default; } } /// /// /// public string noticeTime { get { return remoteConfig != null ? remoteConfig.notice_time : default; } } /// /// 有新的 /// 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(); } } /// /// /// public bool showLoginBtn { get { return !isReview ? remoteConfig.show_login : false; } } /// /// 支持支付 /// 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; } } /// /// 支持分享游戏 /// public bool supportShareGame { get { return remoteConfig != null ? remoteConfig.support_share_game : false; } } /// /// /// 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; } } /// /// /// /// 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(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 } }