Exemplo n.º 1
0
 void A8ConsoleEvent(A8Console.Event e, string txt)
 {
     if (e != A8Console.Event.CLEAR)
     {
         lock (this)
         {
             sendCache.Append(txt + ((e == A8Console.Event.WRITELINE) ? "\n" : ""));
         }
     }
 }
        /// <summary>
        /// Loads a dll or exe and checks for Lua libraries
        /// </summary>
        /// <param name="FileName"></param>
        public static string[] Load(string FileName)
        {
            List <string> modules = new List <string>();

            try {
                //Create a new assembly from the plugin file we're adding..
                Assembly pluginAssembly = Assembly.LoadFrom(FileName);
                //Next we'll loop through all the Types found in the assembly
                if (pluginAssembly != null)
                {
                    foreach (Type pluginType in pluginAssembly.GetTypes())
                    {
                        //Only look at public types
                        if (pluginType.IsPublic)
                        {
                            //Only look at non-abstract types
                            if (!pluginType.IsAbstract)
                            {
                                //Gets a type object of the interface we need the plugins to match
                                Type typeInterface = pluginType.GetInterface("SharpLua.Library.ILuaLibrary", true);

                                //Make sure the interface we want to use actually exists
                                if ((typeInterface != null))
                                {
                                    try {
                                        Library.ILuaLibrary lib = (Library.ILuaLibrary)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                                        //Call initialization for the plugin
                                        lib.RegisterModule(LuaRuntime.GlobalEnvironment);
                                        modules.Add(lib.ModuleName);
                                    } catch (Exception ex) {
                                        A8Console.WriteLine("Error: " + ex.ToString());
                                    }
                                }
                                else
                                {
                                }
                                typeInterface = null;
                                // Clean up
                            }
                        }
                    }
                }
                if (pluginAssembly == null)
                {
                    throw new Exception("Empty Assembly!");
                }
                pluginAssembly = null;
                //more cleanup
            } catch (Exception) {
            }
            return(modules.ToArray());
        }
Exemplo n.º 3
0
 static void A8ConsoleEvent(A8Console.Event e, string txt)
 {
     if (instance != null)
     {
         instance.logPos = new Vector2(0, float.PositiveInfinity);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Prints the SharpLua Banner
 /// </summary>
 public static void PrintBanner()
 {
     A8Console.WriteLine("SharpLua, Copyright (C) 2011-2012 mlnlover11 Productions");
     //A8Console.WriteLine(Prompt);
 }
Exemplo n.º 5
0
        /// <summary>
        /// A REPL (Read, Eval, Print, Loop function)
        /// </summary>
        /// <param name="args">Application startup args</param>
        public static void REPL(string[] args)
        {
            // Create global variables
            GlobalEnvironment = LuaRuntime.CreateGlobalEnviroment();
            PrintBanner();
            // how to handle errors
            #if DEBUG
            GlobalEnvironment.SetNameValue("DEBUG", LuaBoolean.True);
            #else
            GlobalEnvironment.SetNameValue("DEBUG", LuaBoolean.False);
            #endif

            Prompt = "> ";
            // load startup scripts
            LoadFiles();

            // check command line args
            if (args.Length > 0)
            {
                string file = args[0];
                if (File.Exists <LuaRuntime>(file))
                {
                    try
                    {
                        GlobalEnvironment.SetNameValue("_WORKDIR", new LuaString(file));
                        if (file.EndsWith(".out") || file.EndsWith(".luac") || file.EndsWith(".sluac"))
                        {
                            Chunk c = Serializer.Deserialize(file) as Chunk;
                            c.Enviroment = GlobalEnvironment;
                            bool isBreak;
                            c.Execute(GlobalEnvironment, out isBreak).ToString();
                        }
                        else
                        {
                            LuaRuntime.RunFile(file, GlobalEnvironment);
                        }
                        // it loaded successfully
                        if (args.Length > 1) // possibly interactive mode after
                        {
                            A8Console.WriteLine("Loaded file '" + file + "'");
                        }
                    }
                    catch (Exception error)
                    {
                        A8Console.WriteLine(error.Message);
                    }
                    //Console.ReadKey(true);
                    //return;
                    // instead, go to interactive
                }
                else
                {
                    A8Console.WriteLine(file + " not found.");
                }
            }

            // check for interactive mode
            foreach (string arg in args)
            {
                if (arg.ToUpper() == "-I")
                {
                    GoInteractive = true;
                }
            }

            if (args.Length == 0)
            {
                GoInteractive = true;
            }

            if (GoInteractive)
            {
                while (true)
                {
                    A8Console.Write(Prompt);
                    string line = Console.ReadLine();

                    if (line == "quit" || line == "exit" || line == "bye")
                    {
                        break;
                    }
                    else
                    {
                        try
                        {
                            LuaValue v = Run(line, GlobalEnvironment);
                            if (v == null)
                            {
                                A8Console.WriteLine("=> [no returned value]");
                            }
                            else
                            {
                                A8Console.WriteLine("=> " + v.ToString());
                            }
                        }
                        catch (Parser.ParserException error)
                        {
                            // create spacing
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < Prompt.Length; i++)
                            {
                                sb.Append(" ");
                            }
                            for (int i = 0; i < error.FirstErrorIndex; i++)
                            {
                                sb.Append(" ");
                            }

                            A8Console.WriteLine(sb.ToString() + "^");
                            A8Console.WriteLine("Error: " + error.Message);
                            GlobalEnvironment.SetNameValue("LASTERROR", ObjectToLua.ToLuaValue(error));
                        }
                        catch (Exception error)
                        {
                            if (((LuaBoolean)GlobalEnvironment.GetValue(GlobalEnvironment.GetKey("DEBUG"))) == LuaBoolean.True)
                            {
                                A8Console.WriteLine(error.ToString());
                            }
                            else
                            {
                                A8Console.WriteLine("Error: " + error.Message);
                            }
                            GlobalEnvironment.SetNameValue("LASTERROR", ObjectToLua.ToLuaValue(error));
                        }
                    }
                }
            }
        }