Skip to content

Commit

Permalink
Formatting update and nicer directory solving in export script
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubSlaby committed Mar 14, 2021
1 parent 218ef5c commit 15cdd38
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 238 deletions.
36 changes: 18 additions & 18 deletions Development/Editor/ChirpDevelopment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@ namespace WhiteSparrow.Shared.Logging
{
public static class ChirpDevelopment
{
private static string s_RootFilePathCache = null;
private static string s_RootFilePathCache;

private static string s_ChirpRepositoryRootCache;

private static DirectoryInfo s_DataPathDirectoryCache;

private static string RootFilePath
{
get
{
if (s_RootFilePathCache == null)
{
string[] search = AssetDatabase.FindAssets("ChirpDevelopmentRoot");
var search = AssetDatabase.FindAssets("ChirpDevelopmentRoot");
if (search.Length == 0)
throw new Exception("Unable to find ChirpDevelopmentRoot file. The repository structure is corrupted.");
if(search.Length > 1)
throw new Exception("Found more than one ChirpDevelopmentRoot file. The repository structure is corrupted.");
FileInfo rootFile = new FileInfo(AssetDatabase.GUIDToAssetPath(search[0]));
throw new Exception(
"Unable to find ChirpDevelopmentRoot file. The repository structure is corrupted.");
if (search.Length > 1)
throw new Exception(
"Found more than one ChirpDevelopmentRoot file. The repository structure is corrupted.");
var rootFile = new FileInfo(AssetDatabase.GUIDToAssetPath(search[0]));
s_RootFilePathCache = rootFile.Directory.FullName;
}

return s_RootFilePathCache;
}
}

private static string s_ChirpRepositoryRootCache = null;
public static string ChirpRepositoryRoot
{
get
{
if (s_ChirpRepositoryRootCache == null)
{
s_ChirpRepositoryRootCache = RootFilePath + "/../../";
}
if (s_ChirpRepositoryRootCache == null) s_ChirpRepositoryRootCache = RootFilePath + "/../../";

return s_ChirpRepositoryRootCache;
}
}

private static DirectoryInfo s_DataPathDirectoryCache;

private static DirectoryInfo DataPathDirectory
{
get
Expand All @@ -53,10 +53,10 @@ private static DirectoryInfo DataPathDirectory
return s_DataPathDirectoryCache;
}
}

public static string MakePathRelative(string input)
{
int l = 0;
var l = 0;
if (input.IndexOf(Path.DirectorySeparatorChar) != -1)
{
if (input.IndexOf(DataPathDirectory.FullName, StringComparison.Ordinal) == 0)
Expand All @@ -70,9 +70,9 @@ public static string MakePathRelative(string input)

if (l == 0)
return input;
string output = "Assets" + input.Substring(l, input.Length - l);


var output = "Assets" + input.Substring(l, input.Length - l);
return output.Replace("\\", "/").TrimEnd('/');
}
}
Expand Down
87 changes: 47 additions & 40 deletions Development/Editor/ExportPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,51 @@

namespace WhiteSparrow.Shared.Logging
{
public static class ExportPackage
{
private const string s_ContentDirectory = "WhiteSparrow/";
private const string s_ExportDirectory = "Plugins/WhiteSparrow/";

[MenuItem("Tools/Chirp Logger/Export Package", priority = 600)]
public static void ExportChirpPackage()
{
DirectoryInfo rootDirectory = new DirectoryInfo(ChirpDevelopment.ChirpRepositoryRoot);
DirectoryInfo contentDirectory = new DirectoryInfo(Path.Combine(rootDirectory.FullName, s_ContentDirectory));
DirectoryInfo targetDirectory = new DirectoryInfo(Path.Combine(Application.dataPath, s_ExportDirectory));

Debug.Log($"root: {rootDirectory.FullName}\ncontent: {contentDirectory.FullName}\ntarget: {targetDirectory.FullName}");

string from = ChirpDevelopment.MakePathRelative(contentDirectory.FullName);
string to = ChirpDevelopment.MakePathRelative(targetDirectory.FullName);
Debug.Log($"from: {from} to: {to}");
FileUtil.MoveFileOrDirectory(from, to);
AssetDatabase.Refresh();

var files = AssetDatabase.FindAssets("*", new string[] {to.TrimEnd('/')});
if (files.Length == 0)
{
EditorUtility.DisplayDialog("Chirp: Exporting Package", "Couldn't find any files to export", "Close");
return;
}
var paths = new string[files.Length];
for (int i = 0; i < files.Length; i++)
{
paths[i] = AssetDatabase.GUIDToAssetPath(files[i]);
}
AssetDatabase.ExportPackage(paths, $"Chirp_{Chirp.Version}.unitypackage", ExportPackageOptions.Recurse);


FileUtil.MoveFileOrDirectory(to, from);
AssetDatabase.Refresh();
}


}
public static class ExportPackage
{
private const string s_ContentDirectory = "WhiteSparrow/Logging";
private const string s_ExportDirectory = "Plugins/WhiteSparrow/Logging";


[MenuItem("Tools/Chirp Logger/Export Package", priority = 600)]
public static void ExportChirpPackage()
{
var rootDirectory = new DirectoryInfo(ChirpDevelopment.ChirpRepositoryRoot);
var contentDirectory = new DirectoryInfo(Path.Combine(rootDirectory.FullName, s_ContentDirectory));
var targetDirectory = new DirectoryInfo(Path.Combine(Application.dataPath, s_ExportDirectory));
if (targetDirectory.Exists)
{
EditorUtility.DisplayDialog("Chirp: Exporting Package",
$"The target packaging directory is already created - unable to move the files for packaging.\npath: {targetDirectory.FullName}",
"Ok");
return;
}

if (targetDirectory.Parent != null && !targetDirectory.Parent.Exists)
{
Directory.CreateDirectory(targetDirectory.Parent.FullName);
}


var from = ChirpDevelopment.MakePathRelative(contentDirectory.FullName);
var to = ChirpDevelopment.MakePathRelative(targetDirectory.FullName);
FileUtil.MoveFileOrDirectory(from, to);
AssetDatabase.Refresh();

var files = AssetDatabase.FindAssets("*", new[] {to.TrimEnd('/')});
if (files.Length == 0)
{
EditorUtility.DisplayDialog("Chirp: Exporting Package", "Couldn't find any files to export", "Close");
return;
}

var paths = new string[files.Length];
for (var i = 0; i < files.Length; i++) paths[i] = AssetDatabase.GUIDToAssetPath(files[i]);
AssetDatabase.ExportPackage(paths, $"Chirp_{Chirp.Version}.unitypackage", ExportPackageOptions.Recurse);
FileUtil.MoveFileOrDirectory(to, from);
AssetDatabase.Refresh();
}


}
}
60 changes: 27 additions & 33 deletions WhiteSparrow/Logging/Chirp/Channels/LogChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ namespace WhiteSparrow.Shared.Logging
{
public class LogChannel : IEquatable<LogChannel>, IEquatable<string>
{
public readonly Color color;
public readonly string id;
public readonly string name;
public readonly Color color;

public bool isFallback = false;

public LogChannel(string name) : this(name, CreateColorHash(name))
{

}

public LogChannel(string name, Color color)
{
this.id = name.ToLower();
id = name.ToLower();
this.name = name;
this.color = color;

Expand All @@ -28,21 +27,19 @@ public LogChannel(string name, Color color)

private static Color CreateColorHash(string id)
{
char[] characters = id.ToCharArray();
var characters = id.ToCharArray();
double hash = 0;
for (int i = 0; i < characters.Length; i++)
{
hash = char.GetNumericValue(characters[i]) + (((int)hash << 5) - hash);
}

float h = (float)hash % 200;
float v = (float)hash % 240;

return Color.HSVToRGB(Mathf.Abs(h)/200f,
0.9f,
Mathf.Clamp(Mathf.Abs(v)/240f, 0.7f, 1f));
for (var i = 0; i < characters.Length; i++)
hash = char.GetNumericValue(characters[i]) + (((int) hash << 5) - hash);

var h = (float) hash % 200;
var v = (float) hash % 240;

return Color.HSVToRGB(Mathf.Abs(h) / 200f,
0.9f,
Mathf.Clamp(Mathf.Abs(v) / 240f, 0.7f, 1f));
}

// custom operator to use string id for easy assignment
public static implicit operator LogChannel(string logChannel)
{
Expand Down Expand Up @@ -76,15 +73,17 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return (id != null ? id.GetHashCode() : 0);
return id != null ? id.GetHashCode() : 0;
}

#endregion

#region Static Registry

private static Dictionary<string, LogChannel> s_IdToInstanceMapping = new Dictionary<string, LogChannel>();
private static List<string> s_IdList = new List<string>();
private static readonly Dictionary<string, LogChannel> s_IdToInstanceMapping =
new Dictionary<string, LogChannel>();

private static readonly List<string> s_IdList = new List<string>();
private static string[] s_IdListCache;

private void RegisterChannel(LogChannel channel)
Expand All @@ -100,7 +99,7 @@ public static LogChannel Get(string id)
{
if (s_IdToInstanceMapping.TryGetValue(id, out var channel))
return channel;

return new LogChannel(id);
}

Expand All @@ -115,30 +114,28 @@ public static bool HasChannel(string id)
{
return s_IdList.Contains(id);
}

#endregion

#region Type Registry

private static Dictionary<Type, LogChannel> s_TypeToInstanceMapping = new Dictionary<Type, LogChannel>();
private static readonly Dictionary<Type, LogChannel> s_TypeToInstanceMapping =
new Dictionary<Type, LogChannel>();

public static LogChannel RegisterChannelTarget(Type type)
{
if (s_TypeToInstanceMapping.TryGetValue(type, out var existingChannel))
return existingChannel;

string channelName = type.Name;
LogChannel channel = Get(channelName);
var channelName = type.Name;
var channel = Get(channelName);
s_TypeToInstanceMapping.Add(type, channel);
return channel;
}

public static void RegisterChannelTarget(IEnumerable<Type> types)
{
foreach (var type in types)
{
RegisterChannelTarget(type);
}
foreach (var type in types) RegisterChannelTarget(type);
}

public static LogChannel GetForTarget(Type type)
Expand All @@ -149,8 +146,5 @@ public static LogChannel GetForTarget(Type type)
}

#endregion

}


}
1 change: 0 additions & 1 deletion WhiteSparrow/Logging/Chirp/Channels/LogChannelAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace WhiteSparrow.Shared.Logging
[AttributeUsage(AttributeTargets.Class)]
public class LogChannelAttribute : Attribute
{

}
}
Loading

0 comments on commit 15cdd38

Please sign in to comment.