diff --git a/CUDLR/Scripts/Console.cs b/CUDLR/Scripts/Console.cs index ece8b7f..5f240cf 100644 --- a/CUDLR/Scripts/Console.cs +++ b/CUDLR/Scripts/Console.cs @@ -1,11 +1,11 @@ using UnityEngine; using System; -using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Linq; using System.Reflection; using System.Net; +using System.Collections; namespace CUDLR { @@ -64,6 +64,9 @@ public static void Queue(CommandAttribute command, string[] args) { /* Execute a command */ public static void Run(string str) { +#if UNITY_WEBGL && !UNITY_EDITOR + Instance.m_output.Clear(); +#endif if (str.Length > 0) { LogCommand(str); Instance.RecordCommand(str); @@ -71,6 +74,14 @@ public static void Run(string str) { } } +#if UNITY_WEBGL && !UNITY_EDITOR + public static string GetResult() + { + Update(); + return string.Join("\n", Instance.m_output.ToArray()); + } +#endif + /* Clear all output from console */ [Command("clear", "clears console output", false)] public static void Clear() { @@ -101,18 +112,35 @@ public static void LogCommand(string cmd) { /* Logs string to output */ public static void Log(string str) { +#if UNITY_WEBGL && !UNITY_EDITOR + Instance.m_output.Add(str); +#else + LogCore(str); +#endif + } + + private static void LogCore(string str) + { +#if !UNITY_WEBGL || UNITY_EDITOR Instance.m_output.Add(str); if (Instance.m_output.Count > MAX_LINES) Instance.m_output.RemoveAt(0); +#endif } /* Callback for Unity logging */ public static void LogCallback (string logString, string stackTrace, LogType type) { - if (type != LogType.Log) { - Console.Log("" + logString); - Console.Log(stackTrace + ""); - } else { - Console.Log(logString); + if (type != LogType.Log) { +#if UNITY_WEBGL && !UNITY_EDITOR + Console.LogCore(logString); + Console.LogCore(stackTrace); +#else + Console.LogCore("" + logString); + Console.LogCore(stackTrace + ""); +#endif + } + else { + Console.LogCore(logString); } } @@ -174,7 +202,7 @@ private void RegisterAttributes() { } } } - } + } } /* Get a previously ran command from the history */ @@ -333,7 +361,7 @@ public void Run(string commandStr) { } static string[] emptyArgs = new string[0]{}; - private void _run(string[] commands, int index) { + private void _run(string[] commands, int index) { if (commands.Length == index) { RunCommand(emptyArgs); return; diff --git a/CUDLR/Scripts/Server.cs b/CUDLR/Scripts/Server.cs index 2d14c71..09fbe07 100644 --- a/CUDLR/Scripts/Server.cs +++ b/CUDLR/Scripts/Server.cs @@ -25,6 +25,7 @@ public class RequestContext public RequestContext(HttpListenerContext ctx) { +#if !UNITY_WEBGL || UNITY_EDITOR context = ctx; match = null; pass = false; @@ -32,6 +33,7 @@ public RequestContext(HttpListenerContext ctx) if (path == "/") path = "/index.html"; currentRoute = 0; +#endif } } @@ -66,9 +68,14 @@ public class Server : MonoBehaviour { }; public virtual void Awake() { +#if UNITY_WEBGL && !UNITY_EDITOR + WebGL_Relay.Initialize(); +#endif mainThread = Thread.CurrentThread; fileRoot = Path.Combine(Application.streamingAssetsPath, "CUDLR"); +#if !UNITY_WEBGL || UNITY_EDITOR + // Start server Debug.Log("Starting CUDLR Server on port : " + Port); listener = new HttpListener(); @@ -77,6 +84,7 @@ public virtual void Awake() { listener.BeginGetContext(ListenerCallback, null); StartCoroutine(HandleRequests()); +#endif } public void OnApplicationPause(bool paused) { @@ -198,6 +206,7 @@ static void RegisterFileHandlers() { registeredRoutes.Add(fileRoute); } +#if !UNITY_WEBGL || UNITY_EDITOR void OnEnable() { if (RegisterLogCallback) { // Capture Console Logs @@ -218,6 +227,7 @@ void OnDisable() { #endif } } +#endif void Update() { Console.Update(); diff --git a/CUDLR/Scripts/WebGL_Relay.cs b/CUDLR/Scripts/WebGL_Relay.cs new file mode 100644 index 0000000..afe58fb --- /dev/null +++ b/CUDLR/Scripts/WebGL_Relay.cs @@ -0,0 +1,54 @@ +using AOT; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using UnityEngine; + +namespace CUDLR +{ + /// + /// Relay between browser console and CUDLR.Console. + /// + public static class WebGL_Relay + { + /// + /// Initialize by setting callback to a javascript on the browser. + /// + public static void Initialize() + { +#if UNITY_WEBGL && !UNITY_EDITOR + Debug.LogFormat("Initialize CUDLR WebGL Relay"); + SetCudlrCallbackAndCreateInput(RunCommand); + Console.Update(); +#endif + } + + public static void Log(string text) + { +#if UNITY_WEBGL && !UNITY_EDITOR + ShowLog(text); +#endif + } + +#if UNITY_WEBGL && !UNITY_EDITOR + /// + /// General command callback. + /// + /// + [MonoPInvokeCallback(typeof(Func))] + static private string RunCommand(string commandline) + { + Console.Run(commandline); + return Console.GetResult(); + } + + [DllImport("__Internal")] + static extern void SetCudlrCallbackAndCreateInput(Func callback); + + [DllImport("__Internal")] + static extern string ShowLog(string text); +#endif + } +} diff --git a/WebGL/CUDLR_Relay.jslib b/WebGL/CUDLR_Relay.jslib new file mode 100644 index 0000000..3b1e59f --- /dev/null +++ b/WebGL/CUDLR_Relay.jslib @@ -0,0 +1,37 @@ +var CUDLR_Relay = { + $cr_callback: function(){}, + + SetCudlrCallbackAndCreateInput: function(callbackPtr){ + cr_callback = callbackPtr; + + window.cc = function(command) { + var size = lengthBytesUTF8(command) + 1; + var buffer = _malloc(size); + stringToUTF8(command, buffer, size); + + var result = Runtime.dynCall('ii', cr_callback, [buffer]); + var text = UTF8ToString(result); + _free(buffer); + + return text; + } + + document.onclick = function(){ + if(!window.event.getModifierState("Shift") + || !window.event.getModifierState("Alt")) return true; + + var text = cc(prompt()); + console.log(text); + + return true; + }; + + }, + + ShowLog: function(text){ + console.log(text); + }, + +} +autoAddDeps(CUDLR_Relay, '$cr_callback'); +mergeInto(LibraryManager.library, CUDLR_Relay);