public void DeleteFrom(ScriptData script)
 {
     bool changed = false;
     lock (StateSaveLock)
     {
         //if we did remove something, resave it
         if (script.Part.StateSaves.Remove(script.ItemID))
             changed = true;
         if (script.Part.StateSaves.Remove(script.InventoryItem.OldItemID))
             changed = true;
         if (script.Part.StateSaves.Remove(script.InventoryItem.ItemID))
             changed = true;
     }
     if (changed)
         script.Part.ParentEntity.HasGroupChanged = true;
 }
        public void Deserialize(ScriptData instance, StateSave save)
        {
            instance.State = save.State;
            instance.Running = save.Running;
            instance.EventDelayTicks = (long) save.MinEventDelay;
            instance.AssemblyName = save.AssemblyName;
            instance.Disabled = save.Disabled;
            instance.UserInventoryItemID = save.UserInventoryID;
            if (save.Plugins != "")
                instance.PluginData = (OSDMap) OSDParser.DeserializeJson(save.Plugins);
            m_module.CreateFromData(instance.Part.UUID, instance.ItemID, instance.Part.UUID,
                                    instance.PluginData);
            instance.Source = save.Source;
            instance.InventoryItem.PermsGranter = save.PermsGranter;
            instance.InventoryItem.PermsMask = save.PermsMask;
            instance.TargetOmegaWasSet = save.TargetOmegaWasSet;

            if (save.Variables != null && instance.Script != null)
                instance.Script.SetStoreVars(XMLUtils.ParseXmlResponse(save.Variables));
        }
        public void SaveStateTo(ScriptData script, bool forced, bool saveBackup)
        {
            if (!forced)
            {
                if (script.Script == null)
                    return; //If it didn't compile correctly, this happens
                if (!script.Script.NeedsStateSaved)
                    return; //If it doesn't need a state save, don't save one
            }
            if (script.Script != null)
                script.Script.NeedsStateSaved = false;
            StateSave stateSave = new StateSave
                                      {
                                          State = script.State,
                                          ItemID = script.ItemID,
                                          Running = script.Running,
                                          MinEventDelay = script.EventDelayTicks,
                                          Disabled = script.Disabled,
                                          UserInventoryID = script.UserInventoryItemID,
                                          AssemblyName = script.AssemblyName,
                                          Compiled = script.Compiled,
                                          Source = script.Source == null ? "" : script.Source
                                      };
            //Allow for the full path to be put down, not just the assembly name itself
            if (script.InventoryItem != null)
            {
                stateSave.PermsGranter = script.InventoryItem.PermsGranter;
                stateSave.PermsMask = script.InventoryItem.PermsMask;
            }
            else
            {
                stateSave.PermsGranter = UUID.Zero;
                stateSave.PermsMask = 0;
            }
            stateSave.TargetOmegaWasSet = script.TargetOmegaWasSet;

            //Vars
            Dictionary<string, object> vars = new Dictionary<string, object>();
            if (script.Script != null)
                vars = script.Script.GetStoreVars();
            stateSave.Variables = XMLUtils.BuildXmlResponse(vars);

            //Plugins
            stateSave.Plugins =
                OSDParser.SerializeJsonString(m_module.GetSerializationData(script.ItemID, script.Part.UUID));

            lock (StateSaveLock)
                script.Part.StateSaves[script.ItemID] = stateSave;
            if (saveBackup)
                script.Part.ParentEntity.HasGroupChanged = true;
        }
 public void SaveStateTo(ScriptData script)
 {
     SaveStateTo(script, false);
 }
 public void SaveStateTo(ScriptData script, bool forced)
 {
     SaveStateTo(script, forced, true);
 }
        /// <summary>
        ///     This checks the minimum amount of time between script firings as well as control events, making sure that events do NOT fire after scripts reset, close or restart, etc
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="FunctionName"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool CheckIfEventShouldFire(ScriptData ID, string FunctionName, object[] param)
        {
            lock (ID.ScriptEventLock)
            {
                if (ID.Loading)
                {
                    //If the script is loading, enqueue all events
                    return true;
                }
                //This will happen if the script doesn't compile correctly
                if (ID.Script == null)
                {
                    MainConsole.Instance.Info("[DotNetEngine]: Could not load script from item '" +
                                              ID.InventoryItem.Name +
                                              "' to fire event " + FunctionName);
                    return false;
                }
                scriptEvents eventType = (scriptEvents) Enum.Parse(typeof (scriptEvents), FunctionName);

                // this must be done even if there is no event method

                if (eventType == scriptEvents.touch_start)
                    ID.RemoveTouchEvents = false;
                else if (eventType == scriptEvents.collision_start)
                    ID.RemoveCollisionEvents = false;
                else if (eventType == scriptEvents.land_collision_start)
                    ID.RemoveLandCollisionEvents = false;

                if (eventType == scriptEvents.state_entry)
                    ID.ResetEvents();

                if ((ID.Script.GetStateEventFlags(ID.State) & (long) eventType) == 0)
                    return false; //If the script doesn't contain the state, don't even bother queueing it

                //Make sure we can execute events at position
                if (!m_scriptEngine.PipeEventsForScript(ID.Part))
                    return false;

                switch (eventType)
                {
                    case scriptEvents.timer:
                        if (ID.TimerInQueue)
                            return false;
                        ID.TimerInQueue = true;
                        break;
                    case scriptEvents.sensor:
                        if (ID.SensorInQueue)
                            return false;
                        ID.SensorInQueue = true;
                        break;
                    case scriptEvents.no_sensor:
                        if (ID.NoSensorInQueue)
                            return false;
                        ID.NoSensorInQueue = true;
                        break;
                    case scriptEvents.at_target:
                        if (ID.AtTargetInQueue)
                            return false;
                        ID.AtTargetInQueue = true;
                        break;
                    case scriptEvents.not_at_target:
                        if (ID.NotAtTargetInQueue)
                            return false;
                        ID.NotAtTargetInQueue = true;
                        break;
                    case scriptEvents.at_rot_target:
                        if (ID.AtRotTargetInQueue)
                            return false;
                        ID.AtRotTargetInQueue = true;
                        break;
                    case scriptEvents.not_at_rot_target:
                        if (ID.NotAtRotTargetInQueue)
                            return false;
                        ID.NotAtRotTargetInQueue = true;
                        break;
                    case scriptEvents.control:
                        int held = ((LSL_Types.LSLInteger) param[1]).value;
                        // int changed = ((LSL_Types.LSLInteger)data.Params[2]).value;

                        // If the last message was a 0 (nothing held)
                        // and this one is also nothing held, drop it
                        //
                        if (ID.LastControlLevel == held && held == 0)
                            return true;

                        // If there is one or more queued, then queue
                        // only changed ones, else queue unconditionally
                        //
                        if (ID.ControlEventsInQueue > 0)
                        {
                            if (ID.LastControlLevel == held)
                                return false;
                        }
                        break;
                    case scriptEvents.collision:
                        if (ID.CollisionInQueue || ID.RemoveCollisionEvents)
                            return false;
                        ID.CollisionInQueue = true;
                        break;
                    case scriptEvents.moving_start:
                        if (ID.MovingInQueue) //Block all other moving_starts until moving_end is called
                            return false;
                        ID.MovingInQueue = true;
                        break;
                    case scriptEvents.moving_end:
                        if (!ID.MovingInQueue) //If we get a moving_end after we have sent one event, don't fire another
                            return false;
                        break;
                    case scriptEvents.collision_end:
                        if (ID.RemoveCollisionEvents)
                            return false;
                        break;
                    case scriptEvents.touch:
                        if (ID.TouchInQueue || ID.RemoveTouchEvents)
                            return false;
                        ID.TouchInQueue = true;
                        break;
                    case scriptEvents.touch_end:
                        if (ID.RemoveTouchEvents)
                            return false;
                        break;
                    case scriptEvents.land_collision:
                        if (ID.LandCollisionInQueue || ID.RemoveLandCollisionEvents)
                            return false;
                        ID.LandCollisionInQueue = true;
                        break;
                    case scriptEvents.land_collision_end:
                        if (ID.RemoveLandCollisionEvents)
                            return false;
                        break;
                    case scriptEvents.changed:
                        Changed changed;
                        if (param[0] is Changed)
                            changed = (Changed) param[0];
                        else
                            changed = (Changed) (((LSL_Types.LSLInteger) param[0]).value);
                        if (ID.ChangedInQueue.Contains(changed))
                            return false;
                        ID.ChangedInQueue.Add(changed);
                        break;
                }
            }
            return true;
        }
 public StateSave FindScriptStateSave(ScriptData script)
 {
     lock (StateSaveLock)
     {
         StateSave save;
         if (!script.Part.StateSaves.TryGetValue(script.ItemID, out save))
         {
             if (!script.Part.StateSaves.TryGetValue(script.InventoryItem.OldItemID, out save))
             {
                 if (!script.Part.StateSaves.TryGetValue(script.InventoryItem.ItemID, out save))
                 {
                     return null;
                 }
             }
         }
         return save;
     }
 }
 public void AddPreviouslyCompiled(string source, ScriptData ID)
 {
     //string key = source.Length.ToString() + source.GetHashCode().ToString();
     string key = Util.Md5Hash(source);
     lock (PreviouslyCompiled)
     {
         if (!PreviouslyCompiled.ContainsKey(key))
         {
             //PreviouslyCompiled.Add (source, ID.AssemblyName);
             PreviouslyCompiled.Add(key, ID.AssemblyName);
         }
     }
 }
 public void RemoveScript(ScriptData Data)
 {
     lock (ScriptsItems)
     {
         ScriptsItems.Remove(Data.ItemID);
     }
     lock (Scripts)
     {
         Dictionary<UUID, ScriptData> Instances = new Dictionary<UUID, ScriptData>();
         if (Scripts.TryGetValue(Data.Part.UUID, out Instances))
         {
             Instances.Remove(Data.ItemID);
             if (Instances.Count > 0)
                 Scripts[Data.Part.UUID] = Instances;
             else
                 Scripts.Remove(Data.Part.UUID);
         }
     }
 }
        public void AddNewScript(ScriptData ID)
        {
            lock (ScriptsItems)
            {
                if (ID.Part != null)
                    ScriptsItems[ID.ItemID] = ID.Part.UUID;
            }
            lock (Scripts)
            {
                Dictionary<UUID, ScriptData> Instances = new Dictionary<UUID, ScriptData>();
                if (!Scripts.TryGetValue(ID.Part.UUID, out Instances))
                    Instances = new Dictionary<UUID, ScriptData>();

                Instances[ID.ItemID] = ID;
                Scripts[ID.Part.UUID] = Instances;
            }
        }
 public void SetEventSchSetIgnoreNew(ScriptData ID, bool yes)
 {
     if (ID == null)
         return;
     ID.IgnoreNew = yes;
 }
 public void RemoveState(ScriptData ID)
 {
     m_ScriptEngine.StateSave.DeleteFrom(ID);
 }
        public void RemoveFromEventSchQueue(ScriptData ID, bool abortcur)
        {
            if (ID == null)
                return;

            //Ignore any events to be added after this
            ID.IgnoreNew = true;
            //Clear out the old events
            Interlocked.Increment(ref ID.VersionID);
        }
        public void AddEventSchQueue(ScriptData ID, string FunctionName, DetectParams[] qParams, EventPriority priority,
                                     params object[] param)
        {
            QueueItemStruct QIS = new QueueItemStruct
            {
                EventsProcData = new ScriptEventsProcData(),
                ID = ID,
                functionName = FunctionName,
                llDetectParams = qParams,
                param = param,
                VersionID = Interlocked.Read(ref ID.VersionID),
                State = ID.State,
                CurrentlyAt = null
            };

            if (ID == null || ID.Script == null || ID.IgnoreNew)
                return;

            if (!ID.SetEventParams(QIS)) // check events delay rules
                return;

            ScriptEvents.Enqueue(QIS);

            long threadCount = Interlocked.Read(ref scriptThreadpool.nthreads);
            if (threadCount == 0 || threadCount < (ScriptEvents.Count + (SleepingScriptEventCount/2))*EventPerformance)
            {
                scriptThreadpool.QueueEvent(eventLoop, 2);
            }
        }