예제 #1
0
        /// <summary>
        /// Check For UnloadXML Args
        /// </summary>
        /// <param name="client"></param>
        /// <param name="args"></param>
        public void OnCommand(GameClient client, string[] args)
        {
            if (args.Length < 2)
            {
                DisplaySyntax(client);
                return;
            }

            IList <Type> types = LoaderUnloaderXML.GetAllDataTableTypes();

            string argTable = args[1].ToLower();

            // Prepare Write Path
            string directory = Path.IsPathRooted(XML_UNLOAD_DB_DIRECTORY) ? XML_UNLOAD_DB_DIRECTORY : string.Format("{0}{1}scripts{1}{2}", GameServer.Instance.Configuration.RootDirectory, Path.DirectorySeparatorChar, XML_UNLOAD_DB_DIRECTORY);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }


            switch (argTable)
            {
            case "full":
                foreach (Type table in types)
                {
                    string dir         = directory;
                    Type   workingType = table;
                    System.Threading.Tasks.Task.Factory.StartNew(() => LoaderUnloaderXML.UnloadXMLTable(workingType, dir));
                }
                break;

            default:

                Type findType = types.Where(t => t.Name.ToLower().Equals(argTable) ||
                                            t.GetCustomAttributes(typeof(DataTable), false).Cast <DataTable>()
                                            .Where(dt => dt.TableName.ToLower().Equals(argTable)).Any())
                                .FirstOrDefault();
                if (findType == null)
                {
                    DisplaySyntax(client);
                    if (log.IsInfoEnabled)
                    {
                        log.InfoFormat("Could not find table to unload with search string : {0}", argTable);
                    }
                    return;
                }

                System.Threading.Tasks.Task.Factory.StartNew(() => LoaderUnloaderXML.UnloadXMLTable(findType, directory));
                break;
            }
        }
        /// <summary>
        /// Check For UnloadXML Args
        /// </summary>
        /// <param name="client"></param>
        /// <param name="args"></param>
        public void OnCommand(GameClient client, string[] args)
        {
            if (args.Length < 2)
            {
                DisplaySyntax(client);
                return;
            }

            var types = LoaderUnloaderXML.GetAllDataTableTypes();

            var argTable = args[1];

            // Prepare Write Path
            var directory = Path.IsPathRooted(XML_UNLOAD_DB_DIRECTORY) ? XML_UNLOAD_DB_DIRECTORY : string.Format("{0}{1}scripts{1}{2}", GameServer.Instance.Configuration.RootDirectory, Path.DirectorySeparatorChar, XML_UNLOAD_DB_DIRECTORY);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            switch (argTable)
            {
            case "full":
                foreach (Type table in types)
                {
                    var dir         = directory;
                    var workingType = table;
                    System.Threading.Tasks.Task.Factory.StartNew(() => LoaderUnloaderXML.UnloadXMLTable(workingType, dir));
                }

                break;

            default:
                var finddir  = directory;
                var findType = types.FirstOrDefault(t => t.Name.Equals(argTable, StringComparison.OrdinalIgnoreCase) || AttributesUtils.GetTableName(t).Equals(argTable, StringComparison.OrdinalIgnoreCase));
                if (findType == null)
                {
                    DisplaySyntax(client);
                    if (log.IsInfoEnabled)
                    {
                        log.InfoFormat("Could not find table to unload with search string : {0}", argTable);
                    }

                    return;
                }

                System.Threading.Tasks.Task.Factory.StartNew(() => LoaderUnloaderXML.UnloadXMLTable(findType, finddir));
                break;
            }
        }
예제 #3
0
        /// <summary>
        /// Check the XML Package Given for Replace or Insert Apply
        /// </summary>
        /// <param name="xml">FileInfo for XML Package</param>
        /// <param name="replace">Enforce Replace Mode</param>
        /// <returns>True if success, False if any errors</returns>
        private bool CheckXMLPackageAndApply(FileInfo xml, bool replace)
        {
            string packageName = string.Format("{0}{1}{2}", xml.Directory.Name, Path.DirectorySeparatorChar, xml.Name);

            if (log.IsInfoEnabled)
            {
                log.InfoFormat("Auto Loading XML File {0} into Database (Mode:{1})", packageName, replace ? "Replace" : "Insert");
            }

            bool result = true;

            try
            {
                //Load the XML File
                DataObject[] xmlTable = LoaderUnloaderXML.LoadXMLTableFromFile(xml);
                if (xmlTable.Length > 0)
                {
                    // Guess Object Type
                    Type xmlType = xmlTable.First().GetType();

                    // Find unique Fields
                    MemberInfo         primaryKey;
                    IList <MemberInfo> uniqueMember = LoaderUnloaderXML.GetUniqueMembers(xmlType, out primaryKey);

                    // Get all object "Method" Through Reflection
                    MethodInfo  classMethod     = GameServer.Database.GetType().GetMethod("SelectAllObjects", Type.EmptyTypes);
                    MethodInfo  genericMethod   = classMethod.MakeGenericMethod(xmlType);
                    IEnumerable existingObjects = (IEnumerable)genericMethod.Invoke(GameServer.Database, new object[] {});

                    // Check if an Object already exists
                    xmlTable.AsParallel().ForAll(obj =>
                    {
                        DataObject copy = existingObjects.Cast <DataObject>()
                                          // Check all Unique Fields !
                                          .Where(d => {
                            bool exists = false;

                            foreach (MemberInfo objProp in uniqueMember)
                            {
                                if (objProp is PropertyInfo)
                                {
                                    exists |= ((PropertyInfo)objProp).GetValue(obj, null).Equals(((PropertyInfo)objProp).GetValue(d, null));
                                }
                                else if (objProp is FieldInfo)
                                {
                                    exists |= ((FieldInfo)objProp).GetValue(obj).Equals(((FieldInfo)objProp).GetValue(d));
                                }
                            }

                            return(exists);
                        }).FirstOrDefault();

                        // Store previous Add Flag
                        bool previousAllow = obj.AllowAdd;
                        obj.AllowAdd       = true;
                        // Check if we have duplicate
                        if (copy != null)
                        {
                            if (replace)
                            {
                                // Replace
                                GameServer.Database.DeleteObject(copy);
                                GameServer.Database.AddObject(obj);
                            }

                            // Silently ignore inserts...
                        }
                        else
                        {
                            // Insert
                            GameServer.Database.AddObject(obj);
                        }
                        obj.AllowAdd = previousAllow;
                    });
                }
                else
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("XML Package {0} Found Empty, may be a parsing Error...", packageName);
                    }
                    result = false;
                }
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat("Error While Loading XML Package {0} into Database (Mode:{1}) - {2}", packageName, replace ? "Replace" : "Insert", e);
                }
                result = false;
            }

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Check the XML Package Given for Replace or Insert Apply
        /// </summary>
        /// <param name="xml">FileInfo for XML Package</param>
        /// <param name="replace">Enforce Replace Mode</param>
        /// <returns>True if success, False if any errors</returns>
        private bool CheckXMLPackageAndApply(FileInfo xml, bool replace)
        {
            var packageName = string.Format("{0}{1}{2}", xml.Directory.Name, Path.DirectorySeparatorChar, xml.Name);

            if (log.IsInfoEnabled)
            {
                log.InfoFormat("Auto Loading XML File {0} into Database (Mode:{1})", packageName, replace ? "Replace" : "Insert");
            }

            var result = true;

            try
            {
                // Load the XML File
                var xmlTable = LoaderUnloaderXML.LoadXMLTableFromFile(xml);
                if (xmlTable.Length > 0)
                {
                    // Guess Object Type
                    var xmlType      = xmlTable.First().GetType();
                    var tableHandler = new DataTableHandler(xmlType);

                    // Find unique Fields
                    var uniqueMember = DatabaseUtils.GetUniqueMembers(xmlType);

                    // Get all object "Method" Through Reflection
                    var classMethod     = GameServer.Database.GetType().GetMethod("SelectAllObjects", Type.EmptyTypes);
                    var genericMethod   = classMethod.MakeGenericMethod(xmlType);
                    var existingObjects = ((IEnumerable)genericMethod.Invoke(GameServer.Database, new object[] { })).Cast <DataObject>().ToArray();

                    // Store Object to Alter
                    var toDelete = new ConcurrentBag <DataObject>();
                    var toAdd    = new ConcurrentBag <DataObject>();

                    // Check if an Object already exists
                    xmlTable.AsParallel().ForAll(obj => {
                        // Check if Exists Compare Unique and Non-Generated Primary Keys
                        var exists = existingObjects
                                     .FirstOrDefault(entry => uniqueMember
                                                     .Any(constraint => constraint
                                                          .All(bind => bind.ValueType == typeof(string)
                                                                                  ? bind.GetValue(entry).ToString().Equals(bind.GetValue(obj).ToString(), StringComparison.OrdinalIgnoreCase)
                                                                                  : bind.GetValue(entry) == bind.GetValue(obj))));

                        if (exists != null)
                        {
                            if (replace)
                            {
                                // Delete First
                                toDelete.Add(exists);
                                toAdd.Add(obj);
                            }

                            // Silently ignore duplicate inserts only...
                        }
                        else
                        {
                            toAdd.Add(obj);
                        }
                    });

                    // Delete First
                    foreach (var obj in toDelete)
                    {
                        obj.AllowDelete = true;
                    }

                    GameServer.Database.DeleteObject(toDelete);

                    // Then Insert
                    var previousAllowAdd = toAdd.Select(obj => obj.AllowAdd).ToArray();
                    foreach (var obj in toAdd)
                    {
                        obj.AllowAdd = true;
                    }

                    GameServer.Database.AddObject(toAdd);

                    // Reset Allow Add Flag
                    var current = 0;
                    foreach (var obj in toAdd)
                    {
                        obj.AllowAdd = previousAllowAdd[current];
                        current++;
                    }
                }
                else
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("XML Package {0} Found Empty, may be a parsing Error...", packageName);
                    }

                    result = false;
                }
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat("Error While Loading XML Package {0} into Database (Mode:{1}) - {2}", packageName, replace ? "Replace" : "Insert", e);
                }

                result = false;
            }

            return(result);
        }