Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bladecoder committed Oct 16, 2023
1 parent d5a4357 commit b1c1e61
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 288 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/bladecoder/ink/runtime/CallStack.java
Expand Up @@ -178,7 +178,7 @@ public CallStack(CallStack toCopy) {
}

public CallStack(Story storyContext) {
startOfRoot.assign(Pointer.startOf(storyContext.getRootContentContainer()));
startOfRoot.assign(Pointer.startOf(storyContext.getMainContentContainer()));

reset();
}
Expand Down Expand Up @@ -361,7 +361,7 @@ public void setJsonToken(HashMap<String, Object> jRTObject, Story storyContext)
}

threadCounter = (int) jRTObject.get("threadCounter");
startOfRoot.assign(Pointer.startOf(storyContext.getRootContentContainer()));
startOfRoot.assign(Pointer.startOf(storyContext.getMainContentContainer()));
}

public Thread forkThread() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bladecoder/ink/runtime/Choice.java
Expand Up @@ -30,7 +30,7 @@ public class Choice extends RTObject {

String sourcePath;

public Choice() throws Exception {}
public Choice() {}

public int getIndex() {
return index;
Expand Down
103 changes: 22 additions & 81 deletions src/main/java/com/bladecoder/ink/runtime/Container.java
Expand Up @@ -9,16 +9,16 @@
public class Container extends RTObject implements INamedContent {
private String name;

private List<RTObject> content;
private final List<RTObject> content;
private HashMap<String, INamedContent> namedContent;

private boolean visitsShouldBeCounted;
private boolean turnIndexShouldBeCounted;
private boolean countingAtStartOnly;

public Container() {
content = new ArrayList<RTObject>();
setNamedContent(new HashMap<String, INamedContent>());
content = new ArrayList<>();
setNamedContent(new HashMap<>());
}

@Override
Expand All @@ -34,10 +34,6 @@ public List<RTObject> getContent() {
return content;
}

public void setContent(List<RTObject> value) throws Exception {
addContent(value);
}

public HashMap<String, INamedContent> getNamedContent() {
return namedContent;
}
Expand All @@ -55,27 +51,21 @@ public HashMap<String, RTObject> getNamedOnlyContent() {
}

for (RTObject c : getContent()) {
INamedContent named = c instanceof INamedContent ? (INamedContent) c : (INamedContent) null;
INamedContent named = c instanceof INamedContent ? (INamedContent) c : null;
if (named != null && named.hasValidName()) {
namedOnlyContentDict.remove(named.getName());
}
}

if (namedOnlyContentDict.size() == 0) namedOnlyContentDict = null;

return namedOnlyContentDict;
}

public void setNamedOnlyContent(HashMap<String, RTObject> value) {
HashMap<String, RTObject> existingNamedOnly = getNamedOnlyContent();
if (existingNamedOnly != null) {
for (Entry<String, RTObject> kvPair : existingNamedOnly.entrySet()) {
getNamedContent().remove(kvPair.getKey());
}
for (Entry<String, RTObject> kvPair : existingNamedOnly.entrySet()) {
getNamedContent().remove(kvPair.getKey());
}

if (value == null) return;

for (Entry<String, RTObject> kvPair : value.entrySet()) {
INamedContent named = kvPair.getValue() instanceof INamedContent
? (INamedContent) kvPair.getValue()
Expand Down Expand Up @@ -134,46 +124,26 @@ public int getCountFlags() {
}

public void setCountFlags(int value) {
int flag = value;

if ((flag & COUNTFLAGS_VISITS) > 0) setVisitsShouldBeCounted(true);
if ((value & COUNTFLAGS_VISITS) > 0) setVisitsShouldBeCounted(true);

if ((flag & COUNTFLAGS_TURNS) > 0) setTurnIndexShouldBeCounted(true);
if ((value & COUNTFLAGS_TURNS) > 0) setTurnIndexShouldBeCounted(true);

if ((flag & COUNTFLAGS_COUNTSTARTONLY) > 0) setCountingAtStartOnly(true);
if ((value & COUNTFLAGS_COUNTSTARTONLY) > 0) setCountingAtStartOnly(true);
}

@Override
public boolean hasValidName() {
return getName() != null && getName().length() > 0;
return getName() != null && !getName().isEmpty();
}

public Path getPathToFirstLeafContent() {
if (_pathToFirstLeafContent == null)
_pathToFirstLeafContent = getPath().pathByAppendingPath(getInternalPathToFirstLeafContent());

return _pathToFirstLeafContent;
}

Path _pathToFirstLeafContent;

Path getInternalPathToFirstLeafContent() {
List<Component> components = new ArrayList<Path.Component>();

Container container = this;
while (container != null) {
if (container.getContent().size() > 0) {
components.add(new Path.Component(0));
container = container.getContent().get(0) instanceof Container
? (Container) container.getContent().get(0)
: (Container) null;
}
public void addContents(List<RTObject> contentList) throws Exception {
for (RTObject c : contentList) {
addContent(c);
}

return new Path(components);
}

public void addContent(RTObject contentObj) throws Exception {
private void addContent(RTObject contentObj) throws Exception {
getContent().add(contentObj);

if (contentObj.getParent() != null) {
Expand All @@ -185,25 +155,8 @@ public void addContent(RTObject contentObj) throws Exception {
tryAddNamedContent(contentObj);
}

public void addContent(List<RTObject> contentList) throws Exception {
for (RTObject c : contentList) {
addContent(c);
}
}

public void insertContent(RTObject contentObj, int index) throws Exception {
getContent().add(index, contentObj);
if (contentObj.getParent() != null) {
throw new Exception("content is already in " + contentObj.getParent());
}

contentObj.setParent(this);
tryAddNamedContent(contentObj);
}

public void tryAddNamedContent(RTObject contentObj) throws Exception {
INamedContent namedContentObj =
contentObj instanceof INamedContent ? (INamedContent) contentObj : (INamedContent) null;
private void tryAddNamedContent(RTObject contentObj) {
INamedContent namedContentObj = contentObj instanceof INamedContent ? (INamedContent) contentObj : null;
if (namedContentObj != null && namedContentObj.hasValidName()) {
addToNamedContentOnly(namedContentObj);
}
Expand All @@ -219,17 +172,7 @@ public void addToNamedContentOnly(INamedContent namedContentObj) {
getNamedContent().put(namedContentObj.getName(), namedContentObj);
}

public void addContentsOfContainer(Container otherContainer) throws Exception {
getContent().addAll(otherContainer.getContent());

for (RTObject obj : otherContainer.getContent()) {
obj.setParent(this);

tryAddNamedContent(obj);
}
}

protected RTObject contentWithPathComponent(Path.Component component) throws StoryException, Exception {
private RTObject contentWithPathComponent(Path.Component component) {

if (component.isIndex()) {
if (component.getIndex() >= 0 && component.getIndex() < getContent().size()) {
Expand All @@ -252,11 +195,11 @@ protected RTObject contentWithPathComponent(Path.Component component) throws Sto
}
}

public SearchResult contentAtPath(Path path) throws Exception {
public SearchResult contentAtPath(Path path) {
return contentAtPath(path, 0, -1);
}

public SearchResult contentAtPath(Path path, int partialPathStart, int partialPathLength) throws Exception {
public SearchResult contentAtPath(Path path, int partialPathStart, int partialPathLength) {
if (partialPathLength == -1) partialPathLength = path.getLength();

SearchResult result = new SearchResult();
Expand Down Expand Up @@ -342,17 +285,15 @@ public void buildStringOfHierarchy(StringBuilder sb, int indentation, RTObject p
sb.append("\n");
}

HashMap<String, INamedContent> onlyNamed = new HashMap<String, INamedContent>();
HashMap<String, INamedContent> onlyNamed = new HashMap<>();

for (Entry<String, INamedContent> objKV : getNamedContent().entrySet()) {
if (getContent().contains(objKV.getValue())) {
continue;
} else {
if (!getContent().contains(objKV.getValue())) {
onlyNamed.put(objKV.getKey(), objKV.getValue());
}
}

if (onlyNamed.size() > 0) {
if (!onlyNamed.isEmpty()) {
appendIndentation(sb, indentation);

sb.append("-- named: --\n");
Expand Down
114 changes: 0 additions & 114 deletions src/main/java/com/bladecoder/ink/runtime/ControlCommand.java
Expand Up @@ -55,120 +55,6 @@ RTObject copy() {
return new ControlCommand(getCommandType());
}

// The following static factory methods are to make generating these
// RTObjects
// slightly more succinct. Without these, the code gets pretty massive! e.g.
//
// var c = new
// Runtime.ControlCommand(Runtime.ControlCommand.CommandType.EvalStart)
//
// as opposed to
//
// var c = Runtime.ControlCommand.EvalStart()
public static ControlCommand evalStart() {
return new ControlCommand(CommandType.EvalStart);
}

public static ControlCommand evalOutput() {
return new ControlCommand(CommandType.EvalOutput);
}

public static ControlCommand evalEnd() {
return new ControlCommand(CommandType.EvalEnd);
}

public static ControlCommand duplicate() {
return new ControlCommand(CommandType.Duplicate);
}

public static ControlCommand popEvaluatedValue() {
return new ControlCommand(CommandType.PopEvaluatedValue);
}

public static ControlCommand popFunction() {
return new ControlCommand(CommandType.PopFunction);
}

public static ControlCommand popTunnel() {
return new ControlCommand(CommandType.PopTunnel);
}

public static ControlCommand beginString() {
return new ControlCommand(CommandType.BeginString);
}

public static ControlCommand endString() {
return new ControlCommand(CommandType.EndString);
}

public static ControlCommand noOp() {
return new ControlCommand(CommandType.NoOp);
}

public static ControlCommand choiceCount() {
return new ControlCommand(CommandType.ChoiceCount);
}

public static ControlCommand turns() {
return new ControlCommand(CommandType.Turns);
}

public static ControlCommand turnsSince() {
return new ControlCommand(CommandType.TurnsSince);
}

public static ControlCommand readCount() {
return new ControlCommand(CommandType.ReadCount);
}

public static ControlCommand random() {
return new ControlCommand(CommandType.Random);
}

public static ControlCommand seedRandom() {
return new ControlCommand(CommandType.SeedRandom);
}

public static ControlCommand visitIndex() {
return new ControlCommand(CommandType.VisitIndex);
}

public static ControlCommand sequenceShuffleIndex() {
return new ControlCommand(CommandType.SequenceShuffleIndex);
}

public static ControlCommand startThread() {
return new ControlCommand(CommandType.StartThread);
}

public static ControlCommand done() {
return new ControlCommand(CommandType.Done);
}

public static ControlCommand end() {
return new ControlCommand(CommandType.End);
}

public static ControlCommand listFromInt() {
return new ControlCommand(CommandType.ListFromInt);
}

public static ControlCommand listRange() {
return new ControlCommand(CommandType.ListRange);
}

public static ControlCommand listRandom() {
return new ControlCommand(CommandType.ListRandom);
}

public static ControlCommand beginTag() {
return new ControlCommand(CommandType.BeginTag);
}

public static ControlCommand endTag() {
return new ControlCommand(CommandType.EndTag);
}

@Override
public String toString() {
return getCommandType().toString();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bladecoder/ink/runtime/Divert.java
Expand Up @@ -62,7 +62,7 @@ public Pointer getTargetPointer() throws Exception {
RTObject targetObj = resolvePath(targetPath).obj;

if (targetPath.getLastComponent().isIndex()) {
targetPointer.container = (Container) targetObj.getParent();
targetPointer.container = targetObj.getParent();
targetPointer.index = targetPath.getLastComponent().getIndex();
} else {
targetPointer.assign(Pointer.startOf((Container) targetObj));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bladecoder/ink/runtime/InkList.java
Expand Up @@ -549,7 +549,7 @@ public String toString() {
return sb.toString();
}

public class CustomEntry implements Map.Entry<InkListItem, Integer> {
public static class CustomEntry implements Map.Entry<InkListItem, Integer> {

private InkListItem key;
private Integer value;
Expand Down

0 comments on commit b1c1e61

Please sign in to comment.