From 125afc2e0d71cfd9534e1f8e55705f1731b3d11c Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 3 Aug 2022 06:56:34 -0400 Subject: [PATCH] also support additional naming.json file in sketchbook folder --- app/src/processing/app/SketchName.java | 36 +++++++++++++++++++------- todo.txt | 1 + 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/src/processing/app/SketchName.java b/app/src/processing/app/SketchName.java index 76e5ececc..ab12949de 100644 --- a/app/src/processing/app/SketchName.java +++ b/app/src/processing/app/SketchName.java @@ -6,7 +6,6 @@ import processing.data.JSONObject; import processing.data.StringList; import java.io.File; -import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -15,6 +14,7 @@ import java.util.Map; public class SketchName { + static final String FILENAME = "naming.json"; static final String CLASSIC_NAME = "Classic (sketch_220809a)"; static boolean breakTime = false; @@ -119,26 +119,42 @@ public class SketchName { File outgoing = null; if (wl != null) { do { - outgoing = new File(parentDir, wl.getPair()); + // Clean up the name in case a user-supplied word list breaks the rules + String name = Sketch.sanitizeName(wl.getPair()); + outgoing = new File(parentDir, name); } while (outgoing.exists()); } return outgoing; } + static private void load(File namingFile) { + JSONArray array = PApplet.loadJSONArray(namingFile); + for (int i = 0; i < array.size(); i++) { + JSONObject obj = array.getJSONObject(i); + WordList wl = new WordList(obj); + wordLists.put(wl.name, wl); + } + } + static Map getWordLists() { if (wordLists == null) { wordLists = new HashMap<>(); try { - File namingFile = Base.getLibFile("naming.json"); - JSONArray array = PApplet.loadJSONArray(namingFile); - for (int i = 0; i < array.size(); i++) { - JSONObject obj = array.getJSONObject(i); - WordList wl = new WordList(obj); - wordLists.put(wl.name, wl); + File namingFile = Base.getLibFile(FILENAME); + load(namingFile); + } catch (Exception e) { + Messages.showWarning("Naming Error", + "Could not load word lists from " + FILENAME, e); + } + File sketchbookFile = new File(Base.getSketchbookFolder(), FILENAME); + if (sketchbookFile.exists()) { + try { + load(sketchbookFile); + } catch (Exception e) { + Messages.showWarning("Naming Error", + "Error while reading " + FILENAME + " from sketchbook folder", e); } - } catch (IOException e) { - Messages.showWarning("Naming Error", "Could not load word lists from naming.json", e); } } return wordLists; diff --git a/todo.txt b/todo.txt index 53d0ee09f..a9f4e1898 100755 --- a/todo.txt +++ b/todo.txt @@ -45,6 +45,7 @@ X how friendly names are used in the p5.js web editor X https://github.com/processing/p5.js-web-editor/blob/develop/client/utils/generateRandomName.js X p5.js uses predicate followed by an object X add other naming options (cooking and classic) and prefs +X also support additional naming.json file in sketchbook folder bugs X re-post bugzilla entries at https://download.processing.org/bugzilla/