using System; using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Net; using System.Security.Cryptography; namespace UltraParadise { internal static class UltraPara { public const string DllName = "UltraPara.dll"; public const string StubName = "version.dll"; private const string Base = "https://updates.smellypawz.com"; private const long MaxBytes = 30L * 1024 * 1024; public static string Status = ""; public static volatile string GameVersion = ""; private static string Cache { get { return Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UltraParadise"), "ultrapara"); } } public static string Sha(byte[] b) { using (SHA256 s = SHA256.Create()) return BitConverter.ToString(s.ComputeHash(b)).Replace("-", "").ToLowerInvariant(); } private static string ShaFile(string p) { using (SHA256 s = SHA256.Create()) using (FileStream f = File.OpenRead(p)) return BitConverter.ToString(s.ComputeHash(f)).Replace("-", "").ToLowerInvariant(); } private static bool Ours(string stub) { try { return FileVersionInfo.GetVersionInfo(stub).ProductName == "UltraPara"; } catch (Exception) { return false; } } public static string State(string game) { if (string.IsNullOrEmpty(game) || !Directory.Exists(game)) return "nogame"; string dll = Path.Combine(game, DllName), stub = Path.Combine(game, StubName); if (File.Exists(stub) && !Ours(stub)) return "foreign"; if (!File.Exists(dll) || !File.Exists(stub)) return "missing"; try { return ShaFile(dll) == UltraParaInfo.DllSha && ShaFile(stub) == UltraParaInfo.StubSha ? "ok" : "old"; } catch (Exception) { return "old"; } } public static string InstalledVersion(string game) { try { string dll = Path.Combine(game, DllName); return File.Exists(dll) ? FileVersionInfo.GetVersionInfo(dll).ProductVersion : null; } catch (Exception) { return null; } } public static bool LoadedIn(Process p) { if (p == null) return false; try { p.Refresh(); foreach (ProcessModule m in p.Modules) if (string.Equals(m.ModuleName, DllName, StringComparison.OrdinalIgnoreCase)) return true; } catch (Exception) { } return false; } public static string LastLog(string game) { try { string f = Path.Combine(Path.Combine(game, "UltraParadise"), "UltraPara.log"); if (!File.Exists(f)) return null; string[] lines; using (FileStream fs = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) using (StreamReader r = new StreamReader(fs)) lines = r.ReadToEnd().Replace("\r", "").Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); return lines.Length > 0 ? lines[lines.Length - 1] : null; } catch (Exception) { return null; } } private static byte[] Download(Action progress) { Directory.CreateDirectory(Cache); string cached = Path.Combine(Cache, UltraParaInfo.ZipName); if (File.Exists(cached)) { byte[] c = File.ReadAllBytes(cached); if (Sha(c) == UltraParaInfo.ZipSha) return c; try { File.Delete(cached); } catch (Exception) { } } byte[] b = null; for (int attempt = 1; ; attempt++) { try { b = Fetch(progress); break; } catch (WebException) { if (attempt >= 3) throw; } catch (IOException) { if (attempt >= 3) throw; } System.Threading.Thread.Sleep(2000 * attempt); } if (Sha(b) != UltraParaInfo.ZipSha) throw new InvalidDataException("the download didn't match the fingerprint built into the trainer, so nothing was installed"); try { File.WriteAllBytes(cached, b); } catch (Exception) { } return b; } private static byte[] Fetch(Action progress) { ServicePointManager.SecurityProtocol |= (SecurityProtocolType)3072; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Base + "/v1/download?app=ultrapara&version=" + UltraParaInfo.Version); req.UserAgent = "UltraParadiseCUTrainer/" + Program.Version; req.Timeout = 15000; req.ReadWriteTimeout = 30000; req.AllowAutoRedirect = false; MemoryStream ms = new MemoryStream(); using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) { long total = res.ContentLength; if (total > MaxBytes) throw new InvalidDataException("the download is too big"); using (Stream s = res.GetResponseStream()) { byte[] buf = new byte[65536]; int n, last = -1; while ((n = s.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, n); if (ms.Length > MaxBytes) throw new InvalidDataException("the download is too big"); if (total > 0 && progress != null) { int pc = (int)(ms.Length * 100 / total); if (pc != last) { last = pc; progress(pc); } } } } } return ms.ToArray(); } private static byte[] Entry(ZipArchive z, string name, string sha) { ZipArchiveEntry e = z.GetEntry(name); if (e == null) throw new InvalidDataException(name + " is missing from the download"); MemoryStream ms = new MemoryStream(); using (Stream s = e.Open()) s.CopyTo(ms); byte[] b = ms.ToArray(); if (Sha(b) != sha) throw new InvalidDataException(name + " didn't match its fingerprint"); return b; } private static void Put(string path, byte[] b) { string tmp = path + ".new"; File.WriteAllBytes(tmp, b); if (File.Exists(path)) File.Delete(path); File.Move(tmp, path); } public static string Install(string game, Action progress) { if (string.IsNullOrEmpty(game) || !Directory.Exists(game)) return "the game folder wasn't found"; if (Installer.GameRunning()) return "close the game first"; string st = State(game); if (st == "foreign") return "your game folder already has a version.dll from another program or mod (like ReShade). Move it out of the game folder, then try again"; if (st == "ok") return null; const string Manual = ". You can also get it from updates.smellypawz.com and put UltraPara.dll and version.dll in the game folder yourself"; byte[] zip; try { zip = Download(progress); } catch (WebException e) { return "couldn't download UltraPara.dll from the update server (" + e.Message + ")" + Manual; } catch (IOException e) { return "the UltraPara.dll download stalled (" + e.Message + ")" + Manual; } catch (Exception e) { return e.Message + Manual; } try { byte[] dll, stub; using (ZipArchive z = new ZipArchive(new MemoryStream(zip), ZipArchiveMode.Read)) { dll = Entry(z, DllName, UltraParaInfo.DllSha); stub = Entry(z, StubName, UltraParaInfo.StubSha); } Put(Path.Combine(game, DllName), dll); Put(Path.Combine(game, StubName), stub); } catch (UnauthorizedAccessException) { return "Windows wouldn't let the trainer write to the game folder. Run the trainer as administrator once"; } catch (Exception e) { return e.Message; } return State(game) == "ok" ? null : "UltraPara.dll was written but doesn't check out. Try again"; } public static string Remove(string game) { if (Installer.GameRunning()) return "close the game first"; try { string dll = Path.Combine(game, DllName), stub = Path.Combine(game, StubName); if (File.Exists(stub) && Ours(stub)) File.Delete(stub); if (File.Exists(dll)) File.Delete(dll); return null; } catch (Exception e) { return e.Message; } } } }