calling functions from a scene file
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit
This commit is contained in:
parent
0c554d1598
commit
f2877edf37
@ -1,3 +1,4 @@
|
|||||||
|
import { TrackedScene } from "/Scripts/engine/scene/scene-loader";
|
||||||
import { Scene } from "/Scripts/types/scene";
|
import { Scene } from "/Scripts/types/scene";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -5,6 +6,10 @@ import { Scene } from "/Scripts/types/scene";
|
|||||||
*/
|
*/
|
||||||
const TestScene1: Scene = {
|
const TestScene1: Scene = {
|
||||||
|
|
||||||
|
onCreate: (instanceId: number) => {
|
||||||
|
console.log('Hello from the scene! My ID is ' + instanceId)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The compiler object
|
* @description The compiler object
|
||||||
*/
|
*/
|
||||||
let COMPILER = {
|
let COMPILER = {
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ let COMPILER = {
|
|||||||
* The top level directory, "/"
|
* The top level directory, "/"
|
||||||
*/
|
*/
|
||||||
topLevelDirectory: {
|
topLevelDirectory: {
|
||||||
//as reqiored by our framework
|
//as required by our framework
|
||||||
Scripts: {
|
Scripts: {
|
||||||
compiler: {
|
compiler: {
|
||||||
"host_access.js": {
|
"host_access.js": {
|
||||||
@ -65,218 +64,9 @@ let COMPILER = {
|
|||||||
* Registers a file with the compiler
|
* Registers a file with the compiler
|
||||||
* @param {string} fileName The file's name
|
* @param {string} fileName The file's name
|
||||||
* @param {string} content The content of the file
|
* @param {string} content The content of the file
|
||||||
* @returns The list of all files that still need to be registered by the host
|
* @returns {string[]} The list of all files that still need to be registered by the host
|
||||||
*/
|
*/
|
||||||
registerFile: (fileName, content) => [],
|
registerFile: (fileName, content) => {
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a file object for a given path
|
|
||||||
* @param string} fileName The name of the file
|
|
||||||
* @param {string} content The content of the file
|
|
||||||
* @returns The file object
|
|
||||||
*/
|
|
||||||
createFile: (fileName, content) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the path for the file
|
|
||||||
* @param {string} fullyQualifiedFilePath The fully qualified file path
|
|
||||||
* @returns {string[]} The array of directories ending with the name of the file
|
|
||||||
*/
|
|
||||||
getPath: (fullyQualifiedFilePath) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the path for the file
|
|
||||||
* @param {stringp[]} filePathArray The fully qualified file path
|
|
||||||
* @returns The array of directories ending with the name of the file
|
|
||||||
*/
|
|
||||||
getFileByPath: (filePathArray) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a file exists
|
|
||||||
* @param {string[]} filePathArray The file path array
|
|
||||||
* @returns true if it exists, false otherwise
|
|
||||||
*/
|
|
||||||
fileExists: (filePathArray) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The callback invoked when the compiler host tries to read a file
|
|
||||||
* @param {string} fileName The name of the file
|
|
||||||
* @param {*} languageVersion The language version
|
|
||||||
* @returns The file if it exists, null otherwise
|
|
||||||
*/
|
|
||||||
getSourceFile: (fileName, languageVersion) => null,
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// COMPILATION
|
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The compiler options
|
|
||||||
*/
|
|
||||||
compilerOptions: { },
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tracks whether the compiler has run or not
|
|
||||||
*/
|
|
||||||
compilerHasRun: false,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emits a file
|
|
||||||
* @param {string} fileName The name of the file
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
emitFile: (fileName) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs errors raised during emission of files
|
|
||||||
* @param {string} fileName The name of the file to log errors about
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
logEmitError: (fileName) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instructs Typescript to emit the final compiled value
|
|
||||||
*/
|
|
||||||
run: () => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a file
|
|
||||||
* @param {*} fileName The name of the file to load (preferably already has .ts at the end)
|
|
||||||
*/
|
|
||||||
runFile: (fileName) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a file
|
|
||||||
* @param {*} fileName The name of the file to load (preferably already has .ts at the end)
|
|
||||||
*/
|
|
||||||
printSource: (fileName) => null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The typescript compiler host definition
|
|
||||||
*/
|
|
||||||
customCompilerHost: null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The typescript program
|
|
||||||
*/
|
|
||||||
program: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the path for the file
|
|
||||||
* @param {*} fullyQualifiedFilePath The fully qualified file path
|
|
||||||
* @returns The array of directories ending with the name of the file
|
|
||||||
*/
|
|
||||||
COMPILER.getPath = (fullyQualifiedFilePath) => {
|
|
||||||
let modifiedFileName = fullyQualifiedFilePath
|
|
||||||
//remove leading "/"
|
|
||||||
if(modifiedFileName.startsWith("/")){
|
|
||||||
modifiedFileName = modifiedFileName.substring(1)
|
|
||||||
}
|
|
||||||
//split
|
|
||||||
return modifiedFileName.split("/")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a file based on its path array
|
|
||||||
* @param {*} filePathArray The file path array
|
|
||||||
* @returns The file
|
|
||||||
*/
|
|
||||||
COMPILER.getFileByPath = (filePathArray) => {
|
|
||||||
let currentFolder = COMPILER.topLevelDirectory
|
|
||||||
let mutableArray = filePathArray
|
|
||||||
|
|
||||||
//illegal state
|
|
||||||
if(mutableArray?.length < 1){
|
|
||||||
throw new Error("Trying to get a file with a path array of length 0!")
|
|
||||||
}
|
|
||||||
|
|
||||||
while(mutableArray?.length > 1){
|
|
||||||
let nextDirName = mutableArray.shift()
|
|
||||||
currentFolder = currentFolder?.[nextDirName]
|
|
||||||
if(!currentFolder){
|
|
||||||
let errorMessage = "Trying to get file in directory that doesn't exist! \n" +
|
|
||||||
nextDirName
|
|
||||||
throw new Error(errorMessage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return currentFolder[mutableArray?.[0]]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a file exists
|
|
||||||
* @param {*} filePathArray The file path array
|
|
||||||
* @returns true if it exists, false otherwise
|
|
||||||
*/
|
|
||||||
COMPILER.fileExists = (filePathArray) => {
|
|
||||||
let currentFolder = COMPILER.topLevelDirectory
|
|
||||||
let mutableArray = filePathArray
|
|
||||||
|
|
||||||
//illegal state
|
|
||||||
if(mutableArray?.length < 1){
|
|
||||||
throw new Error("Trying to get a file with a path array of length 0!")
|
|
||||||
}
|
|
||||||
|
|
||||||
while(mutableArray.length > 1){
|
|
||||||
let nextDirName = mutableArray.shift()
|
|
||||||
currentFolder = currentFolder?.[nextDirName]
|
|
||||||
if(!currentFolder){
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return !!currentFolder?.[mutableArray[0]]
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a file object for a given path
|
|
||||||
* @param {*} fileName The name of the file
|
|
||||||
* @param {*} content The content of the file
|
|
||||||
* @returns The file object
|
|
||||||
*/
|
|
||||||
COMPILER.createFile = (fileName, content) => {
|
|
||||||
//get the file path array
|
|
||||||
const filePathArray = COMPILER.getPath(fileName)
|
|
||||||
let mutableArray = filePathArray
|
|
||||||
|
|
||||||
//the current folder as we recursively create folders to populate this file
|
|
||||||
let currentFolder = COMPILER.topLevelDirectory
|
|
||||||
|
|
||||||
//recursively create directories until our file is written
|
|
||||||
while(mutableArray.length > 1){
|
|
||||||
let nextDirName = mutableArray.shift()
|
|
||||||
if(!currentFolder?.[nextDirName]){
|
|
||||||
//create directory
|
|
||||||
currentFolder[nextDirName] = {
|
|
||||||
isDir: true,
|
|
||||||
"..": currentFolder,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
currentFolder = currentFolder?.[nextDirName]
|
|
||||||
}
|
|
||||||
|
|
||||||
//create the actual file
|
|
||||||
currentFolder[mutableArray[0]] = {
|
|
||||||
isDir: false,
|
|
||||||
dir: currentFolder,
|
|
||||||
content: content,
|
|
||||||
version: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
//return the file
|
|
||||||
return currentFolder[mutableArray[0]]
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers a file with the compiler
|
|
||||||
* @param {*} fileName The file's name
|
|
||||||
* @param {*} content The content of the file
|
|
||||||
* @returns The list of all files that still need to be registered by the host
|
|
||||||
*/
|
|
||||||
COMPILER.registerFile = (fileName, content) => {
|
|
||||||
|
|
||||||
//the list of files that are imported by this file
|
//the list of files that are imported by this file
|
||||||
let dependentFiles = []
|
let dependentFiles = []
|
||||||
@ -354,21 +144,255 @@ COMPILER.registerFile = (fileName, content) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dependentFiles;
|
return dependentFiles;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a file object for a given path
|
||||||
|
* @param string} fileName The name of the file
|
||||||
|
* @param {string} content The content of the file
|
||||||
|
* @returns The file object
|
||||||
|
*/
|
||||||
|
createFile: (fileName, content) => {
|
||||||
|
//get the file path array
|
||||||
|
const filePathArray = COMPILER.getPath(fileName)
|
||||||
|
let mutableArray = filePathArray
|
||||||
|
|
||||||
|
//the current folder as we recursively create folders to populate this file
|
||||||
|
let currentFolder = COMPILER.topLevelDirectory
|
||||||
|
|
||||||
|
//recursively create directories until our file is written
|
||||||
|
while(mutableArray.length > 1){
|
||||||
|
let nextDirName = mutableArray.shift()
|
||||||
|
if(!currentFolder?.[nextDirName]){
|
||||||
|
//create directory
|
||||||
|
currentFolder[nextDirName] = {
|
||||||
|
isDir: true,
|
||||||
|
"..": currentFolder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentFolder = currentFolder?.[nextDirName]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//create the actual file
|
||||||
|
currentFolder[mutableArray[0]] = {
|
||||||
|
isDir: false,
|
||||||
|
dir: currentFolder,
|
||||||
|
content: content,
|
||||||
|
version: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
//return the file
|
||||||
|
return currentFolder[mutableArray[0]]
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the path for the file
|
||||||
|
* @param {string} fullyQualifiedFilePath The fully qualified file path
|
||||||
|
* @returns {string[]} The array of directories ending with the name of the file
|
||||||
|
*/
|
||||||
|
getPath: (fullyQualifiedFilePath) => {
|
||||||
|
let modifiedFileName = fullyQualifiedFilePath
|
||||||
|
//remove leading "/"
|
||||||
|
if(modifiedFileName.startsWith("/")){
|
||||||
|
modifiedFileName = modifiedFileName.substring(1)
|
||||||
|
}
|
||||||
|
//split
|
||||||
|
return modifiedFileName.split("/")
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the path for the file
|
||||||
|
* @param {stringp[]} filePathArray The fully qualified file path
|
||||||
|
* @returns The array of directories ending with the name of the file
|
||||||
|
*/
|
||||||
|
getFileByPath: (filePathArray) => {
|
||||||
|
let currentFolder = COMPILER.topLevelDirectory
|
||||||
|
let mutableArray = filePathArray
|
||||||
|
|
||||||
|
//illegal state
|
||||||
|
if(mutableArray?.length < 1){
|
||||||
|
throw new Error("Trying to get a file with a path array of length 0!")
|
||||||
|
}
|
||||||
|
|
||||||
|
while(mutableArray?.length > 1){
|
||||||
|
let nextDirName = mutableArray.shift()
|
||||||
|
currentFolder = currentFolder?.[nextDirName]
|
||||||
|
if(!currentFolder){
|
||||||
|
let errorMessage = "Trying to get file in directory that doesn't exist! \n" +
|
||||||
|
nextDirName
|
||||||
|
throw new Error(errorMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentFolder[mutableArray?.[0]]
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a file exists
|
||||||
|
* @param {string[]} filePathArray The file path array
|
||||||
|
* @returns true if it exists, false otherwise
|
||||||
|
*/
|
||||||
|
fileExists: (filePathArray) => {
|
||||||
|
let currentFolder = COMPILER.topLevelDirectory
|
||||||
|
let mutableArray = filePathArray
|
||||||
|
|
||||||
|
//illegal state
|
||||||
|
if(mutableArray?.length < 1){
|
||||||
|
throw new Error("Trying to get a file with a path array of length 0!")
|
||||||
|
}
|
||||||
|
|
||||||
|
while(mutableArray.length > 1){
|
||||||
|
let nextDirName = mutableArray.shift()
|
||||||
|
currentFolder = currentFolder?.[nextDirName]
|
||||||
|
if(!currentFolder){
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !!currentFolder?.[mutableArray[0]]
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The callback invoked when the compiler host tries to read a file
|
* The callback invoked when the compiler host tries to read a file
|
||||||
* @param {*} fileName The name of the file
|
* @param {string} fileName The name of the file
|
||||||
* @param {*} languageVersion The language version
|
* @param {*} languageVersion The language version
|
||||||
* @returns The file if it exists, null otherwise
|
* @returns The file if it exists, null otherwise
|
||||||
*/
|
*/
|
||||||
COMPILER.getSourceFile = (fileName, languageVersion) => {
|
getSourceFile: (fileName, languageVersion) => {
|
||||||
if(!!COMPILER.fileMap[fileName]){
|
if(!!COMPILER.fileMap[fileName]){
|
||||||
return COMPILER.fileMap[fileName].tsSourceFile
|
return COMPILER.fileMap[fileName].tsSourceFile
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// COMPILATION
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The compiler options
|
||||||
|
*/
|
||||||
|
compilerOptions: { },
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tracks whether the compiler has run or not
|
||||||
|
*/
|
||||||
|
compilerHasRun: false,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The typescript compiler host definition
|
||||||
|
*/
|
||||||
|
customCompilerHost: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The typescript program
|
||||||
|
*/
|
||||||
|
program: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emits a file
|
||||||
|
* @param {string} fileName The name of the file
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
emitFile: (fileName) => {
|
||||||
|
loggerScripts.DEBUG('Compiler evaluating source path ' + fileName)
|
||||||
|
/**
|
||||||
|
* {
|
||||||
|
* outputFiles: [ ],
|
||||||
|
* emitSkipped: boolean,
|
||||||
|
* diagnostics: { },
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
const output = COMPILER.program.getEmitOutput(fileName)
|
||||||
|
if (!output.emitSkipped) {
|
||||||
|
output.outputFiles.forEach(outputFile => {
|
||||||
|
loggerScripts.DEBUG(`[ts] Emitting ${outputFile}`);
|
||||||
|
COMPILER.customCompilerHost.writeFile(outputFile.name, outputFile.text)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
loggerScripts.DEBUG(`[ts] Emitting ${fileName} failed`);
|
||||||
|
COMPILER.logEmitError(fileName);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs errors raised during emission of files
|
||||||
|
* @param {string} fileName The name of the file to log errors about
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
logEmitError: (fileName) => {
|
||||||
|
loggerScripts.DEBUG('[ts] logErrors ' + fileName)
|
||||||
|
let allDiagnostics = services
|
||||||
|
.getCompilerOptionsDiagnostics()
|
||||||
|
.concat(services.getSyntacticDiagnostics(fileName))
|
||||||
|
.concat(services.getSemanticDiagnostics(fileName));
|
||||||
|
|
||||||
|
allDiagnostics.forEach(diagnostic => {
|
||||||
|
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
||||||
|
if (diagnostic.file) {
|
||||||
|
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
|
||||||
|
diagnostic.start
|
||||||
|
);
|
||||||
|
loggerScripts.DEBUG(`[ts] Error ${diagnostic.file.fileName} (${line + 1},${character +1}): ${message}`);
|
||||||
|
} else {
|
||||||
|
loggerScripts.DEBUG(`[ts] Error: ${message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instructs Typescript to emit the final compiled value
|
||||||
|
*/
|
||||||
|
run: () => {
|
||||||
|
loggerScripts.INFO('COMPILE ALL REGISTERED FILES')
|
||||||
|
|
||||||
|
if(!COMPILER.program){
|
||||||
|
COMPILER.program = ts.createLanguageService(COMPILER.customCompilerHost, ts.createDocumentRegistry());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Emit all currently known files
|
||||||
|
COMPILER.sourceFiles.forEach(sourcePath => {
|
||||||
|
COMPILER.emitFile(sourcePath)
|
||||||
|
})
|
||||||
|
|
||||||
|
//flag that the compiler has run (ie only incrementally compile when new files are added, now)
|
||||||
|
COMPILER.compilerHasRun = true
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a file
|
||||||
|
* @param {*} fileName The name of the file to load (preferably already has .ts at the end)
|
||||||
|
*/
|
||||||
|
runFile: (fileName) => {
|
||||||
|
let normalizedFilePath = FILE_RESOLUTION_getFilePath(fileName)
|
||||||
|
if(!!COMPILER.fileMap[normalizedFilePath]){
|
||||||
|
loggerScripts.INFO('RUN FILE ' + normalizedFilePath)
|
||||||
|
eval(COMPILER.fileMap[normalizedFilePath].content)
|
||||||
|
} else {
|
||||||
|
const message = 'FAILED TO RESOLVE FILE ' + normalizedFilePath
|
||||||
|
loggerScripts.WARNING(message)
|
||||||
|
throw new Error(message)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a file
|
||||||
|
* @param {*} fileName The name of the file to load (preferably already has .ts at the end)
|
||||||
|
*/
|
||||||
|
printSource: (fileName) => {
|
||||||
|
let normalizedFilePath = FILE_RESOLUTION_getFilePath(fileName)
|
||||||
|
if(!!COMPILER.fileMap[normalizedFilePath]){
|
||||||
|
loggerScripts.INFO('FILE CONTENT ' + normalizedFilePath)
|
||||||
|
} else {
|
||||||
|
const message = 'FAILED TO RESOLVE FILE ' + normalizedFilePath
|
||||||
|
loggerScripts.WARNING(message)
|
||||||
|
loggerScripts.WARNING('file map content:')
|
||||||
|
loggerScripts.WARNING(OBject.keys(COMPILER.fileMap) + "")
|
||||||
|
throw new Error(message)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -449,110 +473,6 @@ COMPILER.customCompilerHost = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
getCompilationSettings: () => COMPILER.compilerOptions,
|
getCompilationSettings: () => COMPILER.compilerOptions,
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emits a file
|
|
||||||
* @param {string} fileName The name of the file
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
COMPILER.emitFile = (fileName) => {
|
|
||||||
loggerScripts.DEBUG('Compiler evaluating source path ' + fileName)
|
|
||||||
/**
|
|
||||||
* {
|
|
||||||
* outputFiles: [ ],
|
|
||||||
* emitSkipped: boolean,
|
|
||||||
* diagnostics: { },
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
const output = COMPILER.program.getEmitOutput(fileName)
|
|
||||||
if (!output.emitSkipped) {
|
|
||||||
output.outputFiles.forEach(outputFile => {
|
|
||||||
loggerScripts.DEBUG(`[ts] Emitting ${outputFile}`);
|
|
||||||
COMPILER.customCompilerHost.writeFile(outputFile.name, outputFile.text)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
loggerScripts.DEBUG(`[ts] Emitting ${fileName} failed`);
|
|
||||||
COMPILER.logEmitError(fileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs errors raised during emission of files
|
|
||||||
* @param {string} fileName The name of the file to log errors about
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
const logEmitError = (fileName) => {
|
|
||||||
loggerScripts.DEBUG('[ts] logErrors ' + fileName)
|
|
||||||
let allDiagnostics = services
|
|
||||||
.getCompilerOptionsDiagnostics()
|
|
||||||
.concat(services.getSyntacticDiagnostics(fileName))
|
|
||||||
.concat(services.getSemanticDiagnostics(fileName));
|
|
||||||
|
|
||||||
allDiagnostics.forEach(diagnostic => {
|
|
||||||
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
|
||||||
if (diagnostic.file) {
|
|
||||||
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
|
|
||||||
diagnostic.start
|
|
||||||
);
|
|
||||||
loggerScripts.DEBUG(`[ts] Error ${diagnostic.file.fileName} (${line + 1},${character +1}): ${message}`);
|
|
||||||
} else {
|
|
||||||
loggerScripts.DEBUG(`[ts] Error: ${message}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instructs Typescript to emit the final compiled value
|
|
||||||
*/
|
|
||||||
COMPILER.run = () => {
|
|
||||||
loggerScripts.INFO('COMPILE ALL REGISTERED FILES')
|
|
||||||
|
|
||||||
if(!COMPILER.program){
|
|
||||||
COMPILER.program = ts.createLanguageService(COMPILER.customCompilerHost, ts.createDocumentRegistry());
|
|
||||||
}
|
|
||||||
|
|
||||||
//Emit all currently known files
|
|
||||||
COMPILER.sourceFiles.forEach(sourcePath => {
|
|
||||||
COMPILER.emitFile(sourcePath)
|
|
||||||
})
|
|
||||||
|
|
||||||
//flag that the compiler has run (ie only incrementally compile when new files are added, now)
|
|
||||||
COMPILER.compilerHasRun = true
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a file
|
|
||||||
* @param {*} fileName The name of the file to load (preferably already has .ts at the end)
|
|
||||||
*/
|
|
||||||
COMPILER.runFile = (fileName) => {
|
|
||||||
let normalizedFilePath = FILE_RESOLUTION_getFilePath(fileName)
|
|
||||||
if(!!COMPILER.fileMap[normalizedFilePath]){
|
|
||||||
loggerScripts.INFO('RUN FILE ' + normalizedFilePath)
|
|
||||||
eval(COMPILER.fileMap[normalizedFilePath].content)
|
|
||||||
} else {
|
|
||||||
const message = 'FAILED TO RESOLVE FILE ' + normalizedFilePath
|
|
||||||
loggerScripts.WARNING(message)
|
|
||||||
throw new Error(message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a file
|
|
||||||
* @param {*} fileName The name of the file to load (preferably already has .ts at the end)
|
|
||||||
*/
|
|
||||||
COMPILER.printSource = (fileName) => {
|
|
||||||
let normalizedFilePath = FILE_RESOLUTION_getFilePath(fileName)
|
|
||||||
if(!!COMPILER.fileMap[normalizedFilePath]){
|
|
||||||
loggerScripts.INFO('FILE CONTENT ' + normalizedFilePath)
|
|
||||||
} else {
|
|
||||||
const message = 'FAILED TO RESOLVE FILE ' + normalizedFilePath
|
|
||||||
loggerScripts.WARNING(message)
|
|
||||||
loggerScripts.WARNING('file map content:')
|
|
||||||
loggerScripts.WARNING(OBject.keys(COMPILER.fileMap) + "")
|
|
||||||
throw new Error(message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//initialized CWD
|
//initialized CWD
|
||||||
|
|||||||
@ -57,3 +57,15 @@ const require = (path) => {
|
|||||||
throw new Error(errorMsg)
|
throw new Error(errorMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Add require to its own cache
|
||||||
|
REQUIRE_CACHE["/Scripts/compiler/require_polyfill.js"] = {
|
||||||
|
exports: {
|
||||||
|
'require': require,
|
||||||
|
'exports': exports,
|
||||||
|
},
|
||||||
|
exportedValues: [
|
||||||
|
'require',
|
||||||
|
'exports',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@ export const ENGINE_onInit = () => {
|
|||||||
|
|
||||||
//load namespaces
|
//load namespaces
|
||||||
let client: NamespaceClient = Client
|
let client: NamespaceClient = Client
|
||||||
|
engine.sceneLoader.hookManager = engine.hookManager
|
||||||
|
|
||||||
loggerScripts.INFO('Script Engine Initialized')
|
loggerScripts.INFO('Script Engine Initialized')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -72,7 +72,7 @@ export class HookManager {
|
|||||||
//Scene related structures
|
//Scene related structures
|
||||||
//
|
//
|
||||||
//track scene if it isn't already being tracked
|
//track scene if it isn't already being tracked
|
||||||
let filteredArr = this.trackedScenes.filter(trackedScene => trackedScene.sceneId === scene.sceneId)
|
let filteredArr = this.trackedScenes.filter(trackedScene => trackedScene.instanceId === scene.instanceId)
|
||||||
if(filteredArr.length < 1){
|
if(filteredArr.length < 1){
|
||||||
this.trackedScenes.push(scene)
|
this.trackedScenes.push(scene)
|
||||||
}
|
}
|
||||||
@ -97,7 +97,7 @@ export class HookManager {
|
|||||||
* @param signal The signal
|
* @param signal The signal
|
||||||
* @param value The value associated with the signal
|
* @param value The value associated with the signal
|
||||||
*/
|
*/
|
||||||
fireSignal(sceneId: number, signal: string, value: any){
|
fireSignal(instanceId: number, signal: string, value: any){
|
||||||
const hooks: Array<TrackedHook> = this.signalHookMap[signal]
|
const hooks: Array<TrackedHook> = this.signalHookMap[signal]
|
||||||
hooks.forEach(trackedHook => {
|
hooks.forEach(trackedHook => {
|
||||||
trackedHook.callback(value)
|
trackedHook.callback(value)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ export interface TrackedScene {
|
|||||||
/**
|
/**
|
||||||
* The id assigned to the scene
|
* The id assigned to the scene
|
||||||
*/
|
*/
|
||||||
sceneId: number,
|
instanceId: number,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The scene itself
|
* The scene itself
|
||||||
@ -47,42 +47,60 @@ export class SceneLoader {
|
|||||||
/**
|
/**
|
||||||
* The list of loaded scenes
|
* The list of loaded scenes
|
||||||
*/
|
*/
|
||||||
loadedScenes: TrackedScene[]
|
loadedScenes: TrackedScene[] = [ ]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A record of tracked scene id to tracked scene object
|
* A record of tracked scene id to tracked scene object
|
||||||
*/
|
*/
|
||||||
sceneIdMap: Record<number,TrackedScene>
|
sceneIdMap: Record<number,TrackedScene> = { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A scene
|
* A scene
|
||||||
*/
|
*/
|
||||||
sceneIdIncrementer: number = 0
|
sceneIdIncrementer: number = 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a scene
|
||||||
|
* @param sceneName The scene to load
|
||||||
|
* @returns The id assigned to the instance of the scene
|
||||||
|
*/
|
||||||
|
loadScene(sceneName: string): number {
|
||||||
|
//@ts-ignore
|
||||||
|
const scene: Scene = require(sceneName).default
|
||||||
|
|
||||||
|
//creates an instance of the scene
|
||||||
|
let sceneInstanceId: number = this.createInstance(scene,true)
|
||||||
|
|
||||||
|
//call on init for scene
|
||||||
|
if(scene.onCreate){
|
||||||
|
scene.onCreate(sceneInstanceId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sceneInstanceId
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers all hooks in a scene to the hook manager
|
* Registers all hooks in a scene to the hook manager
|
||||||
* @param scene The scene
|
* @param scene The scene
|
||||||
|
* @returns The id assigned to the instance of the scene
|
||||||
*/
|
*/
|
||||||
registerScene(scene: Scene, isServerScene: boolean){
|
createInstance(scene: Scene, isServerScene: boolean): number{
|
||||||
const shouldRegister: boolean = !this.containsScene(scene)
|
|
||||||
if(shouldRegister){
|
|
||||||
|
|
||||||
//add to the list of tracked scenes
|
//add to the list of tracked scenes
|
||||||
const trackedScene: TrackedScene = {
|
const trackedScene: TrackedScene = {
|
||||||
sceneId: this.sceneIdIncrementer++,
|
instanceId: this.sceneIdIncrementer++,
|
||||||
scene: scene,
|
scene: scene,
|
||||||
sceneHooks: [],
|
sceneHooks: [],
|
||||||
signalHookMap: { },
|
signalHookMap: { },
|
||||||
}
|
}
|
||||||
this.loadedScenes.push(trackedScene)
|
this.loadedScenes.push(trackedScene)
|
||||||
this.sceneIdMap[trackedScene.sceneId] = trackedScene
|
this.sceneIdMap[trackedScene.instanceId] = trackedScene
|
||||||
|
|
||||||
//load all hooks from the scene
|
//load all hooks from the scene
|
||||||
scene.hooks.forEach((hook: Hook) => {
|
scene?.hooks?.forEach((hook: Hook) => {
|
||||||
engine.hookManager.registerHook(trackedScene,hook,isServerScene)
|
engine.hookManager.registerHook(trackedScene,hook,isServerScene)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
return trackedScene.instanceId
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import { TrackedScene } from "/Scripts/engine/scene/scene-loader";
|
||||||
import { Hook } from "/Scripts/types/hook";
|
import { Hook } from "/Scripts/types/hook";
|
||||||
import { Namespace } from "/Scripts/types/namespace";
|
import { Namespace } from "/Scripts/types/namespace";
|
||||||
|
|
||||||
@ -26,4 +27,9 @@ export interface Scene extends Namespace {
|
|||||||
*/
|
*/
|
||||||
hooks?: Array<Hook>
|
hooks?: Array<Hook>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when the scene is created
|
||||||
|
*/
|
||||||
|
onCreate?: (instanceId: number) => void
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,7 +35,7 @@ public class LoggerInterface {
|
|||||||
loggerDB = new Logger(LogLevel.WARNING);
|
loggerDB = new Logger(LogLevel.WARNING);
|
||||||
loggerAudio = new Logger(LogLevel.WARNING);
|
loggerAudio = new Logger(LogLevel.WARNING);
|
||||||
loggerUI = new Logger(LogLevel.WARNING);
|
loggerUI = new Logger(LogLevel.WARNING);
|
||||||
loggerScripts = new Logger(LogLevel.DEBUG);
|
loggerScripts = new Logger(LogLevel.WARNING);
|
||||||
loggerStartup.INFO("Initialized loggers");
|
loggerStartup.INFO("Initialized loggers");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,6 +42,9 @@ public class ScriptEngine {
|
|||||||
//the object that contains all host values accessible to javascript land
|
//the object that contains all host values accessible to javascript land
|
||||||
Value hostObject;
|
Value hostObject;
|
||||||
|
|
||||||
|
//the engine object
|
||||||
|
Value engineObject;
|
||||||
|
|
||||||
//The files that are loaded on init to bootstrap the script engine
|
//The files that are loaded on init to bootstrap the script engine
|
||||||
static final String[] filesToLoadOnInit = new String[]{
|
static final String[] filesToLoadOnInit = new String[]{
|
||||||
//polyfills
|
//polyfills
|
||||||
@ -104,6 +107,8 @@ public class ScriptEngine {
|
|||||||
|
|
||||||
//run script for engine init
|
//run script for engine init
|
||||||
requireModule("/Scripts/engine/engine-init.ts");
|
requireModule("/Scripts/engine/engine-init.ts");
|
||||||
|
//get the engine object
|
||||||
|
engineObject = topLevelValue.getMember("REQUIRE_CACHE").getMember("/Scripts/engine/engine-init.js").getMember("exports").getMember("engine");
|
||||||
invokeModuleFunction("/Scripts/engine/engine-init.ts","ENGINE_onInit");
|
invokeModuleFunction("/Scripts/engine/engine-init.ts","ENGINE_onInit");
|
||||||
|
|
||||||
|
|
||||||
@ -247,8 +252,10 @@ public class ScriptEngine {
|
|||||||
public void initScene(String scenePath){
|
public void initScene(String scenePath){
|
||||||
//add files to virtual filesystem in script engine
|
//add files to virtual filesystem in script engine
|
||||||
registerFile(scenePath);
|
registerFile(scenePath);
|
||||||
//require the module
|
//load scene from javascript side
|
||||||
requireModule(scenePath);
|
Value sceneLoader = this.engineObject.getMember("sceneLoader");
|
||||||
|
Value loadFunc = sceneLoader.getMember("loadScene");
|
||||||
|
loadFunc.execute(scenePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user