UNPKG

@awayfl/awayfl-player

Version:

Flash Player emulator for executing SWF files (published for FP versions 6 and up) in javascript

280 lines (279 loc) 10.8 kB
import { AVMStage, registerDebugMethod } from "@awayfl/swf-loader"; import { PickGroup } from "@awayjs/view"; import { SharedObjectDebug as SOavm2 } from "@awayfl/playerglobal"; import { SharedObjectDebug as SOavm1 } from "@awayfl/avm1"; function fullSerializer(obj) { var clone = Object.assign({}, obj); Object.keys(clone).forEach(function (key) { if (typeof clone[key] === 'object') { clone[key] = fullSerializer(clone[key]); } else if (typeof clone[key] === 'function') { // replace func with it string representation clone[key] = clone[key].toString(); } }); return clone; } var OBJECT_FIELDS = ['id', 'visible', 'index', 'assetType:type', 'name']; var AVMDebug = /** @class */ (function () { function AVMDebug(player) { this.player = player; this._rafState = 'play'; this._defaultRaf = self.requestAnimationFrame; this._requestedCallbacks = []; registerDebugMethod(this._dirObjectByIds.bind(this), { name: "dirObjectByIds", description: "Export selected object to console", declaration: [{ name: 'ids', type: "object" }] }); registerDebugMethod(this._applyPropsByIds.bind(this), { name: "applyPropsByIds", description: "Apply propertyes by node ids", declaration: [{ name: 'ids', type: "object" }, { name: 'object', type: 'object' }] }); registerDebugMethod(this._removeObjectByIds.bind(this), { name: "removeObjectByIds", description: "Remove object from sceen tree", declaration: [{ name: 'ids', type: "object" }] }); registerDebugMethod(this._getInfo.bind(this), { name: "getInfo", description: "Get file info for app", declaration: [{ name: "return", type: "object" }] }); registerDebugMethod(this._getSceneTree.bind(this), { name: "getNodeTree", description: "Get sceen tree of app", declaration: [ { name: "return", type: "object" }, { name: "flat", type: "boolean" }, { name: "from", type: "number" }, { name: "rect", type: "object" } ] }); registerDebugMethod(this._getStageCanvas.bind(this), { name: "getStageCanvas", description: "Get canvas attahed to stage", declaration: [] }); registerDebugMethod(this._setRAFState.bind(this), { name: "setRAFState", description: "Changed RAF state", declaration: [ { name: 'return', type: 'string' }, { name: 'state', type: 'string' } ] }); registerDebugMethod(this._getRAFState.bind(this), { name: "getRAFState", description: "Changed RAF state", declaration: [ { name: 'return', type: 'string' }, { name: 'state', type: 'string' } ] }); this._mokedRaf = this._mokedRaf.bind(this); //@ts-ignore window._AWAY_DEBUG_PLAYER_ = this; } AVMDebug.prototype.onAvmInit = function (version) { //@ts-ignore window._AWAY_DEBUG_STORAGE = version === 1 ? SOavm1 : SOavm2; }; AVMDebug.prototype._mokedRaf = function (callback) { if (this._requestedCallbacks.indexOf(callback) !== -1) return; this._requestedCallbacks.push(callback); return 0; }; AVMDebug.prototype._setRAFState = function (state) { if (!state) return this._rafState; if (state === this._rafState) return; if (state === 'next' && this._rafState === 'stop') { var time_1 = performance.now(); var callbacks = this._requestedCallbacks.slice(); this._rafState = 'next'; this._requestedCallbacks.length = 0; callbacks.forEach(function (e) { return e && e(time_1); }); return this._rafState = 'stop'; } if (state === 'stop') { this._requestedCallbacks.length = 0; self.requestAnimationFrame = this._mokedRaf; return this._rafState = 'stop'; } if (state === 'play') { var time_2 = performance.now(); var callbacks = this._requestedCallbacks.slice(); this._rafState = 'play'; this._requestedCallbacks.length = 0; self.requestAnimationFrame = this._defaultRaf; callbacks.forEach(function (e) { return e && e(time_2); }); } return this._rafState; }; AVMDebug.prototype._getRAFState = function () { return this._rafState; }; AVMDebug.prototype._selectNode = function (ids) { var node = this.player.root; var _loop_1 = function (i) { node = node._children.find(function (e) { return e.id === i; }); if (!node) { return "break"; } }; for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) { var i = ids_1[_i]; var state_1 = _loop_1(i); if (state_1 === "break") break; } if (!node) { throw new Error("Node not found"); } return node; }; AVMDebug.prototype._getStageCanvas = function () { return this.player.view.stage.container; }; AVMDebug.prototype._dirObjectByIds = function (ids) { var node = this._selectNode(ids); //@ts-ignore var exposeID = window._lastTempNode = window._lastTempNode || 1; //@ts-ignore window._lastTempNode++; window['tempNode' + exposeID] = node; console.log('tempNode' + exposeID, '='); console.dir(node); }; AVMDebug.prototype._getNodeBounds = function (node) { var view = this.player.view; var box; var pool = AVMStage.instance && AVMStage.instance().pool; if (pool) { //@ts-ignore var partition = pool.getNode(node).partition; var picker = PickGroup.getInstance().getBoundsPicker(partition); //@ts-ignore box = picker.getBoxBounds(pool.getNode(this.player.root), true, true); } else { //@ts-ignore box = PickGroup.getInstance().getBoundsPicker(node.partition).getBoxBounds(this.player.root); } if (!box) return null; var sx = view.width / this.player.stageWidth; var sy = view.height / this.player.stageHeight; //console.log("DisplayObject:getRect not yet implemented");FromBounds return { x: box.x * sx, y: box.y * sy, width: box.width * sx, height: box.height * sy }; }; AVMDebug.prototype._traverse = function (node, req, rect, visibleOnly) { if (req === void 0) { req = false; } if (rect === void 0) { rect = false; } if (visibleOnly === void 0) { visibleOnly = false; } var ret = { parentId: node.parent ? node.parent.id : -1, children: null, rect: null, }; for (var _i = 0, OBJECT_FIELDS_1 = OBJECT_FIELDS; _i < OBJECT_FIELDS_1.length; _i++) { var name_1 = OBJECT_FIELDS_1[_i]; var sub = name_1.split(":"); if (sub.length > 1) { ret[sub[1]] = node[sub[0]]; } else { ret[name_1] = node[name_1]; } } ret["globalVisible"] = node.parent ? (node.parent.visible && node.visible) : node.visible; if (rect) { ret.rect = this._getNodeBounds(node); } if (req) { ret.children = []; for (var _a = 0, _b = node._children; _a < _b.length; _a++) { var c = _b[_a]; if (visibleOnly && c.visible || !visibleOnly) { ret.children.push(this._traverse(c, req, rect, visibleOnly)); } } } return ret; }; AVMDebug.prototype._removeObjectByIds = function (ids) { var node = this._selectNode(ids); node.parent.removeChild(node); }; AVMDebug.prototype._applyPropsByIds = function (ids, object) { var node = this._selectNode(ids); Object.assign(node, object); }; AVMDebug.prototype._getSceneTree = function (params, fromArg, rectArg) { if (typeof params !== 'object') { params = { flat: params || false, from: fromArg || 0, rect: rectArg || false, visibleOnly: false }; } var _a = params.flat, flat = _a === void 0 ? false : _a, _b = params.from, from = _b === void 0 ? 0 : _b, _c = params.rect, rect = _c === void 0 ? false : _c, _d = params.visibleOnly, visibleOnly = _d === void 0 ? false : _d; var tree = []; //@ts-ignore var q = this.player.root._children.slice(); while (true) { var node = q.pop(); if (!node) { break; } tree.push(this._traverse(node, !flat, rect, visibleOnly)); if (flat) { q.push.apply(q, node._children.reverse().filter(function (e) { return (e.visible && visibleOnly || !visibleOnly); })); } } return tree; }; AVMDebug.prototype._getInfo = function () { var _a; var player = this.player; var avm = player._avmHandler.avmVersion; var _b = player._swfFile, swfVersion = _b.swfVersion, fpVersion = _b.fpVersion, frameCount = _b.frameCount, frameRate = _b.frameRate, compression = _b.compression, bytesTotal = _b.bytesTotal; var path = (_a = player._gameConfig.binary.filter(function (_a) { var resourceType = _a.resourceType; return resourceType === 'GAME'; })[0]) === null || _a === void 0 ? void 0 : _a.path; if (path && path.indexOf('?') > -1) { path = path.substring(0, path.indexOf('?')); } return { file: { name: player._gameConfig.title, path: path, size: bytesTotal }, runtime: { swfVersion: swfVersion, fpVersion: fpVersion, frameCount: frameCount, frameRate: frameRate, compression: compression, avm: avm }, config: fullSerializer(player._gameConfig) }; }; return AVMDebug; }()); export { AVMDebug };