Skip to content

Commit

Permalink
upgrade C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashNephy committed Apr 6, 2024
1 parent 15a29ba commit d4be002
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 396 deletions.
112 changes: 0 additions & 112 deletions SimpleVoiceroid2Proxy/HttpRequest.cs

This file was deleted.

61 changes: 0 additions & 61 deletions SimpleVoiceroid2Proxy/HttpServer.cs

This file was deleted.

51 changes: 51 additions & 0 deletions SimpleVoiceroid2Proxy/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

namespace SimpleVoiceroid2Proxy;

public sealed class ConsoleLogger : ILogger
{
public static readonly ILogger Instance = new ConsoleLogger();

public void Debug(string message)
{
#if DEBUG
Log(LogLevel.DEBUG, message);
#endif
}

public void Info(string message)
{
Log(LogLevel.INFO, message);
}

public void Warn(string message)
{
Log(LogLevel.WARN, message);
}

public void Error(Exception exception, string message)
{
Log(LogLevel.ERROR, $"{message}\n{exception}");
}

private void Log(LogLevel level, string message)
{
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{level}] {message}");
}
}

public interface ILogger
{
public void Debug(string message);
public void Info(string message);
public void Warn(string message);
public void Error(Exception exception, string message);
}

public enum LogLevel
{
DEBUG,
INFO,
WARN,
ERROR,
}
90 changes: 15 additions & 75 deletions SimpleVoiceroid2Proxy/Program.cs
Original file line number Diff line number Diff line change
@@ -1,83 +1,23 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Threading.Tasks;
using SimpleVoiceroid2Proxy.Server;

namespace SimpleVoiceroid2Proxy
{
public static class Program
{
static Program()
{
KillDuplicatedProcesses();
}

public static readonly ILogger Logger = new LoggerImpl();
private static readonly HttpServer Server = new();
public static readonly VoiceroidEngine VoiceroidEngine = new();

public static void Main()
{
Task.Run(async () =>
{
await VoiceroidEngine.TalkAsync("準備完了!");
await Server.ConsumeAsync();
}).Wait();
}

private static void KillDuplicatedProcesses()
{
var currentProcess = Process.GetCurrentProcess();
var imageName = Assembly.GetExecutingAssembly()
.Location
.Split(Path.DirectorySeparatorChar)
.Last()
.Replace(".exe", "");
namespace SimpleVoiceroid2Proxy;

foreach (var process in Process.GetProcessesByName(imageName).Where(x => x.Id != currentProcess.Id))
{
try
{
process.Kill();
Logger.Info($"{imageName}.exe (PID: {process.Id}) has been killed.");
}
catch
{
Logger.Warn($"Failed to kill {imageName}.exe (PID: {process.Id}).");
}
}
}

private class LoggerImpl : ILogger
{
public void Info(string message)
{
Write("Info", message);
}

public void Warn(string message)
{
Write("Warn", message);
}

public void Error(Exception exception)
{
Write("Error", exception.ToString());
}
public static class Program
{
private static readonly HttpServer server = new();
public static readonly VoiceroidEngine VoiceroidEngine = new();

private static void Write(string level, string message)
{
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}");
}
}
static Program()
{
Utils.KillDuplicateProcesses();
}

public interface ILogger
public static void Main()
{
public void Info(string message);
public void Warn(string message);
public void Error(Exception exception);
Task.WaitAll(
server.ListenAsync(),
VoiceroidEngine.TalkAsync("準備完了!")
);
}
}
Loading

0 comments on commit d4be002

Please sign in to comment.