Skip to content

Commit

Permalink
update third party
Browse files Browse the repository at this point in the history
  • Loading branch information
elringus committed Feb 5, 2018
1 parent a2352a1 commit 126bd28
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 100 deletions.
191 changes: 100 additions & 91 deletions Assets/ThirdParty/UnityCommon/Editor/PackageExporter.cs
Original file line number Diff line number Diff line change
@@ -1,106 +1,115 @@
// Copyright 2012-2017 Elringus (Artyom Sovetnikov). All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UnityCommon
[InitializeOnLoad]
public class PackageExporter : EditorWindow
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class PackageExporter : EditorWindow
protected static string PackageName { get { return PlayerPrefs.GetString(PREFS_PREFIX + "PackageName"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "PackageName", value); } }
protected static string Copyright { get { return PlayerPrefs.GetString(PREFS_PREFIX + "Copyright"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "Copyright", value); } }
protected static string AssetsPath { get { return "Assets/" + PackageName; } }
protected static string OutputPath { get { return PlayerPrefs.GetString(PREFS_PREFIX + "OutputPath"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "OutputPath", value); } }
protected static string OutputFileName { get { return PackageName; } }
protected static string NamespaceToWrap { get { return PlayerPrefs.GetString(PREFS_PREFIX + "NamespaceToWrap"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "NamespaceToWrap", value); } }
protected static bool IsReadyToExport { get { return !string.IsNullOrEmpty(OutputPath) && !string.IsNullOrEmpty(OutputFileName); } }

private const string SKIP_WRAP_TERM = "PackageExporter: SkipWrap";
private const string PREFS_PREFIX = "PackageExporter.";
private const string TAB_CHARS = " ";

private static Dictionary<string, string> modifiedScripts = new Dictionary<string, string>();

private void Awake ()
{
protected static string PackageName { get { return PlayerPrefs.GetString(PREFS_PREFIX + "PackageName"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "PackageName", value); } }
protected static string Copyright { get { return PlayerPrefs.GetString(PREFS_PREFIX + "Copyright"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "Copyright", value); } }
protected static string AssetsPath { get { return "Assets/" + PackageName; } }
protected static string OutputPath { get { return PlayerPrefs.GetString(PREFS_PREFIX + "OutputPath"); } set { PlayerPrefs.SetString(PREFS_PREFIX + "OutputPath", value); } }
protected static string OutputFileName { get { return PackageName; } }
protected static string NamespaceToWrap { get { return PackageName; } }
protected static bool IsReadyToExport { get { return !string.IsNullOrEmpty(OutputPath) && !string.IsNullOrEmpty(OutputFileName); } }

private const string PREFS_PREFIX = "PackageExporter.";
private const string TAB_CHARS = " ";

private static Dictionary<string, string> modifiedScripts = new Dictionary<string, string>();

private void Awake ()
{
if (string.IsNullOrEmpty(PackageName))
PackageName = Application.productName;
}

[MenuItem("Edit/Project Settings/Package Exporter")]
private static void OpenSettingsWindow ()
{
var window = GetWindow<PackageExporter>();
window.Show();
}

[MenuItem("Assets/+ Export Package", priority = 20)]
private static void ExportPackage ()
{
if (IsReadyToExport)
Export();
}

[MenuItem("Assets/+ Export Package (Wrap)", priority = 20)]
private static void ExportPackageStore ()
{
if (IsReadyToExport)
Export(true);
}

private void OnGUI ()
if (string.IsNullOrEmpty(PackageName))
PackageName = Application.productName;
}

[MenuItem("Edit/Project Settings/Package Exporter")]
private static void OpenSettingsWindow ()
{
var window = GetWindow<PackageExporter>();
window.Show();
}

[MenuItem("Assets/+ Export Package", priority = 20)]
private static void ExportPackage ()
{
if (IsReadyToExport)
Export();
}

[MenuItem("Assets/+ Export Package (Wrap)", priority = 20)]
private static void ExportPackageWrapped ()
{
if (IsReadyToExport)
Export(true);
}

private void OnGUI ()
{
EditorGUILayout.LabelField("Package Exporter Settings", EditorStyles.boldLabel);
EditorGUILayout.HelpBox("Settings are stored in editor's PlayerPrefs and won't be exposed in builds or project assets.", MessageType.Info);
EditorGUILayout.Space();
PackageName = EditorGUILayout.TextField("Package Name", PackageName);
Copyright = EditorGUILayout.TextField("Copyright Notice", Copyright);
NamespaceToWrap = EditorGUILayout.TextField("Namespace", NamespaceToWrap);
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField("Package Exporter Settings", EditorStyles.boldLabel);
EditorGUILayout.LabelField("Settings are stored in editor's PlayerPrefs and won't be exposed in builds or project assets.", EditorStyles.miniLabel);
EditorGUILayout.Space();
PackageName = EditorGUILayout.TextField("Package Name", PackageName);
Copyright = EditorGUILayout.TextField("Copyright Notice", Copyright);
using (new EditorGUILayout.HorizontalScope())
{
OutputPath = EditorGUILayout.TextField("Output Path", OutputPath);
if (GUILayout.Button("Select", EditorStyles.miniButton, GUILayout.Width(65)))
OutputPath = EditorUtility.OpenFolderPanel("Output Path", "", "");
}
OutputPath = EditorGUILayout.TextField("Output Path", OutputPath);
if (GUILayout.Button("Select", EditorStyles.miniButton, GUILayout.Width(65)))
OutputPath = EditorUtility.OpenFolderPanel("Output Path", "", "");
}

private static void Export (bool wrapNamespace = false)
}

private static void Export (bool wrapNamespace = false)
{
modifiedScripts.Clear();

var needToModify = !string.IsNullOrEmpty(NamespaceToWrap) || !string.IsNullOrEmpty(Copyright);

if (needToModify)
{
modifiedScripts.Clear();

if (!string.IsNullOrEmpty(NamespaceToWrap))
foreach (var path in AssetDatabase.GetAllAssetPaths())
{
foreach (var path in AssetDatabase.GetAllAssetPaths())
if (!path.StartsWith(AssetsPath)) continue;
if (!path.EndsWith(".cs")) continue;

var fullpath = Application.dataPath.Replace("Assets", "") + path;
var originalScriptText = File.ReadAllText(fullpath, Encoding.UTF8);

if (originalScriptText.Contains(SKIP_WRAP_TERM)) continue;

string scriptText = string.Empty;
var isImportedScript = path.Contains("ThirdParty");

var copyright = isImportedScript || string.IsNullOrEmpty(Copyright) ? string.Empty : "// " + Copyright;
if (!string.IsNullOrEmpty(copyright) && (!isImportedScript || wrapNamespace))
scriptText += copyright + Environment.NewLine + Environment.NewLine;

if (!string.IsNullOrEmpty(NamespaceToWrap))
{
if (!path.StartsWith(AssetsPath)) continue;
if (!path.EndsWith(".cs")) continue;

var fullpath = Application.dataPath.Replace("Assets", "") + path;
var originalScriptText = File.ReadAllText(fullpath, Encoding.UTF8);

string scriptText = string.Empty;
var isImportedScript = path.Contains("ThirdParty");
var copyright = isImportedScript || string.IsNullOrEmpty(Copyright) ? string.Empty : "// " + Copyright;
if (!isImportedScript || wrapNamespace) scriptText += copyright + Environment.NewLine + Environment.NewLine + "namespace " + NamespaceToWrap + Environment.NewLine + "{" + Environment.NewLine;
if (!isImportedScript || wrapNamespace) scriptText += "namespace " + NamespaceToWrap + Environment.NewLine + "{" + Environment.NewLine;
scriptText += isImportedScript ? originalScriptText : TAB_CHARS + originalScriptText.Replace(Environment.NewLine, Environment.NewLine + TAB_CHARS);
if (!isImportedScript || wrapNamespace) scriptText += Environment.NewLine + "}" + Environment.NewLine;
File.WriteAllText(fullpath, scriptText, Encoding.UTF8);

modifiedScripts.Add(fullpath, originalScriptText);
}
else scriptText += originalScriptText;

File.WriteAllText(fullpath, scriptText, Encoding.UTF8);

modifiedScripts.Add(fullpath, originalScriptText);
}

AssetDatabase.ExportPackage(AssetsPath, OutputPath + "/" + OutputFileName + ".unitypackage", ExportPackageOptions.Recurse);

if (!string.IsNullOrEmpty(NamespaceToWrap))
{
foreach (var modifiedScript in modifiedScripts)
File.WriteAllText(modifiedScript.Key, modifiedScript.Value, Encoding.UTF8);
}
}

AssetDatabase.ExportPackage(AssetsPath, OutputPath + "/" + OutputFileName + ".unitypackage", ExportPackageOptions.Recurse);

if (needToModify)
{
foreach (var modifiedScript in modifiedScripts)
File.WriteAllText(modifiedScript.Key, modifiedScript.Value, Encoding.UTF8);
}
}

}
2 changes: 1 addition & 1 deletion Assets/ThirdParty/UnityCommon/Runtime/FpsDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2017 Elringus (Artyom Sovetnikov). All Rights Reserved.
// Copyright 2012-2018 Elringus (Artyom Sovetnikov). All Rights Reserved.

namespace UnityCommon
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/ThirdParty/UnityCommon/Runtime/SceneSwitcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2017 Elringus (Artyom Sovetnikov). All Rights Reserved.
// Copyright 2012-2018 Elringus (Artyom Sovetnikov). All Rights Reserved.

namespace UnityCommon
{
Expand Down
53 changes: 46 additions & 7 deletions Assets/ThirdParty/UnityCommon/Runtime/Utils/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2017 Elringus (Artyom Sovetnikov). All Rights Reserved.
// Copyright 2012-2018 Elringus (Artyom Sovetnikov). All Rights Reserved.

namespace UnityCommon
{
Expand Down Expand Up @@ -142,6 +142,17 @@ public static string[] SplitByNewLine (this string content)
return content.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
}

/// <summary>
/// Removes mathing trailing string.
/// </summary>
public static string TrimEnd (this string source, string value)
{
if (!source.EndsWithFast(value))
return source;

return source.Remove(source.LastIndexOf(value));
}

/// <summary>
/// Checks whether string is null, empty or consists of whitespace chars.
/// </summary>
Expand All @@ -150,18 +161,46 @@ public static bool IsNullEmptyOrWhiteSpace (string content)
if (String.IsNullOrEmpty(content))
return true;

return String.IsNullOrEmpty(content.Trim());
return String.IsNullOrEmpty(content.TrimFull());
}

/// <summary>
/// Removes mathing trailing string.
/// Performes <see cref="string.Trim"/> additionally removing any BOM and other service symbols.
/// </summary>
public static string TrimEnd (this string source, string value)
public static string TrimFull (this string source)
{
if (!source.EndsWithFast(value))
return source;
#if UNITY_WEBGL // WebGL build under .NET 4.6 fails when using Trim with UTF-8 chars. (should be fixed in Unity 2018.1)
var whitespaceChars = new System.Collections.Generic.List<char> {
'\u0009','\u000A','\u000B','\u000C','\u000D','\u0020','\u0085','\u00A0',
'\u1680','\u2000','\u2001','\u2002','\u2003','\u2004','\u2005','\u2006',
'\u2007','\u2008','\u2009','\u200A','\u2028','\u2029','\u202F','\u205F',
'\u3000','\uFEFF','\u200B',
};

// Trim start.
if (string.IsNullOrEmpty(source)) return source;
var c = source[0];
while (whitespaceChars.Contains(c))
{
if (source.Length <= 1) return string.Empty;
source = source.Substring(1);
c = source[0];
}

return source.Remove(source.LastIndexOf(value));
// Trim end.
if (string.IsNullOrEmpty(source)) return source;
c = source[source.Length - 1];
while (whitespaceChars.Contains(c))
{
if (source.Length <= 1) return string.Empty;
source = source.Substring(0, source.Length - 1);
c = source[source.Length - 1];
}

return source;
#else
return source.Trim().Trim(new char[] { '\uFEFF', '\u200B' });
#endif
}
}

Expand Down

0 comments on commit 126bd28

Please sign in to comment.