diff --git a/Assets/Plugins/Editor/Uplift/MenuItems.cs b/Assets/Plugins/Editor/Uplift/MenuItems.cs index 102bd93b..30e6a414 100644 --- a/Assets/Plugins/Editor/Uplift/MenuItems.cs +++ b/Assets/Plugins/Editor/Uplift/MenuItems.cs @@ -26,6 +26,7 @@ using UnityEngine; using Uplift.Windows; using Uplift.Common; +using Uplift.Schemas; using Uplift.Updating; using Uplift.Export; @@ -112,7 +113,7 @@ private static void InstallDependencies() [MenuItem("Tools/Uplift/Show Update Window (experimental)", true, 4)] private static bool EnableShowUpdateWindow() { - return UpliftPreferences.UseExperimental(); + return UpliftPreferences.FromDefaultFile().UseExperimentalFeatures; } [MenuItem("Tools/Uplift/Show Update Window (experimental)", false, 4)] @@ -125,7 +126,7 @@ private static void ShowUpdateWindow() [MenuItem("Tools/Uplift/Edit Upfile (experimental)", true, 101)] private static bool EnableEditUpfile() { - return UpliftPreferences.UseExperimental(); + return UpliftPreferences.FromDefaultFile().UseExperimentalFeatures; } [MenuItem("Tools/Uplift/Edit Upfile (experimental)", false, 101)] diff --git a/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftSettings_Upset.cs b/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftPreferences_UpliftSettings_Upset.cs similarity index 93% rename from Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftSettings_Upset.cs rename to Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftPreferences_UpliftSettings_Upset.cs index a67c935e..4e2f4600 100644 --- a/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftSettings_Upset.cs +++ b/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftPreferences_UpliftSettings_Upset.cs @@ -1,31 +1,7 @@ -// --- BEGIN LICENSE BLOCK --- -/* - * Copyright (c) 2017-present WeWantToKnow AS - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -// --- END LICENSE BLOCK --- - -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // Ce code a été généré par un outil. -// Version du runtime :2.0.50727.8825 +// Version du runtime :2.0.50727.8937 // // Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si // le code est régénéré. @@ -748,6 +724,69 @@ public string Location { } } + /// + [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] + [System.SerializableAttribute()] + [System.Diagnostics.DebuggerStepThroughAttribute()] + [System.ComponentModel.DesignerCategoryAttribute("code")] + [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] + public partial class UpliftPreferences { + + private bool useExperimentalFeaturesField; + + private bool trustUnknowCertificatesField; + + private bool useGithubProxyField; + + private string githubProxyUrlField; + + public UpliftPreferences() { + this.useExperimentalFeaturesField = false; + this.trustUnknowCertificatesField = false; + this.useGithubProxyField = false; + } + + /// + public bool UseExperimentalFeatures { + get { + return this.useExperimentalFeaturesField; + } + set { + this.useExperimentalFeaturesField = value; + } + } + + /// + public bool TrustUnknowCertificates { + get { + return this.trustUnknowCertificatesField; + } + set { + this.trustUnknowCertificatesField = value; + } + } + + /// + public bool UseGithubProxy { + get { + return this.useGithubProxyField; + } + set { + this.useGithubProxyField = value; + } + } + + /// + public string GithubProxyUrl { + get { + return this.githubProxyUrlField; + } + set { + this.githubProxyUrlField = value; + } + } + } + /// [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] diff --git a/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftSettings_Upset.cs.meta b/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftPreferences_UpliftSettings_Upset.cs.meta similarity index 69% rename from Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftSettings_Upset.cs.meta rename to Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftPreferences_UpliftSettings_Upset.cs.meta index 8d697798..e0dc229e 100644 --- a/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftSettings_Upset.cs.meta +++ b/Assets/Plugins/Editor/Uplift/Schemas/Common_RepositoryTypes_Upbring_Upfile_UpliftPreferences_UpliftSettings_Upset.cs.meta @@ -1,7 +1,7 @@ fileFormatVersion: 2 -guid: 5339aac872f92114eae5f79f9e64ef45 -timeCreated: 1511774104 -licenseType: Free +guid: c817bfdf6649fc748b8ab375cc885c59 +timeCreated: 1544201972 +licenseType: Pro MonoImporter: serializedVersion: 2 defaultReferences: [] diff --git a/Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd b/Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd new file mode 100644 index 00000000..12036dbd --- /dev/null +++ b/Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + diff --git a/Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd.meta b/Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd.meta new file mode 100644 index 00000000..61a9ade2 --- /dev/null +++ b/Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d64072a15c12ee34d9059f9af27167f8 +timeCreated: 1544201972 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Editor/Uplift/UpliftPreferences.cs b/Assets/Plugins/Editor/Uplift/UpliftPreferences.cs index bbb78eb1..bb6b8df8 100644 --- a/Assets/Plugins/Editor/Uplift/UpliftPreferences.cs +++ b/Assets/Plugins/Editor/Uplift/UpliftPreferences.cs @@ -23,101 +23,84 @@ // --- END LICENSE BLOCK --- using System; -using System.Text.RegularExpressions; -using UnityEditor; +using System.IO; +using System.Xml.Serialization; using UnityEngine; +using Uplift.Common; -namespace Uplift +namespace Uplift.Schemas { - public class UpliftPreferences : MonoBehaviour - { - private static bool prefsLoaded = false; - private static readonly string useExperimentalFeaturesKey = "UpliftExperimentalFeatures"; - private static readonly string trustUnknownCertificatesKey = "UpliftUnknownCertificates"; - private static readonly string githubProxyUseKey = "UpliftGithubProxyUseKey"; - private static readonly string githubProxyUrlKey = "UpliftGithubProxyUrlKey"; + public partial class UpliftPreferences + { + public static readonly string folderName = ".uplift"; + public static readonly string defaultFileName = "preferences.xml"; - private static bool useExperimentalFeatures; - private static bool trustUnknownCertificates; - private static bool useGithubProxy; - private static string githubProxyUrl; + public static UpliftPreferences FromDefaultFile() + { + UpliftPreferences result; + //if (TryLoadFromFile(GetProjectLocation(), out result)) + // return result; + if (TryLoadFromFile(GetGlobalLocation(), out result)) + return result; - [PreferenceItem("Uplift")] - public static void PreferencesGUI() - { - EnsurePrefsLoaded(); - EditorGUILayout.LabelField("SSL Certificates:", EditorStyles.boldLabel); - EditorGUILayout.HelpBox("Uplift uses SSL certificates when updating itself, as it fetches its update from Github with HTTPS", MessageType.Info); - EditorGUILayout.HelpBox( - "Unknown certificates could be the result of our registered certificates being outdated or the result of a potential attack. Trusting unknown certificates could lead to security breaches. Use at your own risk!", - MessageType.Warning - ); - trustUnknownCertificates = EditorGUILayout.Toggle("Trust unknown certificates", trustUnknownCertificates); + return new UpliftPreferences(); + } - EditorGUILayout.LabelField("Experimental features:", EditorStyles.boldLabel); - EditorGUILayout.HelpBox( - "Experimental features are not thoroughly tested and could induce bugs. Use at your own risk!", - MessageType.Warning - ); - useExperimentalFeatures = EditorGUILayout.Toggle("Use experimental features", useExperimentalFeatures); + public static string GetGlobalLocation() + { + string sourceDir = System.IO.Path.Combine(GetHomePath(), folderName); + return System.IO.Path.Combine(sourceDir, defaultFileName); + } - EditorGUILayout.LabelField("Github related:", EditorStyles.boldLabel); - EditorGUILayout.HelpBox( - "Github does not support TLS versions 1.0 and 1.1 as of 22/02/2018, and TLS 1.2 is available in Unity only with Mono 4.8. If you want to use a Github Repository, it will need to be behind a proxy", - MessageType.Info - ); - useGithubProxy = EditorGUILayout.Toggle("Use Github proxy", useGithubProxy); - GUI.enabled = useGithubProxy; - if (useGithubProxy) - EditorGUILayout.HelpBox( - "The proxy URL will replace 'https://api.github.com'", - MessageType.Info - ); - githubProxyUrl = EditorGUILayout.TextField("Proxy URL", githubProxyUrl); - githubProxyUrl = Regex.Replace(githubProxyUrl, "/$", ""); - GUI.enabled = true; + public static string GetProjectLocation() + { + throw new NotImplementedException(); + } - if (GUI.changed) - { - EditorPrefs.SetBool(useExperimentalFeaturesKey, useExperimentalFeatures); - EditorPrefs.SetBool(trustUnknownCertificatesKey, trustUnknownCertificates); - EditorPrefs.SetBool(githubProxyUseKey, useGithubProxy); - EditorPrefs.SetString(githubProxyUrlKey, githubProxyUrl); - } - } + public static bool TryLoadFromFile(string source, out UpliftPreferences result) + { + result = new UpliftPreferences(); - private static void EnsurePrefsLoaded() - { - if (!prefsLoaded) - { - useExperimentalFeatures = EditorPrefs.GetBool(useExperimentalFeaturesKey, false); - trustUnknownCertificates = EditorPrefs.GetBool(trustUnknownCertificatesKey, false); - useGithubProxy = EditorPrefs.GetBool(githubProxyUseKey, false); - githubProxyUrl = EditorPrefs.GetString(githubProxyUrlKey, ""); - prefsLoaded = true; - } - } + if (!File.Exists(source)) + { + Debug.Log("No preferences file at " + source); + return false; + } - public static bool UseExperimental() - { - EnsurePrefsLoaded(); - return EditorPrefs.GetBool(useExperimentalFeaturesKey, false); - } + XmlSerializer deserializer = new XmlSerializer(typeof(UpliftPreferences)); - public static bool TrustUnknownCertificates() - { - EnsurePrefsLoaded(); - return EditorPrefs.GetBool(trustUnknownCertificatesKey, false); - } + using (FileStream fs = new FileStream(source, FileMode.Open)) + { + try + { + result = deserializer.Deserialize(fs) as UpliftPreferences; + } + catch (InvalidOperationException) + { + Debug.LogError(string.Format("Global Override file at {0} is not well formed", source)); + return false; + } - public static string UseGithubProxy(string url) - { - EnsurePrefsLoaded(); - if (!useGithubProxy || string.IsNullOrEmpty(githubProxyUrl)) - return url; + return true; + } - Debug.Log("Proxying github api with " + githubProxyUrl); - return url.Replace("https://api.github.com", githubProxyUrl); - } - } + } + + public static string GetHomePath() + { + return (Environment.OSVersion.Platform == PlatformID.Unix || + Environment.OSVersion.Platform == PlatformID.MacOSX) + ? Environment.GetEnvironmentVariable("HOME") + : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); + } + + public string GetProxiedUrl(string url) + { + if (!UseGithubProxy || string.IsNullOrEmpty(GithubProxyUrl)) + return url; + + Debug.Log("Proxying github api with " + GithubProxyUrl); + return url.Replace("https://api.github.com", GithubProxyUrl); + } + } } diff --git a/Assets/Plugins/Editor/Uplift/Utils/GitHub.cs b/Assets/Plugins/Editor/Uplift/Utils/GitHub.cs index 93960a44..d0eabfe8 100644 --- a/Assets/Plugins/Editor/Uplift/Utils/GitHub.cs +++ b/Assets/Plugins/Editor/Uplift/Utils/GitHub.cs @@ -32,6 +32,7 @@ using System.Security.Cryptography.X509Certificates; using System.Text; using UnityEngine; +using Uplift.Schemas; namespace Uplift.GitHubModule { @@ -249,7 +250,7 @@ public class GitHub public static IEnumerator LoadReleases(string url, string authToken = null) { - string proxiedUrl = UpliftPreferences.UseGithubProxy(url); + string proxiedUrl = UpliftPreferences.FromDefaultFile().GetProxiedUrl(url); WWW www = string.IsNullOrEmpty(authToken) ? new WWW (proxiedUrl) : @@ -346,7 +347,7 @@ SslPolicyErrors sslPolicyErrors X509Certificate[] githubCertificates = GetCertificates(); - if(!(githubCertificates.Any(cert => cert.GetCertHashString() == certificate.GetCertHashString()) || UpliftPreferences.TrustUnknownCertificates())) + if(!(githubCertificates.Any(cert => cert.GetCertHashString() == certificate.GetCertHashString()) || UpliftPreferences.FromDefaultFile().TrustUnknowCertificates)) { Debug.LogErrorFormat("The received certificate ({0}) is not known by Uplift. We cannot download the update package. You could update Uplift manually, or go to Preferences and set 'Trust unknown certificates' to true.", certificate.GetCertHashString()); Debug.Log("Known certificates are:"); @@ -379,7 +380,7 @@ public static Stream GetAssetStream(GitHubAsset asset) } public static Stream GetAssetStream(GitHubAsset asset, string token) { - string proxiedUrl = UpliftPreferences.UseGithubProxy(asset.apiURL); + string proxiedUrl = UpliftPreferences.FromDefaultFile().GetProxiedUrl(asset.apiURL); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxiedUrl); request.Method = "GET"; @@ -400,7 +401,7 @@ public static Stream GetAssetStream(GitHubAsset asset, string token) address = new Uri(response.GetResponseHeader("Location")); } - request = (HttpWebRequest)WebRequest.Create(UpliftPreferences.UseGithubProxy(address.OriginalString)); + request = (HttpWebRequest)WebRequest.Create(UpliftPreferences.FromDefaultFile().GetProxiedUrl(address.OriginalString)); request.Method = "GET"; request.UserAgent = "Uplift GithubRepository/1.0 - https://github.com/DragonBox/uplift"; HttpWebResponse finalResponse = (HttpWebResponse)request.GetResponse();