Exemplo n.º 1
0
        private static RegionEntry[] LoadRegionIndex(BinaryReader idxReader)
        {
            int count = idxReader.ReadInt32(), skipped = 0;

            RegionEntry[] entries = new RegionEntry[count];

            for (int i = 0; i < count; ++i)
            {
                idxReader.ReadInt32();                //no typeid for regions
                int  serial = idxReader.ReadInt32();
                long pos    = idxReader.ReadInt64();
                int  length = idxReader.ReadInt32();

                if (serial == Serial.MinusOne)
                {
                    ++skipped;
                    continue;
                }

                Region region = Region.FindByUId(serial);
                if (region == null)
                {
                    ++skipped;
                    continue;
                }

                entries[i] = new RegionEntry(region, pos, length);
                Region.AddRegion(region);
            }

            if (skipped > 0)
            {
                log.WarnFormat("{0} regions were skipped", skipped);
                m_LoadErrors += skipped;
            }

            return(entries);
        }
Exemplo n.º 2
0
        public static void Load()
        {
            BinaryReader    reader1;
            BinaryReader    reader2;
            int             num5;
            ArrayList       list5;
            int             num6;
            string          text1;
            Type            type1;
            ConstructorInfo info1;
            int             num7;
            int             num8;
            int             num9;
            long            num10;
            int             num11;

            object[]        objArray2;
            Mobile          mobile1;
            ConstructorInfo info2;
            string          text2;
            BinaryReader    reader3;
            BinaryReader    reader4;
            int             num12;
            ArrayList       list6;
            int             num13;
            string          text3;
            Type            type2;
            ConstructorInfo info3;
            int             num14;
            int             num15;
            int             num16;
            long            num17;
            int             num18;

            object[]             objArray3;
            Item                 item1;
            ConstructorInfo      info4;
            string               text4;
            BinaryReader         reader5;
            CreateGuildEventArgs args1;
            int              num19;
            int              num20;
            long             num21;
            int              num22;
            BaseGuild        guild1;
            BinaryReader     reader6;
            int              num23;
            int              num24;
            long             num25;
            int              num26;
            Region           region1;
            BinaryFileReader reader7;
            int              num28;
            MobileEntry      entry1;
            Mobile           mobile2;
            BinaryFileReader reader8;
            int              num29;
            ItemEntry        entry2;
            Item             item2;
            BinaryFileReader reader9;
            int              num30;
            GuildEntry       entry3;
            BaseGuild        guild2;
            BinaryFileReader reader10;
            int              num31;
            RegionEntry      entry4;
            Region           region2;
            int              num32;
            int              num33;
            int              num34;
            object           obj1;

            object[] objArray4;
            if (World.m_Loaded)
            {
                return;
            }
            World.m_Loaded      = true;
            World.m_LoadingType = null;
            Console.Write("World: Loading...");
            DateTime time1 = DateTime.Now;

            World.m_Loading    = true;
            World.m_DeleteList = new ArrayList();
            int num1 = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;

            object[] objArray1  = new object[1];
            Type[]   typeArray2 = new Type[1];
            typeArray2[0] = typeof(Serial);
            Type[]    typeArray1 = typeArray2;
            ArrayList list1      = new ArrayList();
            ArrayList list2      = new ArrayList();
            ArrayList list3      = new ArrayList();
            ArrayList list4      = new ArrayList();

            if (File.Exists(World.mobIdxPath) && File.Exists(World.mobTdbPath))
            {
                using (FileStream stream1 = new FileStream(World.mobIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader1 = new BinaryReader(stream1);
                    using (FileStream stream2 = new FileStream(World.mobTdbPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        reader2 = new BinaryReader(stream2);
                        num5    = reader2.ReadInt32();
                        list5   = new ArrayList(num5);
                        num6    = 0;
                        while ((num6 < num5))
                        {
                            text1 = reader2.ReadString();
                            type1 = ScriptCompiler.FindTypeByFullName(text1);
                            if (type1 == null)
                            {
                                Console.WriteLine("failed");
                                Console.WriteLine("Error: Type \'{0}\' was not found. Delete all of those types? (y/n)", text1);
                                if (Console.ReadLine() == "y")
                                {
                                    list5.Add(null);
                                    Console.Write("World: Loading...");
                                    goto Label_018C;
                                }
                                Console.WriteLine("Types will not be deleted. An exception will be thrown when you press return");
                                throw new Exception(string.Format("Bad type \'{0}\'", text1));
                            }
                            info1 = type1.GetConstructor(typeArray1);
                            if (info1 != null)
                            {
                                objArray4    = new object[2];
                                objArray4[0] = info1;
                                list5.Add(objArray4);
                            }
                            else
                            {
                                throw new Exception(string.Format("Type \'{0}\' does not have a serialization constructor", type1));
                            }
Label_018C:
                            ++num6;
                        }
                        num1            = reader1.ReadInt32();
                        World.m_Mobiles = new Hashtable(num1);
                        for (num7 = 0; (num7 < num1); ++num7)
                        {
                            num8      = reader1.ReadInt32();
                            num9      = reader1.ReadInt32();
                            num10     = reader1.ReadInt64();
                            num11     = reader1.ReadInt32();
                            objArray2 = ((object[])list5[num8]);
                            if (objArray2 != null)
                            {
                                mobile1 = null;
                                info2   = ((ConstructorInfo)objArray2[0]);
                                text2   = ((string)objArray2[1]);
                                try
                                {
                                    objArray1[0] = Serial.op_Implicit(num9);
                                    mobile1      = ((Mobile)info2.Invoke(objArray1));
                                }
                                catch
                                {
                                }
                                if (mobile1 != null)
                                {
                                    list2.Add(new MobileEntry(mobile1, num8, text2, num10, num11));
                                    World.AddMobile(mobile1);
                                }
                            }
                        }
                        goto Label_0282;
                    }
                    goto Label_0282;
                }
            }
            World.m_Mobiles = new Hashtable();
Label_0282:
            if (File.Exists(World.itemIdxPath) && File.Exists(World.itemTdbPath))
            {
                using (FileStream stream3 = new FileStream(World.itemIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader3 = new BinaryReader(stream3);
                    using (FileStream stream4 = new FileStream(World.itemTdbPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        reader4 = new BinaryReader(stream4);
                        num12   = reader4.ReadInt32();
                        list6   = new ArrayList(num12);
                        num13   = 0;
                        while ((num13 < num12))
                        {
                            text3 = reader4.ReadString();
                            type2 = ScriptCompiler.FindTypeByFullName(text3);
                            if (type2 == null)
                            {
                                Console.WriteLine("failed");
                                Console.WriteLine("Error: Type \'{0}\' was not found. Delete all of those types? (y/n)", text3);
                                if (Console.ReadLine() == "y")
                                {
                                    list6.Add(null);
                                    Console.Write("World: Loading...");
                                    goto Label_0399;
                                }
                                Console.WriteLine("Types will not be deleted. An exception will be thrown when you press return");
                                throw new Exception(string.Format("Bad type \'{0}\'", text3));
                            }
                            info3 = type2.GetConstructor(typeArray1);
                            if (info3 != null)
                            {
                                objArray4    = new object[2];
                                objArray4[0] = info3;
                                objArray4[1] = text3;
                                list6.Add(objArray4);
                            }
                            else
                            {
                                throw new Exception(string.Format("Type \'{0}\' does not have a serialization constructor", type2));
                            }
Label_0399:
                            ++num13;
                        }
                        num2          = reader3.ReadInt32();
                        World.m_Items = new Hashtable(num2);
                        for (num14 = 0; (num14 < num2); ++num14)
                        {
                            num15     = reader3.ReadInt32();
                            num16     = reader3.ReadInt32();
                            num17     = reader3.ReadInt64();
                            num18     = reader3.ReadInt32();
                            objArray3 = ((object[])list6[num15]);
                            if (objArray3 != null)
                            {
                                item1 = null;
                                info4 = ((ConstructorInfo)objArray3[0]);
                                text4 = ((string)objArray3[1]);
                                try
                                {
                                    objArray1[0] = Serial.op_Implicit(num16);
                                    item1        = ((Item)info4.Invoke(objArray1));
                                }
                                catch
                                {
                                }
                                if (item1 != null)
                                {
                                    list1.Add(new ItemEntry(item1, num15, text4, num17, num18));
                                    World.AddItem(item1);
                                }
                            }
                        }
                        goto Label_048F;
                    }
                    goto Label_048F;
                }
            }
            World.m_Items = new Hashtable();
Label_048F:
            if (File.Exists(World.guildIdxPath))
            {
                using (FileStream stream5 = new FileStream(World.guildIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader5 = new BinaryReader(stream5);
                    num3    = reader5.ReadInt32();
                    args1   = new CreateGuildEventArgs(-1);
                    for (num19 = 0; (num19 < num3); ++num19)
                    {
                        reader5.ReadInt32();
                        num20    = reader5.ReadInt32();
                        num21    = reader5.ReadInt64();
                        num22    = reader5.ReadInt32();
                        args1.Id = num20;
                        guild1   = EventSink.InvokeCreateGuild(args1);
                        if (guild1 != null)
                        {
                            list3.Add(new GuildEntry(guild1, num21, num22));
                        }
                    }
                }
            }
            if (File.Exists(World.regionIdxPath))
            {
                using (FileStream stream6 = new FileStream(World.regionIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader6 = new BinaryReader(stream6);
                    num4    = reader6.ReadInt32();
                    for (num23 = 0; (num23 < num4); ++num23)
                    {
                        reader6.ReadInt32();
                        num24   = reader6.ReadInt32();
                        num25   = reader6.ReadInt64();
                        num26   = reader6.ReadInt32();
                        region1 = Region.FindByUId(num24);
                        if (region1 != null)
                        {
                            list4.Add(new RegionEntry(region1, num25, num26));
                            Region.AddRegion(region1);
                            ++num4;
                        }
                    }
                }
            }
            bool      flag1      = false;
            bool      flag2      = false;
            bool      flag3      = false;
            bool      flag4      = false;
            Type      type3      = null;
            Serial    serial1    = Serial.Zero;
            Exception exception1 = null;
            int       num27      = 0;

            if (File.Exists(World.mobBinPath))
            {
                using (FileStream stream7 = new FileStream(World.mobBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader7 = new BinaryFileReader(new BinaryReader(stream7));
                    for (num28 = 0; (num28 < list2.Count); ++num28)
                    {
                        entry1  = ((MobileEntry)list2[num28]);
                        mobile2 = ((Mobile)entry1.Object);
                        if (mobile2 != null)
                        {
                            reader7.Seek(entry1.Position, SeekOrigin.Begin);
                            try
                            {
                                World.m_LoadingType = entry1.TypeName;
                                mobile2.Deserialize(reader7);
                                if (reader7.Position != (entry1.Position + entry1.Length))
                                {
                                    throw new Exception(string.Format("***** Bad serialize on {0} *****", mobile2.GetType()));
                                }
                            }
                            catch (Exception exception2)
                            {
                                list2.RemoveAt(num28);
                                exception1 = exception2;
                                flag1      = true;
                                type3      = mobile2.GetType();
                                num27      = entry1.TypeID;
                                serial1    = mobile2.Serial;
                                goto Label_06F2;
                            }
                        }
                    }
                }
            }
Label_06F2:
            if (!flag1 && File.Exists(World.itemBinPath))
            {
                using (FileStream stream8 = new FileStream(World.itemBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader8 = new BinaryFileReader(new BinaryReader(stream8));
                    for (num29 = 0; (num29 < list1.Count); ++num29)
                    {
                        entry2 = ((ItemEntry)list1[num29]);
                        item2  = ((Item)entry2.Object);
                        if (item2 != null)
                        {
                            reader8.Seek(entry2.Position, SeekOrigin.Begin);
                            try
                            {
                                World.m_LoadingType = entry2.TypeName;
                                item2.Deserialize(reader8);
                                if (reader8.Position != (entry2.Position + entry2.Length))
                                {
                                    throw new Exception(string.Format("***** Bad serialize on {0} *****", item2.GetType()));
                                }
                            }
                            catch (Exception exception3)
                            {
                                list1.RemoveAt(num29);
                                exception1 = exception3;
                                flag2      = true;
                                type3      = item2.GetType();
                                num27      = entry2.TypeID;
                                serial1    = item2.Serial;
                                goto Label_07FA;
                            }
                        }
                    }
                }
            }
Label_07FA:
            World.m_LoadingType = null;
            if ((!flag1 && !flag2) && File.Exists(World.guildBinPath))
            {
                using (FileStream stream9 = new FileStream(World.guildBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader9 = new BinaryFileReader(new BinaryReader(stream9));
                    for (num30 = 0; (num30 < list3.Count); ++num30)
                    {
                        entry3 = ((GuildEntry)list3[num30]);
                        guild2 = ((BaseGuild)entry3.Object);
                        if (guild2 != null)
                        {
                            reader9.Seek(entry3.Position, SeekOrigin.Begin);
                            try
                            {
                                guild2.Deserialize(reader9);
                                if (reader9.Position != (entry3.Position + entry3.Length))
                                {
                                    throw new Exception(string.Format("***** Bad serialize on Guild {0} *****", guild2.Id));
                                }
                            }
                            catch (Exception exception4)
                            {
                                list3.RemoveAt(num30);
                                exception1 = exception4;
                                flag3      = true;
                                type3      = typeof(BaseGuild);
                                num27      = guild2.Id;
                                serial1    = Serial.op_Implicit(guild2.Id);
                                goto Label_0910;
                            }
                        }
                    }
                }
            }
Label_0910:
            if ((!flag1 && !flag2) && File.Exists(World.regionBinPath))
            {
                using (FileStream stream10 = new FileStream(World.regionBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    reader10 = new BinaryFileReader(new BinaryReader(stream10));
                    for (num31 = 0; (num31 < list4.Count); ++num31)
                    {
                        entry4  = ((RegionEntry)list4[num31]);
                        region2 = ((Region)entry4.Object);
                        if (region2 != null)
                        {
                            reader10.Seek(entry4.Position, SeekOrigin.Begin);
                            try
                            {
                                region2.Deserialize(reader10);
                                if (reader10.Position != (entry4.Position + entry4.Length))
                                {
                                    throw new Exception(string.Format("***** Bad serialize on {0} *****", region2.GetType()));
                                }
                            }
                            catch (Exception exception5)
                            {
                                list4.RemoveAt(num31);
                                exception1 = exception5;
                                flag4      = true;
                                type3      = region2.GetType();
                                num27      = entry4.TypeID;
                                serial1    = Serial.op_Implicit(region2.UId);
                                goto Label_0A1B;
                            }
                        }
                    }
                }
            }
Label_0A1B:
            if ((flag2 || flag1) || (flag3 || flag4))
            {
                Console.WriteLine("An error was encountered while loading a saved object");
                Console.WriteLine(" - Type: {0}", type3);
                Console.WriteLine(" - Serial: {0}", serial1);
                Console.WriteLine("Delete the object? (y/n)");
                if (Console.ReadLine() == "y")
                {
                    if ((type3 != typeof(BaseGuild)) && !type3.IsSubclassOf(typeof(Region)))
                    {
                        Console.WriteLine("Delete all objects of that type? (y/n)");
                        if (Console.ReadLine() == "y")
                        {
                            if (flag1)
                            {
                                for (num32 = 0; (num32 < list2.Count); ++num32)
                                {
                                    if (((MobileEntry)list2[num32]).TypeID == num27)
                                    {
                                        list2.RemoveAt(num32);
                                        continue;
                                    }
                                }
                            }
                            else if (flag2)
                            {
                                for (num33 = 0; (num33 < list1.Count); ++num33)
                                {
                                    if (((ItemEntry)list1[num33]).TypeID == num27)
                                    {
                                        list1.RemoveAt(num33);
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                    World.SaveIndex(list2, World.mobIdxPath);
                    World.SaveIndex(list1, World.itemIdxPath);
                    World.SaveIndex(list3, World.guildIdxPath);
                    World.SaveIndex(list4, World.regionIdxPath);
                }
                Console.WriteLine("After pressing return an exception will be thrown and the server will terminate");
                Console.ReadLine();
                objArray4    = new object[6];
                objArray4[0] = flag2;
                objArray4[1] = flag1;
                objArray4[2] = flag3;
                objArray4[3] = flag4;
                objArray4[4] = type3;
                objArray4[5] = serial1;
                throw new Exception(string.Format("Load failed (items={0}, mobiles={1}, guilds={2}, regions={3}, type={4}, serial={5})", objArray4), exception1);
            }
            EventSink.InvokeWorldLoad();
            World.m_Loading = false;
            for (num34 = 0; (num34 < World.m_DeleteList.Count); ++num34)
            {
                obj1 = World.m_DeleteList[num34];
                if ((obj1 is Item))
                {
                    ((Item)obj1).Delete();
                }
                else if ((obj1 is Mobile))
                {
                    ((Mobile)obj1).Delete();
                }
            }
            foreach (Item item3 in World.m_Items.Values)
            {
                if (item3.Parent == null)
                {
                    item3.UpdateTotals();
                }
            }
            ArrayList list7 = new ArrayList(World.m_Mobiles.Values);

            foreach (Mobile mobile3 in list7)
            {
                mobile3.ForceRegionReEnter(true);
                mobile3.UpdateTotals();
            }
            TimeSpan span1 = ((TimeSpan)(DateTime.Now - time1));

            Console.WriteLine("done ({1} items, {2} mobiles) ({0:F1} seconds)", span1.TotalSeconds, World.m_Items.Count, World.m_Mobiles.Count);
        }
Exemplo n.º 3
0
        public static void Load()
        {
            if (m_Loaded)
            {
                return;
            }

            m_Loaded      = true;
            m_LoadingType = null;

            Console.Write("World: Loading...");

            DateTime start = DateTime.Now;

            m_Loading    = true;
            m_DeleteList = new ArrayList();

            int mobileCount = 0, itemCount = 0, guildCount = 0, regionCount = 0;

            object[] ctorArgs  = new object[1];
            Type[]   ctorTypes = new Type[1] {
                typeof(Serial)
            };

            ArrayList items   = new ArrayList();
            ArrayList mobiles = new ArrayList();
            ArrayList guilds  = new ArrayList();
            ArrayList regions = new ArrayList();

            if (File.Exists(mobIdxPath) && File.Exists(mobTdbPath))
            {
                using (FileStream idx = new FileStream(mobIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader idxReader = new BinaryReader(idx);

                    using (FileStream tdb = new FileStream(mobTdbPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        BinaryReader tdbReader = new BinaryReader(tdb);

                        int count = tdbReader.ReadInt32();

                        ArrayList types = new ArrayList(count);

                        for (int i = 0; i < count; ++i)
                        {
                            string typeName = tdbReader.ReadString();

                            Type t = ScriptCompiler.FindTypeByFullName(typeName);

                            if (t == null)
                            {
                                Console.WriteLine("failed");
                                Console.WriteLine("Error: Type '{0}' was not found. Delete all of those types? (y/n)", typeName);

                                if (Console.ReadLine() == "y")
                                {
                                    types.Add(null);
                                    Console.Write("World: Loading...");
                                    continue;
                                }

                                Console.WriteLine("Types will not be deleted. An exception will be thrown when you press return");

                                throw new Exception(String.Format("Bad type '{0}'", typeName));
                            }

                            ConstructorInfo ctor = t.GetConstructor(ctorTypes);

                            if (ctor != null)
                            {
                                types.Add(new object[] { ctor, null });
                            }
                            else
                            {
                                throw new Exception(String.Format("Type '{0}' does not have a serialization constructor", t));
                            }
                        }

                        mobileCount = idxReader.ReadInt32();

                        m_Mobiles = new Hashtable(mobileCount);

                        for (int i = 0; i < mobileCount; ++i)
                        {
                            int  typeID = idxReader.ReadInt32();
                            int  serial = idxReader.ReadInt32();
                            long pos    = idxReader.ReadInt64();
                            int  length = idxReader.ReadInt32();

                            object[] objs = (object[])types[typeID];

                            if (objs == null)
                            {
                                continue;
                            }

                            Mobile          m        = null;
                            ConstructorInfo ctor     = (ConstructorInfo)objs[0];
                            string          typeName = (string)objs[1];

                            try
                            {
                                ctorArgs[0] = (Serial)serial;
                                m           = (Mobile)(ctor.Invoke(ctorArgs));
                            }
                            catch
                            {
                            }

                            if (m != null)
                            {
                                mobiles.Add(new MobileEntry(m, typeID, typeName, pos, length));
                                AddMobile(m);
                            }
                        }

                        tdbReader.Close();
                    }

                    idxReader.Close();
                }
            }
            else
            {
                m_Mobiles = new Hashtable();
            }

            if (File.Exists(itemIdxPath) && File.Exists(itemTdbPath))
            {
                using (FileStream idx = new FileStream(itemIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader idxReader = new BinaryReader(idx);

                    using (FileStream tdb = new FileStream(itemTdbPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        BinaryReader tdbReader = new BinaryReader(tdb);

                        int count = tdbReader.ReadInt32();

                        ArrayList types = new ArrayList(count);

                        for (int i = 0; i < count; ++i)
                        {
                            string typeName = tdbReader.ReadString();

                            Type t = ScriptCompiler.FindTypeByFullName(typeName);

                            if (t == null)
                            {
                                Console.WriteLine("failed");
                                Console.WriteLine("Error: Type '{0}' was not found. Delete all of those types? (y/n)", typeName);

                                if (Console.ReadLine() == "y")
                                {
                                    types.Add(null);
                                    Console.Write("World: Loading...");
                                    continue;
                                }

                                Console.WriteLine("Types will not be deleted. An exception will be thrown when you press return");

                                throw new Exception(String.Format("Bad type '{0}'", typeName));
                            }

                            ConstructorInfo ctor = t.GetConstructor(ctorTypes);

                            if (ctor != null)
                            {
                                types.Add(new object[] { ctor, typeName });
                            }
                            else
                            {
                                throw new Exception(String.Format("Type '{0}' does not have a serialization constructor", t));
                            }
                        }

                        itemCount = idxReader.ReadInt32();

                        m_Items = new Hashtable(itemCount);

                        for (int i = 0; i < itemCount; ++i)
                        {
                            int  typeID = idxReader.ReadInt32();
                            int  serial = idxReader.ReadInt32();
                            long pos    = idxReader.ReadInt64();
                            int  length = idxReader.ReadInt32();

                            object[] objs = (object[])types[typeID];

                            if (objs == null)
                            {
                                continue;
                            }

                            Item            item     = null;
                            ConstructorInfo ctor     = (ConstructorInfo)objs[0];
                            string          typeName = (string)objs[1];

                            try
                            {
                                ctorArgs[0] = (Serial)serial;
                                item        = (Item)(ctor.Invoke(ctorArgs));
                            }
                            catch
                            {
                            }

                            if (item != null)
                            {
                                items.Add(new ItemEntry(item, typeID, typeName, pos, length));
                                AddItem(item);
                            }
                        }

                        tdbReader.Close();
                    }

                    idxReader.Close();
                }
            }
            else
            {
                m_Items = new Hashtable();
            }

            if (File.Exists(guildIdxPath))
            {
                using (FileStream idx = new FileStream(guildIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader idxReader = new BinaryReader(idx);

                    guildCount = idxReader.ReadInt32();

                    CreateGuildEventArgs createEventArgs = new CreateGuildEventArgs(-1);
                    for (int i = 0; i < guildCount; ++i)
                    {
                        idxReader.ReadInt32();                        //no typeid for guilds
                        int  id     = idxReader.ReadInt32();
                        long pos    = idxReader.ReadInt64();
                        int  length = idxReader.ReadInt32();

                        createEventArgs.Id = id;
                        BaseGuild guild = EventSink.InvokeCreateGuild(createEventArgs);                          //new Guild( id );
                        if (guild != null)
                        {
                            guilds.Add(new GuildEntry(guild, pos, length));
                        }
                    }

                    idxReader.Close();
                }
            }

            if (File.Exists(regionIdxPath))
            {
                using (FileStream idx = new FileStream(regionIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader idxReader = new BinaryReader(idx);

                    regionCount = idxReader.ReadInt32();

                    for (int i = 0; i < regionCount; ++i)
                    {
                        int  typeID = idxReader.ReadInt32();
                        int  serial = idxReader.ReadInt32();
                        long pos    = idxReader.ReadInt64();
                        int  length = idxReader.ReadInt32();

                        Region r = Region.FindByUId(serial);

                        if (r != null)
                        {
                            regions.Add(new RegionEntry(r, pos, length));
                            Region.AddRegion(r);
                            regionCount++;
                        }
                    }

                    idxReader.Close();
                }
            }

            bool      failedMobiles = false, failedItems = false, failedGuilds = false, failedRegions = false;
            Type      failedType   = null;
            Serial    failedSerial = Serial.Zero;
            Exception failed       = null;
            int       failedTypeID = 0;

            if (File.Exists(mobBinPath))
            {
                using (FileStream bin = new FileStream(mobBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < mobiles.Count; ++i)
                    {
                        MobileEntry entry = (MobileEntry)mobiles[i];
                        Mobile      m     = (Mobile)entry.Object;

                        if (m != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                m_LoadingType = entry.TypeName;
                                m.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on {0} *****", m.GetType()));
                                }
                            }
                            catch (Exception e)
                            {
                                mobiles.RemoveAt(i);

                                failed        = e;
                                failedMobiles = true;
                                failedType    = m.GetType();
                                failedTypeID  = entry.TypeID;
                                failedSerial  = m.Serial;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }
            }

            if (!failedMobiles && File.Exists(itemBinPath))
            {
                using (FileStream bin = new FileStream(itemBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < items.Count; ++i)
                    {
                        ItemEntry entry = (ItemEntry)items[i];
                        Item      item  = (Item)entry.Object;

                        if (item != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                m_LoadingType = entry.TypeName;
                                item.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on {0} *****", item.GetType()));
                                }
                            }
                            catch (Exception e)
                            {
                                items.RemoveAt(i);

                                failed       = e;
                                failedItems  = true;
                                failedType   = item.GetType();
                                failedTypeID = entry.TypeID;
                                failedSerial = item.Serial;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }
            }

            m_LoadingType = null;

            if (!failedMobiles && !failedItems && File.Exists(guildBinPath))
            {
                using (FileStream bin = new FileStream(guildBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < guilds.Count; ++i)
                    {
                        GuildEntry entry = (GuildEntry)guilds[i];
                        BaseGuild  g     = (BaseGuild)entry.Object;

                        if (g != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                g.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on Guild {0} *****", g.Id));
                                }
                            }
                            catch (Exception e)
                            {
                                guilds.RemoveAt(i);

                                failed       = e;
                                failedGuilds = true;
                                failedType   = typeof(BaseGuild);
                                failedTypeID = g.Id;
                                failedSerial = g.Id;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }
            }

            if (!failedMobiles && !failedItems && File.Exists(regionBinPath))
            {
                using (FileStream bin = new FileStream(regionBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < regions.Count; ++i)
                    {
                        RegionEntry entry = (RegionEntry)regions[i];
                        Region      r     = (Region)entry.Object;

                        if (r != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                r.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on {0} *****", r.GetType()));
                                }
                            }
                            catch (Exception e)
                            {
                                regions.RemoveAt(i);

                                failed        = e;
                                failedRegions = true;
                                failedType    = r.GetType();
                                failedTypeID  = entry.TypeID;
                                failedSerial  = r.UId;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }
            }

            if (failedItems || failedMobiles || failedGuilds || failedRegions)
            {
                Console.WriteLine("An error was encountered while loading a saved object");

                Console.WriteLine(" - Type: {0}", failedType);
                Console.WriteLine(" - Serial: {0}", failedSerial);

                Console.WriteLine("Delete the object? (y/n)");

                if (Console.ReadLine() == "y")
                {
                    if (failedType != typeof(BaseGuild) && !failedType.IsSubclassOf(typeof(Region)))
                    {
                        Console.WriteLine("Delete all objects of that type? (y/n)");

                        if (Console.ReadLine() == "y")
                        {
                            if (failedMobiles)
                            {
                                for (int i = 0; i < mobiles.Count;)
                                {
                                    if (((MobileEntry)mobiles[i]).TypeID == failedTypeID)
                                    {
                                        mobiles.RemoveAt(i);
                                    }
                                    else
                                    {
                                        ++i;
                                    }
                                }
                            }
                            else if (failedItems)
                            {
                                for (int i = 0; i < items.Count;)
                                {
                                    if (((ItemEntry)items[i]).TypeID == failedTypeID)
                                    {
                                        items.RemoveAt(i);
                                    }
                                    else
                                    {
                                        ++i;
                                    }
                                }
                            }
                        }
                    }

                    SaveIndex(mobiles, mobIdxPath);
                    SaveIndex(items, itemIdxPath);
                    SaveIndex(guilds, guildIdxPath);
                    SaveIndex(regions, regionIdxPath);
                }

                Console.WriteLine("After pressing return an exception will be thrown and the server will terminate");
                Console.ReadLine();

                throw new Exception(String.Format("Load failed (items={0}, mobiles={1}, guilds={2}, regions={3}, type={4}, serial={5})", failedItems, failedMobiles, failedGuilds, failedRegions, failedType, failedSerial), failed);
            }

            EventSink.InvokeWorldLoad();

            m_Loading = false;

            for (int i = 0; i < m_DeleteList.Count; ++i)
            {
                object o = m_DeleteList[i];

                if (o is Item)
                {
                    ((Item)o).Delete();
                }
                else if (o is Mobile)
                {
                    ((Mobile)o).Delete();
                }
            }

            m_DeleteList.Clear();

            foreach (Item item in m_Items.Values)
            {
                if (item.Parent == null)
                {
                    item.UpdateTotals();
                }

                item.ClearProperties();
            }

            ArrayList list = new ArrayList(m_Mobiles.Values);

            foreach (Mobile m in list)
            {
                m.ForceRegionReEnter(true);
                m.UpdateTotals();

                m.ClearProperties();
            }

            Console.WriteLine("done ({1} items, {2} mobiles) ({0:F1} seconds)", (DateTime.Now - start).TotalSeconds, m_Items.Count, m_Mobiles.Count);
        }
Exemplo n.º 4
0
        private static void LoadEntities()
        {
            ItemEntry[]   itemEntries   = null;
            MobileEntry[] mobileEntries = null;
            GuildEntry[]  guildEntries  = null;
            RegionEntry[] regionEntries = null;

            if (File.Exists(mobIdxPath) && File.Exists(mobTdbPath))
            {
                log.Debug("loading mobile index");
                EntityType[] types = LoadTypes(mobTdbPath);
                mobileEntries = LoadMobileIndex(mobIdxPath, types);
            }
            else
            {
                m_Mobiles = new Hashtable();
            }

            if (File.Exists(itemIdxPath) && File.Exists(itemTdbPath))
            {
                log.Debug("loading item index");
                EntityType[] types = LoadTypes(itemTdbPath);
                itemEntries = LoadItemIndex(itemIdxPath, types);
            }
            else
            {
                m_Items = new Hashtable();
            }

            if (File.Exists(guildIdxPath))
            {
                log.Debug("loading guild index");

                using (FileStream idx = new FileStream(guildIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader idxReader = new BinaryReader(idx);

                    int count = idxReader.ReadInt32();
                    guildEntries = new GuildEntry[count];

                    CreateGuildEventArgs createEventArgs = new CreateGuildEventArgs(-1);
                    for (int i = 0; i < count; ++i)
                    {
                        idxReader.ReadInt32();                        //no typeid for guilds
                        int  id     = idxReader.ReadInt32();
                        long pos    = idxReader.ReadInt64();
                        int  length = idxReader.ReadInt32();

                        createEventArgs.Id = id;
                        BaseGuild guild = EventSink.InvokeCreateGuild(createEventArgs);                          //new Guild( id );
                        if (guild != null)
                        {
                            guildEntries[i] = new GuildEntry(guild, pos, length);
                        }
                    }

                    idxReader.Close();
                }
            }

            if (File.Exists(regionIdxPath))
            {
                log.Debug("loading region index");

                using (FileStream idx = new FileStream(regionIdxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader idxReader = new BinaryReader(idx);

                    int count = idxReader.ReadInt32();
                    regionEntries = new RegionEntry[count];

                    for (int i = 0; i < count; ++i)
                    {
                        idxReader.ReadInt32();                         /* typeID */
                        int  serial = idxReader.ReadInt32();
                        long pos    = idxReader.ReadInt64();
                        int  length = idxReader.ReadInt32();

                        Region r = Region.FindByUId(serial);

                        if (r != null)
                        {
                            regionEntries[i] = new RegionEntry(r, pos, length);
                            Region.AddRegion(r);
                        }
                    }

                    idxReader.Close();
                }
            }

            bool      failedMobiles = false, failedItems = false, failedGuilds = false, failedRegions = false;
            Type      failedType   = null;
            Serial    failedSerial = Serial.Zero;
            Exception failed       = null;
            int       failedTypeID = 0;

            if (File.Exists(mobBinPath))
            {
                log.Debug("loading mobiles");

                using (FileStream bin = new FileStream(mobBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < mobileEntries.Length; ++i)
                    {
                        MobileEntry entry = mobileEntries[i];
                        Mobile      m     = (Mobile)entry.Object;

                        if (m != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                m_LoadingType = entry.TypeName;
                                m.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on {0} *****", m.GetType()));
                                }
                            }
                            catch (Exception e)
                            {
                                log.Error("failed to load mobile", e);
                                mobileEntries[i].Clear();

                                failed        = e;
                                failedMobiles = true;
                                failedType    = m.GetType();
                                failedTypeID  = entry.TypeID;
                                failedSerial  = m.Serial;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }

                if (!failedMobiles)
                {
                    mobileEntries = null;
                }
            }

            if (!failedMobiles && File.Exists(itemBinPath))
            {
                log.Debug("loading items");

                using (FileStream bin = new FileStream(itemBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < itemEntries.Length; ++i)
                    {
                        ItemEntry entry = itemEntries[i];
                        Item      item  = (Item)entry.Object;

                        if (item != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                m_LoadingType = entry.TypeName;
                                item.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on {0} *****", item.GetType()));
                                }
                            }
                            catch (Exception e)
                            {
                                log.Fatal("failed to load item", e);
                                itemEntries[i].Clear();

                                failed       = e;
                                failedItems  = true;
                                failedType   = item.GetType();
                                failedTypeID = entry.TypeID;
                                failedSerial = item.Serial;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }

                if (!failedItems)
                {
                    itemEntries = null;
                }
            }

            if (!failedMobiles && !failedItems && File.Exists(guildBinPath))
            {
                log.Debug("loading guilds");

                using (FileStream bin = new FileStream(guildBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < guildEntries.Length; ++i)
                    {
                        GuildEntry entry = guildEntries[i];
                        BaseGuild  g     = (BaseGuild)entry.Object;

                        if (g != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                g.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on Guild {0} *****", g.Id));
                                }
                            }
                            catch (Exception e)
                            {
                                log.Fatal("failed to load guild", e);
                                guildEntries[i].Clear();

                                failed       = e;
                                failedGuilds = true;
                                failedType   = typeof(BaseGuild);
                                failedTypeID = g.Id;
                                failedSerial = g.Id;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }

                if (!failedGuilds)
                {
                    guildEntries = null;
                }
            }

            if (!failedMobiles && !failedItems && File.Exists(regionBinPath))
            {
                log.Debug("loading regions");

                using (FileStream bin = new FileStream(regionBinPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFileReader reader = new BinaryFileReader(new BinaryReader(bin));

                    for (int i = 0; i < regionEntries.Length; ++i)
                    {
                        RegionEntry entry = regionEntries[i];
                        Region      r     = (Region)entry.Object;

                        if (r != null)
                        {
                            reader.Seek(entry.Position, SeekOrigin.Begin);

                            try
                            {
                                r.Deserialize(reader);

                                if (reader.Position != (entry.Position + entry.Length))
                                {
                                    throw new Exception(String.Format("***** Bad serialize on {0} *****", r.GetType()));
                                }
                            }
                            catch (Exception e)
                            {
                                log.Fatal("failed to load region", e);
                                regionEntries[i].Clear();

                                failed        = e;
                                failedRegions = true;
                                failedType    = r.GetType();
                                failedTypeID  = entry.TypeID;
                                failedSerial  = r.UId;

                                break;
                            }
                        }
                    }

                    reader.Close();
                }

                if (!failedRegions)
                {
                    regionEntries = null;
                }
            }

            if (failedItems || failedMobiles || failedGuilds || failedRegions)
            {
                Console.WriteLine("An error was encountered while loading a saved object");

                Console.WriteLine(" - Type: {0}", failedType);
                Console.WriteLine(" - Serial: {0}", failedSerial);

                Console.WriteLine("Delete the object? (y/n)");

                if (Console.ReadLine() == "y")
                {
                    if (failedType != typeof(BaseGuild) && !failedType.IsSubclassOf(typeof(Region)))
                    {
                        Console.WriteLine("Delete all objects of that type? (y/n)");

                        if (Console.ReadLine() == "y")
                        {
                            if (failedMobiles)
                            {
                                for (int i = 0; i < mobileEntries.Length; ++i)
                                {
                                    if (mobileEntries[i].TypeID == failedTypeID)
                                    {
                                        mobileEntries[i].Clear();
                                    }
                                }
                            }
                            else if (failedItems)
                            {
                                for (int i = 0; i < itemEntries.Length; ++i)
                                {
                                    if (itemEntries[i].TypeID == failedTypeID)
                                    {
                                        itemEntries[i].Clear();
                                    }
                                }
                            }
                        }
                    }

                    if (mobileEntries != null)
                    {
                        SaveIndex(mobileEntries, mobIdxPath);
                    }
                    if (itemEntries != null)
                    {
                        SaveIndex(itemEntries, itemIdxPath);
                    }
                    if (guildEntries != null)
                    {
                        SaveIndex(guildEntries, guildIdxPath);
                    }
                    if (regionEntries != null)
                    {
                        SaveIndex(regionEntries, regionIdxPath);
                    }
                }

                Console.WriteLine("After pressing return an exception will be thrown and the server will terminate");
                Console.ReadLine();

                throw new Exception(String.Format("Load failed (items={0}, mobiles={1}, guilds={2}, regions={3}, type={4}, serial={5})", failedItems, failedMobiles, failedGuilds, failedRegions, failedType, failedSerial), failed);
            }
        }