oldschool-toontown/toontown/chat/TTWhiteList.py

148 lines
6 KiB
Python
Raw Permalink Normal View History

2019-11-02 17:27:54 -05:00
import datetime
2022-01-19 21:45:39 -06:00
from panda3d.core import *
2019-11-02 17:27:54 -05:00
from direct.directnotify import DirectNotifyGlobal
2022-01-19 23:02:49 -06:00
from direct.showbase.DirectObject import DirectObject
2019-11-02 17:27:54 -05:00
from otp.chat.WhiteList import WhiteList
from toontown.toonbase import TTLocalizer
2022-01-19 23:02:49 -06:00
class TTWhiteList(WhiteList, DirectObject):
2019-11-02 17:27:54 -05:00
RedownloadTaskName = 'RedownloadWhitelistTask'
2022-01-19 21:45:39 -06:00
WhitelistBaseDir = ConfigVariableString('whitelist-base-dir', '').value
WhitelistStageDir = ConfigVariableString('whitelist-stage-dir', 'whitelist').value
WhitelistOverHttp = ConfigVariableBool('whitelist-over-http', False).value
2022-01-19 21:45:39 -06:00
WhitelistFileName = ConfigVariableString('whitelist-filename', 'twhitelist.dat').value
2019-11-02 17:27:54 -05:00
def __init__(self):
2022-01-19 23:02:49 -06:00
DirectObject.__init__(self)
2019-11-02 17:27:54 -05:00
self.redownloadingWhitelist = False
self.startRedownload = datetime.datetime.now()
self.endRedownload = datetime.datetime.now()
self.percentDownloaded = 0.0
self.notify = DirectNotifyGlobal.directNotify.newCategory('TTWhiteList')
vfs = VirtualFileSystem.getGlobalPtr()
filename = Filename('twhitelist.dat')
searchPath = DSearchPath()
2019-11-17 15:29:23 -06:00
if __debug__:
searchPath.appendDirectory(Filename('resources/phase_3/etc'))
2019-11-02 17:27:54 -05:00
found = vfs.resolveFilename(filename, searchPath)
if not found:
self.notify.info("Couldn't find whitelist data file!")
data = vfs.readFile(filename, 1)
2019-12-30 18:17:24 -06:00
lines = data.split(b'\n')
2019-11-02 17:27:54 -05:00
WhiteList.__init__(self, lines)
self.defaultWord = TTLocalizer.ChatGarblerDefault[0]
if self.WhitelistOverHttp:
self.redownloadWhitelist()
self.accept('updateWhitelist', self.handleNewWhitelist)
2019-11-02 17:27:54 -05:00
def unload(self):
2022-01-19 23:02:49 -06:00
if self.WhitelistOverHttp:
self.ignore('updateWhitelist')
self.removeDownloadingTextTask()
2019-11-02 17:27:54 -05:00
def redownloadWhitelist(self):
2022-01-19 23:02:49 -06:00
if not self.WhitelistOverHttp:
return
2019-11-02 17:27:54 -05:00
self.percentDownload = 0.0
self.notify.info('starting redownloadWhitelist')
self.startRedownload = datetime.datetime.now()
self.redownloadingWhitelist = True
self.addDownloadingTextTask()
self.whitelistUrl = self.getWhitelistUrl()
self.whitelistDir = Filename(self.findWhitelistDir())
Filename(self.whitelistDir + '/.').makeDir()
http = HTTPClient.getGlobalPtr()
self.url = self.whitelistUrl + self.WhitelistFileName
self.ch = http.makeChannel(True)
localFilename = Filename(self.whitelistDir, 'twhitelist.dat')
self.ch.getHeader(DocumentSpec(self.url))
size = self.ch.getFileSize()
doc = self.ch.getDocumentSpec()
localSize = localFilename.getFileSize()
outOfDate = True
if size == localSize:
if doc.hasDate():
date = doc.getDate()
localDate = HTTPDate(localFilename.getTimestamp())
if localDate.compareTo(date) > 0:
outOfDate = False
self.notify.info('Whitelist is up to date')
taskMgr.remove(self.RedownloadTaskName)
if outOfDate and self.ch.isValid():
self.ch.beginGetDocument(doc)
self.ch.downloadToFile(localFilename)
taskMgr.add(self.downloadWhitelistTask, self.RedownloadTaskName)
else:
self.updateWhitelist()
def getWhitelistUrl(self):
2022-01-19 21:45:39 -06:00
result = ConfigVariableString('fallback-whitelist-url', 'http://cdn.toontown.disney.go.com/toontown/en/').value
override = ConfigVariableString('whitelist-url', '').value
2019-11-02 17:27:54 -05:00
if override:
self.notify.info('got an override url, using %s for the whitelist' % override)
result = override
else:
try:
launcherUrl = base.launcher.getValue('GAME_WHITELIST_URL', '')
if launcherUrl:
result = launcherUrl
self.notify.info('got GAME_WHITELIST_URL from launcher using %s' % result)
else:
self.notify.info('blank GAME_WHITELIST_URL from launcher, using %s' % result)
except:
self.notify.warning('got exception getting GAME_WHITELIST_URL from launcher, using %s' % result)
return result
def addDownloadingTextTask(self):
self.removeDownloadingTextTask()
task = taskMgr.doMethodLater(1, self.loadingTextTask, 'WhitelistDownloadingTextTask')
task.startTime = globalClock.getFrameTime()
self.loadingTextTask(task)
def removeDownloadingTextTask(self):
taskMgr.remove('WhitelistDownloadingTextTask')
def loadingTextTask(self, task):
timeIndex = int(globalClock.getFrameTime() - task.startTime) % 3
timeStrs = (TTLocalizer.NewsPageDownloadingNews0, TTLocalizer.NewsPageDownloadingNews1, TTLocalizer.NewsPageDownloadingNews2)
textToDisplay = timeStrs[timeIndex] % int(self.percentDownloaded * 100)
return task.again
def findWhitelistDir(self):
if self.WhitelistOverHttp:
return self.WhitelistStageDir
return None
2019-11-02 17:27:54 -05:00
def downloadWhitelistTask(self, task):
if self.ch.run():
return task.cont
doc = self.ch.getDocumentSpec()
date = ''
if doc.hasDate():
date = doc.getDate().getString()
if not self.ch.isValid():
self.notify.warning('Unable to download %s' % self.url)
self.redownloadingWhitelist = False
return task.done
self.notify.info('Done downloading whitelist file')
self.updateWhitelist()
return task.done
def updateWhitelist(self):
localFilename = Filename(self.whitelistDir, 'twhitelist.dat')
if not localFilename.exists():
return
data = vfs.readFile(localFilename, 1)
2019-12-30 18:17:24 -06:00
lines = data.split(b'\n')
2019-11-02 17:27:54 -05:00
self.words = []
for line in lines:
2019-12-30 18:17:24 -06:00
self.words.append(line.strip(b'\n\r').lower())
2019-11-02 17:27:54 -05:00
self.words.sort()
self.numWords = len(self.words)
self.defaultWord = TTLocalizer.ChatGarblerDefault[0]
def handleNewWhitelist(self):
self.redownloadWhitelist()