forked from altmp/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityRepository.cs
92 lines (78 loc) · 3.1 KB
/
EntityRepository.cs
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
using System;
using System.Collections.Generic;
namespace AltV.Net.EntitySync
{
public class EntityRepository : IEntityRepository
{
private readonly EntityThreadRepository[] entityThreadRepositories;
private readonly ulong threadCount;
private readonly Func<IEntity, ulong, ulong> entityThreadId;
private readonly Func<ulong, ulong, ulong, ulong> entityIdAndTypeThreadId;
public EntityRepository(EntityThreadRepository[] entityThreadRepositories, Func<IEntity, ulong, ulong> entityThreadId, Func<ulong, ulong, ulong, ulong> entityIdAndTypeThreadId)
{
this.entityThreadRepositories = entityThreadRepositories;
threadCount = (ulong) entityThreadRepositories.Length;
this.entityThreadId = entityThreadId;
this.entityIdAndTypeThreadId = entityIdAndTypeThreadId;
}
// Entity id needs to start at 0 to work for this
public void Add(IEntity entity)
{
if (entity == null)
{
throw new ArgumentException("Entity must not be null.");
}
entityThreadRepositories[entityThreadId(entity, threadCount)].Add(entity);
}
public void Remove(IEntity entity)
{
if (entity == null)
{
throw new ArgumentException("Entity must not be null.");
}
entityThreadRepositories[entityThreadId(entity, threadCount)].Remove(entity);
}
public void Remove(ulong id, ulong type)
{
entityThreadRepositories[entityIdAndTypeThreadId(id, type, threadCount)].Remove(id, type);
}
public void Update(IEntity entity)
{
if (entity == null)
{
throw new ArgumentException("Entity must not be null.");
}
entityThreadRepositories[entityThreadId(entity, threadCount)].Update(entity);
}
public void UpdateData(IEntity entity, string key, object value)
{
if (entity == null)
{
throw new ArgumentException("Entity must not be null.");
}
entityThreadRepositories[entityThreadId(entity, threadCount)].UpdateData(entity, key, value);
}
public void ResetData(IEntity entity, string key)
{
if (entity == null)
{
throw new ArgumentException("Entity must not be null.");
}
entityThreadRepositories[entityThreadId(entity, threadCount)].ResetData(entity, key);
}
public bool TryGet(ulong id, ulong type, out IEntity entity)
{
return entityThreadRepositories[entityIdAndTypeThreadId(id, type, threadCount)].TryGet(id, type, out entity);
}
public IEnumerable<IEntity> GetAll()
{
foreach (var entityThreadRepository in entityThreadRepositories)
{
foreach (var entity in entityThreadRepository.GetAll())
{
yield return entity;
}
}
}
}
}