Exemple #1
0
        public void Awake()
        {
            // Loading
            var engine = new Jint.Engine();

            //var logic = new Interpreter();
            Registers   = new Dictionary <Type, Action <BlockBehaviour, KeyInputController> >();
            Unregisters = new Dictionary <Type, Action <BlockBehaviour> >();

            ModConsole.RegisterCommand("script", args => {
                var text = string.Join(" ", args);
                //var func = logic.PrepareScript(text);
                //logic.AddExtFunc(func, "print", (ctx, x) => { ModConsole.Log(x[0]?.ToString()); return null; }, true);
                //logic.SetScript(func);
                //var res = logic.ContinueScript(1000);
                Func <JsValue, JsValue[], JsValue> printCb = (thiz, x) => {
                    ModConsole.Log(x[0]?.ToObject().ToString());
                    return(x[0]);
                };

                JsValue curV = null;
                Func <JsValue, JsValue[], JsValue> irqv = (thiz, x) => {
                    curV = x[0];
                    return(null);
                };
                var script = new JavaScriptParser(text, Jint.Engine.DefaultParserOptions).ParseScript();
                engine.SetValue("print", printCb);
                engine.SetValue("irqv", irqv);
                engine.SetScript(script);
                engine.Executor.OnLog = (x) => ModConsole.Log(x?.ToString());
                bool cli = false;
                engine.Executor.OnNextStatement = () =>
                {
                    if (cli)
                    {
                        return;
                    }
                    cli = true;
                    try
                    {
                        if (curV != null)
                        {
                            engine.Invoke(curV);
                        }
                    }
                    finally
                    {
                        cli = false;
                    }
                };
                var res = engine.ContinueScript(1000);
                ModConsole.Log(res?.ToString());
            }, "exec script");

            ModConsole.RegisterCommand("cpuapi", args =>
            {
                foreach (var line in SingleInstance <Blocks.Api.CpuApi> .Instance.GetHelp())
                {
                    ModConsole.Log(line);
                }
            }, "print cpu api list");

            ModConsole.RegisterCommand("sensordbg", args =>
            {
                DrawSensorDebug = args.Length < 1 ? false : args[0] == "true";
            }, "print sensor debug points");

            CpuBlock.Create(this);
            // These creator functions find corresponding block in game prefabs
            // and replace it with inheritor
            ExtLogicGate.Create(this);
            ExtAltimeterBlock.Create(this);
            ExtSpeedometerBlock.Create(this);
            ExtAnglometerBlock.Create(this);
            ExtSensorBlock.Create(this);
            ModConsole.Log($"Logic mod Awake");

            Events.OnMachineSimulationToggle += Events_OnMachineSimulationToggle;
            LineMaterial           = new Material(Shader.Find("Hidden/Internal-Colored"));
            LineMaterial.hideFlags = HideFlags.HideAndDontSave;
            LineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            LineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            LineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            LineMaterial.SetInt("_ZWrite", 0);

            Camera.onPostRender += DrawConnectingLines;
            Events.OnBlockInit  += InitBlock;

            CpuInfoMessage = ModNetworking.CreateMessageType(new DataType[]
            {
                DataType.Block,
                DataType.ByteArray
            });

            CpuLogMessage = ModNetworking.CreateMessageType(new DataType[]
            {
                DataType.Block,
                DataType.String
            });

            ModNetworking.Callbacks[CpuInfoMessage] += (Action <Message>)((msg) =>
            {
                Player localPlayer = Player.GetLocalPlayer();
                if (msg == null || localPlayer == null || !localPlayer.IsHost)
                {
                    return;
                }

                var block = msg.GetData(0) as Modding.Blocks.Block;
                if (!(block?.BlockScript is CpuBlock cpu))
                {
                    return;
                }

                if (block.Machine == localPlayer.Machine)
                {
                    return; // don't read updates for MY machine!
                }
                cpu.AfterEdit_ServerRecv((byte[])msg.GetData(1));
            });

            ModNetworking.Callbacks[CpuLogMessage] += (Action <Message>)((msg) =>
            {
                if (msg == null)
                {
                    return;
                }

                var block = msg.GetData(0) as Modding.Blocks.Block;
                if (!(block?.BlockScript is CpuBlock cpu))
                {
                    return;
                }

                cpu.LogMessage((string)msg.GetData(1));
            });
        }