Example #1
0
        public static bool LoadAllAreas()
        {
            try
            {
                Assembly gasm    = Assembly.GetExecutingAssembly();
                var      DBAreas = GameServer.Database.SelectAllObjects <DBArea>();
                foreach (DBArea thisArea in DBAreas)
                {
                    AbstractArea area = (AbstractArea)gasm.CreateInstance(thisArea.ClassType, false);
                    if (area == null)
                    {
                        foreach (Assembly asm in ScriptMgr.Scripts)
                        {
                            try
                            {
                                area = (AbstractArea)asm.CreateInstance(thisArea.ClassType, false);

                                if (area != null)
                                {
                                    break;
                                }
                            }
                            catch (Exception e)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.Error("LoadAllAreas", e);
                                }
                            }
                        }

                        if (area == null)
                        {
                            log.Debug("area type " + thisArea.ClassType + " cannot be created, skipping");
                            continue;
                        }
                    }
                    area.LoadFromDatabase(thisArea);
                    area.Sound        = thisArea.Sound;
                    area.CanBroadcast = thisArea.CanBroadcast;
                    area.CheckLOS     = thisArea.CheckLOS;
                    Region region = WorldMgr.GetRegion(thisArea.Region);
                    if (region == null)
                    {
                        continue;
                    }
                    region.AddArea(area);
                    log.Info("Area added: " + thisArea.Description);
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error("Loading all areas failed", ex);
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Load from Database override to clone objects from original Region.
        /// Loads Objects, Mobs, Areas from Database using "SkinID"
        /// </summary>
        public override void LoadFromDatabase(Mob[] mobObjs, ref long mobCount, ref long merchantCount, ref long itemCount, ref long bindCount)
        {
            if (!LoadObjects)
            {
                return;
            }

            Assembly gasm       = Assembly.GetAssembly(typeof(GameServer));
            var      staticObjs = GameServer.Database.SelectObjects <WorldObject>("`Region` = @Region", new QueryParameter("@Region", Skin));
            var      areaObjs   = GameServer.Database.SelectObjects <DBArea>("`Region` = @Region", new QueryParameter("@Region", Skin));

            int count = mobObjs.Length + staticObjs.Count;

            if (count > 0)
            {
                PreAllocateRegionSpace(count + 100);
            }

            int myItemCount     = staticObjs.Count;
            int myMobCount      = 0;
            int myMerchantCount = 0;

            string allErrors = string.Empty;

            if (mobObjs.Length > 0)
            {
                foreach (Mob mob in mobObjs)
                {
                    GameNPC myMob = null;
                    string  error = string.Empty;

                    // Default Classtype
                    string classtype = ServerProperties.Properties.GAMENPC_DEFAULT_CLASSTYPE;

                    // load template if any
                    INpcTemplate template = null;
                    if (mob.NPCTemplateID != -1)
                    {
                        template = NpcTemplateMgr.GetTemplate(mob.NPCTemplateID);
                    }

                    if (mob.Guild.Length > 0 && mob.Realm >= 0 && mob.Realm <= (int)eRealm._Last)
                    {
                        Type type = ScriptMgr.FindNPCGuildScriptClass(mob.Guild, (eRealm)mob.Realm);
                        if (type != null)
                        {
                            try
                            {
                                myMob = (GameNPC)type.Assembly.CreateInstance(type.FullName);
                            }
                            catch (Exception e)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.Error("LoadFromDatabase", e);
                                }
                            }
                        }
                    }

                    if (myMob == null)
                    {
                        if (template != null && template.ClassType != null && template.ClassType.Length > 0 && template.ClassType != Mob.DEFAULT_NPC_CLASSTYPE && template.ReplaceMobValues)
                        {
                            classtype = template.ClassType;
                        }
                        else if (!string.IsNullOrWhiteSpace(mob.ClassType) && mob.ClassType != Mob.DEFAULT_NPC_CLASSTYPE)
                        {
                            classtype = mob.ClassType;
                        }

                        try
                        {
                            myMob = (GameNPC)gasm.CreateInstance(classtype, false);
                        }
                        catch
                        {
                            error = classtype;
                        }

                        if (myMob == null)
                        {
                            foreach (Assembly asm in ScriptMgr.Scripts)
                            {
                                try
                                {
                                    myMob = (GameNPC)asm.CreateInstance(classtype, false);
                                    error = string.Empty;
                                }
                                catch
                                {
                                    error = classtype;
                                }

                                if (myMob != null)
                                {
                                    break;
                                }
                            }

                            if (myMob == null)
                            {
                                myMob = new GameNPC();
                                error = classtype;
                            }
                        }
                    }

                    if (!allErrors.Contains(error))
                    {
                        allErrors += $" {error},";
                    }

                    if (myMob != null)
                    {
                        try
                        {
                            Mob clone = (Mob)mob.Clone();
                            clone.AllowAdd    = false;
                            clone.AllowDelete = false;
                            clone.Region      = ID;

                            myMob.LoadFromDatabase(clone);

                            if (myMob is GameMerchant)
                            {
                                myMerchantCount++;
                            }
                            else
                            {
                                myMobCount++;
                            }
                        }
                        catch (Exception e)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error($"Failed: {myMob.GetType().FullName}:LoadFromDatabase({mob.GetType().FullName});", e);
                            }

                            throw;
                        }

                        myMob.AddToWorld();
                    }
                }
            }

            if (staticObjs.Count > 0)
            {
                foreach (WorldObject item in staticObjs)
                {
                    WorldObject itemclone = (WorldObject)item.Clone();
                    itemclone.AllowAdd    = false;
                    itemclone.AllowDelete = false;
                    itemclone.Region      = ID;

                    GameStaticItem myItem;
                    if (!string.IsNullOrEmpty(itemclone.ClassType))
                    {
                        myItem = gasm.CreateInstance(itemclone.ClassType, false) as GameStaticItem;
                        if (myItem == null)
                        {
                            foreach (Assembly asm in ScriptMgr.Scripts)
                            {
                                try
                                {
                                    myItem = (GameStaticItem)asm.CreateInstance(itemclone.ClassType, false);
                                }
                                catch { }

                                if (myItem != null)
                                {
                                    break;
                                }
                            }

                            if (myItem == null)
                            {
                                myItem = new GameStaticItem();
                            }
                        }
                    }
                    else
                    {
                        myItem = new GameStaticItem();
                    }

                    myItem.AddToWorld();
                }
            }

            int areaCnt = 0;

            // Add missing area
            foreach (DBArea area in areaObjs)
            {
                // Don't bind in instance.
                if (area.ClassType.Equals("DOL.GS.Area+BindArea"))
                {
                    continue;
                }

                // clone DB object.
                DBArea newDBArea = (DBArea)area.Clone();
                newDBArea.AllowAdd = false;
                newDBArea.Region   = ID;

                // Instantiate Area with cloned DB object and add to region
                try
                {
                    AbstractArea newArea = (AbstractArea)gasm.CreateInstance(newDBArea.ClassType, false);
                    newArea.LoadFromDatabase(newDBArea);
                    newArea.Sound        = newDBArea.Sound;
                    newArea.CanBroadcast = newDBArea.CanBroadcast;
                    newArea.CheckLOS     = newDBArea.CheckLOS;
                    AddArea(newArea);
                    areaCnt++;
                }
                catch
                {
                    log.Warn($"area type {area.ClassType} cannot be created, skipping");
                    continue;
                }
            }

            if (myMobCount + myItemCount + myMerchantCount > 0)
            {
                if (log.IsInfoEnabled)
                {
                    log.Info($"AdventureWingInstance: {Description} ({ID}) loaded {myMobCount} mobs, {myMerchantCount} merchants, {myItemCount} items, {areaCnt}/{areaObjs.Count} areas from DB ({TimeManager.Name})");
                }

                log.Debug($"Used Memory: {GC.GetTotalMemory(false) / 1024 / 1024}MB");

                if (allErrors != string.Empty)
                {
                    log.Error($"Error loading the following NPC ClassType(s), GameNPC used instead:{allErrors.TrimEnd(',')}");
                }

                Thread.Sleep(0);  // give up remaining thread time to other resources
            }

            mobCount      += myMobCount;
            merchantCount += myMerchantCount;
            itemCount     += myItemCount;
        }