• Log in or sign up for an account here: https://www.sythe.org/login

Simple Console using Lua and Reflection

Swan

When They Cry...
Joined
Jan 23, 2007
Sythe Gold
0
Messages
4,957
Age
32
Location
Australia
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:
u6m4j.png


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;
            }
        }
    }
}
 
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?
 
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?
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.
 
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?
 
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?
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.
 
Runescape Gamblingrunescape gambling
Back
Top