Implement file system API and add shutdown functionality for desktop builds

This commit is contained in:
Lukas Höppner
2026-08-09 22:38:43 +02:00
parent 1654d366b2
commit 8e99fca404
13 changed files with 318 additions and 25 deletions
+7 -3
View File
@@ -10,8 +10,11 @@ internal static class Nova64Core
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void AudioOutputDelegate(IntPtr samples, uint count);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool SdReadSectorDelegate(uint sectorIdx, IntPtr destinationBuffer, uint sectorCount);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate int SdReadFileDelegate(uint slotIdx, string filePath, IntPtr destinationBuffer, uint destinationLength);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate int SdGetFileSizeDelegate(uint slotIdx, string filePath);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CartridgeStatusDelegate(uint slotIdx, bool isInserted);
@@ -24,7 +27,8 @@ internal static class Nova64Core
{
public IntPtr display_flush;
public IntPtr audio_output;
public IntPtr sd_read_sector;
public IntPtr sd_read_file;
public IntPtr sd_get_file_size;
public IntPtr cartridge_status;
public IntPtr log_message;
}
+8
View File
@@ -12,6 +12,14 @@
<NativeLibDir>$(NativeLibDir)</NativeLibDir>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Zio" Version="0.24.0" />
</ItemGroup>
<ItemGroup>
<Content Include="sd\**\*" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup Condition="'$(NativeLibDir)' != ''">
<Content Include="$(NativeLibDir)\nova64_core.dll" Condition="Exists('$(NativeLibDir)\nova64_core.dll')" CopyToOutputDirectory="IfDifferent" />
<Content Include="$(NativeLibDir)\libnova64_core.so" Condition="Exists('$(NativeLibDir)\libnova64_core.so')" CopyToOutputDirectory="IfDifferent" />
+68 -2
View File
@@ -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)
};
Binary file not shown.