mirror of
https://github.com/LmeSzinc/StarRailCopilot.git
synced 2024-11-16 06:25:24 +00:00
Opt: Release resources during wait
This commit is contained in:
parent
848a9e2499
commit
73cf96b418
@ -124,10 +124,10 @@ class ButtonWrapper(Resource):
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.data_buttons = kwargs
|
self.data_buttons = kwargs
|
||||||
self._matched_button: t.Optional[Button] = None
|
self._matched_button: t.Optional[Button] = None
|
||||||
self.resource_add(self.name)
|
self.resource_add(f'{name}:{next(self.iter_buttons(), None)}')
|
||||||
|
|
||||||
def resource_release(self):
|
def resource_release(self):
|
||||||
del_cached_property(self, 'assets')
|
del_cached_property(self, 'buttons')
|
||||||
self._matched_button = None
|
self._matched_button = None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
import module.config.server as server
|
from module.base.decorator import cached_property
|
||||||
from module.base.decorator import cached_property, del_cached_property
|
|
||||||
|
|
||||||
|
|
||||||
def get_assets_from_file(file, regex):
|
def get_assets_from_file(file):
|
||||||
assets = set()
|
assets = set()
|
||||||
|
regex = re.compile(r"file='(.*?)'")
|
||||||
with open(file, 'r', encoding='utf-8') as f:
|
with open(file, 'r', encoding='utf-8') as f:
|
||||||
for row in f.readlines():
|
for row in f.readlines():
|
||||||
result = regex.search(row)
|
result = regex.search(row)
|
||||||
@ -20,11 +20,12 @@ class PreservedAssets:
|
|||||||
assets = set()
|
assets = set()
|
||||||
assets |= get_assets_from_file(
|
assets |= get_assets_from_file(
|
||||||
file='./tasks/base/assets/assets_base_page.py',
|
file='./tasks/base/assets/assets_base_page.py',
|
||||||
regex=re.compile(r'^([A-Za-z][A-Za-z0-9_]+) = ')
|
|
||||||
)
|
)
|
||||||
assets |= get_assets_from_file(
|
assets |= get_assets_from_file(
|
||||||
file='./tasks/base/assets/assets_base_popup.py',
|
file='./tasks/base/assets/assets_base_popup.py',
|
||||||
regex=re.compile(r'^([A-Za-z][A-Za-z0-9_]+) = ')
|
)
|
||||||
|
assets |= get_assets_from_file(
|
||||||
|
file='./tasks/base/assets/assets_base_main_page.py',
|
||||||
)
|
)
|
||||||
return assets
|
return assets
|
||||||
|
|
||||||
@ -44,11 +45,13 @@ class Resource:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_loaded(cls, obj):
|
def is_loaded(cls, obj):
|
||||||
if hasattr(obj, '_image') and obj._image is None:
|
if hasattr(obj, '_image') and obj._image is not None:
|
||||||
return False
|
return True
|
||||||
elif hasattr(obj, 'image') and obj.image is None:
|
if hasattr(obj, 'image') and obj.image is not None:
|
||||||
return False
|
return True
|
||||||
return True
|
if hasattr(obj, 'buttons') and obj.buttons is not None:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def resource_show(cls):
|
def resource_show(cls):
|
||||||
@ -56,11 +59,16 @@ class Resource:
|
|||||||
logger.hr('Show resource')
|
logger.hr('Show resource')
|
||||||
for key, obj in cls.instances.items():
|
for key, obj in cls.instances.items():
|
||||||
if cls.is_loaded(obj):
|
if cls.is_loaded(obj):
|
||||||
continue
|
logger.info(f'{obj}: {key}')
|
||||||
logger.info(f'{obj}: {key}')
|
|
||||||
|
|
||||||
|
|
||||||
def release_resources(next_task=''):
|
def release_resources(next_task=''):
|
||||||
|
# Release all OCR models
|
||||||
|
# det models take 400MB
|
||||||
|
if not next_task:
|
||||||
|
from module.ocr.models import OCR_MODEL
|
||||||
|
OCR_MODEL.resource_release()
|
||||||
|
|
||||||
# Release assets cache
|
# Release assets cache
|
||||||
# module.ui has about 80 assets and takes about 3MB
|
# module.ui has about 80 assets and takes about 3MB
|
||||||
# Alas has about 800 assets, but they are not all loaded.
|
# Alas has about 800 assets, but they are not all loaded.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from pponnxcr import TextSystem as TextSystem_
|
from pponnxcr import TextSystem as TextSystem_
|
||||||
|
|
||||||
from module.base.decorator import cached_property
|
from module.base.decorator import cached_property, del_cached_property
|
||||||
from module.exception import ScriptError
|
from module.exception import ScriptError
|
||||||
|
|
||||||
DIC_LANG_TO_MODEL = {
|
DIC_LANG_TO_MODEL = {
|
||||||
@ -56,6 +56,12 @@ class OcrModel:
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise ScriptError(f'OCR model under lang "{lang}" does not exists')
|
raise ScriptError(f'OCR model under lang "{lang}" does not exists')
|
||||||
|
|
||||||
|
def resource_release(self):
|
||||||
|
del_cached_property(self, 'zhs')
|
||||||
|
del_cached_property(self, 'en')
|
||||||
|
del_cached_property(self, 'ja')
|
||||||
|
del_cached_property(self, 'zht')
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def zhs(self):
|
def zhs(self):
|
||||||
return TextSystem('zhs')
|
return TextSystem('zhs')
|
||||||
@ -66,7 +72,7 @@ class OcrModel:
|
|||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def ja(self):
|
def ja(self):
|
||||||
return TextSystem('zht')
|
return TextSystem('ja')
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def zht(self):
|
def zht(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user