1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4  * This file is part of OpenRA, which is free software. It is made
5  * available to you under the terms of the GNU General Public License
6  * as published by the Free Software Foundation, either version 3 of
7  * the License, or (at your option) any later version. For more
8  * information, see COPYING.
9  */
10 #endregion
11 
12 using System;
13 using System.Collections.Generic;
14 using System.ComponentModel;
15 using System.Diagnostics.CodeAnalysis;
16 using System.Globalization;
17 using System.IO;
18 using System.Linq;
19 using System.Net;
20 using OpenRA.Primitives;
21 using OpenRA.Widgets;
22 
23 namespace OpenRA.Mods.Common.Widgets.Logic
24 {
25 	[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1203:ConstantsMustAppearBeforeFields",
26 		Justification = "SystemInformation version should be defined next to the dictionary it refers to.")]
27 	public class SystemInfoPromptLogic : ChromeLogic
28 	{
29 		// Increment the version number when adding new stats
30 		const int SystemInformationVersion = 4;
31 
GetSystemInformation()32 		static Dictionary<string, Pair<string, string>> GetSystemInformation()
33 		{
34 			var lang = CultureInfo.InstalledUICulture.TwoLetterISOLanguageName;
35 			return new Dictionary<string, Pair<string, string>>()
36 			{
37 				{ "id", Pair.New("Anonymous ID", Game.Settings.Debug.UUID) },
38 				{ "platform", Pair.New("OS Type", Platform.CurrentPlatform.ToString()) },
39 				{ "os", Pair.New("OS Version", Environment.OSVersion.ToString()) },
40 				{ "x64", Pair.New("OS is 64 bit", Environment.Is64BitOperatingSystem.ToString()) },
41 				{ "x64process", Pair.New("Process is 64 bit", Environment.Is64BitProcess.ToString()) },
42 				{ "runtime", Pair.New(".NET Runtime", Platform.RuntimeVersion) },
43 				{ "gl", Pair.New("OpenGL Version", Game.Renderer.GLVersion) },
44 				{ "windowsize", Pair.New("Window Size", "{0}x{1}".F(Game.Renderer.NativeResolution.Width, Game.Renderer.NativeResolution.Height)) },
45 				{ "windowscale", Pair.New("Window Scale", Game.Renderer.NativeWindowScale.ToString("F2", CultureInfo.InvariantCulture)) },
46 				{ "uiscale", Pair.New("UI Scale", Game.Settings.Graphics.UIScale.ToString("F2", CultureInfo.InvariantCulture)) },
47 				{ "lang", Pair.New("System Language", lang) }
48 			};
49 		}
50 
ShouldShowPrompt()51 		public static bool ShouldShowPrompt()
52 		{
53 			return Game.Settings.Debug.SystemInformationVersionPrompt < SystemInformationVersion;
54 		}
55 
CreateParameterString()56 		public static string CreateParameterString()
57 		{
58 			if (!Game.Settings.Debug.SendSystemInformation)
59 				return "";
60 
61 			return "&sysinfoversion={0}&".F(SystemInformationVersion)
62 			       + GetSystemInformation()
63 				       .Select(kv => kv.Key + "=" + Uri.EscapeUriString(kv.Value.Second))
64 				       .JoinWith("&");
65 		}
66 
67 		[ObjectCreator.UseCtor]
SystemInfoPromptLogic(Widget widget, Action onComplete)68 		public SystemInfoPromptLogic(Widget widget, Action onComplete)
69 		{
70 			var sysInfoCheckbox = widget.Get<CheckboxWidget>("SYSINFO_CHECKBOX");
71 			sysInfoCheckbox.IsChecked = () => Game.Settings.Debug.SendSystemInformation;
72 			sysInfoCheckbox.OnClick = () => Game.Settings.Debug.SendSystemInformation ^= true;
73 
74 			var sysInfoData = widget.Get<ScrollPanelWidget>("SYSINFO_DATA");
75 			var template = sysInfoData.Get<LabelWidget>("DATA_TEMPLATE");
76 			sysInfoData.RemoveChildren();
77 
78 			foreach (var info in GetSystemInformation().Values)
79 			{
80 				var label = template.Clone() as LabelWidget;
81 				var text = info.First + ": " + info.Second;
82 				label.GetText = () => text;
83 				sysInfoData.AddChild(label);
84 			}
85 
86 			widget.Get<ButtonWidget>("CONTINUE_BUTTON").OnClick = () =>
87 			{
88 				Game.Settings.Debug.SystemInformationVersionPrompt = SystemInformationVersion;
89 				Game.Settings.Save();
90 				Ui.CloseWindow();
91 				onComplete();
92 			};
93 		}
94 	}
95 }
96