Advertisement
ivandrofly

SubtitleEdit: Loading plugins with its assembly located in custom directory

Nov 13th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. public string DoAction(Form parentForm, string srtText, double frame, string uiLineBreak, string file,
  2.         string videoFile, string rawText)
  3.     {
  4.         // HI2UC.dll
  5.         // Solution: Works if this events is regstered in SubtitleEdit.exe e.g: in method that loads the plugins
  6.         AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
  7.         {
  8.             var assemblyName = new AssemblyName(args.Name);
  9.        
  10.             Debug.WriteLine($"Loading assembly {assemblyName.Name}");
  11.             // Assembly.GetExecutingAssembly().Location
  12.             // IMPORTANT: Assembly.GetExecutingAssembly().Location == string.empty because the assembly are read as by and then loaded in SubtitleEdit:Main.cs
  13.             var assemblyFile = Path.Combine(Assembly.GetExecutingAssembly().Location,
  14.                 "libs", assemblyName.Name + ".dll");
  15.        
  16.             if (File.Exists(assemblyFile))
  17.             {
  18.                 Debug.WriteLine($"Assembly {assemblyName.Name} found at {assemblyFile}");
  19.                 return Assembly.LoadFile(assemblyFile);
  20.             }
  21.  
  22.             return null;
  23.         };
  24.  
  25. #if DEBUG
  26.         /// <summary>
  27.         /// Launch and attach a debugger to the process if it's not already attached.
  28.         /// This is helpful when a need arises to debug code in the startup sequence of an application
  29.         /// or to debug issues that occur when a debugger isn't already attached.
  30.         /// </summary>
  31.         if (!Debugger.IsAttached)
  32.         {
  33.             Debugger.Launch();
  34.         }
  35. #endif
  36.         // Make sure subtitle isn't null or empty
  37.         if (string.IsNullOrWhiteSpace(srtText))
  38.         {
  39.             MessageBox.Show("No subtitle loaded", parentForm.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  40.             return string.Empty;
  41.         }
  42.  
  43.         // Use custom separator for list view new lines
  44.         if (!string.IsNullOrEmpty(uiLineBreak))
  45.         {
  46.             Options.UILineBreak = uiLineBreak;
  47.         }
  48.  
  49.         // Get subtitle raw lines
  50.         var list = new List<string>(srtText.SplitToLines());
  51.         var srt = new SubRip();
  52.         var sub = new Subtitle(srt);
  53.  
  54.         // Load raws subtitle lines into Subtitle object
  55.         srt.LoadSubtitle(sub, list, file);
  56.  
  57.         IPlugin hi2Uc = this;
  58.         using (var form = new PluginForm(sub, hi2Uc.Name, hi2Uc.Description))
  59.         {
  60.             if (form.ShowDialog(parentForm) == DialogResult.OK)
  61.             {
  62.                 return form.Subtitle;
  63.             }
  64.         }
  65.  
  66.         return string.Empty;
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement