public (ISmartScriptLine[], IConditionLine[]) ToDatabaseCompatibleSmartScript(SmartScript script) { if (script.Events.Count == 0) { return(new ISmartScriptLine[0], null); } var eventId = 0; var lines = new List <ISmartScriptLine>(); var conditions = new List <IConditionLine>(); var previousWasWait = false; long nextTriggerId = script.Events.Where(e => e.Id == SmartConstants.EventTriggerTimed) .Select(e => e.GetParameter(0).Value) .DefaultIfEmpty(0) .Max() + 1; foreach (SmartEvent e in script.Events) { if (e.Actions.Count == 0) { continue; } e.ActualId = eventId; for (var index = 0; index < e.Actions.Count; ++index) { SmartEvent actualEvent = e; if (previousWasWait) { actualEvent = smartFactory.EventFactory(SmartConstants.EventTriggerTimed); actualEvent.GetParameter(0).Value = nextTriggerId++; } else if (index > 0) { actualEvent = smartFactory.EventFactory(SmartConstants.EventLink); } int linkTo = e.Actions.Count - 1 == index ? 0 : eventId + 1; SmartAction actualAction = e.Actions[index].Copy(); if (actualAction.Id == SmartConstants.ActionWait) { linkTo = 0; SmartAction waitAction = actualAction; actualAction = smartFactory.ActionFactory(SmartConstants.ActionTriggerTimed, smartFactory.SourceFactory(SmartConstants.SourceNone), smartFactory.TargetFactory(SmartConstants.TargetNone)); actualAction.GetParameter(0).Value = nextTriggerId; actualAction.GetParameter(1).Value = waitAction.GetParameter(0).Value; actualAction.GetParameter(2).Value = waitAction.GetParameter(0).Value; actualAction.Comment = SmartConstants.CommentWait; previousWasWait = true; } else { if (actualAction.Id == SmartConstants.ActionComment) { SmartAction commentAction = actualAction; actualAction = smartFactory.ActionFactory(SmartConstants.ActionNone, smartFactory.SourceFactory(SmartConstants.SourceNone), smartFactory.TargetFactory(SmartConstants.TargetNone)); actualAction.Comment = commentAction.Comment; } previousWasWait = false; } var actionData = smartDataManager.GetRawData(SmartType.SmartAction, actualAction.Id); if (actionData.TargetIsSource) { smartFactory.UpdateTarget(actualAction.Target, actualAction.Source.Id); for (int i = 0; i < actualAction.Target.ParametersCount; ++i) { actualAction.Target.GetParameter(i).Copy(actualAction.Source.GetParameter(i)); } smartFactory.UpdateSource(actualAction.Source, 0); } if (actionData.ImplicitSource != null) { smartFactory.UpdateSource(actualAction.Source, smartDataManager.GetDataByName(SmartType.SmartSource, actionData.ImplicitSource).Id); } else if (actionData.SourceStoreInAction) { actualAction.GetParameter(2).Value = actualAction.Source.Id; actualAction.GetParameter(3).Value = actualAction.Source.GetParameter(0).Value; actualAction.GetParameter(4).Value = actualAction.Source.GetParameter(1).Value; actualAction.GetParameter(5).Value = actualAction.Source.GetParameter(2).Value; } SmartEvent eventToSerialize = actualEvent.ShallowCopy(); eventToSerialize.Parent = script; eventToSerialize.Actions.Add(actualAction); var serialized = eventToSerialize.ToSmartScriptLines(script.EntryOrGuid, script.SourceType, eventId, linkTo); var serializedConditions = actualEvent.ToConditionLines(script.EntryOrGuid, script.SourceType, eventId); if (serialized.Length != 1) { throw new InvalidOperationException(); } lines.Add(serialized[0]); if (serializedConditions != null) { conditions.AddRange(serializedConditions); } eventId++; } } return(lines.ToArray(), conditions.ToArray()); }
public static ISmartScriptLine[] ToWaitFreeSmartScriptLines(this SmartScript script, ISmartFactory smartFactory) { if (script.Events.Count == 0) { return(new ISmartScriptLine[0]); } int eventId = 0; List <ISmartScriptLine> lines = new List <ISmartScriptLine>(); bool previousWasWait = false; int nextTriggerId = script.Events.Where(e => e.Id == SmartConstants.EVENT_TRIGGER_TIMED).Select(e => e.GetParameter(0).Value).DefaultIfEmpty(0).Max() + 1; //@todo: don't use hardcoded IDs!!!! foreach (SmartEvent e in script.Events) { if (e.Actions.Count == 0) { continue; } e.ActualId = eventId; for (int index = 0; index < e.Actions.Count; ++index) { SmartEvent actualEvent = e; if (previousWasWait) { actualEvent = smartFactory.EventFactory(SmartConstants.EVENT_TRIGGER_TIMED); actualEvent.SetParameter(0, nextTriggerId++); } else if (index > 0) { actualEvent = smartFactory.EventFactory(SmartConstants.EVENT_LINK); } int linkTo = (e.Actions.Count - 1 == index ? 0 : eventId + 1); SmartAction actualAction = e.Actions[index]; if (actualAction.Id == SmartConstants.ACTION_WAIT) { linkTo = 0; SmartAction waitAction = actualAction; actualAction = smartFactory.ActionFactory(SmartConstants.ACTION_TRIGGER_TIMED, smartFactory.SourceFactory(SmartConstants.SOURCE_NONE), smartFactory.TargetFactory(SmartConstants.TARGET_NONE)); actualAction.SetParameter(0, nextTriggerId); actualAction.SetParameter(1, waitAction.GetParameter(0).GetValue()); actualAction.SetParameter(2, waitAction.GetParameter(0).GetValue()); actualAction.Comment = SmartConstants.COMMENT_WAIT; previousWasWait = true; } else { previousWasWait = false; } SmartEvent eventToSerialize = actualEvent.ShallowCopy(); eventToSerialize.Actions.Add(actualAction.Copy()); var serialized = eventToSerialize.ToSmartScriptLines(script.EntryOrGuid, script.SourceType, eventId, linkTo); if (serialized.Length != 1) { throw new InvalidOperationException(); } lines.Add(serialized[0]); eventId++; } } return(lines.ToArray()); }