building: Replace pickle with jsons/structs

This commit is contained in:
Little Cat 2021-07-22 01:42:56 -03:00
parent 622653d25e
commit 5f793922c7
No known key found for this signature in database
GPG key ID: 68CC2B4D0E5AF41C
5 changed files with 51 additions and 54 deletions

View file

@ -1603,9 +1603,15 @@ dclass DistributedBuilding : DistributedObject {
dclass DistributedAnimBuilding : DistributedBuilding { dclass DistributedAnimBuilding : DistributedBuilding {
}; };
struct savedBy {
uint32 avId;
string name;
blob dna;
}
dclass DistributedToonInterior : DistributedObject { dclass DistributedToonInterior : DistributedObject {
setZoneIdAndBlock(uint32, uint16) required broadcast ram; setZoneIdAndBlock(uint32, uint16) required broadcast ram;
setToonData(blob) required broadcast ram; setSavedBy(savedBy[]) required broadcast ram;
setState(string, int16) required broadcast ram; setState(string, int16) required broadcast ram;
}; };

View file

@ -56,7 +56,7 @@ class DistributedBuildingAI(DistributedObjectAI.DistributedObjectAI):
self.track = 'c' self.track = 'c'
self.difficulty = 1 self.difficulty = 1
self.numFloors = 0 self.numFloors = 0
self.savedBy = None self.savedBy = []
self.becameSuitTime = 0 self.becameSuitTime = 0
self.frontDoorPoint = None self.frontDoorPoint = None
self.suitPlannerExt = None self.suitPlannerExt = None
@ -89,9 +89,17 @@ class DistributedBuildingAI(DistributedObjectAI.DistributedObjectAI):
DistributedObjectAI.DistributedObjectAI.delete(self) DistributedObjectAI.DistributedObjectAI.delete(self)
del self.fsm del self.fsm
def getPickleData(self): def getBuildingData(self):
pickleData = {'state': str(self.fsm.getCurrentState().getName()), 'block': str(self.block), 'track': str(self.track), 'difficulty': str(self.difficulty), 'numFloors': str(self.numFloors), 'savedBy': self.savedBy, 'becameSuitTime': self.becameSuitTime} buildingData = {
return pickleData 'state': str(self.fsm.getCurrentState().getName()),
'block': str(self.block),
'track': str(self.track),
'difficulty': str(self.difficulty),
'numFloors': str(self.numFloors),
'savedBy': self.savedBy,
'becameSuitTime': self.becameSuitTime
}
return buildingData
def _getMinMaxFloors(self, difficulty): def _getMinMaxFloors(self, difficulty):
return SuitBuildingGlobals.SuitBuildingInfo[difficulty][0] return SuitBuildingGlobals.SuitBuildingInfo[difficulty][0]
@ -99,7 +107,7 @@ class DistributedBuildingAI(DistributedObjectAI.DistributedObjectAI):
def suitTakeOver(self, suitTrack, difficulty, buildingHeight): def suitTakeOver(self, suitTrack, difficulty, buildingHeight):
if not self.isToonBlock(): if not self.isToonBlock():
return return
self.updateSavedBy(None) self.updateSavedBy([])
difficulty = min(difficulty, len(SuitBuildingGlobals.SuitBuildingInfo) - 1) difficulty = min(difficulty, len(SuitBuildingGlobals.SuitBuildingInfo) - 1)
minFloors, maxFloors = self._getMinMaxFloors(difficulty) minFloors, maxFloors = self._getMinMaxFloors(difficulty)
if buildingHeight == None: if buildingHeight == None:
@ -118,7 +126,7 @@ class DistributedBuildingAI(DistributedObjectAI.DistributedObjectAI):
def cogdoTakeOver(self, suitTrack, difficulty, buildingHeight): def cogdoTakeOver(self, suitTrack, difficulty, buildingHeight):
if not self.isToonBlock(): if not self.isToonBlock():
return return
self.updateSavedBy(None) self.updateSavedBy([])
numFloors = self.FieldOfficeNumFloors numFloors = self.FieldOfficeNumFloors
self.track = suitTrack self.track = suitTrack
self.difficulty = difficulty self.difficulty = difficulty

View file

@ -1,6 +1,6 @@
import os import os
from direct.task.Task import Task from direct.task.Task import Task
import pickle import json
from otp.ai.AIBaseGlobal import * from otp.ai.AIBaseGlobal import *
from . import DistributedBuildingAI, HQBuildingAI, GagshopBuildingAI, PetshopBuildingAI from . import DistributedBuildingAI, HQBuildingAI, GagshopBuildingAI, PetshopBuildingAI
from toontown.building.KartShopBuildingAI import KartShopBuildingAI from toontown.building.KartShopBuildingAI import KartShopBuildingAI
@ -231,35 +231,18 @@ class DistributedBuildingMgrAI:
return building return building
def getFileName(self): def getFileName(self):
f = '%s%s_%d.buildings' % (self.serverDatafolder, self.shard, self.branchID) f = '%s%s_%d_buildings.json' % (self.serverDatafolder, self.shard, self.branchID)
return f return f
def saveTo(self, file, block=None): def saveTo(self, file):
if block: buildings = {}
pickleData = block.getPickleData()
pickle.dump(pickleData, file)
else:
for i in list(self.__buildings.values()): for i in list(self.__buildings.values()):
if isinstance(i, HQBuildingAI.HQBuildingAI): if isinstance(i, HQBuildingAI.HQBuildingAI):
continue continue
pickleData = i.getPickleData() buildingData = i.getBuildingData()
pickle.dump(pickleData, file) buildings[buildingData['block']] = buildingData
def fastSave(self, block): json.dump(buildings, file, indent=2)
return
try:
fileName = self.getFileName() + '.delta'
working = fileName + '.temp'
if os.path.exists(working):
os.remove(working)
os.rename(fileName, working)
file = open(working, 'wb')
file.seek(0, 2)
self.saveTo(file, block)
file.close()
os.rename(working, fileName)
except IOError:
self.notify.error(str(sys.exc_info()[1]))
def save(self): def save(self):
try: try:
@ -267,7 +250,7 @@ class DistributedBuildingMgrAI:
backup = fileName + self.backupExtension backup = fileName + self.backupExtension
if os.path.exists(fileName): if os.path.exists(fileName):
os.rename(fileName, backup) os.rename(fileName, backup)
file = open(fileName, 'wb') file = open(fileName, 'w')
file.seek(0) file.seek(0)
self.saveTo(file) self.saveTo(file)
file.close() file.close()
@ -278,25 +261,21 @@ class DistributedBuildingMgrAI:
def loadFrom(self, file): def loadFrom(self, file):
blocks = {} blocks = {}
try: buildingData = json.load(file)
while 1: for block in buildingData:
pickleData = pickle.load(file) blocks[int(block)] = buildingData[block]
blocks[int(pickleData['block'])] = pickleData
except EOFError:
pass
return blocks return blocks
def load(self): def load(self):
fileName = self.getFileName() fileName = self.getFileName()
try: try:
file = open(fileName + self.backupExtension, 'rb') file = open(fileName + self.backupExtension, 'r')
if os.path.exists(fileName): if os.path.exists(fileName):
os.remove(fileName) os.remove(fileName)
except IOError: except IOError:
try: try:
file = open(fileName, 'rb') file = open(fileName, 'r')
except IOError: except IOError:
return {} return {}

View file

@ -137,28 +137,26 @@ class DistributedToonInterior(DistributedObject.DistributedObject):
self.zoneId = zoneId self.zoneId = zoneId
self.block = block self.block = block
def setToonData(self, toonData): def setSavedBy(self, savedBy):
savedBy = pickle.loads(toonData)
self.savedBy = savedBy self.savedBy = savedBy
def buildTrophy(self): def buildTrophy(self):
if self.savedBy == None: if not self.savedBy:
return return
numToons = len(self.savedBy) numToons = len(self.savedBy)
pos = 1.25 - 1.25 * numToons pos = 1.25 - 1.25 * numToons
trophy = hidden.attachNewNode('trophy') trophy = hidden.attachNewNode('trophy')
for avId, name, dnaTuple, isGM in self.savedBy: for avId, name, dnaString in self.savedBy:
frame = self.buildFrame(name, dnaTuple) frame = self.buildFrame(name, dnaString)
frame.reparentTo(trophy) frame.reparentTo(trophy)
frame.setPos(pos, 0, 0) frame.setPos(pos, 0, 0)
pos += 2.5 pos += 2.5
return trophy return trophy
def buildFrame(self, name, dnaTuple): def buildFrame(self, name, dnaString):
frame = loader.loadModel('phase_3.5/models/modules/trophy_frame') frame = loader.loadModel('phase_3.5/models/modules/trophy_frame')
dna = ToonDNA.ToonDNA() dna = ToonDNA.ToonDNA(dnaString)
dna.newToonFromProperties(*dnaTuple)
head = ToonHead.ToonHead() head = ToonHead.ToonHead()
head.setupHead(dna) head.setupHead(dna)
head.setPosHprScale(0, -0.05, -0.05, 180, 0, 0, 0.55, 0.02, 0.55) head.setPosHprScale(0, -0.05, -0.05, 180, 0, 0, 0.55, 0.02, 0.55)

View file

@ -7,6 +7,7 @@ from direct.fsm import ClassicFSM, State
from direct.distributed import DistributedObjectAI from direct.distributed import DistributedObjectAI
from direct.fsm import State from direct.fsm import State
from toontown.toon import NPCToons from toontown.toon import NPCToons
from toontown.toon.ToonDNA import ToonDNA
class DistributedToonInteriorAI(DistributedObjectAI.DistributedObjectAI): class DistributedToonInteriorAI(DistributedObjectAI.DistributedObjectAI):
@ -38,8 +39,13 @@ class DistributedToonInteriorAI(DistributedObjectAI.DistributedObjectAI):
self.zoneId, self.block] self.zoneId, self.block]
return r return r
def getToonData(self): def getSavedBy(self):
return pickle.dumps(self.building.savedBy, 1) savedBy = []
for avId, name, dnaTuple in self.building.savedBy:
dna = ToonDNA()
dna.newToonFromProperties(*dnaTuple)
savedBy.append([avId, name, dna.makeNetString()])
return savedBy
def getState(self): def getState(self):
r = [ r = [