NLDClient-yudde/ProjectNLD/Assets/Editor/NoticeAddress/NoticeAddressSetWindow.cs

281 lines
9.7 KiB
C#

using System;
using System.Collections.Generic;
using GamePlay.Notice;
using UnityEditor;
using UnityEngine;
namespace NoticeAddress
{
public class NoticeAddressSetWindow: EditorWindow
{
private const string CUSTOM_PROFILE_NAME = "自定义";
private static Dictionary<string, NoticeAddressItemData> _defaultAddressDic = new Dictionary<string, NoticeAddressItemData>()
{
{"dev地址", new NoticeAddressItemData(
"https://dev_openapi.kedrgame.com/api/v1/",
"uUi_UI1oMKWo0VOSN1zme17Z70-FlAv3Lcx2mNhoYe4=",
"0SoE48O9sayjkG8J"
)},
{"pre地址", new NoticeAddressItemData(
"https://pre_openapi.kedrgame.com/api/v1/",
"xYq_CUsU1q_K1w1uGDO3S9R7Y7-zcZ0K-C45_Aqz-xU=",
"bYFEOhDAWNtISptP"
)},
{"release地址", new NoticeAddressItemData(
"https://openapi.kedrgame.com/api/v1/",
"xYq_CUsU1q_K1w1uGDO3S9R7Y7-zcZ0K-C45_Aqz-xU=",
"bYFEOhDAWNtISptP"
)},
};
private static readonly string[] _profileOptions =
{
"dev地址",
"pre地址",
"release地址",
CUSTOM_PROFILE_NAME,
};
[MenuItem("Tools/启动流程/公告地址切换")]
public static void ShowWindow()
{
var window = GetWindow<NoticeAddressSetWindow>();
window.Show();
}
private NoticeAddressData _addressData;
private NoticeAddressItemData _customItemDataCache;
private bool _hasCustomItemDataCache;
private void OnGUI()
{
_EnsureAddressDataLoaded();
GUILayout.Label("公告地址切换", EditorStyles.boldLabel);
GUILayout.BeginVertical();
_DrawProfilePopup();
_DrawAddressField();
if (GUILayout.Button("保存", GUILayout.Height(40)))
{
_SaveAddressData();
}
GUILayout.EndVertical();
}
private void _EnsureAddressDataLoaded()
{
if (_addressData != null)
{
return;
}
// 先从editor load
var loadJson = EditorPrefs.GetString(NoticeAddressData.SAVE_KEY_NAME);
if (string.IsNullOrEmpty(loadJson))
{
_addressData = new NoticeAddressData();
}
else
{
try
{
_addressData = JsonUtility.FromJson<NoticeAddressData>(loadJson);
_TryUpgradeLegacyData(loadJson);
}
catch (Exception e)
{
Debug.LogError($"公告地址数据反序列化失败了, loadJson:{loadJson}, error:{e}");
_addressData = new NoticeAddressData();
}
}
if (_addressData == null)
{
_addressData = new NoticeAddressData();
}
_NormalizeAddressData();
}
private void _NormalizeAddressData()
{
_addressData.profileName = (_addressData.profileName ?? string.Empty).Trim();
_addressData.itemData = _CloneItemData(_addressData.itemData);
_TrimItemData(_addressData.itemData);
if (string.IsNullOrEmpty(_addressData.profileName))
{
if (_TryGetProfileNameByAddress(_addressData.itemData.newAddress, out var matchedProfileName))
{
_addressData.profileName = matchedProfileName;
_addressData.itemData = _CloneItemData(_defaultAddressDic[matchedProfileName]);
return;
}
_addressData.profileName = CUSTOM_PROFILE_NAME;
_customItemDataCache = _CloneItemData(_addressData.itemData);
_hasCustomItemDataCache = true;
return;
}
if (_defaultAddressDic.TryGetValue(_addressData.profileName, out var defaultAddress))
{
_addressData.itemData = _CloneItemData(defaultAddress);
return;
}
_addressData.profileName = CUSTOM_PROFILE_NAME;
_customItemDataCache = _CloneItemData(_addressData.itemData);
_hasCustomItemDataCache = true;
}
private void _DrawProfilePopup()
{
var currentIndex = _GetSelectedProfileIndex();
var selectedIndex = EditorGUILayout.Popup("地址配置", currentIndex, _profileOptions);
if (selectedIndex == currentIndex)
{
return;
}
var selectedProfileName = _profileOptions[selectedIndex];
if (_IsCustomProfile(_addressData.profileName))
{
_customItemDataCache = _CloneItemData(_addressData.itemData);
_hasCustomItemDataCache = true;
}
_addressData.profileName = selectedProfileName;
if (_IsCustomProfile(selectedProfileName))
{
_addressData.itemData = _hasCustomItemDataCache ? _CloneItemData(_customItemDataCache) : new NoticeAddressItemData();
return;
}
_addressData.itemData = _CloneItemData(_defaultAddressDic[selectedProfileName]);
}
private void _DrawAddressField()
{
if (_IsCustomProfile(_addressData.profileName))
{
var newAddress = EditorGUILayout.TextField("自定义地址", _addressData.itemData.newAddress);
var httpKey = EditorGUILayout.TextField("SecretKey", _addressData.itemData.httpKey);
var httpAccessKey = EditorGUILayout.TextField("AccessKey", _addressData.itemData.httpAccessKey);
EditorGUILayout.HelpBox("配置为空则表示使用游戏内配置", MessageType.Warning);
if (newAddress == _addressData.itemData.newAddress
&& httpKey == _addressData.itemData.httpKey
&& httpAccessKey == _addressData.itemData.httpAccessKey)
{
return;
}
_addressData.itemData.newAddress = newAddress;
_addressData.itemData.httpKey = httpKey;
_addressData.itemData.httpAccessKey = httpAccessKey;
_customItemDataCache = _CloneItemData(_addressData.itemData);
_hasCustomItemDataCache = true;
return;
}
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField("当前地址", _addressData.itemData.newAddress);
EditorGUILayout.TextField("SecretKey", _addressData.itemData.httpKey);
EditorGUILayout.TextField("AccessKey", _addressData.itemData.httpAccessKey);
EditorGUI.EndDisabledGroup();
}
private int _GetSelectedProfileIndex()
{
for (var i = 0; i < _profileOptions.Length; i++)
{
if (_profileOptions[i] == _addressData.profileName)
{
return i;
}
}
return _profileOptions.Length - 1;
}
private bool _TryGetProfileNameByAddress(string address, out string profileName)
{
foreach (var pair in _defaultAddressDic)
{
if (string.Equals(pair.Value.newAddress, address, StringComparison.OrdinalIgnoreCase))
{
profileName = pair.Key;
return true;
}
}
profileName = string.Empty;
return false;
}
private bool _IsCustomProfile(string profileName)
{
return string.Equals(profileName, CUSTOM_PROFILE_NAME, StringComparison.Ordinal);
}
private void _SaveAddressData()
{
var saveJson = JsonUtility.ToJson(_addressData);
EditorPrefs.SetString(NoticeAddressData.SAVE_KEY_NAME, saveJson);
Debug.Log($"公告地址数据保存了, saveJson:{saveJson}");
}
private void _TryUpgradeLegacyData(string loadJson)
{
if (_addressData == null || _addressData.itemData != null)
{
return;
}
var legacyData = JsonUtility.FromJson<LegacyNoticeAddressData>(loadJson);
if (legacyData == null)
{
return;
}
_addressData.profileName = string.IsNullOrEmpty(_addressData.profileName) ? legacyData.profileName : _addressData.profileName;
_addressData.itemData = new NoticeAddressItemData(legacyData.newAddress, string.Empty, string.Empty);
}
private NoticeAddressItemData _CloneItemData(NoticeAddressItemData source)
{
if (source == null)
{
return new NoticeAddressItemData();
}
return new NoticeAddressItemData(source.newAddress, source.httpKey, source.httpAccessKey);
}
private void _TrimItemData(NoticeAddressItemData itemData)
{
if (itemData == null)
{
return;
}
itemData.newAddress = (itemData.newAddress ?? string.Empty).Trim();
itemData.httpKey = (itemData.httpKey ?? string.Empty).Trim();
itemData.httpAccessKey = (itemData.httpAccessKey ?? string.Empty).Trim();
}
[Serializable]
private class LegacyNoticeAddressData
{
public string profileName = string.Empty;
public string newAddress = string.Empty;
}
}
}