LunaHook-mirror/LunaHook/resource/lunajspatch.js

108 lines
3.7 KiB
JavaScript
Raw Normal View History

2024-05-05 20:53:51 +08:00
var fontface = '';
var magicsend = '\x01LUNAFROMJS\x01'
var magicrecv = '\x01LUNAFROMHOST\x01'
function splitfonttext(transwithfont) {
if (transwithfont.substr(0, magicsend.length) == magicsend) //not trans
2024-03-25 14:59:33 +08:00
{
2024-05-05 20:53:51 +08:00
split = transwithfont.search('\x02')
return transwithfont.substr(split + 1);
2024-03-25 14:59:33 +08:00
}
2024-05-05 20:53:51 +08:00
else if (transwithfont.substr(0, magicrecv.length) == magicrecv) {
transwithfont = transwithfont.substr(magicrecv.length)
2024-03-25 14:59:33 +08:00
//magic font \x02 text
2024-05-05 20:53:51 +08:00
split = transwithfont.search('\x02')
fontface = transwithfont.substr(0, split)
text = transwithfont.substr(split + 1)
return text;
}
2024-05-05 20:53:51 +08:00
else {
return transwithfont;
}
}
2024-05-05 20:53:51 +08:00
function clipboardsender(s, lpsplit) {
lpsplit = typeof lpsplit !== 'undefined' ? lpsplit : 0
2024-03-25 14:59:33 +08:00
//magic split \x02 text
2024-05-05 20:53:51 +08:00
s = magicsend + lpsplit.toString() + '\x02' + s;
try {
const _clipboard = require('nw.gui').Clipboard.get();
2024-05-05 20:53:51 +08:00
_clipboard.set(s, 'text');
transwithfont = _clipboard.get('text');
}
2024-05-05 20:53:51 +08:00
catch (err) {
try {
2024-03-20 22:51:24 +08:00
const clipboard = require('electron').clipboard;
clipboard.writeText(s);
2024-05-05 20:53:51 +08:00
transwithfont = clipboard.readText();
2024-03-20 22:51:24 +08:00
}
2024-05-05 20:53:51 +08:00
catch (err2) {
2024-03-20 22:51:24 +08:00
return s;
}
}
2024-05-05 20:53:51 +08:00
if (transwithfont.length == 0) return s;
2024-03-20 22:51:24 +08:00
return splitfonttext(transwithfont)
}
2024-05-05 20:53:51 +08:00
function rpgmakerhook() {
if (Window_Message.prototype.originstartMessage) return;
Window_Base.prototype.drawTextEx_origin = Window_Base.prototype.drawTextEx;
Window_Base.prototype.drawText_origin = Window_Base.prototype.drawText;
Window_Message.prototype.originstartMessage = Window_Message.prototype.startMessage;
Bitmap.prototype.origin_makeFontNameText = Bitmap.prototype._makeFontNameText;
Bitmap.prototype._makeFontNameText = function () {
if (fontface == '') return this.origin_makeFontNameText();
return (this.fontItalic ? 'Italic ' : '') +
this.fontSize + 'px ' + fontface;
}
2024-05-05 20:53:51 +08:00
Window_Message.prototype.startMessage = function () {
gametext = $gameMessage.allText();
2024-05-05 20:53:51 +08:00
resp = clipboardsender(gametext);
$gameMessage._texts = [resp]
this.originstartMessage();
};
2024-05-05 20:53:51 +08:00
Window_Base.prototype.drawText = function (text, x, y, maxWidth, align) {
text = clipboardsender(text, 1)
return this.drawText_origin(text, x, y, maxWidth, align)
2024-03-25 16:13:58 +08:00
}
2024-05-05 20:53:51 +08:00
Window_Base.prototype.drawTextEx = function (text, x, y) {
text = clipboardsender(text, 1)
2024-03-25 16:13:58 +08:00
return this.drawTextEx_origin(text, x, y)
}
}
2024-03-20 22:51:24 +08:00
function tyranohook() {
2024-05-05 20:53:51 +08:00
if (tyrano.plugin.kag.tag.text.originstart) return;
tyrano.plugin.kag.tag.text.originstart = tyrano.plugin.kag.tag.text.start;
tyrano.plugin.kag.tag.chara_ptext.startorigin = tyrano.plugin.kag.tag.chara_ptext.start;
2024-03-17 02:09:28 +08:00
tyrano.plugin.kag.tag.text.start = function (pm) {
if (1 != this.kag.stat.is_script && 1 != this.kag.stat.is_html) {
2024-05-05 20:53:51 +08:00
pm.val = clipboardsender(pm.val);
if (fontface != '') {
this.kag.stat.font.face = fontface
}
2024-05-05 20:53:51 +08:00
}
2024-03-17 02:09:28 +08:00
return this.originstart(pm)
}
2024-03-25 14:59:33 +08:00
tyrano.plugin.kag.tag.chara_ptext.start = function (pm) {
2024-05-05 20:53:51 +08:00
pm.name = clipboardsender(pm.name, 1)
2024-03-25 14:59:33 +08:00
return this.startorigin(pm)
}
}
2024-05-05 20:53:51 +08:00
function retryinject(times) {
if (times == 0) return;
try {
if (window.tyrano && tyrano.plugin) {
2024-03-20 22:51:24 +08:00
tyranohook();
2024-03-16 13:40:08 +08:00
}
2024-05-05 20:53:51 +08:00
else if (window.Utils && Utils.RPGMAKER_NAME) {
2024-03-20 22:51:24 +08:00
rpgmakerhook();
2024-03-16 13:40:08 +08:00
}
2024-05-05 20:53:51 +08:00
else {
setTimeout(retryinject, 3000, times - 1);
2024-03-16 13:40:08 +08:00
}
}
2024-05-05 20:53:51 +08:00
catch (err) {
2024-03-16 13:40:08 +08:00
//非主线程甚至没有window对象会弹窗报错
}
}
2024-03-25 21:05:25 +08:00
retryinject(3)