Beispiel #1
0
 public AssetDependencies(CompiledAssetFs compiledAssetFs, WritableSerializedAsset serializedAsset)
 {
     _compiledAssetFs = compiledAssetFs;
     _serializedAsset = serializedAsset;
 }
Beispiel #2
0
        private async Task <IAssetFsFile> EnsureCompiled(IAssetFsFile file)
        {
            if (file == null)
            {
                return(null);
            }

            if (file.Extension == "bin")
            {
                return(file);
            }

            SemaphoreSlim localCompilerLock;
            await _compilerLock.WaitAsync().ConfigureAwait(false);

            try
            {
                if (_compiledOverlay.ContainsKey(file.Name))
                {
                    return(_compiledOverlay[file.Name]);
                }

                if (_compilerLocks.ContainsKey(file.Name))
                {
                    localCompilerLock = _compilerLocks[file.Name];
                }
                else
                {
                    localCompilerLock         = new SemaphoreSlim(1);
                    _compilerLocks[file.Name] = localCompilerLock;
                }
            }
            finally
            {
                _compilerLock.Release();
            }

            await localCompilerLock.WaitAsync().ConfigureAwait(false);

            try
            {
                await _compilerLock.WaitAsync().ConfigureAwait(false);

                try
                {
                    if (_compiledOverlay.ContainsKey(file.Name))
                    {
                        return(_compiledOverlay[file.Name]);
                    }
                }
                finally
                {
                    _compilerLock.Release();
                }

                var compilers = _compilers.Where(x => x.Extensions.Contains(file.Extension)).ToArray();
                if (compilers.Length == 0)
                {
                    // No compiler available for this type.
                    OnCompilerMissing(file, _targetPlatform);
                    return(null);
                }

                OnCompileStart(file, _targetPlatform);
                var serializedAsset = new WritableSerializedAsset();
                try
                {
                    foreach (var compiler in compilers)
                    {
                        await compiler.CompileAsync(file, new AssetDependencies(this, serializedAsset), _targetPlatform, serializedAsset).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    OnCompileFailure(file, ex, _targetPlatform);
                    throw;
                }

                var memory = new MemoryStream();
                serializedAsset.WriteTo(memory);
                var compiledFsFile = new CompiledFsFile(file.Name, DateTimeOffset.UtcNow, memory);
                OnCompileFinish(file, compiledFsFile, _targetPlatform);
                _compiledOverlay[file.Name] = compiledFsFile;
                return(_compiledOverlay[file.Name]);
            }
            finally
            {
                localCompilerLock.Release();
            }
        }