Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using IWshRuntimeLibrary;
  6. namespace XUnity.AutoTranslator.Setup
  7. {
  8. class Program
  9. {
  10. static void Main( string[] args )
  11. {
  12. var gamePath = Environment.CurrentDirectory;
  13. List<GameLauncher> launchers = new List<GameLauncher>();
  14. // find all .exe files
  15. var executables = Directory.GetFiles( gamePath, "*.exe", SearchOption.TopDirectoryOnly );
  16. foreach( var executable in executables )
  17. {
  18. var dataFolder = new DirectoryInfo( Path.Combine( gamePath, Path.GetFileNameWithoutExtension( executable ) + "_Data" ) );
  19. if( dataFolder.Exists )
  20. {
  21. launchers.Add( new GameLauncher( new FileInfo( executable ), dataFolder ) );
  22. }
  23. }
  24. var reiPath = Path.Combine( gamePath, "ReiPatcher" );
  25. var reiInfo = new DirectoryInfo( reiPath );
  26. if( !reiInfo.Exists )
  27. {
  28. Console.WriteLine( "ReiPatcher directory missing!" );
  29. Console.WriteLine( "Press any key to exit..." );
  30. return;
  31. }
  32. foreach( var launcher in launchers )
  33. {
  34. var setupPath = Path.Combine( gamePath, "AutoTranslatorSetupFiles" );
  35. var setupInfo = new DirectoryInfo( setupPath );
  36. if( setupInfo.Exists )
  37. {
  38. var setupFiles = setupInfo.GetFiles( "*.dll", SearchOption.TopDirectoryOnly ).Concat( setupInfo.GetFiles( "*.exe", SearchOption.TopDirectoryOnly ) );
  39. foreach( var setupFile in setupFiles )
  40. {
  41. var copyToPath = Path.Combine( gamePath, launcher.Data.Name, "Managed", setupFile.Name );
  42. var copyToFile = new FileInfo( copyToPath );
  43. setupFile.CopyTo( copyToPath, true );
  44. Console.WriteLine( "Copied " + setupFile.Name + " to " + launcher.Data.FullName );
  45. }
  46. }
  47. else
  48. {
  49. Console.WriteLine( "AutoTranslatorSetupFiles directory missing. Skipping copying files to managed directory..." );
  50. }
  51. // create an .ini file for each launcher, if it does not already exist
  52. var iniInfo = new FileInfo( Path.Combine( reiPath, Path.GetFileNameWithoutExtension( launcher.Executable.Name ) + ".ini" ) );
  53. if( !iniInfo.Exists )
  54. {
  55. using( var file = new FileStream( iniInfo.FullName, FileMode.CreateNew ) )
  56. using( var writer = new StreamWriter( file ) )
  57. {
  58. writer.WriteLine( ";" + launcher.Executable.Name + " - ReiPatcher Configuration File" );
  59. writer.WriteLine( ";" );
  60. writer.WriteLine( "[ReiPatcher]" );
  61. writer.WriteLine( ";Directory to search for Patches" );
  62. writer.WriteLine( "PatchesDir=Patches" );
  63. writer.WriteLine( ";Directory to Look for Assemblies to Patch" );
  64. writer.WriteLine( @"AssembliesDir=..\" + launcher.Data.Name + @"\Managed" );
  65. writer.WriteLine( "" );
  66. writer.WriteLine( "[Launch]" );
  67. writer.WriteLine( @"Executable=..\" + launcher.Executable.Name );
  68. writer.WriteLine( "Arguments=" );
  69. writer.WriteLine( @"Directory=..\" );
  70. }
  71. Console.WriteLine( "Created " + iniInfo.Name );
  72. }
  73. else
  74. {
  75. Console.WriteLine( iniInfo.Name + " already exists. skipping..." );
  76. }
  77. var lnkInfo = new FileInfo( Path.GetFileNameWithoutExtension( launcher.Executable.Name ) + ".lnk" );
  78. if( !lnkInfo.Exists )
  79. {
  80. // create shortcuts
  81. CreateShortcut(
  82. Path.GetFileNameWithoutExtension( launcher.Executable.Name ) + ".lnk",
  83. gamePath,
  84. Path.Combine( reiPath, "ReiPatcher.exe" ) );
  85. Console.WriteLine( "Created shortcut for " + launcher.Executable.Name );
  86. }
  87. else
  88. {
  89. Console.WriteLine( lnkInfo.Name + " already exists. skipping..." );
  90. }
  91. }
  92. Console.WriteLine( "Setup completed. Press any key to exit." );
  93. Console.ReadKey();
  94. }
  95. public static void CreateShortcut( string shortcutName, string shortcutPath, string targetFileLocation )
  96. {
  97. string shortcutLocation = Path.Combine( shortcutPath, shortcutName );
  98. WshShell shell = new WshShell();
  99. IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut( shortcutLocation );
  100. shortcut.WorkingDirectory = Path.GetDirectoryName( targetFileLocation );
  101. shortcut.TargetPath = targetFileLocation;
  102. shortcut.Arguments = "-c " + Path.GetFileNameWithoutExtension( shortcutName ) + ".ini";
  103. shortcut.Save();
  104. }
  105. }
  106. }