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.

109 lines
3.8 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. if (fparts.Length > 1 && fparts[1] == "virt")
  77. {
  78. var module = VirtualizedModule.Load(fparts[0]);
  79. module.Virtualize(fparts[0] = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), Path.GetFileName(fparts[0])));
  80. }
  81. var modl = ModuleDefinition.ReadModule(fparts[0]);
  82. foreach(var t in modl.Types)
  83. {
  84. foreach (var m in t.Methods)
  85. {
  86. if (m.Body != null)
  87. {
  88. m.Body.Instructions.Clear();
  89. m.Body.InitLocals = false;
  90. m.Body.Variables.Clear();
  91. }
  92. }
  93. }
  94. var outp = Path.Combine(fdir, Path.GetFileName(fparts[0]));
  95. Console.WriteLine($"Copying {fparts[0]} to {outp}");
  96. modl.Write(outp);
  97. }
  98. }
  99. }
  100. }