Search code examples
c#xnagame-enginemonogame

How to scan all XNB files in Content Manager?


So I'm building a game with MonoGame in VS. I'm using the Content Pipeline from MonoGame (that generates XNB files).

The thing is that my game is expansible (it is a rhytim game that user can download songs/charts). So I had to support "plug-in" XNB files.

So I already made my Custom Importer / Reader / Writer for my custom types, but I cannot find a way to scan the content folder to find which xnb's are available. For example if the user adds a Song1.xnb at Content folder, how will I know (programatically) that the file is there?

I couldn't find any showFiles or scanContent in ContentManager to list all available files. How can I find which files are in my content folder in a Cross-Platform way?

Example of my structure:

Content
  - Songs
    - Song0
      - 0.xnb
      - 1.xnb
      - Song0.xnb
    - Song1
      - 0.xnb
      - Song0.xnb

So my song folder always have a {Folder}.xnb file that have the structure for the songs (and also a reference for X.xnb (0,1,2 ...)). But I cannot find what folders does exists on the Songs folder so I can call Load<Music>("Songs\\{SongFolder}\\{SongFolder}.xnb")

Thanks!


Solution

  • I use this function to load all textures in my folder.

    public static           List<Texture2D>     Textures(string folderPath)
    {
        if (!folderPath.StartsWith(@"\")) folderPath = @"\" + folderPath;
        List<string> paths = Directory.GetFiles(Help.RootDir(Content.RootDirectory) + folderPath).ToList();
        List<Texture2D> images = new List<Texture2D>();
        foreach (string s in paths)
        {
            if (s.EndsWith(".xnb"))
                images.Add(Texture(s.Replace(Content.RootDirectory, "").Replace(".xnb", "")));
        }
        return images;
    }
    

    The folderPath variable should be path from content's folder (without it) to te folder you want to load all textures/songs from. In your case, pass "Songs\Songs0" as a parameter, replace Texture2D to Song or SoundEffect, and it will work fine.

    Also, the Help.RootDir is a simple function I made:

    public static string RootDir(string s)
    { return s.Substring(0, s.LastIndexOf(@"\")); }
    

    And also include System.IO;