You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.2 KiB

  1. using Mono.Cecil;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CollectDependencies
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. string depsfile = File.ReadAllText(args[0]);
  15. string fdir = Path.GetDirectoryName(args[0]);
  16. List<string> files = new List<string>();
  17. { // Create files from stuff in depsfile
  18. Stack<string> fstack = new Stack<string>();
  19. void Push(string val)
  20. {
  21. string pre = "";
  22. if (fstack.Count > 0)
  23. pre = fstack.First();
  24. fstack.Push(pre + val);
  25. }
  26. string Pop() => fstack.Pop();
  27. string Replace(string val)
  28. {
  29. var v2 = Pop();
  30. Push(val);
  31. return v2;
  32. }
  33. foreach (var line in depsfile.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
  34. {
  35. var parts = line.Split('"');
  36. var path = parts.Last();
  37. var level = parts.Length - 1;
  38. if (path.StartsWith("::"))
  39. { // pseudo-command
  40. parts = path.Split(' ');
  41. var command = parts[0].Substring(2);
  42. parts = parts.Skip(1).ToArray();
  43. var arglist = string.Join(" ", parts);
  44. if (command == "from")
  45. { // an "import" type command
  46. path = File.ReadAllText(Path.Combine(fdir, arglist));
  47. }
  48. else if (command == "prompt")
  49. {
  50. Console.Write(arglist);
  51. path = Console.ReadLine();
  52. }
  53. else
  54. {
  55. path = "";
  56. Console.Error.WriteLine($"Invalid command {command}");
  57. }
  58. }
  59. if (level > fstack.Count - 1)
  60. Push(path);
  61. else if (level == fstack.Count - 1)
  62. files.Add(Replace(path));
  63. else if (level < fstack.Count - 1)
  64. {
  65. files.Add(Pop());
  66. while (level < fstack.Count)
  67. Pop();
  68. Push(path);
  69. }
  70. }
  71. files.Add(Pop());
  72. }
  73. foreach (var file in files)
  74. {
  75. var fparts = file.Split('?');
  76. var fname = fparts[0];
  77. if (fname == "") continue;
  78. var outp = Path.Combine(fdir, Path.GetFileName(fname));
  79. Console.WriteLine($"Copying \"{fname}\" to \"{outp}\"");
  80. if (File.Exists(outp)) File.Delete(outp);
  81. if (Path.GetExtension(fname).ToLower() == ".dll")
  82. {
  83. if (fparts.Length > 1 && fparts[1] == "virt")
  84. {
  85. var module = VirtualizedModule.Load(fname);
  86. module.Virtualize(fname = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), Path.GetFileName(fname)));
  87. }
  88. var modl = ModuleDefinition.ReadModule(fparts[0]);
  89. foreach (var t in modl.Types)
  90. {
  91. foreach (var m in t.Methods)
  92. {
  93. if (m.Body != null)
  94. {
  95. m.Body.Instructions.Clear();
  96. m.Body.InitLocals = false;
  97. m.Body.Variables.Clear();
  98. }
  99. }
  100. }
  101. modl.Write(outp);
  102. }
  103. else
  104. {
  105. File.Copy(fname, outp);
  106. }
  107. }
  108. }
  109. }
  110. }