browserImageSvc.py (6316B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\browser\browserImageSvc.py 2 import os 3 import sys 4 import blue 5 import trinity 6 import uthread 7 from carbon.common.script.sys.service import Service 8 from carbonui.control.browser import browserutil 9 from carbonui.util.various_unsorted import GetBuffersize 10 11 class BrowserImage(Service): 12 __guid__ = 'svc.browserImage' 13 __exportedcalls__ = {} 14 __startupdependencies__ = ['imageCache'] 15 16 def __init__(self): 17 self._urlloading = {} 18 super(BrowserImage, self).__init__() 19 20 def GetTextureInfoFromURL(self, path, currentURL = None, fromWhere = None): 21 if path.endswith('.blue'): 22 return self._GetPic_blue(path) 23 fullPath = browserutil.ParseURL(path, currentURL)[0] 24 cacheData = self.imageCache.GetFromCache(fullPath) 25 if not cacheData: 26 cacheData = self.GetTextureFromURL(path, currentURL, fromWhere=fromWhere, sizeonly=1) 27 if cacheData and os.path.exists(cacheData[0].replace('cache:/', blue.paths.ResolvePath(u'cache:/'))): 28 return cacheData 29 30 def GetTextureFromURL(self, path, currentURL = None, ignoreCache = 0, dontcache = 0, fromWhere = None, sizeonly = 0, retry = 1): 31 if path.endswith('.blue'): 32 return self._GetPic_blue(path) 33 dev = trinity.device 34 fullPath = browserutil.ParseURL(path, currentURL)[0] 35 if path.startswith('res:'): 36 try: 37 bmp = trinity.Tr2HostBitmap() 38 bmp.CreateFromFile(path) 39 w, h = bmp.width, bmp.height 40 bw, bh = GetBuffersize(w), GetBuffersize(h) 41 if sizeonly: 42 return (path, 43 w, 44 h, 45 bw, 46 bh) 47 return self._ReturnTexture(path, w, h, bw, bh) 48 except: 49 self.LogError('Failed to load image', path) 50 if fullPath in self._urlloading: 51 del self.urlloading[fullPath] 52 sys.exc_clear() 53 return self._ErrorPic(sizeonly) 54 55 if ignoreCache: 56 self.imageCache.InvalidateImage(fullPath) 57 while fullPath in self._urlloading: 58 blue.pyos.BeNice() 59 60 if not dontcache: 61 cacheData = self.imageCache.GetFromCache(fullPath) 62 if cacheData and os.path.exists(cacheData[0].replace('cache:/', blue.paths.ResolvePath(u'cache:/'))): 63 if sizeonly: 64 return cacheData 65 return self._ReturnTexture(*cacheData) 66 try: 67 self._urlloading[fullPath] = 1 68 ret = browserutil.GetStringFromURL(fullPath) 69 cacheID = int(str(blue.os.GetWallclockTime()) + str(uthread.uniqueId() or uthread.uniqueId())) 70 imagestream = ret.read() 71 ext = None 72 if 'content-type' in ret.headers.keys() and ret.headers['content-type'].startswith('image/'): 73 ext = ret.headers['content-type'][6:] 74 if ext == None or ext == 'png': 75 header = imagestream[:16] 76 for sig, sext in [('PNG', 'PNG'), ('JFI', 'JPEG'), ('BM8', 'BMP')]: 77 for i in xrange(0, 12): 78 if header[i:i + 3] == sig: 79 ext = sext 80 break 81 82 if not ext: 83 header = imagestream[-16:] 84 for sig, sext in [('XFILE', 'TGA')]: 85 for i in xrange(0, 10): 86 if header[i:i + 5] == sig: 87 ext = sext 88 break 89 90 if ext: 91 filename = '%sBrowser/Img/%s.%s' % (blue.paths.ResolvePath(u'cache:/'), cacheID, ext) 92 resfile = blue.classes.CreateInstance('blue.ResFile') 93 if not resfile.Open(filename, 0): 94 resfile.Create(filename) 95 resfile.Write(imagestream) 96 resfile.Close() 97 bmp = trinity.Tr2HostBitmap() 98 bmp.CreateFromFile(filename) 99 w, h = bmp.width, bmp.height 100 bw, bh = GetBuffersize(w), GetBuffersize(h) 101 cachePath = 'cache:/Browser/Img/%s.%s' % (cacheID, ext) 102 if 'pragma' not in ret.headers.keys() or ret.headers['Pragma'].find('no-cache') == -1: 103 self.imageCache.Cache(fullPath, (cachePath, 104 w, 105 h, 106 bw, 107 bh)) 108 del self._urlloading[fullPath] 109 if sizeonly: 110 return (cachePath, 111 w, 112 h, 113 bw, 114 bh) 115 return self._ReturnTexture(cachePath, w, h, bw, bh) 116 del self._urlloading[fullPath] 117 return self._ErrorPic(sizeonly) 118 except Exception as e: 119 if retry: 120 sys.exc_clear() 121 if fullPath in self._urlloading: 122 del self._urlloading[fullPath] 123 return self.GetTextureFromURL(path, currentURL, ignoreCache, dontcache, fromWhere, sizeonly, 0) 124 self.LogError(type(e), 'on line', sys.exc_traceback.tb_lineno, 'in browserImage service') 125 self.LogError(e, 'Failed to load image', path) 126 if fullPath in self._urlloading: 127 del self._urlloading[fullPath] 128 sys.exc_clear() 129 return self._ErrorPic(sizeonly) 130 131 def _ErrorPic(self, sizeonly = 0): 132 if sizeonly: 133 return ('res:/uicore/texture/none.dds', 32, 32, 32, 32) 134 return self._ReturnTexture('res:/uicore/texture/none.dds', 32, 32, 32, 32) 135 136 def _ReturnTexture(self, path, width, height, bufferwidth, bufferheight): 137 tex = trinity.Tr2Sprite2dTexture() 138 tex.resPath = str(path) 139 return (tex, width, height) 140 141 def _GetPic_blue(self, path): 142 tex = blue.resMan.LoadObject(path) 143 w = int(tex.pixelBuffer.width * (tex.scaling.x / (tex.texCoordIndex * 2 or 1))) or 32 144 h = int(tex.pixelBuffer.height * (tex.scaling.y / (tex.texCoordIndex * 2 or 1))) or 32 145 return (tex, w, h)