-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathUndertaleBackground.cs
More file actions
250 lines (220 loc) · 7.81 KB
/
UndertaleBackground.cs
File metadata and controls
250 lines (220 loc) · 7.81 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace UndertaleModLib.Models;
/// <summary>
/// A background or tileset entry in a data file.
/// </summary>
/// <remarks>For GameMaker Studio 2, this will only ever be a tileset. For GameMaker: Studio 1, this is usually a background,
/// but is sometimes repurposed as use for a tileset as well.</remarks>
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class UndertaleBackground : UndertaleNamedResource, IDisposable
{
/// <summary>
/// A tile id, which can be used for referencing specific tiles in a tileset. Game Maker Studio 2 only.
/// </summary>
public class TileID : UndertaleObject, INotifyPropertyChanged
{
private uint _id;
/// <summary>
/// The id of a specific tile.
/// </summary>
public uint ID { get => _id; set { _id = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ID))); } }
/// <inheritdoc />
public event PropertyChangedEventHandler PropertyChanged;
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
writer.Write(ID);
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
ID = reader.ReadUInt32();
}
}
/// <summary>
/// The name of the background.
/// </summary>
public UndertaleString Name { get; set; }
/// <summary>
/// Whether the background should be transparent.
/// </summary>
public bool Transparent { get; set; }
/// <summary>
/// Whether the background should get smoothed.
/// </summary>
public bool Smooth { get; set; }
/// <summary>
/// Whether to preload the background.
/// </summary>
public bool Preload { get; set; }
/// <summary>
/// The <see cref="UndertaleTexturePageItem"/> this background uses.
/// </summary>
public UndertaleTexturePageItem Texture { get; set; }
/// <summary>
/// The version of the tileset format.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2TilesetVersion { get; set; } = 2;
/// <summary>
/// The width of a tile in this tileset.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2TileWidth { get; set; } = 32;
/// <summary>
/// The height of a tile in this tileset.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2TileHeight { get; set; } = 32;
/// <summary>
/// The amount of extra empty pixels left and right next to a tile in this tileset.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2OutputBorderX { get; set; } = 2;
/// <summary>
/// The amount of extra empty pixels above and below a tile in this tileset.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2OutputBorderY { get; set; } = 2;
/// <summary>
/// The amount of columns this tileset has.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2TileColumns { get; set; } = 32;
/// <summary>
/// The number of frames of the tileset animation.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2ItemsPerTileCount { get; set; } = 1;
/// <summary>
/// The amount of tiles this tileset has.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public uint GMS2TileCount { get; set; } = 1024;
/// <summary>
/// Exported sprite index, if the background's corresponding sprite was marked to still be exported.
/// Will be either 0 or -1 (depending on GM version) when the sprite is not exported, which makes this a bit ambiguous.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public int GMS2ExportedSpriteIndex { get; set; } = 0;
/// <summary>
/// The time for each frame in microseconds.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public long GMS2FrameLength { get; set; } = 66666;
/// <summary>
/// All tile ids of this tileset.
/// </summary>
/// <remarks>
/// Added in GameMaker Studio 2.
/// </remarks>
public UndertaleObservableList<TileID> GMS2TileIds { get; set; } = new UndertaleObservableList<TileID>();
/// <remarks>
/// Added in GameMaker 2024.14.1.
/// </remarks>
public uint GMS2TileSeparationX { get; set; } = 0;
/// <remarks>
/// Added in GameMaker 2024.14.1.
/// </remarks>
public uint GMS2TileSeparationY { get; set; } = 0;
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
writer.WriteUndertaleString(Name);
writer.Write(Transparent);
writer.Write(Smooth);
writer.Write(Preload);
writer.WriteUndertaleObjectPointer(Texture);
if (writer.undertaleData.IsGameMaker2())
{
writer.Write(GMS2TilesetVersion);
writer.Write(GMS2TileWidth);
writer.Write(GMS2TileHeight);
if (writer.undertaleData.IsVersionAtLeast(2024, 14, 1))
{
writer.Write(GMS2TileSeparationX);
writer.Write(GMS2TileSeparationY);
}
writer.Write(GMS2OutputBorderX);
writer.Write(GMS2OutputBorderY);
writer.Write(GMS2TileColumns);
writer.Write(GMS2ItemsPerTileCount);
writer.Write(GMS2TileCount);
writer.Write(GMS2ExportedSpriteIndex);
writer.Write(GMS2FrameLength);
if (GMS2TileIds.Count != GMS2TileCount * GMS2ItemsPerTileCount)
throw new UndertaleSerializationException("Bad tile list length, should be tile count * frame count");
for (int i = 0; i < GMS2TileCount * GMS2ItemsPerTileCount; i++)
GMS2TileIds[i].Serialize(writer);
}
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
Name = reader.ReadUndertaleString();
Transparent = reader.ReadBoolean();
Smooth = reader.ReadBoolean();
Preload = reader.ReadBoolean();
Texture = reader.ReadUndertaleObjectPointer<UndertaleTexturePageItem>();
if (reader.undertaleData.IsGameMaker2())
{
GMS2TilesetVersion = reader.ReadUInt32();
GMS2TileWidth = reader.ReadUInt32();
GMS2TileHeight = reader.ReadUInt32();
if (reader.undertaleData.IsVersionAtLeast(2024, 14, 1))
{
GMS2TileSeparationX = reader.ReadUInt32();
GMS2TileSeparationY = reader.ReadUInt32();
}
GMS2OutputBorderX = reader.ReadUInt32();
GMS2OutputBorderY = reader.ReadUInt32();
GMS2TileColumns = reader.ReadUInt32();
GMS2ItemsPerTileCount = reader.ReadUInt32();
GMS2TileCount = reader.ReadUInt32();
GMS2ExportedSpriteIndex = reader.ReadInt32();
GMS2FrameLength = reader.ReadInt64();
GMS2TileIds = new((int)GMS2TileCount * (int)GMS2ItemsPerTileCount);
for (int i = 0; i < GMS2TileCount * GMS2ItemsPerTileCount; i++)
{
TileID id = new TileID();
id.Unserialize(reader);
GMS2TileIds.Add(id);
}
}
}
/// <inheritdoc />
public override string ToString()
{
return Name?.Content + " (" + GetType().Name + ")";
}
/// <inheritdoc/>
public void Dispose()
{
GC.SuppressFinalize(this);
GMS2TileIds = new();
Name = null;
Texture = null;
}
}