Implement file system API and add shutdown functionality for desktop builds
This commit is contained in:
+68
-2
@@ -1,12 +1,77 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using Zio;
|
||||
using Zio.FileSystems;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static readonly IFileSystem SdFileSystem = new PhysicalFileSystem();
|
||||
private static readonly UPath SdRoot = new UPath(Path.Combine(AppContext.BaseDirectory, "sd").Replace('\\', '/').Replace("C:", "/mnt/c"));
|
||||
|
||||
private static UPath GetSlotFilePath(uint slotIdx, string filePath)
|
||||
{
|
||||
var normalizedPath = (filePath ?? string.Empty).Replace('\\', '/').TrimStart('/');
|
||||
return SdRoot / $"slot{slotIdx}" / normalizedPath;
|
||||
}
|
||||
|
||||
private static readonly Nova64Core.DisplayFlushDelegate DisplayFlush = (buffer, width, height) => { };
|
||||
private static readonly Nova64Core.AudioOutputDelegate AudioOutput = (samples, count) => { };
|
||||
private static readonly Nova64Core.SdReadSectorDelegate SdReadSector = (sectorIdx, destinationBuffer, sectorCount) => false;
|
||||
private static readonly Nova64Core.SdReadFileDelegate SdReadFile = (slotIdx, filePath, destinationBuffer, destinationLength) =>
|
||||
{
|
||||
var path = GetSlotFilePath(slotIdx, filePath);
|
||||
Console.WriteLine($"[Core] SD read request: slot {slotIdx}, file '{filePath}', resolved path '{path}' destination length {destinationLength}");
|
||||
|
||||
if (destinationBuffer == IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine("[Core] SD read request failed: destination buffer is null.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!SdFileSystem.FileExists(path))
|
||||
{
|
||||
Console.WriteLine($"[Core] SD read request failed: file not found '{path}'");
|
||||
return -1;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var stream = SdFileSystem.OpenFile(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var bytesToRead = (int)Math.Min(destinationLength, stream.Length);
|
||||
var buffer = new byte[bytesToRead];
|
||||
var bytesRead = stream.Read(buffer, 0, bytesToRead);
|
||||
Marshal.Copy(buffer, 0, destinationBuffer, bytesRead);
|
||||
return bytesRead;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Core] SD read request failed: {ex.Message}");
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
private static readonly Nova64Core.SdGetFileSizeDelegate SdGetFileSize = (slotIdx, filePath) =>
|
||||
{
|
||||
var path = GetSlotFilePath(slotIdx, filePath);
|
||||
Console.WriteLine($"[Core] SD get file size request: slot {slotIdx}, file '{filePath}', resolved path '{path}'");
|
||||
|
||||
if (!SdFileSystem.FileExists(path))
|
||||
{
|
||||
Console.WriteLine($"[Core] SD get file size failed: file not found '{path}'");
|
||||
return -1;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var length = SdFileSystem.GetFileLength(path);
|
||||
return length > int.MaxValue ? -1 : (int)length;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Core] SD get file size failed: {ex.Message}");
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
private static readonly Nova64Core.CartridgeStatusDelegate CartridgeStatus = (slotIdx, isInserted) => { };
|
||||
private static readonly Nova64Core.LogMessageDelegate LogMessage = (message) =>
|
||||
{
|
||||
@@ -51,7 +116,8 @@ internal static class Program
|
||||
{
|
||||
display_flush = Marshal.GetFunctionPointerForDelegate(DisplayFlush),
|
||||
audio_output = Marshal.GetFunctionPointerForDelegate(AudioOutput),
|
||||
sd_read_sector = Marshal.GetFunctionPointerForDelegate(SdReadSector),
|
||||
sd_read_file = Marshal.GetFunctionPointerForDelegate(SdReadFile),
|
||||
sd_get_file_size = Marshal.GetFunctionPointerForDelegate(SdGetFileSize),
|
||||
cartridge_status = Marshal.GetFunctionPointerForDelegate(CartridgeStatus),
|
||||
log_message = Marshal.GetFunctionPointerForDelegate(LogMessage)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user