Story.fromJson constructor

Story.fromJson(
  1. String jsonString
)
Construct a Story object using a JSON string compiled through inklecate.

Implementation

factory Story.fromJson(String jsonString) {
  var story = Story(null);
  Map<String, dynamic> rootObject = jsonDecode(jsonString);

  dynamic versionObj = rootObject['inkVersion'];
  if (versionObj == null) {
    throw Exception(
        "ink version number not found. Are you sure it's a valid .ink.json file?");
  }

  var formatFromFile = versionObj as int;
  if (formatFromFile > inkVersionCurrent) {
    throw Exception(
        'Version of ink used to build story was newer than the current version of the engine');
  } else if (formatFromFile < inkVersionMinimumCompatible) {
    throw Exception(
        'Version of ink used to build story is too old to be loaded by this version of the engine');
  } else if (formatFromFile != inkVersionCurrent) {
    print(
        "WARNING: Version of ink used to build story ($formatFromFile) doesn't match current version of engine. Non-critical, but recommend synchronising.");
  }

  var rootToken = rootObject['root'];
  if (rootToken == null) {
    throw Exception(
        "Root node for ink not found. Are you sure it's a valid .ink.json file?");
  }

  if (rootObject.containsKey('listDefs')) {
    Map<String, dynamic> listDefsObj =
        Map<String, dynamic>.from(rootObject['listDefs']);
    story._listDefinitions =
        JsonSerialization.JTokenToListDefinitions(listDefsObj);
  }

  story._mainContentContainer =
      JsonSerialization.JTokenToRuntimeObject(rootToken) as Container;

  story.resetState();
  return story;
}