Beispiel #1
0
        public static void BeginRequest <T>(string uri, T state, WebAPIRequestSend <T> send, WebAPIRequestReceive <T> receive)
        {
            VitaNexCore.TryCatch(
                () =>
            {
                CSOptions.ToConsole("Requesting: {0}", uri);

                var request = (HttpWebRequest)WebRequest.Create(uri);

                request.Proxy       = null;
                request.Credentials = null;

                if (send != null)
                {
                    send(request, state);
                }

                if (RequestSend != null)
                {
                    RequestSend(request, state);
                }

                RequestUtility.BeginGetResponse(request, state, receive);
            },
                CSOptions.ToConsole);
        }
Beispiel #2
0
        public override void OnThink()
        {
            base.OnThink();

            if (Deleted || Map == null || Map == Map.Internal || !this.InCombat())
            {
                return;
            }

            var now = DateTime.UtcNow;

            if (now > _NextAISwitch)
            {
                _NextAISwitch = now + AISwitchInterval;

                SwitchAI();
            }

            if (!this.InCombat(TimeSpan.Zero) || now <= _NextSpecial)
            {
                return;
            }

            TimeSpan delay;
            var      ability = GetSpecialAbility(out delay);

            if (ability == null)
            {
                return;
            }

            _NextSpecial = now + delay;

            VitaNexCore.TryCatch(ability, x => x.ToConsole(true));
        }
Beispiel #3
0
        public static bool Rewrite(this Packet p, int offset, sbyte value, bool reset = true)
        {
            if (p == null || p.UnderlyingStream == null || offset < 0)
            {
                return(false);
            }

            var success = false;

            VitaNexCore.TryCatch(
                () =>
            {
                var o = p.UnderlyingStream.Position;

                p.UnderlyingStream.Position = offset;
                p.UnderlyingStream.Write(value);

                if (reset)
                {
                    p.UnderlyingStream.Position = o;
                }

                success = true;
            });

            return(success);
        }
Beispiel #4
0
        public void EndTransaction()
        {
            if (_Transaction == null)
            {
                return;
            }

            using (_Transaction)
            {
                if (_Transaction.Connection == null)
                {
                    _Transaction = null;
                    return;
                }

                VitaNexCore.TryCatch(
                    _Transaction.Commit,
                    x =>
                {
                    MySQL.CSOptions.ToConsole(x);

                    VitaNexCore.TryCatch(_Transaction.Rollback, MySQL.CSOptions.ToConsole);
                });

                _Transaction = null;
            }
        }
Beispiel #5
0
        private static bool Backup(bool restore)
        {
            if (restore)
            {
                return(VitaNexCore.TryCatchGet(
                           () =>
                {
                    World.Save(false, false);
                    //World.WaitForWriteCompletion();

                    return true;
                },
                           CSOptions.ToConsole));
            }

            return(VitaNexCore.TryCatchGet(
                       () =>
            {
                DirectoryInfo target = IOUtility.EnsureDirectory(BackupTarget + "/" + _Stamp.ToSimpleString("D d M y - t@h-m@"));

                Parallel.ForEach(
                    GetFiles(BackupSource),
                    file =>
                    VitaNexCore.TryCatch(
                        () =>
                        file.CopyTo(IOUtility.EnsureFile(file.FullName.Replace(BackupSource.FullName, target.FullName)).FullName, true),
                        CSOptions.ToConsole));

                return true;
            },
                       CSOptions.ToConsole));
        }
Beispiel #6
0
        public static void Sync()
        {
            VitaNexCore.TryCatch(
                () =>
            {
                int added = 0;

                foreach (var t in TypeOfEquipmentSet.GetConstructableChildren())
                {
                    Sets.AddOrReplace(
                        t,
                        set =>
                    {
                        if (set == null)
                        {
                            set = t.CreateInstanceSafe <EquipmentSet>();

                            if (set != null)
                            {
                                ++added;
                            }
                        }

                        return(set);
                    });
                }

                if (added > 0)
                {
                    CMOptions.ToConsole("{0:#,0} new sets added.", added);
                }
            },
                CMOptions.ToConsole);
        }
Beispiel #7
0
 public static void CloseAll()
 {
     foreach (var p in Profiles.Where(p => p.Key != null && p.Value != null && p.Key.IsOnline()))
     {
         VitaNexCore.TryCatch(() => p.Value.GetToolbarGump().Close(true), CMOptions.ToConsole);
     }
 }
Beispiel #8
0
        private static void Send(
            WebSocketsClient client,
            string data,
            bool encode,
            bool compress,
            Action <WebSocketsClient, byte[]> callback)
        {
            VitaNexCore.TryCatch(
                () =>
            {
                int len;
                byte[] buffer;

                if (encode)
                {
                    Encode(data, out buffer, out len);
                }
                else
                {
                    buffer = data.Select(c => (byte)c).ToArray();
                    len    = buffer.Length;
                }

                Send(client, buffer, len, compress, callback);
            },
                CMOptions.ToConsole);
        }
Beispiel #9
0
        public bool UpdateStatistics(PvPTeam t, PlayerMobile pm, Action <PvPProfileHistoryEntry> update)
        {
            if (t == null || t.Deleted || pm == null || update == null)
            {
                return(false);
            }

            var s = t.GetStatistics(pm);

            if (s == null)
            {
                return(false);
            }

            var success = true;

            VitaNexCore.TryCatch(
                update,
                s,
                x =>
            {
                AutoPvP.CMOptions.ToConsole(x);
                success = false;
            });

            if (t.IsMember(pm))
            {
                t.UpdateActivity(pm);
            }

            return(success);
        }
Beispiel #10
0
        private static void ListenAsync()
        {
            AcquireListener();

            if (Listener == null)
            {
                return;
            }

            VitaNexCore.TryCatch(
                () => Listener.BeginAcceptTcpClient(
                    r =>
            {
                var client = VitaNexCore.TryCatchGet(() => Listener.EndAcceptTcpClient(r), CMOptions.ToConsole);

                if (client != null && client.Connected)
                {
                    VitaNexCore.TryCatch(() => Connected(client), CMOptions.ToConsole);
                }

                ListenAsync();
            },
                    null),
                e =>
            {
                _Listening = false;
                CMOptions.ToConsole(e);
            });
        }
Beispiel #11
0
        private static void Connected(WebSocketsClient client)
        {
            lock (Clients)
            {
                if (!Clients.Contains(client))
                {
                    Clients.Add(client);
                }
            }

            CMOptions.ToConsole("[{0}] Client connected: {1}", Clients.Count, client.Address);

            if (OnConnected != null)
            {
                VitaNexCore.TryCatch(
                    () => OnConnected(client),
                    e =>
                {
                    CMOptions.ToConsole(e);

                    client.Dispose();
                    Disconnected(client);
                });
            }
        }
Beispiel #12
0
        public override void OnServerClose(NetState owner)
        {
            if (IsDisposed)
            {
                return;
            }

            IsOpen = false;

            UnregisterInstance();

            if (!Hidden)
            {
                VitaNexCore.TryCatch(
                    () =>
                {
                    if (InstancePoller != null)
                    {
                        InstancePoller.Dispose();
                        InstancePoller = null;
                    }
                });
            }

            base.OnServerClose(owner);
        }
Beispiel #13
0
        /// <summary>
        ///     Copies the contents of the specified directory to the specified target directory, only including files that meet
        ///     the mask criteria
        /// </summary>
        /// <param name="source">Directory to copy</param>
        /// <param name="dest">Directory to copy to</param>
        /// <param name="mask">String mask to use to filter file names</param>
        /// <param name="option">Search options</param>
        public static void CopyDirectory(this DirectoryInfo source, DirectoryInfo dest, string mask, SearchOption option)
        {
            source.Refresh();

            if (!source.Exists)
            {
                return;
            }

            EnsureDirectory(dest, false);

            foreach (var f in AllFiles(source, mask, option))
            {
                VitaNexCore.TryCatch(
                    () =>
                {
                    var t = new FileInfo(f.FullName.Replace(source.FullName, dest.FullName));

                    EnsureDirectory(t.Directory);

                    f.CopyTo(t.FullName, true);
                });
            }

            source.Refresh();
            dest.Refresh();
        }
Beispiel #14
0
        /// <summary>
        ///     Ensures a files' existence
        /// </summary>
        /// <param name="file"></param>
        /// <param name="replace">True: replace the file if it exists</param>
        /// <returns>FileInfo representing the file ensured for 'info'</returns>
        public static FileInfo EnsureFile(this FileInfo file, bool replace)
        {
            file.Refresh();

            EnsureDirectory(file.Directory, false);

            if (!file.Exists)
            {
                using (var fs = file.Create())
                {
                    fs.Close();
                }
            }
            else if (replace)
            {
                VitaNexCore.TryCatch(file.Delete);

                using (var fs = file.Create())
                {
                    fs.Close();
                }
            }

            file.Refresh();

            return(file);
        }
Beispiel #15
0
        public static void Serialize(this FileInfo file, Action <GenericWriter> handler, bool truncate)
        {
            if (file == null || handler == null)
            {
                return;
            }

            file = file.EnsureFile(truncate);

            Interlocked.Increment(ref _PendingWriters);

            VitaNexCore.TryCatch(
                () =>
            {
                using (var stream = GetStream(file))
                {
                    var writer = GetBinaryWriter(stream);
                    handler(writer);
                    writer.Close();
                }
            },
                VitaNexCore.ToConsole);

            Interlocked.Decrement(ref _PendingWriters);
        }
Beispiel #16
0
 private static void InternalClose(SuperGump g)
 {
     if (g != null)
     {
         VitaNexCore.TryCatch(() => InternalClose(g.User, g));
     }
 }
Beispiel #17
0
        public static void ReadBlock(this GenericReader reader, Action <GenericReader> onDeserialize)
        {
            using (var ms = new MemoryStream())
            {
                var length    = reader.ReadLong() - 8;
                var chunkSize = (int)Math.Min(0xFFFF, length);
                var chunk     = new byte[chunkSize];

                while (length > 0)
                {
                    chunkSize = (int)Math.Min(chunkSize, length);

                    for (var i = 0; i < chunkSize; i++)
                    {
                        chunk[i] = reader.ReadByte();
                    }

                    ms.Write(chunk, 0, chunkSize);

                    length -= chunkSize;
                }

                ms.Seek(0, SeekOrigin.Begin);

                VitaNexCore.TryCatch(onDeserialize, ms.GetBinaryReader());
            }
        }
Beispiel #18
0
        private static void ProcessHeads(int i)
        {
            if (_Processing)
            {
                return;
            }

            _Processing = true;

            VitaNexCore.TryCatch(
                () =>
            {
                DateTime now = DateTime.UtcNow;

                int skip = i * 5000;
                int take = i == _ExpireTimers.Length - 1 ? AllHeads.Count - skip : (1 + i) * 5000;

                foreach (var h in AllHeads.AsParallel().Skip(skip).Take(take).Where(h => h.ExpireDate <= now).ToArray())
                {
                    h.Expire();
                    AllHeads.Remove(h);
                }

                AllHeads.RemoveAll(h => h == null || h.Deleted);
            },
                x => x.ToConsole(true));

            _Processing = false;
        }
Beispiel #19
0
        public void Close(bool disconnecting)
        {
            if (IsDisposed || !Client.Connected)
            {
                return;
            }

            VitaNexCore.TryCatch(
                () =>
            {
                if (!disconnecting)
                {
                    WebAPI.Disconnect(this);
                }

                if (Stream != null)
                {
                    using (Stream)
                    {
                        Stream.Close();
                    }
                }

                if (Client != null)
                {
                    Client.Close();
                }
            },
                WebAPI.CSOptions.ToConsole);
        }
Beispiel #20
0
        private static void CSInvoke()
        {
            _CreateCorpseSuccessor     = Mobile.CreateCorpseHandler;
            Mobile.CreateCorpseHandler = HandleCreateCorpse;

            Dungeons.Values.Where(d => d != null && !d.Deleted).ForEach(d => VitaNexCore.TryCatch(d.Init, CSOptions.ToConsole));
        }
Beispiel #21
0
        public static void Initialize()
        {
            if (_Initialized)
            {
                return;
            }

            EventSink.Login += e =>
            {
                if (e.Mobile == null || e.Mobile.Deleted || e.Mobile.Backpack == null)
                {
                    return;
                }

                VitaNexCore.TryCatch(
                    () =>
                {
                    var momentos = e.Mobile.Backpack.FindItemsByType <HauntedMomento>(true);

                    momentos.ForEach(
                        m =>
                    {
                        if (m != null && !m.Deleted)
                        {
                            m.InvalidateEntity();
                        }
                    });

                    momentos.Clear();
                });
            };

            _Initialized = true;
        }
Beispiel #22
0
        public static void OnDisconnectedImpl(DisconnectedEventArgs e)
        {
            var user = e.Mobile;

            if (user == null)
            {
                return;
            }

            lock (_InstanceLock)
            {
                if (!Instances.ContainsKey(user))
                {
                    return;
                }
            }

            VitaNexCore.TryCatch(
                () =>
            {
                foreach (var g in EnumerateInstances <SuperGump>(user, true))
                {
                    g.Close(true);
                }
            },
                x => x.ToConsole(true));
        }
Beispiel #23
0
        private static void Delta()
        {
            VitaNexCore.TryCatch(
                () =>
            {
                _LastOnline.Clear();
                _LastOnline.AddRange(
                    NetState.Instances.Where(s => s != null && s.Mobile is PlayerMobile).Select(s => (PlayerMobile)s.Mobile));
                SaveState(true);
            },
                CSOptions.ToConsole);

            VitaNexCore.TryCatch(
                () =>
            {
                CSOptions.ToConsole("Disposing NetStates/Listeners...");
                NetState.Instances.Where(s => s != null && s.Socket != null).ForEach(s => s.Dispose());
                Core.MessagePump.Listeners.ForEach(l => l.Dispose());
                Core.MessagePump.Listeners = new Listener[0];
                CSOptions.ToConsole("Done");

                AutoSave.SavesEnabled = false;
            },
                CSOptions.ToConsole);
        }
Beispiel #24
0
        private void AnimateList()
        {
            VitaNexCore.TryCatch(
                () =>
            {
                var p = this;

                foreach (var g in EnumerateInstances <NotifyGump>(User, true)
                         .Where(g => g != this && g.IsOpen && !g.IsDisposed && g.Y >= p.Y)
                         .OrderBy(g => g.Y))
                {
                    g.Y = p.Y + p.FrameHeight;

                    p = g;

                    if (g.State != AnimState.Pause)
                    {
                        return;
                    }

                    var lr = g.LastAutoRefresh;

                    g.Refresh(true);
                    g.LastAutoRefresh = lr;
                }
            },
                e => e.ToConsole());
        }
Beispiel #25
0
        public static void Broadcast <TGump>(
            string html,
            bool autoClose            = true,
            double delay              = 1.0,
            double pause              = 5.0,
            Color?color               = null,
            Action <TGump> beforeSend = null,
            Action <TGump> afterSend  = null,
            AccessLevel level         = AccessLevel.Player)
            where TGump : NotifyGump
        {
            var c = NetState.Instances.Count;

            while (--c >= 0)
            {
                if (!NetState.Instances.InBounds(c))
                {
                    continue;
                }

                var ns = NetState.Instances[c];

                if (ns != null && ns.Running && ns.Mobile != null && ns.Mobile.AccessLevel >= level)
                {
                    VitaNexCore.TryCatch(
                        m => Send(false, m, html, autoClose, delay, pause, color, beforeSend, afterSend, level),
                        ns.Mobile);
                }
            }

            if (level < AccessLevel.Counselor && OnBroadcast != null)
            {
                OnBroadcast(html.ParseBBCode());
            }
        }
Beispiel #26
0
        protected virtual void OnProductionTimerTick()
        {
            InvalidateProperties();
            Delta(ItemDelta.Properties);

            VitaNexCore.TryCatch(Produce, TrashCollection.CMOptions.ToConsole);
        }
Beispiel #27
0
        private static void Update()
        {
            if (ActionsToExport.Count > 0 && PlayersToExport.Count > 0 && HandsToExport.Count > 0)
            {
                if (ExportThread != null)
                {
                    if (ExportThread.IsAlive || ExportThread.ThreadState == ThreadState.Running)
                    {
                        ExportThread.Abort();
                        ExportThread.Join(3000);
                    }

                    ExportThread = null;
                }

                VitaNexCore.TryCatch <Action>(
                    ConnectAsync,
                    () =>
                {
                    ExportThread = new Thread(UpdateCallback)
                    {
                        Name     = CMOptions.ModuleName,
                        Priority = ThreadPriority.BelowNormal
                    };

                    ExportThread.Start();
                },
                    x =>
                {
                    CMOptions.ToConsole(x);
                    Cancel();
                });
            }
        }
Beispiel #28
0
        public void Send()
        {
            if (Sending)
            {
                return;
            }

            Sending = true;

            VitaNexCore.TryCatch(
                () =>
            {
                Update();

                if (Count == 0 || this[0] == null)
                {
                    return;
                }

                Processing = true;

                CurrentQueue = this[0];
                CurrentQueue.Process();

                OnSend();
            },
                e => e.ToConsole());
        }
Beispiel #29
0
        public static void WriteBlock <T>(this GenericWriter writer, Action <GenericWriter, T> onSerialize, T state)
        {
            using (var ms = new MemoryStream())
            {
                var bw = ms.GetBinaryWriter();

                VitaNexCore.TryCatch(w => onSerialize(w, state), bw);

                bw.Flush();

                ms.Seek(0, SeekOrigin.Begin);

                var length    = ms.Length;
                var chunkSize = (int)Math.Min(0xFFFF, length);
                var chunk     = new byte[chunkSize];

                writer.Write(length + 8);

                while (length > 0)
                {
                    chunkSize = (int)Math.Min(chunkSize, length);

                    ms.Read(chunk, 0, chunkSize);

                    for (var i = 0; i < chunkSize; i++)
                    {
                        writer.Write(chunk[i]);
                    }

                    length -= chunkSize;
                }
            }
        }
Beispiel #30
0
        public override void OnThink()
        {
            base.OnThink();

            DateTime now = DateTime.UtcNow;

            if (now > _NextAISwitch)
            {
                _NextAISwitch = now + AISwitchInterval;

                SwitchAI();
            }

            if (!this.InCombat() || now <= _NextSpecial)
            {
                return;
            }

            TimeSpan delay;
            Action   ability = GetSpecialAbility(out delay);

            if (ability == null)
            {
                return;
            }

            _NextSpecial = now + delay;

            VitaNexCore.TryCatch(ability, x => x.ToConsole(true));
        }