Exemple #1
0
        private void Init()
        {
            SettingsManager.Instance.Load();
            LanguagesManager.Instance.SetCurrentLanguage(SettingsManager.Instance.UseLanguage);

            {
                string configPath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, "Service");
                if (!Directory.Exists(configPath))
                {
                    Directory.CreateDirectory(configPath);
                }

                _amoebaInterfaceManager = new AmoebaInterfaceManager();
                {
                    var info     = UriUtils.Parse(AmoebaEnvironment.Config.Communication.TargetUri);
                    var endpoint = new IPEndPoint(IPAddress.Parse(info.GetValue <string>("Address")), info.GetValue <int>("Port"));

                    _amoebaInterfaceManager.Connect(endpoint, CancellationToken.None);
                    _amoebaInterfaceManager.Load();
                }

                if (_amoebaInterfaceManager.Config.Core.Download.BasePath == null)
                {
                    lock (_amoebaInterfaceManager.LockObject)
                    {
                        var oldConfig = _amoebaInterfaceManager.Config;
                        _amoebaInterfaceManager.SetConfig(new ServiceConfig(new CoreConfig(oldConfig.Core.Network, new DownloadConfig(AmoebaEnvironment.Paths.DownloadsDirectoryPath, oldConfig.Core.Download.ProtectedPercentage)), oldConfig.Connection, oldConfig.Message));
                    }
                }
            }

            {
                string configPath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, "Control", "Message");
                if (!Directory.Exists(configPath))
                {
                    Directory.CreateDirectory(configPath);
                }

                _messageManager = new MessageManager(configPath, _amoebaInterfaceManager);
                _messageManager.Load();
            }

            {
                this.Title = SettingsManager.Instance.AccountSetting.ObserveProperty(n => n.DigitalSignature)
                             .Select(n => $"Amoeba {AmoebaEnvironment.Version} - {n.ToString()}").ToReadOnlyReactiveProperty().AddTo(_disposable);

                this.RelationCommand = new ReactiveCommand().AddTo(_disposable);
                this.RelationCommand.Subscribe(() => this.Relation()).AddTo(_disposable);

                this.OptionsCommand = new ReactiveCommand().AddTo(_disposable);
                this.OptionsCommand.Subscribe(() => this.Options()).AddTo(_disposable);

                this.CheckBlocksCommand = new ReactiveCommand().AddTo(_disposable);
                this.CheckBlocksCommand.Subscribe(() => this.CheckBlocks()).AddTo(_disposable);

                this.LanguageCommand = new ReactiveCommand <string>().AddTo(_disposable);
                this.LanguageCommand.Subscribe((n) => LanguagesManager.Instance.SetCurrentLanguage(n)).AddTo(_disposable);

                this.WebsiteCommand = new ReactiveCommand().AddTo(_disposable);
                this.WebsiteCommand.Subscribe(() => this.Website()).AddTo(_disposable);

                this.VersionCommand = new ReactiveCommand().AddTo(_disposable);
                this.VersionCommand.Subscribe(() => this.Version()).AddTo(_disposable);

                this.IsProgressDialogOpen = new ReactiveProperty <bool>().AddTo(_disposable);

                this.ReceivingSpeed = new ReactiveProperty <decimal>().AddTo(_disposable);
                this.SendingSpeed   = new ReactiveProperty <decimal>().AddTo(_disposable);

                this.WindowSettings = new ReactiveProperty <WindowSettings>().AddTo(_disposable);
            }

            {
                string configPath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, "View", "MainWindow");
                if (!Directory.Exists(configPath))
                {
                    Directory.CreateDirectory(configPath);
                }

                _settings = new Settings(configPath);
                int  version       = _settings.Load("Version", () => 0);
                bool isInitialized = _settings.Load("IsInitialized", () => false);
                this.WindowSettings.Value = _settings.Load(nameof(this.WindowSettings), () => new WindowSettings());
                this.DynamicOptions.SetProperties(_settings.Load(nameof(this.DynamicOptions), () => Array.Empty <DynamicOptions.DynamicPropertyInfo>()));

                if (!isInitialized)
                {
                    var cloudUri = @"https://alliance-network.cloud/amoeba/locations.php";

                    if (_dialogService.ShowDialog($"Are you sure you want to connect to \"{cloudUri}\"?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)
                    {
                        try
                        {
                            using (var httpClient = new HttpClient())
                                using (var response = httpClient.GetAsync(cloudUri).Result)
                                    using (var stream = response.Content.ReadAsStreamAsync().Result)
                                    {
                                        var list = new List <Location>();

                                        foreach (var line in JsonUtils.Load <IEnumerable <string> >(stream))
                                        {
                                            try
                                            {
                                                list.Add(AmoebaConverter.FromLocationString(line));
                                            }
                                            catch (Exception)
                                            {
                                            }
                                        }

                                        _amoebaInterfaceManager.SetCloudLocations(list);
                                    }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            {
                this.CloudControlViewModel        = new CloudControlViewModel(_amoebaInterfaceManager, _dialogService);
                this.ChatControlViewModel         = new ChatControlViewModel(_amoebaInterfaceManager, _messageManager, _dialogService);
                this.StoreControlViewModel        = new StoreControlViewModel(_amoebaInterfaceManager, _dialogService);
                this.StorePublishControlViewModel = new UploadControlViewModel(_amoebaInterfaceManager, _dialogService);
                this.SearchControlViewModel       = new SearchControlViewModel(_amoebaInterfaceManager, _messageManager, _dialogService);
                this.DownloadControlViewModel     = new DownloadControlViewModel(_amoebaInterfaceManager, _dialogService);
                this.UploadControlViewModel       = new UploadControlViewModel(_amoebaInterfaceManager, _dialogService);
            }

            {
                _watchManager = new WatchManager(_amoebaInterfaceManager, _dialogService);
            }

            {
                EventHooks.Instance.SaveEvent += this.Save;
            }

            this.Setting_TrafficView();
        }