Skip to content

Commit

Permalink
Added basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
12Acorns committed Jul 30, 2024
1 parent 0863a88 commit 2faa9c5
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 6 deletions.
55 changes: 49 additions & 6 deletions GameLoader/GameLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,58 @@ public sealed class GameLoader
{
public GameLoader()
{
Directory.CreateDirectory(gamesDirectory);
using var _logger = new Logger("GameLoaderLog", 4096);

var _gameAssemblies = Directory.GetFiles(gamesDirectory, "*.dll")
.Select(Assembly.LoadFrom);
try
{
Directory.CreateDirectory(gamesDirectory);
}
catch(Exception _ex)
{
_logger.AppendOrLog("\n\nERROR: Could not create directory ->\n Target Path: " +
$"{gamesDirectory}\n Exception: {_ex.Message}");

Console.WriteLine("ERROR");
Console.WriteLine($"Log details: {_logger.filePath}");

throw;
}


IEnumerable<Assembly> _gameAssemblies = [];

try
{
_gameAssemblies = Directory.GetFiles(gamesDirectory, "*.dll")
.Select(Assembly.LoadFrom);
}
catch(Exception _ex)
{
_logger.AppendOrLog("\n\nERROR: Could not get files ->\n " + _ex.Message);

Console.WriteLine("ERROR");
Console.WriteLine($"Log details: {_logger.filePath}");

throw;
}

_logger.AppendOrLog($"Assemblies found:\n" +
string.Join('\n', _gameAssemblies.Select(x => " " + x.GetName().FullName)));

_logger.AppendOrLog($"\n\nAssembly locations:\n" +
string.Join('\n', _gameAssemblies.Select(x => " " + x.Location)));

var _types = _gameAssemblies.SelectMany(assembly => assembly.GetTypes());

_logger.AppendOrLog($"\n\nTypes found:\n" +
string.Join('\n', _types.Select(x => $" {x.FullName}")));

var _iGameTypes = _types.Where(type => typeof(IGame).IsAssignableFrom(type));

var _assemblies = _gameAssemblies.SelectMany(assembly => assembly.GetTypes());
var _assignable = _assemblies.Where(type => typeof(IGame).IsAssignableFrom(type));
_logger.AppendOrLog($"\n\nTypes that match {typeof(IGame).Name} found:\n" +
string.Join('\n', _iGameTypes.Select(x => $" {x.FullName}")));

games = _assignable.Select(type => (IGame)Activator.CreateInstance(type)!);
games = _iGameTypes.Select(type => (IGame)Activator.CreateInstance(type)!);
gameNames = games.ToDictionary(x => x.Name, x => x);

var _builder = new StringBuilder("Enter desired game (Case-Sensitive): \n");
Expand Down
100 changes: 100 additions & 0 deletions GameLoader/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Reflection;
using System.Text;

namespace GameLoader;

internal sealed class Logger : IDisposable
{
private static readonly SemaphoreSlim lockObj = new(1, 1);
private static readonly string path =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
$"{Assembly.GetExecutingAssembly().GetName().Name}");

public Logger(string _name, int _size)
{
bufferSize = _size;
buffer = new char[bufferSize];
bufferHandle = 0;

filePath = Path.Combine(path, _name + ".txt");

Directory.CreateDirectory(path);
File.Delete(filePath);
}

public readonly string filePath;

private readonly int bufferSize;
private readonly char[] buffer;
private int bufferHandle;

private bool disposedValue;

public async void AppendOrLog(string _contents)
{
await lockObj.WaitAsync();

var _contentsLength = _contents.Length;

// Buffer full, log contents
if(bufferHandle == buffer.Length - 1)
{
await Log(bufferSize);
}

// Trying to append more than supported contents
int _offset = 0;
while(bufferHandle - _contentsLength < 0)
{
var _bufferHandleStart = bufferHandle;
for(; bufferHandle < buffer.Length; bufferHandle++, _contentsLength--)
{
if(_contentsLength == 0)
{
break;
}

buffer[bufferHandle] = _contents[bufferHandle - _bufferHandleStart + _offset];
}
await Log(bufferSize - (bufferSize - bufferHandle));
_offset += bufferSize - 1;
}

lockObj.Release();
}

private async Task Log(int _size)
{
using var _steam = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, bufferSize, false);

var _bytes = Encoding.ASCII.GetBytes(buffer);

await _steam.WriteAsync(_bytes.AsMemory(0, _size));

bufferHandle = 0;

return;
}

private void Dispose(bool disposing)
{
if(disposedValue)
{
return;
}
disposedValue = true;
lockObj.Dispose();
}

~Logger()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

0 comments on commit 2faa9c5

Please sign in to comment.