Skip to content

Commit

Permalink
CK3 color fixes
Browse files Browse the repository at this point in the history
Allow named colors in CK3 religions
Allow CK3 religions to have HSV colors
Allow "color = rgb { 0 0 0 }" as valid syntax
Correctly load religions and cultures when using a mod
  • Loading branch information
mmyers committed Jun 20, 2024
1 parent 6eaa837 commit 1a86d01
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions EU3_Scenario_Editor/src/editor/mapmode/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static boolean isNotACountry(final String tag) {
private static final java.util.Map<String, String> cultureGroups = new HashMap<>();

private static final List<Color> geographyColors = new ArrayList<>();

private static java.util.Map<String, Color> namedColors;

private static FilenameResolver resolver;

Expand All @@ -143,7 +145,7 @@ private static void initReligions() {
religions = EUGFileIO.load(resolver.resolveFilename("common/religion.txt"), settings);
if (religions == null)
religions = EUGFileIO.loadAll(resolver.listFiles("common/religions"), settings);
if (religions == null)
if (religions == null || religions.isEmpty())
religions = EUGFileIO.loadAllUTF8(resolver.listFiles("common/religion/religions"), settings);
}

Expand All @@ -159,7 +161,7 @@ private static void initCultures() {
cultures = EUGFileIO.load(resolver.resolveFilename("common/cultures.txt"), settings);
if (cultures == null)
cultures = EUGFileIO.loadAll(resolver.listFiles("common/cultures"), settings);
if (cultures == null)
if (cultures == null || cultures.isEmpty())
initCK3Cultures();

// go ahead and set up the colors here since we need to keep track of
Expand Down Expand Up @@ -214,7 +216,7 @@ private static void initCultures() {
}

private static void initCK3Cultures() {
java.util.Map<String, Color> namedColors = readCK3NamedColors();
initNamedColors();
cultures = EUGFileIO.loadAllUTF8(resolver.listFiles("common/culture/cultures"), settings);
for (GenericObject culture : cultures.children) {
Color color = parseColorMaybeHsv(culture, "color");
Expand All @@ -224,20 +226,21 @@ private static void initCK3Cultures() {
}
}

private static java.util.Map<String, Color> readCK3NamedColors() {
java.util.Map<String, Color> ret = new HashMap<>();
GenericObject namedColors = EUGFileIO.loadUTF8(new java.io.File(resolver.resolveFilename("common/named_colors/culture_colors.txt")), ParserSettings.getQuietSettings());
GenericObject colorsRoot = namedColors.getChild("colors");
for (int i = 0; i < colorsRoot.getAllWritable().size(); i++) {
WritableObject obj = colorsRoot.getAllWritable().get(i);
if (obj instanceof GenericList) {
GenericList list = (GenericList) obj;
ret.put(list.getName(), parseColor(list));
} else if (obj instanceof ObjectVariable) {
ret.put(((ObjectVariable) obj).varname, parseColorHsv((GenericList)colorsRoot.getAllWritable().get(++i)));
private static void initNamedColors() {
if (namedColors == null) {
namedColors = new HashMap<>();
GenericObject namedColorsObj = EUGFileIO.loadUTF8(new java.io.File(resolver.resolveFilename("common/named_colors/culture_colors.txt")), ParserSettings.getQuietSettings());
GenericObject colorsRoot = namedColorsObj.getChild("colors");
for (int i = 0; i < colorsRoot.getAllWritable().size(); i++) {
WritableObject obj = colorsRoot.getAllWritable().get(i);
if (obj instanceof GenericList) {
GenericList list = (GenericList) obj;
namedColors.put(list.getName(), parseColor(list));
} else if (obj instanceof ObjectVariable) {
namedColors.put(((ObjectVariable) obj).varname, parseColorHsv((GenericList)colorsRoot.getAllWritable().get(++i)));
}
}
}
return ret;
}

private static void initGeographyColors() {
Expand Down Expand Up @@ -312,6 +315,11 @@ private static Color parseColorMaybeHsv(GenericObject parent, String key) {
if (shouldBeColorObj instanceof GenericList) {
return parseColorHsv((GenericList) shouldBeColorObj);
}
} else if (maybeColorVar.varname.equalsIgnoreCase(key) && maybeColorVar.getValue().equalsIgnoreCase("rgb")) {
WritableObject shouldBeColorObj = parent.getAllWritable().get(i+1);
if (shouldBeColorObj instanceof GenericList) {
return parseColor((GenericList) shouldBeColorObj);
}
}
}
}
Expand Down Expand Up @@ -428,13 +436,15 @@ static Color getReligionColor(String religion) {
}

private static Color cacheReligion(GenericObject group, String religion) {
GenericList color = group.getList("color");
if (color == null) {
Color ret = parseColorMaybeHsv(group, "color");
if (ret == null && group.hasString("color")) {
initNamedColors();
ret = namedColors.get(group.getString("color"));
}
if (ret == null) {
log.log(Level.WARNING, "color for {0} is null", religion);
relColorCache.put(religion, COLOR_NO_RELIGION_DEF);
return COLOR_NO_RELIGION_DEF;
ret = COLOR_NO_RELIGION_DEF;
}
Color ret = parseColor(color);
relColorCache.put(religion, ret);
return ret;
}
Expand Down Expand Up @@ -462,7 +472,7 @@ static Color getCultureColor(String culture) {
}

public static String getCultureGroup(String culture) {
if (cultureGroups.isEmpty())
if (cultures == null)
initCultures();
return cultureGroups.get(culture);
}
Expand Down

0 comments on commit 1a86d01

Please sign in to comment.