-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
306 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
using WDE.Module.Attributes; | ||
using WoWDatabaseEditorCore.Services.Processes; | ||
|
||
namespace WoWDatabaseEditorCore.Services.DotNetUtils | ||
{ | ||
[UniqueProvider] | ||
public interface IDotNetService | ||
{ | ||
Task<bool> IsDotNet6Installed(); | ||
Uri DownloadDotNet6Link { get; } | ||
} | ||
|
||
[AutoRegister] | ||
[SingleInstance] | ||
public class DotNetService : IDotNetService | ||
{ | ||
private readonly IProcessService processService; | ||
|
||
public DotNetService(IProcessService processService) | ||
{ | ||
this.processService = processService; | ||
} | ||
|
||
public async Task<bool> IsDotNet6Installed() | ||
{ | ||
try | ||
{ | ||
var dotnetPath = "dotnet"; | ||
var versions = await processService.RunAndGetOutput(dotnetPath, "--list-runtimes", null); | ||
if (versions == null) | ||
return true; | ||
var runtimes = versions.Split('\n'); | ||
|
||
return runtimes.Any(r => r.StartsWith("Microsoft.NETCore.App 6.") || | ||
r.StartsWith("Microsoft.NETCore.App 7.") || | ||
r.StartsWith("Microsoft.NETCore.App 8.")); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
return true; | ||
} | ||
} | ||
|
||
private Uri GetLink(OSPlatform os, Architecture arch) | ||
{ | ||
var stringOs = os == OSPlatform.Windows ? "windows" : (os == OSPlatform.OSX ? "macos" : "linux"); | ||
var version = "6.0.0"; | ||
return new Uri($"https://dotnet.microsoft.com/download/dotnet/thank-you/runtime-{version}-{stringOs}-{arch.ToString().ToLower()}-installer"); | ||
} | ||
|
||
public Uri DownloadDotNet6Link | ||
{ | ||
get | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
return new Uri("https://docs.microsoft.com/dotnet/core/install/linux?WT.mc_id=dotnet-35129-website"); | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
return GetLink(OSPlatform.OSX, RuntimeInformation.OSArchitecture); | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
return GetLink(OSPlatform.Windows, RuntimeInformation.OSArchitecture); | ||
|
||
throw new Exception($"Your OS is not supported by .net 6.0"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using WDE.Module.Attributes; | ||
|
||
namespace WoWDatabaseEditorCore.Services.Processes | ||
{ | ||
public interface IProcess | ||
{ | ||
bool IsRunning { get; } | ||
void Kill(); | ||
} | ||
|
||
[UniqueProvider] | ||
public interface IProcessService | ||
{ | ||
IProcess RunAndForget(string path, string arguments, string? workingDirectory, bool noWindow, | ||
params (string, string)[] envVars); | ||
|
||
Task<int> Run(CancellationToken token, string path, string arguments, string? workingDirectory, | ||
Action<string>? onOutput, | ||
Action<string>? onError, | ||
Action<TextWriter>? onInput = null, | ||
params (string, string)[] envVars); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using WDE.Module.Attributes; | ||
|
||
namespace WoWDatabaseEditorCore.Services.Processes | ||
{ | ||
[AutoRegister] | ||
[SingleInstance] | ||
public class ProcessService : IProcessService | ||
{ | ||
public class ProcessData : IProcess | ||
{ | ||
private readonly Process process; | ||
|
||
public ProcessData(Process process) | ||
{ | ||
this.process = process; | ||
} | ||
|
||
public bool IsRunning => !process.HasExited; | ||
|
||
public void Kill() | ||
{ | ||
if (IsRunning) | ||
{ | ||
process.Kill(); | ||
} | ||
} | ||
} | ||
|
||
public IProcess RunAndForget(string path, string arguments, string? workingDirectory, bool noWindow, | ||
params (string, string)[] envVars) | ||
{ | ||
var startInfo = new ProcessStartInfo(path, arguments); | ||
startInfo.UseShellExecute = false; | ||
startInfo.CreateNoWindow = noWindow; | ||
|
||
foreach (var envVar in envVars) | ||
startInfo.Environment.Add(envVar.Item1, envVar.Item2); | ||
|
||
if (workingDirectory != null) | ||
startInfo.WorkingDirectory = workingDirectory; | ||
|
||
Process process = new Process(); | ||
process.StartInfo = startInfo; | ||
if (!process.Start()) | ||
throw new Exception("Cannot start " + path); | ||
return new ProcessData(process); | ||
} | ||
|
||
public async Task<int> Run(CancellationToken token, string path, string arguments, string? workingDirectory, Action<string>? onOutput, | ||
Action<string>? onError, Action<TextWriter>? onInput, | ||
params (string, string)[] envVars) | ||
{ | ||
var startInfo = new ProcessStartInfo(path, arguments); | ||
startInfo.UseShellExecute = false; | ||
if (onOutput != null) | ||
startInfo.RedirectStandardOutput = true; | ||
if (onError != null) | ||
startInfo.RedirectStandardError = true; | ||
if (onInput != null) | ||
startInfo.RedirectStandardInput = true; | ||
startInfo.CreateNoWindow = true; | ||
|
||
foreach (var envVar in envVars) | ||
startInfo.Environment.Add(envVar.Item1, envVar.Item2); | ||
|
||
if (workingDirectory != null) | ||
startInfo.WorkingDirectory = workingDirectory; | ||
|
||
Process process = new Process(); | ||
process.StartInfo = startInfo; | ||
process.ErrorDataReceived += (sender, data) => | ||
{ | ||
if (data.Data != null) | ||
{ | ||
onError?.Invoke(data.Data); | ||
} | ||
}; | ||
process.OutputDataReceived += (sender, data) => | ||
{ | ||
if (data.Data != null) | ||
{ | ||
onOutput?.Invoke(data.Data); | ||
} | ||
}; | ||
if (!process.Start()) | ||
throw new Exception("Cannot start " + path); | ||
|
||
if (onOutput != null) | ||
process.BeginOutputReadLine(); | ||
|
||
if (onError != null) | ||
process.BeginErrorReadLine(); | ||
|
||
//onInput?.Invoke(process.StandardInput); | ||
|
||
try | ||
{ | ||
await process.WaitForExitAsync(token); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
process.Kill(); | ||
} | ||
|
||
return process.HasExited ? process.ExitCode : -1; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
WoWDatabaseEditor/Services/Processes/ProcessServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace WoWDatabaseEditorCore.Services.Processes | ||
{ | ||
public static class ProcessServiceExtensions | ||
{ | ||
public static async Task<T?> RunAndGetJson<T>(this IProcessService service, string path, string arguments, | ||
string? workingDirectory) | ||
{ | ||
var output = await service.RunAndGetOutput(path, arguments, workingDirectory); | ||
if (output == null) | ||
return default; | ||
|
||
T? t = JsonConvert.DeserializeObject<T>(output); | ||
return t; | ||
} | ||
|
||
public static async Task<string?> RunAndGetOutput(this IProcessService service, string path, string arguments, string? workingDirectory) | ||
{ | ||
StringBuilder sb = new(); | ||
bool any = false; | ||
await service.Run(default, path, arguments, workingDirectory, s => | ||
{ | ||
sb.AppendLine(s); | ||
any = true; | ||
}, null); | ||
if (any) | ||
return sb.ToString(); | ||
return null; | ||
} | ||
|
||
public static async Task<string?> RunAndGetOutputAndError(this IProcessService service, string path, string arguments, string? workingDirectory) | ||
{ | ||
StringBuilder sb = new(); | ||
bool any = false; | ||
await service.Run(default, path, arguments, workingDirectory, s => | ||
{ | ||
sb.Append(s); | ||
any = true; | ||
}, s => | ||
{ | ||
sb.Append(s); | ||
any = true; | ||
}); | ||
if (any) | ||
return sb.ToString(); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule WoWPacketParser
updated
4 files
+1 −0 | WowPacketParser/Enums/ClientVersionBuild.cs | |
+1 −0 | WowPacketParser/Enums/Version/Opcodes.cs | |
+1 −0 | WowPacketParser/Enums/Version/UpdateFields.cs | |
+2 −0 | WowPacketParser/Misc/ClientVersion.cs |