예제 #1
0
        /// <summary>
        /// Load all assets of type in the specified directory
        /// </summary>
        /// <param name="type"></param>
        /// <param name="directory"></param>
        /// <param name="searchOption"></param>
        public void LoadAll(Type type, string directory, SearchOption searchOption)
        {
            GeneralUtils.LogInfo($"Loading all assets from: {directory}");
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            int prevAssetTotal = TotalLoaded();

            if (!Directory.Exists(content.RootDirectory + "\\" + directory))
            {
                GeneralUtils.LogError("Invalid directory");
                throw new DirectoryNotFoundException("Invalid directory");
            }

            string[] files = Directory.GetFiles(content.RootDirectory + "\\" + directory, "*", searchOption);

            foreach (string file in files)
            {
                string ext  = Path.GetExtension(file);
                string path = file.Substring(file.IndexOf('\\') + 1);
                path = path.Substring(0, path.Length - ext.Length);

                Load(new AssetDefinition(path, type));
            }

            stopwatch.Stop();
            GeneralUtils.LogInfo($"=> Assets loaded: {TotalLoaded() - prevAssetTotal} (updated total: {TotalLoaded()})");
            GeneralUtils.LogInfo($" => Time elapsed: { stopwatch.Elapsed.TotalSeconds.ToString("0.0000")}s");
        }
예제 #2
0
        /// <summary>
        /// Loads an individual asset
        /// </summary>
        /// <param name="asset"></param>
        public void Load(AssetDefinition assetDef)
        {
            //Console.WriteLine($"Loading asset: {assetDef.Name} ({assetDef.Path})");
            GeneralUtils.LogInfo($"Loading asset: {assetDef.Name} ({assetDef.Path})");

            Asset <dynamic> asset = new Asset <dynamic>(assetDef);

            try {
                if (assetDef.Type == typeof(Texture2D))
                {
                    asset.Assign(content.Load <Texture2D>(assetDef.Path));
                }
                else if (assetDef.Type == typeof(SpriteFont))
                {
                    asset.Assign(content.Load <SpriteFont>(assetDef.Path));
                }
                else if (assetDef.Type == typeof(SoundEffect))
                {
                    asset.Assign(content.Load <SoundEffect>(assetDef.Path));
                }
                else if (assetDef.Type == typeof(Song))
                {
                    asset.Assign(content.Load <Song>(assetDef.Path));
                }
                else if (assetDef.Type == typeof(Model))
                {
                    asset.Assign(content.Load <Model>(assetDef.Path));
                }
                else if (assetDef.Type == typeof(Effect))
                {
                    asset.Assign(content.Load <Effect>(assetDef.Path));
                }
                else if (assetDef.Type == typeof(TextureCube))
                {
                    asset.Assign(content.Load <TextureCube>(assetDef.Path));
                }
                else
                {
                    throw new NotSupportedException();
                }

                assets.Add(assetDef.Name, asset);
            }
            catch (Exception ex) {
                if (ex is ContentLoadException)
                {
                    GeneralUtils.LogError($" => Could not find/load {assetDef.Name} ({assetDef.Path}). {ex}");
                }
                else if (ex is NotSupportedException)
                {
                    GeneralUtils.LogError($" => The type {assetDef.Type.ToString().Split('.').Last()} for {assetDef.Name} ({assetDef.Path}) isn't supported.");
                }
                else if (ex is InvalidCastException)
                {
                    GeneralUtils.LogWarning($" => Skipping {assetDef.Name} ({assetDef.Path}). {ex.Message}");
                }
                else
                {
                    GeneralUtils.LogError($" => An unknown error has occured. {ex}");
                }
            }
        }