Skip to content

Commit

Permalink
Switch to json (#232)
Browse files Browse the repository at this point in the history
- Switch to json for data file storage
- Also use the log rather than dumping straight to stdout/stderr in those places where that was still being done
- Bump maximum library version to consider
- Update fontbox to 2.0.20
- Update pdfbox to 2.0.20
- #228: Add option to use either name or title in footer
  • Loading branch information
richardwilkes authored Jul 10, 2020
1 parent f52f37c commit 5f89a70
Show file tree
Hide file tree
Showing 308 changed files with 6,405 additions and 2,388 deletions.
11 changes: 11 additions & 0 deletions .idea/inspectionProfiles/GCS.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bundler/bundler/Bundler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
import java.util.Set;

public class Bundler {
private static final String GCS_VERSION = "4.19.1";
private static final String GCS_VERSION = "4.20.0";
private static String JDK_MAJOR_VERSION = "14";
private static final String ITEXT_VERSION = "2.1.7";
private static final String LOGGING_VERSION = "1.2.0";
private static final String FONTBOX_VERSION = "2.0.17";
private static final String PDFBOX_VERSION = "2.0.17";
private static final String FONTBOX_VERSION = "2.0.20";
private static final String PDFBOX_VERSION = "2.0.20";
private static final String LINUX = "linux";
private static final String MACOS = "macos";
private static final String WINDOWS = "windows";
Expand Down
8 changes: 4 additions & 4 deletions com.trollworks.gcs/src/com/trollworks/gcs/GCS.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.trollworks.gcs.ui.widget.WindowUtils;
import com.trollworks.gcs.ui.widget.Workspace;
import com.trollworks.gcs.utility.I18n;
import com.trollworks.gcs.utility.Log;
import com.trollworks.gcs.utility.Platform;
import com.trollworks.gcs.utility.UpdateChecker;
import com.trollworks.gcs.utility.Version;
Expand All @@ -49,9 +50,8 @@

/** The main entry point for the character sheet. */
public class GCS {
public static final String WEB_SITE = "https://gurpscharactersheet.com";
public static final Version VERSION = new Version();
public static final Version LIBRARY_VERSION = new Version();
public static final String WEB_SITE = "https://gurpscharactersheet.com";
public static final Version VERSION = new Version();
public static final String COPYRIGHT;
public static final String COPYRIGHT_FOOTER;
public static final String APP_BANNER;
Expand Down Expand Up @@ -305,7 +305,7 @@ public static void initialize() {
UIManager.getDefaults().put(Fonts.KEY_STD_TEXT_FIELD, new Font("SansSerif", current.getStyle(), current.getSize()));
WiderToolTipUI.installIfNeeded();
} catch (Exception ex) {
ex.printStackTrace(System.err);
Log.error(ex);
}
Fonts.loadFromPreferences();
}
Expand Down
175 changes: 130 additions & 45 deletions com.trollworks.gcs/src/com/trollworks/gcs/advantage/Advantage.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
import com.trollworks.gcs.ui.widget.outline.Switchable;
import com.trollworks.gcs.utility.FilteredIterator;
import com.trollworks.gcs.utility.FilteredList;
import com.trollworks.gcs.utility.Fixed6;
import com.trollworks.gcs.utility.I18n;
import com.trollworks.gcs.utility.Log;
import com.trollworks.gcs.utility.json.JsonArray;
import com.trollworks.gcs.utility.json.JsonMap;
import com.trollworks.gcs.utility.json.JsonWriter;
import com.trollworks.gcs.utility.notification.Notifier;
import com.trollworks.gcs.utility.text.Enums;
import com.trollworks.gcs.utility.text.Text;
import com.trollworks.gcs.utility.xml.XMLReader;
import com.trollworks.gcs.utility.xml.XMLWriter;
import com.trollworks.gcs.weapon.MeleeWeaponStats;
import com.trollworks.gcs.weapon.RangedWeaponStats;
import com.trollworks.gcs.weapon.WeaponStats;
Expand All @@ -47,6 +51,7 @@

/** A GURPS Advantage. */
public class Advantage extends ListRow implements HasSourceReference, Switchable {
private static final int CURRENT_JSON_VERSION = 1;
private static final int CURRENT_VERSION = 4;
/** The XML tag used for items. */
public static final String TAG_ADVANTAGE = "advantage";
Expand All @@ -69,6 +74,15 @@ public class Advantage extends ListRow implements HasSourceReference, Switchable
private static final String ATTR_ROUND_COST_DOWN = "round_down";
private static final String ATTR_ALLOW_HALF_LEVELS = "allow_half_levels";
private static final String ATTR_HALF_LEVEL = "half_level";
private static final String KEY_CONTAINER_TYPE = "container_type";
private static final String KEY_WEAPONS = "weapons";
private static final String KEY_MODIFIERS = "modifiers";
private static final String KEY_CR_ADJ = "cr_adj";
private static final String KEY_MENTAL = "mental";
private static final String KEY_PHYSICAL = "physical";
private static final String KEY_SOCIAL = "social";
private static final String KEY_EXOTIC = "exotic";
private static final String KEY_SUPERNATURAL = "supernatural";
/** The prefix used in front of all IDs for the advantages. */
public static final String PREFIX = GURPSCharacter.CHARACTER_PREFIX + "advantage" + Notifier.SEPARATOR;
/** The field ID for type changes. */
Expand Down Expand Up @@ -194,6 +208,11 @@ public Advantage(DataFile dataFile, Advantage advantage, boolean deep) {
}
}

public Advantage(DataFile dataFile, JsonMap m, LoadState state) throws IOException {
this(dataFile, m.getString(DataFile.KEY_TYPE).equals(TAG_ADVANTAGE_CONTAINER));
load(m, state);
}

/**
* Loads an advantage and associates it with the specified data file.
*
Expand Down Expand Up @@ -232,6 +251,26 @@ public String getRowType() {
return I18n.Text("Advantage");
}

@Override
public String getJSONTypeName() {
return canHaveChildren() ? TAG_ADVANTAGE_CONTAINER : TAG_ADVANTAGE;
}

@Override
public int getJSONVersion() {
return CURRENT_JSON_VERSION;
}

@Override
public String getXMLTagName() {
return canHaveChildren() ? TAG_ADVANTAGE_CONTAINER : TAG_ADVANTAGE;
}

@Override
public int getXMLTagVersion() {
return CURRENT_VERSION;
}

@Override
protected void prepareForLoad(LoadState state) {
super.prepareForLoad(state);
Expand Down Expand Up @@ -267,7 +306,6 @@ protected void loadAttributes(XMLReader reader, LoadState state) {
@Override
protected void loadSubElement(XMLReader reader, LoadState state) throws IOException {
String name = reader.getName();

if (TAG_NAME.equals(name)) {
mName = reader.readText().replace("\n", " ");
} else if (TAG_CR.equals(name)) {
Expand Down Expand Up @@ -307,63 +345,112 @@ protected void loadSubElement(XMLReader reader, LoadState state) throws IOExcept
}

@Override
public String getXMLTagName() {
return canHaveChildren() ? TAG_ADVANTAGE_CONTAINER : TAG_ADVANTAGE;
protected void loadSelf(JsonMap m, LoadState state) throws IOException {
mRoundCostDown = m.getBoolean(ATTR_ROUND_COST_DOWN);
mDisabled = m.getBoolean(ATTR_DISABLED);
mAllowHalfLevels = m.getBoolean(ATTR_ALLOW_HALF_LEVELS);
if (canHaveChildren()) {
mContainerType = Enums.extract(m.getString(TAG_TYPE), AdvantageContainerType.values(), AdvantageContainerType.GROUP);
}
mName = m.getString(TAG_NAME);
mType = 0;
if (!canHaveChildren()) {
if (m.getBoolean(KEY_MENTAL)) {
mType |= TYPE_MASK_MENTAL;
}
if (m.getBoolean(KEY_PHYSICAL)) {
mType |= TYPE_MASK_PHYSICAL;
}
if (m.getBoolean(KEY_SOCIAL)) {
mType |= TYPE_MASK_SOCIAL;
}
if (m.getBoolean(KEY_EXOTIC)) {
mType |= TYPE_MASK_EXOTIC;
}
if (m.getBoolean(KEY_SUPERNATURAL)) {
mType |= TYPE_MASK_SUPERNATURAL;
}
if (m.has(TAG_LEVELS)) {
Fixed6 levels = new Fixed6(m.getString(TAG_LEVELS), false);
mLevels = (int) levels.asLong();
if (mAllowHalfLevels) {
mHalfLevel = levels.sub(new Fixed6(mLevels)).equals(new Fixed6(0.5));
}
}
mPoints = m.getInt(TAG_BASE_POINTS);
mPointsPerLevel = m.getInt(TAG_POINTS_PER_LEVEL);
if (m.has(KEY_WEAPONS)) {
WeaponStats.loadFromJSONArray(this, m.getArray(KEY_WEAPONS), mWeapons);
}
}
if (m.has(TAG_CR)) {
mCR = SelfControlRoll.getByCRValue(m.getInt(TAG_CR));
if (m.has(KEY_CR_ADJ)) {
mCRAdj = Enums.extract(m.getString(SelfControlRoll.ATTR_ADJUSTMENT), SelfControlRollAdjustments.values(), SelfControlRollAdjustments.NONE);
}
}
if (m.has(KEY_MODIFIERS)) {
JsonArray a = m.getArray(KEY_MODIFIERS);
int count = a.size();
for (int i = 0; i < count; i++) {
mModifiers.add(new AdvantageModifier(getDataFile(), a.getMap(i), state));
}
}
if (getDataFile() instanceof GURPSCharacter) {
mUserDesc = m.getString(TAG_USER_DESC);
}
mReference = m.getString(TAG_REFERENCE);
}

@Override
public int getXMLTagVersion() {
return CURRENT_VERSION;
protected void loadChild(JsonMap m, LoadState state) throws IOException {
if (!state.mForUndo) {
String type = m.getString(DataFile.KEY_TYPE);
if (TAG_ADVANTAGE.equals(type) || TAG_ADVANTAGE_CONTAINER.equals(type)) {
addChild(new Advantage(mDataFile, m, state));
} else {
Log.warn("invalid child type: " + type);
}
}
}

@Override
protected void saveAttributes(XMLWriter out, boolean forUndo) {
super.saveAttributes(out, forUndo);
if (mRoundCostDown) {
out.writeAttribute(ATTR_ROUND_COST_DOWN, true);
}
if (mAllowHalfLevels) {
out.writeAttribute(ATTR_ALLOW_HALF_LEVELS, true);
}
if (mDisabled) {
out.writeAttribute(ATTR_DISABLED, true);
}
protected void saveSelf(JsonWriter w, boolean forUndo) throws IOException {
w.keyValueNot(ATTR_ROUND_COST_DOWN, mRoundCostDown, false);
w.keyValueNot(ATTR_ALLOW_HALF_LEVELS, mAllowHalfLevels, false);
w.keyValueNot(ATTR_DISABLED, mDisabled, false);
if (canHaveChildren() && mContainerType != AdvantageContainerType.GROUP) {
out.writeAttribute(TAG_TYPE, Enums.toId(mContainerType));
w.keyValue(KEY_CONTAINER_TYPE, Enums.toId(mContainerType));
}
}

@Override
public void saveSelf(XMLWriter out, boolean forUndo) {
out.simpleTag(TAG_NAME, mName);
w.keyValue(TAG_NAME, mName);
if (!canHaveChildren()) {
out.simpleTag(TAG_TYPE, getTypeAsText());
w.keyValueNot(KEY_MENTAL, (mType & TYPE_MASK_MENTAL) != 0, false);
w.keyValueNot(KEY_PHYSICAL, (mType & TYPE_MASK_PHYSICAL) != 0, false);
w.keyValueNot(KEY_SOCIAL, (mType & TYPE_MASK_SOCIAL) != 0, false);
w.keyValueNot(KEY_EXOTIC, (mType & TYPE_MASK_EXOTIC) != 0, false);
w.keyValueNot(KEY_SUPERNATURAL, (mType & TYPE_MASK_SUPERNATURAL) != 0, false);
if (mLevels != -1) {
if (mAllowHalfLevels) {
out.simpleTagWithAttribute(TAG_LEVELS, mLevels, ATTR_HALF_LEVEL, mHalfLevel);
} else {
out.simpleTag(TAG_LEVELS, mLevels);
Fixed6 levels = new Fixed6(mLevels);
if (mAllowHalfLevels && mHalfLevel) {
levels = levels.add(new Fixed6(0.5));
}
w.keyValue(TAG_LEVELS, levels.toString());
}
if (mPoints != 0) {
out.simpleTag(TAG_BASE_POINTS, mPoints);
}
if (mPointsPerLevel != 0) {
out.simpleTag(TAG_POINTS_PER_LEVEL, mPointsPerLevel);
}

for (WeaponStats weapon : mWeapons) {
weapon.save(out);
}
w.keyValueNot(TAG_BASE_POINTS, mPoints, 0);
w.keyValueNot(TAG_POINTS_PER_LEVEL, mPointsPerLevel, 0);
WeaponStats.saveList(w, KEY_WEAPONS, mWeapons);
}
mCR.save(out, TAG_CR, mCRAdj);
for (AdvantageModifier modifier : mModifiers) {
modifier.save(out, forUndo);
if (mCR != SelfControlRoll.NONE_REQUIRED) {
w.keyValue(TAG_CR, mCR.getCR());
if (mCRAdj != SelfControlRollAdjustments.NONE) {
w.keyValue(KEY_CR_ADJ, Enums.toId(mCRAdj));
}
}
saveList(w, KEY_MODIFIERS, mModifiers, false);
if (getDataFile() instanceof GURPSCharacter) {
out.simpleTagNotEmpty(TAG_USER_DESC, mUserDesc);
w.keyValueNot(TAG_USER_DESC, mUserDesc, "");
}
out.simpleTagNotEmpty(TAG_REFERENCE, mReference);
w.keyValueNot(TAG_REFERENCE, mReference, "");
}

/** @return The container type. */
Expand Down Expand Up @@ -828,7 +915,6 @@ public boolean contains(String text, boolean lowerCaseOnly) {

private static int getTypeFromText(String text) {
int type = 0;

if (text.contains(TYPE_MENTAL)) {
type |= TYPE_MASK_MENTAL;
}
Expand All @@ -853,7 +939,6 @@ public String getTypeAsText() {
String separator = ", ";
StringBuilder buffer = new StringBuilder();
int type = getType();

if ((type & TYPE_MASK_MENTAL) != 0) {
buffer.append(TYPE_MENTAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,38 @@

package com.trollworks.gcs.advantage;

import com.trollworks.gcs.datafile.DataFile;
import com.trollworks.gcs.datafile.ListFile;
import com.trollworks.gcs.datafile.LoadState;
import com.trollworks.gcs.ui.RetinaIcon;
import com.trollworks.gcs.ui.image.Images;
import com.trollworks.gcs.ui.widget.outline.OutlineModel;
import com.trollworks.gcs.utility.FileType;
import com.trollworks.gcs.utility.Log;
import com.trollworks.gcs.utility.json.JsonArray;
import com.trollworks.gcs.utility.json.JsonMap;
import com.trollworks.gcs.utility.xml.XMLNodeType;
import com.trollworks.gcs.utility.xml.XMLReader;

import java.io.IOException;

/** A list of Advantages. */
public class AdvantageList extends ListFile {
private static final int CURRENT_JSON_VERSION = 1;
/** The current version. */
public static final int CURRENT_VERSION = 1;
public static final int CURRENT_VERSION = 1;
/** The XML tag for {@link AdvantageList}s. */
public static final String TAG_ROOT = "advantage_list";
public static final String TAG_ROOT = "advantage_list";

@Override
public int getJSONVersion() {
return CURRENT_JSON_VERSION;
}

@Override
public String getJSONTypeName() {
return TAG_ROOT;
}

@Override
public int getXMLTagVersion() {
Expand All @@ -49,6 +64,24 @@ public RetinaIcon getFileIcons() {
return Images.ADQ_FILE;
}

@Override
protected void loadList(JsonArray a, LoadState state) throws IOException {
loadIntoModel(this, a, getModel(), state);
}

public static void loadIntoModel(DataFile file, JsonArray a, OutlineModel model, LoadState state) throws IOException {
int count = a.size();
for (int i = 0; i < count; i++) {
JsonMap m1 = a.getMap(i);
String type = m1.getString(DataFile.KEY_TYPE);
if (Advantage.TAG_ADVANTAGE.equals(type) || Advantage.TAG_ADVANTAGE_CONTAINER.equals(type)) {
model.addRow(new Advantage(file, m1, state), true);
} else {
Log.warn("invalid advantage type: " + type);
}
}
}

@Override
protected void loadList(XMLReader reader, LoadState state) throws IOException {
OutlineModel model = getModel();
Expand Down
Loading

0 comments on commit 5f89a70

Please sign in to comment.