~ruther/NosSmooth

ref: 2d0fe0a749c4e7587d3e06cb59759707f34e499e NosSmooth/Local/NosSmooth.LocalBinding/Structs/MapObjBaseList.cs -rw-r--r-- 2.8 KiB
2d0fe0a7 — František Boháček feat(localbinding): split obtaining objects from hooking completely 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
//  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;

/// <summary>
/// List of map objects.
/// </summary>
public class MapObjBaseList : IEnumerable<MapBaseObj>
{
    private readonly IMemory _memory;
    private readonly IntPtr _objListPointer;
    private readonly ArrayPtr<int> _objList;

    /// <summary>
    /// Initializes a new instance of the <see cref="MapObjBaseList"/> class.
    /// </summary>
    /// <param name="memory">The memory.</param>
    /// <param name="objListPointer">The object list pointer.</param>
    public MapObjBaseList(IMemory memory, IntPtr objListPointer)
    {
        memory.Read(objListPointer + 0x04, out uint arrayFirst);
        _objList = new ArrayPtr<int>(arrayFirst, source: memory);
        _memory = memory;
        _objListPointer = objListPointer;
    }

    /// <summary>
    /// Gets the element at the given index.
    /// </summary>
    /// <param name="index">The index of the element.</param>
    /// <exception cref="IndexOutOfRangeException">Thrown if the index is not in the bounds of the array.</exception>
    public MapBaseObj this[int index]
    {
        get
        {
            if (index >= Length || index < 0)
            {
                throw new IndexOutOfRangeException();
            }

            return new MapBaseObj(_memory, (IntPtr)_objList[index]);
        }
    }

    /// <summary>
    /// Gets the length of the array.
    /// </summary>
    public int Length
    {
        get
        {
            _memory.SafeRead(_objListPointer + 0x08, out int length);
            return length;
        }
    }

    /// <inheritdoc/>
    public IEnumerator<MapBaseObj> GetEnumerator()
    {
        return new MapObjBaseEnumerator(this);
    }

    /// <inheritdoc/>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    private class MapObjBaseEnumerator : IEnumerator<MapBaseObj>
    {
        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()
        {
        }
    }
}
Do not follow this link