2014-02-03 20:48:01 +00:00
using System.Collections.Generic ;
2014-06-03 02:19:13 +00:00
using System.IO ;
2012-07-11 15:03:51 +00:00
using System.Linq ;
2014-01-25 20:27:51 +00:00
using System.Reflection ;
2012-07-11 15:03:51 +00:00
using System.Text ;
2016-02-07 13:27:25 +00:00
using Newtonsoft.Json ;
2012-07-11 15:03:51 +00:00
2013-10-31 23:55:17 +00:00
namespace BizHawk.Client.Common
2012-07-11 15:03:51 +00:00
{
2014-06-03 00:34:41 +00:00
public class LuaDocumentation : List < LibraryFunction >
2013-10-31 00:31:25 +00:00
{
2014-06-03 00:34:41 +00:00
public LuaDocumentation ( )
: base ( ) { }
2014-06-03 02:19:13 +00:00
2014-06-05 00:23:05 +00:00
public string ToTASVideosWikiMarkup ( )
2014-06-03 02:19:13 +00:00
{
var sb = new StringBuilder ( ) ;
sb
. AppendLine ( "[module:ListParents]" )
. AppendLine ( )
. AppendLine ( "This page documents the the behavior and parameters of Lua functions available for the [BizHawk] emulator." )
2015-03-23 22:22:02 +00:00
. AppendLine ( "__This is an autogenerated page, do not edit__" )
2014-06-03 02:19:13 +00:00
. AppendLine ( )
. AppendLine ( ) ;
2016-11-11 17:27:06 +00:00
sb . AppendLine ( @ "All type names represent the standard .NET types of the same name. Except for func which represents a lua function and table which represents a lua table. For more information on .NET types can be found in MSDN documentation.
__Types and notation__
* ? ( question mark )
* * A question mark next to a value indicates that it is a Nullable type ( only applies to types that are not normally nullable )
* [ [ ] ] ( brackets )
* * Brackets around a parameter indicate that the parameter is optional . optional parameters have an equals sign followed by the value that will be used if no value is supplied .
* * Brackets after a parameter type indicate it is an array
* null
* * null is equivalent to the lua nil
* Color
* * This is a . NET System . Drawing . Color struct . The value passed from lua is any value acceptable in the Color constructor . This means either a string with the color name such as "" red "" , or a 0xAA RRGGBB integer value . Unless specified , this is not a nullable value
* object
* * A System . Object , literally anything qualifies for this parameter . However , the context of particular function may suggest a narrower range of useful values .
* luaf
* * A Lua function . Note that these are always parameters , and never return values of a call
* table
* * A standard Lua table
");
2014-06-03 02:19:13 +00:00
foreach ( var library in this . Select ( x = > new { Name = x . Library , Description = x . LibraryDescription } ) . Distinct ( ) )
{
sb
. AppendFormat ( "%%TAB {0}%%" , library . Name )
. AppendLine ( )
. AppendLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( library . Description ) )
{
sb
. Append ( library . Description )
. AppendLine ( )
. AppendLine ( ) ;
}
foreach ( var func in this . Where ( x = > x . Library = = library . Name ) )
{
sb
. AppendFormat ( "__{0}.{1}__%%%" , func . Library , func . Name )
. AppendLine ( ) . AppendLine ( )
. AppendFormat ( "* {0} {1}.{2}{3}" , func . ReturnType , func . Library , func . Name , func . ParameterList )
. AppendLine ( ) . AppendLine ( )
. AppendFormat ( "* {0}" , func . Description )
. AppendLine ( ) . AppendLine ( ) ;
}
}
2014-06-14 17:04:54 +00:00
sb . Append ( "%%TAB_END%%" ) ;
2014-06-05 00:23:05 +00:00
return sb . ToString ( ) ;
2014-06-03 02:19:13 +00:00
}
2016-02-07 13:27:25 +00:00
private class SublimeCompletions
{
public SublimeCompletions ( )
{
2016-02-15 03:35:47 +00:00
Scope = "source.lua - string" ;
2016-02-07 13:27:25 +00:00
}
[JsonProperty(PropertyName = "scope")]
public string Scope { get ; set ; }
[JsonProperty(PropertyName = "completions")]
public List < Completion > Completions = new List < Completion > ( ) ;
public class Completion
{
[JsonProperty(PropertyName = "trigger")]
public string Trigger { get ; set ; }
[JsonProperty(PropertyName = "contents")]
public string Contents { get ; set ; }
}
}
public string ToSublime2CompletionList ( )
{
var sc = new SublimeCompletions ( ) ;
foreach ( var f in this . OrderBy ( lf = > lf . Library ) . ThenBy ( lf = > lf . Name ) )
{
var completion = new SublimeCompletions . Completion
{
Trigger = f . Library + "." + f . Name
} ;
var sb = new StringBuilder ( ) ;
if ( f . ParameterList . Any ( ) )
{
sb
. Append ( string . Format ( "{0}.{1}(" , f . Library , f . Name ) ) ;
var parameters = f . Method . GetParameters ( )
. ToList ( ) ;
for ( int i = 0 ; i < parameters . Count ; i + + )
{
sb
. Append ( "${" )
. Append ( i + 1 )
. Append ( ":" ) ;
if ( parameters [ i ] . IsOptional )
{
sb . Append ( string . Format ( "[{0}]" , parameters [ i ] . Name ) ) ;
}
else
{
sb . Append ( parameters [ i ] . Name ) ;
}
sb . Append ( "}" ) ;
if ( i < parameters . Count - 1 )
{
sb . Append ( "," ) ;
}
}
sb . Append ( ")" ) ;
}
else
{
sb . Append ( string . Format ( "{0}.{1}()" , f . Library , f . Name ) ) ;
}
completion . Contents = sb . ToString ( ) ;
sc . Completions . Add ( completion ) ;
}
return JsonConvert . SerializeObject ( sc ) ;
}
public string ToNotepadPlusPlusAutoComplete ( )
{
return string . Empty ; // TODO
}
2013-10-31 00:31:25 +00:00
}
2014-06-03 00:34:41 +00:00
public class LibraryFunction
2012-07-11 15:03:51 +00:00
{
2014-06-04 00:22:44 +00:00
private readonly LuaMethodAttributes _luaAttributes ;
private readonly MethodInfo _method ;
2012-07-23 01:02:04 +00:00
2014-06-04 00:22:44 +00:00
public LibraryFunction ( string library , string libraryDescription , MethodInfo method )
2012-07-23 02:24:48 +00:00
{
2014-06-04 00:22:44 +00:00
_luaAttributes = method . GetCustomAttributes ( typeof ( LuaMethodAttributes ) , false )
. First ( ) as LuaMethodAttributes ;
_method = method ;
2014-02-03 20:48:01 +00:00
2014-06-04 00:22:44 +00:00
Library = library ;
LibraryDescription = libraryDescription ;
}
2013-12-30 01:58:44 +00:00
2014-06-04 00:22:44 +00:00
public string Library { get ; private set ; }
public string LibraryDescription { get ; private set ; }
2014-01-25 20:04:26 +00:00
2016-02-07 13:27:25 +00:00
public MethodInfo Method { get { return _method ; } }
2014-06-04 00:22:44 +00:00
public string Name
{
get { return _luaAttributes . Name ; }
2014-06-03 00:34:41 +00:00
}
2014-01-25 20:27:51 +00:00
2014-06-04 00:22:44 +00:00
public string Description
{
get { return _luaAttributes . Description ; }
}
2014-01-25 20:04:26 +00:00
2014-06-04 01:23:50 +00:00
private string _paramterList = null ;
2014-06-03 00:34:41 +00:00
public string ParameterList
{
get
2012-07-12 00:57:09 +00:00
{
2014-06-04 01:23:50 +00:00
if ( _paramterList = = null )
2012-07-12 00:57:09 +00:00
{
2014-06-04 01:23:50 +00:00
var parameters = _method . GetParameters ( ) ;
var list = new StringBuilder ( ) ;
list . Append ( '(' ) ;
for ( var i = 0 ; i < parameters . Length ; i + + )
2012-07-12 00:57:09 +00:00
{
2016-11-11 17:40:53 +00:00
var p = TypeCleanup ( parameters [ i ] . ToString ( ) ) ;
2014-06-04 01:23:50 +00:00
if ( parameters [ i ] . IsOptional )
{
var def = parameters [ i ] . DefaultValue ! = null ? parameters [ i ] . DefaultValue . ToString ( ) : "null" ;
list . AppendFormat ( "[{0} = {1}]" , p , def ) ;
}
else
{
list . Append ( p ) ;
}
if ( i < parameters . Length - 1 )
{
list . Append ( ", " ) ;
}
2012-07-12 00:57:09 +00:00
}
2013-12-30 01:58:44 +00:00
2014-06-04 01:23:50 +00:00
list . Append ( ')' ) ;
_paramterList = list . ToString ( ) ;
}
2014-01-25 20:27:51 +00:00
2014-06-04 01:23:50 +00:00
return _paramterList ;
2012-07-12 00:57:09 +00:00
}
2014-06-03 00:34:41 +00:00
}
2012-07-12 00:57:09 +00:00
2016-11-11 17:40:53 +00:00
private string TypeCleanup ( string str )
{
return str
. Replace ( "System" , string . Empty )
. Replace ( " " , string . Empty )
. Replace ( "." , string . Empty )
. Replace ( "LuaInterface" , string . Empty )
. Replace ( "Object[]" , "object[] " )
. Replace ( "Object" , "object " )
. Replace ( "Nullable`1[Boolean]" , "bool? " )
. Replace ( "Boolean[]" , "bool[] " )
. Replace ( "Boolean" , "bool " )
. Replace ( "String" , "string " )
. Replace ( "LuaTable" , "table " )
. Replace ( "LuaFunction" , "func " )
. Replace ( "Nullable`1[Int32]" , "int? " )
. Replace ( "Nullable`1[UInt32]" , "uint? " )
. Replace ( "Byte" , "byte " )
. Replace ( "Int16" , "short " )
. Replace ( "Int32" , "int " )
. Replace ( "Int64" , "long " )
. Replace ( "Ushort" , "ushort " )
. Replace ( "Ulong" , "ulong " )
. Replace ( "UInt32" , "uint " )
. Replace ( "UInt64" , "ulong " )
. Replace ( "Double" , "double " )
. Replace ( "Uint" , "uint " )
. Replace ( "Nullable`1[DrawingColor]" , "Color? " )
. Replace ( "DrawingColor" , "Color " )
. ToLower ( ) ;
}
2014-06-03 00:34:41 +00:00
public string ReturnType
{
get
2012-07-12 00:57:09 +00:00
{
2014-06-04 00:22:44 +00:00
var returnType = _method . ReturnType . ToString ( ) ;
2016-11-11 17:40:53 +00:00
return TypeCleanup ( returnType ) . Trim ( ) ;
2012-07-12 00:57:09 +00:00
}
2012-07-11 15:03:51 +00:00
}
}
}