Esempio n. 1
0
        public static async Task <TypeDefinitions> LoadAsync(string path)
        {
            var def       = new TypeDefinitions();
            var terminals = Terminals.Load(Path.Combine(Environment.CurrentDirectory, "terminal.cache"));
            var text      = new SpaceEngineersText();
            await Task.Run(() =>
            {
                var sbcFiles = Directory.EnumerateFiles(path, "*.sbc", SearchOption.AllDirectories);
                Parallel.ForEach(sbcFiles, f => def.Search(f, terminals, text));
            });

            return(def);
        }
Esempio n. 2
0
        public void Search(string fileName, Terminals terminals, SpaceEngineersText text)
        {
            if (!File.Exists(fileName))
            {
                return;
            }
            XDocument document;

            try
            {
                document = XDocument.Load(fileName);
            }
            catch (Exception)
            {
                // Ignore
                return;
            }

            var blockDefinitions = document.XPathSelectElements(@"/Definitions/CubeBlocks/Definition");

            foreach (var blockDefinition in blockDefinitions)
            {
                var isPublic = (bool?)blockDefinition.Element("Public") ?? true;
                if (!isPublic)
                {
                    continue;
                }
                var idElement   = blockDefinition.Element("Id");
                var typeId      = (string)idElement?.Element("TypeId") ?? (string)idElement?.Attribute("Type");
                var subtypeId   = (string)idElement?.Element("SubtypeId") ?? (string)idElement?.Attribute("Typeid");
                var displayName = text.Get((string)blockDefinition.Element("DisplayName"));
                var blockInfo   = terminals.Blocks.FirstOrDefault(b => b.TypeId == typeId);
                var size        = string.Equals((string)blockDefinition.Element("CubeSize"), "Small", StringComparison.OrdinalIgnoreCase) ? "Small Grid" : "Large Grid";
                if (blockInfo == null)
                {
                    continue;
                }

                lock (_definitions)
                {
                    _definitions.Add(new Definition("Blocks", size, displayName, blockInfo.BlockInterfaceType, typeId, subtypeId));
                }
            }

            var componentDefinitions = document.XPathSelectElements(@"/Definitions/Components/Component");

            foreach (var componentDefinition in componentDefinitions)
            {
                var isPublic = (bool?)componentDefinition.Element("Public") ?? true;
                if (!isPublic)
                {
                    continue;
                }
                var canSpawnFromScreen = (bool?)componentDefinition.Element("CanSpawnFromScreen") ?? true;
                if (!canSpawnFromScreen)
                {
                    continue;
                }
                var idElement   = componentDefinition.Element("Id");
                var typeId      = (string)idElement?.Element("TypeId") ?? (string)idElement?.Attribute("Type");
                var subtypeId   = (string)idElement?.Element("SubtypeId") ?? (string)idElement?.Attribute("Typeid");
                var displayName = text.Get((string)componentDefinition.Element("DisplayName"));
                lock (_definitions)
                {
                    _definitions.Add(new Definition("Components", null, displayName, null, typeId, subtypeId));
                }
            }

            //var gasDefinitions = document.XPathSelectElements(@"/Definitions/GasProperties/Gas");
            //foreach (var gasDefinition in gasDefinitions)
            //{
            //    var isPublic = (bool?)gasDefinition.Element("Public") ?? true;
            //    if (!isPublic)
            //        continue;
            //    var idElement = gasDefinition.Element("Id");
            //    var typeId = (string)idElement?.Element("TypeId") ?? (string)idElement?.Attribute("Type");
            //    var subtypeId = (string)idElement?.Element("SubtypeId") ?? (string)idElement?.Attribute("Typeid");
            //    var displayName = (string)gasDefinition.Element("DisplayName");
            //    lock (_definitions)
            //        _definitions.Add(new Definition("Gas", displayName, null, typeId, subtypeId));
            //}
            var physicalItemDefinitions = document.XPathSelectElements(@"/Definitions/PhysicalItems/PhysicalItem");

            foreach (var physicalItemDefinition in physicalItemDefinitions)
            {
                var isPublic = (bool?)physicalItemDefinition.Element("Public") ?? true;
                if (!isPublic)
                {
                    continue;
                }
                var canSpawnFromScreen = (bool?)physicalItemDefinition.Element("CanSpawnFromScreen") ?? true;
                if (!canSpawnFromScreen)
                {
                    continue;
                }
                var idElement   = physicalItemDefinition.Element("Id");
                var typeId      = (string)idElement?.Element("TypeId") ?? (string)idElement?.Attribute("Type");
                var subtypeId   = (string)idElement?.Element("SubtypeId") ?? (string)idElement?.Attribute("Typeid");
                var displayName = text.Get((string)physicalItemDefinition.Element("DisplayName"));
                lock (_definitions)
                {
                    _definitions.Add(new Definition(GroupOf(typeId), null, displayName, null, typeId, subtypeId));
                }
            }
        }