LunaHook-mirror/LunaHook/resource/renpy_hook_text.py

125 lines
3.7 KiB
Python
Raw Normal View History

2024-05-05 02:33:58 +08:00
def callLunaHost(text, split):
2024-03-08 23:54:27 +08:00
try:
import ctypes
2024-05-05 02:33:58 +08:00
2024-03-08 23:54:27 +08:00
try:
2024-05-05 02:33:58 +08:00
internal_renpy_call_host = ctypes.CDLL(
"LunaHook64"
).internal_renpy_call_host
2024-03-08 23:54:27 +08:00
except:
2024-05-05 02:33:58 +08:00
internal_renpy_call_host = ctypes.CDLL(
"LunaHook32"
).internal_renpy_call_host
internal_renpy_call_host.argstype = ctypes.c_wchar_p, ctypes.c_int
internal_renpy_call_host.restype = ctypes.c_wchar_p
2024-03-20 13:25:45 +08:00
try:
2024-05-05 02:33:58 +08:00
_text = text.decode("utf8")
2024-03-20 13:25:45 +08:00
except:
2024-05-05 02:33:58 +08:00
_text = text
text = internal_renpy_call_host(_text, split)
2024-03-08 23:54:27 +08:00
except:
pass
return text
2024-05-05 02:33:58 +08:00
def callLunaIsUsingEmbed(split):
try:
import ctypes
try:
internal_renpy_call_is_embed_using = ctypes.CDLL(
"LunaHook64"
).internal_renpy_call_is_embed_using
except:
internal_renpy_call_is_embed_using = ctypes.CDLL(
"LunaHook32"
).internal_renpy_call_is_embed_using
internal_renpy_call_is_embed_using.argstype = ctypes.c_int,
internal_renpy_call_is_embed_using.restype = ctypes.c_bool
return internal_renpy_call_is_embed_using(split)
except:
return False
2024-03-08 23:54:27 +08:00
try:
2024-05-05 02:33:58 +08:00
# 6.1.0
2024-03-08 23:54:27 +08:00
import renpy
2024-05-05 02:33:58 +08:00
2024-03-08 23:54:27 +08:00
def hook_initT0(original_init):
def new_init(self, *args, **kwargs):
2024-05-05 02:33:58 +08:00
changed=False
2024-03-08 23:54:27 +08:00
if isinstance(args[0], list):
2024-05-05 02:33:58 +08:00
trs = []
for _ in args[0]:
_n=callLunaHost(_, 1)
if _n!=_:
changed=True
trs += [_n]
2024-03-08 23:54:27 +08:00
else:
2024-05-05 02:33:58 +08:00
trs = callLunaHost(args[0], 1)
if args[0]!=trs:
changed=True
2024-03-08 23:54:27 +08:00
2024-05-05 02:33:58 +08:00
if changed and callLunaIsUsingEmbed(1):
args = (trs,) + args[1:]
if "text" in kwargs:
kwargs["text"] = trs
original_init(self, *args, **kwargs)
2024-03-08 23:54:27 +08:00
return new_init
if "original_Text_init_hook" not in globals():
original_Text_init_hook = renpy.text.text.Text.__init__
renpy.text.text.Text.__init__ = hook_initT0(original_Text_init_hook)
def hook_init_renderT0(original):
def new_init(self, *args, **kwargs):
if not hasattr(self, "LunaHooked"):
2024-05-05 02:33:58 +08:00
changed=False
2024-03-08 23:54:27 +08:00
if isinstance(self.text, list):
2024-05-05 02:33:58 +08:00
trs = []
for _ in self.text:
_n=callLunaHost(_, 2)
if _n!=_:
changed=True
trs += [_n]
2024-03-08 23:54:27 +08:00
else:
2024-05-05 02:33:58 +08:00
trs = callLunaHost(self.text, 2)
if self.text!=trs:
changed=True
if changed and callLunaIsUsingEmbed(2):
self.set_text(trs)
self.LunaHooked = True
2024-03-08 23:54:27 +08:00
return original(self, *args, **kwargs)
return new_init
2024-05-05 02:33:58 +08:00
2024-03-08 23:54:27 +08:00
if "original_hook_init_renderT0" not in globals():
original_hook_init_renderT0 = renpy.text.text.Text.render
renpy.text.text.Text.render = hook_init_renderT0(original_hook_init_renderT0)
except:
pass
try:
2024-05-05 02:33:58 +08:00
# 4.0
2024-03-08 23:54:27 +08:00
import renpy
2024-05-05 02:33:58 +08:00
2024-03-08 23:54:27 +08:00
def hook_initT3(original_init):
def new_init(self, *args, **kwargs):
2024-05-05 02:33:58 +08:00
trs = callLunaHost(str(args[0]), 3)
if callLunaIsUsingEmbed(3):
args = (trs,) + args[1:]
original_init(self, *args, **kwargs)
2024-03-08 23:54:27 +08:00
return new_init
if "original_Text_init_hookT3" not in globals():
original_Text_init_hookT3 = renpy.exports.Text.__init__
renpy.exports.Text.__init__ = hook_initT3(original_Text_init_hookT3)
except:
2024-05-05 02:33:58 +08:00
pass