[Bf-extensions-cvs] [df00098] master: Game Engine Publishing: Player creation code cleanup

Oren Titane Genome36 noreply at git.blender.org
Fri Aug 21 05:44:38 CEST 2015


Commit: df00098a868a4f58f8b0f336cae54b6be68f4b38
Author: Oren Titane (Genome36)
Date:   Thu Aug 20 20:40:59 2015 -0700
Branches: master
https://developer.blender.org/rBAdf00098a868a4f58f8b0f336cae54b6be68f4b38

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

===================================================================

M	game_engine_publishing.py

===================================================================

diff --git a/game_engine_publishing.py b/game_engine_publishing.py
index 644fce0..3d7c499 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)



More information about the Bf-extensions-cvs mailing list