Skip to content

Commit

Permalink
Fix image and sound related code to check if the referenced file exists
Browse files Browse the repository at this point in the history
Merge pull request #72 from haroldo-ok/check-file-existance
This fixes #67
  • Loading branch information
haroldo-ok committed Oct 26, 2022
2 parents ea8cbf9 + 086ce44 commit b4f01ea
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/test/src/vn_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void VN_init() {
XGM_setLoopNumber(-1);
XGM_setForceDelayDMA(TRUE);

VDP_drawText("choice4genesis v0.9.0", 18, 27);
VDP_drawText("choice4genesis v0.9.1", 18, 27);
}


Expand Down
21 changes: 17 additions & 4 deletions generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ const getStringConstant = (entity, parameter, context, name) => {
return parameter[1];
}

const getFileNameConstant = (entity, parameter, context, name) => {
const fileName = getStringConstant(entity, parameter, context, name);
if (!fileName) {
// Assumes `getStringConstant()` already generated the error message
return null;
}

if (!context.fileSystem.fileExistsInProjectDir(fileName)) {
context.errors.push(buildEntityError(entity, `${name} points to a missing file: "${fileName}".`));
}
return fileName;
}

const getNumber = (entity, parameter, context, name) => {
if (!parameter) {
context.errors.push(buildEntityError(entity, name + ' was not informed.'));
Expand Down Expand Up @@ -103,7 +116,7 @@ const addResource = (map, fileName, generator) => {
const generateResource = map => Object.values(map).map(({ content }) => content).join('\n');

const generateImageCommand = (functionName, entity, context, mapOption = 'ALL', generatedFlags='') => {
const imageFileName = getStringConstant(entity, entity.params.positional.fileName, context, 'Image filename');
const imageFileName = getFileNameConstant(entity, entity.params.positional.fileName, context, 'Image filename');
const imageVariable = addResource(context.res.gfx, imageFileName, imageVariable =>
`IMAGE ${imageVariable} "../project/${imageFileName}" APLIB ${mapOption}`);

Expand All @@ -130,15 +143,15 @@ const COMMAND_GENERATORS = {
'font': (entity, context) => generateImageCommand('VN_font', entity, context, 'NONE'),

'music': (entity, context) => {
const musicFileName = getStringConstant(entity, entity.params.positional.fileName, context, 'Music filename');
const musicFileName = getFileNameConstant(entity, entity.params.positional.fileName, context, 'Music filename');
const musicVariable = addResource(context.res.music, musicFileName, musicVariable =>
`XGM ${musicVariable} "../project/${musicFileName}" APLIB`);

return `VN_music(${musicVariable});`;
},

'sound': (entity, context) => {
const soundFileName = getStringConstant(entity, entity.params.positional.fileName, context, 'Sound filename');
const soundFileName = getFileNameConstant(entity, entity.params.positional.fileName, context, 'Sound filename');
const soundVariable = addResource(context.res.music, soundFileName, soundVariable =>
`WAV ${soundVariable} "../project/${soundFileName}" XGM`);

Expand Down Expand Up @@ -317,7 +330,7 @@ const COMMAND_GENERATORS = {
},

'cursor': (entity, context) => {
const imageFileName = getStringConstant(entity, entity.params.positional.fileName, context, 'Image filename');
const imageFileName = getFileNameConstant(entity, entity.params.positional.fileName, context, 'Image filename');
const width = getNumber(entity, entity.params.positional.width, context, 'Sprite width in tiles');
const height = getNumber(entity, entity.params.positional.height, context, 'Sprite height in tiles');
const frameDelay = getNumber(entity, entity.params.positional.frameDelay, context, 'Sprite animation delay');
Expand Down
4 changes: 4 additions & 0 deletions generator/transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const transpile = commandLine => {
const fileName = name + '.choice';
const source = readFileSync(projectFolder + 'project/' + fileName, {encoding:'utf8', flag:'r'});
return source;
},

fileExistsInProjectDir: fileName => {
return existsSync(projectFolder + 'project/' + fileName);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "choice4genesis",
"version": "0.9.0",
"version": "0.9.1",
"description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit b4f01ea

Please sign in to comment.