Ejemplo n.º 1
0
        public TextureAsset Load(string name, AssetLoadContext context)
        {
            // Find it in the file system.
            FileSystemEntry entry = null;

            foreach (var path in _pathResolver.GetPaths(name, context.Language))
            {
                foreach (var possibleFileExtension in PossibleFileExtensions)
                {
                    var possibleFilePath = Path.ChangeExtension(path, possibleFileExtension);
                    entry = context.FileSystem.GetFile(possibleFilePath);
                    if (entry != null)
                    {
                        break;
                    }
                }

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

            if (entry == null)
            {
                return(null);
            }

            var texture = LoadImpl(entry, context.GraphicsDevice);

            texture.Name = entry.FilePath;
            return(new TextureAsset(texture, name));
        }
Ejemplo n.º 2
0
        public ModelBoneHierarchy Load(string name, AssetLoadContext context)
        {
            // Find it in the file system.
            FileSystemEntry entry = null;

            foreach (var path in _pathResolver.GetPaths(name, context.Language))
            {
                entry = context.FileSystem.GetFile(path);
                if (entry != null)
                {
                    break;
                }
            }

            // Load hierarchy.
            W3dFile hierarchyFile;

            using (var entryStream = entry.Open())
            {
                hierarchyFile = W3dFile.FromStream(entryStream, entry.FilePath);
            }
            var w3dHierarchy = hierarchyFile.GetHierarchy();

            return(w3dHierarchy != null
                ? new ModelBoneHierarchy(w3dHierarchy)
                : ModelBoneHierarchy.CreateDefault());
        }
Ejemplo n.º 3
0
        public AudioFile Load(string key, AssetLoadContext context)
        {
            var audioSettings = context.AssetStore.AudioSettings.Current;

            var soundFileName = $"{key}.{audioSettings.SoundsExtension}";

            var localisedAudioRoot = Path.Combine(audioSettings.AudioRoot, audioSettings.SoundsFolder, context.Language);
            var audioRoot          = Path.Combine(audioSettings.AudioRoot, audioSettings.SoundsFolder);

            FileSystemEntry entry = null;

            foreach (var rootPath in new[] { localisedAudioRoot, audioRoot })
            {
                var fullPath = Path.Combine(rootPath, soundFileName);
                entry = context.FileSystem.GetFile(fullPath);
                if (entry != null)
                {
                    break;
                }
            }

            if (entry == null)
            {
                return(null);
            }

            return(AudioFile.FromFileSystemEntry(entry, key));
        }
Ejemplo n.º 4
0
        public Model Load(string name, AssetLoadContext context)
        {
            // Find it in the file system.
            FileSystemEntry entry = null;

            foreach (var path in _pathResolver.GetPaths(name, context.Language))
            {
                entry = context.FileSystem.GetFile(path);
                if (entry != null)
                {
                    break;
                }
            }

            if (entry == null)
            {
                return(null);
            }

            // Load model.
            W3dFile w3dFile;

            using (var entryStream = entry.Open())
            {
                w3dFile = W3dFile.FromStream(entryStream, entry.FilePath);
            }

            var w3dHLod      = w3dFile.HLod;
            var w3dHierarchy = w3dFile.Hierarchy;
            ModelBoneHierarchy boneHierarchy;

            if (w3dHierarchy != null)
            {
                boneHierarchy = new ModelBoneHierarchy(w3dHierarchy);
            }
            else if (w3dHLod != null && w3dHierarchy == null)
            {
                // Load referenced hierarchy.
                boneHierarchy = context.AssetStore.ModelBoneHierarchies.GetByName(w3dHLod.Header.HierarchyName);
            }
            else
            {
                boneHierarchy = ModelBoneHierarchy.CreateDefault();
            }

            return(CreateModel(
                       context,
                       w3dFile,
                       boneHierarchy));
        }
Ejemplo n.º 5
0
        private static ModelSubObject CreateSubObject(
            string fullName,
            W3dChunk w3dRenderableObject,
            ModelBone bone,
            AssetLoadContext context)
        {
            return(w3dRenderableObject switch
            {
                W3dMesh w3dMesh => new ModelSubObject(fullName, w3dMesh.Header.MeshName, bone, new ModelMesh(w3dMesh, context)),

                W3dBox w3dBox => new ModelSubObject(fullName, bone, new ModelBox(w3dBox, context)),

                _ => throw new ArgumentOutOfRangeException(nameof(w3dRenderableObject)),
            });
Ejemplo n.º 6
0
        public W3DAnimation Load(string key, AssetLoadContext context)
        {
            var splitName = key.Split('.');

            if (splitName.Length <= 1)
            {
                return(null);
            }

            // Find it in the file system.
            FileSystemEntry entry = null;

            foreach (var path in _pathResolver.GetPaths(splitName[1], context.Language))
            {
                entry = context.FileSystem.GetFile(path);
                if (entry != null)
                {
                    break;
                }
            }

            if (entry == null)
            {
                logger.Warn("Failed to load animation: " + key);
                return(null);
            }

            // Load animation.
            W3dFile w3dFile;

            using (var entryStream = entry.Open())
            {
                w3dFile = W3dFile.FromStream(entryStream, entry.FilePath);
            }
            var animation = W3DAnimation.FromW3dFile(w3dFile);

            if (animation == null)
            {
                logger.Warn("Failed to load animation (was null): " + key);
                return(null);
            }

            if (!string.Equals(animation.Name, key, StringComparison.OrdinalIgnoreCase))
            {
                logger.Warn("animation name '" + animation.Name + "' does not match '" + key + "'");
            }

            return(animation);
        }
Ejemplo n.º 7
0
        public GuiTextureAsset Load(string name, AssetLoadContext context)
        {
            // Find it in the file system.
            FileSystemEntry entry = null;

            foreach (var path in _pathResolver.GetPaths(name, context.Language))
            {
                foreach (var possibleFileExtension in PossibleFileExtensions)
                {
                    var possibleFilePath = Path.ChangeExtension(path, possibleFileExtension);
                    entry = context.FileSystem.GetFile(possibleFilePath);
                    if (entry != null)
                    {
                        break;
                    }

                    // Temporary workaround for user maps
                    if (Path.IsPathRooted(possibleFilePath))
                    {
                        var info = new FileInfo(possibleFilePath);
                        if (info.Exists)
                        {
                            entry = new FileSystemEntry(context.FileSystem, possibleFilePath, (uint)info.Length, info.OpenRead);
                            break;
                        }
                    }
                }

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

            if (entry == null)
            {
                return(null);
            }

            var texture = LoadImpl(entry, context.GraphicsDevice);

            texture.Name = entry.FilePath;
            return(new GuiTextureAsset(texture, name));
        }
Ejemplo n.º 8
0
        public AudioFile Load(string key, AssetLoadContext context)
        {
            FileSystemEntry entry = null;

            // audio events
            if (string.IsNullOrEmpty(Path.GetExtension(key)))
            {
                var audioSettings = context.AssetStore.AudioSettings.Current;

                var soundFileName = $"{key}.{audioSettings.SoundsExtension}";

                var localisedAudioRoot = Path.Combine(audioSettings.AudioRoot, audioSettings.SoundsFolder, context.Language);
                var audioRoot          = Path.Combine(audioSettings.AudioRoot, audioSettings.SoundsFolder);

                foreach (var rootPath in new[] { localisedAudioRoot, audioRoot })
                {
                    var fullPath = Path.Combine(rootPath, soundFileName);
                    entry = context.FileSystem.GetFile(fullPath);
                    if (entry != null)
                    {
                        break;
                    }
                }
            }
            else // music tracks
            {
                entry = context.FileSystem.GetFile(Path.Combine(@"Data\Audio\Tracks", key));
            }

            if (entry == null)
            {
                return(null);
            }

            return(AudioFile.FromFileSystemEntry(entry, key));
        }
Ejemplo n.º 9
0
        private static Model CreateModel(
            AssetLoadContext context,
            W3dFile w3dFile,
            ModelBoneHierarchy boneHierarchy)
        {
            //BoundingSphere boundingSphere = default(BoundingSphere);

            var w3dHLod = w3dFile.HLod;

            var subObjects = new List <ModelSubObject>();

            if (w3dHLod != null)
            {
                foreach (var w3dSubObject in w3dHLod.Lods[0].SubObjects)
                {
                    if (!w3dFile.RenderableObjectsByName.TryGetValue(w3dSubObject.Name, out var w3dRenderableObject))
                    {
                        continue;
                    }

                    var bone = boneHierarchy.Bones[(int)w3dSubObject.BoneIndex];

                    //var meshBoundingSphere = mesh.BoundingSphere.Transform(bone.Transform);

                    //boundingSphere = (i == 0)
                    //    ? meshBoundingSphere
                    //    : BoundingSphere.CreateMerged(boundingSphere, meshBoundingSphere);

                    subObjects.Add(
                        CreateSubObject(
                            w3dSubObject.Name,
                            w3dRenderableObject,
                            bone,
                            context));
                }
            }
            else if (w3dFile.RenderableObjects.Count > 0)
            {
                // Simple models can have only one mesh with no HLod chunk.
                if (w3dFile.RenderableObjects.Count != 1)
                {
                    throw new InvalidOperationException();
                }

                var w3dRenderableObjectPair = w3dFile.RenderableObjectsByName.First();

                subObjects.Add(
                    CreateSubObject(
                        w3dRenderableObjectPair.Key,
                        w3dRenderableObjectPair.Value,
                        boneHierarchy.Bones[0],
                        context));
            }
            else
            {
            }

            return(new Model(
                       Path.GetFileNameWithoutExtension(w3dFile.FilePath),
                       boneHierarchy,
                       subObjects.ToArray()));
        }
Ejemplo n.º 10
0
        private static Model CreateModel(
            AssetLoadContext context,
            W3dFile w3dFile,
            ModelBoneHierarchy boneHierarchy)
        {
            //BoundingSphere boundingSphere = default(BoundingSphere);

            var w3dMeshes = w3dFile.GetMeshes();
            var w3dHLod   = w3dFile.GetHLod();

            var subObjects = new List <ModelSubObject>();

            if (w3dHLod != null)
            {
                foreach (var w3dSubObject in w3dHLod.Lods[0].SubObjects)
                {
                    // TODO: Collision boxes
                    var w3dMesh = w3dMeshes.FirstOrDefault(x => x.Header.ContainerName + "." + x.Header.MeshName == w3dSubObject.Name);
                    if (w3dMesh == null)
                    {
                        continue;
                    }

                    var bone = boneHierarchy.Bones[(int)w3dSubObject.BoneIndex];

                    var mesh = new ModelMesh(w3dMesh, context);

                    //var meshBoundingSphere = mesh.BoundingSphere.Transform(bone.Transform);

                    //boundingSphere = (i == 0)
                    //    ? meshBoundingSphere
                    //    : BoundingSphere.CreateMerged(boundingSphere, meshBoundingSphere);

                    subObjects.Add(new ModelSubObject(w3dSubObject.Name, bone, mesh));
                }
            }
            else if (w3dMeshes.Count > 0)
            {
                // Simple models can have only one mesh with no HLod chunk.
                if (w3dMeshes.Count != 1)
                {
                    throw new InvalidOperationException();
                }

                var w3dMesh = w3dMeshes[0];

                var mesh = new ModelMesh(w3dMesh, context);

                subObjects.Add(new ModelSubObject(
                                   w3dMesh.Header.MeshName,
                                   boneHierarchy.Bones[0],
                                   mesh));
            }
            else
            {
                // TODO: Some .w3d files contain a single W3D_BOX.
            }

            return(new Model(
                       Path.GetFileNameWithoutExtension(w3dFile.FilePath),
                       boneHierarchy,
                       subObjects.ToArray()));
        }