-
Notifications
You must be signed in to change notification settings - Fork 235
/
Library.java
680 lines (565 loc) · 22.2 KB
/
Library.java
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
package processing.app;
import java.awt.EventQueue;
import java.io.*;
import java.util.*;
import java.util.zip.ZipFile;
import processing.app.contrib.*;
import processing.core.*;
import processing.data.StringDict;
import processing.data.StringList;
public class Library extends LocalContribution {
// static final String[] platformNames = PConstants.platformNames;
static StringDict newToOld = new StringDict(new String[][] {
{ "macos-x86_64", "macosx" },
{ "windows-amd64", "windows64" },
{ "linux-amd64", "linux64" },
{ "linux-arm", "linux-armv6hf" },
{ "linux-aarch64", "linux-arm64" }
});
protected File libraryFolder; // shortname/library
protected File examplesFolder; // shortname/examples
protected File referenceFile; // shortname/reference/index.html
/**
* Subfolder for grouping libraries in a menu. Basic subfolder support
* is provided so that some organization can be done in the import menu.
* (This is the replacement for the "library compilation" type.)
*/
protected String group;
/** Packages provided by this library. */
StringList packageList;
/** Per-platform exports for this library. */
Map<String, String[]> exportList;
/** List of default exports */
String[] baseList;
/** Android exports (single platform for now, may not exist). */
String[] androidExportList;
// /** True if there are separate 32/64 bit for the specified platform index. */
// boolean[] multipleArch = new boolean[platformNames.length];
// Map<String, Boolean> multipleArch = new HashMap<>();
/**
* For runtime, the native library path for this platform. e.g. on Windows 64,
* this might be the windows64 subfolder with the library.
*/
String nativeLibraryPath;
// /** True if */
// boolean variants;
static public final String propertiesFileName = "library.properties";
/**
* Filter to pull out just files and none of the platform-specific
* directories, and to skip export.txt.
*
* As of 2.0a2, other directories are included, because we need
* things like the 'plugins' subfolder w/ video.
*
* As of 4.0b4, only checking whether macos, windows, linux, or
* android are the prefix of the folder name, so that we can avoid
* explicitly listing all possible architectures, and so that
* macos-blah as well as and macosx will be handled properly.
*/
static FilenameFilter libraryFolderFilter = (dir, name) -> {
// skip .DS_Store files, .svn folders, etc
if (name.charAt(0) == '.') return false;
// ha, the sftp library still has one [fry 220121]
if (name.equals("CVS")) return false;
if (name.equals("export.txt")) return false;
File file = new File(dir, name);
if (file.isDirectory()) {
//noinspection RedundantIfStatement
if (name.startsWith("macos") ||
name.startsWith("windows") ||
name.startsWith("linux")) {
//name.startsWith("android")) { // no libraries use this
return false;
}
}
return true;
};
static FilenameFilter jarFilter = (dir, name) -> {
if (name.charAt(0) == '.') return false; // skip ._blah.jar crap on OS X
if (new File(dir, name).isDirectory()) return false;
String lc = name.toLowerCase();
return lc.endsWith(".jar") || lc.endsWith(".zip");
};
static public Library load(File folder) {
try {
return new Library(folder);
// } catch (IgnorableException ig) {
// Base.log(ig.getMessage());
} catch (Error err) {
// Handles UnsupportedClassVersionError and others
err.printStackTrace();
}
return null;
}
public Library(File folder) {
this(folder, null);
}
private Library(File folder, String groupName) {
super(folder);
this.group = groupName;
libraryFolder = new File(folder, "library");
examplesFolder = new File(folder, "examples");
referenceFile = new File(folder, "reference/index.html");
handle();
}
/**
* Handles all the Java-specific parsing for library handling.
*/
protected void handle() {
handleNative();
handleExports();
}
/**
* Identify nativeLibraryFolder location for the current platform.
*/
private void handleNative() {
String variant = Platform.getVariant();
// use the root of the library folder as the default
File nativeLibraryFolder = libraryFolder;
/*
String hostPlatform = Platform.getName();
// see if there's a 'windows', 'macosx', or 'linux' folder
File hostLibrary = new File(libraryFolder, hostPlatform);
if (hostLibrary.exists()) {
nativeLibraryFolder = hostLibrary;
}
*/
// see if there's a {platform}-{arch} folder
File hostLibrary = new File(libraryFolder, variant);
if (hostLibrary.exists()) {
nativeLibraryFolder = hostLibrary;
} else {
// if not found, try the old-style naming
String oldName = newToOld.get(variant);
if (oldName != null) {
hostLibrary = new File(libraryFolder, oldName);
if (hostLibrary.exists()) {
nativeLibraryFolder = hostLibrary;
}
}
}
// save that folder for later use
nativeLibraryPath = nativeLibraryFolder.getAbsolutePath();
}
private void handleExports() {
/*
File exportSettings = new File(libraryFolder, "export.txt");
StringDict exportTable = exportSettings.exists() ?
Util.readSettings(exportSettings) : new StringDict();
*/
exportList = new HashMap<>();
// get the list of files just in the library root
baseList = libraryFolder.list(libraryFolderFilter);
for (String variant : Platform.getSupportedVariants().keys()) {
File variantFolder = new File(libraryFolder, variant);
if (!variantFolder.exists()) {
// check to see if old naming is in use
String oldName = newToOld.get(variant, null);
if (oldName != null) {
variantFolder = new File(libraryFolder, variant);
if (variantFolder.exists()) {
Messages.log("Please update " + getName() + " for Processing 4. " +
variantFolder + " is the older naming scheme.");
}
}
}
if (variantFolder.exists()) {
String[] entries = listPlatformEntries(libraryFolder, variant, baseList);
if (entries != null) {
exportList.put(variant, entries);
}
}
}
/*
// not actually used in any libraries
String androidExportStr = exportTable.get("android");
if (androidExportStr != null) {
androidExportList = PApplet.splitTokens(androidExportStr, ", ");
} else {
androidExportList = baseList;
}
*/
/*
// for each individual platform that this library supports, figure out what's around
for (int i = 1; i < platformNames.length; i++) {
String platformName = platformNames[i];
String platformName32 = platformName + "32";
String platformName64 = platformName + "64";
String platformNameArmv6hf = platformName + "-armv6hf";
String platformNameArm64 = platformName + "-arm64";
// First check for things like 'application.macosx=' or 'application.windows32' in the export.txt file.
// These will override anything in the platform-specific subfolders.
String platformAll = exportTable.get("application." + platformName);
String[] platformList = platformAll == null ? null : PApplet.splitTokens(platformAll, ", ");
String platform32 = exportTable.get("application." + platformName + "32");
String[] platformList32 = platform32 == null ? null : PApplet.splitTokens(platform32, ", ");
String platform64 = exportTable.get("application." + platformName + "64");
String[] platformList64 = platform64 == null ? null : PApplet.splitTokens(platform64, ", ");
String platformArmv6hf = exportTable.get("application." + platformName + "-armv6hf");
String[] platformListArmv6hf = platformArmv6hf == null ? null : PApplet.splitTokens(platformArmv6hf, ", ");
String platformArm64 = exportTable.get("application." + platformName + "-arm64");
String[] platformListArm64 = platformArm64 == null ? null : PApplet.splitTokens(platformArm64, ", ");
// If nothing specified in the export.txt entries, look for the platform-specific folders.
if (platformAll == null) {
platformList = listPlatformEntries(libraryFolder, platformName, baseList);
}
if (platform32 == null) {
platformList32 = listPlatformEntries(libraryFolder, platformName32, baseList);
}
if (platform64 == null) {
platformList64 = listPlatformEntries(libraryFolder, platformName64, baseList);
}
if (platformListArmv6hf == null) {
platformListArmv6hf = listPlatformEntries(libraryFolder, platformNameArmv6hf, baseList);
}
if (platformListArm64 == null) {
platformListArm64 = listPlatformEntries(libraryFolder, platformNameArm64, baseList);
}
if (platformList32 != null || platformList64 != null || platformListArmv6hf != null || platformListArm64 != null) {
//multipleArch[i] = true;
multipleArch.put(platformName, true);
}
// if there aren't any relevant imports specified or in their own folders,
// then use the baseList (root of the library folder) as the default.
if (platformList == null && platformList32 == null && platformList64 == null && platformListArmv6hf == null && platformListArm64 == null) {
exportList.put(platformName, baseList);
} else {
// once we've figured out which side our bread is buttered on, save it.
// (also concatenate the list of files in the root folder as well
if (platformList != null) {
exportList.put(platformName, platformList);
}
if (platformList32 != null) {
exportList.put(platformName32, platformList32);
}
if (platformList64 != null) {
exportList.put(platformName64, platformList64);
}
if (platformListArmv6hf != null) {
exportList.put(platformNameArmv6hf, platformListArmv6hf);
}
if (platformListArm64 != null) {
exportList.put(platformNameArm64, platformListArm64);
}
}
}
*/
// for (String p : exportList.keySet()) {
// System.out.println(p + " -> ");
// PApplet.println(exportList.get(p));
// }
// get the path for all .jar files in this code folder
packageList = Util.packageListFromClassPath(getClassPath());
}
/**
* List who's inside a windows64, macosx, linux32, etc folder.
*/
static String[] listPlatformEntries(File libraryFolder, String folderName, String[] baseList) {
File folder = new File(libraryFolder, folderName);
if (folder.exists()) {
String[] entries = folder.list((dir, name) -> name.charAt(0) != '.');
if (entries != null) {
String[] outgoing = new String[entries.length + baseList.length];
for (int i = 0; i < entries.length; i++) {
outgoing[i] = folderName + "/" + entries[i];
}
// Copy the base libraries in there as well
System.arraycopy(baseList, 0, outgoing, entries.length, baseList.length);
return outgoing;
}
}
return null;
}
//static protected HashMap<String, Object> packageWarningMap = new HashMap<>();
/**
* Add the packages provided by this library to the master list that maps
* imports to specific libraries.
* @param importToLibraryTable mapping from package names to Library objects
*/
// public void addPackageList(HashMap<String,Library> importToLibraryTable) {
public void addPackageList(Map<String, List<Library>> importToLibraryTable) {
// PApplet.println(packages);
for (String pkg : packageList) {
// pw.println(pkg + "\t" + libraryFolder.getAbsolutePath());
// PApplet.println(pkg + "\t" + getName());
// Library library = importToLibraryTable.get(pkg);
List<Library> libraries = importToLibraryTable.get(pkg);
if (libraries == null) {
libraries = new ArrayList<>();
importToLibraryTable.put(pkg, libraries);
} else {
if (Base.DEBUG) {
System.err.println("The library found in");
System.err.println(getPath());
System.err.println("conflicts with");
for (Library library : libraries) {
System.err.println(library.getPath());
}
System.err.println("which already define(s) the package " + pkg);
System.err.println("If you have a line in your sketch that reads");
System.err.println("import " + pkg + ".*;");
System.err.println("Then you'll need to first remove one of those libraries.");
System.err.println();
}
}
libraries.add(this);
}
}
public boolean hasExamples() {
return examplesFolder.exists();
}
public File getExamplesFolder() {
return examplesFolder;
}
public String getGroup() {
return group;
}
public String getPath() {
return folder.getAbsolutePath();
}
public String getLibraryPath() {
return libraryFolder.getAbsolutePath();
}
public String getJarPath() {
//return new File(folder, "library/" + name + ".jar").getAbsolutePath();
return new File(libraryFolder, folder.getName() + ".jar").getAbsolutePath();
}
// the returned value begins with File.pathSeparatorChar
// so that it can be appended to other paths safely
public String getClassPath() {
StringBuilder cp = new StringBuilder();
String[] jarHeads = libraryFolder.list(jarFilter);
if (jarHeads != null) {
for (String jar : jarHeads) {
cp.append(File.pathSeparatorChar);
cp.append(new File(libraryFolder, jar).getAbsolutePath());
}
}
File nativeLibraryFolder = new File(nativeLibraryPath);
if (!libraryFolder.equals(nativeLibraryFolder)) {
jarHeads = new File(nativeLibraryPath).list(jarFilter);
if (jarHeads != null) {
for (String jar : jarHeads) {
cp.append(File.pathSeparatorChar);
cp.append(new File(nativeLibraryPath, jar).getAbsolutePath());
}
}
}
return cp.toString();
}
public String getNativePath() {
return nativeLibraryPath;
}
protected File[] wrapFiles(String[] list) {
File[] outgoing = new File[list.length];
for (int i = 0; i < list.length; i++) {
outgoing[i] = new File(libraryFolder, list[i]);
}
return outgoing;
}
public File[] getApplicationExports(String variant) {
String[] list = getApplicationExportList(variant);
return wrapFiles(list);
}
/*
public File[] getApplicationExports(int platform, String variant) {
String[] list = getApplicationExportList(platform, variant);
return wrapFiles(list);
}
// * Returns the necessary exports for the specified platform.
// * If no 32 or 64-bit version of the exports exists, it returns the version
// * that doesn't specify bit depth.
public String[] getApplicationExportList(int platform, String variant) {
String platformName = PConstants.platformNames[platform];
if (variant.equals("32")) {
String[] pieces = exportList.get(platformName + "32");
if (pieces != null) return pieces;
} else if (variant.equals("64")) {
String[] pieces = exportList.get(platformName + "64");
if (pieces != null) return pieces;
} else if (variant.equals("armv6hf")) {
String[] pieces = exportList.get(platformName + "-armv6hf");
if (pieces != null) return pieces;
} else if (variant.equals("arm64")) {
String[] pieces = exportList.get(platformName + "-arm64");
if (pieces != null) return pieces;
}
return exportList.get(platformName);
}
*/
public String[] getApplicationExportList(String variant) {
if (exportList.isEmpty()) {
return baseList;
}
return exportList.get(variant);
}
@SuppressWarnings("unused")
public File[] getAndroidExports() {
return wrapFiles(androidExportList);
}
/*
public boolean hasMultipleArch(int platform) {
//return multipleArch[platform];
return multipleArch.getOrDefault(platform, false);
}
*/
/*
public boolean supportsArch(int platform, String variant) {
// If this is a universal library, or has no natives, then we're good.
if (multipleArch[platform] == false) {
return true;
}
return getApplicationExportList(platform, variant) != null;
}
public boolean isUnsupported(int platform, String variant) {
// If this is a universal library, or has no natives, then we're good.
if (!multipleArch.containsKey(platformNames[platform])) {
return false;
}
return getApplicationExportList(platform, variant) == null;
}
*/
public boolean isUnsupported(String variant) {
if (exportList.isEmpty()) {
// if no per-platform exports, then nothing to worry about
return false;
}
return getApplicationExportList(variant) == null;
}
/*
static public boolean hasMultipleArch(int platform, List<Library> libraries) {
return libraries.stream().anyMatch(library -> library.hasMultipleArch(platform));
}
*/
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
static protected FilenameFilter junkFolderFilter = (dir, name) -> {
// skip .DS_Store files, .svn and .git folders, etc
if (name.charAt(0) == '.') return false;
if (name.equals("CVS")) return false; // old skool
return new File(dir, name).isDirectory();
};
static public String findCollision(File folder) {
File[] jars = PApplet.listFiles(folder, "recursive", "extension=jar");
if (jars != null) {
for (File file : jars) {
try {
ZipFile zf = new ZipFile(file);
if (zf.getEntry("processing/core/PApplet.class") != null) {
return "processing.core";
}
if (zf.getEntry("processing/app/Base.class") != null) {
return "processing.app";
}
} catch (IOException e) {
// ignored
}
}
}
return null;
}
static public List<File> discover(File folder) {
List<File> libraries = new ArrayList<>();
String[] folderNames = folder.list(junkFolderFilter);
// if a bad folder or unreadable, folderNames might be null
if (folderNames != null) {
// alphabetize list, since it's not always alpha order
// replaced hella slow bubble sort with this feller for 0093
Arrays.sort(folderNames, String.CASE_INSENSITIVE_ORDER);
// TODO a little odd because ContributionType.LIBRARY.isCandidate()
// handles some, but not all, of this; and the rules of selection
// should probably be consolidated in a sensible way [fry 200116]
for (String potentialName : folderNames) {
File baseFolder = new File(folder, potentialName);
File libraryFolder = new File(baseFolder, "library");
File libraryJar = new File(libraryFolder, potentialName + ".jar");
// If a .jar file of the same prefix as the folder exists
// inside the 'library' subfolder of the sketch
if (libraryJar.exists()) {
String sanityCheck = Sketch.sanitizeName(potentialName);
if (!sanityCheck.equals(potentialName)) {
final String mess =
"The library \"" + potentialName + "\" cannot be used.\n" +
"Library names must contain only basic letters and numbers.\n" +
"(ASCII only and no spaces, and it cannot start with a number)";
Messages.showMessage("Ignoring bad library name", mess);
} else {
String pkg = findCollision(libraryFolder);
if (pkg != null) {
final String mess =
"The library \"" + potentialName + "\" cannot be used\n" +
"because it contains the " + pkg + " libraries.\n" +
"Please contact the library author for an update.";
Messages.showMessage("Ignoring bad library", mess);
// Move the folder out of the way
File badFolder = new File(baseFolder.getParentFile(), "disabled");
boolean success = true;
if (!badFolder.exists()) {
success = badFolder.mkdirs();
}
if (success) {
File hideFolder = new File(badFolder, baseFolder.getName());
success = baseFolder.renameTo(hideFolder);
if (success) {
System.out.println("Moved " + baseFolder + " to " + hideFolder);
} else {
System.err.println("Could not move " + baseFolder + " to " + hideFolder);
}
}
} else {
libraries.add(baseFolder);
}
}
}
}
}
return libraries;
}
static Set<String> duplicateLibraries = new HashSet<>();
static public List<Library> list(File folder) {
List<Library> libraries = new ArrayList<>();
List<File> librariesFolders = new ArrayList<>(discover(folder));
Map<String, File> seen = new HashMap<>();
for (File baseFolder : librariesFolders) {
Library lib = new Library(baseFolder);
String name = lib.getName();
File foundEarlier = seen.get(name);
if (foundEarlier != null) {
// Warn the user about this duplication (later, on the EDT)
if (!duplicateLibraries.contains(name)) {
Messages.showWarningTiered("Duplicate Library Found",
"There are multiple libraries named “" + name + "”",
"Please remove either “" + foundEarlier.getName() +
" or “" + lib.getName() + "”\n" +
"from " + folder.getPath() + "\n" +
"and restart Processing.", null);
}
duplicateLibraries.add(name);
} else {
seen.put(name, lib.getFolder());
libraries.add(lib);
}
}
return libraries;
}
public ContributionType getType() {
return ContributionType.LIBRARY;
}
/**
* Returns the object stored in the referenceFile field, which contains an
* instance of the file object representing the index file of the reference
*
* @return referenceFile
*/
public File getReferenceIndexFile() {
return referenceFile;
}
/**
* Tests whether the reference's index file indicated by referenceFile exists.
*
* @return true if and only if the file denoted by referenceFile exists; false
* otherwise.
*/
public boolean hasReference() {
return referenceFile.exists();
}
}