2014-05-21 00:17:35 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2013-10-31 23:55:17 +00:00
|
|
|
|
using BizHawk.Client.Common;
|
2017-07-10 04:51:02 +00:00
|
|
|
|
using NLua;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2013-11-03 03:54:37 +00:00
|
|
|
|
namespace BizHawk.Client.EmuHawk
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-06-01 22:02:59 +00:00
|
|
|
|
public sealed class ConsoleLuaLibrary : LuaLibraryBase
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-05-21 00:17:35 +00:00
|
|
|
|
public ConsoleLuaLibrary(Lua lua)
|
|
|
|
|
: base(lua) { }
|
|
|
|
|
|
|
|
|
|
public ConsoleLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
|
|
|
|
: base(lua, logOutputCallback) { }
|
|
|
|
|
|
2017-05-19 14:47:18 +00:00
|
|
|
|
public override string Name => "console";
|
2013-10-29 16:03:06 +00:00
|
|
|
|
|
2018-03-14 01:05:30 +00:00
|
|
|
|
[LuaMethodExample("console.clear( );")]
|
2017-07-10 19:02:00 +00:00
|
|
|
|
[LuaMethod("clear", "clears the output box of the Lua Console window")]
|
2014-01-26 16:15:45 +00:00
|
|
|
|
public static void Clear()
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2017-05-23 18:05:36 +00:00
|
|
|
|
if (GlobalWin.Tools.Has<LuaConsole>())
|
|
|
|
|
{
|
|
|
|
|
GlobalWin.Tools.LuaConsole.ClearOutputWindow();
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 01:05:30 +00:00
|
|
|
|
[LuaMethodExample("local stconget = console.getluafunctionslist( );")]
|
2017-07-10 19:02:00 +00:00
|
|
|
|
[LuaMethod("getluafunctionslist", "returns a list of implemented functions")]
|
2014-01-26 16:15:45 +00:00
|
|
|
|
public static string GetLuaFunctionsList()
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-01-26 16:15:45 +00:00
|
|
|
|
var list = new StringBuilder();
|
2014-06-03 00:34:41 +00:00
|
|
|
|
foreach (var function in GlobalWin.Tools.LuaConsole.LuaImp.Docs)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
list.AppendLine(function.Name);
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-26 16:15:45 +00:00
|
|
|
|
return list.ToString();
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 01:05:30 +00:00
|
|
|
|
[LuaMethodExample("console.log( \"New log.\" );")]
|
2017-07-10 19:02:00 +00:00
|
|
|
|
[LuaMethod("log", "Outputs the given object to the output box on the Lua Console dialog. Note: Can accept a LuaTable")]
|
2014-04-29 05:07:43 +00:00
|
|
|
|
public static void Log(params object[] outputs)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2017-05-23 18:05:36 +00:00
|
|
|
|
if (GlobalWin.Tools.Has<LuaConsole>())
|
|
|
|
|
{
|
|
|
|
|
LogWithSeparator("\t", "\n", outputs);
|
|
|
|
|
}
|
2014-05-01 00:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 01:42:41 +00:00
|
|
|
|
//// Single param version is used by logOutputCallback of some libraries.
|
|
|
|
|
public static void LogOutput(object output)
|
2014-05-01 00:25:14 +00:00
|
|
|
|
{
|
2017-05-23 18:05:36 +00:00
|
|
|
|
if (GlobalWin.Tools.Has<LuaConsole>())
|
|
|
|
|
{
|
|
|
|
|
Log(output);
|
|
|
|
|
}
|
2014-05-01 00:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 01:05:30 +00:00
|
|
|
|
[LuaMethodExample("console.writeline( \"New log line.\" );")]
|
2017-07-10 19:02:00 +00:00
|
|
|
|
[LuaMethod("writeline", "Outputs the given object to the output box on the Lua Console dialog. Note: Can accept a LuaTable")]
|
2014-05-01 00:25:14 +00:00
|
|
|
|
public static void WriteLine(params object[] outputs)
|
|
|
|
|
{
|
2017-05-23 18:05:36 +00:00
|
|
|
|
if (GlobalWin.Tools.Has<LuaConsole>())
|
|
|
|
|
{
|
|
|
|
|
LogWithSeparator("\n", "\n", outputs);
|
|
|
|
|
}
|
2014-05-01 00:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 01:05:30 +00:00
|
|
|
|
[LuaMethodExample("console.write( \"New log message.\" );")]
|
2017-07-10 19:02:00 +00:00
|
|
|
|
[LuaMethod("write", "Outputs the given object to the output box on the Lua Console dialog. Note: Can accept a LuaTable")]
|
2014-05-01 00:25:14 +00:00
|
|
|
|
public static void Write(params object[] outputs)
|
|
|
|
|
{
|
2017-05-10 11:45:23 +00:00
|
|
|
|
LogWithSeparator("", "", outputs);
|
2014-05-01 00:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Outputs the given object to the output box on the Lua Console dialog. Note: Can accept a LuaTable
|
|
|
|
|
private static void LogWithSeparator(string separator, string terminator, params object[] outputs)
|
|
|
|
|
{
|
2017-05-23 18:05:36 +00:00
|
|
|
|
if (!GlobalWin.Tools.Has<LuaConsole>())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 23:26:07 +00:00
|
|
|
|
if (outputs == null)
|
|
|
|
|
{
|
2019-03-18 14:06:37 +00:00
|
|
|
|
GlobalWin.Tools.LuaConsole.WriteToOutputWindow($"(no return){terminator}");
|
2015-10-15 23:26:07 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-19 14:47:18 +00:00
|
|
|
|
|
2014-05-01 00:25:14 +00:00
|
|
|
|
for (var outIndex = 0; outIndex < outputs.Length; outIndex++)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-05-01 00:25:14 +00:00
|
|
|
|
var output = outputs[outIndex];
|
|
|
|
|
|
|
|
|
|
if (outIndex != 0)
|
|
|
|
|
{
|
|
|
|
|
GlobalWin.Tools.LuaConsole.WriteToOutputWindow(separator);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 05:07:43 +00:00
|
|
|
|
if (output == null)
|
|
|
|
|
{
|
2015-10-15 23:30:38 +00:00
|
|
|
|
GlobalWin.Tools.LuaConsole.WriteToOutputWindow("nil");
|
2014-04-29 05:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-04-29 05:07:43 +00:00
|
|
|
|
if (output is LuaTable)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
var lti = output as LuaTable;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2014-04-29 05:07:43 +00:00
|
|
|
|
var keys = (from object key in lti.Keys select key.ToString()).ToList();
|
|
|
|
|
var values = (from object value in lti.Values select value.ToString()).ToList();
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2014-04-29 05:07:43 +00:00
|
|
|
|
var kvps = new List<KeyValuePair<string, string>>();
|
|
|
|
|
for (var i = 0; i < keys.Count; i++)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-04-29 05:07:43 +00:00
|
|
|
|
if (i < values.Count)
|
|
|
|
|
{
|
|
|
|
|
kvps.Add(new KeyValuePair<string, string>(keys[i], values[i]));
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
2014-01-26 16:15:45 +00:00
|
|
|
|
|
2019-10-13 15:50:57 +00:00
|
|
|
|
foreach (var kvp in kvps.OrderBy(x => x.Key))
|
2014-04-29 05:07:43 +00:00
|
|
|
|
{
|
|
|
|
|
sb
|
|
|
|
|
.Append("\"")
|
|
|
|
|
.Append(kvp.Key)
|
|
|
|
|
.Append("\": \"")
|
|
|
|
|
.Append(kvp.Value)
|
|
|
|
|
.Append("\"")
|
|
|
|
|
.AppendLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalWin.Tools.LuaConsole.WriteToOutputWindow(sb.ToString());
|
|
|
|
|
}
|
|
|
|
|
else
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-04-29 05:07:43 +00:00
|
|
|
|
GlobalWin.Tools.LuaConsole.WriteToOutputWindow(output.ToString());
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-19 14:47:18 +00:00
|
|
|
|
|
2014-05-01 00:25:14 +00:00
|
|
|
|
GlobalWin.Tools.LuaConsole.WriteToOutputWindow(terminator);
|
2014-04-29 05:07:43 +00:00
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|