Multipurpose utility for managing Games for Windows - LIVE installs and content. (Mirrored from https://github.com/InvoxiPlayGames/GfWLUtility)
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Drawing;
5using System.IO;
6using System.Linq;
7using System.Runtime.InteropServices;
8using System.Text;
9
10namespace GfWLUtility
11{
12 internal class UtilityFuncs
13 {
14
15 [DllImport("shell32.dll", BestFitMapping = false, CharSet = CharSet.Auto, EntryPoint = "SHDefExtractIcon")]
16 public static extern int SHDefExtractIcon(string pszIconFile, int index, uint uFlags, ref IntPtr phiconLarge, ref IntPtr phiconSmall, uint nIconSize);
17
18 public static string CensorString(string str, int start, int end, int min_length)
19 {
20 if (str == null || str.Length < min_length) return str;
21 int x_fill = str.Length - start - end;
22 return str.Substring(0, start) + new string('\u25CF', x_fill) + str.Substring(str.Length - end, end);
23 }
24
25 public static string CensorEmail(string email)
26 {
27 string[] emailsplit = email.Split('@');
28 if (emailsplit.Length != 2) return email;
29 string[] domainsplit = emailsplit[1].Split('.');
30 if (domainsplit.Length < 2) return email;
31 string finalemail = emailsplit[0][0] + new string('\u25CF', 5) + "@" + domainsplit[0][0] + new string('\u25CF', 5) + "." + domainsplit[1];
32 if (domainsplit.Length > 2)
33 finalemail += '.' + string.Join(".", domainsplit.Skip(2).ToArray());
34 return finalemail;
35 }
36
37 // https://stackoverflow.com/a/58779
38 public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
39 {
40 foreach (DirectoryInfo dir in source.GetDirectories())
41 CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
42 foreach (FileInfo file in source.GetFiles())
43 file.CopyTo(Path.Combine(target.FullName, file.Name));
44 }
45
46 public static string BytesToString(double bytes, int round = 2)
47 {
48 if (bytes > Math.Pow(1000, 4)) return $"{Math.Round(bytes / Math.Pow(1000, 4), round)} TB";
49 if (bytes > Math.Pow(1000, 3)) return $"{Math.Round(bytes / Math.Pow(1000, 3), round)} GB";
50 if (bytes > Math.Pow(1000, 2)) return $"{Math.Round(bytes / Math.Pow(1000, 2), round)} MB";
51 if (bytes > Math.Pow(1000, 1)) return $"{Math.Round(bytes / Math.Pow(1000, 1), round)} KB";
52 return $"{bytes} B";
53 }
54
55 public static int CountTrailingNulls(byte[] buf)
56 {
57 int i = buf.Length - 1;
58 while (buf[i] == 0 && i > 0)
59 --i;
60 return i;
61 }
62
63 public static Bitmap Get48x48Icon(string exe_path)
64 {
65 // get the icon from the shell
66 IntPtr hIconLarge = default;
67 IntPtr hIconSmall = default; // ignored?
68 if (SHDefExtractIcon(exe_path, 0, 0, ref hIconLarge, ref hIconSmall, 48) != 0)
69 return null;
70
71 // load the icon and convert it to a bitmap
72 Icon largeIcon = Icon.FromHandle(hIconLarge);
73 Bitmap largeBitmap = largeIcon.ToBitmap();
74
75 largeIcon.Dispose();
76 return largeBitmap;
77 }
78
79 public static string GetFormattedTitleID(uint titleID)
80 {
81 ushort gameNum = (ushort)(titleID & 0xFFFF);
82 char publisher1 = (char)(titleID >> 24);
83 char publisher2 = (char)((titleID >> 16) & 0xFF);
84 return publisher1.ToString() + publisher2.ToString() + "-" + gameNum.ToString();
85 }
86
87 public static Version GetProductVersion(string exe_path)
88 {
89 FileVersionInfo info = FileVersionInfo.GetVersionInfo(exe_path);
90 if (info == null) return null;
91 return new Version(info.ProductVersion);
92 }
93
94 public static bool IsWindowsModern()
95 {
96 // are we higher than Windows 8?
97 return Environment.OSVersion.Version.CompareTo(new Version("6.2")) >= 0;
98 }
99
100 public static bool IsWindowsXP()
101 {
102 // are we lower than Windows Vista?
103 return Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0;
104 }
105
106 public static bool IsWindowsLegacy()
107 {
108 // are we lower than Windows XP?
109 return Environment.OSVersion.Version.CompareTo(new Version("5.1")) < 0;
110 }
111
112 [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
113 [return: MarshalAs(UnmanagedType.Bool)]
114 private static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool wow64Process);
115
116 public static bool IsWindows64Bit()
117 {
118 using (Process p = Process.GetCurrentProcess())
119 {
120 bool retVal;
121 if (!IsWow64Process(p.Handle, out retVal))
122 {
123 return false;
124 }
125 return retVal;
126 }
127 }
128
129 public static string GetLocalDirectory(string dirname)
130 {
131 return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\GfWLUtility\\" + dirname + "\\";
132 }
133
134 // https://stackoverflow.com/a/5076491
135 public static T BytesToStructure<T>(byte[] bytes)
136 {
137 int size = Marshal.SizeOf(typeof(T));
138 if (bytes.Length != size)
139 throw new Exception($"Invalid size (got {bytes.Length}, expected {size})");
140
141 IntPtr ptr = Marshal.AllocHGlobal(size);
142 try
143 {
144 Marshal.Copy(bytes, 0, ptr, size);
145 return (T)Marshal.PtrToStructure(ptr, typeof(T));
146 }
147 finally
148 {
149 Marshal.FreeHGlobal(ptr);
150 }
151 }
152 }
153}