Minor improvements and fixes
This commit is contained in:
22
src/main.ts
22
src/main.ts
@@ -1,16 +1,14 @@
|
||||
import { ErrorMapper } from "utils/ErrorMapper";
|
||||
|
||||
export function loop() {
|
||||
try {
|
||||
// Clear non-existing creep memory.
|
||||
for (const name in Memory.creeps) {
|
||||
if (!Game.creeps[name]) {
|
||||
delete Memory[name];
|
||||
}
|
||||
}
|
||||
// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change
|
||||
// This utility uses source maps to get the line numbers and file names of the original, TS source code
|
||||
export const loop = ErrorMapper.wrapLoop(function() {
|
||||
console.log(`Current game tick is ${Game.time}`);
|
||||
|
||||
console.log(`Current tick is ${Game.time}`);
|
||||
} catch (e) {
|
||||
console.log(ErrorMapper.sourceMappedStackTrace(e));
|
||||
// Automatically delete memory of missing creeps
|
||||
for (let name in Memory.creeps) {
|
||||
if (!(name in Game.creeps)) {
|
||||
delete Memory.creeps[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -67,4 +67,23 @@ export class ErrorMapper {
|
||||
this.cache[stack] = outStack;
|
||||
return outStack;
|
||||
}
|
||||
|
||||
public static wrapLoop(loop: () => void): () => void {
|
||||
return () => {
|
||||
try {
|
||||
loop();
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
if ("sim" in Game.rooms) {
|
||||
console.log(`<span style='color:red'>Source maps don't work in the simulator - displaying original error<br>${_.escape(e.stack)}</span>`)
|
||||
} else {
|
||||
console.log(`<span style='color:red'>${_.escape(this.sourceMappedStackTrace(e))}</span>`);
|
||||
}
|
||||
} else {
|
||||
// can't handle it
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user