Skip to content

Commit

Permalink
Add colorized console logs
Browse files Browse the repository at this point in the history
  • Loading branch information
litolax committed Apr 14, 2023
1 parent b04fe08 commit cec5db7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 55 deletions.
1 change: 1 addition & 0 deletions .idea/.idea.altVPackage/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/.idea.altVPackage/.idea/git_toolbox_prj.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.altVPackage/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

146 changes: 91 additions & 55 deletions altVPackage/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using ImprovedConsole;

namespace altVPackage
{
Expand All @@ -18,60 +19,61 @@ public class Config
public string OutputPath { get; set; } = "./";
}

private static readonly List<string> Branches = new() {"release", "rc", "dev"};
private static readonly List<string> Branches = new() { "release", "rc", "dev" };
private const string CDN_URL = "cdn.alt-mp.com";

public static async Task Main(string[] args)
{
ConsoleWrapper.WriteLine("alt:V Package started", LogType.Success);
var config = new Config();

if (!File.Exists("./config.json"))
{
Console.WriteLine("Config file not found");
Console.WriteLine("Please enter branch name");
ConsoleWrapper.WriteLine("Config file not found", LogType.Error);
ConsoleWrapper.WriteLine("Please enter branch name");

var branch = Console.ReadLine()?.ToLower() ?? "release";
config.Branch = !Branches.Contains(branch) ? Branches.First() : branch;

Console.WriteLine("Windows build? (y/n)");
ConsoleWrapper.WriteLine("Windows build? (y/n)");
config.Windows = Console.ReadLine()?.ToLower() == "y";

Console.WriteLine("Do you want server build? (y/n)");
ConsoleWrapper.WriteLine("Do you want server build? (y/n)");
config.Server = Console.ReadLine()?.ToLower() == "y";

Console.WriteLine("Do you want voice build? (y/n)");
ConsoleWrapper.WriteLine("Do you want voice build? (y/n)");
config.Voice = Console.ReadLine()?.ToLower() == "y";

Console.WriteLine("Do you want CSharp build? (y/n)");
ConsoleWrapper.WriteLine("Do you want CSharp build? (y/n)");
config.CSharp = Console.ReadLine()?.ToLower() == "y";

Console.WriteLine("Do you want JS build? (y/n)");
ConsoleWrapper.WriteLine("Do you want JS build? (y/n)");
config.Js = Console.ReadLine()?.ToLower() == "y";

if (config.Branch == "release")
{
Console.WriteLine("Do you want JS bytecode build? (y/n)");
ConsoleWrapper.WriteLine("Do you want JS bytecode build? (y/n)");
config.JsByteCode = Console.ReadLine()?.ToLower() == "y";
}

Console.WriteLine("Input output path");
ConsoleWrapper.WriteLine("Input output path");
config.OutputPath = Console.ReadLine() ?? "./";

await File.WriteAllTextAsync("./config.json", JsonSerializer.Serialize(config));
Console.WriteLine("Config file created\n");
ConsoleWrapper.WriteLine("Config file created\n", LogType.Success);
}
else
{
config = JsonSerializer.Deserialize<Config>(await File.ReadAllTextAsync("./config.json"));
if (config is null)
{
Console.WriteLine("Config file is invalid, remove it and run the program again\n");
ConsoleWrapper.WriteLine("Config file is invalid, remove it and run the program again\n", LogType.Error);
return;
}
}

Console.WriteLine("Starting download\n");
ConsoleWrapper.WriteLine("Begin packages download\n", LogType.Info);

if (config.Server) Directory.CreateDirectory($@"{config.OutputPath}/data");
if (config.CSharp) Directory.CreateDirectory($@"{config.OutputPath}/modules");
if (config.Js) Directory.CreateDirectory($@"{config.OutputPath}/modules/js-module");
Expand All @@ -86,69 +88,79 @@ public static async Task Main(string[] args)

if (config.Js) await DownloadJs(config, system);

if (config.JsByteCode && config.Branch == "release") await DownloadJsByteCode(config, system);
Console.WriteLine("Download finished\n");
if (config is { JsByteCode: true, Branch: "release" }) await DownloadJsByteCode(config, system);

ConsoleWrapper.WriteLine("Download finished\n", LogType.Success);
Console.ReadKey();
}

public static async Task DownloadServer(Config config, string system)
{
Console.WriteLine("Starting server download\n");
ConsoleWrapper.WriteLine("Starting server download\n", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/{(config.Windows ? "altv-server.exe" : "altv-server")}") !=
await GetUpdatedHash($"https://{CDN_URL}/server/{config.Branch}/{system}/update.json",
$"{(config.Windows ? "altv-server.exe" : "altv-server")}"))
{
await DownloadFile(
$"https://{CDN_URL}/server/{config.Branch}/{system}/{(config.Windows ? "altv-server.exe" : "altv-server")}",
$"{config.OutputPath}/{(config.Windows ? "altv-server.exe" : "altv-server")}");

ConsoleWrapper.WriteLine("Server is downloaded", LogType.Success);
}
else Console.WriteLine("Server is up to date and skipped");
else ConsoleWrapper.WriteLine("Server is up to date and skipped", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/data/vehmodels.bin") !=
await GetUpdatedHash($"https://{CDN_URL}/data/{config.Branch}/update.json",
$"vehmodels.bin"))
{
await DownloadFile($"https://{CDN_URL}/data/{config.Branch}/data/vehmodels.bin",
$"{config.OutputPath}/data/vehmodels.bin");

ConsoleWrapper.WriteLine("vehmodels.bin is downloaded", LogType.Success);
}
else Console.WriteLine("vehmodels.bin is up to date and skipped");
else ConsoleWrapper.WriteLine("vehmodels.bin is up to date and skipped", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/data/vehmods.bin") !=
await GetUpdatedHash($"https://{CDN_URL}/data/{config.Branch}/update.json",
$"vehmods.bin"))
{
await DownloadFile($"https://{CDN_URL}/data/{config.Branch}/data/vehmods.bin",
$"{config.OutputPath}/data/vehmods.bin");

ConsoleWrapper.WriteLine("vehmods.bin is downloaded", LogType.Success);
}
else Console.WriteLine("vehmods.bin is up to date and skipped");
else ConsoleWrapper.WriteLine("vehmods.bin is up to date and skipped", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/data/clothes.bin") !=
await GetUpdatedHash($"https://{CDN_URL}/data/{config.Branch}/update.json",
$"clothes.bin"))
{
await DownloadFile($"https://{CDN_URL}/data/{config.Branch}/data/clothes.bin",
$"{config.OutputPath}/data/clothes.bin");

ConsoleWrapper.WriteLine("clothes.bin is downloaded", LogType.Success);
}
else Console.WriteLine("clothes.bin is up to date and skipped");
else ConsoleWrapper.WriteLine("clothes.bin is up to date and skipped", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/data/pedmodels.bin") !=
await GetUpdatedHash($"https://{CDN_URL}/data/{config.Branch}/update.json",
$"pedmodels.bin"))
{
await DownloadFile($"https://{CDN_URL}/data/{config.Branch}/data/pedmodels.bin",
$"{config.OutputPath}/data/pedmodels.bin");

ConsoleWrapper.WriteLine("pedmodels.bin is downloaded", LogType.Success);
}
else Console.WriteLine("pedmodels.bin is up to date and skipped");
else ConsoleWrapper.WriteLine("pedmodels.bin is up to date and skipped", LogType.Info);

Console.WriteLine();
Console.WriteLine("Finish server download\n");
ConsoleWrapper.WriteLine("Finish server download\n", LogType.Success);
}

public static async Task DownloadVoice(Config config, string system)
{
Console.WriteLine("Starting voice server download\n");
ConsoleWrapper.WriteLine("Starting voice server download\n", LogType.Info);

if (await CalculateHash(
$"{config.OutputPath}/{(config.Windows ? "altv-voice-server.exe" : "altv-voice-server")}") !=
Expand All @@ -158,25 +170,29 @@ await GetUpdatedHash($"https://{CDN_URL}/voice-server/{config.Branch}/{system}/u
await DownloadFile(
$"https://{CDN_URL}/voice-server/{config.Branch}/{system}/{(config.Windows ? "altv-voice-server.exe" : "altv-voice-server")}",
$"{config.OutputPath}/{(config.Windows ? "altv-voice-server.exe" : "altv-voice-server")}");

ConsoleWrapper.WriteLine("altv-voice-server.exe is downloaded", LogType.Success);
}
else Console.WriteLine("Voice is up to date and skipped");
else ConsoleWrapper.WriteLine("Voice is up to date and skipped", LogType.Info);

Console.WriteLine();
Console.WriteLine("Finish voice server download\n");
ConsoleWrapper.WriteLine("Finish voice server download\n", LogType.Success);
}

public static async Task DownloadCsharp(Config config, string system)
{
Console.WriteLine("Starting csharp download\n");
ConsoleWrapper.WriteLine("Starting csharp download\n", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/AltV.Net.Host.dll") != await GetUpdatedHash(
$"https://{CDN_URL}/coreclr-module/{config.Branch}/{system}/update.json",
"AltV.Net.Host.dll"))
{
await DownloadFile($"https://{CDN_URL}/coreclr-module/{config.Branch}/{system}/AltV.Net.Host.dll",
$"{config.OutputPath}/AltV.Net.Host.dll");

ConsoleWrapper.WriteLine("AltV.Net.Host.dll is downloaded", LogType.Success);
}
else Console.WriteLine("AltV.Net.Host.dll is up to date and skipped");
else ConsoleWrapper.WriteLine("AltV.Net.Host.dll is up to date and skipped", LogType.Info);

if (await CalculateHash($"{config.OutputPath}/AltV.Net.Host.runtimeconfig.json") != await GetUpdatedHash(
$"https://{CDN_URL}/coreclr-module/{config.Branch}/{system}/update.json",
Expand All @@ -185,8 +201,10 @@ await DownloadFile($"https://{CDN_URL}/coreclr-module/{config.Branch}/{system}/A
await DownloadFile(
$"https://{CDN_URL}/coreclr-module/{config.Branch}/{system}/AltV.Net.Host.runtimeconfig.json",
$"{config.OutputPath}/AltV.Net.Host.runtimeconfig.json");

ConsoleWrapper.WriteLine("AltV.Net.Host.runtimeconfig.json is downloaded", LogType.Success);
}
else Console.WriteLine("AltV.Net.Host.runtimeconfig.json is up to date and skipped");
else ConsoleWrapper.WriteLine("AltV.Net.Host.runtimeconfig.json is up to date and skipped", LogType.Info);

if (await CalculateHash(
$"{config.OutputPath}/{(config.Windows ? "modules/csharp-module.dll" : "modules/libcsharp-module.so")}") !=
Expand All @@ -197,18 +215,20 @@ await GetUpdatedHash(
await DownloadFile(
$"https://{CDN_URL}/coreclr-module/{config.Branch}/{system}/modules/{(config.Windows ? "csharp-module.dll" : "libcsharp-module.so")}",
$"{config.OutputPath}/modules/{(config.Windows ? "csharp-module.dll" : "libcsharp-module.so")}");

ConsoleWrapper.WriteLine("csharp-module.dll is downloaded", LogType.Success);
}
else
Console.WriteLine(
$"{(config.Windows ? "csharp-module.dll" : "libcsharp-module.so")} is up to date and skipped");
ConsoleWrapper.WriteLine(
$"{(config.Windows ? "csharp-module.dll" : "libcsharp-module.so")} is up to date and skipped", LogType.Info);

Console.WriteLine();
Console.WriteLine("Finish csharp download\n");
ConsoleWrapper.WriteLine("Finish csharp download\n", LogType.Success);
}

public static async Task DownloadJs(Config config, string system)
{
Console.WriteLine("Starting js download\n");
ConsoleWrapper.WriteLine("Starting js download\n", LogType.Info);

if (await CalculateHash(
$"{config.OutputPath}/{(config.Windows ? "modules/js-module/js-module.dll" : "modules/js-module/libjs-module.so")}") !=
Expand All @@ -219,10 +239,12 @@ await GetUpdatedHash(
await DownloadFile(
$"https://{CDN_URL}/js-module/{config.Branch}/{system}/modules/js-module/{(config.Windows ? "js-module.dll" : "libjs-module.so")}",
$"{config.OutputPath}/modules/js-module/{(config.Windows ? "js-module.dll" : "libjs-module.so")}");

ConsoleWrapper.WriteLine("js-module.dll is downloaded", LogType.Success);
}
else
Console.WriteLine(
$"{(config.Windows ? "js-module.dll" : "libjs-module.so")} is up to date and skipped");
ConsoleWrapper.WriteLine(
$"{(config.Windows ? "js-module.dll" : "libjs-module.so")} is up to date and skipped", LogType.Info);

if (await CalculateHash(
$"{config.OutputPath}/{(config.Windows ? "modules/js-module/libnode.dll" : "modules/js-module/libnode.so.108")}") !=
Expand All @@ -233,16 +255,18 @@ await GetUpdatedHash(
await DownloadFile(
$"https://{CDN_URL}/js-module/{config.Branch}/{system}/modules/js-module/{(config.Windows ? "libnode.dll" : "libnode.so.108")}",
$"{config.OutputPath}/modules/js-module/{(config.Windows ? "libnode.dll" : "libnode.so.108")}");

ConsoleWrapper.WriteLine("libnode.dll is downloaded", LogType.Success);
}
else Console.WriteLine($"{(config.Windows ? "libnode.dll" : "libnode.so.108")} is up to date and skipped");
else ConsoleWrapper.WriteLine($"{(config.Windows ? "libnode.dll" : "libnode.so.108")} is up to date and skipped", LogType.Info);

Console.WriteLine();
Console.WriteLine("Finish js download\n");
ConsoleWrapper.WriteLine("Finish js download\n", LogType.Success);
}

public static async Task DownloadJsByteCode(Config config, string system)
{
Console.WriteLine("Starting js-bytecode download\n");
ConsoleWrapper.WriteLine("Starting js-bytecode download\n", LogType.Info);

if (await CalculateHash(
$"{config.OutputPath}/{(config.Windows ? "modules/js-bytecode-module.dll" : "modules/libjs-bytecode-module.so")}") !=
Expand All @@ -253,32 +277,44 @@ await GetUpdatedHash(
await DownloadFile(
$"https://{CDN_URL}/js-bytecode-module/{config.Branch}/{system}/modules/{(config.Windows ? "js-bytecode-module.dll" : "libjs-bytecode-module.so")}",
$"{config.OutputPath}/modules/{(config.Windows ? "js-bytecode-module.dll" : "libjs-bytecode-module.so")}");

ConsoleWrapper.WriteLine("js-bytecode-module.dll is downloaded", LogType.Success);
}
else
Console.WriteLine(
$"{(config.Windows ? "js-bytecode-module.dll" : "libjs-bytecode-module.so")} is up to date and skipped");
ConsoleWrapper.WriteLine(
$"{(config.Windows ? "js-bytecode-module.dll" : "libjs-bytecode-module.so")} is up to date and skipped", LogType.Info);

Console.WriteLine();
Console.WriteLine("Finish js-bytecode download\n");
ConsoleWrapper.WriteLine("Finish js-bytecode download\n", LogType.Success);
}

public static async Task DownloadFile(string url, string fileName)
private static async Task DownloadFile(string url, string fileName)
{
var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(5);
var fileBytes = await httpClient.GetByteArrayAsync(url);
byte[] fileBytes;
try
{
fileBytes = await httpClient.GetByteArrayAsync(url);
}
catch
{
ConsoleWrapper.WriteLine($"Connection timeout on {fileName}", LogType.Error);
Console.ReadLine();
return;
}
await File.WriteAllBytesAsync(fileName, fileBytes);
}

public static async Task<string> GetUpdatedHash(string url, string file)
private static async Task<string> GetUpdatedHash(string url, string file)
{
var httpClient = new HttpClient();
var fileBytes = await httpClient.GetByteArrayAsync(url);
var json = Encoding.UTF8.GetString(fileBytes, 0, fileBytes.Length);
return json.Substring(json.IndexOf($"{file}", StringComparison.Ordinal) + file.Length + 3, 40);
}

public static async Task<string> CalculateHash(string filePath)
private static async Task<string> CalculateHash(string filePath)
{
if (!File.Exists(filePath)) return string.Empty;
await using var fileStream = File.OpenRead($"{filePath}");
Expand All @@ -287,4 +323,4 @@ public static async Task<string> CalculateHash(string filePath)
}
}
}
}
}
4 changes: 4 additions & 0 deletions altVPackage/altVPackage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImprovedСonsole" Version="1.0.3" />
</ItemGroup>

</Project>

0 comments on commit cec5db7

Please sign in to comment.