1package com.fs.starfarer.api.impl.codex;
3import java.io.IOException;
4import java.util.ArrayList;
6import java.util.regex.Matcher;
7import java.util.regex.Pattern;
11import org.json.JSONArray;
12import org.json.JSONException;
13import org.json.JSONObject;
15import com.fs.starfarer.api.Global;
16import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
17import com.fs.starfarer.api.ui.CustomPanelAPI;
18import com.fs.starfarer.api.ui.LabelAPI;
19import com.fs.starfarer.api.ui.TooltipMakerAPI;
20import com.fs.starfarer.api.ui.UIPanelAPI;
21import com.fs.starfarer.api.util.ListMap;
22import com.fs.starfarer.api.util.Misc;
26 public static class ParaData {
27 public int fontSize = 0;
28 public int bulletMode = 0;
31 public List<String> highlights =
new ArrayList<>();
32 public List<Color> colors =
new ArrayList<>();
34 public float imageHeight;
38 public List<Object> data =
new ArrayList<>();
39 public String parentId;
40 public TextEntry(String
id, String
title, String
icon) {
46 public void createTitleForList(
TooltipMakerAPI info,
float width, ListMode mode) {
47 super.createTitleForList(info, width, mode);
51 public boolean hasCustomDetailPanel() {
69 float horzBoxPad = 30f;
73 float tw = width - 290f - opad - horzBoxPad + 10f;
78 int prevBulletMode = 0;
79 for (Object o : data) {
80 if (o instanceof ParaData) {
81 ParaData para = (ParaData) o;
84 float outerPad = initPad;
85 if (para.image !=
null) {
86 float height = para.imageHeight;
92 if (prevBulletMode != 0 && para.bulletMode != 0) {
95 prevBulletMode = para.bulletMode;
97 if (para.fontSize == -1) {
99 }
else if (para.fontSize == 0) {
101 }
else if (para.fontSize == 1) {
103 }
else if (para.fontSize == 2) {
105 }
else if (para.fontSize == 3) {
109 if (para.bulletMode == 0) {
111 }
else if (para.bulletMode == 1) {
113 }
else if (para.bulletMode == 2) {
118 para.colors.toArray(
new Color[0]), para.highlights.toArray(
new String[0]));
120 label.
setHighlight(para.highlights.toArray(
new String[0]));
124 prevBulletMode = para.bulletMode;
126 if (para.image !=
null) {
130 if (para.fontSize > 0) {
139 if (relatedEntries !=
null) {
144 if (relatedEntries !=
null) {
154 for (String key :
LINKS.keySet()) {
156 for (String key2 :
LINKS.getList(key)) {
158 if (entry !=
null && other !=
null) {
168 String csvName =
"data/codex/text_codex_entries.csv";
172 for (
int i = 0; i < rows.length(); i++) {
173 JSONObject row = rows.getJSONObject(i);
174 String filename = row.getString(
"filename");
180 }
catch (IOException e) {
181 throw new RuntimeException(e);
182 }
catch (JSONException e) {
183 throw new RuntimeException(e);
189 contents = contents.replaceAll(
"\\r",
"").trim();
190 String [] lines = contents.split(
"\\n");
195 Color currColor =
null;
196 int currFontSize = 0;
197 String currCategory =
null;
198 String paraImage =
null;
199 int paraImageHeight = 0;
201 TextEntry entry =
null;
203 for (String line : lines) {
205 if (line.startsWith(
"#"))
continue;
206 if (line.isBlank())
continue;
209 if (line.startsWith(
"CATEGORY ")) {
210 line = line.replaceFirst(
"CATEGORY ",
"").trim();
212 String [] arr = line.split(
"\\|");
214 cat.parentId = arr[3];
218 if (parent !=
null) {
225 if (line.startsWith(
"COLOR ")) {
226 line = line.replaceFirst(
"COLOR ",
"").trim();
230 if (line.startsWith(
"FONT ")) {
231 line = line.replaceFirst(
"FONT ",
"").trim();
232 currFontSize = line.toLowerCase().equals(
"small") ? -1 : 0;
235 if (line.startsWith(
"IMAGE ")) {
236 line = line.replaceFirst(
"IMAGE ",
"").trim();
237 String [] arr = line.split(
"\\|");
239 if (arr.length > 1) {
241 paraImageHeight = Integer.parseInt(arr[1]);
242 }
catch (Throwable t) {
244 if (arr.length > 2) {
245 paraImageHeight = Integer.parseInt(arr[2]);
253 if (line.startsWith(
"RESET")) {
258 if (line.startsWith(
"CURRENT_CATEGORY ")) {
259 line = line.replaceFirst(
"CURRENT_CATEGORY ",
"").trim();
264 if (line.startsWith(
"BEGIN ")) {
265 line = line.replaceFirst(
"BEGIN ",
"").trim();
267 String [] arr = line.split(
"\\|");
268 entry =
new TextEntry(arr[0], arr[1],
null);
269 entry.parentId = currCategory;
270 if (arr.length >= 3) entry.parentId = arr[2];
275 throw new RuntimeException(String.format(
"Error parsing [%s] line %s, expected to be inside entry",
276 filename,
"" + lineNum));
279 if (line.startsWith(
"END")) {
285 if (parent !=
null) {
295 if (line.startsWith(
"ICON ")) {
296 line = line.replaceFirst(
"ICON ",
"").trim();
302 if (line.startsWith(
"RELATED ")) {
303 line = line.replaceFirst(
"RELATED ",
"").trim();
304 String [] ids = line.split(
"\\|");
305 for (String
id : ids) {
313 ParaData para =
new ParaData();
315 Pattern p = Pattern.compile(
"(?is)^(=+)(.+?)=+$");
316 Matcher m = p.matcher(line);
319 para.fontSize = m.group(1).length();
323 if (line.startsWith(
"_ ")) {
324 line = line.replaceFirst(
"_ ",
"").trim();
326 }
else if (line.startsWith(
"- ")) {
327 line = line.replaceFirst(
"- ",
"").trim();
331 p = Pattern.compile(
"(?is)\\{\\{(.*?)\\}\\}");
335 String curr = m.group(1);
336 String replacement =
"";
337 if (curr.startsWith(
"color:")) {
338 curr = curr.replaceFirst(
"color:",
"").trim();
339 String [] arr = curr.split(
"\\|");
341 para.colors.add(color);
342 para.highlights.add(arr[1]);
343 replacement = arr[1];
344 }
else if (curr.startsWith(
"rel:")) {
345 curr = curr.replaceFirst(
"rel:",
"").trim();
346 String [] arr = curr.split(
"\\|");
349 para.colors.add(base);
350 para.highlights.add(arr[1]);
351 replacement = arr[1];
354 para.colors.add(color);
355 para.highlights.add(curr);
358 line = line.replaceFirst(Pattern.quote(m.group(0)), replacement);
361 para.text = line.replaceAll(
"%",
"%%");
362 if (currColor !=
null) {
363 para.color = currColor;
365 if (para.fontSize == 0) {
366 para.fontSize = currFontSize;
369 para.image = paraImage;
370 para.imageHeight = paraImageHeight;
374 entry.data.add(para);
378 throw new RuntimeException(String.format(
"Error parsing [%s] file ended while still parsing entry", filename));
396 if (str.equals(
"h")) color = h;
397 else if (str.equals(
"good")) color = good;
398 else if (str.equals(
"bad")) color = bad;
399 else if (str.equals(
"gray")) color = gray;
400 else if (str.equals(
"grey")) color = gray;
401 else if (str.equals(
"text")) color = textColor;
402 else if (str.equals(
"player")) color = player;
403 else if (str.equals(
"blue")) color = player;
404 else if (str.equals(
"base")) color = player;
405 else if (str.equals(
"dark")) color = dark;
408 throw new RuntimeException(String.format(
"Parsing error: color [%s] not found", str));
414 public static void main(String[] args) {
415 String test =
"Testing 100% in text";
416 test = test.replaceAll(
"%",
"%%");
417 System.out.println(String.format(test));
static SettingsAPI getSettings()
static Logger getLogger(Class c)
static final String INDENT
static final String BULLET
static String getIcon(String key)
static void makeRelated(CodexEntryPlugin ... plugins)
static Map< String, CodexEntryPlugin > ENTRIES
static CodexEntryPlugin getEntry(String id)
void setParent(CodexEntryPlugin parent)
void setIcon(String icon)
void setParam(Object param)
static Color getColor(String str)
static ListMap< String > LINKS
static void linkRelated()
static void parseContents(String contents, String filename)
static void main(String[] args)
static void loadTextEntries()
static Color getTextColor()
static Color getNegativeHighlightColor()
static Color getBasePlayerColor()
static Color getGrayColor()
static Color getHighlightColor()
static Color getDarkPlayerColor()
static Color getPositiveHighlightColor()
boolean hasDesignTypeColor(String designType)
JSONArray loadCSV(String filename)
String loadText(String filename)
String getSpriteName(String category, String id)
SpriteAPI getSprite(String filename)
Color getColor(String id)
Color getDesignTypeColor(String designType)
void addChild(CodexEntryPlugin entry)
TooltipMakerAPI createUIElement(float width, float height, boolean withScroller)
void updateUIElementSizeAndMakeItProcessInput(TooltipMakerAPI element)
UIPanelAPI wrapTooltipWithBox(TooltipMakerAPI tooltip)
void setHighlight(int start, int end)
void setColor(Color color)
void setHighlightColors(Color ... colors)
PositionAPI inTL(float xPad, float yPad)
PositionAPI setSize(float width, float height)
PositionAPI inTR(float xPad, float yPad)
PositionAPI getPosition()
PositionAPI addComponent(UIComponentAPI custom)