NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

shipSkinApplicationSvc.py (5578B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\cosmetics\client\shipSkinApplicationSvc.py
      2 import logging
      3 import uthread2
      4 from cosmetics.client.shipSkinLicensesSvc import get_ship_skin_license_svc
      5 from cosmetics.client.ships.ship_skin_signals import on_skin_state_reapplied
      6 from cosmetics.common.ships.skins.static_data.skin_type import ShipSkinType
      7 from eve.client.script.ui.shared.skins.defaultSkinSettings import get_default_skin_id_for_hull
      8 from eveexceptions import UserError
      9 logger = logging.getLogger(__name__)
     10 _BUFFER_DURATION = 1
     11 _instance = None
     12 
     13 def get_ship_skin_application_svc():
     14     global _instance
     15     if _instance is None:
     16         _instance = _ShipSkinApplicationController()
     17     return _instance
     18 
     19 
     20 class _ShipSkinApplicationController(object):
     21     __notifyevents__ = ['OnSessionChanged', 'OnSkinLicenseRemoved']
     22 
     23     def __init__(self):
     24         self._buffered_skin_changes = {}
     25         sm.RegisterNotify(self)
     26 
     27     def OnSessionChanged(self, _isRemote, _sess, change):
     28         if 'charid' in change:
     29             for ship_id in self._buffered_skin_changes.iterkeys():
     30                 self._cancel_buffered_skin_change(ship_id)
     31 
     32     def OnSkinLicenseRemoved(self, _skin_id, ship_id, type_id):
     33         self.apply_first_party_skin(ship_id, type_id, None)
     34 
     35     def apply_first_party_skin(self, ship_id, hull_type_id, skin):
     36         self._buffer_skin_change(ship_id, ShipSkinType.FIRST_PARTY_SKIN, [hull_type_id, skin], apply_skin=skin is not None)
     37 
     38     def apply_third_party_skin(self, ship_id, license_id, apply_license, activate_license = False):
     39         if apply_license and activate_license:
     40             get_ship_skin_license_svc().activate_license(license_id)
     41         self._buffer_skin_change(ship_id, ShipSkinType.THIRD_PARTY_SKIN, license_id, apply_license)
     42 
     43     def apply_default_skin(self, item_id, type_id):
     44         skin_id = get_default_skin_id_for_hull(hull_id=type_id)
     45         if skin_id is None:
     46             return
     47         try:
     48             if isinstance(skin_id, int):
     49                 skins = sm.GetService('cosmeticsSvc').GetSkins(hullTypeID=type_id)
     50                 target_skin = next((skin for skin in skins if skin.skinID == skin_id), None)
     51                 if target_skin is not None:
     52                     self.apply_first_party_skin(ship_id=item_id, hull_type_id=type_id, skin=target_skin)
     53             else:
     54                 activated_licenses = get_ship_skin_license_svc().get_my_activated_licenses(ship_type_id=type_id)
     55                 target_license = next((license for license in activated_licenses if license.skin_hex == skin_id), None)
     56                 if target_license is not None:
     57                     self.apply_third_party_skin(ship_id=item_id, license_id=skin_id, apply_license=True, activate_license=False)
     58         except Exception as e:
     59             logger.exception(e)
     60 
     61     def _buffer_skin_change(self, ship_id, skin_type, skin_data, apply_skin):
     62         self._cancel_buffered_skin_change(ship_id)
     63         if skin_type == ShipSkinType.FIRST_PARTY_SKIN:
     64             skin_id = skin_data[1].skinID if len(skin_data) == 2 and skin_data[1] is not None else None
     65             logger.info('SKIN APPLICATION - buffering 1P skin change for ship id %s, skin id %s' % (ship_id, skin_id))
     66         elif skin_type == ShipSkinType.THIRD_PARTY_SKIN:
     67             skin_id = skin_data
     68             logger.info('SKIN APPLICATION - buffering 3P skin change for ship id %s, license id %s' % (ship_id, skin_id))
     69         self._buffered_skin_changes[ship_id] = uthread2.StartTasklet(self._perform_skin_change, ship_id, skin_type, skin_data, apply_skin)
     70 
     71     def _cancel_buffered_skin_change(self, ship_id):
     72         current_change = self._buffered_skin_changes.get(ship_id, None)
     73         if current_change is not None:
     74             try:
     75                 current_change.kill()
     76                 logger.info('SKIN APPLICATION - cancelled buffered skin change for ship id %s' % ship_id)
     77             except Exception as e:
     78                 logger.exception('SKIN APPLICATION - error when cancelling buffered skin change: %s' % e)
     79             finally:
     80                 if ship_id in self._buffered_skin_changes:
     81                     self._buffered_skin_changes.pop(ship_id)
     82 
     83     def _perform_skin_change(self, ship_id, skin_type, skin_data, apply_skin):
     84         try:
     85             uthread2.SleepSim(_BUFFER_DURATION)
     86             if skin_type == ShipSkinType.FIRST_PARTY_SKIN:
     87                 ship_type_id = skin_data[0]
     88                 skin = skin_data[1]
     89                 skin_id = skin.skinID if skin else None
     90                 logger.info('SKIN APPLICATION - applying buffered 1P SKIN change for ship id %s, skin id %s' % (ship_id, skin_id))
     91                 sm.GetService('cosmeticsSvc').InternalApplySkin(ship_id, ship_type_id, skin if apply_skin else None)
     92             elif skin_type == ShipSkinType.THIRD_PARTY_SKIN:
     93                 license_id = skin_data
     94                 logger.info('SKIN APPLICATION - applying buffered 3P SKIN change for ship id %s, license id %s' % (ship_id, license_id))
     95                 get_ship_skin_license_svc().apply_internal(license_id, ship_id, apply_skin)
     96         except UserError as e:
     97             if e.msg == 'SkinAlreadyApplied':
     98                 on_skin_state_reapplied(ship_id)
     99             else:
    100                 raise e
    101         except Exception as e:
    102             logger.exception('SKIN APPLICATION - Error when performing buffered skin change: %s' % e)
    103             raise e
    104         finally:
    105             if ship_id in self._buffered_skin_changes:
    106                 self._buffered_skin_changes.pop(ship_id)