Skip to content

Commit

Permalink
work rmp converters into sgott
Browse files Browse the repository at this point in the history
  • Loading branch information
zeddidragon committed Nov 6, 2019
1 parent b3a9a91 commit bbadd46
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
13 changes: 1 addition & 12 deletions converters/rmp/from-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('fs')
const hexview = require('../../helpers/hexview')
const sgo = require('../sgo/from-json').compiler()
require('util').inspect.defaultOptions.depth = null

Expand All @@ -10,7 +9,6 @@ function abort() {

function compile(obj) {
const { endian } = obj

function Str(buffer, value, offset = 0x00, base = 0x00) {
return buffer.write(value, base + offset)
}
Expand Down Expand Up @@ -109,7 +107,6 @@ function compile(obj) {

function addCfg(cfg, offset, base) {
const buf = sgo(cfg)
console.log(hexview(buf), '\n')
buffers.push(buf)
heapSize += buf.length
Int(buffer, heapIdx(base), offset, base)
Expand Down Expand Up @@ -210,12 +207,4 @@ function compile(obj) {
return Buffer.concat(buffers)
}

const json = JSON.parse(fs.readFileSync('tmp/m190mission.json', 'utf8'))
const result = compile(json)
const slice = 0x180
process.exit(0)
console.log('\nReconstructed')
console.log(hexview(result.slice(0, slice)))
const buffer = fs.readFileSync('testdata/M190/MISSION.RMPA')
console.log('\nReal')
console.log(hexview(buffer.slice(0, slice)))
exports.compile = compile
9 changes: 4 additions & 5 deletions converters/rmp/to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const json = require('json-stringify-pretty-compact')
const hexview = require('../../helpers/hexview')
const sgo = require('../sgo/to-json').decompiler
require('util').inspect.defaultOptions.depth = null

// Cheapo(tm) debugging
function abort() {
throw new Error('abort')
Expand Down Expand Up @@ -101,7 +100,7 @@ function decompiler(config = {}) {
return StructDef
}

function Leader() {
function Leader(buffer) {
const leader = buffer.slice(0, 4).toString('ascii')
endian = leader === 'RMP\0' ? 'LE' : 'BE'
return leader
Expand Down Expand Up @@ -345,6 +344,6 @@ function decompile(buffer, opts = {}) {
return json(data)
}

const buffer = fs.readFileSync('testdata/M190/MISSION.RMPA')
const result = decompile(buffer)
console.log(result)
decompile.decompiler = decompiler

module.exports = decompile
75 changes: 57 additions & 18 deletions sgott.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@ const fs = require('fs')
const config = require('./package.json')
const sgoToJson = require('./converters/sgo/to-json')
const jsonToSgo = require('./converters/sgo/from-json')
const rmpToJson = require('./converters/rmp/to-json')
const jsonToRmp = require('./converters/rmp/from-json')

function isSgo(obj) {
if(/^sgo$/i.test(obj.format)) return true
if(obj.variables) return true
return false
}

function isRmp(obj) {
if(/^rmp/i.test(obj.format)) return true
if(obj.routes) return true
if(obj.shapes) return true
if(obj.cameras) return true
if(obj.spawns) return true
return false
}

const transforms = {
sgo: sgoToJson,
json: jsonToSgo,
rmp: rmpToJson,
json(buffer, opts) {
const parsed = JSON.parse(buffer.toString())
if(isSgo(parsed)) return jsonToSgo.compiler(opts)(parsed)
if(isRmp(parsed)) return jsonToRmp.compile(parsed, opts)
throw new Error('Unable to recognize JSON format')
},
}

const flagMap = {
Expand All @@ -31,7 +54,7 @@ Usage:
Options:
-t --type
Can be "json" or "sgo". Override automatically inferred input type.
Can be "json", "sgo", or "rmp". Override automatically inferred input type.
-h --help
Prints this help text, then quits.
Expand Down Expand Up @@ -86,53 +109,69 @@ function parseCli(cb) {

const [readFile, writeFile] = plain

function convertFileName(fileName) {
function convertFileName(fileName, target) {
const format = /\.json$/.exec(fileName) ? 'SGO' : 'json'
return fileName.replace(/\..*$/, '.' + format)
return fileName.replace(/\..*$/, '.' + target)
}

function write(data) {
function write(data, type) {
const target = type === 'json'
? (data.format || 'sgo').toUpperCase()
: 'json'
if(writeFile && fs.existsSync(writeFile) && fs.lstatSync(writeFile).isDirectory()) {
const path = writeFile + '/' + convertFileName(readFile.split('/').pop())
const path = writeFile + '/' + convertFileName(readFile.split('/').pop(), target)
fs.writeFileSync(path, data)
console.log(path)
} else if(writeFile) {
fs.writeFileSync(writeFile, data)
console.log(writeFile)
} else if(readFile) {
fs.writeFileSync(convertFileName(readFile), data)
const path = convertFileName(readFile, target)
fs.writeFileSync(path, data)
console.log(path)
} else {
process.stdout.write(data)
}
}

var type = (opts.t || opts.type || '').toLowerCase()
function inferType(buffer) {
const ext4 = readFile && readFile.slice(-4)
if(ext4 === '.sgo') return 'sgo'
if(ext4 === '.rmp') return 'rmp'

const ext5 = readFile && readFile.slice(-5)
if(ext5 === '.json') return 'json'
if(ext5 === '.rmpa') return 'rmp'

const leader = buffer.slice(0, 4).toString()
if(leader === 'SGO\0' || leader === '\0OGS') return 'sgo'
if(leader === 'RMP\0' || leader === '\0PMR') return 'rmp'
if(leader.replace(/\u0000/g, '').trim()[0] === '{') return 'json'

throw new Error('Unable to infer format')
}

const type = (opts.t || opts.type || '').toLowerCase()
if(opts.version) {
console.log(config.name, config.version)
console.log(config.description)
} else if(opts.help) {
console.log(help)
} else if(readFile) {
if(!type) type = (readFile.slice(-5).toLowerCase() === '.json'
? 'json'
: 'sgo'
)
const buffer = fs.readFileSync(readFile)
cb(buffer, type, opts, write)
cb(buffer, type || inferType(buffer), opts, write)
} else {
const chunks = []
process.stdin.on('data', chunk => chunks.push(chunk))
process.stdin.on('end', () => {
const buffer = Buffer.concat(chunks)
if(!type) type = buffer.slice(0, 1).toString() === '{'
? 'json'
: 'sgo'
cb(Buffer.concat(chunks), type, opts, write)
cb(Buffer.concat(chunks), type || inferType(buffer), opts, write)
})
}
}

function handle(buffer, type, opts, write) {
write(transforms[type](buffer, opts))
write(transforms[type](buffer, opts), type)
}

parseCli(handle)

0 comments on commit bbadd46

Please sign in to comment.