-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMechTransfer.cs
339 lines (285 loc) · 13.4 KB
/
MechTransfer.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using MechTransfer.ContainerAdapters;
using MechTransfer.Items;
using MechTransfer.Tiles;
using MechTransfer.Tiles.Simple;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using EnumerateItemsDelegate = System.Func<int, int, System.Collections.Generic.IEnumerable<System.Tuple<Terraria.Item, object>>>;
using InjectItemDelegate = System.Func<int, int, Terraria.Item, bool>;
using TakeItemDelegate = System.Action<int, int, object, int>;
namespace MechTransfer
{
public class MechTransfer : Mod
{
private const string callErorPrefix = "MechTransfer Call() error: ";
private const string registerAdapter = "RegisterAdapter";
private const string registerAdapterReflection = "RegisterAdapterReflection";
private List<Action> simpleTileAddRecipequeue;
private Mod modMagicStorage = null;
public override object Call(params object[] args)
{
if ((args[0] as string) == registerAdapter)
{
if (args.Length != 5)
{
this.Logger.Error(callErorPrefix + "Invalid number of arguments at " + registerAdapter);
return null;
}
ContainerAdapterDefinition definition = new ContainerAdapterDefinition();
if (!(args[1] is InjectItemDelegate))
{
this.Logger.Error(callErorPrefix + "Invalid argument 2 InjectItem at " + registerAdapter);
return null;
}
definition.InjectItem = args[1] as InjectItemDelegate;
if (!(args[2] is EnumerateItemsDelegate))
{
this.Logger.Error(callErorPrefix + "Invalid argument 3 EnumerateItems at " + registerAdapter);
return null;
}
definition.EnumerateItems = args[2] as EnumerateItemsDelegate;
if (!(args[3] is TakeItemDelegate))
{
this.Logger.Error(callErorPrefix + "Invalid argument 4 TakeItem at " + registerAdapter);
return null;
}
definition.TakeItem = args[3] as TakeItemDelegate;
if (!(args[4] is int[]))
{
this.Logger.Error(callErorPrefix + "Invalid argument 5 TileType at " + registerAdapter);
return null;
}
foreach (var type in (int[])args[4])
{
if (!ModContent.GetInstance<TransferAgent>().ContainerAdapters.ContainsKey(type))
ModContent.GetInstance<TransferAgent>().ContainerAdapters.Add(type, definition);
}
return definition;
}
if ((args[0] as string) == registerAdapterReflection)
{
try
{
ContainerAdapterDefinition definition = new ContainerAdapterDefinition();
Type type = args[1].GetType();
ParameterExpression paramX = Expression.Parameter(typeof(int));
ParameterExpression paramY = Expression.Parameter(typeof(int));
MethodInfo inject = type.GetMethod("InjectItem", new Type[] { typeof(int), typeof(int), typeof(Item) });
ParameterExpression paramInjectItem = Expression.Parameter(typeof(Item));
InjectItemDelegate injectLambda = Expression.Lambda<InjectItemDelegate>(
Expression.Call(Expression.Constant(args[1]), inject, paramX, paramY, paramInjectItem),
paramX, paramY, paramInjectItem).Compile();
MethodInfo enumerate = type.GetMethod("EnumerateItems", new Type[] { typeof(int), typeof(int) });
EnumerateItemsDelegate enumerateLambda = Expression.Lambda<EnumerateItemsDelegate>(
Expression.Call(Expression.Constant(args[1]), enumerate, paramX, paramY),
paramX, paramY).Compile();
MethodInfo take = type.GetMethod("TakeItem", new Type[] { typeof(int), typeof(int), typeof(object), typeof(int) });
ParameterExpression paramTakeIdentifier = Expression.Parameter(typeof(object));
ParameterExpression paramTakeAmount = Expression.Parameter(typeof(int));
TakeItemDelegate takeLambda = Expression.Lambda<TakeItemDelegate>(
Expression.Call(Expression.Constant(args[1]), take, paramX, paramY, paramTakeIdentifier, paramTakeAmount),
paramX, paramY, paramTakeIdentifier, paramTakeAmount).Compile();
definition.InjectItem = injectLambda;
definition.EnumerateItems = enumerateLambda;
definition.TakeItem = takeLambda;
if (!(args[2] is int[]))
{
this.Logger.Error(callErorPrefix + "Invalid argument 5 TileType at " + registerAdapterReflection);
return null;
}
foreach (var t in (int[])args[2])
{
if (!ModContent.GetInstance<TransferAgent>().ContainerAdapters.ContainsKey(t))
ModContent.GetInstance<TransferAgent>().ContainerAdapters.Add(t, definition);
}
return definition;
}
catch (Exception e)
{
this.Logger.Error(callErorPrefix + "An exception has occurred while loading adapter at " + registerAdapterReflection);
this.Logger.Error(e.Message);
return null;
}
}
this.Logger.Error(callErorPrefix + "Invalid command");
return null;
}
public override void HandlePacket(BinaryReader reader, int whoAmI)
{
NetRouter.RouteMessage(reader, whoAmI);
}
public override void Load()
{
ModLoader.TryGetMod("MagicStorage", out modMagicStorage);
Assembly asm = Assembly.GetExecutingAssembly();
simpleTileAddRecipequeue = new List<Action>();
Dictionary<Type, List<int>> TEValid = new Dictionary<Type, List<int>>();
SimpleTileEntity.validTiles = new Dictionary<int, int[]>();
foreach (var item in asm.GetTypes())
{
if (item.IsAbstract)
continue;
if (item.IsSubclassOf(typeof(SimpleTile)))
{
SimpleTile tile = (SimpleTile)Activator.CreateInstance(item);
AddContent(tile);
tile.PostLoad();
simpleTileAddRecipequeue.Add(new Action(tile.AddRecipes));
Type TEType;
if (IsTETile(item, out TEType))
{
if (!TEValid.ContainsKey(TEType))
TEValid.Add(TEType, new List<int>());
TEValid[TEType].Add(tile.Type);
}
}
}
foreach (var item in asm.GetTypes())
{
if (item.IsSubclassOf(typeof(SimpleTileEntity)))
{
SimpleTileEntity TE = (SimpleTileEntity)Activator.CreateInstance(item);
AddContent(TE);
if (TEValid.ContainsKey(item))
{
SimpleTileEntity.validTiles.Add(TE.Type, TEValid[item].ToArray());
}
else
{
SimpleTileEntity.validTiles.Add(TE.Type, new int[0]);
}
TE.PostLoadPrototype();
}
}
}
private bool IsTETile(Type type, out Type TEType)
{
List<Type> bases = new List<Type>();
Type baseType = type.BaseType;
while (baseType != typeof(object))
{
if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(SimpleTETile<>))
break;
baseType = baseType.BaseType;
}
if (baseType == typeof(object))
{
TEType = null;
return false;
}
else
{
TEType = type.GetMethod("GetEntity").ReturnType;
return true;
}
}
public override void PostSetupContent()
{
LoadAdapters();
}
private void LoadAdapters()
{
//Item frame
ItemFrameAdapter itemFrameAdapter = new ItemFrameAdapter();
Call(registerAdapterReflection, itemFrameAdapter, new int[] { TileID.ItemFrame });
//Snowball launcher
SnowballLauncherAdapter snowballLauncherAdapter = new SnowballLauncherAdapter();
Call(registerAdapterReflection, snowballLauncherAdapter, new int[] { TileID.SnowballLauncher });
//Cannon
CannonAdapter cannonAdapter = new CannonAdapter();
Call(registerAdapterReflection, cannonAdapter, new int[] { TileID.Cannon });
//Crystal stand
CrystalStandAdapter crystalStandAdapter = new CrystalStandAdapter();
Call(registerAdapterReflection, crystalStandAdapter, new int[] { TileID.ElderCrystalStand });
//Weapon rack
WeaponRackAdapter weaponRackAdapter = new WeaponRackAdapter();
Call(registerAdapterReflection, weaponRackAdapter, new int[] { TileID.WeaponsRack });
//tModLoader Weapon rack
WeaponRack2Adapter weaponRack2Adapter = new WeaponRack2Adapter();
Call(registerAdapterReflection, weaponRack2Adapter, new int[] { TileID.WeaponsRack2 });
//Omni turret
OmniTurretAdapter omniTurretAdapter = new OmniTurretAdapter(this);
Call(registerAdapterReflection, omniTurretAdapter, new int[] { ModContent.TileType<OmniTurretTile>() });
//Extractinator
ExtractinatorAdapter extractinatorAdapter = new ExtractinatorAdapter();
Call(registerAdapterReflection, extractinatorAdapter, new int[] { TileID.Extractinator, TileID.ChlorophyteExtractinator });
//Player interface
PlayerInterfaceAdapter playerInterfaceAdapter = new PlayerInterfaceAdapter(this);
Call(registerAdapterReflection, playerInterfaceAdapter, new int[] { ModContent.TileType<PlayerInterfaceTile>() });
if (modMagicStorage != null)
{
//Magic storage interface
MagicStorageInterfaceAdapter magicStorageInterfaceAdapter = new MagicStorageInterfaceAdapter();
Call(registerAdapterReflection, magicStorageInterfaceAdapter, new int[] { ModContent.TileType<MagicStorageInterfaceTile>() });
}
}
//This needs to be called from SetupRecipies, because chests are made in SetupContent.
private void LoadChestAdapters()
{
ChestAdapter chestAdapter = new ChestAdapter(this);
List<int> chestTypes = new List<int>();
for (int i = 0; i < TileLoader.TileCount; i++)
{
if (TileID.Sets.BasicChest[i] || TileID.Sets.BasicChestFake[i] || TileID.Sets.BasicDresser[i]) // TileLoader.IsDresser
{
chestTypes.Add(i);
}
}
Call(registerAdapterReflection, chestAdapter, chestTypes.ToArray());
}
public override void AddRecipes()
{
foreach (var item in simpleTileAddRecipequeue)
{
item();
}
simpleTileAddRecipequeue = null;
if (modMagicStorage != null)
{
//Magic storage interface
Recipe r = Recipe.Create(Find<ModItem>("MagicStorageInterfaceItem").Item.type);
r.AddIngredient(modMagicStorage.Find<ModItem>("StorageComponent"));
r.AddRecipeGroup("MagicStorage:AnyDiamond", 1);
r.AddIngredient(ModContent.ItemType<PneumaticActuatorItem>(), 1);
r.AddTile(TileID.WorkBenches);
r.Register();
}
LoadChestAdapters();
}
public override void PostAddRecipes()
{
NetRouter.Init(0);
}
public override void Unload()
{
NetRouter.Unload();
}
}
public static class ModExtensions
{
public static ModItem GetPlaceItem<T>(this Mod mod) where T : SimplePlaceableTile
{
SimplePlaceableTile tile = ModContent.GetInstance<T>();
return tile.PlaceItem;
}
public static ModItem GetPlaceItem<T>(this Mod mod, int kind) where T : SimpleTileObject
{
SimpleTileObject tile = ModContent.GetInstance<T>();
return tile.GetPlaceItem(kind);
}
public static int PlaceItemType<T>(this Mod mod) where T : SimplePlaceableTile
{
return GetPlaceItem<T>(mod).Item.type;
}
public static int PlaceItemType<T>(this Mod mod, int style) where T : SimpleTileObject
{
return GetPlaceItem<T>(mod, style).Item.type;
}
}
}