Skip to content

Commit

Permalink
Migrating from gRPC to SignalT
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Oct 18, 2023
1 parent a749785 commit 529bda5
Show file tree
Hide file tree
Showing 30 changed files with 423 additions and 205 deletions.
16 changes: 4 additions & 12 deletions src/Launcher/Sucrose.Launcher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
using Application = System.Windows.Application;
using SELLT = Skylark.Enum.LevelLogType;
using SEWTT = Skylark.Enum.WindowsThemeType;
using SGCL = Sucrose.Grpc.Common.Launcher;
using SGSGSS = Sucrose.Grpc.Services.GeneralServerService;
using SHC = Skylark.Helper.Culture;
using SMC = Sucrose.Memory.Constant;
using SMMI = Sucrose.Manager.Manage.Internal;
using SMMM = Sucrose.Manager.Manage.Manager;
using SMR = Sucrose.Memory.Readonly;
using SSDH = Sucrose.Shared.Discord.Hook;
using SSLCI = Sucrose.Shared.Launcher.Command.Interface;
using SSLMI = Sucrose.Shared.Launcher.Manage.Internal;
using SSLMM = Sucrose.Shared.Launcher.Manage.Manager;
using SSMI = Sucrose.Signal.Manage.Internal;
using SSRHR = Sucrose.Shared.Resources.Helper.Resources;
using SSSHI = Sucrose.Shared.Space.Helper.Instance;
using SSSSLSS = Sucrose.Shared.Server.Services.LauncherServerService;
using SSSSLSS = Sucrose.Shared.Signal.Services.LauncherSignalService;
using SSWDEMB = Sucrose.Shared.Watchdog.DarkErrorMessageBox;
using SSWLEMB = Sucrose.Shared.Watchdog.LightErrorMessageBox;
using SSWW = Sucrose.Shared.Watchdog.Watch;
Expand Down Expand Up @@ -132,16 +130,11 @@ protected void Configure()

SSLMI.TrayIconManager.Start();

SGSGSS.ServerCreate(SGCL.BindService(new SSSSLSS()));

SMMI.LauncherSettingManager.SetSetting(SMC.Host, SGSGSS.Host);
SMMI.LauncherSettingManager.SetSetting(SMC.Port, SGSGSS.Port);

SMMI.LauncherLogManager.Log(SELLT.Info, "Configuration initialized..");

SMMI.LauncherLogManager.Log(SELLT.Info, "Server initializing..");

SGSGSS.ServerInstance.Start();
SSMI.LauncherManager.StartChannel(SSSSLSS.Handler);

SMMI.LauncherLogManager.Log(SELLT.Info, "Server initialized..");

Expand All @@ -158,8 +151,7 @@ protected override void OnExit(ExitEventArgs e)

Discord.Dispose();

SGSGSS.ServerInstance.KillAsync().Wait();
//SGSGSS.ServerInstance.ShutdownAsync().Wait();
SSMI.LauncherManager.StopChannel();

SSLMI.TrayIconManager.Dispose();

Expand Down
4 changes: 2 additions & 2 deletions src/Launcher/Sucrose.Launcher/Sucrose.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\gRPC\Sucrose.Grpc\Sucrose.Grpc.csproj" />
<ProjectReference Include="..\..\Library\Sucrose.Signal\Sucrose.Signal.csproj" />
<ProjectReference Include="..\..\Library\Sucrose.Manager\Sucrose.Manager.csproj" />
</ItemGroup>

<Import Project="..\..\Shared\Sucrose.Shared.Zip\Sucrose.Shared.Zip.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Live\Sucrose.Shared.Live.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Space\Sucrose.Shared.Space.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Theme\Sucrose.Shared.Theme.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Server\Sucrose.Shared.Server.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Signal\Sucrose.Shared.Signal.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Discord\Sucrose.Shared.Discord.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Launcher\Sucrose.Shared.Launcher.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Watchdog\Sucrose.Shared.Watchdog.projitems" Label="Shared" />
Expand Down
2 changes: 2 additions & 0 deletions src/Library/Sucrose.Memory/Readonly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static class Readonly

public static readonly string LogFolder = "Log";

public static readonly string Signal = "Signal";

public static readonly string GifFolder = "Gif";

public static readonly string Bundle = "Bundle";
Expand Down
46 changes: 46 additions & 0 deletions src/Library/Sucrose.Signal/Helper/Reader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using SMR = Sucrose.Memory.Readonly;

namespace Sucrose.Signal.Helper
{
internal static class Reader
{
public static async Task<string> Read(string filePath)
{
try
{
using FileStream fileStream = new(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
using StreamReader reader = new(fileStream);

return reader.ReadToEnd();
}
catch
{
try
{
await Task.Delay(SMR.Randomise.Next(5, 50));

using FileStream fileStream = new(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
using StreamReader reader = new(fileStream);

return reader.ReadToEnd();
}
catch
{
try
{
await Task.Delay(SMR.Randomise.Next(5, 50));

using FileStream fileStream = new(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
using StreamReader reader = new(fileStream);

return reader.ReadToEnd();
}
catch
{
return string.Empty;
}
}
}
}
}
}
48 changes: 48 additions & 0 deletions src/Library/Sucrose.Signal/Helper/Writer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using SMR = Sucrose.Memory.Readonly;

namespace Sucrose.Signal.Helper
{
internal static class Writer
{
public static async void Write(string filePath, string fileContent)
{
FileMode fileMode = File.Exists(filePath) ? FileMode.Truncate : FileMode.CreateNew;

try
{
using FileStream fileStream = new(filePath, fileMode, FileAccess.Write, FileShare.None);
using StreamWriter writer = new(fileStream);

writer.Write(fileContent);
}
catch
{
try
{
await Task.Delay(SMR.Randomise.Next(5, 50));

using FileStream fileStream = new(filePath, fileMode, FileAccess.Write, FileShare.None);
using StreamWriter writer = new(fileStream);

writer.Write(fileContent);
}
catch
{
try
{
await Task.Delay(SMR.Randomise.Next(5, 50));

using FileStream fileStream = new(filePath, fileMode, FileAccess.Write, FileShare.None);
using StreamWriter writer = new(fileStream);

writer.Write(fileContent);
}
catch
{
//
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/Library/Sucrose.Signal/Interface/Launcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Sucrose.Signal.Interface
{
public class Launcher
{
public bool Hide { get; set; } = false;

public bool Show { get; set; } = false;

public bool Release { get; set; } = false;
}
}
9 changes: 9 additions & 0 deletions src/Library/Sucrose.Signal/Interface/Websiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Sucrose.Signal.Interface
{
public class Websiter
{
public bool Hook { get; set; } = false;

public string Url { get; set; } = string.Empty;
}
}
13 changes: 13 additions & 0 deletions src/Library/Sucrose.Signal/Manage/Internal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using SSST = Sucrose.Signal.SignalT;

namespace Sucrose.Signal.Manage
{
public static class Internal
{
public static readonly SSST PortalManager = new("Portal.sgnl");

public static readonly SSST LauncherManager = new("Launcher.sgnl");

public static readonly SSST WebsiterManager = new("Websiter.sgnl");
}
}
149 changes: 149 additions & 0 deletions src/Library/Sucrose.Signal/SignalT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System.Text.Json;
using SMR = Sucrose.Memory.Readonly;
using SSHR = Sucrose.Signal.Helper.Reader;
using SSHW = Sucrose.Signal.Helper.Writer;

namespace Sucrose.Signal
{
public class SignalT(string Name)
{
private readonly string Source = Path.Combine(SMR.AppDataPath, SMR.AppName, SMR.CacheFolder, SMR.Signal);
private readonly JsonSerializerOptions Options = new() { WriteIndented = true };
private readonly Random Random = new();
private FileSystemWatcher FileWatcher;

public FileSystemEventHandler CreatedEventHandler;
public FileSystemEventHandler ChangedEventHandler;
public FileSystemEventHandler DeletedEventHandler;
public RenamedEventHandler RenamedEventHandler;

public void StopChannel()
{
if (FileWatcher != null)
{
FileWatcher.EnableRaisingEvents = false;
FileWatcher.Dispose();
}
}

public void StartChannel(FileSystemEventHandler Handler)
{
CreatedEventHandler += Handler;

if (Directory.Exists(Source))
{
string[] Files = Directory.GetFiles(Source, "*.*", SearchOption.TopDirectoryOnly);

foreach (string Record in Files)
{
if (FileCheck(Record))
{
File.Delete(Record);
}
}
}
else
{
Directory.CreateDirectory(Source);
}

FileWatcher = new()
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
Filter = "*.*",
Path = Source
};

FileWatcher.Created += (s, e) =>
{
if (FileCheck(e.FullPath))
{
CreatedEventHandler?.Invoke(s, e);
}
};

FileWatcher.Changed += (s, e) =>
{
if (FileCheck(e.FullPath))
{
ChangedEventHandler?.Invoke(s, e);
}
};

FileWatcher.Deleted += (s, e) =>
{
if (FileCheck(e.FullPath))
{
DeletedEventHandler?.Invoke(s, e);
}
};

FileWatcher.Renamed += (s, e) =>
{
if (FileCheck(e.FullPath))
{
RenamedEventHandler?.Invoke(s, e);
}
};

FileWatcher.EnableRaisingEvents = true;
}

public void FileSave<T>(T Data)
{
string Destination = Path.Combine(Source, Name);

while (File.Exists(Destination))
{
Destination = Path.Combine(Source, $"{Path.GetFileNameWithoutExtension(Name)}-{Random.Next(0, int.MaxValue)}{Path.GetExtension(Name)}");
}

SSHW.Write(Destination, JsonSerializer.Serialize(Data, Options));
}

public string FileName(string Source)
{
return Path.GetFileName(Source);
}

public T FileRead<T>(string Source, T Default)
{
try
{
string Data = SSHR.Read(Source).Result;

return JsonSerializer.Deserialize<T>(Data, Options);
}
catch
{
return Default;
}
}

public string FileRead(string Source, string Default)
{
try
{
string Data = SSHR.Read(Source).Result;

return Data;
}
catch
{
return Default;
}
}

private bool FileCheck(string Source)
{
if (FileName(Source).Contains(Path.GetFileNameWithoutExtension(Name)) || FileName(Source) == Name)
{
return true;
}
else
{
return false;
}
}
}
}
22 changes: 22 additions & 0 deletions src/Library/Sucrose.Signal/Sucrose.Signal.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>$([System.DateTime]::Today.ToString(yy)).$([System.DateTime]::Today.ToString(MM)).$([System.DateTime]::Today.ToString(dd))</Version>
<TargetFrameworks>netstandard2.0;netstandard2.1;net48;net481;net6.0;net7.0;net8.0</TargetFrameworks>
<OutputPath>..\..\Sucrose\Signal</OutputPath>
<AssemblyVersion>$(Version)</AssemblyVersion>
<ImplicitUsings>enable</ImplicitUsings>
<AnalysisLevel>preview</AnalysisLevel>
<FileVersion>$(Version)</FileVersion>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net48')) OR $(TargetFramework.StartsWith('netstandard'))">
<PackageReference Include="System.Text.Json" Version="8.0.0-rc.2.23479.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Sucrose.Memory\Sucrose.Memory.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 529bda5

Please sign in to comment.