forked from KhloeLeclair/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModApi.cs
167 lines (129 loc) · 4.32 KB
/
ModApi.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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Leclair.Stardew.CloudySkies.Effects;
using Leclair.Stardew.CloudySkies.LayerData;
using Leclair.Stardew.CloudySkies.Layers;
using Leclair.Stardew.CloudySkies.Models;
using Leclair.Stardew.Common.Types;
using StardewModdingAPI;
namespace Leclair.Stardew.CloudySkies;
public class ModApi : ICloudySkiesApi {
private readonly ModEntry Mod;
private readonly IManifest Other;
public ModApi(ModEntry mod, IManifest other) {
Mod = mod;
Other = other;
}
public string WeatherAssetName => ModEntry.DATA_ASSET;
public string ContextAssetName => ModEntry.EXTENSION_DATA_ASSET;
public void RegenerateLayers(string? weatherId = null) {
Mod.UncacheLayers(weatherId);
}
public void RegenerateEffects(string? weatherId = null) {
Mod.UncacheEffects(weatherId);
}
public IEnumerable<IWeatherData> GetAllCustomWeather() {
Mod.LoadWeatherData();
foreach (var data in Mod.Data)
yield return data.Value;
}
public IEnumerable<ILocationContextExtensionData> GetAllContextData() {
Mod.LoadContextData();
foreach (var data in Mod.ContextData)
yield return data.Value;
}
public bool TryGetContextData(string id, [NotNullWhen(true)] out ILocationContextExtensionData? data) {
if (Mod.TryGetContextData(id, out var cdata)) {
data = cdata;
return data is not null;
}
data = null;
return false;
}
public bool TryGetWeather(string id, [NotNullWhen(true)] out IWeatherData? data) {
if (Mod.TryGetWeather(id, out var weather)) {
data = weather;
return data is not null;
}
data = null;
return false;
}
private static readonly Dictionary<Type, Func<object?>> WeatherDataEditorTypes = new() {
{ typeof(IWeatherData), () => new WeatherData() },
{ typeof(IScreenTintData), () => new ScreenTintData() },
{ typeof(ICustomLayerData), () => new CustomLayerData() },
{ typeof(ICustomEffectData), () => new CustomEffectData() },
// When creating our discriminated types, make sure to set Type.
{ typeof(IColorLayerData), () => new ColorLayerData() {
Type = "Color"
} },
{ typeof(IDebrisLayerData), () => new DebrisLayerData() {
Type = "Debris"
} },
{ typeof(IRainLayerData), () => new RainLayerData() {
Type = "Rain"
} },
{ typeof(ITextureScrollLayerData), () => new TextureScrollLayerData() {
Type = "TextureScroll"
} },
{ typeof(IBuffEffectData), () => new BuffEffectData() {
Type = "Buff"
} },
{ typeof(IModifyHealthEffectData), () => new ModifyHealthEffectData() {
Type = "ModifyHealth"
} },
{ typeof(IModifyStaminaEffectData), () => new ModifyStaminaEffectData() {
Type = "ModifyStamina"
} },
{ typeof(ITriggerEffectData), () => new TriggerEffectData() {
Type = "Trigger"
} }
};
public IModAssetEditor<IWeatherData> GetWeatherEditor(IAssetData assetData) {
return new ModAssetEditor<ModEntry, WeatherData, IWeatherData>(
Mod,
Other,
assetData,
data => data.Id,
(data, id) => data.Id = id,
WeatherDataEditorTypes
);
}
public IModAssetEditor<ILocationContextExtensionData> GetContextEditor(IAssetData assetData) {
return new ModAssetEditor<ModEntry, LocationContextExtensionData, ILocationContextExtensionData>(
Mod,
Other,
assetData,
data => data.Id,
(data, id) => data.Id = id
);
}
public bool RegisterLayerType(string type, ICloudySkiesApi.WeatherLayerFactoryDelegate factory) {
IWeatherLayer? CreateLayer(ulong id, ILayerData data) {
if (data is not ICustomLayerData customData)
return null;
var result = factory(id, customData);
return result is null ? null : new WrappedLayer(Mod, result);
}
return Mod.RegisterLayerType(type, CreateLayer);
}
public bool UnregisterLayerType(string type) {
return Mod.UnregisterLayerType(type);
}
public bool RegisterEffectType(string type, ICloudySkiesApi.WeatherEffectFactoryDelegate factory) {
IWeatherEffect? CreateEffect(ulong id, IEffectData data) {
if (data is not ICustomEffectData customData)
return null;
var result = factory(id, customData);
return result is null ? null : new WrappedEffect(Mod, result);
}
return Mod.RegisterEffectType(type, CreateEffect);
}
public bool UnregisterEffectType(string type) {
return Mod.UnregisterEffectType(type);
}
public void NotifyLoadsAsset(ulong id, string assetName) {
Mod.MarkLoadsAsset(id, assetName);
}
}