-
Notifications
You must be signed in to change notification settings - Fork 45
/
TilesetStageExecutor.ts
252 lines (236 loc) · 8.62 KB
/
TilesetStageExecutor.ts
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
import { TilesetStage } from "./TilesetStage";
import { ContentStageExecutor } from "./ContentStageExecutor";
import { PipelineError } from "./PipelineError";
import { TilesetStages } from "./TilesetStages";
import { BasicTilesetProcessor } from "../tilesetProcessing/BasicTilesetProcessor";
import { TilesetUpgrader } from "../tilesetProcessing/TilesetUpgrader";
import { TilesetCombiner } from "../tilesetProcessing/TilesetCombiner";
import { ContentDataTypeChecks } from "../contentTypes/ContentDataTypeChecks";
import { ContentDataTypes } from "../contentTypes/ContentDataTypes";
import { TilesetDataProcessor } from "../tilesetProcessing/TilesetDataProcessor";
import { TilesetEntry } from "../tilesetData/TilesetEntry";
import { Buffers } from "../base/Buffers";
/**
* Methods to execute `TilesetStage` objects.
*/
export class TilesetStageExecutor {
/**
* Executes the given `TilesetStage`.
*
* @param tilesetStage - The `TilesetStage` object
* @param currentInput - The current input name, or a temporary
* name for intermediate steps (see `Pipeline.input` for details)
* @param currentOutput - The current output name, or a temporary
* name for intermediate steps (see `Pipeline.input` for details)
* @param overwrite - Whether outputs should be overwritten if
* they already exist
* @returns A promise that resolves when the process is finished
* @throws PipelineError If one of the processing steps causes
* an error.
*/
static async executeTilesetStage(
tilesetStage: TilesetStage,
currentInput: string,
currentOutput: string,
overwrite: boolean
) {
console.log(` Executing tilesetStage : ${tilesetStage.name}`);
console.log(` currentInput: ${currentInput}`);
console.log(` currentOutput: ${currentOutput}`);
try {
await TilesetStageExecutor.executeTilesetStageInternal(
tilesetStage,
currentInput,
currentOutput,
overwrite
);
} catch (e) {
throw new PipelineError(`${e}`);
}
}
/**
* Implementation for `executeTilesetStage`.
*
* For details about the arguments, see `executeTilesetStage`.
*
* @param tilesetStage - The `TilesetStage` object
* @param currentInput - The current input name
* @param currentOutput - The current output name
* @param overwrite - Whether outputs should be overwritten
* @returns A promise that resolves when the process is finished
* @throws Error If one of the processing steps causes
* an error.
*/
private static async executeTilesetStageInternal(
tilesetStage: TilesetStage,
currentInput: string,
currentOutput: string,
overwrite: boolean
) {
if (tilesetStage.name === TilesetStages.TILESET_STAGE_GZIP) {
const condition = ContentDataTypeChecks.createTypeCheck(
tilesetStage.includedContentTypes,
tilesetStage.excludedContentTypes
);
await TilesetStageExecutor.executeGzip(
currentInput,
currentOutput,
overwrite,
condition
);
} else if (tilesetStage.name === TilesetStages.TILESET_STAGE_UNGZIP) {
await TilesetStageExecutor.executeGunzip(
currentInput,
currentOutput,
overwrite
);
} else if (tilesetStage.name === TilesetStages.TILESET_STAGE_UPGRADE) {
const quiet = false;
const gltfUpgradeOptions = undefined;
const tilesetUpgrader = new TilesetUpgrader(quiet, gltfUpgradeOptions);
await tilesetUpgrader.upgrade(currentInput, currentOutput, overwrite);
} else if (tilesetStage.name === TilesetStages.TILESET_STAGE_COMBINE) {
const externalTilesetDetector = ContentDataTypeChecks.createIncludedCheck(
ContentDataTypes.CONTENT_TYPE_TILESET
);
const tilesetCombiner = new TilesetCombiner(externalTilesetDetector);
await tilesetCombiner.combine(currentInput, currentOutput, overwrite);
} else {
await TilesetStageExecutor.executeTilesetContentStages(
tilesetStage,
currentInput,
currentOutput,
overwrite
);
}
}
/**
* Performs the 'gzip' tileset stage with the given parameters.
*
* This will process all entries of the source tileset. The
* data of entries that match the given condition will be
* compressed with gzip. Other entries remain unaffected.
*
* @param currentInput - The current input name
* @param currentOutput - The current output name
* @param overwrite - Whether outputs should be overwritten
* @param condition The condition that was created from
* the included- and excluded types that have been defined
* in the `ContentStage`.
* @returns A promise that resolves when the process is finished
* @throws Error If one of the processing steps causes
* an error.
*/
private static async executeGzip(
currentInput: string,
currentOutput: string,
overwrite: boolean,
condition: (type: string | undefined) => boolean
): Promise<void> {
const quiet = true;
const tilesetProcessor = new TilesetDataProcessor(quiet);
await tilesetProcessor.begin(currentInput, currentOutput, overwrite);
// The entry processor receives the source entry, and
// returns a target entry where the `value` is zipped
// if the source entry matches the given condition.
const entryProcessor = async (
sourceEntry: TilesetEntry,
type: string | undefined
) => {
let targetValue = sourceEntry.value;
const shouldZip = condition(type);
if (shouldZip) {
targetValue = Buffers.gzip(sourceEntry.value);
}
const targetEntry = {
key: sourceEntry.key,
value: targetValue,
};
return targetEntry;
};
await tilesetProcessor.processAllEntries(entryProcessor);
await tilesetProcessor.end();
}
/**
* Performs the 'gunzip' tileset stage with the given parameters.
*
* This will process all entries of the source tileset. The
* data of entries that is compressed with gzip will be
* uncompressed. Other entries remain unaffected.
*
* @param currentInput - The current input name
* @param currentOutput - The current output name
* @param overwrite - Whether outputs should be overwritten
* @returns A promise that resolves when the process is finished
* @throws Error If one of the processing steps causes
* an error.
*/
private static async executeGunzip(
currentInput: string,
currentOutput: string,
overwrite: boolean
): Promise<void> {
const quiet = true;
const tilesetProcessor = new TilesetDataProcessor(quiet);
await tilesetProcessor.begin(currentInput, currentOutput, overwrite);
// The entry processor receives the source entry, and
// returns a target entry where the `value` is unzipped
// (If the data was not zipped, then `Buffers.gunzip`
// returns an unmodified result)
const entryProcessor = async (
sourceEntry: TilesetEntry,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type: string | undefined
) => {
const targetEntry = {
key: sourceEntry.key,
value: Buffers.gunzip(sourceEntry.value),
};
return targetEntry;
};
await tilesetProcessor.processAllEntries(entryProcessor);
await tilesetProcessor.end();
}
/**
* Execute all `ContentStage` objects in the given `TilesetStage`.
*
* For details about the arguments, see `executeTilesetStage`.
*
* @param tilesetStage - The `TilesetStage` object
* @param currentInput - The current input name
* @param currentOutput - The current output name
* @param overwrite - Whether outputs should be overwritten
* @returns A promise that resolves when the process is finished
* @throws Error If one of the processing steps causes
* an error.
*/
private static async executeTilesetContentStages(
tilesetStage: TilesetStage,
currentInput: string,
currentOutput: string,
overwrite: boolean
) {
try {
const quiet = false;
const tilesetProcessor = new BasicTilesetProcessor(quiet);
await tilesetProcessor.begin(currentInput, currentOutput, overwrite);
const contentStages = tilesetStage.contentStages;
if (contentStages) {
for (let c = 0; c < contentStages.length; c++) {
const contentStage = contentStages[c];
const message =
` Executing contentStage ${c} of ` +
`${contentStages.length}: ${contentStage.name}`;
console.log(message);
await ContentStageExecutor.executeContentStage(
contentStage,
tilesetProcessor
);
}
await tilesetProcessor.end();
}
} catch (e) {
throw new PipelineError(`${e}`);
}
}
}