feat: Lua trigger getGlobal(Chat)Var (#810)

# PR Checklist
- [ ] Have you checked if it works normally in all models? *Ignore this
if it doesn't use models.*
- [x] Have you checked if it works normally in all web, local, and node
hosted versions? If it doesn't, have you blocked it in those versions?
- [x] Have you added type definitions?

# Description
Allow usage of {{getglobalvar::}} equivalent in Lua trigger script

Further on, setting the global variable is the next scope to be
implemented.
This commit is contained in:
kwaroran
2025-04-08 19:20:26 +09:00
committed by GitHub
2 changed files with 19 additions and 4 deletions

View File

@@ -1278,7 +1278,7 @@ function basicMatcher (p1:string,matcherArg:matcherArg,vars:{[key:string]:string
}
case 'getglobalvar':{
return getGlobalChatVar(v)
}
} // setglobalvar cbs support?
case 'button':{
return `<button class="button-default" risu-trigger="${arra[2]}">${arra[1]}</button>`
}
@@ -2235,6 +2235,10 @@ export function getGlobalChatVar(key:string){
return DBState.db.globalChatVariables[key] ?? 'null'
}
export function setGlobalChatVar(key:string, value:string){
DBState.db.globalChatVariables[key] = value // String to String Map(dictionary)
}
export function setChatVar(key:string, value:string){
const selectedChar = get(selectedCharID)
if(!DBState.db.characters[selectedChar].chats[DBState.db.characters[selectedChar].chatPage].scriptstate){

View File

@@ -1,4 +1,4 @@
import { getChatVar, hasher, setChatVar, type simpleCharacterArgument } from "../parser.svelte";
import { getChatVar, hasher, setChatVar, getGlobalChatVar, type simpleCharacterArgument } from "../parser.svelte";
import { LuaEngine, LuaFactory } from "wasmoon";
import { getCurrentCharacter, getCurrentChat, getDatabase, setCurrentChat, setDatabase, type Chat, type character, type groupChat } from "../storage/database.svelte";
import { get } from "svelte/store";
@@ -25,7 +25,8 @@ interface LuaEngineState {
mutex: Mutex;
chat: Chat;
setVar: (key:string, value:string) => void,
getVar: (key:string) => string
getVar: (key:string) => string,
getGlobalVar: (key:string) => any,
}
let LuaEngines = new Map<string, LuaEngineState>()
@@ -35,6 +36,7 @@ export async function runLua(code:string, arg:{
chat?:Chat
setVar?: (key:string, value:string) => void,
getVar?: (key:string) => string,
getGlobalVar?: (key:string) => any,
lowLevelAccess?: boolean,
mode?: string,
data?: any
@@ -42,6 +44,7 @@ export async function runLua(code:string, arg:{
const char = arg.char ?? getCurrentCharacter()
const setVar = arg.setVar ?? setChatVar
const getVar = arg.getVar ?? getChatVar
const getGlobalVar = arg.getGlobalVar ?? getGlobalChatVar
const mode = arg.mode ?? 'manual'
const data = arg.data ?? {}
let chat = arg.chat ?? getCurrentChat()
@@ -60,7 +63,8 @@ export async function runLua(code:string, arg:{
mutex: new Mutex(),
chat,
setVar,
getVar
getVar,
getGlobalVar
}
LuaEngines.set(mode, luaEngineState)
wasEmpty = true
@@ -68,6 +72,7 @@ export async function runLua(code:string, arg:{
luaEngineState.chat = chat
luaEngineState.setVar = setVar
luaEngineState.getVar = getVar
luaEngineState.getGlobalVar = getGlobalVar
}
return await luaEngineState.mutex.runExclusive(async () => {
if (wasEmpty || code !== luaEngineState.code) {
@@ -87,6 +92,12 @@ export async function runLua(code:string, arg:{
}
return luaEngineState.getVar(key)
})
luaEngine.global.set('getGlobalVar', (id:string, key:string) => {
if(!LuaSafeIds.has(id) && !LuaEditDisplayIds.has(id)){
return
}
return luaEngineState.getGlobalVar(key)
})
luaEngine.global.set('stopChat', (id:string) => {
if(!LuaSafeIds.has(id)){
return