Skip to content

Commit 1bdff05

Browse files
committed
Use read-only collection types in IMemoryApi
1 parent cba206e commit 1bdff05

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,8 @@ private void WriteUnsigned(long addr, uint value, int size, string domain = null
185185

186186
public void SetBigEndian(bool enabled = true) => _isBigEndian = enabled;
187187

188-
public List<string> GetMemoryDomainList() =>
189-
DomainList
190-
.Select(domain => domain.Name)
191-
.ToList();
188+
public IReadOnlyCollection<string> GetMemoryDomainList()
189+
=> DomainList.Select(static domain => domain.Name).ToList();
192190

193191
public uint GetMemoryDomainSize(string name = null) => (uint) NamedDomainOrCurrent(name).Size;
194192

@@ -243,7 +241,7 @@ public string HashRegion(long addr, int count, string domain = null)
243241

244242
public void WriteByte(long addr, uint value, string domain = null) => WriteUnsigned(addr, value, 1, domain);
245243

246-
public List<byte> ReadByteRange(long addr, int length, string domain = null)
244+
public IReadOnlyList<byte> ReadByteRange(long addr, int length, string domain = null)
247245
{
248246
var d = NamedDomainOrCurrent(domain);
249247
if (addr < 0) LogCallback($"Warning: Attempted reads on addresses {addr}..-1 outside range of domain {d.Name} in {nameof(ReadByteRange)}()");
@@ -255,10 +253,10 @@ public List<byte> ReadByteRange(long addr, int length, string domain = null)
255253
for (var i = addr < 0 ? -addr : 0; i != indexAfterLast; i++) bytes[i] = d.PeekByte(addr + i);
256254
}
257255
if (lastReqAddr >= d.Size) LogCallback($"Warning: Attempted reads on addresses {d.Size}..{lastReqAddr} outside range of domain {d.Name} in {nameof(ReadByteRange)}()");
258-
return bytes.ToList();
256+
return bytes;
259257
}
260258

261-
public void WriteByteRange(long addr, List<byte> memoryblock, string domain = null)
259+
public void WriteByteRange(long addr, IReadOnlyList<byte> memoryblock, string domain = null)
262260
{
263261
var d = NamedDomainOrCurrent(domain);
264262
if (!d.Writable)

src/BizHawk.Client.Common/Api/Interfaces/IMemoryApi.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ public interface IMemoryApi : IExternalApi
88

99
void SetBigEndian(bool enabled = true);
1010

11-
List<string> GetMemoryDomainList();
11+
IReadOnlyCollection<string> GetMemoryDomainList();
1212
uint GetMemoryDomainSize(string name = "");
1313
string GetCurrentMemoryDomain();
1414
uint GetCurrentMemoryDomainSize();
1515
bool UseMemoryDomain(string domain);
1616
string HashRegion(long addr, int count, string domain = null);
1717

1818
uint ReadByte(long addr, string domain = null);
19-
List<byte> ReadByteRange(long addr, int length, string domain = null);
19+
IReadOnlyList<byte> ReadByteRange(long addr, int length, string domain = null);
2020
float ReadFloat(long addr, string domain = null);
2121

2222
int ReadS8(long addr, string domain = null);
@@ -30,7 +30,7 @@ public interface IMemoryApi : IExternalApi
3030
uint ReadU32(long addr, string domain = null);
3131

3232
void WriteByte(long addr, uint value, string domain = null);
33-
void WriteByteRange(long addr, List<byte> memoryblock, string domain = null);
33+
void WriteByteRange(long addr, IReadOnlyList<byte> memoryblock, string domain = null);
3434
void WriteFloat(long addr, double value, string domain = null);
3535

3636
void WriteS8(long addr, int value, string domain = null);

src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Linq;
45

@@ -19,7 +20,7 @@ public MemoryLuaLibrary(IPlatformLuaLibEnv luaLibsImpl, ApiContainer apiContaine
1920
[LuaMethod("getmemorydomainlist", "Returns a string of the memory domains for the loaded platform core. List will be a single string delimited by line feeds")]
2021
[return: LuaASCIIStringParam]
2122
public LuaTable GetMemoryDomainList()
22-
=> _th.ListToTable(APIs.Memory.GetMemoryDomainList(), indexFrom: 0);
23+
=> _th.ListToTable((List<string>) APIs.Memory.GetMemoryDomainList(), indexFrom: 0); //HACK cast will succeed as long as impl. returns .Select<T, string>().ToList() as IROC<string>
2324

2425
[LuaMethodExample("local uimemget = memory.getmemorydomainsize( mainmemory.getname( ) );")]
2526
[LuaMethod("getmemorydomainsize", "Returns the number of bytes of the specified memory domain. If no domain is specified, or the specified domain doesn't exist, returns the current domain size")]

0 commit comments

Comments
 (0)