Skip to content

Commit

Permalink
🐛 added xiaomawang support
Browse files Browse the repository at this point in the history
  • Loading branch information
FurryR committed Apr 20, 2024
1 parent 42356a2 commit e8765f1
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 118 deletions.
18 changes: 9 additions & 9 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@

## 📄 文档

- `Hyren.Compiler.set(flag?: boolean)`
- `Hyren.compiler(flag?: boolean)`
启用/禁用编译器。如果 flag 未被指定,返回当前的状态。默认为 `true`
- `Hyren.Compiler.warp(flag?: boolean)`
- `Hyren.compiler.warp(flag?: boolean)`
启用/禁用编译器的循环检测器。如果 flag 未被指定,返回当前的状态。如果你在使用编辑器,它将默认设定为 `true`,否则它将设定为 `false`
- `Hyren.Interpolation.set(flag?: boolean)`
- `Hyren.interpolation(flag?: boolean)`
启用/禁用补帧。如果 flag 未被指定,返回当前的状态。默认为 `false`
- `Hyren.Options.hires(flag?: boolean)`
- `Hyren.hires(flag?: boolean)`
启用/禁用高质量渲染器 (又称 `HQPen` 或高清画笔). 如果 flag 未被指定,返回当前的状态。默认为 `false`
- `Hyren.Options.fps(num?: number)`
- `Hyren.fps(num?: number)`
设置帧率为 `num`。允许的帧率区间为 `[0,250]``0` 是一个特殊值,意味着帧率将匹配设备刷新率。如果 num 未被指定,返回当前的帧率。默认为 `30`.
- `Hyren.Options.maxClones(num?: number)`
- `Hyren.maxClones(num?: number)`
设置最大克隆体限制为 `num`。如果 num 未被指定,返回当前的最大限制。默认为 `300`.
- `Hyren.Options.fencing(flag?: boolean)`
- `Hyren.fencing(flag?: boolean)`
启用/禁用角色边缘检测。如果 flag 未被指定,返回当前的状态。默认为 `true`
- `Hyren.Options.miscLimit(flag?: boolean)`
- `Hyren.miscLimit(flag?: boolean)`
启用/禁用原版 Scratch 的其它限制。如果 flag 未被指定,返回当前的状态。默认为 `true`
- `Hyren.Options.size(width?: number, height?: number)`
- `Hyren.size(width?: number, height?: number)`
设置舞台大小。如果 width 和 height 都未被指定,返回当前状态。否则,将设置舞台的宽/高并对未指定的那部分保持原来的值。
- `Hyren.save()`
保存设置到项目。兼容 Turbowarp。
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ Usually you can use hyren to boost most projects, but there are always some exce

## 📄 Documentation

- `Hyren.Compiler.set(flag?: boolean)`
- `Hyren.compiler(flag?: boolean)`
Enable/Disable compiler. If flag is not specified, returns current status. Defaults to `true`.
- `Hyren.Compiler.warp(flag?: boolean)`
- `Hyren.compiler.warp(flag?: boolean)`
Enable/Disable compiler warp timer. If flag is not specified, returns current status. If you are using editor, it is set to `true`, otherwise it is set to `false` at first.
- `Hyren.Interpolation.set(flag?: boolean)`
- `Hyren.interpolation(flag?: boolean)`
Enable/Disable interpolation. If flag is not specified, returns current status. Defaults to `false`.
- `Hyren.Options.hires(flag?: boolean)`
- `Hyren.hires(flag?: boolean)`
Enable/Disable high quality renderer (aka `HQPen` or `High quality pen`). If flag is not specified, returns current status. Defaults to `false`.
- `Hyren.Options.fps(num?: number)`
- `Hyren.fps(num?: number)`
Set framerate to `num`. The allowed framerate range is `[0,250]`. `0` is a special value which means **"matching device screen refresh rate"**. If num is not specified, returns current specified framerate. Defaults to `30`.
- `Hyren.Options.maxClones(num?: number)`
- `Hyren.maxClones(num?: number)`
Set max clone limit to `num`. If num is not specified, returns current specified limit. Defaults to `300`.
- `Hyren.Options.fencing(flag?: boolean)`
- `Hyren.fencing(flag?: boolean)`
Enable/Disable fencing check. If flag is not specified, returns current status. Defaults to `true`.
- `Hyren.Options.miscLimit(flag?: boolean)`
- `Hyren.miscLimit(flag?: boolean)`
Enable/Disable miscellaneous limits presented by original Scratch. If flag is not specified, returns current status. Defaults to `true`.
- `Hyren.Options.size(width?: number, height?: number)`
- `Hyren.size(width?: number, height?: number)`
Set stage size. If both width and height are not specified, returns current status. Otherwise, set stage width/height while keeping the unspecified one.
- `Hyren.save()`
Save settings to the project. Compatible with Turbowarp.
Expand Down
52 changes: 50 additions & 2 deletions src/compiler/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,61 @@ export default function patchRenderer(vm: VM) {
if (vm.runtime.renderer) {
onReady()
} else {
// Object.defineProperty(vm.runtime, 'renderer', {
// get() {
// return null
// },
// set(renderer: RenderWebGL) {
// console.error('renderer set')
// Object.defineProperty(vm.runtime, 'renderer', {
// value: renderer,
// writable: true,
// configurable: true,
// enumerable: true
// })
// onReady()
// },
// configurable: true,
// enumerable: true
// })
const _attachRenderer = vm.runtime.constructor.prototype.attachRenderer
vm.runtime.constructor.prototype.attachRenderer = function (
renderer: RenderWebGL
) {
vm.runtime.constructor.prototype.attachRenderer = _attachRenderer
const originalRenderer = this.renderer
// vm.runtime.constructor.prototype.attachRenderer = _attachRenderer
_attachRenderer.call(this, renderer)
onReady()
// Xiaomawang attaches renderer twice.
if (originalRenderer !== renderer && !(renderer instanceof RenderWebGL)) {
const isXiaomawang = !!(renderer as any).clearAllSkins
if (isXiaomawang) {
const { clearAllSkins } = renderer as any
onReady()
renderer.constructor.prototype.clearAllSkins = clearAllSkins
renderer.constructor.prototype.extractDrawable = function () {
return {
data: '',
x: 0,
y: 0,
width: 0,
height: 0,
scratchOffset: [0, 0]
}
}
new ResizeObserver(() => {
renderer.resize(
renderer.canvas.clientWidth,
renderer.canvas.clientHeight
)
requestAnimationFrame(() => {
;(renderer as any).dirty = true
renderer.draw()
})
}).observe(renderer.canvas)
} else {
onReady()
}
}
}
}
}
8 changes: 8 additions & 0 deletions src/compiler/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ export default function patchRuntime(vm: VM) {
}
})
}
runtime.constructor.prototype.enableDebug = function () {
this.resetAllCaches()
this.debug = true
}
vm.constructor.prototype.enableDebug = function () {
this.runtime.enableDebug()
return 'enabled debug mode'
}
runtime.constructor.prototype.setCompilerOptions = function (
compilerOptions: object
) {
Expand Down
193 changes: 95 additions & 98 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ trap(vm => {
})
patchRuntime(vm)
;(window as any).Hyren = {
Interpolation: {
set(flag: unknown) {
if (flag === undefined) return (vm.runtime as any).interpolationEnabled
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.interpolation.enabled'))
else MainLog.log(formatMessage('hyren.interpolation.disabled'))
;(vm.runtime as any).setInterpolation(enabled)
}
interpolation(flag: unknown) {
if (flag === undefined) return (vm.runtime as any).interpolationEnabled
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.interpolation.enabled'))
else MainLog.log(formatMessage('hyren.interpolation.disabled'))
;(vm.runtime as any).setInterpolation(enabled)
},
Compiler: {
set(flag: unknown) {
compiler: Object.assign(
(flag: unknown) => {
if (flag === undefined)
return (vm.runtime as any).compilerOptions?.enabled
const enabled = !!flag
Expand All @@ -60,99 +58,98 @@ trap(vm => {
enabled
})
},
warp(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).compilerOptions?.warpTimer
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.warp.enabled'))
else MainLog.log(formatMessage('hyren.warp.disabled'))
;(vm.runtime as any).setCompilerOptions({
warpTimer: enabled
})
{
warp(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).compilerOptions?.warpTimer
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.warp.enabled'))
else MainLog.log(formatMessage('hyren.warp.disabled'))
;(vm.runtime as any).setCompilerOptions({
warpTimer: enabled
})
}
}
),
size(width: unknown, height: unknown) {
const nativeSize = vm.runtime.renderer.getNativeSize()
if (width === undefined && height == undefined) return nativeSize
width = width ?? nativeSize[0]
height = height ?? nativeSize[1]
const v = Number(width)
const v2 = Number(height)
const x = isNaN(v) ? nativeSize[0] : width
const y = isNaN(v2) ? nativeSize[1] : height
MainLog.log(
formatMessage('hyren.size')
.replace('%o', String(x))
.replace('%2o', String(y))
)
;(vm.runtime as any).setStageSize(x, y)
return [x, y]
},
Options: {
size(width: unknown, height: unknown) {
const nativeSize = vm.runtime.renderer.getNativeSize()
if (width === undefined && height == undefined) return nativeSize
width = width ?? nativeSize[0]
height = height ?? nativeSize[1]
const v = Number(width)
const v2 = Number(height)
const x = isNaN(v) ? nativeSize[0] : width
const y = isNaN(v2) ? nativeSize[1] : height
hires(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).renderer?.useHighQualityRender
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.hires.enabled'))
else MainLog.log(formatMessage('hyren.hires.disabled'))
;(vm.runtime as any).renderer?.setUseHighQualityRender(enabled)
return enabled
},
fps(num: unknown) {
if (num === undefined) return (vm.runtime as any).frameLoop?.framerate
const v = Number(num)
const fps = isNaN(v) ? 30 : v
if (fps === 0) {
MainLog.log(formatMessage('hyren.fps.sync'))
} else {
MainLog.log(formatMessage('hyren.fps').replace('%o', String(fps)))
}
;(vm.runtime as any).setFramerate(fps)
return fps
},
maxClones(num: unknown) {
if (num === undefined)
return (vm.runtime as any).runtimeOptions?.maxClones
const v = Number(num)
const maxClones = isNaN(v)
? (vm.runtime.constructor as any).MAX_CLONES
: v
if (maxClones === (vm.runtime.constructor as any).MAX_CLONES) {
MainLog.log(
formatMessage('hyren.size')
.replace('%o', String(x))
.replace('%2o', String(y))
)
;(vm.runtime as any).setStageSize(x, y)
return [x, y]
},
hires(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).renderer?.useHighQualityRender
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.hires.enabled'))
else MainLog.log(formatMessage('hyren.hires.disabled'))
;(vm.runtime as any).renderer?.setUseHighQualityRender(enabled)
return enabled
},
fps(num: unknown) {
if (num === undefined) return (vm.runtime as any).frameLoop?.framerate
const v = Number(num)
const fps = isNaN(v) ? 30 : v
if (fps === 0) {
MainLog.log(formatMessage('hyren.fps.sync'))
} else {
MainLog.log(formatMessage('hyren.fps').replace('%o', String(fps)))
}
;(vm.runtime as any).setFramerate(fps)
return fps
},
maxClones(num: unknown) {
if (num === undefined)
return (vm.runtime as any).runtimeOptions?.maxClones
const v = Number(num)
const maxClones = isNaN(v)
? (vm.runtime.constructor as any).MAX_CLONES
: v
if (maxClones === (vm.runtime.constructor as any).MAX_CLONES) {
MainLog.log(
formatMessage('hyren.maxClones.default').replace(
'%o',
String(maxClones)
)
formatMessage('hyren.maxClones.default').replace(
'%o',
String(maxClones)
)
} else {
MainLog.log(
formatMessage('hyren.maxClones').replace('%o', String(maxClones))
)
}
;(vm.runtime as any).setRuntimeOptions({
maxClones
})
},
miscLimits(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).runtimeOptions?.miscLimits
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.miscLimits.enabled'))
else MainLog.log(formatMessage('hyren.miscLimits.disabled'))
;(vm.runtime as any).setRuntimeOptions({
miscLimits: enabled
})
},
fencing(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).runtimeOptions?.fencing
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.fencing.enabled'))
else MainLog.log(formatMessage('hyren.fencing.disabled'))
;(vm.runtime as any).setRuntimeOptions({
fencing: enabled
})
)
} else {
MainLog.log(
formatMessage('hyren.maxClones').replace('%o', String(maxClones))
)
}
;(vm.runtime as any).setRuntimeOptions({
maxClones
})
},
miscLimits(flag: unknown) {
if (flag === undefined)
return (vm.runtime as any).runtimeOptions?.miscLimits
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.miscLimits.enabled'))
else MainLog.log(formatMessage('hyren.miscLimits.disabled'))
;(vm.runtime as any).setRuntimeOptions({
miscLimits: enabled
})
},
fencing(flag: unknown) {
if (flag === undefined) return (vm.runtime as any).runtimeOptions?.fencing
const enabled = !!flag
if (enabled) MainLog.log(formatMessage('hyren.fencing.enabled'))
else MainLog.log(formatMessage('hyren.fencing.disabled'))
;(vm.runtime as any).setRuntimeOptions({
fencing: enabled
})
},
save() {
MainLog.log(formatMessage('hyren.save'))
Expand Down

0 comments on commit e8765f1

Please sign in to comment.