private static CallbackResult OnObjectPicker(byte[] data, CallbackResult prevResult) { lock (syncRoot) { if (CurrentState == State.WaitMenu) { Debug.Assert(waitQueue != null && waitQueue.Count > 0, "Empty waitQueue."); Debug.Assert(waitQueue.Peek().IsMenu, "Target in top of the queue instead of menu."); Menu packet = new Menu(data); WaitQuery query = waitQueue.Dequeue(); MenuSelection menu = query.Menu; if (packet.Title.ToLowerInvariant().Contains(menu.Name.ToLowerInvariant())) { // Cancel menu if (menu.Option == null) { byte[] selectedData = PacketBuilder.ObjectPicked(packet.DialogSerial, packet.MenuSerial, 0, 0, 0); Core.SendToServer(selectedData); FinishWork(); query.Finish(RequestState.Completed); return(CallbackResult.Eat); } // Select option string option = menu.Option.ToLowerInvariant(); for (int i = 0; i < packet.Options.Count; i++) { if (packet.Options[i].Text.ToLowerInvariant().Contains(option)) { byte[] selectedData = PacketBuilder.ObjectPicked(packet.DialogSerial, packet.MenuSerial, (ushort)(i + 1), packet.Options[i].Artwork, packet.Options[i].Hue); Core.SendToServer(selectedData); FinishWork(); query.Finish(RequestState.Completed); return(CallbackResult.Eat); } } Trace.WriteLine("Unable to find requested option. Menu passed to client.", "UIManager"); } else { Trace.WriteLine("Unexpected menu received. Menu passed to client.", "UIManager"); } query.Finish(RequestState.Failed); FinishWork(); } if (CurrentState != State.Ready) { Reset(); } return(CallbackResult.Normal); } }
private static CallbackResult OnServerTarget(byte[] data, CallbackResult prevResult) { lock (syncRoot) { if (CurrentState == State.WaitTarget) { Debug.Assert(waitQueue != null && waitQueue.Count > 0, "Empty waitQueue."); Debug.Assert(!waitQueue.Peek().IsMenu, "Menu is in the top of the queue instead of Target."); TargetData target = TargetData.FromData(data); WaitQuery query = waitQueue.Dequeue(); IClientTarget waitTarget = query.Target; if ((byte)waitTarget.Type > target.Type) { Trace.WriteLine("Unexpected Server target type received. Target passed to client.", "UIManager"); query.Finish(RequestState.Failed); } else { Core.SendToServer(TargetData.ToData(waitTarget, target.TargetId)); FinishWork(); Trace.WriteLine("Processed expected Server target.", "UIManager"); query.Finish(RequestState.Completed); return(CallbackResult.Eat); } } if (CurrentState == State.ServerTarget) { TargetData target = TargetData.FromData(data); if (target.Flags == 0x03) { Trace.WriteLine("Cancel-target packet passed to client.", "UIManager"); return(CallbackResult.Normal); } else { Trace.WriteLine("Warning: Server target updated without previous cancellation.", "UIManager"); clientTarget = target; return(CallbackResult.Normal); } } if (CurrentState != State.Ready) { Reset(); } Debug.Assert(CurrentState == State.Ready, "CurrentState is not Ready. Internal error."); clientTarget = TargetData.FromData(data); CurrentState = State.ServerTarget; } return(CallbackResult.Normal); }
/// <summary> /// /// </summary> /// <param name="menus"></param> /// <returns>Request result for last menu from this list that will be processed.</returns> public static IRequestResult WaitForMenu(params MenuSelection[] menus) { if (menus == null) { throw new ArgumentNullException("menus"); } if (!Core.LoggedIn) { return(UIManager.FailedResult); } if (menus.Length == 0) { WaitQuery query = new WaitQuery(null); query.Finish(RequestState.Completed); return(query); } lock (syncRoot) { if ((CurrentState != State.WaitMenu && CurrentState != State.WaitTarget) || waitQueue == null || waitQueue.Count == 0 || waitQueue.Peek().ThreadId != Thread.CurrentThread.ManagedThreadId) { Reset(); waitQueue = new Queue <WaitQuery>(); CurrentState = State.WaitMenu; } Debug.Assert(waitQueue != null, "waitQueue == null"); WaitQuery query = null; for (int i = 0; i < menus.Length; i++) { query = new WaitQuery(menus[i]); waitQueue.Enqueue(query); } Debug.Assert(query != null); Debug.WriteLine(menus.Length.ToString() + " MenuSelection item(s) enqueued.", "UIManager"); BeginTimeout(WaitMenuTimeout); query.Finish(RequestState.Pending); return(query); } }
/// <summary> /// /// </summary> /// <param name="menus"></param> /// <returns>Request result for last menu from this list that will be processed.</returns> public static IRequestResult WaitForMenu(params MenuSelection[] menus) { if (menus == null) throw new ArgumentNullException("menus"); if (!Core.LoggedIn) return UIManager.FailedResult; if (menus.Length == 0) { WaitQuery query = new WaitQuery(null); query.Finish(RequestState.Completed); return query; } lock (syncRoot) { if ((CurrentState != State.WaitMenu && CurrentState != State.WaitTarget) || waitQueue == null || waitQueue.Count == 0 || waitQueue.Peek().ThreadId != Thread.CurrentThread.ManagedThreadId) { Reset(); waitQueue = new Queue<WaitQuery>(); CurrentState = State.WaitMenu; } Debug.Assert(waitQueue != null, "waitQueue == null"); WaitQuery query = null; for (int i = 0; i < menus.Length; i++) { query = new WaitQuery(menus[i]); waitQueue.Enqueue(query); } Debug.Assert(query != null); Debug.WriteLine(menus.Length.ToString() + " MenuSelection item(s) enqueued.", "UIManager"); BeginTimeout(WaitMenuTimeout); query.Finish(RequestState.Pending); return query; } }