diff options
author | Oren Titane (Genome36) <orentitane@gmail.com> | 2015-08-21 06:40:59 +0300 |
---|---|---|
committer | Mitchell Stokes <mogurijin@gmail.com> | 2015-08-21 06:42:57 +0300 |
commit | df00098a868a4f58f8b0f336cae54b6be68f4b38 (patch) | |
tree | 223c2b8fc07bc0e0d785cb4e5b1914802a366806 /game_engine_publishing.py | |
parent | 82ff141efc015ab3cb37cd9969a0073f24664661 (diff) |
Game Engine Publishing: Player creation code cleanup
Changed the way files get opened to a "with open() as file" statement. Makes for a easier to understand code as the file data manipulations are nested in that same statement.
Reviewers: moguri
Reviewed By: moguri
Subscribers: Genome36
Projects: #game_engine, #addons
Differential Revision: https://developer.blender.org/D1455
Diffstat (limited to 'game_engine_publishing.py')
-rw-r--r-- | game_engine_publishing.py | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/game_engine_publishing.py b/game_engine_publishing.py index 644fce07..3d7c4998 100644 --- a/game_engine_publishing.py +++ b/game_engine_publishing.py @@ -80,10 +80,9 @@ def WriteRuntime(player_path, output_path, asset_paths, copy_python, overwrite_l output_path = bpy.path.ensure_ext(output_path, '.exe') # Get the player's binary and the offset for the blend - file = open(player_path, 'rb') - player_d = file.read() - offset = file.tell() - file.close() + with open(player_path, "rb") as file: + player_d = file.read() + offset = file.tell() # Create a tmp blend file (Blenderplayer doesn't like compressed blends) tempdir = tempfile.mkdtemp() @@ -95,31 +94,28 @@ def WriteRuntime(player_path, output_path, asset_paths, copy_python, overwrite_l ) # Get the blend data - blend_file = open(blend_path, 'rb') - blend_d = blend_file.read() - blend_file.close() + with open(blend_path, "rb") as blend_file: + blend_d = blend_file.read() # Get rid of the tmp blend, we're done with it os.remove(blend_path) os.rmdir(tempdir) # Create a new file for the bundled runtime - output = open(output_path, 'wb') - - # Write the player and blend data to the new runtime - print("Writing runtime...", end=" ", flush=True) - output.write(player_d) - output.write(blend_d) - - # Store the offset (an int is 4 bytes, so we split it up into 4 bytes and save it) - output.write(struct.pack('BBBB', (offset >> 24) & 0xFF, - (offset >> 16) & 0xFF, - (offset >> 8) & 0xFF, - (offset >> 0) & 0xFF)) - - # Stuff for the runtime - output.write(b'BRUNTIME') - output.close() + with open(output_path, "wb") as output: + # Write the player and blend data to the new runtime + print("Writing runtime...", end=" ", flush=True) + output.write(player_d) + output.write(blend_d) + + # Store the offset (an int is 4 bytes, so we split it up into 4 bytes and save it) + output.write(struct.pack('BBBB', (offset >> 24) & 0xFF, + (offset >> 16) & 0xFF, + (offset >> 8) & 0xFF, + (offset >> 0) & 0xFF)) + + # Stuff for the runtime + output.write(b'BRUNTIME') print("done", flush=True) |