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,17 +1,30 @@
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 Type; public byte[] Name;
public long Size; public int Unknown;
public byte Type;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3)]
public byte[] Padding;
public long Size;
}
enum DirectoryEntryType
{
Directory,
File
} }
class IDirectory : IIpcInterface class IDirectory : IIpcInterface
@ -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>()
@ -39,15 +50,17 @@ namespace Ryujinx.OsHle.Objects.FspSrv
{ {
string[] Directories = Directory.GetDirectories(HostPath, "*", SearchOption.TopDirectoryOnly). string[] Directories = Directory.GetDirectories(HostPath, "*", SearchOption.TopDirectoryOnly).
Where(x => (new FileInfo(x).Attributes & FileAttributes.Hidden) == 0).ToArray(); Where(x => (new FileInfo(x).Attributes & FileAttributes.Hidden) == 0).ToArray();
foreach (string Directory in Directories) foreach (string Directory in Directories)
{ {
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);
} }
} }
@ -56,27 +69,28 @@ namespace Ryujinx.OsHle.Objects.FspSrv
{ {
string[] Files = Directory.GetFiles(HostPath, "*", SearchOption.TopDirectoryOnly). string[] Files = Directory.GetFiles(HostPath, "*", SearchOption.TopDirectoryOnly).
Where(x => (new FileInfo(x).Attributes & FileAttributes.Hidden) == 0).ToArray(); Where(x => (new FileInfo(x).Attributes & FileAttributes.Hidden) == 0).ToArray();
foreach (string FileName in Files) foreach (string FileName in Files)
{ {
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);
Writer.Write(Encoding.UTF8.GetBytes(DirectoryEntries[CurrentItem].Name)); Marshal.Copy(Ptr, DirectoryEntry, 0, Marshal.SizeOf(typeof(DirectoryEntry)));
Writer.Seek(0x304, SeekOrigin.Begin); Marshal.FreeHGlobal(Ptr);
Writer.Write(DirectoryEntries[CurrentItem].Type);
Writer.Seek(0x308, SeekOrigin.Begin);
Writer.Write(DirectoryEntries[CurrentItem].Size);
MemStream.Seek(0, SeekOrigin.Begin); AMemoryHelper.WriteBytes(Context.Memory, BufferPosition + Marshal.SizeOf(typeof(DirectoryEntry)) * CurrentIndex, DirectoryEntry);
MemStream.Read(DirectoryEntry, 0, 0x310);
AMemoryHelper.WriteBytes(Context.Memory, BufferPosition + DirectoryEntrySize * CurrentIndex, DirectoryEntry);
} }
if (LastItem < DirectoryEntries.Count) if (LastItem < DirectoryEntries.Count)