/// <summary> /// Displays help for the LuaPackageDescriptor to the output window. /// </summary> /// <param name="package">The package to display help for.</param> public void DisplayPackageHelp(LuaPackageDescriptor package) { IDictionaryEnumerator functions = package.Functions.GetEnumerator(); string[] results = new string[package.Functions.Count]; int counter = 0; txtResult.Text += package.PackageName + ":\n"; for (int i = 0; i <= package.PackageName.Length; i++) { txtResult.Text += "="; } txtResult.Text += "\n" + package.PackageDocumentation + "\n\n" + "Available commands:\n"; // Display functions within the package while (functions.MoveNext()) { results[counter] = ((LuaFunctionDescriptor)functions.Value).FunctionHeader; counter++; } Array.Sort(results); for (int i = 0; i < counter; i++) { txtResult.Text += results[i] + "\n"; } txtResult.Text += "\n"; }
/// <summary> /// Displays help for the LuaPackageDescriptor to the output window. /// </summary> /// <param name="package">The package to display help for.</param> public void DisplayPackageHelp( LuaPackageDescriptor package ) { IDictionaryEnumerator functions = package.Functions.GetEnumerator(); string[] results = new string[package.Functions.Count]; int counter = 0; txtResult.Text += package.PackageName + ":\n"; for ( int i = 0; i <= package.PackageName.Length; i++ ) txtResult.Text += "="; txtResult.Text += "\n" + package.PackageDocumentation + "\n\n" + "Available commands:\n"; // Display functions within the package while ( functions.MoveNext() ) { results[counter] = ( (LuaFunctionDescriptor) functions.Value ).FunctionHeader; counter++; } Array.Sort( results ); for ( int i = 0; i < counter; i++ ) txtResult.Text += results[i] + "\n"; txtResult.Text += "\n"; }
public void Help() { string[] results; int counter; // Display stand-alone commands if (_lua.Functions.Count > 0) { results = new string[_lua.Functions.Count]; counter = 0; txtResult.Text += "HELP:\n=====\nCommands and packages available through Lua interface.\n" + "\nAvailable commands:\n"; IDictionaryEnumerator funcs = _lua.Functions.GetEnumerator(); while (funcs.MoveNext()) { results[counter] = ((LuaFunctionDescriptor)funcs.Value).FunctionHeader; counter++; } Array.Sort(results); for (int i = 0; i < counter; i++) { txtResult.Text += results[i] + "\n"; } } // Display packages if (_lua.Packages.Count > 0) { results = new string[_lua.Packages.Count]; counter = 0; txtResult.Text += "\nAvailable packages:\n"; IDictionaryEnumerator pkgs = _lua.Packages.GetEnumerator(); while (pkgs.MoveNext()) { LuaPackageDescriptor desc = (LuaPackageDescriptor)pkgs.Value; results[counter] += desc.PackageName + " - " + desc.PackageDocumentation; counter++; } Array.Sort(results); for (int i = 0; i < counter; i++) { txtResult.Text += results[i] + "\n"; } } txtResult.Text += "\n"; }
public static void registerLuaFunctions(string strPackage, object pTarget, string strPkgDoc) { if (((pLuaVM != null) && (pLuaFuncs != null)) && (pLuaPackages != null)) { LuaPackageDescriptor descriptor = null; if (strPackage != null) { pLuaVM.DoString(strPackage + " = {}"); descriptor = new LuaPackageDescriptor(strPackage, strPkgDoc); } Type type = pTarget.GetType(); foreach (MethodInfo info in type.GetMethods()) { foreach (Attribute attribute in Attribute.GetCustomAttributes(info)) { if (attribute.GetType() == typeof(AttrLuaFunc)) { AttrLuaFunc func = (AttrLuaFunc)attribute; ArrayList strParams = new ArrayList(); ArrayList strParamDocs = new ArrayList(); string strFuncName = func.getFuncName(); string strFuncDoc = func.getFuncDoc(); string[] strArray = func.getFuncParams(); ParameterInfo[] parameters = info.GetParameters(); if ((strArray != null) && (parameters.Length != strArray.Length)) { Console.WriteLine(string.Concat(new object[] { "Function ", info.Name, " (exported as ", strFuncName, ") argument number mismatch. Declared ", strArray.Length, " but requires ", parameters.Length, "." })); break; } for (int i = 0; i < parameters.Length; i++) { strParams.Add(parameters[i].Name); strParamDocs.Add(strArray[i]); } LuaFuncDescriptor pFunc = new LuaFuncDescriptor(strFuncName, strFuncDoc, strParams, strParamDocs); if (descriptor != null) { descriptor.AddFunc(pFunc); pLuaVM.RegisterFunction(strPackage + strFuncName, pTarget, info); pLuaVM.DoString(strPackage + "." + strFuncName + " = " + strPackage + strFuncName); pLuaVM.DoString(strPackage + strFuncName + " = nil"); } else { pLuaFuncs.Add(strFuncName, pFunc); pLuaVM.RegisterFunction(strFuncName, pTarget, info); } } } } if (descriptor != null) { pLuaPackages.Add(strPackage, descriptor); } } }
public void help(string strCmd) { if (pLuaFuncs.ContainsKey(strCmd)) { Console.WriteLine(pLuaFuncs[strCmd].getFuncFullDoc()); } else if (strCmd.IndexOf(".") == -1) { if (pLuaPackages.ContainsKey(strCmd)) { ((LuaPackageDescriptor)pLuaPackages[strCmd]).WriteHelp(); } else { Console.WriteLine("No such function or package: " + strCmd); } } else { string[] strArray = strCmd.Split(new char[] { '.' }); if (!pLuaPackages.ContainsKey(strArray[0])) { Console.WriteLine("No such function or package: " + strCmd); } else { LuaPackageDescriptor descriptor3 = (LuaPackageDescriptor)pLuaPackages[strArray[0]]; if (!descriptor3.HasFunc(strArray[1])) { Console.WriteLine("Package " + strArray[0] + " doesn't have a " + strArray[1] + " function."); } else { descriptor3.WriteHelp(strArray[1]); } } } }
/// <summary> /// Registers the Lua-attributed functions in the target object. /// </summary> /// <param name="target">The target to load Lua-attributed functions from.</param> /// <param name="package">The package to register the functions under.</param> /// <param name="packageDocumentation">The documentation for the package.</param> public void RegisterLuaFunctions( object target, string package, string packageDocumentation ) { // Sanity checks if ( target == null || _lua == null || _functions == null || _packages == null ) return; try { LuaPackageDescriptor pPackage = null; Console.WriteLine("Loading Lua library: " + target.ToString()); if ( package != null ) { // Check if the package already exists if ( !_packages.ContainsKey( "package" ) ) { // Create a new package _lua.DoString( package + " = {}" ); pPackage = new LuaPackageDescriptor( package, packageDocumentation ); } else // Access the old package pPackage = (LuaPackageDescriptor) _packages["package"]; } // Get the target type Type targetType = target.GetType(); // ... and simply iterate through all its methods foreach ( MethodInfo info in targetType.GetMethods() ) { // ... then through all this method's attributes foreach ( Attribute attr in Attribute.GetCustomAttributes( info ) ) { // ... and if they happen to be one of our LuaFunctionAttribute attributes if ( attr.GetType() == typeof( LuaFunctionAttribute ) ) { LuaFunctionAttribute luaAttr = (LuaFunctionAttribute) attr; ArrayList paramList = new ArrayList(); ArrayList paramDocs = new ArrayList(); // Get the desired function name and doc string, along with parameter info string fName = luaAttr.FunctionName; string fDoc = luaAttr.FunctionDocumentation; string[] pDocs = luaAttr.FunctionParameters; // Now get the expected parameters from the MethodInfo object ParameterInfo[] pInfo = info.GetParameters(); // If they don't match, someone forgot to add some documentation to the // attribute, complain and go to the next method if ( pDocs != null && ( pInfo.Length != pDocs.Length ) ) { Console.WriteLine( "Function " + info.Name + " (exported as " + fName + ") argument number mismatch. Declared " + pDocs.Length + " but requires " + pInfo.Length + "." ); break; } // Build a parameter <-> parameter doc hashtable for ( int i = 0; i < pInfo.Length; i++ ) { paramList.Add( pInfo[i].Name ); paramDocs.Add( pDocs[i] ); } // Get a new function descriptor from this information LuaFunctionDescriptor func = new LuaFunctionDescriptor( fName, fDoc, paramList, paramDocs ); if ( pPackage != null ) { // Check if the package already contains the function if ( !pPackage.Functions.ContainsKey( fName ) ) { // Add the new package function pPackage.AddFunction( func ); _lua.RegisterFunction( package + fName, target, info ); _lua.DoString( package + "." + fName + " = " + package + fName ); _lua.DoString( package + fName + " = nil" ); } } else { // Check if the function has already been loaded if ( !_functions.ContainsKey( fName ) ) { // Add it to the global hashtable _functions.Add( fName, func ); // And tell the VM to register it _lua.RegisterFunction( fName, target, info ); } } } } } if ( pPackage != null && !_packages.ContainsKey( package ) ) _packages.Add( package, pPackage ); } catch ( Exception e ) { } finally { } }
public void Help(string command) { // Check if the command is a function name if (_lua.Functions.Contains(command)) { LuaFunctionDescriptor func = (LuaFunctionDescriptor)_lua.Functions[command]; // Display basic help message txtResult.Text += "Help for " + command + ":\n========="; for (int i = 0; i <= command.Length; i++) { txtResult.Text += "="; } txtResult.Text += "\n" + func.FunctionFullDocumentation + "\n\n"; return; } // Check if the command is a package name if (command.IndexOf(".") == -1) { // Check if the package exists if (_lua.Packages.ContainsKey(command)) { LuaPackageDescriptor pkg = (LuaPackageDescriptor)_lua.Packages[command]; DisplayPackageHelp(pkg); return; } else { txtResult.Text += "No such function or package: " + command + "\n\n"; return; } } // Determine the path to the function name string[] parts = command.Split('.'); // Check if the package exists if (!_lua.Packages.ContainsKey(parts[0])) { txtResult.Text += "No such function or package: " + command + "\n\n"; return; } LuaPackageDescriptor desc = (LuaPackageDescriptor)_lua.Packages[parts[0]]; // Check if the function exists within the package if (!desc.HasFunction(parts[1])) { txtResult.Text += "Package " + parts[0] + " doesn't have a " + parts[1] + " function.\n\n"; return; } // Display basic help message txtResult.Text += "Help for " + command + ":\n========="; for (int i = 0; i <= command.Length; i++) { txtResult.Text += "="; } txtResult.Text += "\n" + desc.PackageName + "." + desc.WriteHelp(parts[1]) + "\n\n"; }