This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
forked from Dyvinia/StayOnOrigin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
207 lines (178 loc) · 9.07 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using DyviniaUtils;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO.Compression;
using System.Reflection;
namespace StayOnOriginPlus {
internal class Program {
public static string OriginPath => Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Origin")?.GetValue("OriginPath")?.ToString();
public static Version OriginVersion => new(FileVersionInfo.GetVersionInfo(OriginPath).FileVersion.Replace(",", "."));
public static string TempDirPath => Path.Combine(Environment.CurrentDirectory, "temp");
static void Main() {
// Version and Stuff
Console.WriteLine($"StayOnOrigin+ v{Assembly.GetEntryAssembly().GetName().Version.ToString()[..5]} by VictorPL\n" +
" Forked from StayOnOrigin by Dyvinia and VictorPL");
WriteSeparator();
// Check if Origin Exists
if (!File.Exists(OriginPath)) {
Console.WriteLine("Origin is not installed or could not be found.");
Console.WriteLine("Press Y to install Origin, or any other key to exit.");
if (Console.ReadKey().Key != ConsoleKey.Y)
Environment.Exit(0);
Console.WriteLine("\nDownloading Origin...");
ResetTempDir();
InstallOrigin().Wait();
WriteSeparator();
}
// Kill All Origin/EA related processes
KillProcesses();
// Check if Origin is too new (Anything after 10.5.120.x)
if (OriginVersion.CompareTo(new("10.5.120.0")) > 0) {
Console.WriteLine($"Origin v{OriginVersion} is too recent");
Console.WriteLine("Downgrading to Origin v10.5.118.52644...");
ResetTempDir();
UpdateOrigin().Wait();
WriteSeparator();
}
else {
Console.WriteLine($"Found Origin v{OriginVersion}");
WriteSeparator();
}
// Delete Origin's internal updater
ClearFile(OriginPath.Replace("Origin.exe", "OriginSetupInternal.exe"), false);
ClearFile(OriginPath.Replace("Origin.exe", "OriginThinSetupInternal.exe"), false);
WriteSeparator();
// Disable EA Desktop migration
DisableMigration();
WriteSeparator();
// Enable R&D mode
Console.WriteLine("Press Y to enable Origin's hidden R&D mode for (reportedly) better downloads, or any other key to skip.");
if (Console.ReadKey().Key == ConsoleKey.Y)
EnableResearchDevelopment(OriginPath.Replace("Origin.exe", "EACore.ini"));
WriteSeparator();
// End
ResetTempDir(false);
Console.WriteLine("Done");
WriteSeparator();
Console.WriteLine("Press Y if you would like to launch Origin (and exit), or any other key to exit.");
if (Console.ReadKey().Key == ConsoleKey.Y)
Process.Start(OriginPath);
}
static async Task InstallOrigin() {
// Download Origin
string originURL = @"https://cdn.discordapp.com/attachments/693482239593283694/1086045449191968899/OriginSetup_1.exe";
//string originURL = @"https://download.dm.origin.com/origin/live/OriginSetup.exe";
string destinationPath = Path.Combine(TempDirPath, Path.GetFileName(originURL));
IProgress<double> progress = new Progress<double>(p => {
int percentage = Convert.ToInt32(p * 100);
Console.Write($"\rDownloading: {percentage}%");
});
await Downloader.Download(originURL, destinationPath, progress);
Console.WriteLine();
Console.WriteLine($"Downloaded {Path.GetFileName(originURL)}");
// Install
var originInstall = Process.Start(destinationPath);
Console.WriteLine("Origin is being installed...");
originInstall.WaitForExit();
Console.WriteLine();
Console.WriteLine($"Installed Origin");
}
static async Task UpdateOrigin() {
// Download from EA Servers
string originURL = @"https://origin-a.akamaihd.net/Origin-Client-Download/origin/live/OriginUpdate_10_5_118_52644.zip";
string destinationPath = Path.Combine(TempDirPath, Path.GetFileName(originURL));
IProgress<double> progress = new Progress<double>(p => {
int percentage = Convert.ToInt32(p * 100);
Console.Write($"\rDownloading: {percentage}%");
});
await Downloader.Download(originURL, destinationPath, progress);
Console.WriteLine();
Console.WriteLine($"Downloaded {Path.GetFileName(originURL)}");
// Install
using ZipArchive archive = ZipFile.OpenRead(destinationPath);
int i = 0;
foreach (ZipArchiveEntry entry in archive.Entries) {
int percentage = Convert.ToInt32(100 * i++ / (float)archive.Entries.Count);
Console.Write($"\rInstalling: {percentage}%");
entry.ExtractToFile(Path.Combine(Path.GetDirectoryName(OriginPath), entry.FullName), true);
}
Console.WriteLine();
Console.WriteLine($"Installed Origin v10.5.118.52644");
}
static void ClearFile(string path, bool readOnly) {
if (File.Exists(path) && !File.GetAttributes(path).HasFlag(FileAttributes.ReadOnly)) {
// Backup file by copying to file.exe.bak
Console.WriteLine($"Backing up {path}");
if (!File.Exists(path + ".bak"))
File.Copy(path, path + ".bak");
// Replace the contents of the file with nothing
Console.WriteLine($"Clearing {path}");
File.WriteAllText(path, "");
// Set the file to read-only
if (readOnly)
File.SetAttributes(path, FileAttributes.ReadOnly);
}
else
Console.WriteLine($"ERROR: {path} does not exist or is read-only.\nThis error is expected for OriginSetupInternal.exe and can be ignored.");
}
static void EnableResearchDevelopment(string path) {
File.WriteAllText(path, "[connection]\r\nEnvironmentName=production\r\n\r\n[Feature]\r\nCdnOverride=akamai");
Console.WriteLine("\nR&D mode enabled successfully.");
}
static void DisableMigration() {
string localXML = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Origin\local.xml");
Console.WriteLine($"Opening {localXML}");
string[] fileLines = File.ReadAllLines(localXML).ToArray();
List<string> fileLinesNew = new();
List<string> settingsCheck = new() {
"MigrationDisabled",
"UpdateURL",
"AutoPatchGlobal",
"AutoUpdate",
"/Settings"
};
foreach (string line in fileLines) {
if (!settingsCheck.Any(line.Contains))
fileLinesNew.Add(line);
}
// Add new settings
fileLinesNew.Add(" <Setting key=\"MigrationDisabled\" value=\"true\" type=\"1\"/>");
fileLinesNew.Add(" <Setting key=\"UpdateURL\" value=\"http://blah.blah/\" type=\"10\"/>");
fileLinesNew.Add(" <Setting key=\"AutoPatchGlobal\" value=\"false\" type=\"1\"/>");
fileLinesNew.Add(" <Setting key=\"AutoUpdate\" value=\"false\" type=\"1\"/>");
fileLinesNew.Add("</Settings>");
// write new text
Console.WriteLine($"Writing text to {localXML}");
File.WriteAllLines(localXML, fileLinesNew.ToArray());
}
static void KillProcesses() {
bool addSeparator = false;
foreach (Process process in Process.GetProcessesByName("EADesktop")) {
process.Kill();
Console.WriteLine($"Killed {process.ProcessName}");
addSeparator = true;
}
foreach (Process process in Process.GetProcessesByName("Origin")) {
process.Kill();
Console.WriteLine($"Killed {process.ProcessName}");
addSeparator = true;
}
foreach (Process process in Process.GetProcessesByName("OriginWebHelperService")) {
process.Kill();
Console.WriteLine($"Killed {process.ProcessName}");
addSeparator = true;
}
if (addSeparator)
WriteSeparator(); // Prevent double separator from appearing in console
}
static void ResetTempDir(bool recreateDir = true) {
if (Directory.Exists(TempDirPath))
Directory.Delete(TempDirPath, true);
if (recreateDir)
Directory.CreateDirectory(TempDirPath);
}
static void WriteSeparator() {
Console.WriteLine(new string('-', Console.WindowWidth - 1));
}
}
}