Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.IO;
- using System.Runtime.InteropServices;
- using IWshRuntimeLibrary;
- using File = System.IO.File;
- namespace t5m_link_helper
- {
- class Program
- {
- static void ExitPrint(string message)
- {
- Console.WriteLine($"ERROR: {message}");
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- Environment.Exit(0x1);
- }
- static void ExitError(Exception e)
- {
- File.WriteAllText("error.log", e.Message);
- ExitPrint("An exception occurred. error.log has been created, please send it to the developers. (on Discord or forums)");
- }
- static void InfoPrint(string message)
- {
- Console.WriteLine($"INFO: {message}");
- }
- static bool CheckT5MDir()
- {
- string[] files = Directory.GetFiles(Directory.GetCurrentDirectory());
- if (files.Any(file => file.ToLowerInvariant().Contains("t5m.exe")))
- {
- return true;
- }
- return false;
- }
- static string GetGamePath()
- {
- string iniPath = Path.Combine(Directory.GetCurrentDirectory(), "t5m.ini");
- if (File.Exists(iniPath))
- {
- string[] lines = File.ReadAllLines(iniPath);
- if (lines.Length > 0)
- {
- string gamePath = lines[1].Replace("GamePath=", "");
- if (Directory.Exists(gamePath) && Directory.GetFiles(gamePath).Any(file => file.ToLowerInvariant().Contains("localization.txt")))
- {
- return gamePath;
- }
- else
- {
- ExitPrint("The specified game path in t5m.ini does not contain a black ops 1 installation.");
- }
- }
- else
- {
- ExitPrint("t5m.ini is empty, please delete it and run t5m.exe again.");
- }
- }
- else
- {
- ExitPrint("t5m.ini not found, please run t5m.exe at least once before running this tool.");
- }
- return null;
- }
- static void CheckAndMakeLink(string gamePath, string targetPath, string linkName)
- {
- string source = Path.Combine(gamePath, linkName);
- string dest = Path.Combine(targetPath, linkName);
- bool sourceExists = Directory.Exists(source);
- bool destExists = Directory.Exists(dest);
- void MakeLink()
- {
- InfoPrint($"Creating symbolic link for {linkName}...");
- try
- {
- CreateSymbolicLink(Path.Combine(targetPath, linkName), Path.Combine(gamePath, linkName), SymbolicLink.Directory);
- InfoPrint($"Link for {linkName} created!");
- }
- catch (Exception e)
- {
- ExitError(e);
- }
- }
- if (destExists)
- {
- InfoPrint($"Moving old {linkName} folder to {linkName}.old");
- Directory.Move(dest, $"{dest}.old");
- }
- if (sourceExists)
- {
- MakeLink();
- }
- else
- {
- InfoPrint($"{linkName} folder not found, creating empty one.");
- Directory.CreateDirectory(source);
- MakeLink();
- }
- }
- static void WriteResourceToFile(string resourceName, string fileName)
- {
- if (File.Exists(fileName))
- {
- File.Delete(fileName);
- }
- using (var resource = Properties.Resources.shortcut_icon)
- {
- using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- resource.Save(file);
- }
- }
- }
- static void CreateShortcut(string modPath)
- {
- string shortcutName = "T5M LinkerMod";
- string shortcutLocation = Path.Combine(modPath, shortcutName + ".lnk");
- string iconPath = Path.Combine(modPath, "data", "linker_mod.ico");
- WriteResourceToFile("shortcut_icon", iconPath);
- WshShell shell = new WshShell();
- IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
- shortcut.Description = "T5M with Linker Mod";
- shortcut.IconLocation = iconPath;
- shortcut.TargetPath = Path.Combine(modPath, "t5m.exe");
- shortcut.Arguments = "-linker_mod";
- shortcut.WorkingDirectory = modPath;
- shortcut.Save();
- }
- static void Main(string[] args)
- {
- if (CheckT5MDir())
- {
- string gamePath = GetGamePath();
- string cwd = Directory.GetCurrentDirectory();
- CheckAndMakeLink(gamePath, cwd, "mods");
- CheckAndMakeLink(gamePath, cwd, "players");
- InfoPrint("Creating shortcut...");
- try
- {
- CreateShortcut(cwd);
- InfoPrint("Successfully created shortcut \"T5M LinkerMod\"");
- }
- catch (Exception e)
- {
- ExitError(e);
- }
- Console.WriteLine("All done! Press any key to exit.");
- Console.ReadKey();
- }
- else
- {
- ExitPrint("Please put me in the directory where T5M.exe is, and then run again.");
- }
- }
- enum SymbolicLink
- {
- File = 0,
- Directory = 1
- }
- [DllImport("kernel32.dll")]
- static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement