コード例 #1
0
ファイル: Settings.cs プロジェクト: freezy/vpdb-agent
		protected internal static Settings Copy(Settings from, Settings to)
		{
			to.ApiKey = from.ApiKey;
			to.AuthUser = from.AuthUser;
			to.AuthPass = from.AuthPass;
			to.Endpoint = from.Endpoint;
			to.PbxFolder = from.PbxFolder;
			to.SyncStarred = from.SyncStarred;
			to.MinimizeToTray = from.MinimizeToTray;
			to.StartWithWindows = from.StartWithWindows;
			to.ReformatXml = from.ReformatXml;
			to.XmlFile = new Dictionary<Platform.PlatformType, string>(from.XmlFile);
			to.DownloadOnStartup = from.DownloadOnStartup;
			to.PatchTableScripts = from.PatchTableScripts;
			to.DownloadOrientation = from.DownloadOrientation;
			to.DownloadOrientationFallback = from.DownloadOrientationFallback;
			to.DownloadLighting = from.DownloadLighting;
			to.DownloadLightingFallback = from.DownloadLightingFallback;
			to.WindowPosition = from.WindowPosition;
			return to;
		}
コード例 #2
0
ファイル: SettingsManager.cs プロジェクト: freezy/vpdb-agent
		public IObservable<Settings> Save(Settings settings)
		{
			if (!settings.IsValidated) {
				throw new InvalidOperationException("Settings must be validated before saved.");
			}
			var result = new Subject<Settings>();

			Task.Run(async () => {
				Settings.Copy(settings, Settings);
				await Settings.WriteToStorage(_storage);

				// handle startup settings todo test!
				if (Settings.StartWithWindows)
				{
					var linkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Start Menu", "Programs", "VPDB", "VPDB Agent.lnk");
					string cmd;
					if (File.Exists(linkPath)) {
						var lnk = new ShellShortcut(linkPath);
						cmd = $"cmd /c \"cd /d {lnk.WorkingDirectory} && {lnk.Path} {lnk.Arguments} --process-start-args \\\"--minimized\\\"\"";
					} else {
						cmd = File.Exists(linkPath) ? linkPath : Assembly.GetEntryAssembly().Location;
					}
					_registryKey.SetValue("VPDB Agent", cmd);
				} else {
					if (_registryKey.GetValue("VPDB Agent") != null) {
						_registryKey.DeleteValue("VPDB Agent");
					}
				}

				result.OnNext(Settings);
				result.OnCompleted();
			});

			CanCancel = true;
			return result;
		}
コード例 #3
0
ファイル: SettingsManager.cs プロジェクト: freezy/vpdb-agent
		public IObservable<Settings> SaveInternal(Settings settings)
		{
			var result = new Subject<Settings>();
			Task.Run(async () => {
				Settings.Copy(settings, Settings);
				await Settings.WriteInternalToStorage(_storage);
				result.OnNext(settings);
				result.OnCompleted();
			});
			return result;
		}
コード例 #4
0
ファイル: SettingsManager.cs プロジェクト: freezy/vpdb-agent
		public async Task<Dictionary<string, string>> Validate(Settings settings, IMessageManager messageManager = null)
		{
			_logger.Info("Validating settings...");
			var errors = new Dictionary<string, string>();

			// pinballx folder
			if (string.IsNullOrEmpty(settings.PbxFolder)) {
				errors.Add("PbxFolder", "The folder where PinballX is installed must be set.");
			} else if (!Directory.Exists(settings.PbxFolder) || !Directory.Exists(settings.PbxFolder + @"\Config")) {
				errors.Add("PbxFolder", "The folder \"" + settings.PbxFolder + "\" is not a valid PinballX folder.");
			}

			// network params
			if (string.IsNullOrEmpty(settings.ApiKey)) {
				errors.Add("ApiKey", "The API key is mandatory and needed in order to communicate with VPDB.");
			}
			if (string.IsNullOrEmpty(settings.Endpoint)) {
				errors.Add("Endpoint", "The endpoint is mandatory. In doubt, put \"https://vpdb.io\".");
			}

			// xml file name
			var badFilenameChars = new Regex("[\\\\" + Regex.Escape(new string(Path.GetInvalidPathChars())) + "]");
			var filename = settings.XmlFile[Platform.PlatformType.VP];
			if (string.IsNullOrWhiteSpace(filename)) {
				errors.Add("XmlFileVP", "You need to provide a file name for the XML database.");
			} else if (badFilenameChars.IsMatch(filename)) {
				errors.Add("XmlFileVP", "That doesn't look like a valid file name!");
			} else if (filename.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) {
				errors.Add("XmlFileVP", "No need to provide the .xml extension, we'll do that!");
			}

			// test params if set
			if (!string.IsNullOrEmpty(settings.ApiKey) && !string.IsNullOrEmpty(settings.Endpoint)) {
				try {
					var handler = new AuthenticatedHttpClientHandler(settings.ApiKey, settings.AuthUser, settings.AuthPass);
					var client = new HttpClient(handler) {BaseAddress = new Uri(settings.Endpoint)};
					var s = new RefitSettings { JsonSerializerSettings = new JsonSerializerSettings {ContractResolver = new SnakeCasePropertyNamesContractResolver()} };
					var api = RestService.For<IVpdbApi>(client, s);
					var user = await api.GetProfile().SubscribeOn(Scheduler.Default).ToTask();

					_logger.Info("Logged as <{0}>", user.Email);
					OnValidationResult(user);

				} catch (ApiException e) {
					HandleApiError(errors, e);
					OnValidationResult(null);
					messageManager?.LogApiError(e, "Error while logging in");

				} catch (Exception e) {
					errors.Add("ApiKey", e.Message);
					OnValidationResult(null);
					messageManager?.LogError(e, "Error while logging in");
				}
			}
			settings.IsValidated = errors.Count == 0;

			return errors;
		}