Ejemplo n.º 1
0
        public object LoadAsset(AssetInfo assetInfo, IAssetStore assetStore)
        {
            using var assetFileStream = _fileSystem.GetFile(assetInfo.AssetFilePath).OpenRead();
            var assetData         = AssetData.Load(assetFileStream);
            var soundAssetContent = assetData.ReadJsonContent <SoundAssetContent>();

            var relativeSiblingPath = soundAssetContent.SoundFilePath ??
                                      throw new InvalidOperationException(
                                                $"{nameof(SoundAssetContent)}.{nameof(SoundAssetContent.SoundFilePath)} cannot be null.");

            var soundFilePath = PathUtils.GetSiblingPath(assetInfo.AssetFilePath, relativeSiblingPath);
            var fileExtension = Path.GetExtension(soundFilePath);
            var soundFormat   = SoundFormatParser.ParseFromFileExtension(fileExtension);

            using var soundFileStream = _fileSystem.GetFile(soundFilePath).OpenRead();
            return(_audioBackend.CreateSound(soundFileStream, soundFormat));
        }
        public void SetUp()
        {
            _audioBackend = Substitute.For <IAudioBackend>();
            _fileSystem   = Substitute.For <IFileSystem>();
            _assetStore   = Substitute.For <IAssetStore>();

            const string      soundFilePath = "sound.wav";
            const SoundFormat soundFormat   = SoundFormat.Wav;

            const string assetFilePath     = "sound-asset-path";
            var          soundAssetContent = new SoundAssetContent
            {
                SoundFilePath = soundFilePath
            };

            _assetInfo = new AssetInfo(AssetId.CreateUnique(), AudioAssetTypes.Sound, assetFilePath);
            var assetData    = AssetData.CreateWithJsonContent(_assetInfo.AssetId, _assetInfo.AssetType, soundAssetContent);
            var memoryStream = new MemoryStream();

            assetData.Save(memoryStream);
            memoryStream.Position = 0;

            var assetFile = Substitute.For <IFile>();

            assetFile.OpenRead().Returns(memoryStream);
            _fileSystem.GetFile(assetFilePath).Returns(assetFile);

            var soundFile = Substitute.For <IFile>();
            var stream    = Substitute.For <Stream>();

            soundFile.OpenRead().Returns(stream);
            _fileSystem.GetFile(soundFilePath).Returns(soundFile);

            _sound = Substitute.For <ISound>();
            _audioBackend.CreateSound(stream, soundFormat).Returns(_sound);

            _soundAssetLoader = new SoundAssetLoader(_audioBackend, _fileSystem);
        }