Skip to content

Commit f26881f

Browse files
committed
Implement Ink changes from the reference C# impl. to 24/02
1 parent 1bb8327 commit f26881f

File tree

6 files changed

+180
-56
lines changed

6 files changed

+180
-56
lines changed

src/main/java/com/bladecoder/ink/runtime/CallStack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ public void setTemporaryVariable(String name, RTObject value, boolean declareNew
379379
}
380380

381381
public void setTemporaryVariable(String name, RTObject value, boolean declareNew, int contextIndex)
382-
throws StoryException, Exception {
382+
throws Exception {
383383
if (contextIndex == -1)
384384
contextIndex = getCurrentElementIndex() + 1;
385385

386386
Element contextElement = getCallStack().get(contextIndex - 1);
387387

388388
if (!declareNew && !contextElement.temporaryVariables.containsKey(name)) {
389-
throw new StoryException("Could not find temporary variable to set: " + name);
389+
throw new Exception("Could not find temporary variable to set: " + name);
390390
}
391391

392392
RTObject oldValue = contextElement.temporaryVariables.get(name);

src/main/java/com/bladecoder/ink/runtime/InkList.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.Comparator;
6-
import java.util.Map;
76
import java.util.HashMap;
87
import java.util.List;
8+
import java.util.Map;
99

1010
/**
1111
* The InkList is the underlying type that's used to store an instance of a list
@@ -47,7 +47,7 @@ public InkList(String singleOriginListName, Story originStory) throws Exception
4747
ListDefinition def = originStory.getListDefinitions().getListDefinition(singleOriginListName);
4848

4949
if (def != null) {
50-
origins = new ArrayList<ListDefinition>();
50+
origins = new ArrayList<>();
5151
origins.add(def);
5252
} else
5353
throw new Exception(
@@ -60,6 +60,22 @@ public InkList(String singleOriginListName, Story originStory) throws Exception
6060
public InkList(InkList otherList) {
6161
super(otherList);
6262
this.originNames = otherList.originNames;
63+
64+
if (otherList.origins != null) {
65+
origins = new ArrayList<>(otherList.origins);
66+
}
67+
}
68+
69+
/**
70+
* Converts a string to an ink list and returns for use in the story.
71+
*/
72+
public static InkList FromString(String myListItem, Story originStory) throws Exception {
73+
ListValue listValue = originStory.getListDefinitions().findSingleItemListWithName(myListItem);
74+
if (listValue != null)
75+
return new InkList(listValue.value);
76+
else
77+
throw new Exception("Could not find the InkListItem from the string '" + myListItem
78+
+ "' to create an InkList because it doesn't exist in the original list definition in ink.");
6379
}
6480

6581
ListDefinition getOriginOfMaxItem() {
@@ -86,7 +102,7 @@ public List<ListDefinition> getOrigins() {
86102
public List<String> getOriginNames() {
87103
if (this.size() > 0) {
88104
if (originNames == null && this.size() > 0)
89-
originNames = new ArrayList<String>();
105+
originNames = new ArrayList<>();
90106
else
91107
originNames.clear();
92108

@@ -98,15 +114,15 @@ public List<String> getOriginNames() {
98114
}
99115

100116
void setInitialOriginName(String initialOriginName) {
101-
originNames = new ArrayList<String>();
117+
originNames = new ArrayList<>();
102118
originNames.add(initialOriginName);
103119
}
104120

105121
void setInitialOriginNames(List<String> initialOriginNames) {
106122
if (initialOriginNames == null)
107123
originNames = null;
108124
else {
109-
originNames = new ArrayList<String>();
125+
originNames = new ArrayList<>();
110126
originNames.addAll(initialOriginNames);
111127
}
112128
}
@@ -128,8 +144,7 @@ public InkList union(InkList otherList) {
128144
* items removed that are in the passed in list. Equivalent to calling (list1 -
129145
* list2) in ink.
130146
*
131-
* @param listToRemove
132-
* List to remove.
147+
* @param listToRemove List to remove.
133148
*/
134149

135150
public InkList without(InkList listToRemove) {
@@ -191,8 +206,7 @@ public Map.Entry<InkListItem, Integer> getMinItem() {
191206
* Returns true if the current list contains all the items that are in the list
192207
* that is passed in. Equivalent to calling (list1 ? list2) in ink.
193208
*
194-
* @param otherList
195-
* Other list.
209+
* @param otherList Other list.
196210
*/
197211
public boolean contains(InkList otherList) {
198212
for (Map.Entry<InkListItem, Integer> kv : otherList.entrySet()) {
@@ -505,7 +519,7 @@ public int hashCode() {
505519
}
506520

507521
List<Entry<InkListItem, Integer>> getOrderedItems() {
508-
List<Entry<InkListItem, Integer>> ordered = new ArrayList<Entry<InkListItem, Integer>>(entrySet());
522+
List<Entry<InkListItem, Integer>> ordered = new ArrayList<>(entrySet());
509523

510524
Collections.sort(ordered, new Comparator<Entry<InkListItem, Integer>>() {
511525
@Override

src/main/java/com/bladecoder/ink/runtime/Profiler.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private class StepDetails {
4949
}
5050
}
5151

52-
private List<StepDetails> stepDetails = new ArrayList<StepDetails>();
52+
private List<StepDetails> stepDetails = new ArrayList<>();
5353

5454
/**
5555
* The root node in the hierarchical tree of recorded ink timings.
@@ -99,14 +99,18 @@ void step(CallStack callstack) {
9999

100100
String[] stack = new String[callstack.getElements().size()];
101101
for (int i = 0; i < stack.length; i++) {
102-
Path objPath = callstack.getElements().get(i).currentPointer.getPath();
102+
103103
String stackElementName = "";
104104

105-
for (int c = 0; c < objPath.getLength(); c++) {
106-
Component comp = objPath.getComponent(c);
107-
if (!comp.isIndex()) {
108-
stackElementName = comp.getName();
109-
break;
105+
if (!callstack.getElements().get(i).currentPointer.isNull()) {
106+
Path objPath = callstack.getElements().get(i).currentPointer.getPath();
107+
108+
for (int c = 0; c < objPath.getLength(); c++) {
109+
Component comp = objPath.getComponent(c);
110+
if (!comp.isIndex()) {
111+
stackElementName = comp.getName();
112+
break;
113+
}
110114
}
111115
}
112116

@@ -153,7 +157,7 @@ public String stepLengthReport() {
153157
sb.append("TOTAL: " + rootNode.getTotalMillisecs() + "ms\n");
154158

155159
// AVERAGE STEP TIMES
156-
HashMap<String, Double> typeToDetails = new HashMap<String, Double>();
160+
HashMap<String, Double> typeToDetails = new HashMap<>();
157161

158162
// average group by s.type
159163
for (StepDetails sd : stepDetails) {
@@ -177,9 +181,10 @@ public String stepLengthReport() {
177181
}
178182

179183
// sort by average
180-
List<Entry<String, Double>> averageStepTimes = new LinkedList<Entry<String, Double>>(typeToDetails.entrySet());
184+
List<Entry<String, Double>> averageStepTimes = new LinkedList<>(typeToDetails.entrySet());
181185

182186
Collections.sort(averageStepTimes, new Comparator<Entry<String, Double>>() {
187+
@Override
183188
public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
184189
return (int) (o1.getValue() - o2.getValue());
185190
}
@@ -198,8 +203,7 @@ public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
198203
}
199204

200205
sb.append('\n');
201-
202-
206+
203207
// ACCUMULATED STEP TIMES
204208
typeToDetails.clear();
205209

@@ -217,13 +221,14 @@ public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
217221
}
218222
}
219223

220-
typeToDetails.put(sd.type + " (x"+typeToDetails.size()+")", sum);
224+
typeToDetails.put(sd.type + " (x" + typeToDetails.size() + ")", sum);
221225
}
222226

223227
// sort by average
224-
List<Entry<String, Double>> accumStepTimes = new LinkedList<Entry<String, Double>>(typeToDetails.entrySet());
228+
List<Entry<String, Double>> accumStepTimes = new LinkedList<>(typeToDetails.entrySet());
225229

226230
Collections.sort(accumStepTimes, new Comparator<Entry<String, Double>>() {
231+
@Override
227232
public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
228233
return (int) (o1.getValue() - o2.getValue());
229234
}

0 commit comments

Comments
 (0)