public void Dispose() { mConfig = null; mEmulator = null; mCaptureResult = null; mDisposed = true; }
public static BBMacro DeserializeMacro(BinaryReader reader) { BBMacro ret = null; BBMacroType type = (BBMacroType)reader.ReadByte(); switch (type) { case BBMacroType.If: ret = new BBIfMacro(); break; case BBMacroType.Series: ret = new BBSeriesMacro(); break; case BBMacroType.Parallel: ret = new BBParallelMacro(); break; case BBMacroType.Normal: ret = new BBMacro(); break; } ret.Deserialize(reader); return(ret); }
public static void Export(BBConfig config, BBMacro macro, string path) { StringBuilder builder = new StringBuilder(); config.ToCString(builder, "config"); builder.AppendLine(); macro.ToCString(builder, "macro"); File.WriteAllText(path, builder.ToString()); }
public static byte[] SerializedMacro(BBMacro macro) { using (var stream = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(stream); SerializedMacro(macro, writer); return(stream.ToArray()); } }
private BBMacro Merge(List <BBInputSnapshot> snapshots) { if (snapshots.Count < 2) { throw new System.InvalidOperationException(); } if (snapshots[0].type == BBInputType.Press) { if (snapshots[snapshots.Count - 1].type != BBInputType.Release) { throw new System.InvalidOperationException(); } BBMacro macro = new BBMacro(); macro.button = snapshots[0].button; macro.duration = snapshots[snapshots.Count - 1].timeStamp - snapshots[0].timeStamp; if (snapshots.Count == 2) { macro.code = BBMacroOpCode.Click; var coord = mConfig.Screen2Axis(snapshots[0].inputPosition); macro.data = coord; } else { macro.code = BBMacroOpCode.Drag; macro.data = new float[snapshots.Count * 2]; for (int i = 0; i < snapshots.Count; i++) { var coord = mConfig.Screen2Axis(snapshots[i].inputPosition); macro.data[i * 2] = coord[0]; macro.data[i * 2 + 1] = coord[1]; } } return(macro); } else if (snapshots[0].type == BBInputType.KeyDown) { if (snapshots.Count != 2 || snapshots[1].type != BBInputType.KeyUp) { throw new System.InvalidOperationException(); } BBMacro macro = new BBMacro(); macro.code = BBMacroOpCode.KeyPress; macro.key = snapshots[0].key; macro.duration = snapshots[1].timeStamp - snapshots[0].timeStamp; return(macro); } else { throw new System.InvalidOperationException(); } }
public void Play() { if (!mIsPlaying) { mIsPlaying = true; mCurrent = mHead; mCurrent.RunScript(); mCurrentTimes = 1; mCurrentTime = 0; mCurrentDelay = mCurrent.delay; mEmulator.Clear(); } }
public bool Test(out BBMacro item) { for (int i = 0; i < testCompileResults.Length; i++) { if (testCompileResults[i] != null && testCompileResults[i]()) { item = items[i]; return(true); } } item = null; return(false); }
public static void Load(string path, out BBConfig config, out BBMacro macro) { if (!File.Exists(path)) { throw new InvalidDataException(); } using (FileStream stream = new FileStream(path, FileMode.Open)) { stream.Position = 0; BinaryReader reader = new BinaryReader(stream); config = new BBConfig(); config.Deserialize(reader); macro = Load(reader); } }
public static void Save(BBConfig config, BBMacro macro, string path) { if (macro == null) { throw new InvalidDataException(); } using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) { stream.SetLength(0); BinaryWriter writer = new BinaryWriter(stream); config.Serialize(writer); Save(macro, writer); stream.Flush(); } }
private static void Save(BBMacro macro, BinaryWriter writer) { BBMacro.SerializedMacro(macro, writer); switch (macro.macroType) { case BBMacroType.If: { BBIfMacro ifmacro = macro as BBIfMacro; writer.Write(ifmacro.items.Length); for (int i = 0; i < ifmacro.items.Length; i++) { Save(ifmacro.items[i], writer); } break; } case BBMacroType.Series: { BBSeriesMacro srmacro = macro as BBSeriesMacro; writer.Write(srmacro.items.Length); for (int i = 0; i < srmacro.items.Length; i++) { Save(srmacro.items[i], writer); } break; } case BBMacroType.Parallel: { BBParallelMacro prmacro = macro as BBParallelMacro; writer.Write(prmacro.items.Length); for (int i = 0; i < prmacro.items.Length; i++) { Save(prmacro.items[i], writer); } break; } } if (macro.next != null) { writer.Write(true); Save(macro.next, writer); } else { writer.Write(false); } }
private static BBMacro Load(BinaryReader reader) { BBMacro macro = BBMacro.DeserializeMacro(reader); switch (macro.macroType) { case BBMacroType.If: { BBIfMacro ifmacro = macro as BBIfMacro; int itemLen = reader.ReadInt32(); ifmacro.items = new BBMacro[itemLen]; for (int i = 0; i < itemLen; i++) { ifmacro.items[i] = Load(reader); } break; } case BBMacroType.Series: { BBSeriesMacro srmacro = macro as BBSeriesMacro; int itemLen = reader.ReadInt32(); srmacro.items = new BBMacro[itemLen]; for (int i = 0; i < itemLen; i++) { srmacro.items[i] = Load(reader); } break; } case BBMacroType.Parallel: { BBParallelMacro prmacro = macro as BBParallelMacro; int itemLen = reader.ReadInt32(); prmacro.items = new BBMacro[itemLen]; for (int i = 0; i < itemLen; i++) { prmacro.items[i] = Load(reader); } break; } } if (reader.ReadBoolean()) { macro.next = Load(reader); } return(macro); }
public void Dispose() { mConfig = null; mEmulator = null; mHead = null; mCurrent = null; if (mInterrupts != null) { for (int i = mInterrupts.Count - 1; i >= 0; i--) { mInterrupts[i].Dispose(); } mInterrupts = null; } mDisposed = true; }
public void Update(float deltaTime) { mEmulator.Update(deltaTime); BBInputSnapshot current = mEmulator.recordHead; while (current != null) { if (current.type == BBInputType.Release || current.type == BBInputType.KeyUp) { mCaptureResult = StopCapture(); break; } current = current.next; } }
private void Execute(BBMacro macro, out BBLoopAction action) { action = BBLoopAction.Continue; switch (macro.macroType) { case BBMacroType.If: BBIfMacro ifmacro = macro as BBIfMacro; BBMacro future = null; if (ifmacro.Test(out future)) { BBMacroPlayer inserted = new BBMacroPlayer(mConfig, mModuleProxy); inserted.Set(future); inserted.Play(); mInterrupts.Add(inserted); } else { action = ifmacro.action; } break; case BBMacroType.Series: BBSeriesMacro srmacro = macro as BBSeriesMacro; foreach (BBMacro item in srmacro.items) { BBMacroPlayer inserted = new BBMacroPlayer(mConfig, mModuleProxy); inserted.Set(item); mSeriesQueue.Enqueue(inserted); } break; case BBMacroType.Parallel: BBParallelMacro prmacro = macro as BBParallelMacro; foreach (BBMacro item in prmacro.items) { BBMacroPlayer inserted = new BBMacroPlayer(mConfig, mModuleProxy); inserted.Set(item); inserted.Play(); mInterrupts.Add(inserted); } break; default: mEmulator.Set(Expand(macro)); break; } }
public virtual BBMacro Clone(bool deepClone) { BBMacro macro = null; switch (macroType) { case BBMacroType.If: macro = new BBIfMacro(); break; case BBMacroType.Series: macro = new BBSeriesMacro(); break; case BBMacroType.Parallel: macro = new BBParallelMacro(); break; case BBMacroType.Normal: macro = new BBMacro(); break; } macro.code = code; macro.button = button; macro.key = key; macro.data = new float[data.Length]; Array.Copy(data, macro.data, data.Length); macro.times = times; macro.duration = duration; macro.delay = delay; macro.script = script; macro.scriptData = scriptData; if (deepClone) { if (next != null) { macro.next = next.Clone(deepClone); } } return(macro); }
private BBMacro Parse(List <BBInputSnapshot> snapshots) { BBMacro head = null; BBMacro current = null; for (int i = 0; i < snapshots.Count; i++) { BBInputSnapshot snapshot = snapshots[i]; BBMacro macro = new BBMacro(); macro.button = snapshot.button; macro.code = BBUtil.InputType2OpCode(snapshot.type); macro.key = snapshot.key; if (BBUtil.IsStrokeInputType(snapshot.type)) { var coord = mConfig.Screen2Axis(snapshot.inputPosition); if (snapshot.type == BBInputType.Wheel) { macro.data = new float[] { coord[0], coord[1], snapshot.delta }; } else { macro.data = coord; } } if (head == null) { head = macro; current = head; } else { current.next = macro; current = current.next; } } return(head); }
public BBMacro StopRecord() { if (!isRecording) { throw new System.InvalidOperationException(); } mEmulator.StopRecord(); BBMacro head = null; BBMacro current = null; BBInputSnapshot snapTail = null; bool parted = false; var elements = new List <BBInputSnapshot>(); var currentSnap = mEmulator.recordHead; while (currentSnap != null) { parted = false; elements.Add(currentSnap); if (elements.Count == 1) { if (currentSnap.type == BBInputType.Press || currentSnap.type == BBInputType.KeyDown) { // Wait to merge } else { parted = true; } } if (elements.Count > 1) { if (currentSnap.type == BBInputType.Move) { // Wait to merge } else if ((currentSnap.type == BBInputType.Release && currentSnap.button == elements[0].button) || currentSnap.type == BBInputType.KeyUp) { var fusion = Merge(elements); if (head == null) { head = fusion; current = head; } else { current.next = fusion; current = current.next; current.delay = elements[0].timeStamp - snapTail.timeStamp; } snapTail = currentSnap; elements.Clear(); } else { parted = true; } } if (parted) { BBMacro fragment = Parse(elements); if (head == null) { head = fragment; current = head; } else { current.next = fragment; current = current.next; } for (int i = 0; i < elements.Count; i++) { if (snapTail != null) { current.delay = elements[i].timeStamp - snapTail.timeStamp; } snapTail = elements[i]; if (i != elements.Count - 1) { current = current.next; } } elements.Clear(); } currentSnap = currentSnap.next; } return(head); }
private BBInputSnapshot Expand(BBMacro macro) { BBInputSnapshot expand = new BBInputSnapshot(); expand.button = macro.button; switch (macro.code) { case BBMacroOpCode.Press: { expand.type = BBInputType.Press; expand.inputPosition = mConfig.Axis2Screen(new float[] { macro.data[0], macro.data[1] }); expand.duration = macro.duration; break; } case BBMacroOpCode.Release: { expand.type = BBInputType.Release; expand.inputPosition = mConfig.Axis2Screen(new float[] { macro.data[0], macro.data[1] }); expand.duration = macro.duration; break; } case BBMacroOpCode.Move: { expand.type = BBInputType.Move; expand.inputPosition = mConfig.Axis2Screen(new float[] { macro.data[0], macro.data[1] }); expand.duration = macro.duration; break; } case BBMacroOpCode.Click: { float[] inputPosition = mConfig.Axis2Screen(new float[] { macro.data[0], macro.data[1] }); expand.type = BBInputType.Press; expand.inputPosition = inputPosition; expand.duration = macro.duration; expand.next = new BBInputSnapshot() { button = expand.button, type = BBInputType.Release, inputPosition = inputPosition, }; break; } case BBMacroOpCode.Drag: { expand.type = BBInputType.Press; expand.inputPosition = mConfig.Axis2Screen(new float[] { macro.data[0], macro.data[1] }); int dataLen2 = macro.data.Length / 2; float duration = macro.duration / dataLen2; BBInputSnapshot current = expand; for (int i = 0; i < dataLen2; i++) { current.next = new BBInputSnapshot() { button = current.button, type = BBInputType.Move, inputPosition = mConfig.Axis2Screen(new float[] { macro.data[i * 2], macro.data[i * 2 + 1] }), duration = duration, }; current = current.next; } current.next = new BBInputSnapshot() { button = current.button, type = BBInputType.Release, inputPosition = current.inputPosition, }; break; } case BBMacroOpCode.Wheel: { expand.type = BBInputType.Wheel; expand.inputPosition = mConfig.Axis2Screen(new float[] { macro.data[0], macro.data[1] }); expand.delta = (int)macro.data[2]; expand.duration = macro.duration; break; } case BBMacroOpCode.KeyDown: { expand.type = BBInputType.KeyDown; expand.key = macro.key; expand.duration = macro.duration; break; } case BBMacroOpCode.KeyUp: { expand.type = BBInputType.KeyUp; expand.key = macro.key; expand.duration = macro.duration; break; } case BBMacroOpCode.KeyPress: { expand.type = BBInputType.KeyDown; expand.key = macro.key; expand.duration = macro.duration; expand.next = new BBInputSnapshot() { type = BBInputType.KeyUp, key = macro.key }; break; } } return(expand); }
public void Set(BBMacro macro) { mHead = macro; }
public static void SerializedMacro(BBMacro macro, BinaryWriter writer) { writer.Write((byte)macro.macroType); macro.Serialize(writer); }
public void Update(float deltaTime) { if (mIsPlaying && !mIsPaused) { if (mInterrupts.Count > 0) { for (int i = mInterrupts.Count - 1; i >= 0; i--) { if (!mInterrupts[i].isPlaying) { mInterrupts[i].Dispose(); mInterrupts.RemoveAt(i); } else { mInterrupts[i].Update(deltaTime); } } } else if (mSeriesQueue.Count > 0) { BBMacroPlayer player = mSeriesQueue.Dequeue(); player.Play(); mInterrupts.Add(player); } else if (mCurrent == null) { mIsPlaying = false; } else { if (mCurrentTime > 0) { mCurrentTime -= deltaTime; } else { mCurrentDelay -= deltaTime; } if (mEmulator.isPlaying) { mEmulator.Update(deltaTime); } else { while ((mCurrentDelay < 0 || mCurrentTime < 0) && !mEmulator.isPlaying && mInterrupts.Count == 0) { if (mCurrentTime < 0) { if (mCurrentTimes < mCurrent.times) { mCurrentTimes += 1; } else { mCurrent = mCurrent.next; if (mCurrent == null) { break; } } mCurrent.RunScript(); mCurrentDelay += mCurrent.delay; mCurrentDelay += mCurrentTime; mCurrentTime = 0; } if (mCurrentDelay < 0) { BBLoopAction action = BBLoopAction.Continue; Execute(mCurrent, out action); if (action == BBLoopAction.Continue) { mCurrentTime += mCurrent.duration; mCurrentTime += mCurrentDelay; } else if (action == BBLoopAction.Break) { mCurrentTimes = mCurrent.times; mCurrentTime = -float.Epsilon; } mCurrentDelay = 0; } } } } } }