Esempio n. 1
0
        /// <summary>
        /// Loads the specified package into the engine.
        /// </summary>
        /// <param name="package">The package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(RantPackage package, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }
            if (_loadedPackages.Contains(RantPackageDependency.Create(package)))
            {
                return;
            }

            var patterns = package.GetPatterns();
            var tables   = package.GetTables();

            if (patterns.Any())
            {
                foreach (var pattern in patterns)
                {
                    _patternCache[pattern.Name] = pattern;
                    if (pattern.Module != null)
                    {
                        PackageModules[pattern.Name] = pattern.Module;
                    }
                }
            }

            if (tables.Any())
            {
                if (_dictionary == null)
                {
                    _dictionary = new RantDictionary(tables, mergeBehavior);
                }
                else
                {
                    foreach (var table in tables)
                    {
                        _dictionary.AddTable(table, mergeBehavior);
                    }
                }
            }

            _loadedPackages.Add(RantPackageDependency.Create(package));

            foreach (var dependency in package.GetDependencies())
            {
                RantPackage pkg;
                if (!_resolver.TryResolvePackage(dependency, out pkg))
                {
                    throw new FileNotFoundException($"Package '{package}' was unable to resolve dependency '{dependency}'");
                }
                LoadPackage(pkg, mergeBehavior);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Adds a new RantDictionaryTable object to the collection.
 /// </summary>
 /// <param name="table"></param>
 /// <param name="mergeBehavior">The merging strategy to employ.</param>
 public void AddTable(RantDictionaryTable table, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
 {
     RantDictionaryTable oldTable;
     if (_tables.TryGetValue(table.Name, out oldTable))
     {
         oldTable.Merge(table, mergeBehavior);
     }
     else
     {
         _tables[table.Name] = table;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Adds a new RantDictionaryTable object to the collection.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="mergeBehavior">The merging strategy to employ.</param>
        public void AddTable(RantDictionaryTable table, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            RantDictionaryTable oldTable;

            if (_tables.TryGetValue(table.Name, out oldTable))
            {
                oldTable.Merge(table, mergeBehavior);
            }
            else
            {
                _tables[table.Name] = table;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Loads the package at the specified file path into the engine.
        /// </summary>
        /// <param name="path">The path to the package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(string path, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (Util.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException("Path cannot be null nor empty.");
            }

            if (Util.IsNullOrWhiteSpace(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            LoadPackage(RantPackage.Load(path), mergeBehavior);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a new RantDictionary object from the specified dictionary collection.
        /// </summary>
        /// <param name="tables">The tables to store in the dictionary.</param>
        /// <param name="mergeBehavior">The merging strategy to employ.</param>
        public RantDictionary(IEnumerable<RantDictionaryTable> tables, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            _tables = new Dictionary<string, RantDictionaryTable>();

            if (tables == null) return;

            RantDictionaryTable table;
            foreach (var list in tables)
            {
                if (_tables.TryGetValue(list.Name, out table))
                {
                    table.Merge(list, mergeBehavior);
                }
                else
                {
                    _tables[list.Name] = list;
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Adds another table's entries to the current table, given that they share the same name and subtypes.
        /// </summary>
        /// <param name="other">The table whose entries will be added to the current instance.</param>
        /// <param name="mergeBehavior">The merging strategy to employ.</param>
        /// <returns>True if merge succeeded; otherwise, False.</returns>
        public bool Merge(RantDictionaryTable other, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (other._name != _name || other == this)
            {
                return(false);
            }
            if (!other._subtypes.SequenceEqual(_subtypes))
            {
                return(false);
            }
            int oldLength = _entries.Length;

            switch (mergeBehavior)
            {
            case TableMergeBehavior.Naive:
                Array.Resize(ref _entries, _entries.Length + other._entries.Length);
                Array.Copy(other._entries, 0, _entries, oldLength, other._entries.Length);
                break;

            case TableMergeBehavior.RemoveEntryDuplicates:                     // TODO: Make this NOT O(n^2*subtypes) -- speed up with HashSet?
            {
                var otherEntries =
                    other._entries.Where(e => !_entries.Any(ee => ee.Terms.SequenceEqual(e.Terms))).ToArray();
                Array.Resize(ref _entries, _entries.Length + otherEntries.Length);
                Array.Copy(otherEntries, 0, _entries, oldLength, otherEntries.Length);
                break;
            }

            case TableMergeBehavior.RemoveFirstTermDuplicates:                     // TODO: Make this NOT O(n^2)
            {
                var otherEntries =
                    other._entries.Where(e => _entries.All(ee => ee.Terms[0] != e.Terms[0])).ToArray();
                Array.Resize(ref _entries, _entries.Length + otherEntries.Length);
                Array.Copy(otherEntries, 0, _entries, oldLength, otherEntries.Length);
                break;
            }
            }

            return(true);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a new RantDictionary object from the specified dictionary collection.
        /// </summary>
        /// <param name="tables">The tables to store in the dictionary.</param>
        /// <param name="mergeBehavior">The merging strategy to employ.</param>
        public RantDictionary(IEnumerable <RantDictionaryTable> tables, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            _tables = new Dictionary <string, RantDictionaryTable>();

            if (tables == null)
            {
                return;
            }

            RantDictionaryTable table;

            foreach (var list in tables)
            {
                if (_tables.TryGetValue(list.Name, out table))
                {
                    table.Merge(list, mergeBehavior);
                }
                else
                {
                    _tables[list.Name] = list;
                }
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Loads all dictionary (.dic) files from the specified directories and returns a RantDictionary object that contains the loaded data.
 /// </summary>
 /// <param name="directories">The directories from which to load dictionaries.</param>
 /// <param name="mergeBehavior">The merging strategy to employ.</param>
 /// <returns></returns>
 public static RantDictionary FromMultiDirectory(string[] directories, TableMergeBehavior mergeBehavior)
 {
     return(new RantDictionary(directories.SelectMany(path => Directory.GetFiles(path, "*.dic", SearchOption.AllDirectories)).Select(RantDictionaryTable.FromFile), mergeBehavior));
 }
Esempio n. 9
0
 /// <summary>
 /// Loads all dictionary (.dic) files from the specified directory and returns a RantDictionary object that contains the loaded data.
 /// </summary>
 /// <param name="directory">The directory from which to load dictionaries.</param>
 /// <param name="mergeBehavior">The merging strategy to employ.</param>
 /// <returns></returns>
 public static RantDictionary FromDirectory(string directory, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
 {
     return(new RantDictionary(Directory.GetFiles(directory, "*.dic", SearchOption.AllDirectories).Select(RantDictionaryTable.FromFile).ToList(), mergeBehavior));
 }
Esempio n. 10
0
 /// <summary>
 /// Loads all dictionary (.dic) files from the specified directories and returns a RantDictionary object that contains the loaded data.
 /// </summary>
 /// <param name="directories">The directories from which to load dictionaries.</param>
 /// <param name="mergeBehavior">The merging strategy to employ.</param>
 /// <returns></returns>
 public static RantDictionary FromMultiDirectory(string[] directories, TableMergeBehavior mergeBehavior)
 {
     return new RantDictionary(directories.SelectMany(path => Directory.GetFiles(path, "*.dic", SearchOption.AllDirectories)).Select(RantDictionaryTable.FromFile), mergeBehavior);
 }
Esempio n. 11
0
 /// <summary>
 /// Loads all dictionary (.dic) files from the specified directory and returns a RantDictionary object that contains the loaded data.
 /// </summary>
 /// <param name="directory">The directory from which to load dictionaries.</param>
 /// <param name="mergeBehavior">The merging strategy to employ.</param>
 /// <returns></returns>
 public static RantDictionary FromDirectory(string directory, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
 {
     return new RantDictionary(Directory.GetFiles(directory, "*.dic", SearchOption.AllDirectories).Select(RantDictionaryTable.FromFile).ToList(), mergeBehavior);
 }
Esempio n. 12
0
        /// <summary>
        /// Loads the package at the specified file path into the engine.
        /// </summary>
        /// <param name="path">The path to the package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(string path, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (Util.IsNullOrWhiteSpace(path))
                throw new ArgumentException("Path cannot be null nor empty.");

	        if (Util.IsNullOrWhiteSpace(Path.GetExtension(path)))
		        path += ".rantpkg";
			
			LoadPackage(RantPackage.Load(path), mergeBehavior);
		}
Esempio n. 13
0
        /// <summary>
        /// Loads the specified package into the engine.
        /// </summary>
        /// <param name="package">The package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(RantPackage package, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (package == null) throw new ArgumentNullException(nameof(package));
	        if (_loadedPackages.Contains(RantPackageDependency.Create(package))) return;

            var patterns = package.GetPatterns();
            var tables = package.GetTables();

            if (patterns.Any())
            {
				foreach (var pattern in patterns)
				{
					_patternCache[pattern.Name] = pattern;
					if (pattern.Module != null)
						PackageModules[pattern.Name] = pattern.Module;
				}
            }

            if (tables.Any())
            {
                if (_dictionary == null)
                {
                    _dictionary = new RantDictionary(tables, mergeBehavior);
                }
                else
                {
                    foreach (var table in tables)
                    {
                        _dictionary.AddTable(table, mergeBehavior);
                    }
                }
            }

	        _loadedPackages.Add(RantPackageDependency.Create(package));

	        foreach (var dependency in package.GetDependencies())
	        {
		        RantPackage pkg;
				if (!_resolver.TryResolvePackage(dependency, out pkg))
					throw new FileNotFoundException($"Package '{package}' was unable to resolve dependency '{dependency}'");
                LoadPackage(pkg, mergeBehavior);
	        }
        }
Esempio n. 14
0
        /// <summary>
        /// Adds another table's entries to the current table, given that they share the same name and subtypes.
        /// </summary>
        /// <param name="other">The table whose entries will be added to the current instance.</param>
        /// <param name="mergeBehavior">The merging strategy to employ.</param>
        /// <returns>True if merge succeeded; otherwise, False.</returns>
        public bool Merge(RantDictionaryTable other, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (other._name != _name || other == this) return false;
            if (!other._subtypes.SequenceEqual(_subtypes)) return false;
            int oldLength = _entries.Length;
            switch (mergeBehavior)
            {
                case TableMergeBehavior.Naive:
                    Array.Resize(ref _entries, _entries.Length + other._entries.Length);
                    Array.Copy(other._entries, 0, _entries, oldLength, other._entries.Length);
                    break;
                case TableMergeBehavior.RemoveEntryDuplicates: // TODO: Make this NOT O(n^2*subtypes) -- speed up with HashSet?
                    {
                        var otherEntries =
                            other._entries.Where(e => !_entries.Any(ee => ee.Terms.SequenceEqual(e.Terms))).ToArray();
                        Array.Resize(ref _entries, _entries.Length + otherEntries.Length);
                        Array.Copy(otherEntries, 0, _entries, oldLength, otherEntries.Length);
                        break;
                    }
                case TableMergeBehavior.RemoveFirstTermDuplicates: // TODO: Make this NOT O(n^2)
                    {
                        var otherEntries =
                            other._entries.Where(e => _entries.All(ee => ee.Terms[0] != e.Terms[0])).ToArray();
                        Array.Resize(ref _entries, _entries.Length + otherEntries.Length);
                        Array.Copy(otherEntries, 0, _entries, oldLength, otherEntries.Length);
                        break;
                    }
            }

            return true;
        }
Esempio n. 15
0
        /// <summary>
        /// Loads the package at the specified file path into the engine.
        /// </summary>
        /// <param name="path">The path to the package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(string path, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (String.IsNullOrEmpty(path))
                throw new ArgumentException("Path cannot be null nor empty.");

            if (String.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            var package = RantPackage.Load(path);

            var patterns = package.GetPatterns();
            var tables = package.GetTables();

            if (patterns.Any())
            {
                foreach (var pattern in patterns)
                    _patternCache[pattern.Name] = pattern;
            }

            if (tables.Any())
            {
                if (_dictionary == null)
                {
                    _dictionary = new RantDictionary(tables, mergeBehavior);
                }
                else
                {
                    foreach (var table in tables)
                    {
                        _dictionary.AddTable(table, mergeBehavior);
                    }
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Loads the specified package into the engine.
        /// </summary>
        /// <param name="package">The package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(RantPackage package, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (package == null) throw new ArgumentNullException(nameof(package));

            var patterns = package.GetPatterns();
            var tables = package.GetTables();

            if (patterns.Any())
            {
                foreach (var pattern in patterns)
                    _patternCache[pattern.Name] = pattern;
            }

            if (tables.Any())
            {
                if (_dictionary == null)
                {
                    _dictionary = new RantDictionary(tables, mergeBehavior);
                }
                else
                {
                    foreach (var table in tables)
                    {
                        _dictionary.AddTable(table, mergeBehavior);
                    }
                }
            }
        }