Attach function objects to function names

This commit is contained in:
Jordan Eldredge 2019-06-16 12:39:55 -07:00
parent 2a17fa1a30
commit efbb5c6d48
3 changed files with 37 additions and 3 deletions

View file

@ -1,6 +1,6 @@
const { COMMANDS } = require("./constants");
const Command = require("./command");
const { getClass } = require("./objects");
const { getClass, getObjectFunction } = require("./objects");
const MAGIC = "FG";
const ENCODING = "binary";
@ -47,10 +47,12 @@ class Parser {
const typeOffset = classCode & 0xff;
const dummy2 = this._readUInt16LE();
const name = this._readString();
const klass = types[typeOffset];
functionNames.push({
dummy2,
name,
class: types[typeOffset]
class: klass,
function: getObjectFunction(klass, name)
});
}
return functionNames;

View file

@ -71,6 +71,7 @@ describe("standardframe.maki", () => {
"init"
]);
expect(maki.functionNames.every(func => func.class != null)).toBe(true);
expect(maki.functionNames.every(func => func.function != null)).toBe(true);
});
test("can read variables", () => {

View file

@ -3933,6 +3933,23 @@ Object.keys(objects).forEach(key => {
normalizedObjects[key.toLowerCase()] = objects[key];
});
const objectsByName = {};
Object.values(objects).forEach(object => {
objectsByName[object.name] = object;
});
Object.values(normalizedObjects).forEach(object => {
const parentClass = objectsByName[object.parent];
if (parentClass == null) {
if (object.parent === "@{00000000-0000-0000-0000-000000000000}@") {
} else {
console.log(`Could not find parent class named ${object.parent}`);
throw new Error("wat");
}
}
object.parentClass = parentClass;
});
function getClass(id) {
// https://en.wikipedia.org/wiki/Universally_unique_identifier#Encoding
const formattedId = id.replace(
@ -3943,4 +3960,18 @@ function getClass(id) {
return normalizedObjects[formattedId.toLowerCase()];
}
module.exports = { getClass };
function getObjectFunction(klass, functionName) {
const method = klass.functions.find(func => {
// TODO: This could probably be normalized at load time, or evern sooner.
return func.name.toLowerCase() === functionName.toLowerCase();
});
if (method != null) {
return method;
}
if (klass.parentClass == null) {
throw new Error(`Could not find method ${functionName} on ${klass.name}.`);
}
return getObjectFunction(klass.parentClass, functionName);
}
module.exports = { getClass, getObjectFunction };