public PlayfieldScriptData(EmpyrionScripting parent)
        {
            Playfield_OnEntityLoaded   = (IEntity entity) => AddEntity(entity);
            Playfield_OnEntityUnloaded = (IEntity entity) => RemoveEntity(entity);

            ScriptExecQueue = new ScriptExecQueue(D => parent.ProcessScript(this, D));
        }
 public EmpyrionScripting()
 {
     EmpyrionScriptingInstance     = this;
     EmpyrionConfiguration.ModName = "EmpyrionScripting";
     DeviceLock.Log      = Log;
     ConveyorHelpers.Log = Log;
     ScriptExecQueue.Log = Log;
     ScriptExecQueue     = new ScriptExecQueue(ProcessScript);
     SetupHandlebarsComponent();
 }
        private void Application_OnPlayfieldUnloaded(string playfieldName)
        {
            ModApi.Log($"PauseScripts for {playfieldName} {(PauseScripts ? "always stopped" : "scripts running")}");
            PauseScripts = true;

            DisplayScriptInfos();
            ScriptExecQueue.Clear();
            LcdCompileCache.Clear();
            PersistendData.Clear();
        }
 private void QueueScriptExecuting(object state)
 {
     for (int i = Configuration.Current.ScriptsParallelExecution - 1; i >= 0 && ScriptExecQueue.ExecNext(); i--)
     {
         ;
     }
 }
        private int ProcessAllInGameScripts(IEntity entity)
        {
            Log($"ProcessAllInGameScripts: {entity.Name}:{entity.Type} Pause:{PauseScripts}", LogLevel.Debug);
            if (entity.Type == EntityType.Proxy || PauseScripts)
            {
                return(0);
            }

            try
            {
                var entityScriptData = new ScriptRootData(CurrentEntities, ModApi.Playfield, entity, DeviceLockAllowed, PersistendData);

                var deviceNames = entityScriptData.E.S.AllCustomDeviceNames.Where(N => N.StartsWith(ScriptKeyword)).ToArray();
                Log($"ProcessAllInGameScripts: #{deviceNames.Length}", LogLevel.Debug);

                int count = 0;
                Parallel.ForEach(deviceNames, N =>
                {
                    if (PauseScripts)
                    {
                        return;
                    }

                    var lcd = entity.Structure.GetDevice <ILcd>(N);
                    if (lcd == null)
                    {
                        return;
                    }

                    try
                    {
                        Log($"ProcessAllInGameScripts: {N}", LogLevel.Debug);

                        var data = new ScriptRootData(entityScriptData)
                        {
                            Script = lcd.GetText(),
                            Error  = L,
                        };

                        AddTargetsAndDisplayType(data, N.Substring(ScriptKeyword.Length));

                        if (Configuration.Current.ScriptTracking)
                        {
                            var trackfile = GetTrackingFileName(entity, data.Script.GetHashCode().ToString());
                            if (!File.Exists(trackfile))
                            {
                                File.WriteAllText(trackfile, data.Script);
                            }
                        }


                        data.ScriptId = entity.Id + "/" + N;
                        ScriptExecQueue.Add(data);

                        Interlocked.Increment(ref count);
                    }
                    catch (Exception lcdError)
                    {
                        if (Configuration.Current.LogLevel >= EmpyrionNetAPIDefinitions.LogLevel.Debug)
                        {
                            ModApi.Log($"UpdateLCDs ({entity.Id}/{entity.Name}):LCD: {lcdError}");
                        }
                    }
                });

                return(count);
            }
            catch (Exception error)
            {
                File.WriteAllText(GetTrackingFileName(entity, string.Empty) + ".error", error.ToString());
                return(0);
            }
        }