added packing
authorhgn <hgodden00@gmail.com>
Mon, 18 Apr 2022 15:40:16 +0000 (16:40 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 18 Apr 2022 15:40:16 +0000 (16:40 +0100)
__init__.py

index 763e6e3479032d2985a09a6c571b02c4501143b5..14f23e5c581b58a65a0648da0912faf57a22105e 100644 (file)
@@ -1497,6 +1497,17 @@ class CXR_DETECT_COMPILERS(bpy.types.Operator):
 
       return {'FINISHED'}
 
+def cxr_compiler_path( compiler ):
+   settings = bpy.context.scene.cxr_data
+   subdir = settings.subdir
+   path = os.path.normpath(F'{subdir}/../bin/{compiler}.exe')
+   
+   if os.path.exists( path ): return path
+   else: return None
+
+def cxr_winepath( path ):
+   return 'z:'+path.replace('/','\\')
+
 # Main compile function
 #
 class CXR_COMPILER_CHAIN(bpy.types.Operator):
@@ -1617,195 +1628,241 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
       static = _.__class__
       wm = context.window_manager
       
-      if static.TIMER == None:
-         print("Launching compiler toolchain")
+      if static.TIMER != None:
+         print("Chain exiting...")
+         static.USER_EXIT=True
+         return {'RUNNING_MODAL'}
 
-         # Run static compilation units now (collect, vmt..)
-         filepath = bpy.data.filepath
-         directory = os.path.dirname(filepath)
-         settings = bpy.context.scene.cxr_data
-   
-         asset_dir = F"{directory}/modelsrc"
-         material_dir = F"{settings.subdir}/materials/{settings.project_name}"
-         model_dir = F"{settings.subdir}/models/{settings.project_name}"
-         output_vmf = F"{directory}/{settings.project_name}.vmf"
-
-         os.makedirs( asset_dir, exist_ok=True )
-         os.makedirs( material_dir, exist_ok=True )
-         os.makedirs( model_dir, exist_ok=True )
-         
-         static.FILE = open(F"/tmp/convexer_compile_log.txt","w")
-         static.LOG = []
+      print("Launching compiler toolchain")
+
+      # Run static compilation units now (collect, vmt..)
+      filepath = bpy.data.filepath
+      directory = os.path.dirname(filepath)
+      settings = bpy.context.scene.cxr_data
+
+      asset_dir = F"{directory}/modelsrc"
+      material_dir = F"{settings.subdir}/materials/{settings.project_name}"
+      model_dir = F"{settings.subdir}/models/{settings.project_name}"
+      output_vmf = F"{directory}/{settings.project_name}.vmf"
+
+      bsp_local = F"{directory}/{settings.project_name}.bsp"
+      bsp_remote = F"{settings.subdir}/maps/{settings.project_name}.bsp"
+      bsp_packed = F"{settings.subdir}/maps/{settings.project_name}_pack.bsp"
+      packlist = F"{directory}/{settings.project_name}_assets.txt"
+
+      os.makedirs( asset_dir, exist_ok=True )
+      os.makedirs( material_dir, exist_ok=True )
+      os.makedirs( model_dir, exist_ok=True )
+      
+      static.FILE = open(F"/tmp/convexer_compile_log.txt","w")
+      static.LOG = []
 
-         sceneinfo = cxr_scene_collect()
-         image_jobs = []
-         qc_jobs = []
+      sceneinfo = cxr_scene_collect()
+      image_jobs = []
+      qc_jobs = []
 
-         # Collect materials
-         a_materials = set()
-         for brush in sceneinfo['geo']:
-            for ms in brush['object'].material_slots:
+      # Collect materials
+      a_materials = set()
+      for brush in sceneinfo['geo']:
+         for ms in brush['object'].material_slots:
+            a_materials.add( ms.material )
+            if ms.material.cxr_data.shader == 'VertexLitGeneric':
+               errmat = ms.material.name
+               errnam = brush['object'].name
+               print( F"Vertex shader {errmat} used on {errnam}")
+               return {'CANCELLED'}
+      
+      a_models = set()
+      model_jobs = []
+      for ent in sceneinfo['entities']:
+         if ent['object'] == None: continue
+
+         if ent['classname'] == 'prop_static':
+            obj = ent['object']
+            if isinstance(obj,bpy.types.Collection):
+               target = obj
+               a_models.add( target )
+               model_jobs += [(target, ent['origin'], asset_dir, \
+                              settings.project_name, ent['transform'])]
+            else:
+               target = obj.instance_collection
+               if target in a_models:
+                  continue
+               a_models.add( target )
+               
+               # TODO: Should take into account collection instancing offset
+               model_jobs += [(target, [0,0,0], asset_dir, \
+                              settings.project_name, ent['transform'])]
+      
+         elif ent['object'].type == 'MESH':
+            for ms in ent['object'].material_slots:
                a_materials.add( ms.material )
-               if ms.material.cxr_data.shader == 'VertexLitGeneric':
+         
+      for mdl in a_models:
+         uid = asset_uid(mdl)
+         qc_jobs += [F'{uid}.qc']
+
+         for obj in mdl.objects:
+            for ms in obj.material_slots:
+               a_materials.add( ms.material )
+               if ms.material.cxr_data.shader == 'LightMappedGeneric' or \
+                  ms.material.cxr_data.shader == 'WorldVertexTransition':
+
                   errmat = ms.material.name
-                  errnam = brush['object'].name
-                  print( F"Vertex shader {errmat} used on {errnam}")
+                  errnam = obj.name
+                  print( F"Lightmapped shader {errmat} used on {errnam}")
                   return {'CANCELLED'}
-         
-         a_models = set()
-         model_jobs = []
-         for ent in sceneinfo['entities']:
-            if ent['object'] == None: continue
-
-            if ent['classname'] == 'prop_static':
-               obj = ent['object']
-               if isinstance(obj,bpy.types.Collection):
-                  target = obj
-                  a_models.add( target )
-                  model_jobs += [(target, ent['origin'], asset_dir, \
-                                 settings.project_name, ent['transform'])]
-               else:
-                  target = obj.instance_collection
-                  if target in a_models:
-                     continue
-                  a_models.add( target )
-                  
-                  # TODO: Should take into account collection instancing offset
-                  model_jobs += [(target, [0,0,0], asset_dir, \
-                                 settings.project_name, ent['transform'])]
-         
-            elif ent['object'].type == 'MESH':
-               for ms in ent['object'].material_slots:
-                  a_materials.add( ms.material )
-            
-         for mdl in a_models:
-            uid = asset_uid(mdl)
-            qc_jobs += [F'{uid}.qc']
-
-            for obj in mdl.objects:
-               for ms in obj.material_slots:
-                  a_materials.add( ms.material )
-                  if ms.material.cxr_data.shader == 'LightMappedGeneric' or \
-                     ms.material.cxr_data.shader == 'WorldVertexTransition':
-
-                     errmat = ms.material.name
-                     errnam = obj.name
-                     print( F"Lightmapped shader {errmat} used on {errnam}")
-                     return {'CANCELLED'}
-         
-         # Collect images
+      
+      # Collect images
+      for mat in a_materials:
+         for pair in compile_material(mat):
+            decl = pair[0]
+            pdef = pair[1]
+            prop = pair[2]
+
+            if isinstance(prop,bpy.types.Image):
+               flags = 0
+               if 'flags' in pdef: flags = pdef['flags']
+               if prop not in image_jobs:
+                  image_jobs += [(prop,)]
+                  prop.cxr_data.flags = flags
+
+      # Create packlist
+      with open( packlist, "w" ) as fp:
+
          for mat in a_materials:
-            for pair in compile_material(mat):
-               decl = pair[0]
-               pdef = pair[1]
-               prop = pair[2]
-
-               if isinstance(prop,bpy.types.Image):
-                  flags = 0
-                  if 'flags' in pdef: flags = pdef['flags']
-                  if prop not in image_jobs:
-                     image_jobs += [(prop,)]
-                     prop.cxr_data.flags = flags
-         
-         # Convexer jobs
-         static.JOBID = 0
-         static.JOBINFO = []
-         
-         if settings.comp_vmf:
-            static.JOBINFO += [{ 
-               "title": "Convexer",
-               "w": 20,
-               "colour": (1.0,0.3,0.1,1.0),
-               "exec": cxr_export_vmf,
-               "jobs": [(sceneinfo,output_vmf)]
-            }]
-         
-         if settings.comp_textures:
-            if len(image_jobs) > 0:
-               static.JOBINFO += [{
-                  "title": "Textures",
-                  "w": 40,
-                  "colour": (0.1,1.0,0.3,1.0),
-                  "exec": compile_image,
-                  "jobs": image_jobs
-               }]
-
-         game = 'z:'+settings.subdir.replace('/','\\')
-         args = [ \
-             '-game', game, settings.project_name
-         ]
-
-         # FBX stage
-         if settings.comp_models:
-            if len(model_jobs) > 0:
-               static.JOBINFO += [{
-                  "title": "Batches",
-                  "w": 25,
-                  "colour": (0.5,0.5,1.0,1.0),
-                  "exec": cxr_export_modelsrc,
-                  "jobs": model_jobs
-               }]
-         
-            if len(qc_jobs) > 0:
-               static.JOBINFO += [{
-                  "title": "StudioMDL",
-                  "w": 20,
-                  "colour": (0.8,0.1,0.1,1.0),
-                  "exec": "studiomdl",
-                  "jobs": [[settings[F'exe_studiomdl']] + [\
-                        '-nop4', '-game', game, qc] for qc in qc_jobs],
-                  "cwd": asset_dir
-               }]
-
-         # VBSP stage
-         if settings.comp_compile:
+            if mat.cxr_data.shader == 'Builtin': continue
+            fp.write(F"{asset_path('materials',mat)}.vmt\n")
+            fp.write(F"{cxr_winepath(asset_full_path('materials',mat))}.vmt\n")
+
+         for img_job in image_jobs:
+            img = img_job[0]
+            fp.write(F"{asset_path('materials',img)}.vtf\n")
+            fp.write(F"{cxr_winepath(asset_full_path('materials',img))}.vmt\n")
+
+         for mdl in a_models:
+            local = asset_path('models',mdl)
+            winep = cxr_winepath(asset_full_path('models',mdl))
+
+            fp.write(F"{local}.vvd\n")
+            fp.write(F"{winep}.vvd\n")
+            fp.write(F"{local}.dx90.vtx\n")
+            fp.write(F"{winep}.dx90.vtx\n")
+            fp.write(F"{local}.mdl\n")
+            fp.write(F"{winep}.mdl\n")
+            fp.write(F"{local}.vvd\n")
+            fp.write(F"{winep}.vvd\n")
+      
+      # Convexer jobs
+      static.JOBID = 0
+      static.JOBINFO = []
+      
+      if settings.comp_vmf:
+         static.JOBINFO += [{ 
+            "title": "Convexer",
+            "w": 20,
+            "colour": (1.0,0.3,0.1,1.0),
+            "exec": cxr_export_vmf,
+            "jobs": [(sceneinfo,output_vmf)]
+         }]
+      
+      if settings.comp_textures:
+         if len(image_jobs) > 0:
             static.JOBINFO += [{
-               "title": "VBSP",
-               "w": 25,
-               "colour": (0.1,0.2,1.0,1.0),
-               "exec": "vbsp",
-               "jobs": [[settings[F'exe_vbsp']] + args],
-               "cwd": directory
+               "title": "Textures",
+               "w": 40,
+               "colour": (0.1,1.0,0.3,1.0),
+               "exec": compile_image,
+               "jobs": image_jobs
             }]
-            
+
+      game = cxr_winepath( settings.subdir )
+      args = [ \
+          '-game', game, settings.project_name
+      ]
+
+      # FBX stage
+      if settings.comp_models:
+         if len(model_jobs) > 0:
             static.JOBINFO += [{
-               "title": "VVIS",
+               "title": "Batches",
                "w": 25,
-               "colour": (0.9,0.5,0.5,1.0),
-               "exec": "vvis",
-               "jobs": [[settings[F'exe_vvis']] + ['-fast'] + args ],
-               "cwd": directory
+               "colour": (0.5,0.5,1.0,1.0),
+               "exec": cxr_export_modelsrc,
+               "jobs": model_jobs
             }]
-            
-            vrad_opt = settings.opt_vrad.split()
+      
+         if len(qc_jobs) > 0:
             static.JOBINFO += [{
-               "title": "VRAD",
-               "w": 25,
-               "colour": (0.9,0.2,0.3,1.0),
-               "exec": "vrad",
-               "jobs": [[settings[F'exe_vrad']] + vrad_opt + args ],
-               "cwd": directory
+               "title": "StudioMDL",
+               "w": 20,
+               "colour": (0.8,0.1,0.1,1.0),
+               "exec": "studiomdl",
+               "jobs": [[settings[F'exe_studiomdl']] + [\
+                     '-nop4', '-game', game, qc] for qc in qc_jobs],
+               "cwd": asset_dir
             }]
 
-            static.JOBINFO += [{
-               "title": "CXR",
-               "w": 5,
-               "colour": (0.0,1.0,0.4,1.0),
-               "exec": cxr_patchmap,
-               "jobs": [(F"{directory}/{settings.project_name}.bsp",\
-                         F"{settings.subdir}/maps/{settings.project_name}.bsp")]
-            }]
+      # VBSP stage
+      if settings.comp_compile:
+         static.JOBINFO += [{
+            "title": "VBSP",
+            "w": 25,
+            "colour": (0.1,0.2,1.0,1.0),
+            "exec": "vbsp",
+            "jobs": [[settings[F'exe_vbsp']] + args],
+            "cwd": directory
+         }]
+         
+         static.JOBINFO += [{
+            "title": "VVIS",
+            "w": 25,
+            "colour": (0.9,0.5,0.5,1.0),
+            "exec": "vvis",
+            "jobs": [[settings[F'exe_vvis']] + ['-fast'] + args ],
+            "cwd": directory
+         }]
+         
+         vrad_opt = settings.opt_vrad.split()
+         static.JOBINFO += [{
+            "title": "VRAD",
+            "w": 25,
+            "colour": (0.9,0.2,0.3,1.0),
+            "exec": "vrad",
+            "jobs": [[settings[F'exe_vrad']] + vrad_opt + args ],
+            "cwd": directory
+         }]
 
-         static.USER_EXIT=False
-         static.TIMER=wm.event_timer_add(0.1,window=context.window)
-         wm.modal_handler_add(_)
+         static.JOBINFO += [{
+            "title": "CXR",
+            "w": 5,
+            "colour": (0.0,1.0,0.4,1.0),
+            "exec": cxr_patchmap,
+            "jobs": [(bsp_local,bsp_remote)]
+         }]
 
-         cxr_jobs_update_graph( static.JOBINFO )
-         scene_redraw()
-         return {'RUNNING_MODAL'}
+      if settings.comp_pack:
+         static.JOBINFO += [{
+            "title": "Pack",
+            "w": 5,
+            "colour": (0.2,0.2,0.2,1.0),
+            "exec": "bspzip",
+            "jobs": [[cxr_compiler_path("bspzip"), '-addlist', \
+                  cxr_winepath(bsp_remote), 
+                  cxr_winepath(packlist),
+                  cxr_winepath(bsp_packed) ]],
+            "cwd": directory
+         }]
 
-      print("Chain exiting...")
-      static.USER_EXIT=True
+      if len(static.JOBINFO) == 0:
+         return {'CANCELLED'}
+
+      static.USER_EXIT=False
+      static.TIMER=wm.event_timer_add(0.1,window=context.window)
+      wm.modal_handler_add(_)
+
+      cxr_jobs_update_graph( static.JOBINFO )
+      scene_redraw()
       return {'RUNNING_MODAL'}
 
 class CXR_RESET_HASHES(bpy.types.Operator):
@@ -1927,6 +1984,7 @@ class CXR_INTERFACE(bpy.types.Panel):
       row.prop(settings,"comp_textures")
       row.prop(settings,"comp_models")
       row.prop(settings,"comp_compile")
+      row.prop(settings,"comp_pack")
       
       text = "Compile" if CXR_COMPILER_CHAIN.TIMER == None else "Cancel"
       row = box.row()
@@ -2242,6 +2300,7 @@ class CXR_SCENE_SETTINGS(bpy.types.PropertyGroup):
    comp_models: bpy.props.BoolProperty(name="Models",default=True)
    comp_textures: bpy.props.BoolProperty(name="Textures",default=True)
    comp_compile: bpy.props.BoolProperty(name="Compile",default=True)
+   comp_pack: bpy.props.BoolProperty(name="Pack",default=False)
 
 classes = [ CXR_RELOAD, CXR_DEV_OPERATOR, CXR_INTERFACE, \
             CXR_MATERIAL_PANEL, CXR_IMAGE_SETTINGS,\