mami/src/mami.js/compat.js

34 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-01-18 19:50:37 +00:00
#include server.js
2024-01-22 13:12:14 +00:00
const MamiCompat = (current, path, handler) => {
if(typeof path !== 'string')
throw 'path must be a string';
if(typeof handler !== 'function')
throw 'handler must be a function';
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
path = path.split('.');
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
const final = path.pop();
if(final === undefined)
throw 'invalid path';
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
for(const part of path) {
if(!(part in current))
current[part] = {};
current = current[part];
}
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
if(!(final in current))
current[final] = handler;
};
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
// Backwards compat for scripts
// Keep in sync with <https://fii.moe/fp/13176> for as long as possible
MamiCompat(Umi, 'Server.SendMessage', text => Umi.Server.sendMessage(text));
MamiCompat(Umi, 'Protocol.SockChat.Protocol.Instance.SendMessage', text => Umi.Server.sendMessage(text));
MamiCompat(Umi, 'Protocol.SockLegacy.Protocol.Instance.SendMessage', text => Umi.Server.sendMessage(text));
MamiCompat(Umi, 'Parser.SockChatBBcode.EmbedStub', () => {}); // intentionally a no-op
MamiCompat(Umi, 'UI.View.SetText', text => console.log(`Umi.UI.View.SetText(text: ${text})`));
MamiCompat(Umi, 'UI.Menus.Add', (baseId, title, initiallyHidden) => console.log(`Umi.UI.Menus.Add(baseId: ${baseId}, title: ${title}, initiallyHidden: ${initiallyHidden})`));
MamiCompat(Umi, 'UI.Menus.Get', (baseId, icon) => console.log(`Umi.UI.Menus.Get(baseId: ${baseId}, icon: ${icon})`));