Ejemplo n.º 1
0
 public bool Resume(LuaValue[] args)
 {
     if (thread == null)
     {
         thread = new Thread(new ThreadStart(delegate()
         {
             try {
                 _status = "running";
                 func.Invoke(args);
                 _status = "dead";
                 Running = null;
             } catch (Exception e) {
                 A8Console.WriteLine("Coroutine exception: " + e.GetType().Name + " - " + e.Message + "\n" + e.StackTrace);
                 _status = "dead";
             }
         }));
         //thread.SetApartmentState(ApartmentState.MTA);
         thread.Start();
     }
     else
     {
         if (_status == "dead")
         {
             throw new Exception("Error: coroutine is dead, it cannot be resumed!");
         }
         try {
             if (_status == "suspended")
             {
                 lock (thread)
                 {
                     Monitor.Pulse(thread);
                 }
             }
             else
             {
                 thread.Start();
             }
             _status = "running";
         } catch (Exception ex) {
             _status = "dead";
             throw ex;
         }
     }
     Running = this;
     return(true);
 }
Ejemplo n.º 2
0
        public LuaValue CallMethod(LuaValue[] args)
        {
            //(func, ...)
            // strip method name
            string          func  = args[0].Value.ToString();
            List <LuaValue> args2 = new List <LuaValue>();

            if ((args[1] as LuaUserdata) != null)
            {
                foreach (LuaValue a in ((args[1] as LuaUserdata).Value as LuaValue[]))
                {
                    args2.Add(a);
                }
            }
            else
            {
                foreach (LuaValue a in args)
                {
                    args2.Add(a);
                }
            }
            try
            {
                args2.RemoveAt(0);
            }
            catch
            {
            }
            LuaFunction f = ClassLib.FindMethod(new LuaValue[] { new LuaString(func), this }) as LuaFunction;

            if ((f == null))
            {
                f = InternalCallParentMethod(func) as LuaFunction;
            }
            // if its still LuaNil.Nil then throw an error
            if ((f == null))
            {
                throw new Exception("Cannot find function '" + func + "'!");
            }

            return(f.Invoke(args2.ToArray()));
        }
Ejemplo n.º 3
0
 public bool Resume(LuaValue[] args)
 {
     if (thread == null)
     {
         thread = new Thread(new ThreadStart(delegate()
         {
             try {
                 _status = "running";
                 func.Invoke(args);
                 _status = "dead";
                 Running = null;
             } catch (Exception) {
                 _status = "dead";
             }
         }));
         thread.SetApartmentState(ApartmentState.MTA);
         thread.Start();
     }
     else
     {
         if (_status == "dead")
         {
             throw new Exception("Error: coroutine is dead, it cannot be resumed!");
         }
         try {
             if (_status == "suspended")
             {
                 thread.Resume();
             }
             else
             {
                 thread.Start();
             }
             _status = "running";
         } catch (Exception ex) {
             _status = "dead";
             throw ex;
         }
     }
     Running = this;
     return(true);
 }
Ejemplo n.º 4
0
        public static LuaValue GetKeyValue(LuaValue baseValue, LuaValue key)
        {
            LuaTable table = baseValue as LuaTable;

            if (((baseValue as LuaClass) != null) && table == null)
            {
                table = (baseValue as LuaClass).Self;
            }

            if (table != null)
            {
                return(table.GetValue(key));
            }
            else
            {
                LuaUserdata userdata = baseValue as LuaUserdata;
                if (userdata != null)
                {
                    if (userdata.MetaTable != null)
                    {
                        LuaValue index = userdata.MetaTable.GetValue("__index");
                        if (index != null)
                        {
                            LuaFunction func = index as LuaFunction;
                            if (func != null)
                            {
                                // its a getter function
                                return(func.Invoke(new LuaValue[] { baseValue, key }));
                            }
                            else
                            {
                                // its a probably a table
                                return(GetKeyValue(index, key));
                            }
                        }
                    }
                }

                throw new Exception(string.Format("Access field '{0}' not from a table.", key.Value));
            }
        }
Ejemplo n.º 5
0
        public LuaValue CallParentMethod(LuaValue[] args)
        {
            //method, ...
            string method = args[0].Value.ToString();
            // strip method name
            List <LuaValue> args2 = new List <LuaValue>();

            foreach (LuaValue a in args)
            {
                args2.Add(a);
            }
            args2.RemoveAt(0);

            LuaFunction func = InternalCallParentMethod(method) as LuaFunction;

            if (func == null)
            {
                throw new Exception("Cannot find function '" + method + "'!");
            }
            else
            {
                return(func.Invoke(args2.ToArray()));
            }
        }
Ejemplo n.º 6
0
        public void GenerateMetaTable()
        {
            Self.MetaTable = new LuaTable();
            Self.MetaTable.Register("__index", new LuaFunc(delegate(LuaValue[] args)
            {                                                       //(table, key)
                LuaValue key = args[0];
                IndexFunction.Invoke(new LuaValue[] { Self, key }); // user defined __index function
                // attempt to get from parents also
                return(GetObject(key, this));                       //CallMethod(new LuaValue[] {key});
            }));

            Self.MetaTable.Register("__call", new LuaFunc(delegate(LuaValue[] args)
            {                                               //(func, ...)
                if (args.Length == 0)
                {
                    return(LuaNil.Nil);
                }
                List <LuaValue> args2 = new List <LuaValue>();
                foreach (LuaValue a in args)
                {
                    args2.Add(a);
                }
                args2.RemoveAt(0);
                CallFunction.Invoke(new LuaValue[] { args[0], new LuaMultiValue(args2.ToArray()) });                                         // user defined __call function
                return(CallMethod(new LuaValue[] { args[0], new LuaUserdata(args2.ToArray()) }));                                            // call function
            }));

            Self.MetaTable.Register("__newindex", new LuaFunc(delegate(LuaValue[] args)
            {
                LuaValue key   = args[1];
                LuaValue value = args[2];
                //(table, key, value)
                NewIndexFunction.Invoke(new LuaValue[] { Self, key, value });                                                // user defined __newindex function
                // check for user defined "metamethods"
                string obj = key.Value.ToString();
                if (obj == "__index")
                {
                    // assign this to the __indexfunction variable
                    IndexFunction = value as LuaFunction;
                }
                else if (obj == "__newindex")
                {
                    NewIndexFunction = value as LuaFunction;
                }
                else if (obj == "__call")
                {
                    CallFunction = value as LuaFunction;
                }
                else if (obj == "__tostring")
                {
                    ToStringFunction = value as LuaFunction;
                }
                else if (obj == "final")
                {
                    Final = true;
                }
                else if (obj == "static")
                {
                    Static = true;
                }
                else if (obj == "name")
                {
                    Name = Rename(value.Value.ToString());
                }
                else if (obj == "constructor")
                {
                    Constructor = (LuaFunction)value;
                }
                else if (obj == "destructor")
                {
                    Destructor = (LuaFunction)value;
                }
                else                                                  // its a normal key/value pair
                {
                    Self.SetKeyValue(key, value);
                }
                return(LuaNil.Nil);
            }));
            Self.MetaTable.Register("__tostring", new LuaFunc(delegate(LuaValue[] args)
            {
                return(ToStringFunction.Invoke(new LuaValue[] {}));
            }));
            this.MetaTable = Self.MetaTable;
        }
Ejemplo n.º 7
0
 public override string ToString()
 {
     return(ToStringFunction.Invoke(new LuaValue[] {}).ToString());
 }