getmusicbee.com

Support => Developers' Area => MusicBee API => Topic started by: Mayibongwe on December 08, 2024, 04:46:18 PM

Title: General Sample Code Blocks
Post by: Mayibongwe on December 08, 2024, 04:46:18 PM
Might be useful to others. Will post more as time goes by.
Anybody's welcome to add to this thread.

Catering For The Portable Version Or Otherwise

Getting the MusicBee edition:

private string getMusicBeeEdition()
{
   string appData = mbApi.Setting_GetPersistentStoragePath();
   if(appData.Contains("Roaming") == false) return "Portable";
   else return "Installer or Store";
}

Getting the Internal Cache folder:

private string getCacheFolder()
{
   string mbType = getMusicBeeEdition();
   string cacheFolder = "InternalCache";

   if(mbType != "Portable") cacheFolder = @"..\..\Local\MusicBee\InternalCache";
   cacheFolder = Path.Combine(mbApi.Setting_GetPersistentStoragePath(), cacheFolder);

   return cacheFolder;
}

Skin Overrides For Your Plugin

Loading the skin file:

private string loadSkin()
{
   string dataFolder = Path.Combine(mbApi.Setting_GetPersistentStoragePath(), "mb_MyPlugin");
   string skinsFolder = Path.Combine(dataFolder, "skins");

   string skin = Path.GetFileNameWithoutExtension(mbApi.Setting_GetSkin());
   string skinPath = Path.Combine(skinsFolder, skin + ".xml");
      
   if(!File.Exists(skinPath))
   {
      File.WriteAllText
      (   skinPath,
         "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
         + "<root>\n" + Environment.NewLine
         + "<element id=\"Element_1\" bg=\"r,g,b\" fg=\"r,g,b\"/>\n"
         + "<element id=\"Element_2\" bg=\"r,g,b\" fg=\"r,g,b\"/>\n"
         + Environment.NewLine + "</root>"
      );
   }

   using (StreamReader reader = File.OpenText(skinPath)) return reader.ReadToEnd();   
}

Querying the contents of the skin:

private color getSkinColour(string element, string attribute)
{         
   string search = "<element id=\"" + element + "\".*?" + attribute + "=\"(.*?)\".*?/>";
   string colour = Regex.Match(loadSkin(), search).Groups[1].Value;
      
   int r = Int32.Parse(Regex.Replace(colour, @"(\d+),\d+,\d+", "$1"));
   int g = Int32.Parse(Regex.Replace(colour, @"\d+,(\d+),\d+", "$1"));
   int b = Int32.Parse(Regex.Replace(colour, @"\d+,\d+,(\d+)", "$1"));
      
   return Color.FromArgb(r, g, b);
}