Skip to content

Commit

Permalink
Discourage direct Lua file modification (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
BytewaveMLP authored Dec 23, 2020
1 parent 290e264 commit acf877c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const renderSvg = require('./tools/resvg');
const minifyLua = require('./tools/luamin');
const compileMoonscript = require('./tools/moonscript');
const optimizeLua = require ('./tools/optimizations');
const discourageLuaMod = require ('./tools/discourageLuaModification');

const lastRunCache = new Map();
function lastRunIgnoreErrors(task) {
Expand Down Expand Up @@ -67,6 +68,7 @@ function moon() {
return gulp.src('src/**/*.moon', { since: lastRunIgnoreErrors(moon) })
.pipe(compileMoonscript())
.pipe(optimizeLua())
.pipe(discourageLuaMod())
// .pipe(minifyLua())
.pipe(gulp.dest('dest', { mode: 0777 }));
}
Expand Down
54 changes: 54 additions & 0 deletions tools/discourageLuaModification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { Transform } = require('stream');

const { streamToBuffer } = require('./util');

const LUA_FILE_PREFIX =
`-- !!! THIS FILE IS COMPILED !!!
-- This gamemode is written in Moonscript, and its source code is available
-- here: https://github.com/NotMyWing/GarrysModAmongUs
-- The code you see here is the result of compiled code, and is probably not
-- something you want to edit directly. Please consider editing the original
-- gamemode source off GitHub instead.`;

/**
* Discourage direct Lua file modification by notifying users where to find
* the uncompiled gamemode source code.
*/
class DiscourageLuaModificationTransform extends Transform {

constructor() {
super({ objectMode: true });
}

/**
* Transforms a file.
* @param {Object} file The file to process.
* @param {string} encoding The encoding of the file.
* @param {Function} next A callback function.
*/
_transform(file, encoding, next) {
if (file.isNull()) {
return next(null, file);
}

if (file.isStream()) {
streamToBuffer(file.contents, (err, contents) => {
if (err) this.emit('error', err);
else {
const code = contents.toString(encoding);
file.contents = Buffer.from(LUA_FILE_PREFIX + '\n\n' + code, encoding);
next(null, file);
}
});
}

if (file.isBuffer()) {
const code = file.contents.toString(encoding);
file.contents = Buffer.from(LUA_FILE_PREFIX + '\n\n' + code, encoding);
next(null, file);
}
}
}

module.exports = () => new DiscourageLuaModificationTransform();
module.exports.DiscourageLuaModificationTransform = DiscourageLuaModificationTransform;

0 comments on commit acf877c

Please sign in to comment.