forked from DRKV333/ShopExpander
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShopExpanderMod.cs
More file actions
170 lines (136 loc) · 5.27 KB
/
ShopExpanderMod.cs
File metadata and controls
170 lines (136 loc) · 5.27 KB
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
namespace ShopExpander;
using Patches;
using Providers;
using Terraria.Localization;
// ReSharper disable once ClassNeverInstantiated.Global
public class ShopExpanderMod : Mod
{
private static bool _textureSetupDone;
public static CircularBufferProvider Buyback { get; private set; } = null!;
public static ModItem ArrowLeft { get; private set; } = null!;
public static ModItem ArrowRight { get; private set; } = null!;
public static ShopAggregator? ActiveShop { get; private set; }
// Ignore all shops from this npc
internal static HashSet<int> NpcTypeIgnoreList { get; private set; } = null!;
// Ignore specific shop from this npc
internal static HashSet<(int Type, string ShopName)> NpcShopIgnoreList { get; private set; } = null!;
[MemberNotNull(nameof(ActiveShop))]
public static void ResetAndBindShop()
{
ActiveShop = new ShopAggregator();
ActiveShop.AddPage(Buyback = new(Language.GetTextValue("Mods.ShopExpander.Items.Buyback"), ProviderPriority.Buyback));
Main.instance.shop[Main.npcShop].item = ActiveShop.CurrentFrame;
}
public override void Load()
{
NpcTypeIgnoreList = new();
NpcShopIgnoreList = new();
ArrowLeft = new ArrowItem("ArrowLeft");
AddContent(ArrowLeft);
ArrowRight = new ArrowItem("ArrowRight");
AddContent(ArrowRight);
}
public override void PostSetupContent()
{
SetupShopPatch.Load();
AddShopPatch.Load();
LeftRightClickPatch.Load();
if (!Main.dedServ)
{
Main.QueueMainThreadAction(() =>
{
TextureAssets.Item[ArrowLeft.Item.type] = TextureAsset(CropTexture(TextureAssets.TextGlyph[0].Value, new Rectangle(4 * 28, 0, 28, 28)));
TextureAssets.Item[ArrowRight.Item.type] = TextureAsset(CropTexture(TextureAssets.TextGlyph[0].Value, new Rectangle(5 * 28, 0, 28, 28)));
_textureSetupDone = true;
});
}
}
public override void Unload()
{
NpcTypeIgnoreList = null!;
NpcShopIgnoreList = null!;
SetupShopPatch.Unload();
Main.QueueMainThreadAction(() =>
{
if (!_textureSetupDone)
{
return;
}
TextureAssets.Item[ArrowLeft.Item.type].Value.Dispose();
TextureAssets.Item[ArrowRight.Item.type].Value.Dispose();
});
}
public override object? Call(params object[] args)
{
if (args[0] is not string command)
{
throw new ArgumentException("first argument must be string");
}
int argNum = 1;
switch (command)
{
case CallApi.AddPageFromArray:
{
if (ActiveShop == null)
{
throw new InvalidOperationException($"No active shop, try calling {CallApi.ResetAndBindShop} first");
}
var name = AssertAndCast<string>(args, ref argNum, CallApi.AddPageFromArray);
var priority = AssertAndCast<int>(args, ref argNum, CallApi.AddPageFromArray);
var items = AssertAndCast<Item[]>(args, ref argNum, CallApi.AddPageFromArray);
ActiveShop.AddPage(new ArrayProvider(name, priority, items));
break;
}
case CallApi.ResetAndBindShop:
{
ResetAndBindShop();
break;
}
case CallApi.GetLastShopExpanded:
{
return ActiveShop?.GetAllItems().ToArray();
}
case CallApi.AddNpcTypeToIgnoreList:
{
int npcType = AssertAndCast<int>(args, ref argNum, CallApi.AddNpcTypeToIgnoreList);
return NpcTypeIgnoreList.Add(npcType);
}
case CallApi.AddNpcShopToIgnoreList:
{
int npcType = AssertAndCast<int>(args, ref argNum, CallApi.AddNpcShopToIgnoreList);
string shopName = AssertAndCast<string>(args, ref argNum, CallApi.AddNpcShopToIgnoreList);
return NpcShopIgnoreList.Add((npcType, shopName));
}
default:
{
throw new ArgumentException($"Unknown command: {command}");
}
}
return null;
}
private static T AssertAndCast<T>(object[] args, ref int index, string site)
{
if (args[index] is not T casted)
{
throw new ArgumentException($"args[{index}] must be {typeof(T).Name} for {site}");
}
index++;
return casted;
}
private static Texture2D CropTexture(Texture2D texture, Rectangle newBounds)
{
var newTexture = new Texture2D(Main.graphics.GraphicsDevice, newBounds.Width, newBounds.Height);
var area = newBounds.Width * newBounds.Height;
var data = new Color[area];
texture.GetData(0, newBounds, data, 0, area);
newTexture.SetData(data);
return newTexture;
}
private Asset<Texture2D> TextureAsset(Texture2D texture)
{
using MemoryStream stream = new(texture.Width * texture.Height);
texture.SaveAsPng(stream, texture.Width, texture.Height);
stream.Position = 0;
return Assets.CreateUntracked<Texture2D>(stream, ".png");
}
}