mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 19:19:52 +02:00
feat: rename cli
This commit is contained in:
1 parent
157fa0c1dd
commit
74a8df6842
18 files changed
+1419
-33
No files matched your search
@@ -0,0 +1,120 @@
|
||||
using System.IO.Compression;
|
||||
using System.Runtime.InteropServices;
|
||||
using CUE4Parse.Compression;
|
||||
|
||||
namespace BlitzKit.CLI.Utils
|
||||
{
|
||||
public static class AgnosticHelpers
|
||||
{
|
||||
public const string BINARY_DIR = "../../temp/";
|
||||
public const string ZLIB_BINARY_URL_PREFIX =
|
||||
"https://github.com/NotOfficer/Zlib-ng.NET/releases/download/1.0.0";
|
||||
public const string OODLE_BINARY_URL_PREFIX =
|
||||
"https://github.com/WorkingRobot/OodleUE/releases/download/2024-11-01-726";
|
||||
|
||||
public static async Task Initialize()
|
||||
{
|
||||
if (!Directory.Exists(BINARY_DIR))
|
||||
{
|
||||
Directory.CreateDirectory(BINARY_DIR);
|
||||
}
|
||||
|
||||
InitializeZlib();
|
||||
await InitializeOodle();
|
||||
}
|
||||
|
||||
public static async Task InitializeOodle()
|
||||
{
|
||||
string url;
|
||||
string extension;
|
||||
string entry;
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
url = $"{OODLE_BINARY_URL_PREFIX}/msvc.zip";
|
||||
entry = "bin/Release/oodle-data-shared.dll";
|
||||
extension = "dll";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
url = $"{OODLE_BINARY_URL_PREFIX}/gcc.zip";
|
||||
entry = "lib/Release/liboodle-data-shared.so";
|
||||
extension = "so";
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
|
||||
string fileName = $"oodle.{extension}";
|
||||
string filePath = Path.Combine(BINARY_DIR, fileName);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
Console.WriteLine($"Oodle found at {filePath}; using that");
|
||||
OodleHelper.Initialize(filePath);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(
|
||||
$"Oodle not found; downloading to {filePath} from {url} and extracting {entry}"
|
||||
);
|
||||
|
||||
HttpClient client = new();
|
||||
using var response = await client.GetAsync(url).ConfigureAwait(false);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
await using var responseStream = await response
|
||||
.Content.ReadAsStreamAsync()
|
||||
.ConfigureAwait(false);
|
||||
using var zip = new ZipArchive(responseStream, ZipArchiveMode.Read);
|
||||
var zipEntry = zip.GetEntry(entry);
|
||||
|
||||
ArgumentNullException.ThrowIfNull(zipEntry, "oodle entry in zip not found");
|
||||
|
||||
await using var entryStream = zipEntry.Open();
|
||||
await using var fs = File.Create(filePath);
|
||||
await entryStream.CopyToAsync(fs).ConfigureAwait(false);
|
||||
|
||||
OodleHelper.Initialize(filePath);
|
||||
}
|
||||
|
||||
public static void InitializeZlib()
|
||||
{
|
||||
string url;
|
||||
string extension;
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
url = $"{ZLIB_BINARY_URL_PREFIX}/zlib-ng2.dll";
|
||||
extension = "dll";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
url = $"{ZLIB_BINARY_URL_PREFIX}/libz-ng.so";
|
||||
extension = "so";
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
|
||||
string fileName = $"zlib.{extension}";
|
||||
string filePath = Path.Combine(BINARY_DIR, fileName);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
Console.WriteLine($"Zlib found at {filePath}; using that");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Zlib not found; downloading to {filePath} from {url}");
|
||||
ZlibHelper.DownloadDll(filePath, url);
|
||||
}
|
||||
|
||||
ZlibHelper.Initialize(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
namespace BlitzKit.CLI.Utils
|
||||
{
|
||||
public class ExplicitlySizedStream(Stream stream, long length) : Stream
|
||||
{
|
||||
private readonly Stream _stream = stream;
|
||||
private readonly long _length = length;
|
||||
private long _position = 0;
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
public override bool CanSeek => _stream.CanSeek;
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
|
||||
public override long Length => _length;
|
||||
public override long Position
|
||||
{
|
||||
get => _position;
|
||||
set => _position = value;
|
||||
}
|
||||
|
||||
public override void Flush() => _stream.Flush();
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_position >= _length)
|
||||
return 0; // End of stream
|
||||
var bytesRead = _stream.Read(buffer, offset, count);
|
||||
_position += bytesRead;
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
long targetPosition = origin switch
|
||||
{
|
||||
SeekOrigin.Begin => offset,
|
||||
SeekOrigin.Current => _position + offset,
|
||||
SeekOrigin.End => _length + offset,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(origin), "Invalid SeekOrigin value."),
|
||||
};
|
||||
|
||||
if (targetPosition < 0 || targetPosition > _length)
|
||||
throw new ArgumentOutOfRangeException(nameof(offset), "Seek position is out of range.");
|
||||
|
||||
// Simulate seek by discarding data up to the target position
|
||||
if (targetPosition != _position)
|
||||
{
|
||||
if (targetPosition < _position)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"Backward seeking is not supported for non-seekable streams."
|
||||
);
|
||||
}
|
||||
|
||||
var toSkip = targetPosition - _position;
|
||||
Skip(toSkip);
|
||||
_position = targetPosition;
|
||||
}
|
||||
|
||||
return _position;
|
||||
}
|
||||
|
||||
private void Skip(long bytes)
|
||||
{
|
||||
byte[] buffer = new byte[8192];
|
||||
while (bytes > 0)
|
||||
{
|
||||
int toRead = (int)Math.Min(buffer.Length, bytes);
|
||||
int read = _stream.Read(buffer, 0, toRead);
|
||||
if (read == 0)
|
||||
throw new EndOfStreamException();
|
||||
bytes -= read;
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_stream.Write(buffer, offset, count);
|
||||
_position += count;
|
||||
}
|
||||
|
||||
// public override void Flush() => _stream.Flush();
|
||||
|
||||
// public override int Read(byte[] buffer, int offset, int count) =>
|
||||
// _stream.Read(buffer, offset, count);
|
||||
|
||||
// public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin);
|
||||
|
||||
// public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
// public override void Write(byte[] buffer, int offset, int count) =>
|
||||
// _stream.Write(buffer, offset, count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
namespace BlitzKit.CLI.Utils
|
||||
{
|
||||
public class IniParser
|
||||
{
|
||||
private readonly Dictionary<string, string> strings = [];
|
||||
private readonly Dictionary<string, List<string>> arrays = [];
|
||||
|
||||
public IniParser(string file)
|
||||
{
|
||||
string content = File.ReadAllText(file);
|
||||
|
||||
foreach (string line in content.Split('\n'))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
string[] parts = line.Split('=');
|
||||
if (parts.Length < 2)
|
||||
continue;
|
||||
|
||||
string key = parts[0].Trim();
|
||||
string value = string.Join("=", parts, 1, parts.Length - 1).Trim();
|
||||
|
||||
if (key.StartsWith('+'))
|
||||
{
|
||||
string keySuffix = key[1..];
|
||||
|
||||
if (arrays.TryGetValue(keySuffix, out List<string>? listValue))
|
||||
{
|
||||
listValue.Add(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
arrays[keySuffix] = [value];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Overwrite existing key instead of throwing an exception
|
||||
strings[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string? GetString(string key) =>
|
||||
strings.TryGetValue(key, out string? value) ? value : null;
|
||||
|
||||
public List<string>? GetArray(string key) =>
|
||||
arrays.TryGetValue(key, out List<string>? value) ? value : null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user