Skip to content

Commit

Permalink
Merge pull request #63 from niezbop/feature/file_for_preferences
Browse files Browse the repository at this point in the history
Preferences: use .uplift/preferences.xml rather than EditorPrefs for storage
  • Loading branch information
niezbop authored Dec 18, 2018
2 parents 5db2206 + eec2d98 commit c4f45b0
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 119 deletions.
5 changes: 3 additions & 2 deletions Assets/Plugins/Editor/Uplift/MenuItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using UnityEngine;
using Uplift.Windows;
using Uplift.Common;
using Uplift.Schemas;
using Uplift.Updating;

using Uplift.Export;
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ---

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// 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é.
Expand Down Expand Up @@ -748,6 +724,69 @@ public string Location {
}
}

/// <remarks/>
[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;
}

/// <remarks/>
public bool UseExperimentalFeatures {
get {
return this.useExperimentalFeaturesField;
}
set {
this.useExperimentalFeaturesField = value;
}
}

/// <remarks/>
public bool TrustUnknowCertificates {
get {
return this.trustUnknowCertificatesField;
}
set {
this.trustUnknowCertificatesField = value;
}
}

/// <remarks/>
public bool UseGithubProxy {
get {
return this.useGithubProxyField;
}
set {
this.useGithubProxyField = value;
}
}

/// <remarks/>
public string GithubProxyUrl {
get {
return this.githubProxyUrlField;
}
set {
this.githubProxyUrlField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Assets/Plugins/Editor/Uplift/Schemas/UpliftPreferences.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:include schemaLocation="Common.xsd"/>
<xs:include schemaLocation="RepositoryTypes.xsd"/>

<xs:element name="UpliftPreferences" type="UpliftPreferences"/>

<xs:complexType name="UpliftPreferences">
<xs:sequence>
<xs:element type="xs:boolean" name="UseExperimentalFeatures" default="false" />
<xs:element type="xs:boolean" name="TrustUnknowCertificates" default="false" />
<xs:element type="xs:boolean" name="UseGithubProxy" default="false" />
<xs:element type="xs:string" name="GithubProxyUrl" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

151 changes: 67 additions & 84 deletions Assets/Plugins/Editor/Uplift/UpliftPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
9 changes: 5 additions & 4 deletions Assets/Plugins/Editor/Uplift/Utils/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Text;
using UnityEngine;
using Uplift.Schemas;

namespace Uplift.GitHubModule
{
Expand Down Expand Up @@ -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) :
Expand Down Expand Up @@ -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:");
Expand Down Expand Up @@ -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";
Expand All @@ -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();
Expand Down

0 comments on commit c4f45b0

Please sign in to comment.