-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.js
1732 lines (1468 loc) · 55.2 KB
/
tool.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This script is a little helper program to make using the fte imgtool a little bit more convenient.
//
//
// fte imgtool: https://fte.triptohell.info/moodles/win64/
//
// Usage:
// 2. create folders inside of this folder (or any other folder, see config.js)
// 2. put (prepared) img files into those folders.
// 3. edit the config.js file to setup your outputWadDir
// 4. on commandline: run: node ./tool.js
//
//
// It also uses: didder
// - https://github.com/makeworld-the-better-one/didder
// - https://github.com/makeworld-the-better-one/didder/blob/main/MANPAGE.md
//
// Extra info
// The tool converts the pixels of img files into the Quake palette,
// but you get no configuration for this, You want to prepare the img files
// with something like Gimp, to convert it into a non fullbright quake palette friendly img.
//
// Supported img formats (only png, tga, jpg, jpeg and bmp tested): [
// 'png', 'tga', 'jpg', 'jpeg', 'bmp', 'dds', 'ktx', 'ico',
// 'psd', 'pfm', 'pbm', 'pgm', 'ppm', 'hdr', 'astc', 'pkm', 'pcx'
// ]
//
//
//
// @todo: instead of doing the comparison of the squashed viewport in the fix16 by checking the difference percentage of each viewport, we should instead get the apect ratio (W / H) and then compare it to the previous one.
//
// We should also try different scale values than just 2, by getting the aspect ratio, maybe seeing a pattern in the aspect ratio, or maybe getting the common factors of the W / H... Anways, somehow I need to try to do it times 1.5, 1.2 and these other kinds of fractional values, just because it might give us a better result in certain cases.
//
// 0. initial stuff
const fs = require('fs')
const child_process = require("child_process")
const path = require('path')
const config = require('./config.js')
// define console colors, for prettier logging output
// SEE https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
const cc = {
bgblue: '\u001b[44m',
bgcyan: '\u001b[46m',
bgred: '\u001b[41m',
bgyellow: '\u001b[33;1m\u001b[7m',
bggreen: '\u001b[32m\u001b[7m',
bggrey: '\u001b[48;5;238m',
bgorange: '\u001b[48;5;202m',
r: '\u001b[0m',
}
// Set vars from the config
const thisPath = normalizeFolder('')
const inputDir = normalizeFolder(config.inputDir)
const pngOutputDir = normalizeFolder(config.pngOutputDir)
const outputWadDir = normalizeFolder(config.outputWadDir)
const img2mipCommand = config.img2mipCommand
const buildWadCommand = config.buildWadCommand
const wad2pngsCommand = config.wad2pngsCommand
const img2pngCommand = config.img2pngCommand
const didderConvertCommand = config.didderConvertCommand
const imageInfoCommand = config.imageInfoCommand
const imgtoolLog = config.imgtoolLog
const basicLog = config.basicLog
const pedanticLog = config.pedanticLog
const commandLog = config.commandLog
const devLog = config.devLog
const toolPath = path.normalize(config.toolPath)
const didderToolPath = path.normalize(config.didderToolPath)
const indentStr1 = ' '
// The list of allowed config names, in order of trying to find them.
// the first one found is the one that gets used for that folder.
const allowedConfigNames = [
'!wadconfig.js',
'!config.js',
'wadconfig.js',
'config.js',
]
const skipFolderNames = [ // The folder names which are skipped
'.git', 'mip', 'dithered'
]
const supportedImgtoolTypes = [
'png', 'tga', 'jpg', 'jpeg',
'bmp', 'dds', 'ktx', 'ico',
'psd', 'pfm', 'pbm', 'pgm',
'ppm', 'hdr', 'pkm', 'astc',
'pcx'
]
const supportedDidderTypes = [
'png', 'jpg', 'jpeg',
'gif', 'bmp',
]
// Define the palette strings for didder. they are defined as space seperated RGB colors (Red Blue Green), where each color is a comma seperated RGB component
// so for example: 225,0,20 meaning 225 Red, 0 Green and 20 Blue
const nonFullbrightPalette = '0,0,0 15,15,15 31,31,31 47,47,47 63,63,63 75,75,75 91,91,91 107,107,107 123,123,123 139,139,139 155,155,155 171,171,171 187,187,187 203,203,203 219,219,219 235,235,235 15,11,7 23,15,11 31,23,11 39,27,15 47,35,19 55,43,23 63,47,23 75,55,27 83,59,27 91,67,31 99,75,31 107,83,31 115,87,31 123,95,35 131,103,35 143,111,35 11,11,15 19,19,27 27,27,39 39,39,51 47,47,63 55,55,75 63,63,87 71,71,103 79,79,115 91,91,127 99,99,139 107,107,151 115,115,163 123,123,175 131,131,187 139,139,203 0,0,0 7,7,0 11,11,0 19,19,0 27,27,0 35,35,0 43,43,7 47,47,7 55,55,7 63,63,7 71,71,7 75,75,11 83,83,11 91,91,11 99,99,11 107,107,15 7,0,0 15,0,0 23,0,0 31,0,0 39,0,0 47,0,0 55,0,0 63,0,0 71,0,0 79,0,0 87,0,0 95,0,0 103,0,0 111,0,0 119,0,0 127,0,0 19,19,0 27,27,0 35,35,0 47,43,0 55,47,0 67,55,0 75,59,7 87,67,7 95,71,7 107,75,11 119,83,15 131,87,19 139,91,19 151,95,27 163,99,31 175,103,35 35,19,7 47,23,11 59,31,15 75,35,19 87,43,23 99,47,31 115,55,35 127,59,43 143,67,51 159,79,51 175,99,47 191,119,47 207,143,43 223,171,39 239,203,31 255,243,27 11,7,0 27,19,0 43,35,15 55,43,19 71,51,27 83,55,35 99,63,43 111,71,51 127,83,63 139,95,71 155,107,83 167,123,95 183,135,107 195,147,123 211,163,139 227,179,151 171,139,163 159,127,151 147,115,135 139,103,123 127,91,111 119,83,99 107,75,87 95,63,75 87,55,67 75,47,55 67,39,47 55,31,35 43,23,27 35,19,19 23,11,11 15,7,7 187,115,159 175,107,143 163,95,131 151,87,119 139,79,107 127,75,95 115,67,83 107,59,75 95,51,63 83,43,55 71,35,43 59,31,35 47,23,27 35,19,19 23,11,11 15,7,7 219,195,187 203,179,167 191,163,155 175,151,139 163,135,123 151,123,111 135,111,95 123,99,83 107,87,71 95,75,59 83,63,51 67,51,39 55,43,31 39,31,23 27,19,15 15,11,7 111,131,123 103,123,111 95,115,103 87,107,95 79,99,87 71,91,79 63,83,71 55,75,63 47,67,55 43,59,47 35,51,39 31,43,31 23,35,23 15,27,19 11,19,11 7,11,7 255,243,27 239,223,23 219,203,19 203,183,15 187,167,15 171,151,11 155,131,7 139,115,7 123,99,7 107,83,0 91,71,0 75,55,0 59,43,0 43,31,0 27,15,0 11,7,0 0,0,255 11,11,239 19,19,223 27,27,207 35,35,191 43,43,175 47,47,159 47,47,143 47,47,127 47,47,111 47,47,95 43,43,79 35,35,63 27,27,47 19,19,31 11,11,15'
const fullPalette = nonFullbrightPalette + ' 43,0,0 59,0,0 75,7,0 95,7,0 111,15,0 127,23,7 147,31,7 163,39,11 183,51,15 195,75,27 207,99,43 219,127,59 227,151,79 231,171,95 239,191,119 247,211,139 167,123,59 183,155,55 199,195,55 231,227,87 127,191,255 171,231,255 215,255,255 103,0,0 139,0,0 179,0,0 215,0,0 255,0,0 255,243,147 255,247,199 255,255,255 159,91,83'
// Check if the imgtool exists
if(!pathExists(toolPath)) {
console.error(cc.bgred, 'ERROR', cc.r, toolPath, 'not found: download the program from https://fte.triptohell.info/moodles/win64/')
process.exit(1)
}
// Code for verifying that didder exists
let didderExists = undefined
function verifyDidderExistsWithError() {
didderExists = pathExists(didderToolPath)
if(!didderExists) {
console.error(cc.bgred, 'ERROR', cc.r, didderToolPath, 'not found: download the program from https://github.com/makew0rld/didder/releases')
}
return didderExists
}
ensureFolder(inputDir)
ensureFolder(outputWadDir)
ensureFolder(pngOutputDir)
// check command line arguments
const commands = {
'-d': defaultRoute,
'--default': defaultRoute,
'-r': reverseRoute,
'--reverse': reverseRoute,
'-h': helpRoute,
'--help': helpRoute,
}
const cmdArgs = process.argv.slice(2)
let forceRecreateFlag = false
let forceSingleName
if(cmdArgs.length == 0) {
helpRoute()
} else {
const firstArg = cmdArgs[0].toLowerCase()
if(cmdArgs.length >= 2) {
const secondArg = cmdArgs[1].toLowerCase()
if(secondArg == '-fa' || secondArg == '--forceall') {
forceRecreateFlag = true
console.log('Force recreate.')
} else {
// assume name, and just force that one.
forceSingleName = cmdArgs[1]
forceRecreateFlag = true
}
}
let found = false
for(let k in commands) {
if(k == firstArg) {
found = true
const fn = commands[k]
fn()
break
}
}
if(!found) {
console.log('invalid command:', firstArg)
helpRoute()
}
}
function helpRoute() {
const helpLines = [
cc.bgblue + 'Usage:' + cc.r,
' node tool.js "command", where "command" is something like -d or -r',
'',
' ' + cc.bgcyan + 'example:' + cc.r,
' node tool.js -d',
'',
' ' + cc.bgcyan + 'example2:' + cc.r + ' force recreate just 1:',
' node tool.js -d wadfolder',
'',
cc.bgblue + 'Commands:' + cc.r,
' -d, --default:',
'',
' The default command, converts folders of imgs into mips and into wads',
'',
' -r, --reverse:',
'',
' The reverse command, converts wads back into folders with png files.',
' This only works for the wads directly in the outputWadDir, wads in subdirectories are not handled',
'',
' -fa, --forceall: ',
'',
' After another command to forcefully recreate everything regardless of the modification dates',
'',
' "", -h, --help:',
'',
' This help message',
]
console.log(helpLines.join('\n'))
}
/**
* Converts the wads back into png files.
*/
function reverseRoute() {
// 1. get the wads
const wads = getFileNames(outputWadDir, ['wad'])
const logWadPath = getLogPath(thisPath, outputWadDir)
// 2. loop through the wads
for(let i=0; i < wads.length; i++) {
// 3. get the wadName and folders
const wadName = removeExtension(wads[i])
const pngFolder = normalizeFolder(pngOutputDir + wadName)
const logPngFolder = getLogPath(thisPath, pngFolder)
// 4. ensure the png path exists
ensureFolder(pngFolder)
// 5. compare edit dates of wad and pngs
if(!forceRecreateFlag) {
const wadEditDateItems = getEditDates(outputWadDir, [wadName + '.wad'])
const lastWadEditDate = getLastEditDate(wadEditDateItems)
const pngs = getFileNames(pngFolder, ['png'])
const pngEditDateItems = getEditDates(pngFolder, pngs)
const lastPngEditDate = getLastEditDate(pngEditDateItems)
if(lastPngEditDate > lastWadEditDate) {
console.log(`${cc.bggrey} pngs for ${wadName + '.wad'} ${cc.r} up 2 date`)
continue
}
}
// 5. because we change the cwd, we need to change the toolpath as well
const relativeToolPath = path.relative(pngFolder, toolPath)
// 6. execute the command
const wad2pngShellCommand = wad2pngsCommand(relativeToolPath, outputWadDir, wadName)
if(commandLog) { console.log(`wad 2 png shell command: ${wad2pngShellCommand}`)}
const result = executeShellScript(wad2pngShellCommand, {cwd: pngFolder})
if(!result.success) {
console.error(
cc.bgred, 'imgtool ERROR', cc.r, 'Converting wad',
(logWadPath + wadName + '.wad'), 'to pngs in', logPngFolder, result.error
)
continue
} else
// This function has other ways it can fail I have no Idea Why
if(
(result.msg.indexOf('Write failed') != -1) ||
(result.msg.indexOf('\\') != -1) ||
(result.msg.indexOf('/') != -1)
) {
console.error(cc.bgred, 'unknown imgtool ERROR', cc.r, (logWadPath + wadName + '.wad'))
}
if(pedanticLog) { console.log(`${logWadPath + wadName + '.wad'} -> ${logPngFolder}`) }
if(imgtoolLog) { console.log(cc.bgcyan + ' imgtool ' + cc.r, afterFirstLineIndentLog(indentStr1, result.msg)) }
}
// this is good, if we're in the correct folder, and I believe this works through the current folder thingy.
// '..\\imgtool64.exe -x ..\\..\\tech.wad'
console.log(cc.bggreen, 'Program done.', cc.r)
}
/**
* This is the entry for the default route ( -d ) functionality.
* obviously we want to convert all the folders and sub folders within the inputDir folder into same name wad files.
*
* but how do we go about doing this?
* well: first we split on the case where you just want a single item,
* this is the case when you do -d somename where somename is a folder name inside of the inputDir
*/
function defaultRoute() {
// This is the default -d case
if(!forceSingleName) {
const folderObjs = getFolderObjs(inputDir)
if(devLog) { console.log('folders handled: ', folderObjs) }
if(folderObjs.length == 0) {
console.error(cc.bgred, 'ERROR', cc.r, 'could not find any folders in ', inputDir)
process.exit(1)
}
folderObjs.forEach(defaultRoute_handleFolder)
} else
// This is the case for forceSingle name being set
{
const folderObjs = getFolderObjs(inputDir)
const forceSingleArr = getPathItems(forceSingleName)
const filteredFolders = folderObjs.filter(function(folderObj) {
return arrayItemsMatchInOrder(folderObj.parts, forceSingleArr)
})
filteredFolders.forEach(defaultRoute_handleFolder)
}
}
/**
* This is the entry function for every folder.
*
* This function checks if the wad has to be built at all,
* and then does the building.
*
*/
function defaultRoute_handleFolder(folderObj) {
// 1. setup
const folderName = folderObj.name
const folderPath = folderObj.path
const ditheredPath = normalizeFolder(folderPath + 'dithered')
// 2. get the images
const ditheredImages = getFileNames(ditheredPath, supportedImgtoolTypes)
const images = getFileNames(folderPath, supportedImgtoolTypes)
if(images.length == 0 && ditheredImages.length == 0) {
// console.error(cc.bgred, 'ERROR', cc.r, 'No valid image files found.')
return
}
// 3. get the wadConfig
const wadConfig = getWadConfig(folderPath)
// 5. set a forceRebuild flag on the wadConfig
wadConfig.forceRebuilt = forceRecreateFlag || wadConfig.forceRebuilt
// 5. get the edit dates of the images, and check if the wad exists.
const imgEditDateItems = getEditDates(folderPath, images)
const ditheredEditDates = getEditDates(ditheredPath, ditheredImages)
const combinedImages = imgEditDateItems.concat(ditheredEditDates)
const wadExists = pathExists(wadConfig.outputWadDir + folderName + '.wad')
// 6. check if the wad needs to be built at all. if not, we can skip it.
let wadHasToBeBuilt = false
if(wadExists && !wadConfig.forceRebuilt) {
// compare the edit dates of the imgs and the wad, to check if we need to rebuild the wad
const lastImgEditDate = getLastEditDate(combinedImages)
const wadEditDateItems = getEditDates(wadConfig.outputWadDir, [folderName + '.wad'])
const lastWadEditDate = getLastEditDate(wadEditDateItems)
wadHasToBeBuilt = lastImgEditDate > lastWadEditDate
} else {
wadHasToBeBuilt = true
}
// 7. if the wad does need to be build, go ahead and build it.
if(wadHasToBeBuilt) {
console.log('Building', cc.bgblue, (folderName + '.wad'), cc.r )
buildWadForFolder(folderObj, wadConfig, imgEditDateItems)
} else {
console.log(cc.bggrey, (folderName + '.wad'), cc.r, 'up 2 date.')
}
}
/**
* We need to get the wad config, which is a combination
* of the defaultWadConfig and an optional wadconfig file within any of the proceeding folder paths.
*
*/
function getWadConfig(folderPath) {
// 1. get the default wadConfig.
let defaultWadConfig = {}
if(config.defaultWadConfig) {
defaultWadConfig = ObjectAssignDeep(defaultWadConfig, config.defaultWadConfig)
}
const wadConfig = defaultWadConfig
const propDepth = {}
wadConfig.propDepth = propDepth
incrementDefineDepth(wadConfig, propDepth)
// 2. build up an overwriteWadConfig
// loop through the path chain
const relativePath = path.relative(inputDir, folderPath)
const pathItems = getPathItems(relativePath)
pathItems.unshift('')
let handlePath = inputDir
for(const item of pathItems) {
handlePath = normalizeFolder(handlePath + item)
// overwrite it.
{
let overwriteConfig
const configName = allowedConfigNames.find(function(configName) {
return pathExists(handlePath + configName)
})
if(configName != undefined) {
overwriteConfig = require(handlePath + configName)
}
if(overwriteConfig) {
incrementDefineDepth(overwriteConfig, propDepth)
ObjectAssignDeep(wadConfig, overwriteConfig)
}
}
}
// 3. handle outputWadDir property
{
// 1. check for a wadConfig flag
let otherOutputDir = false
if(wadConfig.relativeOutputWadDir) {
otherOutputDir = true
} else {
// Second case in which a otheroutputdir is possible (if the path is deeper.)
const relativePath = path.relative(inputDir, folderPath)
const slashArr = getPathItems(relativePath)
if(slashArr.length > 1) {
otherOutputDir = true
// also set the relativeOutputDir, so we can do the next section in the same way.
wadConfig.relativeOutputWadDir = slashArr.slice(0, -1).join('/')
}
}
if(otherOutputDir) {
wadConfig.outputWadDir = normalizeFolder(outputWadDir + wadConfig.relativeOutputWadDir)
ensureFolder(wadConfig.outputWadDir)
} else {
wadConfig.outputWadDir = outputWadDir
}
}
// 4. return it
return wadConfig
}
/**
* We have wadConfig, but for the scale and fix16AlignedMethod prop, we want to make it
* so that when the width or height (or both) properties are set, it will overwrite
* scale and fix16AlignedMethod. Now... A problem would arise if someone sets width/height properties in 1 directory,
* but then sets the scale and fix16AlignedMethod in a deeper directory :: the width and height props would propagate downwards.
* if we had a depth level however, which indicated the level a property was defined at: we could alleviate these problems,
* by making scale / fix16AlignedMethod take precidence, whenever these settings were defined in a deeper level.
* And we could do more as well.
*/
function incrementDefineDepth(wadConfig, propDepth) {
if(propDepth.currentDepth == undefined) {
propDepth.currentDepth = 0
}
propDepth.currentDepth++
// 2. loop though all of the keys in the wadConfig (minus propDepth)
for(let k in wadConfig) {
propDepth[k] = propDepth.currentDepth
}
}
// convert a string with slashes into an array of items in between the slashes
function getPathItems(path) {
return path.split(/\\|\//)
}
//
// does object.assign, but also caters for deeply nested objects within the root object,
// at the same time it's also used as a simple deep copy
//
function ObjectAssignDeep(base, extender) {
for(let k in extender) {
const extenderVal = extender[k]
const baseVal = base[k]
const extenderValType = getType(extenderVal)
const baseValType = getType(baseVal)
// simple set when type is unequal
if(extenderValType != baseValType) {
base[k] = extenderVal
continue
}
const extenderTypeIsComplex = extenderValType == 'array' || extenderValType == 'object'
if(!extenderTypeIsComplex) {
base[k] = extenderVal
} else {
// recurse
base[k] = ObjectAssignDeep(baseVal, extenderVal)
}
}
return base
}
/**
* 2. build the wad, for 1 folder.
* these are the steps (for the default setup)
* 1. convert all of the images into pngs
* 2. convert all of the pngs into dithered pngs
* 3. convert all of the dithered pngs into mips
* 4. convert all of the mips into a wad file
*
* the next thing is my idea of what it would look like, maybe it's possible.
*/
function buildWadForFolder(folderObj, wadConfig, imgEditDateItems) {
const folderName = folderObj.name
const folderPath = folderObj.path
const folderParts = folderObj.parts
// 1. check FBR name settings.
{
let fullBrightSuffixNameMatchIndex
const fbrNameSettings = wadConfig.useFullbrightNames.folders
if(fbrNameSettings.fullbright) {
fullBrightSuffixNameMatchIndex = nameArrayMatchesArrayWithSuffixes(folderParts, wadConfig.fullBrightNameSuffixes)
}
let noFullBrightSuffixNameMatchIndex
if(fbrNameSettings.nofullbright) {
noFullBrightSuffixNameMatchIndex = nameArrayMatchesArrayWithSuffixes(folderParts, wadConfig.noFullbrightNameSuffixes)
}
const theSame = fullBrightSuffixNameMatchIndex == noFullBrightSuffixNameMatchIndex
const notTheSame = !theSame
if(notTheSame) {
if(fullBrightSuffixNameMatchIndex > noFullBrightSuffixNameMatchIndex) {
console.log('Found fullbright suffix on folder')
wadConfig.removeFullbrightPixels = false
} else {
console.log('Found nofullbright suffix on folder')
wadConfig.removeFullbrightPixels = true
}
}
}
// 2. Do the extra steps.
if(!wadConfig.skipDithering_nofullbright) {
// skip some steps I guess.
convertUnsupportedToPng(folderPath, imgEditDateItems, wadConfig)
convertToFullBrightFixedDithered(folderPath, wadConfig)
normalizeFolder(folderPath + 'dithered')
}
buildMips(folderPath, wadConfig)
buildWadFromMips(folderPath, folderName, wadConfig)
}
function convertUnsupportedToPng(folderPath, imgEditDateItems, wadConfig) {
if(basicLog) { console.log('converting unsupported images to png') }
// 0. define some vars for later usage
const relativeToolPath = path.relative(folderPath, toolPath)
const logFolderPath = getLogPath(thisPath, folderPath)
// 1. loop through a map, to get png item, and non png item.
const map = convertDateItemsToMap(imgEditDateItems)
// console.log('map', map)
for(let name in map) {
const sameNamedImages = map[name]
// 2. give an error when there are more than 2 items
if(sameNamedImages.length > 2 && pedanticLog) {
console.warn(`${cc.bgyellow} WARN ${cc.r} Item with name ${name} has more than 2 items. This could lead to unexpected behaviour.`)
}
// 3. get the supported item and the not supported item
let supportedItem
let notSupportedItem
for(const dateItem of sameNamedImages) {
const ext = getExtension(dateItem.fileName)
if(supportedDidderTypes.includes(ext)) {
supportedItem = dateItem
} else {
notSupportedItem = dateItem
}
}
// 4. check if the png has to be built, this is based on state and existence of supported / notSupportedItem
let buildPng = true
if(notSupportedItem != undefined)
// 4.A. if there is a notSupportedItem item
{
if(supportedItem != undefined) {
// check if the supportedItem's date is newer, and if so, turn buildPng to false
if(!wadConfig.forceRebuilt && supportedItem.editDate > notSupportedItem.editDate) {
buildPng = false
}
}
if(wadConfig?.textureOpts?.[name]?.skipDithering_nofullbright) {
buildPng = false
}
} else
// 4.B. since there isn't a notSupportedItem, it means there is a supportedItem, and it's the only item.
// therefore, we don't have to build a png.
{
buildPng = false
}
// 5. If buildPng is true, build a PNG
if(buildPng) {
console.log(`converting ${notSupportedItem.fileName} -> .png`)
const shellCommand = img2pngCommand(relativeToolPath, notSupportedItem.fileName)
if(commandLog) { console.log(`img 2 png command: ${shellCommand}`)}
const result = executeShellScript(shellCommand, {cwd: folderPath})
if(!result.success) {
console.error(cc.bgred, 'imgtool ERROR', cc.r, 'Converting:', (logFolderPath + notSupportedItem.fileName), result.error)
continue
}
}
}
}
/**
* This step runs the didder code.
* this handles the dithering and the fullbright fixing.
*/
function convertToFullBrightFixedDithered(folderPath, wadConfig) {
if(basicLog) { console.log('dithering items') }
// check if didder exists.
if(didderExists == undefined) {
didderExists = verifyDidderExistsWithError()
}
if(!didderExists) {
console.log('Didder was not found, skipping dithering.')
return
}
// 1. reobtain the items within the current folder path
const images = getFileNames(folderPath, supportedDidderTypes)
// 2. also obtain the files from the dithered folder.
const ditherFolderPath = normalizeFolder(folderPath + 'dithered')
const ditherLogFolderPath = getLogPath(thisPath, ditherFolderPath)
ensureFolder(ditherFolderPath)
const ditheredImages = getFileNames(ditherFolderPath, supportedImgtoolTypes)
// 3. get the dates.
const imgEditDateItems = getEditDates(folderPath, images)
const ditheredDateItems = getEditDates(ditherFolderPath, ditheredImages)
// console.log('imgEditDateItems', imgEditDateItems)
// console.log('ditheredDateItems', ditheredDateItems)
const relativeDidderToolPath = path.relative(folderPath, didderToolPath)
const relativeToolPath = path.relative(folderPath, toolPath)
const logFolderPath = getLogPath(thisPath, folderPath)
// Get a list of items we need to rebuild again:
const from = imgEditDateItems
const to = ditheredDateItems
const toBuildItems = []
for(const fromItem of from) {
const toItem = to.find((toItem) => toItem.fileName == fromItem.fileName)
if(wadConfig.forceRebuilt || toItem == undefined || fromItem.editDate > toItem.editDate) {
toBuildItems.push(fromItem)
}
}
// Handle fullbright / nofullbright suffix stuff for images before dithering.
// Compare names, with suffix names, and non suffix names, and so on.
// Basically uses the 1 with last editDate. with it's base name.
// after this we have an object, with the baseName, and the item which we want to mip.
const allSuffixes = wadConfig.fullBrightNameSuffixes.concat(wadConfig.noFullbrightNameSuffixes)
const baseNamedToBuildItems = {}
for(const item of toBuildItems) {
const name = removeExtension(item.fileName)
const suffixLessName = stripSuffixes(name, allSuffixes)
if(baseNamedToBuildItems[suffixLessName] == undefined) {
baseNamedToBuildItems[suffixLessName] = []
}
baseNamedToBuildItems[suffixLessName].push(item)
}
for(const baseName in baseNamedToBuildItems) {
const array = baseNamedToBuildItems[baseName]
if(array.length > 1) {
// get the item with the last editDate
let lastEditDate = array[0].editDate
let lastEditIndex = 0
for(let i = 1; i < array.length; i++) {
const item = array[i]
if(item.editDate > lastEditDate) {
lastEditDate = item.editDate
lastEditIndex = i
}
}
const lastItem = array[lastEditIndex]
// set the array such that it only contains that item.
baseNamedToBuildItems[baseName] = [lastItem]
}
// remove the array from the baseNamedToBuildItems
baseNamedToBuildItems[baseName] = baseNamedToBuildItems[baseName][0]
}
// Loops through all images, and do dithering and all of the other things.
for(const baseName in baseNamedToBuildItems) {
const fromItem = baseNamedToBuildItems[baseName]
ditherSingleImage(fromItem, baseName)
}
/**
* Handles dithering, and all of the other functionality Didder has to offer, for 1 single image.
*/
function ditherSingleImage(fromItem, baseName) {
const name = removeExtension(fromItem.fileName)
// set textureOpts obj
let textureOpts = wadConfig?.textureOpts?.[name]
if(name !== baseName) {
const baseNameTextureOpts = wadConfig?.textureOpts?.[baseName]
const specificOpts = textureOpts
if(baseNameTextureOpts) {
textureOpts = Object.assign({}, baseNameTextureOpts, specificOpts)
}
}
// get the depth
const propDepth = Object.assign({}, wadConfig.propDepth)
if(textureOpts) {
incrementDefineDepth(textureOpts, propDepth) // inc
}
// start handling the params.
if(textureOpts?.skipDithering_nofullbright) {
// we can remove the file from the dithered folder, when it exists
const ditheredFilePath = ditherFolderPath + fromItem.fileName
if(pathExists(ditheredFilePath)) {
console.log('removing', ditherLogFolderPath + fromItem.fileName)
removeFile(ditheredFilePath)
}
return
}
// get the palette
let palette = fullPalette
if(wadConfig.removeFullbrightPixels) {
palette = nonFullbrightPalette
}
if(textureOpts?.removeFullbrightPixels != undefined) {
if(textureOpts?.removeFullbrightPixels) {
palette = nonFullbrightPalette
} else {
palette = fullPalette
}
}
// Fullbright overwrite based on fileName suffix
{
let fullBrightSuffixNameMatchIndex
const fbrNameSettings = wadConfig.useFullbrightNames.files
if(fbrNameSettings.fullbright) {
fullBrightSuffixNameMatchIndex = nameArrayMatchesArrayWithSuffixes([name], wadConfig.fullBrightNameSuffixes)
}
let noFullBrightSuffixNameMatchIndex
if(fbrNameSettings.nofullbright) {
noFullBrightSuffixNameMatchIndex = nameArrayMatchesArrayWithSuffixes([name], wadConfig.noFullbrightNameSuffixes)
}
const theSame = fullBrightSuffixNameMatchIndex == noFullBrightSuffixNameMatchIndex
const notTheSame = !theSame
if(notTheSame) {
if(fullBrightSuffixNameMatchIndex > noFullBrightSuffixNameMatchIndex) {
console.log('Found fullbright suffix on file')
palette = fullPalette
} else {
console.log('Found nofullbright suffix on file')
palette = nonFullbrightPalette
}
}
}
// custom palette overwrite support
let customPalette = wadConfig.customPalette
if(textureOpts?.customPalette != undefined) {
customPalette = textureOpts?.customPalette
}
if(customPalette != undefined) {
palette = customPalette
}
// get the algorithm
let algorithm = wadConfig.algorithm
if(textureOpts?.algorithm != undefined) {
algorithm = textureOpts?.algorithm
}
if(algorithm == undefined) {
console.log('algorithm not defined. defaulting to FloydSteinberg')
algorithm = 'edm FloydSteinberg'
}
// strength
let strengthStr = ''
let strength = wadConfig.strength
if(textureOpts?.strength != undefined) {
strength = textureOpts?.strength
}
if(strength != undefined) {
strengthStr = `-s ${strength}`
}
// grayscale
let grayscaleStr = ''
let grayscale = wadConfig.grayscale
if(textureOpts?.grayscale != undefined) {
grayscale = textureOpts?.grayscale
}
if(grayscale) {
grayscaleStr = '-g'
}
// recolor.
let recolorStr = ''
let recolor = wadConfig.recolor
if(textureOpts?.recolor != undefined) {
recolor = textureOpts?.recolor
}
if(recolor != undefined) {
recolorStr = `-r "${recolor}"`
}
// saturation
let saturationStr = ''
let saturation = wadConfig.saturation
if(textureOpts?.saturation != undefined) {
saturation = textureOpts?.saturation
}
if(saturation != undefined) {
saturationStr = `--saturation ${saturation}`
}
// brightness
let brightnessStr = ''
let brightness = wadConfig.brightness
if(textureOpts?.brightness != undefined) {
brightness = textureOpts?.brightness
}
if(brightness != undefined) {
brightnessStr = `--brightness ${brightness}`
}
// contrast
let contrastStr = ''
let contrast = wadConfig.contrast
if(textureOpts?.contrast != undefined) {
contrast = textureOpts?.contrast
}
if(contrast != undefined) {
contrastStr = `--contrast ${contrast}`
}
// width
let width = wadConfig.width
if(textureOpts?.width != undefined) {
width = textureOpts?.width
}
// height
let height = wadConfig.height
if(textureOpts?.height != undefined) {
height = textureOpts?.height
}
// get the define depth for width and height props. This way,
// we know whether or not to do scale and fix16Aligned
const largestWidthHeightDepth = Math.max((propDepth.width||0), (propDepth.height||0))
// used for scale and fix16aligncode
let sizeInfo
// scale
let scale = wadConfig.scale
if(textureOpts?.scale != undefined) {
scale = textureOpts?.scale
}
const scaleDepth = propDepth.scale||0
if(scale != undefined && scaleDepth > largestWidthHeightDepth) {
// Convert it to the array syntax anyway
if(!Array.isArray(scale)) {
scale = [scale]
}
// Convert it to the double array value
if(scale.length == 1) {
scale.push(scale[0])
}
// get info about the size
if(sizeInfo == undefined) {
sizeInfo = getImageSizeInfo(relativeToolPath, fromItem.fileName, folderPath)
}
if(sizeInfo == undefined) { return }
// scale it
sizeInfo.width = sizeInfo.width * scale[0]
sizeInfo.height = sizeInfo.height * scale[1]
}
let overwrite_fix16AlignedSmart_maxTexSize
// maxWidth
let maxWidth = wadConfig.maxWidth
if(textureOpts?.maxWidth != undefined) {
maxWidth = textureOpts?.maxWidth
}
const maxWidthDepth = propDepth.maxWidth||0
// maxHeight
let maxHeight = wadConfig.maxHeight
if(textureOpts?.maxHeight != undefined) {
maxHeight = textureOpts?.maxHeight
}
const maxHeightDepth = propDepth.maxHeight||0
if(
(maxWidth != undefined && maxWidthDepth > largestWidthHeightDepth) ||
(maxHeight != undefined && maxHeightDepth > largestWidthHeightDepth)
) {
// get info about the size
if(sizeInfo == undefined) {
sizeInfo = getImageSizeInfo(relativeToolPath, fromItem.fileName, folderPath)
}
if(sizeInfo == undefined) { return }
// To fix issues with fix16 making it too large, we set fix16AlignedSmart_maxTexSize
overwrite_fix16AlignedSmart_maxTexSize = Math.max(maxWidth||0, maxHeight||0)
const imageAspectRatio = sizeInfo.width / sizeInfo.height
// set the vars so that if they aren't defined, they become the size of the current image,
// so it's not the factor that will downscale it.
if(maxWidth == undefined) { maxWidth = sizeInfo.width }
if(maxHeight == undefined) { maxHeight = sizeInfo.height }
if(
maxWidth >= sizeInfo.width &&
maxHeight >= sizeInfo.height
) {
// we don't need to do anything in this case, because both sizes are larger
} else {
const maxSizeAspectRatio = maxWidth / maxHeight
//
let scaleFactor
if(imageAspectRatio > maxSizeAspectRatio) {
// scale the width
scaleFactor = maxWidth / sizeInfo.width
} else {
// scale the height
scaleFactor = maxHeight / sizeInfo.height
}
sizeInfo.width = sizeInfo.width * scaleFactor
sizeInfo.height = sizeInfo.height * scaleFactor
}
}
// fix16AlignedMethod code.
let fix16AlignedMethod = wadConfig.fix16AlignedMethod
if(textureOpts?.fix16AlignedMethod != undefined) {
fix16AlignedMethod = textureOpts?.fix16AlignedMethod
}
const fix16AlignedMethodDepth = propDepth.fix16AlignedMethod||0
if(fix16AlignedMethod != undefined && fix16AlignedMethod != 'none' && fix16AlignedMethodDepth > largestWidthHeightDepth) {
const valids = [
'stretch',
'scale',
'smart'
]
if(valids.indexOf(fix16AlignedMethod) == -1) {