Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow playing a json file #1059

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions script/inkjs-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var path = require('path');
import * as fs from "fs";
import { ErrorHandler } from '../src/engine/Error';

const BOM = '\u{feff}';

const help = process.argv.includes("-h");
if(help){
process.stdout.write(`
Expand Down Expand Up @@ -36,30 +38,39 @@ if(!inputFile){
process.stderr.write("No input file specified. -h for help\n");
process.exit(1);
}
outputfile = outputfile || inputFile+".json";

const fileHandler = new PosixFileHandler(path.dirname(inputFile));
const mainInk = fileHandler.LoadInkFileContents(inputFile);

const errorHandler: ErrorHandler = (message, errorType) => {
process.stderr.write(message + "\n");
};
const options = new CompilerOptions(
inputFile, [], countAllVisit, errorHandler, fileHandler
)

const c = new Compiler(mainInk, options);
const rstory = c.Compile();
if (!rstory) {
process.stderr.write("*** Compilation failed ***\n");
process.exit(1);
}

const jsonStory = rstory.ToJson()
let jsonStory: string = "";

if(!inputFile.endsWith(".json")){
outputfile = outputfile || inputFile+".json";

const fileHandler = new PosixFileHandler(path.dirname(inputFile));
const mainInk = fileHandler.LoadInkFileContents(inputFile);

const errorHandler: ErrorHandler = (message, errorType) => {
process.stderr.write(message + "\n");
};
const options = new CompilerOptions(
inputFile, [], countAllVisit, errorHandler, fileHandler
)

if(jsonStory && write){
const BOM = '\u{feff}';
fs.writeFileSync(outputfile, BOM+jsonStory);
const c = new Compiler(mainInk, options);
const rstory = c.Compile();
if (!rstory) {
process.stderr.write("*** Compilation failed ***\n");
process.exit(1);
}
let jsonified: string | void;

if((jsonified = rstory.ToJson())){
jsonStory = jsonified
}

if(jsonStory && write){
fs.writeFileSync(outputfile, BOM+jsonStory);
}
}else{
jsonStory = fs.readFileSync(inputFile,"utf-8").replace(BOM, "")
}

if(jsonStory && play){
Expand Down
2 changes: 1 addition & 1 deletion src/engine/ControlCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class ControlCommand extends InkObject {
return new ControlCommand(ControlCommand.CommandType.EndTag);
}
public toString() {
return this.commandType.toString();
return "ControlCommand " + this.commandType.toString();
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/engine/Void.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { InkObject } from "./Object";

export class Void extends InkObject {}
export class Void extends InkObject {
public toString() {
return "Void";
}
}
Loading