forked from Violet-CLM/MLLE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlusAngelscriptLibrary.cs
342 lines (310 loc) · 14.2 KB
/
PlusAngelscriptLibrary.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
340
341
342
using System.Collections.Generic;
using System.IO;
using Ionic.Zlib;
using Ionic.Crc;
using System.Text.RegularExpressions;
namespace MLLE
{
public partial struct PlusPropertyList
{
const uint CurrentMLLEData5Version = 0x104;
const string MLLEData5MagicString = "MLLE";
const string CurrentMLLEData5VersionStringForComparison = "0x104";
const string CurrentMLLEData5VersionString = "1.4";
const string AngelscriptLibraryFilename = "MLLE-Include-" + CurrentMLLEData5VersionString + ".asc";
const string AngelscriptLibraryCallStockLine = "const bool MLLESetupSuccessful = MLLE::Setup();\r\n";
const string AngelscriptLibrary =
@"//This is a standard library created by MLLE to read some JJ2+ properties from a level file whose script includes this library. DO NOT MANUALLY MODIFY THIS FILE.
#pragma require '" + AngelscriptLibraryFilename + @"'
namespace MLLE {
jjPAL@ Palette;
dictionary@ _layers;
bool Setup() {
jjPAL palette = jjBackupPalette;
@Palette = @palette;
dictionary layers;
@_layers = @layers;
jjSTREAM crcCheck('" + AngelscriptLibraryFilename + @"');
string crcLine;
if (crcCheck.isEmpty() || !crcCheck.getLine(crcLine)) {
jjDebug('MLLE::Setup: Include file has been renamed!');
return false;
}
array<string> regexResults;
if (!jjRegexMatch(crcLine, '\\/\\/(\\d+)\\r?', regexResults)) {
jjDebug('MLLE::Setup: Include file is improperly formatted!');
return false;
}
if (parseUInt(regexResults[1]) != jjCRC32(crcCheck)) {
jjDebug('MLLE::Setup: Include file has been damaged!');
return false;
}
jjSTREAM level(jjLevelFileName);
if (level.isEmpty()) {
jjDebug('MLLE::Setup: Level file cannot be read from that folder for security reasons!');
return false;
}
level.discard(230);
array<uint> CompressedDataSizes(4, 0);
for (uint i = 0; i < CompressedDataSizes.length; ++i) {
level.pop(CompressedDataSizes[i]);
level.discard(4);
}
for (uint i = 0; i < CompressedDataSizes.length; ++i)
level.discard(CompressedDataSizes[i]);
if (level.getSize() < 20) {
jjDebug('MLLE::Setup: Level file does not contain any additional data!');
return false;
}
string magic;
level.get(magic, '" + MLLEData5MagicString + @"'.length);
if (magic != '" + MLLEData5MagicString + @"') {
jjDebug('MLLE::Setup: Level was not saved by MLLE!');
return false;
}
uint levelDataVersion;
level.pop(levelDataVersion);
if (levelDataVersion != " + CurrentMLLEData5VersionStringForComparison + @") {
jjDebug('MLLE::Setup: Level\'s Data5 section was saved in a different version of MLLE than this script!');
return false;
}
uint csize, usize;
level.pop(csize); level.pop(usize);
jjSTREAM data5;
if (!jjZlibUncompress(level, data5, usize)) {
jjDebug('MLLE::Setup: Error during ZLIB uncompression!');
return false;
}
bool pbool; uint8 pbyte; int8 pchar; float pfloat; int pint; uint puint, puint2;
data5.pop(pbool); jjIsSnowing = pbool;
data5.pop(pbool); jjIsSnowingOutdoorsOnly = pbool;
data5.pop(pbyte); jjSnowingIntensity = pbyte;
data5.pop(pbyte); jjSnowingType = SNOWING::Type(pbyte);
if (jjIsSnowing) {
if (jjSnowingType == SNOWING::SNOW && jjAnimSets[ANIM::SNOW] == 0)
jjAnimSets[ANIM::SNOW].load();
else if (jjSnowingType == SNOWING::LEAF && jjAnimSets[ANIM::PLUS_SCENERY] == 0)
jjAnimSets[ANIM::PLUS_SCENERY].load();
}
data5.pop(pbool); jjWarpsTransmuteCoins = pbool;
data5.pop(pbool); jjDelayGeneratedCrateOrigins = pbool;
data5.pop(pint); jjEcho = pint;
data5.pop(puint); jjSetDarknessColor(_colorFromArgb(puint));
data5.pop(pfloat);jjWaterChangeSpeed = pfloat;
data5.pop(pbyte); jjWaterInteraction = WATERINTERACTION::WaterInteraction(pbyte);
data5.pop(pint); jjWaterLayer = pint;
data5.pop(pbyte); jjWaterLighting = WATERLIGHT::wl(pbyte);
data5.pop(pfloat); if (int(pfloat) < jjLayerHeight[4] * 32) jjSetWaterLevel(pfloat, true);
data5.pop(puint); data5.pop(puint2); jjSetWaterGradient(_colorFromArgb(puint), _colorFromArgb(puint2));
data5.pop(pbool); if (pbool) {
for (uint i = 0; i < 256; ++i) {
data5.pop(palette.color[i].red);
data5.pop(palette.color[i].green);
data5.pop(palette.color[i].blue);
}
palette.apply();
}
_recolorAnimationIf(data5, ANIM::PINBALL, 0, 4);
_recolorAnimationIf(data5, ANIM::PINBALL, 2, 4);
_recolorAnimationIf(data5, ANIM::CARROTPOLE, 0, 1);
_recolorAnimationIf(data5, ANIM::DIAMPOLE, 0, 1);
_recolorAnimationIf(data5, ANIM::PINBALL, 4, 8);
_recolorAnimationIf(data5, ANIM::JUNGLEPOLE, 0, 1);
_recolorAnimationIf(data5, ANIM::PLUS_SCENERY, 0, 17);
_recolorAnimationIf(data5, ANIM::PSYCHPOLE, 0, 1);
_recolorAnimationIf(data5, ANIM::SMALTREE, 0, 1);
_recolorAnimationIf(data5, ANIM::SNOW, 0, 8);
_recolorAnimationIf(data5, ANIM::COMMON, 2, 18);
data5.pop(pbyte);
for (uint i = 0; i < pbyte; ++i) {
string tilesetFilename = _read7BitEncodedStringFromStream(data5);
uint16 tileStart, tileCount;
data5.pop(tileStart); data5.pop(tileCount);
array<uint8>@ colors = null;
data5.pop(pbool); if (pbool) {
@colors = array<uint8>(256);
for (uint j = 0; j < 256; ++j)
data5.pop(colors[j]);
}
jjTilesFromTileset(tilesetFilename, tileStart, tileCount, colors);
}
if (pbyte != 0) {
array<uint> layersIDsWithTileMaps;
for (uint i = 1; i <= 8; ++i)
if (jjLayers[i].hasTileMap)
layersIDsWithTileMaps.insertLast(i);
jjLayersFromLevel(jjLevelFileName, layersIDsWithTileMaps);
}
array<jjLAYER@> newLayerOrder, nonDefaultLayers;
data5.pop(puint);
for (uint i = 8; i < puint; i += 8) {
array<uint> layerIDsToGrab;
for (uint j = i; j < puint && j < i + 8; ++j) {
layerIDsToGrab.insertLast((j & 7) + 1);
}
array<jjLAYER@> extraLayers = jjLayersFromLevel(jjLevelFileName.substr(0, jjLevelFileName.length() - 4) + '-MLLE-Data-' + (i/8) + '.j2l', layerIDsToGrab);
for (uint j = 0; j < extraLayers.length(); ++j)
nonDefaultLayers.insertLast(extraLayers[j]);
}
uint nextNonDefaultLayerID = 0;
for (uint i = 0; i < puint; ++i) {
data5.pop(pchar);
jjLAYER@ layer;
if (pchar >= 0)
@layer = jjLayers[pchar + 1];
else
@layer = nonDefaultLayers[nextNonDefaultLayerID++];
string layerName = _read7BitEncodedStringFromStream(data5);
_layers.set(layerName, @layer);
data5.pop(pbool);
if (layer.hasTileMap)
layer.hasTiles = !pbool;
data5.pop(pbyte);
layer.spriteMode = SPRITE::Mode(pbyte);
data5.pop(pbyte);
layer.spriteParam = pbyte;
data5.pop(pint);
layer.rotationAngle = pint;
data5.pop(pint);
layer.rotationRadiusMultiplier = pint;
newLayerOrder.insertLast(layer);
}
jjLayerOrderSet(newLayerOrder);
uint16 numberOfImages; data5.pop(numberOfImages);
for (uint16 i = 0; i < numberOfImages; ++i) {
uint16 tileID; data5.pop(tileID);
jjPIXELMAP tile(32, 32);
for (int y = 0; y < 32; ++y)
for (int x = 0; x < 32; ++x)
data5.pop(tile[x,y]);
tile.save(tileID, true);
}
data5.pop(numberOfImages);
for (uint16 i = 0; i < numberOfImages; ++i) {
uint16 tileID; data5.pop(tileID);
jjMASKMAP tile;
for (int y = 0; y < 32; ++y)
for (int x = 0; x < 32; ++x)
data5.pop(tile[x,y]);
tile.save(tileID, true);
}
if (!data5.isEmpty()) {
jjDebug('MLLE::Setup: Warning, Data5 longer than expected');
}
return true;
}
jjLAYER@ GetLayer(const string &in name) {
jjLAYER@ handle = null;
_layers.get(name, @handle);
return handle;
}
jjPALCOLOR _colorFromArgb(uint Argb) {
return jjPALCOLOR(Argb >> 16, Argb >> 8, Argb >> 0);
}
uint _read7BitEncodedUintFromStream(jjSTREAM@ stream) {
uint result = 0;
while (true) {
uint8 byteRead; stream.pop(byteRead);
result |= (byteRead & 0x7F);
if (byteRead >= 0x80)
result <<= 7;
else
break;
}
return result;
}
string _read7BitEncodedStringFromStream(jjSTREAM@ stream) {
string result;
stream.get(result, _read7BitEncodedUintFromStream(stream));
return result;
}
void _recolorAnimationIf(jjSTREAM@ stream, ANIM::Set set, uint animID, uint frameCount) {
bool pbool; stream.pop(pbool); if (!pbool) return;
if (jjAnimSets[set] == 0)
jjAnimSets[set].load();
const uint firstFrameID = jjAnimations[jjAnimSets[set] + animID];
array<uint8> colors(256);
for (uint i = 0; i < 256; ++i)
stream.pop(colors[i]);
for (uint i = 0; i < frameCount; ++i) {
jjANIMFRAME@ frame = jjAnimFrames[firstFrameID + i];
jjPIXELMAP image(frame);
for (uint x = 0; x < image.width; ++x)
for (uint y = 0; y < image.height; ++y)
image[x,y] = colors[image[x,y]];
image.save(frame);
}
}
}";
static string GetPragmaRequire(string filename)
{
return "#pragma require \"" + filename + "\"\r\n";
}
public static string GetExtraDataLevelFilepath(string filepath, int index)
{
return Path.Combine(Path.GetDirectoryName(filepath), Path.GetFileNameWithoutExtension(filepath) + "-MLLE-Data-" + (index + 1) + ".j2l");
}
internal void SaveLibrary(string filepath, List<J2TFile> Tilesets, int numberOfExtraDataLevels)
{
var encoding = J2LFile.FileEncoding;
using (BinaryWriter binwriter = new BinaryWriter(File.Open(Path.Combine(Path.GetDirectoryName(filepath), AngelscriptLibraryFilename), FileMode.Create, FileAccess.Write), encoding)) {
binwriter.Write(encoding.GetBytes("//"));
var libraryFileAsBytes = encoding.GetBytes(AngelscriptLibrary);
CRC32 CRCCalculator = new CRC32();
CRCCalculator.SlurpBlock(libraryFileAsBytes, 0, libraryFileAsBytes.Length);
binwriter.Write(encoding.GetBytes(((uint)CRCCalculator.Crc32Result).ToString() + "\r\n"));
binwriter.Write(libraryFileAsBytes);
}
string scriptFilepath = Path.ChangeExtension(filepath, ".j2as");
string fileContents = "";
if (File.Exists(scriptFilepath))
fileContents = System.IO.File.ReadAllText(scriptFilepath, encoding);
{
string pragma = GetPragmaRequire(Path.GetFileName(filepath));
if (!fileContents.Contains(pragma))
fileContents = pragma + fileContents;
}
for (int i = 1; i < Tilesets.Count; ++i)
{
string pragma = GetPragmaRequire(Tilesets[i].FilenameOnly);
if (!fileContents.Contains(pragma))
fileContents = pragma + fileContents;
}
int extraDataLevelID = 0;
for (extraDataLevelID = 0; extraDataLevelID < numberOfExtraDataLevels; ++extraDataLevelID)
{
string pragma = GetPragmaRequire(Path.GetFileName(GetExtraDataLevelFilepath(filepath, extraDataLevelID)));
if (!fileContents.Contains(pragma))
fileContents = pragma + fileContents;
}
while (true) //remove extra such pragmas/files if the number of layers has decreased since the last time this level was saved
{
string extraFilepath = GetExtraDataLevelFilepath(filepath, extraDataLevelID++);
File.Delete(extraFilepath);
string pragma = GetPragmaRequire(Path.GetFileName(extraFilepath));
if (fileContents.Contains(pragma))
fileContents = fileContents.Replace(pragma, "");
else
break;
}
if (!fileContents.Contains("MLLE::Setup()"))
fileContents = AngelscriptLibraryCallStockLine + fileContents;
System.IO.File.WriteAllText(scriptFilepath, "#include \"" + AngelscriptLibraryFilename + "\"\r\n" + fileContents, encoding);
}
public static void RemovePriorReferencesToMLLELibrary(string filepath)
{
string scriptFilepath = Path.ChangeExtension(filepath, ".j2as");
if (File.Exists(scriptFilepath))
{
var encoding = J2LFile.FileEncoding;
string fileContents = System.IO.File.ReadAllText(scriptFilepath, encoding);
fileContents = fileContents.Replace(AngelscriptLibraryCallStockLine, ""); //get rid of the simpler old uses of MLLE::Setup(), though not all can be so painlessly removed
fileContents = Regex.Replace(fileContents, "\\s*#include\\s+['\"]MLLE-Include-\\d+\\.\\d+\\.asc['\"]\\s*\\r?\\n?", ""); //get rid of existing #include calls to MLLE-Include, especially if they referenced older/newer versions of the file
if (fileContents.Length > 0)
System.IO.File.WriteAllText(scriptFilepath, fileContents, encoding);
else
File.Delete(scriptFilepath);
}
}
}
}