-
Notifications
You must be signed in to change notification settings - Fork 0
3. API
API object contains helpers for doing complex c# stuff easily
All helpers automatically dispose added stuff on JS.Stop and most methods can use native js functions
-
Console- Allows you to add remove and call DebugConsole commands -
Game- Has info about game state -
JSHook- Allows you to patch the game with Harmony -
XMLHook- API for listening for xml hooks in status effects -
Net- API for sending net messages between barotrauma client and server -
Web- Allows you to start simple Http and websocket servers and open web links -
Utils- some helper methods
Commands are cleared on JS.Stop, also if other script adds command with the same name there will be an exception
-
AddCommand(string name, object scriptFunc)- Adds console command, scriptFunc should be convertible toAction<string[]> -
CommandExists(string name)- checks if command was already added -
RemoveCommand(string name)- removes command -
ExecuteCommand(string command)- executes command string in barotrauma console
For now only has
-
IsClient- note, it's true for singleplayer or client multiplayer IsServerIsSingleplayerIsMultiplayer
It allows you to convert js functions to c# code and add it before or after any game method
Change its args, prevent execution, replace entirely or swallow exceptions
It uses HarmonyX: Harmony, HarmonyX
Also all patches are removed at JS.Stop
It has:
-
Prefixes- PatchTracker that tracks JSPrefixes -
Postfixes- PatchTracker that tracks JSPostfixes -
Finalizers- PatchTracker that tracks JSFinalizers -
Clear()- Removes all patches
-
Add(MethodBase original, object scriptFunc, int priority = Priority.Normal) => int- Adds patch, scriptFunc must be convertible to PatchTrackers delegate, it returns int patch id, save it if you plan to unpatch it manually -
WasPatched(MethodBase original)- Checks if method has any patches -
Remove(MethodBase original, int ID)- Removes patch from method with ID -
Clear()- Removes all patches of some type
Prefixes use JSPrefix delegate
Postfixes use JSPostfix delegate
Finalizers use JSFinalizer delegate
delegate bool JSPrefix(object __instance, LilParamTable __args, FakeRefObject __resultreturned bool means run original method or not
delegate void JSPostfix(object __instance, LilParamTable __args, FakeRefObject __result)delegate Exception JSFinalizer(object __instance, LilParamTable __args, FakeRefObject __result, Exception __exception)original method with all prefixes and postfixes is wrapped in try catch, __exception is the exception that occured or null
You must always return Exception or null
-
object __instance- That c# object on which the original method was called, null if the method is static -
LilParamTable __args- is an object containing method args by name and index, if you change them they will be changed in original method -
FakeRefObject __result- __result.Value is the result of the method
Stack traces in js patches are buthered beyond recognition, but you can GetStackTrace below the patch from Utils
XML hooks are cleared on JS.Stop, also only 1 hook per name allowed, so if there's mod conflict you'll notice
-
Add(string name, object scriptFunc)- adds xml hook callback, scriptFunc should match Action<object[]>, args are these https://evilfactory.github.io/LuaCsForBarotrauma/lua-docs/manual/how-to-use-hooks/ but this is temporary as in the future barotrauma will have its own xml hook system and i doubt that args will be the same -
Remove(string name)- Removes xml hook callback, -
Has(string name)- Checks if it's already added
Net API is different on client and server
Also all messages are strings only, if you need complex data - send json
-
Send(string header, string data)- Sends a message -
ListenFor(string header, object scriptFunc)- Listen for a message, scriptFunc should be convertible toAction<string> -
OnConnected- Callback, called when client connects to the server
-
Send(string header, string data, Client client)- Sends a message to a client -
Broadcast(string header, string data)- Sends a message to all clients -
ListenFor(string header, object scriptFunc)- Listen for a message, scriptFunc should be convertible toAction<string, Client>
It allows you to start http and websocket servers and open web links
Why? You can create stuff like control panels in steam overlay that can interact with the game and use full might of browser GUI
For a time being it's client only
Http servers uses https://github.com/qoollo/SharpHttpServer
Websocket server uses https://github.com/jchristn/WatsonWebsocket
I have no idea which ports are ok to use, don't ask me
-
OpenURL(string url)- opens url in system browser -
OpenURLInSteam(string url)- opens url in steam overlay -
IsValidURL(string url)- checks url -
HasHttpServer(int port)- checks if http server with that port already was created -
CreateHttpServer(int port)- creates http server -
RemoveHttpServer(int port)- stops and removes http server on that port -
HasWSServer(int port)- checks if ws server with that port already was created -
CreateWSServer(int port)- creates ws server -
RemoveWSServer(int port)- stops and removes ws server on that port
-
Port- its port -
ServeStatic(string rootDir, string url)- makes server serve files in rootDir under url, you can get rootDir fromModInfo.FullPath -
IsRunning()- note that this is a method not a prop, also it's useless as RemoveHttpServer stops it automatically -
Run()- Starts the server -
Stop()- Stops the server -
Get,Post,Put,Delete- RequestHandlers
RequestHandlers - are internally Dictionary<string, Func<HttpListenerRequest, string>>
They accept script functions
They contain response funcs that should return string responses to HttpListenerRequests
Use them like this Server.Get["/bruh"] = (e) =>"bruh"
-
ClientConnected-scriptFunc(GUID clientGUID)callback -
ClientDisconnected-scriptFunc(GUID clientGUID)callback -
MessageReceived-scriptFunc(GUID clientGUID, string data)callback -
Stop()- Stops the server -
Start()- Starts the server -
Send(Guid clientId, string data)- Sends message to the client
-
GetStackTrace()- returns stack trace below methods in JSHook, made it just to get cleaner stack traces in js debugger -
ToJSArray(IEnumerable csEnumerable)- converts c# IEnumerable to JS array, all other methods like Array.from don't work -
ToCsArray(object scriptArray)- converts js array to c# Array