//
// MapObjBaseList.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections;
using Reloaded.Memory.Pointers;
using Reloaded.Memory.Sources;
namespace NosSmooth.LocalBinding.Structs;
///
/// List of map objects.
///
public class MapObjBaseList : IEnumerable
{
private readonly IMemory _memory;
private readonly IntPtr _objListPointer;
private readonly ArrayPtr _objList;
///
/// Initializes a new instance of the class.
///
/// The memory.
/// The object list pointer.
public MapObjBaseList(IMemory memory, IntPtr objListPointer)
{
memory.Read(objListPointer + 0x04, out uint arrayFirst);
_objList = new ArrayPtr(arrayFirst, source: memory);
_memory = memory;
_objListPointer = objListPointer;
}
///
/// Gets the element at the given index.
///
/// The index of the element.
/// Thrown if the index is not in the bounds of the array.
public MapBaseObj this[int index]
{
get
{
if (index >= Length || index < 0)
{
throw new IndexOutOfRangeException();
}
return new MapBaseObj(_memory, (IntPtr)_objList[index]);
}
}
///
/// Gets the length of the array.
///
public int Length
{
get
{
_memory.SafeRead(_objListPointer + 0x08, out int length);
return length;
}
}
///
public IEnumerator GetEnumerator()
{
return new MapObjBaseEnumerator(this);
}
///
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private class MapObjBaseEnumerator : IEnumerator
{
private readonly MapObjBaseList _list;
private int _index;
public MapObjBaseEnumerator(MapObjBaseList list)
{
_index = -1;
_list = list;
}
public bool MoveNext()
{
if (_list.Length > _index + 1)
{
_index++;
return true;
}
return false;
}
public void Reset()
{
_index = -1;
}
public MapBaseObj Current => _list[_index];
object IEnumerator.Current => Current;
public void Dispose()
{
}
}
}