|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using JahroConsole.Core.Data; |
| 4 | +using JahroConsole.Core.Context; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEngine; |
| 7 | +using JahroConsole.Core.Network; |
| 8 | + |
| 9 | +namespace JahroConsole.Editor |
| 10 | +{ |
| 11 | + public class JahroEditorController |
| 12 | + { |
| 13 | + public enum EditorState |
| 14 | + { |
| 15 | + Loading, |
| 16 | + Welcome, |
| 17 | + Settings, |
| 18 | + Error |
| 19 | + } |
| 20 | + |
| 21 | + public event Action<EditorState> OnStateChanged; |
| 22 | + public event Action<string> OnLoadingError; |
| 23 | + public event Action<VersionInfo> OnVersionChecked; |
| 24 | + public event Action<KeyValidator.ValidateKeyResponse> OnApiKeyValidated; |
| 25 | + public event Action OnApiKeyReset; |
| 26 | + public event Action<bool, string> OnValidationComplete; |
| 27 | + |
| 28 | + private EditorState _currentState = EditorState.Loading; |
| 29 | + private JahroProjectSettings _projectSettings; |
| 30 | + private KeyValidator.ValidateKeyResponse _apiKeyValidation; |
| 31 | + private VersionInfo _versionInfo; |
| 32 | + |
| 33 | + public EditorState CurrentState => _currentState; |
| 34 | + public JahroProjectSettings ProjectSettings => _projectSettings; |
| 35 | + public KeyValidator.ValidateKeyResponse ApiKeyValidation => _apiKeyValidation; |
| 36 | + public VersionInfo VersionInfo => _versionInfo; |
| 37 | + |
| 38 | + public async Task InitializeAsync() |
| 39 | + { |
| 40 | + _projectSettings = JahroProjectSettings.Load(); |
| 41 | + _projectSettings.OnSettingsChanged += SaveProjectSettings; |
| 42 | + |
| 43 | + SetState(EditorState.Loading); |
| 44 | + await PerformInitializationAsync(); |
| 45 | + } |
| 46 | + |
| 47 | + public void Cleanup() |
| 48 | + { |
| 49 | + if (_projectSettings != null) |
| 50 | + { |
| 51 | + _projectSettings.OnSettingsChanged -= SaveProjectSettings; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void ResetApiKey() |
| 56 | + { |
| 57 | + if (_projectSettings != null) |
| 58 | + { |
| 59 | + _projectSettings.APIKey = string.Empty; |
| 60 | + _apiKeyValidation = null; |
| 61 | + OnApiKeyReset?.Invoke(); |
| 62 | + SetState(EditorState.Welcome); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public async Task CheckAndValidateExistingApiKeyAsync(string apiKey) |
| 67 | + { |
| 68 | + if (string.IsNullOrEmpty(apiKey)) |
| 69 | + { |
| 70 | + SetState(EditorState.Welcome); |
| 71 | + OnValidationComplete?.Invoke(false, $"API key is required"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + try |
| 76 | + { |
| 77 | + await KeyValidator.EditorSend(apiKey, |
| 78 | + (response) => HandleApiKeyValidation(response), |
| 79 | + (error) => |
| 80 | + { |
| 81 | + SetState(EditorState.Welcome); |
| 82 | + OnValidationComplete?.Invoke(false, $"API key error: {error.message}"); |
| 83 | + } |
| 84 | + ); |
| 85 | + } |
| 86 | + catch (Exception ex) |
| 87 | + { |
| 88 | + SetState(EditorState.Welcome); |
| 89 | + OnLoadingError?.Invoke(ex.Message); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private async Task PerformInitializationAsync() |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + await CheckVersionAsync(); |
| 98 | + await CheckAndValidateExistingApiKeyAsync(_projectSettings?.APIKey); |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + Debug.LogError(ex.Message); |
| 103 | + OnLoadingError?.Invoke(ex.Message); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private async Task CheckVersionAsync() |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + await VersionChecker.EditorSend(JahroEditorView.isFreshInstall, |
| 112 | + (response) => |
| 113 | + { |
| 114 | + _versionInfo = response; |
| 115 | + OnVersionChecked?.Invoke(response); |
| 116 | + }, |
| 117 | + (error) => |
| 118 | + { |
| 119 | + _versionInfo = null; |
| 120 | + OnLoadingError?.Invoke(error.message); |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | + catch (Exception ex) |
| 125 | + { |
| 126 | + OnLoadingError?.Invoke(ex.Message); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void HandleApiKeyValidation(KeyValidator.ValidateKeyResponse response) |
| 131 | + { |
| 132 | + if (response != null && response.success) |
| 133 | + { |
| 134 | + _apiKeyValidation = response; |
| 135 | + _projectSettings.APIKey = response.apiKey; |
| 136 | + SetState(EditorState.Settings); |
| 137 | + OnApiKeyValidated?.Invoke(response); |
| 138 | + OnValidationComplete?.Invoke(true, null); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + string errorMessage = response?.message ?? "Invalid API key"; |
| 143 | + OnLoadingError?.Invoke(errorMessage); |
| 144 | + OnValidationComplete?.Invoke(false, errorMessage); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void SetState(EditorState newState) |
| 149 | + { |
| 150 | + if (_currentState != newState) |
| 151 | + { |
| 152 | + _currentState = newState; |
| 153 | + OnStateChanged?.Invoke(newState); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private void SaveProjectSettings() |
| 158 | + { |
| 159 | + if (_projectSettings != null) |
| 160 | + { |
| 161 | + EditorUtility.SetDirty(_projectSettings); |
| 162 | + AssetDatabase.SaveAssets(); |
| 163 | + AssetDatabase.Refresh(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments