Exemplo n.º 1
0
        private HWModel[] ImportModels()
        {
            var models = new List <HWModel>();

            this.Model = new ModelImpl();

            var visModels   = new List <VisModel>();
            var visModelMap = new Dictionary <string, VisModel>();

            var modelTags = XmlData.Elements("model");

            foreach (var modelTag in modelTags)
            {
                var modelName = modelTag.Attribute("name").Value;

                // Gets model(s).
                ISet <string> modelPaths;
                {
                    var component = modelTag.Elements("component").Single();

                    var logicAssets =
                        component.Elements("logic")
                        .Elements("logicdata")
                        .Elements("asset");
                    var directAssets = component.Elements("asset");

                    modelPaths =
                        logicAssets.Concat(directAssets)
                        .Where(assetTag =>
                               assetTag.Attribute("type").Value == "Model")
                        .Elements("file")
                        .Select(file => file.Value)
                        .ToHashSet();
                }

                ISet <string>         animationPaths;
                ISet <VisSubModelRef> subModelRefs;
                {
                    var animTags = modelTag.Elements("anim");

                    animationPaths =
                        animTags.Elements("asset")
                        .Where(assetTag =>
                               assetTag.Attribute("type").Value == "Anim")
                        .Elements("file")
                        .Select(fileTag => fileTag.Value)
                        .ToHashSet();

                    subModelRefs =
                        animTags.Elements("attach")
                        .Where(attachTag =>
                               attachTag.Attribute("type").Value ==
                               "ModelRef")
                        .Select(
                            attachTag => {
                        return(new VisSubModelRef {
                            ModelName = attachTag.Attribute("name").Value,
                            ToBone = attachTag.Attribute("tobone")?.Value,
                            FromBone =
                                attachTag.Attribute("frombone")?.Value,
                        });
                    })
                        .ToHashSet();
                }

                var visModel = new VisModel {
                    Name           = modelName,
                    ModelPaths     = modelPaths,
                    AnimationPaths = animationPaths,
                    SubModelRefs   = subModelRefs,
                };

                visModels.Add(visModel);
                visModelMap[modelName] = visModel;
            }

            var firstModel = visModels[0];

            var modelQueue = new Queue <(VisModel, VisSubModelRef?, bool)>();

            modelQueue.Enqueue((firstModel, null, true));

            var boneMap = new Dictionary <string, IBone>();

            while (modelQueue.Count > 0)
            {
                var(visModel, subModelRef, flipFaces) = modelQueue.Dequeue();

                foreach (var modelPath in visModel.ModelPaths)
                {
                    var file = Path.Combine("art", modelPath);

                    var extension = Path.GetExtension(file);
                    if (extension.Length > 0 && extension != ".ugx")
                    {
                        continue;
                    }

                    // TODO: Sometimes models are missing, why is this??
                    try {
                        var resource = HWUgxResource.FromFile(
                            Context, file, (this.Model, subModelRef, flipFaces, boneMap));
                    } catch { }

                    /*if (resource != null) {
                     * models.Add(
                     *    new HWModel(visModel., resource));
                     * }*/
                }

                foreach (var child in visModel.SubModelRefs)
                {
                    // Sometimes model references are missing--just ignore em.
                    if (visModelMap.TryGetValue(child.ModelName, out var childModel))
                    {
                        modelQueue.Enqueue((childModel, child, !flipFaces));
                    }
                }
            }

            return(models.ToArray());
        }