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";
|
||||
|
||||
/**
|
||||
@ -5,6 +6,10 @@ import { Scene } from "/Scripts/types/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 = {
|
||||
|
||||
@ -25,7 +24,7 @@ let COMPILER = {
|
||||
* The top level directory, "/"
|
||||
*/
|
||||
topLevelDirectory: {
|
||||
//as reqiored by our framework
|
||||
//as required by our framework
|
||||
Scripts: {
|
||||
compiler: {
|
||||
"host_access.js": {
|
||||
@ -65,9 +64,87 @@ let COMPILER = {
|
||||
* Registers a file with the compiler
|
||||
* @param {string} fileName The file's name
|
||||
* @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) => {
|
||||
|
||||
//the list of files that are imported by this file
|
||||
let dependentFiles = []
|
||||
|
||||
loggerScripts.INFO('REGISTER FILE ' + fileName)
|
||||
if(!COMPILER.fileMap[fileName]){
|
||||
//create the virtual file
|
||||
COMPILER.fileMap[fileName] = COMPILER.createFile(fileName,content)
|
||||
//register the file itself
|
||||
COMPILER.fileMap[fileName].tsSourceFile = ts.createSourceFile(
|
||||
fileName,
|
||||
content,
|
||||
ts.ScriptTarget.Latest,
|
||||
)
|
||||
COMPILER.sourceFiles.push(fileName)
|
||||
/**
|
||||
* The preprocessed info about the file
|
||||
* {
|
||||
* referencedFiles: ?,
|
||||
* typeReferenceDirectives: ?,
|
||||
* libReferenceDirectives: ?,
|
||||
* importedFiles: Array<{
|
||||
* fileName: string, //the path (without file ending) of the file that is imported by this file
|
||||
* pos: ?,
|
||||
* end: ?,
|
||||
* }>,
|
||||
* isLibFile: boolean,
|
||||
* ambientExternalModules: ?,
|
||||
* }
|
||||
*/
|
||||
const fileInfo = ts.preProcessFile(content)
|
||||
loggerScripts.INFO('==========================')
|
||||
loggerScripts.INFO(fileName)
|
||||
loggerScripts.INFO('Registered file depends on:')
|
||||
fileInfo.importedFiles.forEach(module => {
|
||||
let extension = ".ts"
|
||||
/**
|
||||
* {
|
||||
* resolvedModule: ?,
|
||||
* failedLookupLocations: Array<string>,
|
||||
* affectingLocations: ?,
|
||||
* resolutionDiagnostics: ?,
|
||||
* alternateResult: ?,
|
||||
* }
|
||||
*/
|
||||
const resolvedImport = ts.resolveModuleName(module.fileName,fileName,COMPILER.compilerOptions,COMPILER.customCompilerHost)
|
||||
if(resolvedImport?.resolvedModule){
|
||||
/**
|
||||
* undefined
|
||||
* OR
|
||||
* {
|
||||
* resolvedFileName: ?,
|
||||
* originalPath: ?,
|
||||
* extension: string, (ie ".js", ".ts", etc)
|
||||
* isExternalLibraryImport: boolean,
|
||||
* packageId: ?,
|
||||
* resolvedUsingTsExtension: boolean,
|
||||
* }
|
||||
*/
|
||||
const module = resolvedImport.resolvedModule
|
||||
extension = module.extension
|
||||
}
|
||||
//am assuming we're always importing typescript for the time being
|
||||
const dependentFile = module.fileName + extension
|
||||
const normalizedDependentFilePath = FILE_RESOLUTION_getFilePath(dependentFile,false)
|
||||
if(!COMPILER.fileMap[normalizedDependentFilePath]){
|
||||
dependentFiles.push(normalizedDependentFilePath)
|
||||
loggerScripts.INFO(" - " + normalizedDependentFilePath)
|
||||
}
|
||||
})
|
||||
|
||||
//If the compiler has already run once, run the language service against only this file
|
||||
if(!!COMPILER.compilerHasRun){
|
||||
COMPILER.emitFile(fileName)
|
||||
}
|
||||
}
|
||||
return dependentFiles;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates a file object for a given path
|
||||
@ -75,28 +152,103 @@ let COMPILER = {
|
||||
* @param {string} content The content of the file
|
||||
* @returns The file object
|
||||
*/
|
||||
createFile: (fileName, content) => null,
|
||||
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) => null,
|
||||
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) => null,
|
||||
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) => null,
|
||||
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
|
||||
@ -104,7 +256,13 @@ let COMPILER = {
|
||||
* @param {*} languageVersion The language version
|
||||
* @returns The file if it exists, null otherwise
|
||||
*/
|
||||
getSourceFile: (fileName, languageVersion) => null,
|
||||
getSourceFile: (fileName, languageVersion) => {
|
||||
if(!!COMPILER.fileMap[fileName]){
|
||||
return COMPILER.fileMap[fileName].tsSourceFile
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
//
|
||||
@ -123,37 +281,6 @@ let COMPILER = {
|
||||
*/
|
||||
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
|
||||
*/
|
||||
@ -163,212 +290,109 @@ let COMPILER = {
|
||||
* 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
|
||||
let dependentFiles = []
|
||||
|
||||
loggerScripts.INFO('REGISTER FILE ' + fileName)
|
||||
if(!COMPILER.fileMap[fileName]){
|
||||
//create the virtual file
|
||||
COMPILER.fileMap[fileName] = COMPILER.createFile(fileName,content)
|
||||
//register the file itself
|
||||
COMPILER.fileMap[fileName].tsSourceFile = ts.createSourceFile(
|
||||
fileName,
|
||||
content,
|
||||
ts.ScriptTarget.Latest,
|
||||
)
|
||||
COMPILER.sourceFiles.push(fileName)
|
||||
/**
|
||||
* Emits a file
|
||||
* @param {string} fileName The name of the file
|
||||
* @returns {void}
|
||||
*/
|
||||
emitFile: (fileName) => {
|
||||
loggerScripts.DEBUG('Compiler evaluating source path ' + fileName)
|
||||
/**
|
||||
* The preprocessed info about the file
|
||||
* {
|
||||
* referencedFiles: ?,
|
||||
* typeReferenceDirectives: ?,
|
||||
* libReferenceDirectives: ?,
|
||||
* importedFiles: Array<{
|
||||
* fileName: string, //the path (without file ending) of the file that is imported by this file
|
||||
* pos: ?,
|
||||
* end: ?,
|
||||
* }>,
|
||||
* isLibFile: boolean,
|
||||
* ambientExternalModules: ?,
|
||||
* outputFiles: [ ],
|
||||
* emitSkipped: boolean,
|
||||
* diagnostics: { },
|
||||
* }
|
||||
*/
|
||||
const fileInfo = ts.preProcessFile(content)
|
||||
loggerScripts.INFO('==========================')
|
||||
loggerScripts.INFO(fileName)
|
||||
loggerScripts.INFO('Registered file depends on:')
|
||||
fileInfo.importedFiles.forEach(module => {
|
||||
let extension = ".ts"
|
||||
/**
|
||||
* {
|
||||
* resolvedModule: ?,
|
||||
* failedLookupLocations: Array<string>,
|
||||
* affectingLocations: ?,
|
||||
* resolutionDiagnostics: ?,
|
||||
* alternateResult: ?,
|
||||
* }
|
||||
*/
|
||||
const resolvedImport = ts.resolveModuleName(module.fileName,fileName,COMPILER.compilerOptions,COMPILER.customCompilerHost)
|
||||
if(resolvedImport?.resolvedModule){
|
||||
/**
|
||||
* undefined
|
||||
* OR
|
||||
* {
|
||||
* resolvedFileName: ?,
|
||||
* originalPath: ?,
|
||||
* extension: string, (ie ".js", ".ts", etc)
|
||||
* isExternalLibraryImport: boolean,
|
||||
* packageId: ?,
|
||||
* resolvedUsingTsExtension: boolean,
|
||||
* }
|
||||
*/
|
||||
const module = resolvedImport.resolvedModule
|
||||
extension = module.extension
|
||||
}
|
||||
//am assuming we're always importing typescript for the time being
|
||||
const dependentFile = module.fileName + extension
|
||||
const normalizedDependentFilePath = FILE_RESOLUTION_getFilePath(dependentFile,false)
|
||||
if(!COMPILER.fileMap[normalizedDependentFilePath]){
|
||||
dependentFiles.push(normalizedDependentFilePath)
|
||||
loggerScripts.INFO(" - " + normalizedDependentFilePath)
|
||||
}
|
||||
})
|
||||
|
||||
//If the compiler has already run once, run the language service against only this file
|
||||
if(!!COMPILER.compilerHasRun){
|
||||
COMPILER.emitFile(fileName)
|
||||
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);
|
||||
}
|
||||
}
|
||||
return dependentFiles;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 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}`);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* The callback invoked when the compiler host tries to read a file
|
||||
* @param {*} fileName The name of the file
|
||||
* @param {*} languageVersion The language version
|
||||
* @returns The file if it exists, null otherwise
|
||||
*/
|
||||
COMPILER.getSourceFile = (fileName, languageVersion) => {
|
||||
if(!!COMPILER.fileMap[fileName]){
|
||||
return COMPILER.fileMap[fileName].tsSourceFile
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
/**
|
||||
* 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,
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@ -57,3 +57,15 @@ const require = (path) => {
|
||||
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
|
||||
let client: NamespaceClient = Client
|
||||
engine.sceneLoader.hookManager = engine.hookManager
|
||||
|
||||
loggerScripts.INFO('Script Engine Initialized')
|
||||
}
|
||||
|
||||
@ -72,7 +72,7 @@ export class HookManager {
|
||||
//Scene related structures
|
||||
//
|
||||
//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){
|
||||
this.trackedScenes.push(scene)
|
||||
}
|
||||
@ -97,7 +97,7 @@ export class HookManager {
|
||||
* @param signal 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]
|
||||
hooks.forEach(trackedHook => {
|
||||
trackedHook.callback(value)
|
||||
|
||||
@ -12,7 +12,7 @@ export interface TrackedScene {
|
||||
/**
|
||||
* The id assigned to the scene
|
||||
*/
|
||||
sceneId: number,
|
||||
instanceId: number,
|
||||
|
||||
/**
|
||||
* The scene itself
|
||||
@ -47,42 +47,60 @@ export class SceneLoader {
|
||||
/**
|
||||
* The list of loaded scenes
|
||||
*/
|
||||
loadedScenes: TrackedScene[]
|
||||
loadedScenes: TrackedScene[] = [ ]
|
||||
|
||||
/**
|
||||
* A record of tracked scene id to tracked scene object
|
||||
*/
|
||||
sceneIdMap: Record<number,TrackedScene>
|
||||
sceneIdMap: Record<number,TrackedScene> = { }
|
||||
|
||||
/**
|
||||
* A scene
|
||||
*/
|
||||
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
|
||||
* @param scene The scene
|
||||
* @returns The id assigned to the instance of the scene
|
||||
*/
|
||||
registerScene(scene: Scene, isServerScene: boolean){
|
||||
const shouldRegister: boolean = !this.containsScene(scene)
|
||||
if(shouldRegister){
|
||||
|
||||
//add to the list of tracked scenes
|
||||
const trackedScene: TrackedScene = {
|
||||
sceneId: this.sceneIdIncrementer++,
|
||||
scene: scene,
|
||||
sceneHooks: [],
|
||||
signalHookMap: { },
|
||||
}
|
||||
this.loadedScenes.push(trackedScene)
|
||||
this.sceneIdMap[trackedScene.sceneId] = trackedScene
|
||||
|
||||
//load all hooks from the scene
|
||||
scene.hooks.forEach((hook: Hook) => {
|
||||
engine.hookManager.registerHook(trackedScene,hook,isServerScene)
|
||||
})
|
||||
|
||||
createInstance(scene: Scene, isServerScene: boolean): number{
|
||||
//add to the list of tracked scenes
|
||||
const trackedScene: TrackedScene = {
|
||||
instanceId: this.sceneIdIncrementer++,
|
||||
scene: scene,
|
||||
sceneHooks: [],
|
||||
signalHookMap: { },
|
||||
}
|
||||
this.loadedScenes.push(trackedScene)
|
||||
this.sceneIdMap[trackedScene.instanceId] = trackedScene
|
||||
|
||||
//load all hooks from the scene
|
||||
scene?.hooks?.forEach((hook: Hook) => {
|
||||
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 { Namespace } from "/Scripts/types/namespace";
|
||||
|
||||
@ -26,4 +27,9 @@ export interface Scene extends Namespace {
|
||||
*/
|
||||
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);
|
||||
loggerAudio = new Logger(LogLevel.WARNING);
|
||||
loggerUI = new Logger(LogLevel.WARNING);
|
||||
loggerScripts = new Logger(LogLevel.DEBUG);
|
||||
loggerScripts = new Logger(LogLevel.WARNING);
|
||||
loggerStartup.INFO("Initialized loggers");
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,6 +42,9 @@ public class ScriptEngine {
|
||||
//the object that contains all host values accessible to javascript land
|
||||
Value hostObject;
|
||||
|
||||
//the engine object
|
||||
Value engineObject;
|
||||
|
||||
//The files that are loaded on init to bootstrap the script engine
|
||||
static final String[] filesToLoadOnInit = new String[]{
|
||||
//polyfills
|
||||
@ -104,6 +107,8 @@ public class ScriptEngine {
|
||||
|
||||
//run script for engine init
|
||||
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");
|
||||
|
||||
|
||||
@ -247,8 +252,10 @@ public class ScriptEngine {
|
||||
public void initScene(String scenePath){
|
||||
//add files to virtual filesystem in script engine
|
||||
registerFile(scenePath);
|
||||
//require the module
|
||||
requireModule(scenePath);
|
||||
//load scene from javascript side
|
||||
Value sceneLoader = this.engineObject.getMember("sceneLoader");
|
||||
Value loadFunc = sceneLoader.getMember("loadScene");
|
||||
loadFunc.execute(scenePath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user