예제 #1
0
        /// <summary>
        /// Loads a set of named schemes from a file.
        /// </summary>
        public static Dictionary <string, Scheme> Load(Stream File)
        {
            Lua.lua_State l = Lua.luaL_newstate();
            Lua.luaL_openlibs(l);

            // Add block constants
            foreach (KeyValuePair <string, byte> blockname in Names)
            {
                Lua.lua_pushnumber(l, blockname.Value);
                Lua.lua_setglobal(l, blockname.Key);
            }

            // Derive function
            Lua.lua_pushcfunction(l, delegate(Lua.lua_State ll)
            {
                int source = Lua.lua_gettop(ll);
                Lua.lua_newtable(ll);
                int dest = Lua.lua_gettop(ll);

                // Copy
                Lua.lua_pushnil(ll);
                while (Lua.lua_next(ll, source) != 0)
                {
                    Lua.lua_pushvalue(ll, -2); // Copy key on top
                    Lua.lua_pushvalue(ll, -2); // Copy value on top

                    Lua.lua_settable(ll, dest);

                    Lua.lua_pop(ll, 1); // Remove value
                }


                return(1);
            });
            Lua.lua_setglobal(l, "Derive");

            // Create a "Schemes" variable

            Lua.lua_newtable(l);
            Lua.lua_setglobal(l, "Schemes");

            // Run that shit
            string lua        = new StreamReader(File).ReadToEnd();
            int    compileerr = Lua.luaL_loadstring(l, lua);

            if (compileerr != 0)
            {
                string error = Lua.lua_tostring(l, -1).ToString();
                Lua.lua_pop(l, 1); // pop the error
                throw LuaException.Create(error, false);
            }

            int runerr = Lua.lua_pcall(l, 0, 0, 0);

            if (runerr != 0)
            {
                string error = Lua.lua_tostring(l, -1).ToString();
                Lua.lua_pop(l, 1); // pop the error
                throw LuaException.Create(error, true);
            }

            Lua.lua_pop(l, 1); // Pop the file

            // Read off schemes
            Dictionary <string, Scheme> schemes = new Dictionary <string, Scheme>();

            Lua.lua_getglobal(l, "Schemes");
            Lua.lua_pushnil(l);
            while (Lua.lua_next(l, -2) != 0)
            {
                // v, k, test table
                int    k    = -2;
                string name = Lua.lua_tostring(l, k).ToString();
                Dictionary <byte, IMaterial> data = new Dictionary <byte, IMaterial>();

                int internal_table = Lua.lua_gettop(l);
                // v, k, test table

                Lua.lua_pushnil(l);
                // nil, v, k, test table

                while (Lua.lua_next(l, internal_table) != 0)
                {
                    // v, k, v, k, test table
                    int k2 = -2;

                    byte id = (byte)Lua.lua_tonumber(l, k2);

                    int block_tbl = Lua.lua_gettop(l);

                    Lua.lua_getfield(l, block_tbl, "r");      // 1
                    Lua.lua_getfield(l, block_tbl, "g");      // 2
                    Lua.lua_getfield(l, block_tbl, "b");      // 3
                    Lua.lua_getfield(l, block_tbl, "a");      // 4
                    Lua.lua_getfield(l, block_tbl, "border"); // 5
                    Lua.lua_getfield(l, block_tbl, "b_r");    // 6
                    Lua.lua_getfield(l, block_tbl, "b_g");    // 7
                    Lua.lua_getfield(l, block_tbl, "b_b");    // 8

                    double r      = Lua.lua_tonumber(l, block_tbl + 1);
                    double g      = Lua.lua_tonumber(l, block_tbl + 2);
                    double b      = Lua.lua_tonumber(l, block_tbl + 3);
                    double a      = Lua.lua_tonumber(l, block_tbl + 4);
                    double border = Lua.lua_tonumber(l, block_tbl + 5);
                    double b_r    = Lua.lua_tonumber(l, block_tbl + 6);
                    double b_g    = Lua.lua_tonumber(l, block_tbl + 7);
                    double b_b    = Lua.lua_tonumber(l, block_tbl + 8);

                    if (id == 20)
                    {
                        int abc = 101;
                        abc++;
                    }

                    if (border == 0)
                    {
                        if (a > 0.0)
                        {
                            data[id] = new SolidColorMaterial(Color.RGBA(r, g, b, a));
                        }
                        else
                        {
                            data[id] = new SolidColorMaterial(Color.RGB(r, g, b));
                        }
                    }
                    else
                    {
                        if (a > 0.0)
                        {
                            data[id] = new BorderedMaterial(Color.RGB(b_r, b_g, b_b), border, new SolidColorMaterial(Color.RGBA(r, g, b, a)));
                        }
                        else
                        {
                            data[id] = new BorderedMaterial(Color.RGB(b_r, b_g, b_b), border, new SolidColorMaterial(Color.RGB(r, g, b)));
                        }
                    }

                    Lua.lua_pop(l, 8); // pop all the values

                    Lua.lua_pop(l, 1); // pop val - k is poped with next()
                    // k, v, k, test table
                }
                // v, k, test table
                Lua.lua_pop(l, 1);

                schemes.Add(name, new Scheme(data));
            }

            return(schemes);
        }
예제 #2
0
        /// <summary>
        /// Loads a set of named schemes from a file.
        /// </summary>
        public static Dictionary<string, Scheme> Load(Stream File)
        {
            Lua.lua_State l = Lua.luaL_newstate();
            Lua.luaL_openlibs(l);

            // Add block constants
            foreach(KeyValuePair<string, byte> blockname in Names)
            {
                Lua.lua_pushnumber(l, blockname.Value);
                Lua.lua_setglobal(l, blockname.Key);
            }

            // Derive function
            Lua.lua_pushcfunction(l, delegate(Lua.lua_State ll)
            {
                int source = Lua.lua_gettop(ll);
                Lua.lua_newtable(ll);
                int dest = Lua.lua_gettop(ll);

                // Copy
                Lua.lua_pushnil(ll);
                while (Lua.lua_next(ll, source) != 0)
                {
                    Lua.lua_pushvalue(ll, -2); // Copy key on top
                    Lua.lua_pushvalue(ll, -2); // Copy value on top

                    Lua.lua_settable(ll, dest);

                    Lua.lua_pop(ll, 1); // Remove value
                }

                return 1;
            });
            Lua.lua_setglobal(l, "Derive");

            // Create a "Schemes" variable

            Lua.lua_newtable(l);
            Lua.lua_setglobal(l, "Schemes");

            // Run that shit
            string lua = new StreamReader(File).ReadToEnd();
            int compileerr = Lua.luaL_loadstring(l, lua);

            if(compileerr != 0)
            {
                string error = Lua.lua_tostring(l, -1).ToString();
                Lua.lua_pop(l, 1); // pop the error
                throw LuaException.Create(error, false);
            }

            int runerr = Lua.lua_pcall(l, 0, 0, 0);
            if (runerr != 0)
            {
                string error = Lua.lua_tostring(l, -1).ToString();
                Lua.lua_pop(l, 1); // pop the error
                throw LuaException.Create(error, true);
            }

            Lua.lua_pop(l, 1); // Pop the file

            // Read off schemes
            Dictionary<string, Scheme> schemes = new Dictionary<string, Scheme>();
            Lua.lua_getglobal(l, "Schemes");
            Lua.lua_pushnil(l);
            while(Lua.lua_next(l, -2) != 0)
            {
                // v, k, test table
                int k = -2;
                string name = Lua.lua_tostring(l, k).ToString();
                Dictionary<byte, IMaterial> data = new Dictionary<byte, IMaterial>();

                int internal_table = Lua.lua_gettop(l);
                // v, k, test table

                Lua.lua_pushnil(l);
                // nil, v, k, test table

                while (Lua.lua_next(l, internal_table) != 0)
                {
                    // v, k, v, k, test table
                    int k2 = -2;

                    byte id = (byte)Lua.lua_tonumber(l, k2);

                    int block_tbl = Lua.lua_gettop(l);

                    Lua.lua_getfield(l, block_tbl, "r"); // 1
                    Lua.lua_getfield(l, block_tbl, "g"); // 2
                    Lua.lua_getfield(l, block_tbl, "b"); // 3
                    Lua.lua_getfield(l, block_tbl, "a"); // 4
                    Lua.lua_getfield(l, block_tbl, "border"); // 5
                    Lua.lua_getfield(l, block_tbl, "b_r"); // 6
                    Lua.lua_getfield(l, block_tbl, "b_g"); // 7
                    Lua.lua_getfield(l, block_tbl, "b_b"); // 8

                    double r = Lua.lua_tonumber(l, block_tbl + 1);
                    double g = Lua.lua_tonumber(l, block_tbl + 2);
                    double b = Lua.lua_tonumber(l, block_tbl + 3);
                    double a = Lua.lua_tonumber(l, block_tbl + 4);
                    double border = Lua.lua_tonumber(l, block_tbl + 5);
                    double b_r = Lua.lua_tonumber(l, block_tbl + 6);
                    double b_g = Lua.lua_tonumber(l, block_tbl + 7);
                    double b_b = Lua.lua_tonumber(l, block_tbl + 8);

                    if (id == 20)
                    {
                        int abc = 101;
                        abc++;
                    }

                    if(border == 0)
                    {
                        if(a>0.0)
                            data[id] = new SolidColorMaterial(Color.RGBA(r, g, b, a));
                        else
                            data[id] = new SolidColorMaterial(Color.RGB(r, g, b));
                    }
                    else
                    {
                        if(a > 0.0)
                            data[id] = new BorderedMaterial(Color.RGB(b_r, b_g, b_b), border, new SolidColorMaterial(Color.RGBA(r, g, b, a)));
                        else
                            data[id] = new BorderedMaterial(Color.RGB(b_r, b_g, b_b), border, new SolidColorMaterial(Color.RGB(r, g, b)));
                    }

                    Lua.lua_pop(l, 8); // pop all the values

                    Lua.lua_pop(l, 1); // pop val - k is poped with next()
                    // k, v, k, test table
                }
                // v, k, test table
                Lua.lua_pop(l, 1);

                schemes.Add(name, new Scheme(data));
            }

            return schemes;
        }