-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathItemInterop.cs
224 lines (190 loc) · 6.28 KB
/
ItemInterop.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.Runtime.CompilerServices;
using FortressCraft.Community.ItemInterops;
namespace FortressCraft.Community
{
/// <summary>
/// A class that provides Interopability between Storage and Machines for Pre-StorageInterface mods
/// </summary>
[Obsolete("Official Item Interfaces are released, use those. http://steamcommunity.com/app/254200/discussions/1/364040166673615738/ (ItemInterop no longer required, All machines use official Item Interfaces~)", true)]
public static class ItemInterop
{
// Not truly readonly, it can be accessed and manually added to via Reflection
// But if someone is that desparate, than whatever.
private static readonly Dictionary<Type, ItemInteropInterface> _supportedTypes;
static ItemInterop()
{
_supportedTypes = new Dictionary<Type, ItemInteropInterface>
{
{ typeof (StorageHopper), new StorageHopperInterop() },
{ typeof (ConveyorEntity), new ConveyorEntityInterop() }
};
}
private static Type SupportedType(SegmentEntity entity)
{
if (_supportedTypes.ContainsKey(entity.GetType()))
return entity.GetType();
return entity is CommunityItemInterface ? typeof (CommunityItemInterface) : null;
}
/// <summary>
/// Checks to see if the <see cref="SegmentEntity">entity</see> has any items
/// </summary>
/// <param name="entity">A <see cref="SegmentEntity">SegmentEntity</see></param>
/// <returns>True if the <see cref="SegmentEntity">entity</see> has any items, false otherwise</returns>
public static Boolean HasItems(SegmentEntity entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
var type = SupportedType(entity);
if (type == null)
return false;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.HasItems();
}
var interop = _supportedTypes[type];
return interop.HasItems(entity);
}
public static Boolean HasItem(SegmentEntity entity, ItemBase item)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (item == null)
throw new ArgumentNullException(nameof(item));
Int32 amount;
return HasItems(entity, item, out amount);
}
public static Boolean HasItems(SegmentEntity entity, ItemBase item, out Int32 amount)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (item == null)
throw new ArgumentNullException(nameof(item));
amount = 0;
var type = SupportedType(entity);
if (type == null)
return false;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.HasItems(item, out amount);
}
var interop = _supportedTypes[type];
return interop.HasItems(entity, item, out amount);
}
public static Boolean HasCapacity(SegmentEntity entity, UInt32 amount)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
var type = SupportedType(entity);
if (type == null)
return false;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.HasFreeSpace(amount);
}
var interop = _supportedTypes[type];
return interop.HasFreeSpace(entity, amount);
}
public static Int32 GetCapacity(SegmentEntity entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
var type = SupportedType(entity);
if (type == null)
return 0;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.GetFreeSpace();
}
var interop = _supportedTypes[type];
return interop.GetFreeSpace(entity);
}
/// <summary>
///
/// </summary>
/// <param name="entity"></param>
/// <param name="item"></param>
/// <returns></returns>
public static Boolean GiveItem(SegmentEntity entity, ItemBase item)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (item == null)
throw new ArgumentNullException(nameof(item));
var type = SupportedType(entity);
if (type == null)
return false;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.GiveItem(item);
}
var interop = _supportedTypes[type];
return interop.GiveItem(entity, item);
}
/// <summary>
/// Will attempt to return the requested <see cref="ItemBase">item</see> from the <see cref="SegmentEntity">entity</see>
/// </summary>
/// <param name="entity">The <see cref="SegmentEntity">entity</see> to try and get the <see cref="ItemBase">item</see> from</param>
/// <param name="item">The <see cref="ItemBase">ItemBase</see> with details of what to retrrieve</param>
/// <returns></returns>
public static ItemBase TakeItem(SegmentEntity entity, ItemBase item)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (item == null)
throw new ArgumentNullException(nameof(item));
try
{
var type = SupportedType(entity);
if (type == null)
return null;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.TakeItem(item);
}
var interop = _supportedTypes[type];
return interop.TakeItem(entity, item);
}
catch(NotImplementedException)
{
return null;
}
}
/// <summary>
/// Will return any <see cref="ItemBase">item</see> from the <see cref="SegmentEntity">entity</see>
/// </summary>
/// <param name="entity">The <see cref="SegmentEntity">entity</see> to get a <see cref="ItemBase">item</see> from</param>
/// <returns>A <see cref="ItemBase">item</see>, or <c>NULL</c></returns>
public static ItemBase TakeAnyItem(SegmentEntity entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
try
{
var type = SupportedType(entity);
if (type == null)
return null;
if (type == typeof (CommunityItemInterface))
{
var iEntity = entity as CommunityItemInterface;
return iEntity.TakeAnyItem();
}
var interop = _supportedTypes[type];
return interop.TakeAnyItem(entity);
}
catch (NotImplementedException)
{
return null;
}
}
}
}