Simple Console using Lua and Reflection

Discussion in 'Programming General' started by Swan, Dec 9, 2009.

Simple Console using Lua and Reflection
  1. Unread #1 - Dec 9, 2009 at 4:47 AM
  2. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Simple Console using Lua and Reflection

    Is as the title says it is. Download LuaInterface from Here and add it to your project references.

    I made this out of boredom in a few minutes because I could. If you wanted to you could likely make a text-based game or something similar.

    Screenshot:
    [​IMG]

    Code:
    using System;
    using System.Reflection;
    using LuaInterface;
    
    namespace LuaConsole
    {
        class Program
        {
            private static bool running = true;
            private static Lua lSEngine = new Lua();
    
            static void Main(string[] args)
            {
                Console.WriteLine("Basic Lua Console\nType Help() for a list of commands.\n---\n");
    
                Program program = new Program();
                
                foreach (MethodInfo mInfo in program.GetType().GetMethods())
                {
                    LuaFunctionAttribute[] FunctionAttributes = (LuaFunctionAttribute[])mInfo.GetCustomAttributes(typeof(LuaFunctionAttribute), false);
                    foreach(LuaFunctionAttribute fAttribute in FunctionAttributes)
                    {
                        lSEngine.RegisterFunction(fAttribute.Name, program, mInfo);
                    }
                }
    
                do
                {
                    try
                    {
                        Console.Write(">");
                        string input = Console.ReadLine();
                        lSEngine.DoString(input);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("{0} - {1}", e.GetType().Name, e.Message);
                    }
                } while (running);
            }
    
            [LuaFunctionAttribute("Print", "Prints text to the console.")]
            [LuaFunctionAttribute("Write", "Prints text to the console.")]
            public void Print(string text)
            {
                Console.Write(text);
            }
            [LuaFunctionAttribute("PrintLine", "Prints text followed by a newline.")]
            [LuaFunctionAttribute("WriteLine", "Prints text followed by a newline.")]
            public void PrintLine(string text)
            {
                Console.WriteLine(text);
            }
    
            [LuaFunctionAttribute("Quit", "Quits the program.")]
            public void Quit()
            {
                running = false;
            }
    
            [LuaFunctionAttribute("Help", "Prints a list of commands.")]
            public void PrintHelp()
            {
                Console.WriteLine("Here is a list of commands you may use:\n---");
                foreach (MethodInfo mInfo in this.GetType().GetMethods())
                {
                    LuaFunctionAttribute[] Attributes = (LuaFunctionAttribute[])mInfo.GetCustomAttributes(typeof(LuaFunctionAttribute), false);
                    foreach(LuaFunctionAttribute fAttribute in Attributes)
                    {
                        Console.Write("{0}(", fAttribute.Name);
                        if (mInfo.GetParameters().Length > 0)
                        {
                            foreach (ParameterInfo arg in mInfo.GetParameters())
                            {
                                Console.Write("{0} {1},", arg.ParameterType.Name, arg.Name);
                            }
                            Console.Write('\b');
                        }
                        Console.WriteLine(") - {0}", fAttribute.Description);
                    }
                }
            }
    
            [LuaFunctionAttribute("LoadScript", "Loads a Lua script to run.")]
            public void LoadScript(string path)
            {
                try
                {
                    lSEngine.DoFile(path);   
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
                }
            }
        }
    
     
    
        [AttributeUsage(AttributeTargets.Method, AllowMultiple=true)]
        class LuaFunctionAttribute : Attribute
        {
            private string fName;
            private string fDescription;
    
            public LuaFunctionAttribute(string Name, string Description)
            {
                this.fName = Name;
                this.fDescription = Description;
            }
    
            public string Name
            {
                get
                {
                    return this.fName;
                }
            }
            public string Description
            {
                get
                {
                    return this.fDescription;
                }
            }
        }
    }
    
     
  3. Unread #2 - Dec 12, 2009 at 10:38 AM
  4. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    Simple Console using Lua and Reflection

    cool idea; however, it would be even cooler if you wrote the interpreter yourself :) what's so hard about lexers, parsers, and AST walkers?
    edit:
    why not:
    Code:
    ...
    public LuaFunctionAttribute(List<string> or maybe string[] names, string description) {
        ...
    }
    ...
    too? that way you could do stuff like:
    Code:
            [LuaFunctionAttribute({ "Print", "Write }, "Prints text to the console.")]
    
    as opposed to... uh... 'marking' the method with it twice?
     
  5. Unread #3 - Dec 12, 2009 at 8:00 PM
  6. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Simple Console using Lua and Reflection

    I used Lua because Lua is an easily implemented and fully capable scripting language. It saved time.

    Giving the names in an array could work I guess, I just found it easier at the time, considering there isn't any REAL point to using multiple names for the same function, I just felt like allowing that.
     
  7. Unread #4 - Dec 13, 2009 at 11:10 AM
  8. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    Simple Console using Lua and Reflection

    i didn't question WHY you used lua; i questioned WHY you used a pre-existing interpreter for it :) i get it... don't reinvent the wheel; however, that shouldn't get in the way of a cool learning experience. i mean, you've never written an interpreter or compiler, have you?
     
  9. Unread #5 - Dec 13, 2009 at 7:45 PM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Simple Console using Lua and Reflection

    Now I understand you.

    I used a pre-existing interpreter because I didn't see the point if one was already there ... And if anything else, I was lazy.
     
  11. Unread #6 - Jul 3, 2010 at 4:34 AM
  12. TheKraken
    Joined:
    Jul 3, 2010
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    TheKraken Member

    Simple Console using Lua and Reflection

    coool
     
< need help plsss :( | Help with TryParse >

Users viewing this thread
1 guest


 
 
Adblock breaks this site