Implement file system API and add shutdown functionality for desktop builds
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
@@ -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.
Reference in New Issue
Block a user