forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackLDrawModel.js
303 lines (183 loc) · 6 KB
/
packLDrawModel.js
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
/**
* @author yomboprime / https://github.com/yomboprime/
*
* LDraw object packer
*
* Usage:
*
* - Download official parts library from LDraw.org and unzip in a directory (e.g. ldraw/)
*
* - Download your desired model file and place in the ldraw/models/ subfolder.
*
* - Place this script also in ldraw/
*
* - Issue command 'node packLDrawModel models/<modelFileName>'
*
* The packed object will be in ldraw/models/<modelFileName>_Packed.mpd and will contain all the object subtree as embedded files.
*
*
*/
var ldrawPath = './';
var materialsFileName = 'LDConfig.ldr';
var fs = require( 'fs' );
var path = require( 'path' );
if ( process.argv.length !== 3 ) {
console.log( "Usage: node packLDrawModel <modelFilePath>" );
exit( 0 );
}
var fileName = process.argv[ 2 ];
var materialsFilePath = path.join( ldrawPath, materialsFileName );
console.log( 'Loading materials file "' + materialsFilePath + '"...' );
var materialsContent = fs.readFileSync( materialsFilePath, { encoding: "utf8" } );
console.log( 'Packing "' + fileName + '"...' );
var objectsPaths = [];
var objectsContents = [];
var pathMap = {};
var listOfNotFound = [];
// Parse object tree
parseObject( fileName, true );
// Check if previously files not found are found now
// (if so, probably they were already embedded)
var someNotFound = false;
for ( var i = 0; i < listOfNotFound.length; i ++ ) {
if ( ! pathMap[ listOfNotFound[ i ] ] ) {
someNotFound = true;
console.log( 'Error: File object not found: "' + fileName + '".' );
}
}
if ( someNotFound ) {
console.log( "Some files were not found, aborting." );
process.exit( - 1 );
}
// Obtain packed content
var packedContent = materialsContent + "\n";
for ( var i = objectsPaths.length - 1; i >= 0; i -- ) {
packedContent += objectsContents[ i ];
}
packedContent += "\n";
// Save output file
var outPath = fileName + "_Packed.mpd";
console.log( 'Writing "' + outPath + '"...' );
fs.writeFileSync( outPath, packedContent );
console.log( 'Done.' );
//
function parseObject( fileName, isRoot ) {
// Returns the located path for fileName or null if not found
console.log( 'Adding "' + fileName + '".' );
var originalFileName = fileName;
var prefix = "";
var objectContent = null;
for ( var attempt = 0; attempt < 2; attempt ++ ) {
prefix = "";
if ( attempt === 1 ) {
fileName = fileName.toLowerCase();
}
if ( fileName.startsWith( '48/' ) ) {
prefix = "p/";
} else if ( fileName.startsWith( 's/' ) ) {
prefix = "parts/";
}
var absoluteObjectPath = path.join( ldrawPath, fileName );
try {
objectContent = fs.readFileSync( absoluteObjectPath, { encoding: "utf8" } );
break;
} catch ( e ) {
prefix = "parts/";
absoluteObjectPath = path.join( ldrawPath, prefix, fileName );
try {
objectContent = fs.readFileSync( absoluteObjectPath, { encoding: "utf8" } );
break;
} catch ( e ) {
prefix = "p/";
absoluteObjectPath = path.join( ldrawPath, prefix, fileName );
try {
objectContent = fs.readFileSync( absoluteObjectPath, { encoding: "utf8" } );
break;
} catch ( e ) {
try {
prefix = "models/";
absoluteObjectPath = path.join( ldrawPath, prefix, fileName );
objectContent = fs.readFileSync( absoluteObjectPath, { encoding: "utf8" } );
break;
} catch ( e ) {
if ( attempt === 1 ) {
// The file has not been found, add to list of not found
listOfNotFound.push( originalFileName );
}
}
}
}
}
}
var objectPath = path.join( prefix, fileName );
if ( ! objectContent ) {
// File was not found, but could be a referenced embedded file.
return null;
}
if ( objectContent.indexOf( '\r\n' ) !== - 1 ) {
// This is faster than String.split with regex that splits on both
objectContent = objectContent.replace( /\r\n/g, '\n' );
}
var processedObjectContent = isRoot ? "" : "0 FILE " + objectPath + "\n";
var lines = objectContent.split( "\n" );
for ( var i = 0, n = lines.length; i < n; i ++ ) {
var line = lines[ i ];
var lineLength = line.length;
// Skip spaces/tabs
var charIndex = 0;
while ( ( line.charAt( charIndex ) === ' ' || line.charAt( charIndex ) === '\t' ) && charIndex < lineLength ) {
charIndex ++;
}
line = line.substring( charIndex );
lineLength = line.length;
charIndex = 0;
if ( line.startsWith( '0 FILE ' ) ) {
if ( i === 0 ) {
// Ignore first line FILE meta directive
continue;
}
// Embedded object was found, add to path map
var subobjectFileName = line.substring( charIndex ).trim().replace( /\\/g, '/' );
if ( subobjectFileName ) {
// Find name in path cache
var subobjectPath = pathMap[ subobjectFileName ];
if ( ! subobjectPath ) {
pathMap[ subobjectFileName ] = subobjectFileName;
}
}
}
if ( line.startsWith( '1 ' ) ) {
// Subobject, add it
charIndex = 2;
// Skip material, position and transform
for ( var token = 0; token < 13 && charIndex < lineLength; token ++ ) {
// Skip token
while ( line.charAt( charIndex ) !== ' ' && line.charAt( charIndex ) !== '\t' && charIndex < lineLength ) {
charIndex ++;
}
// Skip spaces/tabs
while ( ( line.charAt( charIndex ) === ' ' || line.charAt( charIndex ) === '\t' ) && charIndex < lineLength ) {
charIndex ++;
}
}
var subobjectFileName = line.substring( charIndex ).trim().replace( /\\/g, '/' );
if ( subobjectFileName ) {
// Find name in path cache
var subobjectPath = pathMap[ subobjectFileName ];
if ( ! subobjectPath ) {
// Add new object
subobjectPath = parseObject( subobjectFileName );
}
pathMap[ subobjectFileName ] = subobjectPath ? subobjectPath : subobjectFileName;
processedObjectContent += line.substring( 0, charIndex ) + pathMap[ subobjectFileName ] + "\n";
}
} else {
processedObjectContent += line + "\n";
}
}
if ( objectsPaths.indexOf( objectPath ) < 0 ) {
objectsPaths.push( objectPath );
objectsContents.push( processedObjectContent );
}
return objectPath;
}