This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic lua intergration (#20)
- Loading branch information
1 parent
37b5de6
commit 4a8323d
Showing
17 changed files
with
1,051 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
import 'package:yaru/yaru.dart'; | ||
|
||
Future<void> requestFilePermissions() async { | ||
if (Platform.isAndroid) { | ||
final status = await Permission.storage.request(); | ||
if (status.isGranted) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
class DefaultYaru extends StatelessWidget { | ||
final Widget child; | ||
const DefaultYaru(this.child, {super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return YaruTheme( | ||
data: const YaruThemeData(variant: YaruVariant.purple), | ||
child: child, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:eludris/lua/manager.dart'; | ||
import 'package:eludris/models/gateway/message.dart'; | ||
import 'package:lua_dardo/lua.dart'; | ||
import 'package:http/http.dart' show post; | ||
|
||
class LuaAPI { | ||
final API api; | ||
bool rejected = false; | ||
final MessageData? message; | ||
|
||
LuaAPI(this.api, {this.message}); | ||
|
||
static const Map<String, DartFunction> _registry = {}; | ||
static const Map<String, DartFunction> _apiMember = { | ||
"sendMessage": _sendMessage, | ||
"rejectMessage": _rejectMessage, | ||
"updateMessage": _updateMessage, | ||
}; | ||
|
||
static LuaAPI _getThis(LuaState ls) => ls.toUserdata<LuaAPI>(1)!.data; | ||
|
||
static int _openAPILib(LuaState ls) { | ||
ls.newMetatable("APIClass"); | ||
ls.pushValue(-1); | ||
ls.setField(-2, "__index"); | ||
ls.setFuncs(_apiMember, 0); | ||
|
||
ls.newLib(_registry); | ||
return 1; | ||
} | ||
|
||
static int _updateMessage(LuaState ls) { | ||
final api = _getThis(ls); | ||
final String content = ls.checkString(2)!; | ||
final String author = ls.checkString(3)!; | ||
if (api.message != null) { | ||
api.api.updateMessage(api.message!, content: content, author: author); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int _rejectMessage(LuaState ls) { | ||
_getThis(ls).rejected = true; | ||
return 0; | ||
} | ||
|
||
static int _sendMessage(LuaState ls) { | ||
LuaAPI luaAPI = _getThis(ls); | ||
String content = ls.checkString(2)!; | ||
String author = ls.checkString(3)!; | ||
|
||
luaAPI.api.sendMessage(content, author); | ||
return 0; | ||
} | ||
|
||
static void require(LuaState ls) { | ||
ls.requireF("API", _openAPILib, true); | ||
ls.pop(1); | ||
} | ||
} | ||
|
||
class API { | ||
final PluginInfo plugin; | ||
final List<MessageData> messages = []; | ||
final String http; | ||
|
||
API({required this.http, required this.plugin}); | ||
|
||
void sendMessage(String content, String author) async { | ||
if (!plugin.manifest.permissions.contains("SEND_MESSAGES")) { | ||
throw Exception("Plugin does not have permission to send messages"); | ||
} | ||
messages | ||
.add(MessageData(author, content, true, plugin: plugin.manifest.name)); | ||
|
||
await post(Uri.parse("$http/messages"), | ||
body: jsonEncode({"content": content, "author": author}), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}); | ||
} | ||
|
||
void updateMessage(MessageData message, {String? content, String? author}) { | ||
if (!plugin.manifest.permissions.contains("MODIFY_MESSAGES")) { | ||
throw Exception("Plugin does not have permission to modify messages"); | ||
} | ||
if (content != null) { | ||
message.content = content; | ||
} | ||
if (author != null) { | ||
message.author = author; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:eludris/lua/api.dart'; | ||
import 'package:eludris/lua/message.dart'; | ||
import 'package:eludris/models/gateway/message.dart'; | ||
import 'package:lua_dardo/lua.dart'; | ||
|
||
void pushToLua<T>(LuaState ls, String varName, String className, T object) { | ||
Userdata u = ls.newUserdata<T>(); | ||
u.data = object; | ||
ls.getMetatableAux("${className}Class"); | ||
ls.setMetatable(-2); | ||
ls.setGlobal(varName); | ||
} | ||
|
||
LuaState prepareLua([LuaAPI? api, MessageData? message]) { | ||
final ls = LuaState.newState(); | ||
ls.openLibs(); | ||
|
||
if (api != null) { | ||
LuaAPI.require(ls); | ||
pushToLua<LuaAPI>(ls, "api", "API", api); | ||
} | ||
|
||
if (message != null) { | ||
pushMessageToLua(ls, message); | ||
} | ||
return ls; | ||
} |
Oops, something went wrong.