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

212 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using GamePlay.Notice;
using UnityEditor;
using UnityEngine;
namespace NoticeAddress
{
public class NoticeAddressSetWindow: UnityEditor.EditorWindow
{
private const string CUSTOM_PROFILE_NAME = "自定义";
private static Dictionary<string, string> _defaultAddressDic = new Dictionary<string, string>()
{
{"dev地址", "https://dev_openapi.kedrgame.com/api/v1/"},
{"pre地址", "https://pre_openapi.kedrgame.com/api/v1/"},
{"release地址", "https://openapi.kedrgame.com/api/v1/"},
};
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 string _customAddressCache = string.Empty;
private bool _hasCustomAddressCache;
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);
}
catch (Exception e)
{
Debug.LogError($"公告地址数据反序列化失败了, loadJson:{loadJson}, error:{e}");
_addressData = new NoticeAddressData();
}
}
if (_addressData == null)
{
_addressData = new NoticeAddressData();
}
_NormalizeAddressData();
}
private void _NormalizeAddressData()
{
_addressData.newAddress = (_addressData.newAddress ?? string.Empty).Trim();
_addressData.profileName = (_addressData.profileName ?? string.Empty).Trim();
if (string.IsNullOrEmpty(_addressData.profileName))
{
if (_TryGetProfileNameByAddress(_addressData.newAddress, out var matchedProfileName))
{
_addressData.profileName = matchedProfileName;
_addressData.newAddress = _defaultAddressDic[matchedProfileName];
return;
}
_addressData.profileName = CUSTOM_PROFILE_NAME;
_customAddressCache = _addressData.newAddress;
_hasCustomAddressCache = true;
return;
}
if (_defaultAddressDic.TryGetValue(_addressData.profileName, out var defaultAddress))
{
_addressData.newAddress = defaultAddress;
return;
}
_addressData.profileName = CUSTOM_PROFILE_NAME;
_customAddressCache = _addressData.newAddress;
_hasCustomAddressCache = 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))
{
_customAddressCache = _addressData.newAddress;
_hasCustomAddressCache = true;
}
_addressData.profileName = selectedProfileName;
if (_IsCustomProfile(selectedProfileName))
{
_addressData.newAddress = _hasCustomAddressCache ? _customAddressCache : string.Empty;
return;
}
_addressData.newAddress = _defaultAddressDic[selectedProfileName];
}
private void _DrawAddressField()
{
if (_IsCustomProfile(_addressData.profileName))
{
var newAddress = EditorGUILayout.TextField("自定义地址", _addressData.newAddress);
EditorGUILayout.HelpBox("配置为空则表示使用游戏内配置", MessageType.Warning);
if (newAddress == _addressData.newAddress)
{
return;
}
_addressData.newAddress = newAddress;
_customAddressCache = newAddress;
_hasCustomAddressCache = true;
return;
}
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField("当前地址", _addressData.newAddress);
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, 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}");
}
}
}