Skip to content
This repository has been archived by the owner on Jun 24, 2023. It is now read-only.

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyvinia committed Mar 15, 2023
1 parent c1133c5 commit 54e31fb
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 0 deletions.
174 changes: 174 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System.Diagnostics;
using System.IO.Compression;
using System.Reflection;
using DyviniaUtils;
using Microsoft.Win32;

namespace StayOnOrigin {
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 TempFolder => Path.Combine(Environment.CurrentDirectory, "temp");

static void Main() {
// Version and Stuff
Console.WriteLine($"StayOnOrigin v{Assembly.GetEntryAssembly().GetName().Version.ToString()[..5]} by Dyvinia");
WriteSeparator();

// Kill All Origin/EA related processes
KillEA();

// 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...");
UpdateOrigin().Wait();
WriteSeparator();
}
else {
Console.WriteLine($"Origin v{OriginVersion}");
WriteSeparator();
}

// Delete Origin's internal updater
RemoveSetupInternal();
WriteSeparator();

// Disable EA Desktop migration
DisableMigration();
WriteSeparator();

// End
ResetTempDir(false);
Console.WriteLine("Done");
AnyKeyContinue();

}

static async Task UpdateOrigin() {
ResetTempDir();

// Download
string originURL = @"https://origin-a.akamaihd.net/Origin-Client-Download/origin/live/OriginUpdate_10_5_118_52644.zip";
string destinationPath = Path.Combine(TempFolder, 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 RemoveSetupInternal() {
string originSetupInternal = OriginPath.Replace("Origin.exe", "OriginSetupInternal.exe");
string originThinSetupInternal = OriginPath.Replace("Origin.exe", "OriginThinSetupInternal.exe");

if (File.Exists(originSetupInternal)) {
// Backup file by copying to file.exe.bak
Console.WriteLine($"Backing up {originSetupInternal}");
File.Copy(originSetupInternal, originSetupInternal + ".bak", true);

// Replace the contents of the file with nothing
Console.WriteLine($"Clearing {originSetupInternal}");
File.WriteAllText(originSetupInternal, "");
}

if (File.Exists(originThinSetupInternal)) {
// Backup file by copying to file.exe.bak
Console.WriteLine($"Backing up {originThinSetupInternal}");
File.Copy(originThinSetupInternal, originThinSetupInternal + ".bak", true);

// Replace the contents of the file with nothing
Console.WriteLine($"Clearing {originThinSetupInternal}");
File.WriteAllText(originThinSetupInternal, "");
}
}

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();

string migrationSetting = " <Setting value=\"true\" key=\"MigrationDisabled\" type=\"1\"/>";
string updateUrlSetting = " <Setting key=\"UpdateURL\" value=\"http://blah.blah/\" type=\"10\"/>";
string autoPatchGlobalSetting = " <Setting key=\"AutoPatchGlobal\" value=\"true\" type=\"1\"/>";
string autoUpdateSetting = " <Setting value=\"true\" key=\"AutoUpdate\" type=\"1\"/>";

List<string> settingsCheck = new() {
"</Settings>",
migrationSetting,
updateUrlSetting,
autoPatchGlobalSetting,
autoUpdateSetting
};
foreach (string line in fileLines) {
if (!settingsCheck.Any(line.Contains))
fileLinesNew.Add(line);
}

// Add stuff
fileLinesNew.Add(migrationSetting);
fileLinesNew.Add(updateUrlSetting);
fileLinesNew.Add(autoPatchGlobalSetting.Replace("true", "false"));
fileLinesNew.Add(autoUpdateSetting.Replace("true", "false"));
fileLinesNew.Add("</Settings>");

// write new text
Console.WriteLine($"Writing text to {localXML}");
File.WriteAllLines(localXML, fileLinesNew.ToArray());
}

static void KillEA() {
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();
}

static void ResetTempDir(bool recreateDir = true) {
if (Directory.Exists(TempFolder))
Directory.Delete(TempFolder, true);
if (recreateDir)
Directory.CreateDirectory(TempFolder);
}

static void WriteSeparator() {
Console.WriteLine(new string('-', Console.WindowWidth - 1));
}

static void AnyKeyContinue() {
Console.Write("Press Any Key to Continue...");
Console.ReadKey();
}
}
}
18 changes: 18 additions & 0 deletions StayOnOrigin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<PublishAot>true</PublishAot>
<ApplicationIcon>icon.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>

<AssemblyName>StayOnOrigin</AssemblyName>
<Version>1.0.0</Version>
<Copyright>Copyright © 2023 Dyvinia</Copyright>
<Company>Dyvinia</Company>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions StayOnOrigin.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StayOnOrigin", "StayOnOrigin.csproj", "{1483428B-D2E5-4735-9C11-8E4D874B071D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1483428B-D2E5-4735-9C11-8E4D874B071D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1483428B-D2E5-4735-9C11-8E4D874B071D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1483428B-D2E5-4735-9C11-8E4D874B071D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1483428B-D2E5-4735-9C11-8E4D874B071D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3CD5550B-B172-4007-819F-A0A64CAD3E1B}
EndGlobalSection
EndGlobal
46 changes: 46 additions & 0 deletions Utils/Downloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace DyviniaUtils {
class Downloader {
/// <summary>
/// Downloads file to destination
/// </summary>
public async static Task Download(string downloadUrl, string destinationFilePath, IProgress<double> progress) {
using HttpClient httpClient = new() { Timeout = TimeSpan.FromMinutes(30) };
using HttpResponseMessage response = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead);

response.EnsureSuccessStatusCode();
long? totalBytes = response.Content.Headers.ContentLength;

using Stream contentStream = await response.Content.ReadAsStreamAsync();
long? totalBytesRead = 0L;
long readCount = 0L;
byte[] buffer = new byte[4096];
bool isMoreToRead = true;

using var fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);

do {
int bytesRead = await contentStream.ReadAsync(buffer);
if (bytesRead == 0) {
isMoreToRead = false;
progress.Report((double)((double)totalBytesRead / totalBytes));
continue;
}

await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead));

totalBytesRead += bytesRead;
readCount++;

if (readCount % 100 == 0) {
progress.Report((double)((double)totalBytesRead / totalBytes));
}
}
while (isMoreToRead);
}
}
}
79 changes: 79 additions & 0 deletions app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

<!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

</application>
</compatibility>

<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
-->

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
-->

</assembly>
Binary file added icon.ico
Binary file not shown.

0 comments on commit 54e31fb

Please sign in to comment.