Skip to content

Commit

Permalink
Update v1.0.7
Browse files Browse the repository at this point in the history
### Additions

- Added a generic extension method `Log<T>(this T obj, string format = null, IFormatProvider formatProvider = null)`, which logs `obj.ToString()` using `UnityEngine.Debug.Log()` when using the Unity Editor, `Console.WriteLine()` otherwise
- Added some IO/Directory related utility as methods in the static `DirectoryExtensions` class. NOTE: These are subject to change

### Improvements

- When using the Unity Editor, the `Window\C# Dev Tools\Manage Safety Checks` window now shows the number of method calls of Assertions that can be enabled/disabled. It is not necessarily 100% accurate.
- Made I/O bound task during Unity editor startup asynchronous
  • Loading branch information
MrUnbelievable92 committed Jun 11, 2022
1 parent 72a8a06 commit aa4c45a
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 207 deletions.
7 changes: 3 additions & 4 deletions AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("C Sharp Dev Tools")]
[assembly: AssemblyCopyright("Copyright © 2020 - 2021 Maximilian Kalimon")]
[assembly: AssemblyCopyright("Copyright 2020 - 2022 Maximilian Kalimon")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -28,6 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.6")]
[assembly: AssemblyFileVersion("1.0.6")]

[assembly: AssemblyVersion("1.0.7")]
[assembly: AssemblyFileVersion("1.0.7")]
294 changes: 280 additions & 14 deletions Assert.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion C Sharp Dev Tools.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CSharpDevTools",
"rootNamespace": "",
"rootNamespace": "DevTools",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
61 changes: 26 additions & 35 deletions Dump.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
namespace DevTools
namespace DevTools
{
unsafe public static class Dump
{
public static string Bits(byte value, bool spaces = true)
{
if (spaces)
{
char* result = stackalloc char[9];

for (int i = 0; i < 4; i++)
{
result[i] = (char)(((value >> (7 - i)) & 1) + '0');
}

result[4] = ' ';

for (int i = 5; i < 9; i++)
{
result[i] = (char)(((value >> (7 - i)) & 1) + '0');
}
char* result = stackalloc char[8];

return new string(result, 0, 9);
}
else
for (int i = 0; i < 8; i++)
{
char* result = stackalloc char[8];

for (int i = 0; i < 8; i++)
{
result[i] = (char)(((value >> (7 - i)) & 1) + '0');
}

return new string(result, 0, 8);
result[i] = (char)(((value >> (7 - i)) & 1) + '0');
}

return new string(result, 0, 8).Insert(4, spaces ? " " : "");
}

public static string Bits<T>(T value, bool spaces = true)
Expand All @@ -51,7 +30,7 @@ public static string Bits(void* ptr, int bytes, bool spaces = true)

while (bytes != 0)
{
result = result.Insert(0, Bits(*address, spaces) + (spaces ? " " : string.Empty));
result = result.Insert(0, Bits(*address, spaces) + (spaces ? " " : ""));

address++;
bytes--;
Expand All @@ -61,15 +40,20 @@ public static string Bits(void* ptr, int bytes, bool spaces = true)
}


public static string Hex(byte value)
private static string Hex(byte value, char* HEX_VALUES)
{
char* HEX_VALUES = stackalloc char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

char* result = stackalloc char[] { HEX_VALUES[value >> 4], HEX_VALUES[value & 0b1111] };

return new string(result, 0, 2);
}

public static string Hex(byte value)
{
char* HEX_VALUES = stackalloc char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

return Hex(value, HEX_VALUES);
}

public static string Hex<T>(T value, bool spaces = true)
where T : unmanaged
{
Expand All @@ -80,20 +64,27 @@ public static string Hex(void* ptr, int bytes, bool spaces = true)
{
Assert.IsNotNull(ptr);
Assert.IsNonNegative(bytes);

char* HEX_VALUES = stackalloc char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

byte* address = (byte*)ptr;
int iterations = 0;
uint iterations = 0;
string result = string.Empty;

while (iterations != bytes)
{
result = result.Insert(0, Hex(*address) + ((spaces && (iterations != 0) && (iterations % 2 == 0)) ? " " : string.Empty));

string block = Hex(*address, HEX_VALUES);
if (spaces && (iterations != 0) & (iterations % 2 == 0))
{
block += " ";
}
result = result.Insert(0, block);

address++;
iterations++;
}

return result;
}
}
}
}
50 changes: 50 additions & 0 deletions Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.IO;

namespace DevTools
{
public static class DirectoryExtensions
{
/// <summary> Appends <see cref="Path.DirectorySeparatorChar"/> to '<paramref name="directory"/>' and returns the result. </summary>
public static string AsDirectory(this string directory)
{
return directory + Path.DirectorySeparatorChar;
}

/// <summary> Recursively searches for a file '<paramref name="fileName"/>' in the '<paramref name="folder"/>' directory, including subdirectories. </summary>
public static string FindInFolder(string folder, string fileName)
{
string result = folder.AsDirectory() + fileName;
if (File.Exists(result))
{
return result;
}

foreach (string subDirectory in Directory.GetDirectories(folder))
{
result = FindInFolder(subDirectory, fileName);
if (result != null)
{
return result;
}
}

return null;
}

/// <summary> Deletes a directory '<paramref name="folder"/>' and all of its contents, including subdirectories. </summary>
public static void DeleteFolder(string folder)
{
foreach (string subFolder in Directory.GetDirectories(folder))
{
DeleteFolder(subFolder);
}

foreach (string file in Directory.GetFiles(folder))
{
File.Delete(file);
}

Directory.Delete(folder);
}
}
}
26 changes: 26 additions & 0 deletions Extensions/GenericExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace DevTools
{
public static class GenericExtensions
{
/// <summary> Logs a message of type '<typeparamref name="T"/>' to the console. </summary>
public static void Log<T>(this T obj)
{
#if DEBUG
#if UNITY_EDITOR
UnityEngine.Debug.Log(obj);
#else
Console.WriteLine(obj);
#endif
#endif
}

/// <summary> Logs a message of type '<typeparamref name="T"/>' to the console. </summary>
public static void Log<T>(this T obj, string format, IFormatProvider formatProvider = null)
where T : IFormattable
{
Log(obj.ToString(format, formatProvider));
}
}
}
Loading

0 comments on commit aa4c45a

Please sign in to comment.