Search Unity

Updating Game While .exe Is Running

Discussion in 'Scripting' started by ShermanZero, Oct 14, 2018.

  1. ShermanZero

    ShermanZero

    Joined:
    Dec 29, 2016
    Posts:
    11
    Hey guys,

    So I have a potentially tricky question. I've been working on creating an updater for my game. It automatically pulls all the files my game uses to run from my server, and attempts to overwrite the existing ones that are on the user's local machine. The problem I'm facing is a sharing exception, which was unsurprising. Windows can't write/overwrite a file if it already exists and is currently being used. So I've brainstormed for a bit, trying to figure out a workaround for this. Before I spend too much time on it though, I wanted to ask for some advice so I don't overcomplicate everything.

    My first thought is that I don't need to download every single file, but I'm not entirely sure. If you want to check out all the files that my game downloads, you can navigate to the PHP script I have set up to list all the raw contents of the game over at:

    https://shermanzero.com/pixelate/resources/php/getRaw.php

    This is essentially what my game pulls, which is then parsed by a function into the actual directory and file locations (function parses data by: $'directory'$ 'file'# !'endOfDirectory'). After that, a coroutine is started to download each individual file. It's not complicated, and it works great... if the game isn't open. Which brings me to another thought, I could create a launcher which acts independently of the game, and then I wouldn't need to worry about files being open when written to.

    Anyway, I'm just looking for some of you guys to toss ideas around with. Let me know if you have any thoughts or advice!

    Thanks!
    Kieran
     
  2. If you really want to update during use, I would do it in this way:
    - create temp directory
    - download the update in there
    - when next time the exe starts, notices the temp directory
    - instead of normal boot, it quits and run an updater (it can be even a batch file) which applies the update
    - the updater restarts the game, which is now updated

    Why on next run? Because you cannot overwrite anything while in use.
    It's a bad idea to update when the user leaves (if you saw updating windows when you shut down, you know it). It's a risk, the player maybe shutting down the computer.

    Although there is a reason why the launchers are the industry standards. They work.
     
    publiweb and ShermanZero like this.
  3. fumiko_239

    fumiko_239

    Joined:
    Jan 11, 2019
    Posts:
    1
    What about renaming the currently running process to something different like "programName_old.exe" and then download the new version to the same directory? When it's downloaded, the old game launches the newly downloaded "programName.exe" and exits itself. The new version can then check if there is a file named "programName_old.exe" and deletes it.
     
    Last edited: Jul 17, 2019
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Sounds doable, but a few thoughts:
    • Process renaming is platform dependent, so you'll need to make implementations for each platform. For Windows I'd look at WMIC for that.
    • You probably need elevated rights before you can rename a process. And this is again platform dependent.
    • Renaming the process probably still blocks the exe that is the source. So you might need to download B.exe from A.exe, then start B.exe, which in turn copies itself to A.exe and then starts A.exe again.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    This is why the launcher is usually a different exe than the game.
     
    Joe-Censored and Lurking-Ninja like this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This ^^^

    Almost every game which implements auto updates does so via a launcher.
     
    SparrowGS likes this.