Update IDirectory.cs

Marshalling version
This commit is contained in:
Ac_K 2018-02-19 21:59:52 +01:00 committed by Ezekiel Bethel
commit 198a83d866

View file

@ -1,19 +1,32 @@
using ChocolArm64.Memory; using ChocolArm64.Memory;
using Ryujinx.OsHle.Ipc; using Ryujinx.OsHle.Ipc;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
namespace Ryujinx.OsHle.Objects.FspSrv namespace Ryujinx.OsHle.Objects.FspSrv
{ {
[StructLayout(LayoutKind.Sequential, Size = 0x310)]
struct DirectoryEntry struct DirectoryEntry
{ {
public string Name; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x300)]
public byte[] Name;
public int Unknown;
public byte Type; public byte Type;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3)]
public byte[] Padding;
public long Size; public long Size;
} }
enum DirectoryEntryType
{
Directory,
File
}
class IDirectory : IIpcInterface class IDirectory : IIpcInterface
{ {
private List<DirectoryEntry> DirectoryEntries = new List<DirectoryEntry>(); private List<DirectoryEntry> DirectoryEntries = new List<DirectoryEntry>();
@ -23,8 +36,6 @@ namespace Ryujinx.OsHle.Objects.FspSrv
private string HostPath; private string HostPath;
const int DirectoryEntryType_Directory = 0;
const int DirectoryEntryType_File = 1;
public IDirectory(string HostPath, int flags) public IDirectory(string HostPath, int flags)
{ {
m_Commands = new Dictionary<int, ServiceProcessRequest>() m_Commands = new Dictionary<int, ServiceProcessRequest>()
@ -44,10 +55,12 @@ namespace Ryujinx.OsHle.Objects.FspSrv
{ {
DirectoryEntry Info = new DirectoryEntry DirectoryEntry Info = new DirectoryEntry
{ {
Name = Directory, Name = Encoding.UTF8.GetBytes(Directory),
Type = DirectoryEntryType_Directory, Type = (byte)DirectoryEntryType.Directory,
Size = 0 Size = 0
}; };
Array.Resize(ref Info.Name, 0x300);
DirectoryEntries.Add(Info); DirectoryEntries.Add(Info);
} }
} }
@ -61,22 +74,23 @@ namespace Ryujinx.OsHle.Objects.FspSrv
{ {
DirectoryEntry Info = new DirectoryEntry DirectoryEntry Info = new DirectoryEntry
{ {
Name = Path.GetFileName(FileName), Name = Encoding.UTF8.GetBytes(Path.GetFileName(FileName)),
Type = DirectoryEntryType_File, Type = (byte)DirectoryEntryType.File,
Size = new FileInfo(Path.Combine(HostPath, FileName)).Length Size = new FileInfo(Path.Combine(HostPath, FileName)).Length
}; };
Array.Resize(ref Info.Name, 0x300);
DirectoryEntries.Add(Info); DirectoryEntries.Add(Info);
} }
} }
} }
private int LastItem = 0; private int LastItem = 0;
const int DirectoryEntrySize = 0x310;
public long Read(ServiceCtx Context) public long Read(ServiceCtx Context)
{ {
long BufferPosition = Context.Request.ReceiveBuff[0].Position; long BufferPosition = Context.Request.ReceiveBuff[0].Position;
long BufferLen = Context.Request.ReceiveBuff[0].Size; long BufferLen = Context.Request.ReceiveBuff[0].Size;
long MaxDirectories = BufferLen / DirectoryEntrySize; long MaxDirectories = BufferLen / Marshal.SizeOf(typeof(DirectoryEntry));
if (MaxDirectories > DirectoryEntries.Count - LastItem) if (MaxDirectories > DirectoryEntries.Count - LastItem)
{ {
@ -84,23 +98,17 @@ namespace Ryujinx.OsHle.Objects.FspSrv
} }
int CurrentIndex; int CurrentIndex;
byte[] DirectoryEntry = new byte[DirectoryEntrySize];
for (CurrentIndex = 0; CurrentIndex < MaxDirectories; CurrentIndex++) for (CurrentIndex = 0; CurrentIndex < MaxDirectories; CurrentIndex++)
{ {
int CurrentItem = LastItem + CurrentIndex; int CurrentItem = LastItem + CurrentIndex;
MemoryStream MemStream = new MemoryStream(); byte[] DirectoryEntry = new byte[Marshal.SizeOf(typeof(DirectoryEntry))];
BinaryWriter Writer = new BinaryWriter(MemStream); IntPtr Ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DirectoryEntry)));
Marshal.StructureToPtr(DirectoryEntries[CurrentItem], Ptr, true);
Marshal.Copy(Ptr, DirectoryEntry, 0, Marshal.SizeOf(typeof(DirectoryEntry)));
Marshal.FreeHGlobal(Ptr);
Writer.Write(Encoding.UTF8.GetBytes(DirectoryEntries[CurrentItem].Name)); AMemoryHelper.WriteBytes(Context.Memory, BufferPosition + Marshal.SizeOf(typeof(DirectoryEntry)) * CurrentIndex, DirectoryEntry);
Writer.Seek(0x304, SeekOrigin.Begin);
Writer.Write(DirectoryEntries[CurrentItem].Type);
Writer.Seek(0x308, SeekOrigin.Begin);
Writer.Write(DirectoryEntries[CurrentItem].Size);
MemStream.Seek(0, SeekOrigin.Begin);
MemStream.Read(DirectoryEntry, 0, 0x310);
AMemoryHelper.WriteBytes(Context.Memory, BufferPosition + DirectoryEntrySize * CurrentIndex, DirectoryEntry);
} }
if (LastItem < DirectoryEntries.Count) if (LastItem < DirectoryEntries.Count)