refactor key value parsers into parse.common, use for global prefs, versions, and options

This commit is contained in:
4am 2018-03-28 10:42:37 -04:00
parent e555e5b901
commit 5e15db31ae
5 changed files with 140 additions and 118 deletions

View File

@ -1 +1 @@
# Pitch Dark preferences file # Do not edit by hand. # Or do. I'm a comment, not a cop. # value=0|1 FORCE40COLUMNS=0 FORCEUPPERCASE=0 SCRIPTTOFILE=0 AUTOSCRIPT=0 # value=game directory LASTPLAYED=ZORK.I # key=game directory, value=filename of selected version ADVENTURE=R9.060321.Z5 AMFV=R79.DEPROT.Z4 BALLYHOO=R97.851218.Z3 BEYOND.ZORK=R57.871221.Z5 BORDER.ZONE=R9.871008.Z5 BUREAUCRACY=R116.DEPROT.Z4 CUTTHROATS=R23.840809.Z3 DEADLINE=R27.831005.Z3 ENCHANTER=R29.860820.Z3 HGTTG=R59.851108.Z3 HOLLYWOOD=R37.861215.Z3 INFIDEL=R22.830916.Z3 LGOP=R59.860730.Z3 LURKING.HORROR=R221.870918.Z3 MINI.ZORK=R34.871124.Z3 MOONMIST=R9.861022.Z3 NORD.AND.BERT=R19.870722.Z4 PLANETFALL=R37.851003.Z3 PLUNDERED=R26.870730.Z3 SEASTALKER=R16B.850603.Z3 SHERLOCK=R26.880127.Z5 SORCERER=R18.DEPROT.Z3 SPELLBREAKER=R87.DEPROT.Z3 STARCROSS=R17.821021.Z3 STATIONFALL=R107.DEPROT.Z3 SUSPECT=R14.841005.Z3 SUSPENDED=R8B.840521.Z3 TRINITY=R12.860926.Z4 WISHBRINGER=R69.850920.Z3 WITNESS=R22.840924.Z3 ZORK.I=R88.840726.Z3 ZORK.II=R48.840904.Z3 ZORK.III=R17.840727.Z3 ZTUU=R16.970828.Z5 =EOF
# Pitch Dark preferences file # Do not edit by hand. # Or do. I'm a comment, not a cop. # value=0|1 FORCE40COLUMNS=0 FORCEUPPERCASE=0 SCRIPTTOFILE=0 AUTOSCRIPT=0 # value=game directory LASTPLAYED=ZORK.I # key=game directory, value=filename of selected version ADVENTURE=R9.060321.Z5 AMFV=R79.DEPROT.Z4 BALLYHOO=R97.851218.Z3 BEYOND.ZORK=R57.871221.Z5 BORDER.ZONE=R9.871008.Z5 BUREAUCRACY=R116.DEPROT.Z4 CUTTHROATS=R23.840809.Z3 DEADLINE=R27.831005.Z3 ENCHANTER=R29.860820.Z3 HGTTG=R59.851108.Z3 HOLLYWOOD=R37.861215.Z3 INFIDEL=R22.830916.Z3 LGOP=R59.860730.Z3 LURKING.HORROR=R221.870918.Z3 MINI.ZORK=R34.871124.Z3 MOONMIST=R9.861022.Z3 NORD.AND.BERT=R19.870722.Z4 PLANETFALL=R37.851003.Z3 PLUNDERED=R26.870730.Z3 SEASTALKER=R16B.850603.Z3 SHERLOCK=R26.880127.Z5 SORCERER=R18.DEPROT.Z3 SPELLBREAKER=R87.DEPROT.Z3 STARCROSS=R17.821021.Z3 STATIONFALL=R107.DEPROT.Z3 SUSPECT=R14.841005.Z3 SUSPENDED=R8B.840521.Z3 TRINITY=R12.860926.Z4 WISHBRINGER=R69.850920.Z3 WITNESS=R22.840924.Z3 ZORK.I=R88.840726.Z3 ZORK.II=R48.840904.Z3 ZORK.III=R17.840727.Z3 ZTUU=R16.970828.Z5 [eof]

View File

@ -1,11 +1,123 @@
gKeyLen = $3F00
gKey = $3F01
gValLen = $3F10
gVal = $3F11
gValLen = $3F80
gVal = $3F81
;------------------------------------------------------------------------------
; ParseKeyValueText
; parse buffer with KEY=VALUE lines of text into an okvs
; keys and values limited to 127 characters, which should be enough for anyone
;
; in: stack contains 5 bytes of parameters:
; +1 [word] handle to storage space
; +3 [word] handle to data buffer
; +5 [byte] max length for okvs records (or 0)
; out: all registers and flags clobbered
; $3F00..$3FFF clobbered
; $00/$01 clobbered
; $02/$03 clobbered
; $04/$05 has the address of the next available byte after the okvs
;------------------------------------------------------------------------------
!zone {
ParseKeyValueText
+PARAMS_ON_STACK 5
ldy #1
lda (PARAM),y
sta .initStore
sta .appendStore
iny
lda (PARAM),y
sta .initStore+1
sta .appendStore+1
iny
lda (PARAM),y
sta $FE
iny
lda (PARAM),y
sta $FF
iny
lda (PARAM),y
sta .useMaxLength+1
lda ($FE)
pha
ldy #1
lda ($FE),y
tay
pla
sec
sbc #$01
sta $FE
bcs +
dey
+ sty $FF
jsr okvs_init ; reset key/value store
.initStore
!word $FDFD ; set at runtime
ldy #$00 ; index into ($FE) pointing to current character
.newKey
ldx #$00 ; X = index into current key
.gatherKey
jsr IncAndGetChar
and #$7F ; keys get their high bit stripped
cmp #$23 ; '#' is starts a comment (until CR)
beq .skipComment
cmp #$3D ; '=' ends the key
beq .endKey
cmp #$0D ; found CR before '=', ignore key and start over (also handles blank lines)
beq .newKey
cmp #$5B ; '[' ends the parsing
beq .exit
sta gKey,x
inx
bpl .gatherKey
.endKey
stx gKeyLen
ldx #$00 ; now X = index into the current value
.gatherValue
jsr IncAndGetChar ; note: values do NOT get their high bit stripped
cmp #$0D ; CR ends the value
beq .endValue
cmp #$00 ; null also ends the value
beq .endValue
sta gVal,x
inx
bpl .gatherValue
.endValue
stx gValLen
cpx #1
bne .useMaxLength
lda gVal
and #1
sta gVal ; single-character values get converted to #$00 or #$01 in prefs store
lda #0
!byte $2C
.useMaxLength
lda #$FD ; set at runtime
sta .appendMaxLength ; all other values get upgraded to max_length so we can update them in place
phy ; okvs functions clobber everything but we need Y
jsr okvs_append
.appendStore
!word $FDFD ; set at runtime
!word gKeyLen
!word gValLen
.appendMaxLength
!byte $FD ; set at runtime
ply
bra .newKey
.skipComment ; skip to CR
jsr IncAndGetChar
cmp #$0D ; CR
bne .skipComment
bra .newKey
IncAndGetChar
iny
bne +
inc $FF
+ lda ($FE),y
rts
.exit rts
}

View File

@ -14,6 +14,8 @@ kTextRootDirectory
gVersionsStore
!word $FDFD ; set at runtime in LoadGlobalPreferences
gOptionsStore
!word $FDFD ; set at runtime in LoadGameInfo
sectionPointers ; array of pointers to start of each section
addrEOF
@ -103,57 +105,20 @@ LoadGameInfo
dex
bpl .convertSectionNoInc
jmp LoadVersions
;jmp LoadGamePreferences
}
!zone {
LoadVersions
jsr okvs_init
jsr ParseKeyValueText ; parse [versions] section into versions store
!word gVersionsStore
lda addrVersions+1
sta $FF
lda addrVersions
sec
sbc #$01
sta $FE
bcs +
dec $FF
+ ldy #$00 ; index into ($FE) pointing to current character
.newKey ldx #$00 ; X = index into current key
.gatherKey
stx gKeyLen
jsr IncAndGetChar
and #$7F
cmp #$3D ; '=' ends the key
beq .endKey
cmp #$5B ; '[' ends the parsing
beq .doneParsing
sta gKey,x
inx
bra .gatherKey
.endKey stx gKeyLen
ldx #$00 ; X = index into the current value
.gatherValue
stx gValLen
jsr IncAndGetChar
cmp #$00 ; null ends the value
beq .endValue
and #$7F
sta gVal,x
inx
bra .gatherValue
.endValue
stx gValLen
phy ; okvs functions clobber everything but we need Y
jsr okvs_append
!word gVersionsStore
!word gKeyLen
!word gValLen
!word addrVersions
!byte 0
ply
bra .newKey
.doneParsing
lda SRC ; save pointer to free space for next store
sta gOptionsStore
lda SRC+1
sta gOptionsStore+1
jsr ParseKeyValueText ; parse [options] section into options store
!word gOptionsStore
!word addrOptions
!byte 0
rts
}

View File

@ -53,78 +53,24 @@ kLastPlayed
LoadGlobalPreferences
stz gNeedToSavePrefs
jsr okvs_init
!word gGlobalPrefsStore
jsr LoadFile ; load prefs file at $2000
!word .globalPrefsFilename
.prefsAddress
!word $2000
!word $2000
!word kProDOSFileBuffer
lda #$FF
sta $FE
lda #$1F
sta $FF ; ($FE) points to start of data buffer
ldy #$00 ; index into ($FE) pointing to current character
.newKey ldx #$00 ; X = index into current key
.gatherKey
stx gKeyLen
jsr IncAndGetChar
cmp #$23 ; '#' is starts a comment (until CR)
beq .skipComment
cmp #$3D ; '=' ends the key
beq .endKey
cmp #$0D ; found CR before '=', ignore key and start over
beq .newKey
sta gKey,x
inx
bra .gatherKey
.endKey stx gKeyLen
txa ; to set Z flag
beq .doneParsing ; empty key ends the parsing
ldx #$00 ; X = index into the current value
.gatherValue
stx gValLen
jsr IncAndGetChar
cmp #$0D ; CR ends the value
beq .endValue
sta gVal,x
inx
bra .gatherValue
.endValue
stx gValLen
lda gValLen
cmp #1
bne .use16
lda gVal
and #1
sta gVal ; single-character values get converted to #$00 or #$01 in prefs store
lda #0
!byte $2C
.use16 lda #16
sta .okvslen ; all other values get upgraded to 16 bytes so we can update them in place
phy ; okvs functions clobber everything but we need Y
jsr okvs_append
jsr ParseKeyValueText ; parse contents into global prefs store
!word gGlobalPrefsStore
!word gKeyLen
!word gValLen
.okvslen !byte $FD ; set at runtime
ply
bra .newKey
!word .prefsAddress
!byte 16
.skipComment ; skip to CR
jsr IncAndGetChar
cmp #$0D ; CR
bne .skipComment
bra .newKey
.doneParsing
lda SRC
lda SRC ; save pointer to free space for next store
sta gVersionsStore
lda SRC+1
sta gVersionsStore+1
jsr okvs_get
jsr okvs_get ; set gCurrentGame
!word gGlobalPrefsStore
!word kLastPlayed
sta DEST
@ -308,7 +254,7 @@ addString
.fluff3 !byte 58
!byte $0D
!raw "# key=game directory, value=filename of selected version",$0D
.eof !byte 6
.eof !byte 7
!byte $0D
!raw "=EOF",$0D
!raw "[eof]",$0D
}

View File

@ -185,7 +185,6 @@ paintInfoView
lda (SRC)
tay
- lda (SRC),y
ora #$80
sta $3F00,y
dey
bne -