Skip to content

Commit

Permalink
Version 0.3.0
Browse files Browse the repository at this point in the history
seamless auto-update
  • Loading branch information
uDMBK authored Apr 22, 2021
1 parent 7e1001f commit 4d352ef
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions AdRework/AdRework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
67 changes: 60 additions & 7 deletions AdRework/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO.Compression;
using Timer = System.Timers.Timer;

namespace AdRework {
static class Program {
// if u are forking the project set this to your fork so the auto-updater checks your repo!
// any files other than AdRework.exe will not be updated automatically
private const string repo = "uDMBK/AdRework";

[STAThread]
static void Main() { AdReworkStart(); }
public enum SpotifyAdStatus { None, Ad, Unknown }
Expand Down Expand Up @@ -102,10 +109,8 @@ private static string GetSpotifyTrackInfo() {

private static void CreateShortcut() {
try {
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

using (StreamWriter writer = new StreamWriter($"{Environment.GetFolderPath(Environment.SpecialFolder.Startup)}\\AdRework.url")) {
string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
string app = Assembly.GetExecutingAssembly().Location;
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=file:///" + app);
writer.WriteLine("IconIndex=0");
Expand Down Expand Up @@ -151,14 +156,17 @@ private static string GetBetween(string Source, string Start, string End) {
// ms interval AdRework should perform an 'integrity check' at
private static int IntegrityInterval = 450;

// if AdRework should Auto-Update
private static bool AutoUpdate = true;

private static void LoadConfiguration() {
try {
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

if (!Directory.Exists($"{AppData}\\dmbk")) Directory.CreateDirectory($"{AppData}\\dmbk");
if (!Directory.Exists($"{AppData}\\dmbk\\AdRework")) Directory.CreateDirectory($"{AppData}\\dmbk\\AdRework");
if (!File.Exists($"{AppData}\\dmbk\\AdRework\\config.ini")) {
File.WriteAllText($"{AppData}\\dmbk\\AdRework\\config.ini", "SkipAds='True'\nMuteAds='True'\nBypassAds='True'\nImmediateSkip='True'\nRegistryStartup='True'\nForceRun='False'\nFallbackVolume='50'\nAdInterval='100'\nIntegrityInterval='450'");
File.WriteAllText($"{AppData}\\dmbk\\AdRework\\config.ini", "SkipAds='True'\nMuteAds='True'\nBypassAds='True'\nImmediateSkip='True'\nRegistryStartup='True'\nForceRun='False'\nAutoUpdate='True'\nFallbackVolume='100'\nAdInterval='100'\nIntegrityInterval='450'");
return; }

try {
Expand All @@ -169,23 +177,68 @@ private static void LoadConfiguration() {
BypassAds = bool.Parse(GetBetween(config, "BypassAds='", "'"));
ImmediateSkip = bool.Parse(GetBetween(config, "ImmediateSkip='", "'"));
RegistryStartup = bool.Parse(GetBetween(config, "RegistryStartup='", "'"));
AutoUpdate = bool.Parse(GetBetween(config, "AutoUpdate='", "'"));
ForceRun = bool.Parse(GetBetween(config, "ForceRun'", "'"));
FallbackVolume = Convert.ToInt32(GetBetween(config, "FallbackVolume='", "'"));
AdInterval = Convert.ToInt32(GetBetween(config, "AdInterval='", "'"));
IntegrityInterval = Convert.ToInt32(GetBetween(config, "IntegrityInterval='", "'")); }
catch (Exception) { // if reading config fails, reset it
Console.WriteLine("failed to read config!");
File.WriteAllText($"{AppData}\\dmbk\\AdRework\\config.ini", "SkipAds='True'\nMuteAds='True'\nBypassAds='True'\nImmediateSkip='True'\nRegistryStartup='True'\nForceRun='False'\nFallbackVolume='50'\nAdInterval='100'\nIntegrityInterval='450'"); }
File.WriteAllText($"{AppData}\\dmbk\\AdRework\\config.ini", "SkipAds='True'\nMuteAds='True'\nBypassAds='True'\nImmediateSkip='True'\nRegistryStartup='True'\nForceRun='False'\nAutoUpdate='True'\nFallbackVolume='100'\nAdInterval='100'\nIntegrityInterval='450'"); }
} catch (Exception) {}}

private static void IntegrityCheck(object sender, EventArgs e) {
Process Spotify = Process.GetProcessesByName("Spotify").FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.MainWindowTitle));
if (Spotify == null) return;
Console.WriteLine($"spotify volume: {GetApplicationVolume(Spotify.Id):N0} || {GetAdStatus()}");
if (GetAdStatus() == SpotifyAdStatus.None && GetApplicationVolume(Spotify.Id) <= 0) SetApplicationVolume(Spotify.Id, FallbackVolume / 100); }

private static string GetLatestTag() {
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.github.com/repos/{repo}/releases/latest");
request.Method = "GET"; request.UserAgent = "AdRework Auto-Update"; request.Accept = "application/json";
StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());

return GetBetween(reader.ReadToEnd(), "\"tag_name\":\"", "\""); } catch (Exception e) { Console.WriteLine($"{e.Message}"); }
return ""; }

private static void UpdateProgram(string TagName) {
try {
// create install directory
Directory.CreateDirectory($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update");

// ensure that the install directory is empty
foreach (FileInfo file in new DirectoryInfo($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update").GetFiles()) file.Delete();

// download update
new WebClient().DownloadFile($"https://github.com/{repo}/releases/download/{TagName}/AdRework.zip", $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update\\AdRework.new.zip");

// extract update
ZipFile.ExtractToDirectory($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update\\AdRework.new.zip", $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update");
// delete original zip
File.Delete($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update\\AdRework.new.zip");

// create a batch file to override current version with update then delete itself after 500ms
File.WriteAllText($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\update.bat", $"@echo off\nping 127.0.0.1 -n 1 -w 500> nul\"\nmove \"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update\\*.*\" \"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\"\nstart \"\" \"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\AdRework.exe\"\n(goto) 2>nul & del \"%~f0\"");

// run the batch file and immediately terminate process
Process.Start($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\update.bat");
Process.GetCurrentProcess().Kill();
} catch (Exception e) { Console.WriteLine($"failed to auto-update due to {e.Message}!"); }}

private static void AdReworkStart() {
// load config
LoadConfiguration();

// update cleanup
if (Directory.Exists($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update"))
Directory.Delete($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Update");

// check for updates
if (AutoUpdate) {
string LatestTag = GetLatestTag();
if (Assembly.GetExecutingAssembly().GetName().Version < Version.Parse(LatestTag)) UpdateProgram(LatestTag); }

if (!SkipAds && !MuteAds && !BypassAds && !ForceRun) Process.GetCurrentProcess().Kill(); // terminate if it has nothing to do

// start timer checking for ads every 100ms
Expand All @@ -198,7 +251,7 @@ private static void AdReworkStart() {
CreateShortcut();
if (RegistryStartup)
try { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) {
key.SetValue("AdRework", System.Reflection.Assembly.GetExecutingAssembly().Location); }} catch (Exception) {}
key.SetValue("AdRework", Assembly.GetExecutingAssembly().Location); }} catch (Exception) {}

// keep thread alive indefinetly
Thread.Sleep(-1); }}}
Thread.Sleep(-1); }}}
6 changes: 3 additions & 3 deletions AdRework/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AdRework 0.2.3")]
[assembly: AssemblyTitle("AdRework 0.3.0")]
[assembly: AssemblyDescription("Automatically and seamlessly skip spotify ads!")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("dmbk development")]
Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.3.0")]
[assembly: AssemblyFileVersion("0.2.3.0")]
[assembly: AssemblyVersion("0.3.0.0")]
[assembly: AssemblyFileVersion("0.3.0.0")]

0 comments on commit 4d352ef

Please sign in to comment.