LunaHook-mirror/LunaHook/resource/lunajspatch.js
恍兮惚兮 a3922d7ffb v8
2024-10-19 22:24:04 +08:00

201 lines
6.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var fontface = '';
var magicsend = '\x01LUNAFROMJS\x01'
var magicrecv = '\x01LUNAFROMHOST\x01'
var is_packed = IS_PACKED
var is_useclipboard = IS_USECLIPBOARD
var internal_http_port = INTERNAL_HTTP_PORT
function splitfonttext(transwithfont) {
if (transwithfont.substr(0, magicsend.length) == magicsend) //not trans
{
split = transwithfont.search('\x02')
return transwithfont.substr(split + 1);
}
else if (transwithfont.substr(0, magicrecv.length) == magicrecv) {
transwithfont = transwithfont.substr(magicrecv.length)
//magic font \x02 text
split = transwithfont.search('\x02')
fontface = transwithfont.substr(0, split)
text = transwithfont.substr(split + 1)
return text;
}
else {
return transwithfont;
}
}
function syncquery(s) {
if (internal_http_port == 0) { throw new Error('') }
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:' + internal_http_port + '/fuck?' + s, false);
xhr.send();
if (xhr.status === 200) {
return xhr.responseText;
} else {
throw new Error('')
}
}
function isEmptyString(str) {
return str === null || str === undefined || str.length == 0;
}
function clipboardsender(name, s_raw, lpsplit) {
//magic split \x02 text
if (isEmptyString(s_raw))
return s_raw
s = magicsend + name + '\x03' + lpsplit.toString() + '\x02' + s_raw;
try {
if (!is_useclipboard) { throw new Error('') }
const _clipboard = require('nw.gui').Clipboard.get();
_clipboard.set(s, 'text');
transwithfont = _clipboard.get('text');
}
catch (err) {
try {
if (!is_useclipboard) { throw new Error('') }
const clipboard = require('electron').clipboard;
clipboard.writeText(s);
transwithfont = clipboard.readText();
}
catch (err2) {
try {
transwithfont = syncquery(s)
}
catch (err3) {
return s_raw;
}
}
}
if (transwithfont.length == 0) return s_raw;
return splitfonttext(transwithfont)
}
function clipboardsender_only_send(name, s_raw, lpsplit) {
//magic split \x02 text
if (isEmptyString(s_raw))
return s_raw
s = magicsend + name + '\x03' + lpsplit.toString() + '\x02' + s_raw;
try {
if (!is_useclipboard) { throw new Error('') }
const _clipboard = require('nw.gui').Clipboard.get();
_clipboard.set(s, 'text');
}
catch (err) {
try {
if (!is_useclipboard) { throw new Error('') }
const clipboard = require('electron').clipboard;
clipboard.writeText(s);
}
catch (err2) {
try {
syncquery(s)
}
catch (err3) {
}
}
}
}
function rpgmakerhook() {
if (Window_Message.prototype.originstartMessage) { }
else {
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.drawText_ori = Bitmap.prototype.drawText;
Bitmap.prototype.last_y = 0;
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;
}
Bitmap.prototype.collectstring = { 2: '', 5: '', 6: '' };
setInterval(function () {
for (lpsplit in Bitmap.prototype.collectstring) {
if (Bitmap.prototype.collectstring[lpsplit].length) {
clipboardsender_only_send('rpgmakermv', Bitmap.prototype.collectstring[lpsplit], lpsplit)
Bitmap.prototype.collectstring[lpsplit] = ''
}
}
}, 100);
if (!is_packed) {
Bitmap.prototype.drawText = function (text, x, y, maxWidth, lineHeight, align) {
//y>100的有重复慢速是单字符快速是多字符
if (text && (y < 100)) {
extra = 5 + ((text.length == 1) ? 0 : 1);
if (y != Bitmap.prototype.last_y) {
Bitmap.prototype.collectstring[extra] += '\n'
}
Bitmap.prototype.collectstring[extra] += text;
Bitmap.prototype.last_y = y;
}
return this.drawText_ori(text, x, y, maxWidth, lineHeight, align);
}
}
Window_Message.prototype.startMessage = function () {
gametext = $gameMessage.allText();
resp = clipboardsender('rpgmakermv', gametext, 0);
$gameMessage._texts = [resp]
this.originstartMessage();
};
Window_Base.prototype.drawText = function (text, x, y, maxWidth, align) {
text = clipboardsender('rpgmakermv', text, 1)
return this.drawText_origin(text, x, y, maxWidth, align)
}
Window_Base.prototype.lastcalltime = 0
Window_Base.prototype.drawTextEx = function (text, x, y) {
__last = Window_Base.prototype.lastcalltime
__now = new Date().getTime()
Window_Base.prototype.lastcalltime = __now
if (__now - __last > 100)
text = clipboardsender('rpgmakermv', text, 2)
else {
Bitmap.prototype.collectstring[2] += text;
}
return this.drawTextEx_origin(text, x, y)
}
}
function tyranohook() {
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;
tyrano.plugin.kag.tag.text.start = function (pm) {
if (1 != this.kag.stat.is_script && 1 != this.kag.stat.is_html) {
pm.val = clipboardsender('tyranoscript', pm.val, 0);
if (fontface != '') {
this.kag.stat.font.face = fontface
}
}
return this.originstart(pm)
}
tyrano.plugin.kag.tag.chara_ptext.start = function (pm) {
pm.name = clipboardsender('tyranoscript', pm.name, 1)
return this.startorigin(pm)
}
}
function retryinject(times) {
if (times == 0) return;
try {
if (window.tyrano && tyrano.plugin) {
tyranohook();
}
else if (window.Utils && Utils.RPGMAKER_NAME) {
rpgmakerhook();
}
else {
setTimeout(retryinject, 3000, times - 1);
}
}
catch (err) {
//非主线程甚至没有window对象会弹窗报错
}
}
retryinject(3)