Elin Decompiled Documentation EA 23.343.5 Nightly
Loading...
Searching...
No Matches
TileLookup.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEngine;
5
6public class TileLookup<T> where T : SourceData.BaseRow
7{
8 private const int DefaultRange = 1024;
9
10 private readonly Dictionary<int, T> _map;
11
12 private T[] _builtin = Array.Empty<T>();
13
14 private T _fallback;
15
16 private HashSet<int> _missing;
17
18 public T this[int id]
19 {
20 get
21 {
22 T[] builtin = _builtin;
23 if ((uint)id < (uint)builtin.Length)
24 {
25 T val = builtin[id];
26 if (val != null)
27 {
28 return val;
29 }
30 }
31 return Resolve(id);
32 }
33 }
34
35 public TileLookup(Dictionary<int, T> rowsById)
36 {
37 _map = rowsById;
38 }
39
40 public void Build()
41 {
42 _missing = null;
43 _fallback = _map.TryGetValue(0);
44 int maxId = -1;
45 foreach (int item in _map.Keys.Where((int id) => id > maxId && id < 1024))
46 {
47 maxId = item;
48 }
49 _builtin = ((maxId < 0) ? Array.Empty<T>() : new T[maxId + 1]);
50 foreach (KeyValuePair<int, T> item2 in _map.Where((KeyValuePair<int, T> kv) => kv.Key <= maxId))
51 {
52 _builtin[item2.Key] = item2.Value;
53 }
54 }
55
56 private T Resolve(int id)
57 {
58 if (_map.TryGetValue(id, out var value))
59 {
60 return value;
61 }
62 if (_missing == null)
63 {
64 _missing = new HashSet<int>();
65 }
66 if (_missing.Add(id))
67 {
68 Debug.LogWarning($"#source missing tile row: {typeof(T).DeclaringType?.Name}#{id}");
69 }
70 return _fallback ?? _map.TryGetValue(0);
71 }
72}
T Resolve(int id)
Definition: TileLookup.cs:56
HashSet< int > _missing
Definition: TileLookup.cs:16
const int DefaultRange
Definition: TileLookup.cs:8
TileLookup(Dictionary< int, T > rowsById)
Definition: TileLookup.cs:35
readonly Dictionary< int, T > _map
Definition: TileLookup.cs:10
void Build()
Definition: TileLookup.cs:40
T[] _builtin
Definition: TileLookup.cs:12