Files
bansonic_beta_main/Assets/scripts/settings/userSettings.cs
T
2026-07-20 02:22:51 +08:00

384 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Bansonic;
using UnityEngine.SceneManagement;
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX
using Steamworks;
#endif
#if UNITY_EDITOR
using UnityEditor;
#endif
public class userSettings : MonoBehaviour
{
[Header("Inspector")]
[Tooltip("Documentation text normalized.")]
public Text user_login_source_text;
[Tooltip("Documentation text normalized.")]
public Text user_source_uid_text;
[Header("Inspector")]
[Tooltip("Documentation text normalized.")]
public Dropdown language_dropdown;
[Header("Inspector")]
[Tooltip("Documentation text normalized.")]
public Button export_saveData_button;
[Tooltip("Documentation text normalized.")]
public Button import_saveData_button;
[Tooltip("Documentation text normalized.")]
public Button clearup_saveData_button;
[Header("Inspector")]
[Tooltip("Documentation text normalized.")]
public Button view_detailedHighlight_button;
private const string RuntimeResourcesPath = "song_songIndex";
private const string EditorAssetsPath = "Assets/Resources/song_songIndex";
[SerializeField] private float clearupConfirmSeconds = 5f;
private int clearupClickCount = 0;
private float clearupExpireTime;
private bool _updatingLanguageDropdown;
private readonly List<string> _availableLanguageCodes = new List<string>();
private void Start()
{
InitializeLanguageDropdown();
UpdateUserInfoDisplay();
}
private void UpdateUserInfoDisplay()
{
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX
if (SteamManager.Initialized)
{
if (user_login_source_text != null)
{
string branch;
if (SteamApps.GetCurrentBetaName(out branch, 64))
{
user_login_source_text.text = "(Steam) " + branch;
}
else
{
user_login_source_text.text = LocalizationService.Get("settings.source.steam_public", "(Steam) public");
}
}
if (user_source_uid_text != null)
{
CSteamID steamID = SteamUser.GetSteamID();
string personaName = SteamFriends.GetPersonaName();
user_source_uid_text.text = string.Format("{0} ({1})", personaName, steamID.m_SteamID.ToString());
}
return;
}
#endif
if (user_login_source_text != null)
user_login_source_text.text = LocalizationService.Get("settings.source.unknown_server", "Unknown Server");
if (user_source_uid_text != null)
user_source_uid_text.text = LocalizationService.Get("settings.source.unknown_user", "Unknown User");
}
private void OnEnable()
{
LocalizationService.LanguageChanged += HandleLanguageChanged;
if (clearup_saveData_button != null)
clearup_saveData_button.onClick.AddListener(OnClearupSaveDataClicked);
if (export_saveData_button != null) export_saveData_button.onClick.AddListener(OnUnimplementedFeatureClicked);
if (import_saveData_button != null) import_saveData_button.onClick.AddListener(OnUnimplementedFeatureClicked);
if (view_detailedHighlight_button != null) view_detailedHighlight_button.onClick.AddListener(OnUnimplementedFeatureClicked);
if (language_dropdown != null) language_dropdown.onValueChanged.AddListener(OnLanguageDropdownValueChanged);
ResetClearupCounter();
InitializeLanguageDropdown();
SyncLanguageDropdownValue();
}
private void OnDisable()
{
LocalizationService.LanguageChanged -= HandleLanguageChanged;
if (clearup_saveData_button != null)
clearup_saveData_button.onClick.RemoveListener(OnClearupSaveDataClicked);
if (export_saveData_button != null) export_saveData_button.onClick.RemoveListener(OnUnimplementedFeatureClicked);
if (import_saveData_button != null) import_saveData_button.onClick.RemoveListener(OnUnimplementedFeatureClicked);
if (view_detailedHighlight_button != null) view_detailedHighlight_button.onClick.RemoveListener(OnUnimplementedFeatureClicked);
if (language_dropdown != null) language_dropdown.onValueChanged.RemoveListener(OnLanguageDropdownValueChanged);
ResetClearupCounter();
}
private void Update()
{
if (clearupClickCount > 0 && Time.unscaledTime > clearupExpireTime)
{
ResetClearupCounter();
}
}
private void ResetClearupCounter()
{
clearupClickCount = 0;
}
// 导出/导入存档、查看详细高光等按钮已定义 UI 但尚未接入实际逻辑,
// 点击时提示功能未开放。仍复位危险操作确认计数,保持原有副作用。
private void OnUnimplementedFeatureClicked()
{
ResetClearupCounter();
gNotice.error.display(LocalizationService.LocalizeLiteral("功能未开放"));
}
private void InitializeLanguageDropdown()
{
if (language_dropdown == null)
{
return;
}
LocalizationService.EnsureInitialized();
IReadOnlyList<string> availableCodes = LocalizationService.GetAvailableLanguageCodes();
_availableLanguageCodes.Clear();
List<Dropdown.OptionData> options = new List<Dropdown.OptionData>();
for (int i = 0; i < availableCodes.Count; i++)
{
string code = availableCodes[i];
_availableLanguageCodes.Add(code);
options.Add(new Dropdown.OptionData(GetLanguageDisplayName(code)));
}
if (options.Count == 0)
{
_availableLanguageCodes.Add(LocalizationService.DefaultLanguageCode);
_availableLanguageCodes.Add("en-US");
options.Add(new Dropdown.OptionData("简体中文"));
options.Add(new Dropdown.OptionData("English"));
}
_updatingLanguageDropdown = true;
language_dropdown.ClearOptions();
language_dropdown.AddOptions(options);
language_dropdown.RefreshShownValue();
_updatingLanguageDropdown = false;
SyncLanguageDropdownValue();
}
private static string GetLanguageDisplayName(string languageCode)
{
if (string.IsNullOrWhiteSpace(languageCode))
{
return "Unknown";
}
if (languageCode.StartsWith("zh", System.StringComparison.OrdinalIgnoreCase))
{
return "简体中文";
}
if (languageCode.StartsWith("en", System.StringComparison.OrdinalIgnoreCase))
{
return "English";
}
return languageCode;
}
private void SyncLanguageDropdownValue()
{
if (language_dropdown == null || _availableLanguageCodes.Count == 0)
{
return;
}
string currentCode = LocalizationService.CurrentLanguageCode;
int index = GetLanguageDropdownIndex(currentCode);
_updatingLanguageDropdown = true;
language_dropdown.SetValueWithoutNotify(index);
language_dropdown.RefreshShownValue();
_updatingLanguageDropdown = false;
}
private int GetLanguageDropdownIndex(string languageCode)
{
for (int i = 0; i < _availableLanguageCodes.Count; i++)
{
if (string.Equals(_availableLanguageCodes[i], languageCode, System.StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
for (int i = 0; i < _availableLanguageCodes.Count; i++)
{
if (languageCode.StartsWith("zh", System.StringComparison.OrdinalIgnoreCase) &&
_availableLanguageCodes[i].StartsWith("zh", System.StringComparison.OrdinalIgnoreCase))
{
return i;
}
if (languageCode.StartsWith("en", System.StringComparison.OrdinalIgnoreCase) &&
_availableLanguageCodes[i].StartsWith("en", System.StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
return 0;
}
private void OnLanguageDropdownValueChanged(int index)
{
ResetClearupCounter();
if (_updatingLanguageDropdown || _availableLanguageCodes.Count == 0)
{
return;
}
if (index < 0 || index >= _availableLanguageCodes.Count)
{
index = 0;
}
string targetLanguageCode = _availableLanguageCodes[index];
if (IsTemporarilyUnsupportedLanguage(targetLanguageCode))
{
gNotice.error.display(LocalizationService.LocalizeLiteral("暂不支持该语言"));
SyncLanguageDropdownValue();
return;
}
LocalizationService.SetLanguage(targetLanguageCode);
}
private void HandleLanguageChanged(string languageCode)
{
SyncLanguageDropdownValue();
UpdateUserInfoDisplay();
}
private static bool IsTemporarilyUnsupportedLanguage(string languageCode)
{
return !string.IsNullOrWhiteSpace(languageCode) &&
languageCode.StartsWith("en", System.StringComparison.OrdinalIgnoreCase);
}
private void OnClearupSaveDataClicked()
{
if (clearupClickCount > 0 && Time.unscaledTime > clearupExpireTime)
{
ResetClearupCounter();
}
clearupClickCount++;
clearupExpireTime = Time.unscaledTime + clearupConfirmSeconds;
if (clearupClickCount == 1)
{
gNotice.warning.display(LocalizationService.LocalizeLiteral("此操作将重置存档"));
}
else if (clearupClickCount == 2)
{
gNotice.error.display(LocalizationService.LocalizeLiteral("再次点击以确认此危险操作"));
}
else if (clearupClickCount >= 3)
{
ResetClearupCounter();
StartCoroutine(ClearSongDataRoutine());
}
}
private IEnumerator ClearSongDataRoutine()
{
Dictionary<int, SongData> songs = new Dictionary<int, SongData>();
SongData[] runtimeSongs = RuntimeResourcesCache.LoadSongsFromPath(RuntimeResourcesPath);
if (runtimeSongs != null)
{
for (int i = 0; i < runtimeSongs.Length; i++)
{
SongData so = runtimeSongs[i];
if (so != null && !songs.ContainsKey(so.songID))
songs.Add(so.songID, so);
if ((i % 24) == 0)
yield return null;
}
}
#if UNITY_EDITOR
string[] guids = AssetDatabase.FindAssets("t:SongData", new[] { EditorAssetsPath });
if (guids != null)
{
for (int i = 0; i < guids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
SongData so = AssetDatabase.LoadAssetAtPath<SongData>(path);
if (so != null && !songs.ContainsKey(so.songID))
songs.Add(so.songID, so);
if ((i % 24) == 0)
yield return null;
}
}
#endif
int index = 0;
foreach (var kvp in songs)
{
SongData so = kvp.Value;
if (so != null)
so.ClearSaveDataAndReload();
index++;
if ((index % 16) == 0)
yield return null;
}
AllyHeroDeployLedger.EnsureInstance().ResetGrowthProgressOnly();
yield return null;
ExpBottleLedger.EnsureInstance().ResetAllToZero();
yield return null;
DushMaterialLedger.EnsureInstance().ResetAllToZero();
yield return null;
EquipmentConsumableLedger.EnsureInstance().ResetAllToZero();
yield return null;
AllyHero_SO.ClearAllEquippedSkills();
yield return null;
equipSmelt.ClearPersistentState();
yield return null;
PlayerPrefs.DeleteKey("bansonic_equipment_next_id_v1");
yield return null;
Bansonic.equipmentGenerator.ClearRuntimeGeneratedPersistence();
yield return null;
PlayerRksService.ClearPersistentState();
yield return null;
PlayerSkillService.ClearPersistentState();
yield return null;
#if UNITY_EDITOR
AssetDatabase.SaveAssets();
#endif
if (!gTransition.LoadScene("Main_main", LoadSceneMode.Single))
{
SceneManager.LoadScene("Main_main");
}
}
}