save.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. export interface SavedMoment {
  2. /** The title of the associated passage. */
  3. title: string;
  4. /** The current variable store object, which contains sigil - less name ⇒ value pairs(e.g.$foo exists as foo). */
  5. variables: { [x: string]: any };
  6. /** The current pull count of SugarCube's seedable PRNG, exists only if enabled. */
  7. pull?: number | undefined;
  8. }
  9. export interface SavedState {
  10. /** The array of moment objects. */
  11. history: SavedMoment[];
  12. /** The index of the active moment. */
  13. index: number;
  14. /** The array of expired moment passage titles, exists only if any moments have expired. */
  15. expired?: string[] | undefined;
  16. /** The seed of SugarCube's seedable PRNG, exists only if enabled. */
  17. seed?: string | undefined;
  18. }
  19. export interface SaveObject {
  20. /** The story's save ID. */
  21. id: string;
  22. /** The marshaled story history(see below for details). */
  23. state: SavedState;
  24. /** The title of the save. */
  25. title: string;
  26. /** The date when the save was created(in milliseconds elapsed since epoch). */
  27. date: number;
  28. /** Save metadata(end - user specified; must be JSON - serializable). */
  29. metadata?: any;
  30. /** Save version(end - user specified via Config.saves.version). */
  31. version?: any;
  32. }
  33. export interface SaveDetails {
  34. /**
  35. * A string representing how the save operation came about — i.e., what caused it.
  36. */
  37. type: "autosave" | "disk" | "serialize" | "slot";
  38. }
  39. type SaveHandler = (save: SaveObject, details: SaveDetails) => void;
  40. type LoadHandler = (save: SaveObject) => void;
  41. interface SaveEventAPI<HandlerType> {
  42. /**
  43. * Add new handler
  44. * @param handler
  45. */
  46. add(handler: HandlerType): void;
  47. /**
  48. * Deletes all currently registered handlers.
  49. */
  50. clear(): void;
  51. /**
  52. * Deletes the specified handler.
  53. * @param handler The handler function to be deleted.
  54. * @returns `true` if the handler existed or `false` if not.
  55. * @example
  56. * // Given:
  57. * // let myOnLoadHandler = function (save) {
  58. * // // code to process the save object before it's loaded
  59. * // };
  60. * // Save.onLoad.add(myOnLoadHandler);
  61. *
  62. * Save.onLoad.delete(myOnLoadHandler);
  63. */
  64. delete(handler: HandlerType): boolean;
  65. /**
  66. * Returns the number of currently registered handlers.
  67. */
  68. size: number;
  69. }
  70. export interface SaveAPI {
  71. /**
  72. * Deletes all slot saves and the autosave, if it's enabled.
  73. * @since 2.0.0
  74. */
  75. clear(): void;
  76. /**
  77. * Returns the saves object.
  78. * @since 2.0.0
  79. */
  80. get(): object;
  81. /**
  82. * Returns whether both the slot saves and autosave are available and ready.
  83. * @since 2.0.0
  84. */
  85. ok(): boolean;
  86. /**
  87. * Performs any required processing before the save data is loaded — e.g., upgrading
  88. * out-of-date save data. The handler is passed one parameter, the save object to be
  89. * processed. If it encounters an unrecoverable problem during its processing, it may
  90. * throw an exception containing an error message; the message will be displayed to
  91. * the player and loading of the save will be terminated.
  92. * @since 2.36.0
  93. */
  94. onLoad: SaveEventAPI<LoadHandler>;
  95. /**
  96. * Performs any required processing before the save data is saved. The handlers is passed
  97. * two parameters, the save object to be processed and save operation details object.
  98. * @since 2.36.0
  99. */
  100. onSave: SaveEventAPI<SaveHandler>;
  101. slots: {
  102. /**
  103. * Returns the total number of available slots.
  104. * @since 2.0.0
  105. */
  106. length: number;
  107. /**
  108. * Returns the total number of filled slots.
  109. * @since 2.0.0
  110. */
  111. count(): number;
  112. /**
  113. * Deletes a save from the given slot.
  114. * @param slot Save slot index (0-based).
  115. * @since 2.0.0
  116. */
  117. delete(slot: number): void;
  118. /**
  119. * Returns a save object from the given slot or null, if there was no save in the given slot.
  120. * @param slot Save slot index (0-based).
  121. * @since 2.0.0
  122. */
  123. get(slot: number): SaveObject;
  124. /**
  125. * Returns whether the given slot is filled.
  126. * @param slot Save slot index (0-based).
  127. * @since 2.0.0
  128. */
  129. has(slot: number): boolean;
  130. /**
  131. * Returns whether there are any filled slots.
  132. * @since 2.0.0
  133. */
  134. isEmpty(): boolean;
  135. /**
  136. * Loads a save from the given slot.
  137. * @param slot
  138. */
  139. load(slot: number): void;
  140. /**
  141. * Returns whether the slot saves are available and ready.
  142. * @since 2.0.0
  143. */
  144. ok(): boolean;
  145. /**
  146. * Saves to the given slot.
  147. * @param slot Save slot index (0-based).
  148. * @param title The title of the save. If omitted or null, defaults to the passage's description.
  149. * @param metadata The data to be stored in the save object's metadata property. Must be JSON-serializable.
  150. * @since 2.0.0
  151. */
  152. save(slot: number, title?: string, metadata?: any): void;
  153. };
  154. autosave: {
  155. /**
  156. * Deletes the autosave.
  157. * @since 2.0.0
  158. */
  159. delete(): void;
  160. /**
  161. * Returns the save object from the autosave or null, if there was no autosave.
  162. * @since 2.0.0
  163. */
  164. get(): SaveObject;
  165. /**
  166. * Returns whether the autosave is filled.
  167. * @since 2.0.0
  168. */
  169. has(): boolean;
  170. /**
  171. * Loads the autosave.
  172. * @since 2.0.0
  173. */
  174. load(): void;
  175. /**
  176. * Returns whether the autosave is available and ready.
  177. * @since 2.0.0
  178. */
  179. ok(): boolean;
  180. /**
  181. * Saves to the autosave.
  182. * @param title The title of the save. If omitted or null, defaults to the passage's description.
  183. * @param metadata The data to be stored in the save object's metadata property. Must be JSON-serializable.
  184. * @since 2.0.0
  185. */
  186. save(title?: string, metadata?: any): void;
  187. };
  188. /**
  189. * Saves to disk.
  190. * @param filename The base filename of the save, which gets slugified to remove most symbols. Appended to this is a datestamp
  191. * (format: YYYMMDD-hhmmss) and the file extension .save. (e.g. "The Scooby Chronicles" would result in the full filename:
  192. * the-scooby-chronicles-{datestamp}.save). If omitted or null, defaults to the story's title.
  193. * @param metadata The data to be stored in the save object's metadata property. Must be JSON-serializable.
  194. * @since 2.8.0
  195. */
  196. export(filename?: string, metadata?: any): void;
  197. /**
  198. * Loads a save from disk.
  199. *
  200. * NOTE: You do not call this manually, it must be called by the change event handler of an <input type="file"> element.
  201. * @param event The event object which was passed to the change event handler of the associated <input type="file"> element.
  202. * @since 2.0.0
  203. * @example
  204. * // Add file input
  205. * var input = document.createElement('input');
  206. * input.type = 'file';
  207. * input.id = 'saves-import-file';
  208. * input.name = 'saves-import-file';
  209. * // Set up Save.import() as the event handler for when a file has been chosen
  210. * jQuery(input).on('change', Save.import);
  211. */
  212. import(event: Event): void;
  213. /**
  214. * Returns a save as a serialized string, or null if saving is not allowed within the current context.
  215. * @param metadata The data to be stored as metadata. Must be JSON-serializable.
  216. * @since 2.21.0
  217. */
  218. serialize(metadata?: any): string;
  219. /**
  220. * Deserializes the given save string, created via Save.serialize(), and loads the save. Returns the bundled metadata, if any,
  221. * or null if the given save could not be deserialized and loaded.
  222. * @param saveStr The serialized save string.
  223. * @since 2.21.0
  224. */
  225. deserialize(saveStr: string): any;
  226. }
  227. export {};