- Home /
How to create a patch/patcher/updater
Hi guys, I'd like to create a patch/patcher/updater for my game. For example if i changed something i don't wanna reupload all my game but only a patch that changes only what i want. Is it possible? Thanks.
What target platform do you have in $$anonymous$$d? Standalone? (Win, Linux, $$anonymous$$ac)
Answer by Bunny83 · Jan 27, 2018 at 01:23 PM
For standalone builds there are generally three kinds of patchers / updaters:
A version-to-version updater
A live validator file-to-file updater
An actual partial / differential file patcher which has patch instructions on a file-to-file basis
The first kind is where you would explicitly download an update from a specific version to of the game to a new version. This requires that you can identify the current version of the game in order to not apply the update to the wrong version. This is usually done by storing a version string somewhere along the game files. The actual patch / update could be distributed any way you like
For the second kind you need some webspace where you can actually host all your game files individually and which allows the automatic patcher to request any file individually. You also need to identify the "version" of each file. This is usually done by simply storing the file name, it's size and a hash value to uniquely identifying a file version. At each start the updater would download the current file descriptions from your server and calculate the hashes of all local files. If there is some kind of difference (missing files, outdated files, corrupted files) the patcher would selectively download the latest version of each file. This however has some drawbacks. You need a way to authenticate the user to your webservice. Otherwise everyone could simply pull your game from your webservice. Most games that use such an approach are either free-to-play where it doesn't matter or they require some kind of user account.
The last kind is more a technique than a seperate kind. It could be used in the first and second kind i've mentioned. However it can get quite complicated. That means you would create actual patch files which contain instructions how to bring version X of a certain file to version Y. This requires that you create actualy differential patches between all possible versions of a file. The main advantage is that patch files are usually smaller for large files if only a tiny bit has changed in that file. This technique has several drawbacks. It requires the previous version to be 100% intact. If a file is missing or corrupted it can't be patched and you would need the complete version of the file. So using this technique in the first kind of updates would simple make the update fail if something is wrong. In the second kind the updater would choose what to download based on the current version of the files. Implementing such a feature yourself can be quite complicated and usually isn't worth it. If you want to use differential patches you better look for existing solutions.
Since Unity packs most assets into a few asset database files the success of the differential technique highly depends on Unity's internal asset format. AFAIK the asset files are not compressed on standalone so differences could be detected. However if the files are actually compressed differential patches would be more or less useless as changing a character in a TextAsset could result in a completely different file depending on how it's compressed. Since the assets format is an internal implementation detail it could change in the future so i wouldn't rely on the current implementation. In most cases you want to use a per-file patch.
Generally the updater would be composed of several "components" (not in the sense of Unity but in the sense of modules):
Validator
Updater
The patcher is usually a seperate application. You can create a simple .NET application. Automatic updaters do not really require much UI as they just validate the game and if it detects a new version it automatically updates the game before launch. The easiest solution is to run the actual game through the updater. So you would use the updater as game launcher. If the game is a multiplayer game which requires the user to have the latest version you may need to implement the validation in both, the updater and the game to prevent synchronisation problems / errors with outdated clients.
The validator would check the current state of the game files and if the update can be used on this game (in case of a standalone update). The live updater would start by downloading the current game file definition file (JSON, XML, a binary format, ....) and then comparing each file in the current version with the expected one. This results in a list of files that need to be updated / downloaded.
The updater itself performs the actual update. The live updater should download all required files in a temporary folder to ensure all required files are there. You don't want to patch on a file-to-file basis as this could corrupt the game. If you only update some files but not all (due to interrupted connection) the game would be broken. Once all required file are there you would replace / patch each file based on the downloaded files. When done just start the game.
Can you post a link for a tutorial? I'm searching them but i'm only finding demos... Thank you.
No, i can't since i don't know any tutorials and i never searched for any tutorials. I highly doubt that there is a "tutorial" for "how to create a patch system from scratch". It seems nowadays people do no longer understand what a tutorial is meant for. It's meant as a program$$anonymous$$g example to $$anonymous$$ch basic concepts. You basically ask for a ready-to-use solution. The point of program$$anonymous$$g and software engineering is to use your skill and knowledge to solve problems on your own. So if you think you are not able to pull off such a system on your own you should either search for a ready-to-use solution or improve your skill in various fields. Such a system is not trivial even if it sounds like it should be simple. Especially when it comes to a live update system the server side alone can be a real challance to get it right and secure. If you are a beginner in software development you should definitely should stay away from such a project.
I recently developed an autopatcher/autoupdater in c#. It falls on @Bunny83 's "second type of patchers"
Checkout my open source repository if you're interested in this approach. I think you could learn from it. It also serves as a tutorial and I'm also searching for someone to contribute to the repository as well: https://github.com/igor-ruivo/generic-auto-updater
Hi im working on a launcher for my game but i'm confused how can you modify or patch a unity game files as a third party app ( c# ) plz reply any help is appreciated and thx for the great explanation!
Well, what exactly do you mean by "app"? What platform? "App" usually refers to Android or iOS app. Though there are now UWP apps as well. Or do you mean an ordinary standalone application?
$$anonymous$$y answer was mainly focused on how you might implement such an updater / launcher on windows for a standalone build.
Anyways as I already mentioned in the answer the simplest solution is to just replace changed files of the actual game build. If you're asking for how to perform differential patches, I'm sorry but that's a too advanced topic to explain in a single answer here on Unity Answers. Such a system would require a lot of pre-processing to deter$$anonymous$$e what parts of which files have changed and what actions you have to perform on the files to get to the wanted final stage. Also a lot of precautions has to be taken since trying to patch the wrong version would result in complete garbage that might just crash or even damage the users device. Anyways for creating actual diff patches you would most likely use an existing diff tool that supports binary files.
$$anonymous$$ost games with launchers / updates just add, replace, remove the changed files completely. So when you create a new build you would create a list of all files that belong to the build and probably calculate a hash value for each file in order to deter$$anonymous$$e if the user has the right file or not. The list of files and the corresponding hash values is essentially a dictionary / file list that the updater will download and compare the list against the files currently on the users machine. If any outdated, corrupted or missing files are detected the updater would download them one by one and once all files has been downloaded you can replace them in the actual game data folder.
As I said we can't really go much more into detail. There are generally several different ways how to implement such a system.
Your answer
Follow this Question
Related Questions
Custom .assets files in the built "Project"_Data folder 0 Answers
Asset Server problems. Old updates appearing in updates list after deleting assets 1 Answer
How I can download Terrain from .Asset file? 2 Answers
Unity Asset Store and Text Serialisation 0 Answers
Import Project from Asset store 6 Answers