public override object CreateInstance(Type neosType)
        {
            Data.OverloadSetting typeOverloadSetting = GetOverload(neosType);
            if (typeOverloadSetting is null)
            {
                UniLog.Log($"Missing Component overload for {neosType.FullName}");
                return(null);
            }
            Component instance = InstanceSlot.AttachComponent(typeOverloadSetting.OverloadType ?? neosType.FullName);

            if (typeOverloadSetting.Initializer != null)
            {
                try
                {
                    //Create a new environment each time
                    LuaTable environment = LuaInstance.CreateEnvironment();
                    environment.DefineFunction("FindType", new Func <string, Type>(luaFindType));
                    //Set "Instance" to point to the component we want to modify
                    environment.SetMemberValue("Instance", instance);
                    //Compile and run the initializer script
                    LuaInstance.CompileChunk(typeOverloadSetting.Initializer, "Init.lua", new LuaCompileOptions()
                    {
                        ClrEnabled = false
                    }).Run(environment);
                }
                catch (Exception ex)
                {
                    UniLog.Log($"Unable to run initializer on {typeOverloadSetting.OverloadType}: {ex}");
                    return(null);
                }
            }
            return(instance);
        }
Example #2
0
            internal ScriptGlobal(Lua lua) : base(lua)
            {
                this["game"]      = Game.DataModel;
                this["workspace"] = Game.Workspace;
                this["io"]        = null;
                this["os"]        = null;
                this["dofile"]    = null;
                this["dochunk"]   = null;
                this["Enum"]      = BuildEnumTable();
                var instanceTable = new LuaTable();

                instanceTable.DefineFunction("new", new Func <string, Instance, Instance>(NewInstance));
                //instanceTable.DefineFunction("new", new Func<string, LuaTable, Instance>(NewInstance));
                this["Instance"] = instanceTable;

                RegisterPackage("math", typeof(MathPackage));

                foreach (
                    var methodInfo in
                    GetType()
                    .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static |
                                BindingFlags.DeclaredOnly))
                {
                    var name = methodInfo.Name;
                    var del  = CreateDelegate(methodInfo, this);
                    DefineFunction(name, del, false);
                    DefineFunction(char.ToLowerInvariant(name[0]) + name.Substring(1), del, false);
                }

                RegisterPackage("debug", typeof(DebugPackage));

                foreach (var t in typeof(IDataType).GetDescendantTypes())
                {
                    RegisterPackage(t.Name, t);
                }

                var pprintSource = ContentProvider.DownloadStream("internal://scripts/pprint.lua").Result.ReadString();
                var pprint       = ScriptService.Lua.CompileChunk(pprintSource, "pprint", new LuaCompileOptions());

                this["pprint"] = DoChunk(pprint).Values[0];

                var middleclassSource =
                    ContentProvider.DownloadStream("internal://scripts/pprint.lua").Result.ReadString();
                var middleclass = ScriptService.Lua.CompileChunk(middleclassSource, "middleclass",
                                                                 new LuaCompileOptions());

                this["class"] = DoChunk(middleclass).Values[0];
            }