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
32 changes: 21 additions & 11 deletions CUDLR/Examples/GameObjectExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@
using System.Collections.Generic;
using System.Reflection;
using System.Net;
using System;

/**
* Example console commands for getting information about GameObjects
*/
public static class GameObjectCommands {

[CUDLR.Command("object list", "lists all the game objects in the scene")]
[CUDLR.Command("object list", "lists all the game objects in the scene", "object")]
public static void ListGameObjects() {
UnityEngine.Object[] objects = UnityEngine.Object.FindObjectsOfType(typeof(GameObject));
foreach (UnityEngine.Object obj in objects) {
CUDLR.Console.Log(obj.name);
Action<Transform, int> deepList = (Transform xform, int indentLevel) => {
string listing = "- " + xform.name;
CUDLR.Console.Log(listing.PadLeft(listing.Length + indentLevel * 2));

int childCount = xform.childCount;
for (int child = 0; child < childCount; child++)
deepList(xform.GetChild(child), indentLevel + 1);
};

foreach (Transform xform in GameObject.FindObjectsOfType(typeof(Transform))) {
if (xform.parent == null)
deepList(xform, 0);
}
}

[CUDLR.Command("object print", "lists properties of the object")]
[CUDLR.Command("object print", "lists properties of the object", "object")]
public static void PrintGameObject(string[] args) {
if (args.Length < 1) {
CUDLR.Console.Log( "expected : object print <Object Name>" );
CUDLR.Console.Log("expected: object print <Object Name>");
return;
}

GameObject obj = GameObject.Find( args[0] );
if (obj == null) {
CUDLR.Console.Log("GameObject not found : "+args[0]);
CUDLR.Console.Log("GameObject not found: "+args[0]);
} else {
CUDLR.Console.Log("Game Object : "+obj.name);
CUDLR.Console.Log("Game Object: "+obj.name);
foreach (Component component in obj.GetComponents(typeof(Component))) {
CUDLR.Console.Log(" Component : "+component.GetType());
CUDLR.Console.Log(" + Component: "+component.GetType());
foreach (FieldInfo f in component.GetType().GetFields()) {
CUDLR.Console.Log(" "+f.Name+" : "+f.GetValue(component));
CUDLR.Console.Log(" - "+f.Name+": "+f.GetValue(component));
}
}
}
Expand All @@ -47,7 +57,7 @@ public static void PrintGameObject(string[] args) {
// FIXME need main thread support

public static class GameObjectRoutes {

[CUDLR.Route("^/object/list.json$")]
public static bool ListGameObjects(HttpListenerContext context) {
string json = "[";
Expand Down
4 changes: 3 additions & 1 deletion CUDLR/Scripts/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ public class CommandAttribute : Attribute
public delegate void CallbackSimple();
public delegate void Callback(string[] args);

public CommandAttribute(string cmd, string help, bool runOnMainThread = true)
public CommandAttribute(string cmd, string help, string cat, bool runOnMainThread = true)
{
m_command = cmd;
m_help = help;
m_cat = cat;
m_runOnMainThread = runOnMainThread;
}

public string m_command;
public string m_help;
public string m_cat;
public bool m_runOnMainThread;
public Callback m_callback;
}
Expand Down
64 changes: 47 additions & 17 deletions CUDLR/Scripts/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct QueuedCommand {

public class Console {

// Max number of lines in the console output
// Max number of lines in the buffer
const int MAX_LINES = 100;

// Maximum number of commands stored in the history
Expand Down Expand Up @@ -71,15 +71,27 @@ public static void Run(string str) {
}
}

/* Clear all output from console */
[Command("clear", "clears console output", false)]
public static void Clear() {
Instance.m_output.Clear();
}

/* Print a list of all console commands */
[Command("help", "prints commands", false)]
[Command("help", "prints commands", "basic", false)]
public static void Help() {
if (string.IsNullOrEmpty(Instance.m_help)) {
var commands = Instance.m_commands.ToList();
commands.Sort(delegate(CommandAttribute a, CommandAttribute b) {
return a.m_cat.CompareTo(b.m_cat);
});

string help = "";
string cat = null;
foreach (var cmd in commands) {
if (cmd.m_cat != cat) {
help += "\n\n Category: " + cmd.m_cat;
cat = cmd.m_cat;
}
help += string.Format("\n {0} : {1}", cmd.m_command, cmd.m_help);
}
Instance.m_help = help;
}

Log( string.Format("Commands:{0}", Instance.m_help));
}

Expand All @@ -96,7 +108,7 @@ public static void LogCommand(string cmd) {
/* Logs string to output */
public static void Log(string str) {
Instance.m_output.Add(str);
if (Instance.m_output.Count > MAX_LINES)
if (Instance.m_output.Count > MAX_LINES)
Instance.m_output.RemoveAt(0);
}

Expand All @@ -110,20 +122,23 @@ public static void LogCallback (string logString, string stackTrace, LogType typ

/* Returns the output */
public static string Output() {
return string.Join("\n", Instance.m_output.ToArray());
string output = string.Join("\n", Instance.m_output.ToArray());
if (!string.IsNullOrEmpty(output))
output += "\n";
Instance.m_output.Clear();
return output;
}

/* Register a new console command */
public static void RegisterCommand(string command, string desc, CommandAttribute.Callback callback, bool runOnMainThread = true) {
public static void RegisterCommand(string command, string desc, string cat, CommandAttribute.Callback callback, bool runOnMainThread = true) {
if (command == null || command.Length == 0) {
throw new Exception("Command String cannot be empty");
}

CommandAttribute cmd = new CommandAttribute(command, desc, runOnMainThread);
CommandAttribute cmd = new CommandAttribute(command, desc, cat, runOnMainThread);
cmd.m_callback = callback;

Instance.m_commands.Add(cmd);
Instance.m_help += string.Format("\n{0} : {1}", command, desc);
}

private void RegisterAttributes() {
Expand Down Expand Up @@ -157,10 +172,9 @@ private void RegisterAttributes() {
Debug.LogError(string.Format("Method {0}.{1} needs a valid command name.", type, method.Name));
continue;
}

cmd.m_callback = cb;
m_commands.Add(cmd);
m_help += string.Format("\n{0} : {1}", cmd.m_command, cmd.m_help);
}
}
}
Expand All @@ -174,7 +188,7 @@ public static string PreviousCommand(int index) {
/* Update history with a new command */
private void RecordCommand(string command) {
m_history.Insert(0, command);
if (m_history.Count > MAX_HISTORY)
if (m_history.Count > MAX_HISTORY)
m_history.RemoveAt(m_history.Count - 1);
}

Expand Down Expand Up @@ -230,6 +244,22 @@ public CommandTree() {
m_subcommands = new Dictionary<string, CommandTree>();
}

public List<CommandAttribute> ToList() {
HashSet<CommandAttribute> set = new HashSet<CommandAttribute>();
_toList(set, this);
return set.ToList();
}

private void _toList(HashSet<CommandAttribute> set, CommandTree tree) {
if (tree.m_command != null) {
set.Add(tree.m_command);
return;
}
var subcmds = tree.m_subcommands.Values.ToArray();
foreach (CommandTree childTree in subcmds)
tree._toList(set, childTree);
}

public void Add(CommandAttribute cmd) {
_add(cmd.m_command.ToLower().Split(' '), 0, cmd);
}
Expand Down Expand Up @@ -334,4 +364,4 @@ private void RunCommand(string[] args) {
}
}
}
}
}
9 changes: 6 additions & 3 deletions StreamingAssets/CUDLR/console.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
textarea {resize:none;}
textarea {
resize:none;
}

body.console {
background-color:black;
}

textarea.console {
width:100%;
background-color:#383838;
color:#F0F0F0;
font-size:14px;
font-family:;"Courier New", Courier, monospace;

font-family:"Courier New", Courier, monospace;
}

#output {
height:500px;
}
11 changes: 9 additions & 2 deletions StreamingAssets/CUDLR/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@
// Check if we are scrolled to the bottom to force scrolling on update
var output = $('#output');
shouldScroll = (output[0].scrollHeight - output.scrollTop()) == output.innerHeight();
output.val(String(data));
var current = output.val();
output.val(current + String(data));
if (callback) callback();
if (shouldScroll) scrollBottom();
});
}

function resetOutput() {
$('#output').val("");
scrollBottom();
}

function resetInput() {
commandIndex = -1;
$("#input").val("");
Expand All @@ -52,7 +58,7 @@
}

function updateCommand(index) {
// Check if we are at the defualt index and clear the input
// Check if we are at the default index and clear the input
if (index < 0) {
resetInput();
return;
Expand Down Expand Up @@ -82,6 +88,7 @@
<body class="console">
<textarea id="output" class="console" readOnly></textarea>
<textarea id="input" class="console" autofocus rows="1"></textarea>
<button type="button" onclick="javascript:resetOutput();">Clear Console</button>

<script>
$("#input").keydown( function (e) {
Expand Down