Advertisement
avail

t5m_link_helper

May 6th, 2019
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.12 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using IWshRuntimeLibrary;
  6. using File = System.IO.File;
  7.  
  8. namespace t5m_link_helper
  9. {
  10.     class Program
  11.     {
  12.         static void ExitPrint(string message)
  13.         {
  14.             Console.WriteLine($"ERROR: {message}");
  15.             Console.WriteLine("Press any key to exit...");
  16.             Console.ReadKey();
  17.             Environment.Exit(0x1);
  18.         }
  19.  
  20.         static void ExitError(Exception e)
  21.         {
  22.             File.WriteAllText("error.log", e.Message);
  23.             ExitPrint("An exception occurred. error.log has been created, please send it to the developers. (on Discord or forums)");
  24.         }
  25.  
  26.         static void InfoPrint(string message)
  27.         {
  28.             Console.WriteLine($"INFO: {message}");
  29.         }
  30.  
  31.         static bool CheckT5MDir()
  32.         {
  33.             string[] files = Directory.GetFiles(Directory.GetCurrentDirectory());
  34.  
  35.             if (files.Any(file => file.ToLowerInvariant().Contains("t5m.exe")))
  36.             {
  37.                 return true;
  38.             }
  39.  
  40.             return false;
  41.         }
  42.  
  43.         static string GetGamePath()
  44.         {
  45.             string iniPath = Path.Combine(Directory.GetCurrentDirectory(), "t5m.ini");
  46.  
  47.             if (File.Exists(iniPath))
  48.             {
  49.                 string[] lines = File.ReadAllLines(iniPath);
  50.  
  51.                 if (lines.Length > 0)
  52.                 {
  53.                     string gamePath = lines[1].Replace("GamePath=", "");
  54.  
  55.                     if (Directory.Exists(gamePath) && Directory.GetFiles(gamePath).Any(file => file.ToLowerInvariant().Contains("localization.txt")))
  56.                     {
  57.                         return gamePath;
  58.                     }
  59.                     else
  60.                     {
  61.                         ExitPrint("The specified game path in t5m.ini does not contain a black ops 1 installation.");
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     ExitPrint("t5m.ini is empty, please delete it and run t5m.exe again.");
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 ExitPrint("t5m.ini not found, please run t5m.exe at least once before running this tool.");
  72.             }
  73.  
  74.             return null;
  75.         }
  76.  
  77.         static void CheckAndMakeLink(string gamePath, string targetPath, string linkName)
  78.         {
  79.             string source = Path.Combine(gamePath, linkName);
  80.             string dest = Path.Combine(targetPath, linkName);
  81.  
  82.             bool sourceExists = Directory.Exists(source);
  83.             bool destExists = Directory.Exists(dest);
  84.  
  85.             void MakeLink()
  86.             {
  87.                 InfoPrint($"Creating symbolic link for {linkName}...");
  88.  
  89.                 try
  90.                 {
  91.                     CreateSymbolicLink(Path.Combine(targetPath, linkName), Path.Combine(gamePath, linkName), SymbolicLink.Directory);
  92.                     InfoPrint($"Link for {linkName} created!");
  93.                 }
  94.                 catch (Exception e)
  95.                 {
  96.                     ExitError(e);
  97.                 }
  98.             }
  99.  
  100.             if (destExists)
  101.             {
  102.                 InfoPrint($"Moving old {linkName} folder to {linkName}.old");
  103.                 Directory.Move(dest, $"{dest}.old");
  104.             }
  105.  
  106.             if (sourceExists)
  107.             {
  108.                 MakeLink();                
  109.             }
  110.             else
  111.             {
  112.                 InfoPrint($"{linkName} folder not found, creating empty one.");
  113.                 Directory.CreateDirectory(source);
  114.  
  115.                 MakeLink();
  116.             }
  117.         }
  118.  
  119.         static void WriteResourceToFile(string resourceName, string fileName)
  120.         {
  121.             if (File.Exists(fileName))
  122.             {
  123.                 File.Delete(fileName);
  124.             }
  125.  
  126.             using (var resource = Properties.Resources.shortcut_icon)
  127.             {
  128.                 using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  129.                 {
  130.                     resource.Save(file);
  131.                 }
  132.             }
  133.         }
  134.  
  135.         static void CreateShortcut(string modPath)
  136.         {
  137.             string shortcutName = "T5M LinkerMod";
  138.             string shortcutLocation = Path.Combine(modPath, shortcutName + ".lnk");
  139.             string iconPath = Path.Combine(modPath, "data", "linker_mod.ico");
  140.  
  141.             WriteResourceToFile("shortcut_icon", iconPath);
  142.  
  143.             WshShell shell = new WshShell();
  144.             IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
  145.  
  146.             shortcut.Description = "T5M with Linker Mod";
  147.             shortcut.IconLocation = iconPath;
  148.             shortcut.TargetPath = Path.Combine(modPath, "t5m.exe");
  149.             shortcut.Arguments = "-linker_mod";
  150.             shortcut.WorkingDirectory = modPath;
  151.             shortcut.Save();
  152.         }
  153.  
  154.         static void Main(string[] args)
  155.         {
  156.             if (CheckT5MDir())
  157.             {
  158.                 string gamePath = GetGamePath();
  159.                 string cwd = Directory.GetCurrentDirectory();
  160.  
  161.                 CheckAndMakeLink(gamePath, cwd, "mods");
  162.                 CheckAndMakeLink(gamePath, cwd, "players");
  163.  
  164.                 InfoPrint("Creating shortcut...");
  165.                 try
  166.                 {
  167.                     CreateShortcut(cwd);
  168.                     InfoPrint("Successfully created shortcut \"T5M LinkerMod\"");
  169.                 }
  170.                 catch (Exception e)
  171.                 {
  172.                     ExitError(e);
  173.                 }
  174.  
  175.  
  176.                 Console.WriteLine("All done! Press any key to exit.");
  177.                 Console.ReadKey();
  178.             }
  179.             else
  180.             {
  181.                 ExitPrint("Please put me in the directory where T5M.exe is, and then run again.");
  182.             }
  183.         }
  184.  
  185.         enum SymbolicLink
  186.         {
  187.             File = 0,
  188.             Directory = 1
  189.         }
  190.  
  191.         [DllImport("kernel32.dll")]
  192.         static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement