-
Notifications
You must be signed in to change notification settings - Fork 34
Implement WebGL console command support #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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,13 +64,24 @@ 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); | ||
| Instance.m_commands.Run(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 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In WebGL mode, debug logs are already shown in browser console. |
||
| 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("<span class='" + type + "'>" + logString); | ||
| Console.Log(stackTrace + "</span>"); | ||
| } else { | ||
| Console.Log(logString); | ||
| if (type != LogType.Log) { | ||
| #if UNITY_WEBGL && !UNITY_EDITOR | ||
| Console.LogCore(logString); | ||
| Console.LogCore(stackTrace); | ||
| #else | ||
| Console.LogCore("<span class='" + type + "'>" + logString); | ||
| Console.LogCore(stackTrace + "</span>"); | ||
| #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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,13 +25,15 @@ public class RequestContext | |
|
|
||
| public RequestContext(HttpListenerContext ctx) | ||
| { | ||
| #if !UNITY_WEBGL || UNITY_EDITOR | ||
| context = ctx; | ||
| match = null; | ||
| pass = false; | ||
| path = WWW.UnEscapeURL(context.Request.Url.AbsolutePath); | ||
| 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 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disabled them because we cant use socket in WebGL mode. |
||
|
|
||
| // 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(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| { | ||
| /// <summary> | ||
| /// Relay between browser console and CUDLR.Console. | ||
| /// </summary> | ||
| public static class WebGL_Relay | ||
| { | ||
| /// <summary> | ||
| /// Initialize by setting callback to a javascript on the browser. | ||
| /// </summary> | ||
| 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 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method can show text on browser console, without using debug logger. |
||
| ShowLog(text); | ||
| #endif | ||
| } | ||
|
|
||
| #if UNITY_WEBGL && !UNITY_EDITOR | ||
| /// <summary> | ||
| /// General command callback. | ||
| /// </summary> | ||
| /// <param name="commandline"></param> | ||
| [MonoPInvokeCallback(typeof(Func<string,string>))] | ||
| static private string RunCommand(string commandline) | ||
| { | ||
| Console.Run(commandline); | ||
| return Console.GetResult(); | ||
| } | ||
|
|
||
| [DllImport("__Internal")] | ||
| static extern void SetCudlrCallbackAndCreateInput(Func<string,string> callback); | ||
|
|
||
| [DllImport("__Internal")] | ||
| static extern string ShowLog(string text); | ||
| #endif | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| var CUDLR_Relay = { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is converted into javascript and placed on browser side. |
||
| $cr_callback: function(){}, | ||
|
|
||
| SetCudlrCallbackAndCreateInput: function(callbackPtr){ | ||
| cr_callback = callbackPtr; | ||
|
|
||
| window.cc = function(command) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method invoke |
||
| 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(){ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. click on document with Shift and Alt key to open prompt. |
||
| 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); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In WebGL mode, reset m_output for each command.