Jumper.cs Example C# Code

[Home] C# Visual Novel Interpreter Example


  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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class NovelBranch
{
	public string title;
	public List<NovelFrame> frames;
	public NovelBranch(string title, List<NovelFrame> frames)
	{
		this.title = title;
		this.frames = frames;
	}
}

public abstract class NovelFrame
{
	public Tokens token;
	public object payload;
	public override string ToString()
	{
		return  token.ToString()+":" + payload.ToString();
	}

}


public class NovelOption : NovelFrame
{
	public override string ToString()
	{
		return  token.ToString()+"=>("+target+"):" + payload.ToString();
	}

	public string title;
	public string target;
	public NovelOption(ref List<FrameFragment> fragments)
	{ 
		token = Tokens.option;
		payload = fragments[0].payload;
		fragments.Remove(fragments[0]);
		target = (string) new NovelJump(ref fragments).payload;
	}
}

public class NovelText: NovelFrame
{
	public override string ToString()
	{
		return token.ToString()+":"+character+":"+line;
	}
	public string character;
	public string line;
	public NovelText(ref List<FrameFragment> fragments)
	{
		token = Tokens.TEXT;
		fragments.Remove(fragments[0]);

		NovelCharacter novelCharacter = new NovelCharacter(ref fragments);

		string line = string.Empty;
		foreach (NovelFrame frame in novelCharacter.ownedFrames)
		{
			line += frame.payload;
		}

		this.character = novelCharacter.name;
		this.line = line;
	}
}

public class NovelJump : NovelFrame
{
	public NovelJump(ref List<FrameFragment> fragments)
	{ 
		token = Tokens.jump;
		payload = fragments[0].payload;
		fragments.Remove(fragments[0]);
	}
}

public class NovelHideCharacter : NovelFrame
{
	public NovelHideCharacter(ref List<FrameFragment> fragments)
	{ 
		token = Tokens.hidecharacter;
		payload = fragments[0].payload;
		fragments.Remove(fragments[0]);
	}
}

public class NovelDirective : NovelFrame
{
	public NovelDirective(ref List<FrameFragment> fragments)
	{ 
		token = Tokens.directive;
		payload = fragments[0].payload;
		fragments.Remove(fragments[0]);
	}
}

public class NovelOptions : NovelFrame
{
	public List<NovelOption> options;

	public override string ToString()
	{
		string result = token.ToString()+":\n";
		foreach (NovelOption option in options)
		{result += option.ToString() + "\n";}
		return result;
	}

	public NovelOptions(ref List<FrameFragment> frames)
	{
		token = Tokens.options;
		frames.Remove(frames[0]);
		options = new List<NovelOption>();

		List<FrameFragment> selected = new List<FrameFragment>();
		List<FrameFragment> post = new List<FrameFragment>();

		bool foundExit = false;
        // make sure the options syntax is terminated before the branch, and gather
        // frames that are subordinate to this one.
		foreach (FrameFragment fragment in frames)
		{
			if (fragment.token == Tokens.exitbranch)
			{ throw new System.Exception("Reached the end of a branch before an option tree was terminated.");}

			if (fragment.token == Tokens.endoptions)
			{
				foundExit = true;
			}
			else
			{
				if (foundExit)
				{ post.Add(fragment); }
				else 
				{ selected.Add(fragment); }
			}
		}

		if (foundExit == false)
		{
			 throw new System.Exception("Expected token to terminate option tree "
			+" but did not find terminator endoptions."); 
		}

		while (selected.Count > 0)
		{
			options.Add(new NovelOption(ref selected));
		}

		frames = post;

	}
}

public class NovelMonologue : NovelFrame
{
	public NovelMonologue(ref List<FrameFragment> lines)
	{
		token = Tokens.monologue;

		List<FrameFragment> selected = new List<FrameFragment>();
		List<FrameFragment> post = new List<FrameFragment>();
		bool terminator = false;
		string payload = string.Empty;

		lines.Remove(lines[0]);

		foreach (FrameFragment fragment in lines)
		{ 
			if (fragment.token != Tokens.none)
			{
				terminator = true;
				post.Add(fragment);
			}
			else
			{
				if (terminator)
				{
					post.Add(fragment);
				}
				else 
				{
					selected.Add(fragment);
					if (payload != string.Empty)
					{payload += " ";}
					payload += fragment.payload;
				}
			}
		}

		this.payload = payload;
		lines = post;
	}
}

public class NovelLine : NovelFrame
{
	public string character;
	public NovelLine(ref List<FrameFragment> lines, NovelCharacter character)
	{
		this.character = character.name;
		token = Tokens.line;

		List<FrameFragment> selected = new List<FrameFragment>();
		List<FrameFragment> post = new List<FrameFragment>();
		bool terminator = false;
		string payload = lines[0].payload;

		lines.Remove(lines[0]);

		foreach (FrameFragment fragment in lines)
		{ 
			if (fragment.token != Tokens.none)
			{
				terminator = true;
				post.Add(fragment);
			}
			else
			{
				if (terminator)
				{
					post.Add(fragment);
				}
				else 
				{
					selected.Add(fragment);
					if (payload != string.Empty)
					{payload += " ";}
					payload += fragment.payload;
				}
			}
		}

		this.payload = payload;
		lines = post;
	}
}

public class NovelBackground : NovelFrame
{
	public NovelBackground(ref List<FrameFragment> fragments)
	{ 
		token = Tokens.background;
		payload = fragments[0].payload;
		fragments.Remove(fragments[0]);
	}
}

public class NovelExpression : NovelFrame
{
	public string character;
	public NovelExpression(ref List<FrameFragment> fragments, NovelCharacter character)
	{ 
		this.character = character.name;
		token = Tokens.expression;
		payload = fragments[0].payload;
		fragments.Remove(fragments[0]);
	}
}

public class NovelCharacter : NovelFrame
{
	public override string ToString()
	{
		string result = token.ToString() + "("+name+")";
		if (ownedFrames != null)
		{
			result+="\n";
			foreach (NovelFrame frame in ownedFrames)
			{
				result += frame.ToString() + "\n";
			}
		}
		return result;
	}
	public string name;
	public List<NovelFrame> ownedFrames;
    // this function takes the frames it consumed and returns the whole thing as a 'flat' list
    //  instead of as a character object that has a list of frames inside it.
	public List<NovelFrame> Flatten()
	{
		List<NovelFrame> frames = ownedFrames;
		ownedFrames = null;
		frames.Insert(0, this);
		return frames;
	}
	public NovelCharacter(ref List<FrameFragment> fragments)
	{
		token = Tokens.character;
		name = fragments[0].payload;
		ownedFrames = new List<NovelFrame>();

		bool terminator = false;

		fragments.Remove(fragments[0]);

        // in the case of expression and line statements, they actually don't contain enough 
        // information to be run by themselves, which is why there must be (somewhere) a preceeding character statement.
        // this function provides these frames with the missing information (who is speaking) and consumes them 
        // from the parser.
		while (!terminator && fragments.Count > 0)
		{ 
			switch (fragments[0].token)
			{
				case Tokens.expression:
				{
					ownedFrames.Add(new NovelExpression(ref fragments, this));
				} break;
				case Tokens.line:
				{
					ownedFrames.Add(new NovelLine(ref fragments, this));
				} break;
				default:
				{
					terminator = true;
				} break;
			}
		}
	}
}


public enum Tokens { none, monologue, background, branch, character, hidecharacter, 
		expression, line, jump, exitbranch, directive, TEXT,
		options, option, optionexit, endoptions}

public class NovelScript
{
	Dictionary<string, List<NovelFrame>> branches;

}

public class FrameFragment
{
	public Tokens token;
	public string payload;

}

public class ParseScript {

	ParseState state = ParseState.none;

	[System.Flags]
	public enum ParseState { none=0, branch, character, line, options, option}

	// Use this for initialization
	public List<NovelBranch> LoadScript () {
		//StreamReader reader = new StreamReader("Assets/Resources/LDSCRIPTRRG.script");
		StringReader reader = new StringReader(TheDAta.rawDAta);

		List<FrameFragment> lines = new List<FrameFragment>();
		
		while (reader.Peek() != -1)
		{
			string outLine = string.Empty;
			string line = reader.ReadLine();
			line = line.TrimStart(' ','\t').TrimEnd(' ','\t');
			if (line.StartsWith("//") || line == string.Empty || line.StartsWith("endline"))
			{ 
				outLine = "COMMENT: " + line;
			}
			else
			{ 
				lines.Add(Parse(line));
				outLine = line;
			}
		}

		List<NovelBranch> branches = new List<NovelBranch>();
		while (lines.Count > 0)
		{ 
			NovelBranch branch = ConsumeBranch(ref lines);
			string log = "BRANCH:"+branch.title+"\n";
			foreach (NovelFrame frame in branch.frames)
			{
				log += frame + "\n";
			}
			//Debug.Log(log);
			branches.Add(branch);
		}
		return branches;
	}
	
	FrameFragment Parse(string line)
	{
		//Debug.Log("PARSE("+state+"):"+line);
		string payload = string.Empty;
		Tokens t = Tokens.none;
		foreach (int i in System.Enum.GetValues(typeof(Tokens)))
		{
			
			Tokens tIndex = (Tokens)i;
            // weird conditional is a hack dealing with inconsistent(incorrect?) spelling of monologue in the system
			if (line.StartsWith(tIndex.ToString())
			|| (tIndex == Tokens.monologue && line.StartsWith("monolouge")))
			{
				t = tIndex;
				payload = line.Substring(tIndex.ToString().Length).TrimStart(' ','\t').TrimEnd(' ','\t');
				
				break;
			}
		}

		if (t == Tokens.none)
		{payload = line;}
		//Debug.Log(t.ToString() + "|" + payload);
		
		return new FrameFragment() {token = t, payload = payload};
	}

	NovelBranch ConsumeBranch(ref List<FrameFragment> lines)
	{
		List<NovelFrame> result = new List<NovelFrame>();

		if (lines[0].token != Tokens.branch)
		{ throw new System.Exception("Expected TOKEN branch but got "+lines[0].token); }

		string title = lines[0].payload;

		lines.Remove(lines[0]);

		List<FrameFragment> frames = new List<FrameFragment>();
		List<FrameFragment> post = new List<FrameFragment>();

		bool foundExit = false;

        // check that the branch is terminated, and store the frames in this branch
		foreach (FrameFragment fragment in lines)
		{
			if (foundExit)
			{ post.Add(fragment); }
			else 
			{ 
				if (fragment.token == Tokens.exitbranch)
				{ foundExit = true; }
				else 
				{ frames.Add(fragment);} 
			}
		}

		if (foundExit == false)
		{
			 throw new System.Exception("Expected token to terminate Branch "+title
			+" but did not find terminator. ("
			+("exitbranch "+title)+")"); 
		}

        // re-organize the frames into a runnable configuration. (for instance, get rid of expression frames, since their 
        //  functionality is duplicated by character frames)
		while (frames.Count > 0)
		{ 
			NovelFrame frame = Consume(ref frames);
			if (frame is NovelCharacter)
			{
				List<NovelFrame> flattened = (frame as NovelCharacter).Flatten();
				result.AddRange(flattened);
			} 
			else
			{
				result.Add( frame);
			}
		}

        // remove the branch and all frame in it from the list of frames.
		lines = post;
		return new NovelBranch(title, result);
	}

	NovelFrame Consume(ref List<FrameFragment> fragments)
	{
		switch (fragments[0].token)
		{
			case Tokens.background:
			{ 
				return new NovelBackground(ref fragments);
			} 
			case Tokens.monologue:
			{ 
				return new NovelMonologue(ref fragments);
			} 
			case Tokens.character:
			{
				return new NovelCharacter(ref fragments);
			} 
			case Tokens.options:
			{
				return new NovelOptions(ref fragments);
			} 
			case Tokens.hidecharacter:
			{
				return new NovelHideCharacter(ref fragments);
			} 
			case Tokens.directive:
			{
				return new NovelDirective(ref fragments);
			}
			case Tokens.jump:
			{ 
				return new NovelJump(ref fragments); 
			}
			case Tokens.TEXT:
			{
				return new NovelText(ref fragments);
			}
			
 			default: 
			{
				throw new System.IndexOutOfRangeException("Bad token:"+fragments[0].token.ToString());
			}
		}
	}

}