Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions CUDLR/Scripts/Console.cs
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 {

Expand Down Expand Up @@ -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();
Copy link
Author

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.

#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() {
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In WebGL mode, debug logs are already shown in browser console.
So I disable log callback to avoid duplicate output.

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);
}
}

Expand Down Expand Up @@ -174,7 +202,7 @@ private void RegisterAttributes() {
}
}
}
}
}
}

/* Get a previously ran command from the history */
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions CUDLR/Scripts/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The 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();
Expand All @@ -77,6 +84,7 @@ public virtual void Awake() {
listener.BeginGetContext(ListenerCallback, null);

StartCoroutine(HandleRequests());
#endif
}

public void OnApplicationPause(bool paused) {
Expand Down Expand Up @@ -198,6 +206,7 @@ static void RegisterFileHandlers() {
registeredRoutes.Add(fileRoute);
}

#if !UNITY_WEBGL || UNITY_EDITOR
void OnEnable() {
if (RegisterLogCallback) {
// Capture Console Logs
Expand All @@ -218,6 +227,7 @@ void OnDisable() {
#endif
}
}
#endif

void Update() {
Console.Update();
Expand Down
54 changes: 54 additions & 0 deletions CUDLR/Scripts/WebGL_Relay.cs
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
Copy link
Author

Choose a reason for hiding this comment

The 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
}
}
37 changes: 37 additions & 0 deletions WebGL/CUDLR_Relay.jslib
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var CUDLR_Relay = {
Copy link
Author

Choose a reason for hiding this comment

The 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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method invoke CUDLR.Console.Run() and output result on browser console.
You can also type like cc("object list") in the broser console command line to Run it.

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(){
Copy link
Author

@ShinodaNaoki ShinodaNaoki Feb 12, 2018

Choose a reason for hiding this comment

The 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);