Beispiel #1
0
    // Use this for initialization
    void Start()
    {
        var     script        = LuaManager.Get().CreateLuaObject("GameBoot");
        LuaFunc startFunction = script.GetInPath <LuaFunc>("GameBoot.StartGame");

        startFunction.Invoke();
    }
Beispiel #2
0
        public LuaFunction Register(string name, LuaFunc function)
        {
            LuaFunction luaFunc = new LuaFunction(function);

            this.SetNameValue(name, luaFunc);
            return(luaFunc);
        }
Beispiel #3
0
 public static void PushCClosure(IntPtr State, LuaFunc F, int N = 0)
 {
     if (!LuaFuncs.Contains(F))
     {
         LuaFuncs.Add(F);
     }
     _PushCClosure(State, F, N);
 }
Beispiel #4
0
 public static void PushCFunction(IntPtr State, LuaFunc F)
 {
     if (!LuaFuncs.Contains(F))
     {
         LuaFuncs.Add(F);
     }
     PushCClosure(State, F, 0);
 }
Beispiel #5
0
        /// <summary>
        /// Creates a new LuaDefinedMethod from the given method.
        /// </summary>
        /// <param name="env">The current environment.</param>
        /// <param name="name">The name of the method, used for errors.</param>
        /// <param name="method">The method to invoke.</param>
        /// <param name="target">The target object.</param>
        /// <exception cref="System.ArgumentNullException">If method, E, or target is null.</exception>
        /// <exception cref="System.ArgumentException">If method does not have
        /// the correct method signature:
        /// ILuaMultiValue Method(ILuaEnvironment, ILuaMultiValue)</exception>
        public LuaDefinedFunction(ILuaEnvironment env, string name, MethodInfo method, object target)
            : base(name)
        {
            LuaFunc func = (LuaFunc)Delegate.CreateDelegate(typeof(LuaFunc), target, method);

            _method = func;
            _env    = env;
        }
Beispiel #6
0
 public static IEnumerator EnumLuaCoroutine(LuaFunc lfunc)
 {
     if (lfunc != null)
     {
         LuaThread lthd = new LuaThread(lfunc);
         return(EnumLuaCoroutine(lthd));
     }
     else
     {
         return(GetEmptyEnumerator());
     }
 }
Beispiel #7
0
        public static void TheMain()
        {
            Console.Title = "GSharp Test";
            //Console.WriteLine("GShap Test");

            L = Lua.NewState();
            Lua.OpenLibs(L);

            LuaFunc AtPanicOld = null;

            AtPanicOld = Lua.AtPanic(L, (LL) => {
                AtPanicOld(LL);
                throw new Exception();
            });

            Lua.PushCFunction(L, (LL) => {
                int Top          = Lua.GetTop(LL);
                StringBuilder SB = new StringBuilder();
                for (int i = 1; i < Top + 1; i++)
                {
                    SB.Append(Lua.ToString(LL, i)).Append("\t");
                }
                Console.WriteLine(SB.ToString().Trim());
                return(0);
            });
            Lua.SetGlobal(L, "print");

            ErrorCheck(Lua.LoadString(L, "function helloworld() print(\"Hello World!\") return helloworld end"));
            ErrorCheck(Lua.PCall(L, 0, 0, 0));
            ErrorCheck(Lua.LoadString(L, "function printt(t) for k,v in pairs(t) do print(tostring(k) .. \" - \" .. tostring(v)) end end"));
            ErrorCheck(Lua.PCall(L, 0, 0, 0));

            //try {
            Init();

            /*} catch (Exception) {
             *      throw;
             * }//*/
            GSharp.Dynamic.Delegates.Dump();
            while (true)
            {
                Console.Write(">> ");
                try {
                    ErrorCheck(Lua.LoadString(L, Console.ReadLine()));
                    ErrorCheck(Lua.PCall(L, 0, 0, 0));
                } catch (Exception E) {
                    Console.WriteLine("exception: {0}", E.Message);
                }
            }
        }
Beispiel #8
0
        public T L_Opt <T>(LuaFunc <int, T> f, int n, T def)
        {
            LuaType t = API.Type(n);

            if (t == LuaType.LUA_TNONE ||
                t == LuaType.LUA_TNIL)
            {
                return(def);
            }
            else
            {
                return(f(n));
            }
        }
        /// <summary>
        /// Creates a new LuaDefinedMethod from the given method.
        /// </summary>
        /// <param name="E">The current environment.</param>
        /// <param name="name">The name of the method, used for errors.</param>
        /// <param name="method">The method to invoke.</param>
        /// <param name="target">The target object.</param>
        /// <exception cref="System.ArgumentNullException">If method, E, or target is null.</exception>
        /// <exception cref="System.ArgumentException">If method does not have
        /// the correct method signature: 
        /// ILuaMultiValue Method(ILuaEnvironment, ILuaMultiValue)</exception>
        public LuaDefinedFunction(ILuaEnvironment E, string name, MethodInfo method, object target)
            : base(name)
        {
            Contract.Requires<ArgumentNullException>(E != null, "E");
            Contract.Requires<ArgumentNullException>(method != null, "method");
            Contract.Requires<ArgumentNullException>(target != null, "target");
            Contract.Ensures(_Method != null);

            LuaFunc func = (LuaFunc)Delegate.CreateDelegate(typeof(LuaFunc), target, method);
            Contract.Assume(func != null);

            this._Method = func;
            this._E = E;
        }
Beispiel #10
0
        public static int ClrFuncCoroutine(IntPtr l)
        {
            var oldtop = l.gettop();

            if (l.isfunction(1))
            {
                var lfunc = new LuaFunc(l, 1);
                var co    = UnityFramework.UnityLua.StartLuaCoroutine(lfunc);
                l.settop(oldtop);
                l.PushLua(co);
            }

            return(l.gettop() - oldtop);
        }
Beispiel #11
0
        public static int ClrFuncBehavCoroutine(IntPtr l)
        {
            var oldtop = l.gettop();

            if (l.isuserdata(1) && !l.islightuserdata(1) && l.isfunction(2))
            {
                var go    = l.GetLua(1).UnwrapDynamic() as UnityEngine.MonoBehaviour;
                var lfunc = new LuaFunc(l, 2);
                var co    = UnityFramework.UnityLua.StartLuaCoroutineForBehav(go, lfunc);
                l.settop(oldtop);
                l.PushLua(co);
            }

            return(l.gettop() - oldtop);
        }
Beispiel #12
0
 public static Coroutine StartLuaCoroutineForBehav(this MonoBehaviour behav, LuaFunc lfunc)
 {
     if (behav != null)
     {
         //var go = new GameObject();
         //ResManager.DontDestroyOnLoad(go);
         //return StartLuaCoroutineForMonoBehaviourAndDestroy(go, behav, lfunc);
         return(behav.StartCoroutine(EnumLuaCoroutine(lfunc)));
     }
     else
     {
         var go = new GameObject();
         ResManager.DontDestroyOnLoad(go);
         return(StartLuaCoroutineForGameObjectAndDestroy(go, lfunc));
     }
 }
Beispiel #13
0
        public static object To(IntPtr L, int I = -1, params string[] Pth)
        {
            int T = Lua.Type(L, I);

            switch (T)
            {
            case TSTRING:
                return(Lua.ToString(L, I));

            case TNUMBER:
                return(Lua.ToNumber(L, I));

            case TFUNCTION: {
                LuaFunc LF = Lua.ToCFunction(L, I);
                if (!LuaFuncs.Contains(LF))
                {
                    LuaFuncs.Add(LF);
                }
                return(LF);
            }

            case TBOOLEAN:
                return(Lua.ToBoolean(L, I));

            case TNIL: {
                LuaObject O = new LuaObject(L);
                O.Type = T;
                O.Path.AddRange(Pth);
                return(O);
            }

            case TTABLE: {
                LuaTable Tbl = new LuaTable(L, Pth);
                Tbl.Type = T;
                return(Tbl);
            }
            }

            return(null);
        }
Beispiel #14
0
 public static Coroutine StartLuaCoroutineForBehav(this MonoBehaviour behav, LuaFunc lfunc)
 {
     if (behav != null)
     {
         var work = EnumLuaCoroutine(lfunc);
         if (work is IDisposable)
         {
             var info = new CoroutineRunner.CoroutineInfo()
             {
                 behav = behav, work = work
             };
             return(info.coroutine = behav.StartCoroutine(CoroutineRunner.SafeEnumerator(work, info)));
         }
         else
         {
             return(behav.StartCoroutine(EnumLuaCoroutine(lfunc)));
         }
     }
     else
     {
         return(CoroutineRunner.StartCoroutine(EnumLuaCoroutine(lfunc)));
     }
 }
Beispiel #15
0
        public static object To(IntPtr L, int I = -1)
        {
            int T = Lua.Type(L, I);

            switch (T)
            {
            case TSTRING:
                return(Lua.ToString(L, I));

            case TNUMBER:
                return(Lua.ToNumber(L, I));

            case TFUNCTION: {
                if (!IsCFunction(L, I))
                {
                    return(new Dynamic.LuaObject(L).Deref());
                }
                LuaFunc LF = Lua.ToCFunction(L, I);
                if (!LuaFuncs.Contains(LF))
                {
                    LuaFuncs.Add(LF);
                }
                return(LF);
            }

            case TBOOLEAN:
                return(Lua.ToBoolean(L, I));

            case TNIL:
                return(null);

            case TTABLE:
                return(new Dynamic.LuaObject(L).Deref());
            }

            return(null);
        }
Beispiel #16
0
 public static void Push(IntPtr L, object O)
 {
     if (O == null)
     {
         Lua.PushNil(L);
     }
     else if (O is string)
     {
         Lua.PushString(L, O.ToString());
     }
     else if (O is int)
     {
         Lua.PushInteger(L, (int)O);
     }
     else if (O is float)
     {
         Lua.PushNumber(L, (double)(float)O);
     }
     else if (O is double)
     {
         Lua.PushNumber(L, (double)O);
     }
     else if (O is LuaFunc)
     {
         Lua.PushCFunction(L, (LuaFunc)O);
     }
     else if (O is bool)
     {
         Lua.PushBoolean(L, (bool)O ? 1 : 0);
     }
     else if (O is Dynamic.LuaObject)
     {
         ((Dynamic.LuaObject)O).Deref();
     }
     else if (O is Array)
     {
         Array A = (Array)O;
         Lua.CreateTable(L, A.Length, 0);
         for (int i = 0; i < A.Length; i++)
         {
             Push(L, i);
             Push(L, A.GetValue(i));
             Lua.SetTable(L, -3);
         }
     }
     else if (O is IDictionary)
     {
         IDictionary D = (IDictionary)O;
         Lua.CreateTable(L, 0, D.Count);
         foreach (var K in D.Keys)
         {
             Push(L, K);
             Push(L, D[K]);
             Lua.SetTable(L, -3);
         }
     }
     else if (O is Delegate)
     {
         LuaFunc F = ((Delegate)O).CreateLuaDelegateInvoker(L);
         Lua.PushCFunction(L, F);
     }
     else
     {
         throw new Exception("Invalid type " + O.GetType().FullName);
     }
 }
Beispiel #17
0
 public static Coroutine StartLuaCoroutine(LuaFunc lfunc)
 {
     return(StartLuaCoroutineForBehav(null, lfunc));
 }
Beispiel #18
0
 /// <summary>
 /// Registers a function with the global environment
 /// </summary>
 /// <param name="name">Name of the command to add</param>
 /// <param name="func">Logic of the command to add</param>
 public void Register(string name, LuaFunc func, string module = null)
 {
     if (String.IsNullOrEmpty(module)) {
         lock (_env)
             Environment.Register(name, func);
     } else {
         LuaTable table;
         lock (_env)
             table = Environment.GetValue(module) as LuaTable;
         try {
             lock (table)
                 table.Register(name, func);
         } catch (NullReferenceException) {
             Console.Error.WriteLine("{0} module not found.", module);
         }
     }
 }
Beispiel #19
0
 public static IEnumerator EnumLuaCoroutineForMonoBehaviourAndDestroy(this GameObject go, MonoBehaviour behav, LuaFunc lfunc)
 {
     if (behav != null)
     {
         var work = EnumLuaCoroutine(lfunc);
         if (work != null)
         {
             while (behav != null && work.MoveNext())
             {
                 yield return(work.Current);
             }
         }
     }
     if (go != null)
     {
         GameObject.Destroy(go);
     }
 }
Beispiel #20
0
 public LuaFunction Register(string name, LuaFunc function)
 {
     LuaFunction luaFunc = new LuaFunction(function);
     this.SetNameValue(name, luaFunc);
     return luaFunc;
 }
Beispiel #21
0
 public LuaFunction(LuaFunc function)
 {
     Function = function;
 }
Beispiel #22
0
 public LuaFunction(LuaFunc function)
 {
     this.Function = function;
 }
Beispiel #23
0
        public static void RegisterCFunction(IntPtr State, string TableName, string FuncName, LuaFunc F)
        {
            GetField(State, GLOBALSINDEX, TableName);
            if (!IsTable(State, -1))
            {
                CreateTable(State, 0, 1);
                SetField(State, GLOBALSINDEX, TableName);
                Pop(State);

                GetField(State, GLOBALSINDEX, TableName);
            }

            PushString(State, FuncName);
            PushCFunction(State, F);
            SetTable(State, -3);
            Pop(State);
        }
Beispiel #24
0
 public static extern int CPCall(IntPtr State, LuaFunc F, IntPtr P);
Beispiel #25
0
 public static extern void PushCClosure(IntPtr State, LuaFunc F, int I);
Beispiel #26
0
 public static extern LuaFunc AtPanic(IntPtr State, LuaFunc F);
Beispiel #27
0
 static extern void _PushCClosure(IntPtr State, LuaFunc F, int N = 0);
Beispiel #28
0
 public static void PushCFunction(IntPtr State, LuaFunc F)
 {
     PushCClosure(State, F, 0);
 }
Beispiel #29
0
        public static Coroutine StartLuaCoroutineForMonoBehaviourAndDestroy(this GameObject go, MonoBehaviour behav, LuaFunc lfunc)
        {
            var behav2 = go.AddComponent <DummyBehav>();

            return(behav2.StartCoroutine(EnumLuaCoroutineForMonoBehaviourAndDestroy(go, behav, lfunc)));
        }
Beispiel #30
0
 public LuaFunction(LuaFunc function)
 {
     this.Function = function;
 }
Beispiel #31
0
        public void Load(Emulator Emul, IntPtr L, dynamic G)
        {
            G.require = new LuaFunc((LL) => {
                Lua.CheckType(LL, -1, Lua.TSTRING);
                string ModName = Path.GetFileNameWithoutExtension(Lua.ToString(LL, -1));

                if (File.Exists(ModName))
                {
                }
                else if (File.Exists(ModName + ".dll"))
                {
                    ModName += ".dll";
                }
                else if (File.Exists(ModName + "_win32.dll"))
                {
                    ModName += "_win32.dll";
                }
                else if (File.Exists("gmcl_" + ModName + "_win32.dll"))
                {
                    ModName = "gmcl_" + ModName + "_win32.dll";
                }
                else if (File.Exists("gmsv_" + ModName + "_win32.dll"))
                {
                    ModName = "gmsv_" + ModName + "_win32.dll";
                }
                else
                {
                    Lua.Error(LL, "Module '" + ModName + "' could not be found");
                    return(0);
                }

                if (!ModName.EndsWith("_win32.dll"))
                {
                    return(0);
                }

                IntPtr Module = LoadLibrary(ModName);
                if (Module == IntPtr.Zero)
                {
                    Lua.Error(LL, "Module '" + ModName + "' could not be loaded");
                    return(0);
                }

                IntPtr gmod13_open_addr = GetProcAddress(Module, "gmod13_open");
                if (gmod13_open_addr == IntPtr.Zero)
                {
                    Lua.Error(LL, "gmod13_open entry point not found in '" + ModName + "'");
                    return(0);
                }

                if (ModHandles.Contains(Module.ToInt32()))
                {
                    Emul.Print(LL, "Skipping '{0}' 0x{1:X}", ModName, Module.ToInt32());
                    Lua.PushInteger(LL, Module.ToInt32());
                    return(1);
                }
                Emul.Print(LL, "Loading '{0}' 0x{1:X}", ModName, Module.ToInt32());
                ModHandles.Add(Module.ToInt32());
                try {
                    LuaFunc gmod13_open = (LuaFunc)Marshal.GetDelegateForFunctionPointer(gmod13_open_addr, typeof(LuaFunc));
                    Lua.PushInteger(LL, Module.ToInt32());
                    int R = gmod13_open(LL);
                    if (R != -1)
                    {
                        return(R + 1);
                    }
                    return(R);
                } catch (Exception E) {
                    Lua.Error(LL, "[ERROR] Module failure\n" + E.Message);
                }

                return(0);
            });

            G.unrequire = new LuaFunc((LL) => {
                int MNum = (int)Lua.CheckInt(LL, -1);
                if (ModHandles.Contains(MNum))
                {
                    ModHandles.Remove(MNum);
                }
                else
                {
                    return(0);
                }

                IntPtr ModH = new IntPtr(MNum);
                IntPtr gmod13_close_addr = GetProcAddress(ModH, "gmod13_close");

                if (gmod13_close_addr != IntPtr.Zero)
                {
                    LuaFunc gmod13_close = (LuaFunc)Marshal.GetDelegateForFunctionPointer(gmod13_close_addr, typeof(LuaFunc));
                    gmod13_close(LL);
                }
                else
                {
                    Emul.Print(LL, "[WARNING] gmod13_close not found in 0x{0:X}", MNum);
                }


                if (FreeLibrary(ModH))
                {
                    Emul.Print(LL, "Module 0x{0:X} free'd", MNum);
                }
                else
                {
                    Emul.Print(LL, "Failed 0x{0:X}", Marshal.GetLastWin32Error());
                }
                return(0);
            });

            G.require_all = new LuaFunc((LL) => {
                string[] Modules = Directory.GetFiles(".", "*.dll");
                foreach (var Module in Modules)
                {
                    Lua.GetGlobal(LL, "require");
                    Lua.PushString(LL, Module);
                    Emul.ErrorCheck(LL, Lua.PCall(L, 1, 0, 0));
                }
                return(0);
            });

            G.unrequire_all = new LuaFunc((LL) => {
                int[] MHand = ModHandles.ToArray();
                foreach (var M in MHand)
                {
                    Lua.GetGlobal(LL, "unrequire");
                    Lua.PushInteger(LL, M);
                    Emul.ErrorCheck(LL, Lua.PCall(L, 1, 0, 0));
                }
                return(0);
            });
        }