Skip to content

Commit 77c6df2

Browse files
committed
Renamed "Pointer" and "Address" to "Offset" in LUT classes and made OffsetLut non-abstract
1 parent b73ec01 commit 77c6df2

8 files changed

Lines changed: 251 additions & 247 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Code reused all throughout the SA3D Code Library.
88
|------------------------ |-------------------------------------------------------------------------------------- |
99
| SA3D.Common | Various Utility Methods |
1010
| SA3D.Ascii | Writer for writing Ascii C-structures |
11-
| SA3D.Common.Lookup | 2-way pointer dictionaries for storing pairs with unique values and unique addresses |
11+
| SA3D.Common.Lookup | 2-way pointer dictionaries for storing pairs with unique values and unique offsets |
1212
| SA3D.Common.Ini | Ini data de/serializer. |
1313
| SA3D.Common.IO | Data reader and writer, as well as Executable utilities. |
1414
| SA3D.Common.Converters | Various converters, mostly used in conjunction with Ini data. |

src/SA3D.Common/IO/BinaryHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static ReadNullReferenceException ReadNullReference(this BinaryValueReade
4444
/// <summary>
4545
/// Gets the current offset position in the stream
4646
/// </summary>
47-
/// <param name="reader">The reader to get the pointer address from</param>
47+
/// <param name="reader">The reader to get the pointer offset from</param>
4848
public static long GetPositionOffset(this BinaryObjectReader reader)
4949
{
5050
return reader.OffsetHandler.CalculateOffset(reader.Position);
@@ -53,7 +53,7 @@ public static long GetPositionOffset(this BinaryObjectReader reader)
5353
/// <summary>
5454
/// Gets the current offset position in the stream
5555
/// </summary>
56-
/// <param name="writer">The writer to get the pointer address from</param>
56+
/// <param name="writer">The writer to get the pointer offset from</param>
5757
public static long GetPositionOffset(this BinaryObjectWriter writer)
5858
{
5959
return writer.OffsetHandler.CalculateOffset(writer.Position);

src/SA3D.Common/IO/LookupBinaryHelper.cs

Lines changed: 51 additions & 51 deletions
Large diffs are not rendered by default.

src/SA3D.Common/Lookup/LabelDictionary.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace SA3D.Common.Lookup
66
{
77
/// <summary>
8-
/// A dictionary for mapping labels to addresses.
8+
/// A dictionary for mapping labels to offsets.
99
/// </summary>
10-
public sealed partial class LabelDictionary : PointerDictionary<string>
10+
public sealed partial class LabelDictionary : OffsetDictionary<string>
1111
{
1212
[GeneratedRegex("(?![0-9A-Za-z_]).")]
1313
private static partial Regex IllegalCharactersCheck();
@@ -31,56 +31,56 @@ public LabelDictionary(Dictionary<long, string> labels) : base()
3131
/// <summary>
3232
/// Adds a new label to the dictionary. If the name is already taken, it gets preceded by a number to keep it unique.
3333
/// </summary>
34-
/// <param name="address">The address to add.</param>
34+
/// <param name="offset">The offset to add.</param>
3535
/// <param name="label">The label to add.</param>
3636
/// <returns>The label as it was added.</returns>
3737
/// <exception cref="ArgumentException"/>
38-
public string AddSafe(long address, string label)
38+
public string AddSafe(long offset, string label)
3939
{
4040
label = IllegalCharactersCheck().Replace(label, "_");
4141

42-
if(_toAddr.ContainsKey(label))
42+
if(_toOffset.ContainsKey(label))
4343
{
4444
int append = 1;
45-
while(_toAddr.ContainsKey($"{label}_{append}"))
45+
while(_toOffset.ContainsKey($"{label}_{append}"))
4646
{
4747
append++;
4848
}
4949

5050
label = $"{label}_{append}";
5151
}
5252

53-
Add(address, label);
53+
Add(offset, label);
5454
return label;
5555
}
5656

5757
/// <summary>
58-
/// Returns either the found address, or assembled a custom label for the address
58+
/// Returns either the found offset, or assembled a custom label for the offset
5959
/// </summary>
60-
/// <param name="address">The lookup address</param>
60+
/// <param name="offset">The lookup offset</param>
6161
/// <param name="prefix">The prefix to use when creating a custom label</param>
6262
/// <returns></returns>
63-
public string GetSafe(long address, string prefix)
63+
public string GetSafe(long offset, string prefix)
6464
{
65-
if(!TryGetValue(address, out string? result))
65+
if(!TryGetValue(offset, out string? result))
6666
{
67-
result = prefix + address.ToString("X8");
67+
result = prefix + offset.ToString("X8");
6868
}
6969

7070
return result;
7171
}
7272

7373
/// <summary>
74-
/// Attempts to get the label for the specified address. If none is found, it will return a hexadecimal representation of the address prefixed with the specified prefix.
74+
/// Attempts to get the label for the specified offset. If none is found, it will return a hexadecimal representation of the offset prefixed with the specified prefix.
7575
/// </summary>
76-
/// <param name="address">The address to get the label for.</param>
76+
/// <param name="offset">The offset to get the label for.</param>
7777
/// <param name="prefix">The prefix to add when no label is found.</param>
7878
/// <returns>The found or generated label.</returns>
79-
public string GetGenerateValue(long address, string prefix)
79+
public string GetGenerateValue(long offset, string prefix)
8080
{
81-
return TryGetValue(address, out string? result)
81+
return TryGetValue(offset, out string? result)
8282
? result
83-
: $"{prefix}{address:X8}";
83+
: $"{prefix}{offset:X8}";
8484
}
8585

8686
}

src/SA3D.Common/Lookup/LookupExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public static class LookupExtensions
1414
/// <summary>
1515
/// Utility function for checking whether a dictionary is null. Needed for reliable Amicitia.IO serialization
1616
/// </summary>
17-
/// <param name="pointerDictionary">The dictionary to check for null</param>
17+
/// <param name="offsetDictionary">The dictionary to check for null</param>
1818
/// <exception cref="NullReferenceException"></exception>
19-
public static void NullReferenceCheck<T>([NotNull] this PointerDictionary<T>? pointerDictionary) where T : notnull
19+
public static void NullReferenceCheck<T>([NotNull] this OffsetDictionary<T>? offsetDictionary) where T : notnull
2020
{
21-
if(pointerDictionary == null)
21+
if(offsetDictionary == null)
2222
{
23-
throw new NullReferenceException("No pointer lookup dictionary!");
23+
throw new NullReferenceException("No offset lookup dictionary!");
2424
}
2525
}
2626

@@ -29,7 +29,7 @@ public static void NullReferenceCheck<T>([NotNull] this PointerDictionary<T>? po
2929
/// </summary>
3030
/// <param name="lut">The lookup table to check for null</param>
3131
/// <exception cref="NullReferenceException"></exception>
32-
public static void NullReferenceCheck([NotNull] this BaseLUT? lut)
32+
public static void NullReferenceCheck([NotNull] this OffsetLUT? lut)
3333
{
3434
if(lut == null)
3535
{
@@ -41,9 +41,9 @@ public static void NullReferenceCheck([NotNull] this BaseLUT? lut)
4141
/// Adds a value to the LUT at the current writing position of a <see cref="BinaryObjectWriter"/>
4242
/// </summary>
4343
/// <param name="lut">Lookup table to add the value to</param>
44-
/// <param name="writer">The writer from which to get the pointer</param>
44+
/// <param name="writer">The writer from which to get the offset</param>
4545
/// <param name="value">The value to add</param>
46-
public static void AddForWriter<T>(this BaseLUT lut, BinaryObjectWriter writer, T value) where T : class
46+
public static void AddForWriter<T>(this OffsetLUT lut, BinaryObjectWriter writer, T value) where T : class
4747
{
4848
if(value is IList list && list.Count == 0)
4949
{
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
namespace SA3D.Common.Lookup
6+
{
7+
/// <summary>
8+
/// A dictionary for mapping unique objects to unique offsets.
9+
/// </summary>
10+
/// <typeparam name="T">Type of the objects to map</typeparam>
11+
public class OffsetDictionary<T> where T : notnull
12+
{
13+
private protected readonly Dictionary<long, T> _fromOffset;
14+
private protected readonly Dictionary<T, long> _toOffset;
15+
16+
/// <summary>
17+
/// Number of entries in the dictionary
18+
/// </summary>
19+
public int Count => _fromOffset.Count;
20+
21+
/// <summary>
22+
/// Creates a new pointer dictionary
23+
/// </summary>
24+
public OffsetDictionary()
25+
{
26+
_fromOffset = [];
27+
_toOffset = [];
28+
}
29+
30+
/// <summary>
31+
/// Gets the value mapped to the specified offset.
32+
/// </summary>
33+
/// <param name="offset">The offset of the value to get.</param>
34+
/// <returns>The value if successful; <see langword="default"/> if unsuccessful.</returns>
35+
/// <exception cref="ArgumentNullException"/>
36+
public T? GetValue(long offset)
37+
{
38+
if(_fromOffset.TryGetValue(offset, out T? value))
39+
{
40+
return value;
41+
}
42+
43+
return default;
44+
}
45+
46+
/// <summary>
47+
/// Gets the offset mapped to the specifed value.
48+
/// </summary>
49+
/// <param name="value">The value of the offset to get.</param>
50+
/// <returns>The offset if successful; <see langword="null"/> if unsuccessful.</returns>
51+
/// <exception cref="ArgumentNullException"/>
52+
public long? GetAddress(T value)
53+
{
54+
if(_toOffset.TryGetValue(value, out long offset))
55+
{
56+
return offset;
57+
}
58+
59+
return null;
60+
}
61+
62+
/// <summary>
63+
/// Gets the offset value to the specifed offset.
64+
/// </summary>
65+
/// <param name="offset">The offset to get the value of.</param>
66+
/// <param name="result">When this method returns, contains the value associated with the specified offset, if the offset is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
67+
/// <returns><see langword="true"/> if the <see cref="OffsetDictionary{T}"/> contains a value with the specified offset, otherwise <see langword="false"/>.</returns>
68+
/// <exception cref="ArgumentNullException"/>
69+
public bool TryGetValue(long offset, [MaybeNullWhen(false)] out T result)
70+
{
71+
return _fromOffset.TryGetValue(offset, out result);
72+
}
73+
74+
/// <summary>
75+
/// Gets the offset to the specifed offset.
76+
/// </summary>
77+
/// <param name="value">The value to get the offset of.</param>
78+
/// <param name="result">When this method returns, contains the offset associated with the specified value, if the value is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param>
79+
/// <returns><see langword="true"/> if the <see cref="OffsetDictionary{T}"/> contains an offset with the specified value, otherwise <see langword="false"/>.</returns>
80+
/// <exception cref="ArgumentNullException"/>
81+
public bool TryGetAddress(T value, [MaybeNullWhen(false)] out long result)
82+
{
83+
return _toOffset.TryGetValue(value, out result);
84+
}
85+
86+
/// <summary>
87+
/// Adds a new offset/value pair to the dictionary.
88+
/// </summary>
89+
/// <param name="offset">The offset to add.</param>
90+
/// <param name="value">The value to add.</param>
91+
/// <exception cref="ArgumentException"/>
92+
public void Add(long offset, T value)
93+
{
94+
if(!_fromOffset.TryAdd(offset, value))
95+
{
96+
throw new ArgumentException($"An item with the same offset has already been added. Address: {offset:X8}");
97+
}
98+
99+
if(!_toOffset.TryAdd(value, offset))
100+
{
101+
_fromOffset.Remove(offset);
102+
throw new ArgumentException($"An item with the same value has already been added. Value: {value}");
103+
}
104+
}
105+
106+
/// <summary>
107+
/// Attempts to add a new offset/value pair to the dictionary.
108+
/// </summary>
109+
/// <param name="offset"></param>
110+
/// <param name="value"></param>
111+
/// <returns><see langword="true"/> if the offset/value pair was successfully added to the dictionary; otherwise <see langword="false"/>.</returns>
112+
public bool TryAdd(long offset, T value)
113+
{
114+
if(!_fromOffset.TryAdd(offset, value))
115+
{
116+
return false;
117+
}
118+
119+
if(!_toOffset.TryAdd(value, offset))
120+
{
121+
_fromOffset.Remove(offset);
122+
return false;
123+
}
124+
125+
return true;
126+
}
127+
128+
/// <summary>
129+
/// Get a copy of the dictionary mapping values to offsets.
130+
/// </summary>
131+
/// <returns></returns>
132+
public Dictionary<long, T> GetDictFrom()
133+
{
134+
return new(_fromOffset);
135+
}
136+
137+
/// <summary>
138+
/// Get a copy of the dictionary mapping offsets to values.
139+
/// </summary>
140+
/// <returns></returns>
141+
public Dictionary<T, long> GetDictTo()
142+
{
143+
return new(_toOffset);
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)