Skip to content

Commit

Permalink
More cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
bladecoder committed Sep 25, 2023
1 parent dd0e1ab commit 5231e43
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 152 deletions.
9 changes: 2 additions & 7 deletions src/main/java/com/bladecoder/ink/runtime/Container.java
Expand Up @@ -57,20 +57,15 @@ public HashMap<String, RTObject> getNamedOnlyContent() {
}
}

if (namedOnlyContentDict.isEmpty()) 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
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
23 changes: 8 additions & 15 deletions src/main/java/com/bladecoder/ink/runtime/Json.java
Expand Up @@ -332,10 +332,6 @@ public static RTObject jTokenToRuntimeObject(Object token) throws Exception {
if ("L^".equals(str)) str = "^";
if (NativeFunctionCall.callExistsWithName(str)) return NativeFunctionCall.callWithName(str);

// Pop
if ("->->".equals(str)) return ControlCommand.popTunnel();
else if ("~ret".equals(str)) return ControlCommand.popFunction();

// Void
if ("void".equals(str)) return new Void();
}
Expand Down Expand Up @@ -538,21 +534,18 @@ public static void writeRuntimeContainer(SimpleJson.Writer writer, Container con
int countFlags = container.getCountFlags();
boolean hasNameProperty = container.getName() != null && !withoutName;

boolean hasTerminator = namedOnlyContent != null || countFlags > 0 || hasNameProperty;
boolean hasTerminator = !namedOnlyContent.isEmpty()|| countFlags > 0 || hasNameProperty;

if (hasTerminator) writer.writeObjectStart();

if (namedOnlyContent != null) {

for (Entry<String, RTObject> namedContent : namedOnlyContent.entrySet()) {
String name = namedContent.getKey();
Container namedContainer =
namedContent.getValue() instanceof Container ? (Container) namedContent.getValue() : null;
for (Entry<String, RTObject> namedContent : namedOnlyContent.entrySet()) {
String name = namedContent.getKey();
Container namedContainer =
namedContent.getValue() instanceof Container ? (Container) namedContent.getValue() : null;

writer.writePropertyStart(name);
writeRuntimeContainer(writer, namedContainer, true);
writer.writePropertyEnd();
}
writer.writePropertyStart(name);
writeRuntimeContainer(writer, namedContainer, true);
writer.writePropertyEnd();
}

if (countFlags > 0) writer.writeProperty("#f", countFlags);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/bladecoder/ink/runtime/Story.java
Expand Up @@ -1561,7 +1561,6 @@ boolean performLogicAndFlowControl(RTObject contentObj) throws Exception {

DivertTargetValue target = (DivertTargetValue) varContents;
state.setDivertedPointer(pointerAtPath(target.getTargetPath()));

} else if (currentDivert.isExternal()) {
callExternalFunction(currentDivert.getTargetPathString(), currentDivert.getExternalArgs());
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bladecoder/ink/runtime/StoryState.java
Expand Up @@ -287,11 +287,11 @@ void trimWhitespaceFromFunctionEnd() {
for (int i = getOutputStream().size() - 1; i >= functionStartPoint; i--) {
RTObject obj = getOutputStream().get(i);

if (obj instanceof ControlCommand) break;

if (!(obj instanceof StringValue)) continue;
StringValue txt = (StringValue) obj;

if (obj instanceof ControlCommand) break;

if (txt.isNewline() || txt.isInlineWhitespace()) {
getOutputStream().remove(i);
outputStreamDirty();
Expand Down
Expand Up @@ -222,7 +222,7 @@ int getContextIndexOfVariableNamed(String varName) {
return callStack.getCurrentElementIndex();
}

RTObject getRawVariableWithName(String name, int contextIndex) throws Exception {
RTObject getRawVariableWithName(String name, int contextIndex) {
RTObject varValue = null;
// 0 context = global
if (contextIndex == 0 || contextIndex == -1) {
Expand Down
Expand Up @@ -217,6 +217,8 @@ public void conditionalChoice() throws Exception {
String json = TestUtils.getJsonString("inkfiles/choices/conditional-choice.ink.json");
Story story = new Story(json);

System.out.println(story.buildStringOfHierarchy());

TestUtils.nextAll(story, text);

Assert.assertEquals(4, story.getCurrentChoices().size());
Expand Down
Expand Up @@ -18,6 +18,8 @@ public void ifTrue() throws Exception {
String json = TestUtils.getJsonString("inkfiles/conditional/iftrue.ink.json");
Story story = new Story(json);

System.out.println(story.buildStringOfHierarchy());

TestUtils.nextAll(story, text);

Assert.assertEquals(1, text.size());
Expand Down
20 changes: 9 additions & 11 deletions src/test/java/com/bladecoder/ink/runtime/test/TestUtils.java
Expand Up @@ -4,11 +4,11 @@

import com.bladecoder.ink.runtime.Choice;
import com.bladecoder.ink.runtime.Story;
import com.bladecoder.ink.runtime.StoryException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -18,9 +18,9 @@ public static String getJsonString(String filename) throws IOException {

InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream(filename);

BufferedReader br = new BufferedReader(new InputStreamReader(systemResourceAsStream, "UTF-8"));

try {
assert systemResourceAsStream != null;
try (BufferedReader br =
new BufferedReader(new InputStreamReader(systemResourceAsStream, StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();

Expand All @@ -33,8 +33,6 @@ public static String getJsonString(String filename) throws IOException {
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}

Expand All @@ -43,8 +41,6 @@ public static List<String> runStory(String filename, List<Integer> choiceList, L
// 1) Load story
String json = getJsonString(filename);

// System.out.println(json);

Story story = new Story(json);

List<String> text = new ArrayList<>();
Expand All @@ -53,7 +49,9 @@ public static List<String> runStory(String filename, List<Integer> choiceList, L

int choiceListIndex = 0;

while (story.canContinue() || story.getCurrentChoices().size() > 0) {
while (story.canContinue() || !story.getCurrentChoices().isEmpty()) {
//System.out.println(story.buildStringOfHierarchy());

// 2) Game content, line by line
while (story.canContinue()) {
String line = story.Continue();
Expand All @@ -69,7 +67,7 @@ public static List<String> runStory(String filename, List<Integer> choiceList, L
}

// 3) Display story.currentChoices list, allow player to choose one
if (story.getCurrentChoices().size() > 0) {
if (!story.getCurrentChoices().isEmpty()) {

for (Choice c : story.getCurrentChoices()) {
System.out.println(c.getText());
Expand Down Expand Up @@ -103,7 +101,7 @@ public static boolean isEnded(Story story) {
return !story.canContinue() && story.getCurrentChoices().isEmpty();
}

public static void nextAll(Story story, List<String> text) throws StoryException, Exception {
public static void nextAll(Story story, List<String> text) throws Exception {
while (story.canContinue()) {
String line = story.Continue();
System.out.print(line);
Expand Down

0 comments on commit 5231e43

Please sign in to comment.