-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInkStory.m
135 lines (109 loc) · 4.16 KB
/
InkStory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// InkStory.m
// Created by Russell Quinn
//
#import "InkStory.h"
@implementation InkStory
- (instancetype)initWithStoryJsonFile:(NSString *)filePath
{
self = [super init];
if (self)
{
self.jsContext = [[JSContext alloc] init];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *pathForInkJSRuntime = [mainBundle pathForResource:@"ink" ofType:@"js"];
if (pathForInkJSRuntime != nil)
{
NSString *jsRuntimeScript = [NSString stringWithContentsOfFile:pathForInkJSRuntime
encoding:NSUTF8StringEncoding
error:NULL];
[self evaluateScriptInContext:jsRuntimeScript];
}
else
{
NSException *fileException = [NSException
exceptionWithName:@"FileNotFoundException"
reason:@"Cannot find ink.js runtime"
userInfo:nil];
[fileException raise];
}
if (filePath != nil)
{
NSString *pathForStoryJson = [mainBundle pathForResource:filePath ofType:nil];
if (pathForStoryJson != nil)
{
NSString *storyJsonString = [NSString stringWithContentsOfFile:pathForStoryJson
encoding:NSUTF8StringEncoding
error:NULL];
[self evaluateScriptInContext:[NSString stringWithFormat:@"var storyContent = %@;", storyJsonString]];
[self evaluateScriptInContext:@"story = new inkjs.Story(storyContent);"];
}
}
}
return self;
}
- (NSString *)toJson
{
// There's a typo in inkjs, should be ToJson -- will fix when inkjs is fixed.
return [[self evaluateScriptInContext:@"story.state.toJson();"] toString];
}
- (void)loadJson:(NSString *)json
{
// Double escape JSON input to pass as a parameter inside evaluateScriptInContext
json = [json stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
json = [json stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
json = [json stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
json = [json stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
json = [json stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
json = [json stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
[self evaluateScriptInContext:[NSString stringWithFormat:@"story.state.LoadJson(\"%@\");", json]];
}
- (BOOL)canContinue
{
return [[self evaluateScriptInContext:@"story.canContinue;"] toBool];
}
- (NSString *)continueStory
{
return [[self evaluateScriptInContext:@"story.Continue();"] toString];
}
- (NSString *)currentText
{
return [[self evaluateScriptInContext:@"story.currentText;"] toString];
}
- (BOOL)hasError
{
return [[self evaluateScriptInContext:@"story.hasError;"] toBool];
}
- (NSArray *)currentErrors
{
return [[self evaluateScriptInContext:@"story.currentErrors;"] toArray];
}
- (NSArray *)currentChoices
{
return [[self evaluateScriptInContext:@"story.currentChoices;"] toArray];
}
- (void)chooseChoiceIndex:(NSInteger)index
{
[self evaluateScriptInContext:[NSString stringWithFormat:@"story.ChooseChoiceIndex(%ld);", (long)index]];
}
- (NSArray *)currentTags
{
return [[self evaluateScriptInContext:@"story.currentTags;"] toArray];
}
- (NSArray *)globalTags
{
return [[self evaluateScriptInContext:@"story.globalTags;"] toArray];
}
- (NSArray *)tagsForContentAtPath:(NSString *)path
{
return [[self evaluateScriptInContext:[NSString stringWithFormat:@"story.TagsForContentAtPath(\"%@\");", path]] toArray];
}
- (void)choosePathString:(NSString *)path
{
[self evaluateScriptInContext:[NSString stringWithFormat:@"story.ChoosePathString(\"%@\");", path]];
}
- (JSValue *)evaluateScriptInContext:(NSString *)jsToRun
{
return [self.jsContext evaluateScript:jsToRun];
}
@end