Added compatibility checking when loading a save game - based on hashing the global structure offset tables.

This commit is contained in:
Martin Haye 2016-08-05 09:27:11 -07:00
parent 8f2a5ada3d
commit 3d06158a04
3 changed files with 32 additions and 28 deletions

View File

@ -232,13 +232,11 @@ def loadInternal()
guaranteeMLI(MLI_CLOSE, @close_params)
fin
// Init the heap with the correct size
// Copy the heap up, and init it with the correct size.
p_loaded = $4000
copyHeap(1) // main to aux
initHeap(p_loaded=>w_heapSize)
global = getGlobals()
// Now copy the data back up to the heap space, and we're done
copyHeap(1) // main to aux
return TRUE
end

View File

@ -450,33 +450,27 @@ asm getXReg
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate 16-bit hash of a string
asm hashString
+asmPlasm 1
// Calculate 16-bit hash of a buffer. arg1=pointer, arg2=length
export asm hashBuffer
+asmPlasm 2
lda evalStkL+1,x ; first arg is buffer pointer
sta pTmp
sty pTmp+1
lda evalStkH+1,x
sta pTmp+1
lda evalStkL,x ; second arg is length
tax
ldy #0
sty tmp+1
lda (pTmp),y
tax
tya
clc
- adc (pTmp),y
ror
ror tmp+1
ror
ror tmp+1
ror
ror tmp+1
iny
- clc
adc (pTmp),y
bcc +
inc tmp+1
+ asl
rol tmp+1
bcc +
ora #1
+ asl
rol tmp+1
bcc +
ora #1
+ asl
rol tmp+1
bcc +
ora #1
+ iny
dex
bne -
ldy tmp+1
@ -1575,6 +1569,11 @@ def kbdLoop()
loop
end
///////////////////////////////////////////////////////////////////////////////////////////////////
def hashString(str)
return hashBuffer(str+1, ^str)
end
///////////////////////////////////////////////////////////////////////////////////////////////////
export def showMapName(mapName)
word newNameHash
@ -2259,6 +2258,7 @@ end
// Set up the small-object heap. Set loadedSize to zero on initial, or non-zero for loaded game.
export def initHeap(loadedSize)
byte i
word typeHash
if !heapLocked
mmgr(SET_MEM_TARGET, HEAP_BOTTOM)
@ -2276,10 +2276,15 @@ export def initHeap(loadedSize)
mmgr(HEAP_ADD_TYPE, typeTbls[i])
i = i+1
loop
typeHash = hashBuffer(@typeTbl_Global, @typeTbls - @typeTbl_Global)
if loadedSize <> 0
global = HEAP_BOTTOM
if global=>w_typeHash <> typeHash
fatal("Incompatible saved game")
fin
else
global = mmgr(HEAP_ALLOC, TYPE_GLOBAL)
global=>w_typeHash = typeHash
fin
end

View File

@ -30,8 +30,9 @@ struc Global
// Shared player gold amount
word w_gold
// Heap size for restoring saved game
// Heap size for restoring saved game, and hash of type table to check compatibility of old saves.
word w_heapSize
word w_typeHash
// General flags maintained by scripts. Linked list of Modifiers.
word p_gameFlags