
	SECTION	MyCode,CODE

	incdir	include:
	include	exec/execbase.i
	include	exec/types.i
	include	exec/memory.i
	include	dos/dosextens.i
	include	libraries/dos.i
	include	lvo/exec.i
	include	lvo/dos.i
	include	lvo/graphics.i

CALL	MACRO	;FunctionName
	jsr	_LVO\1(a6)
	ENDM

Start:
	move.l	4,a6
	move.l	a6,SysBase
	move.l	ThisTask(a6),a3
	tst.l	pr_CLI(a3)
	bne	.CLIStart
 
	lea	pr_MsgPort(a3),a0
	CALL	WaitPort
	lea	pr_MsgPort(a3),a0
	CALL	GetMsg
	move.l	d0,Message
.CLIStart:

	lea	DosName,a1		; library name
	CALL	OldOpenLibrary
	move.l	d0,DosBase
	beq	no_DosBase
	lea	GfxName,a1		; library name
	CALL	OldOpenLibrary
	move.l	d0,GfxBase
	beq	no_GfxBase

	pea	StartupTEXT(pc)
	jsr	TextAusgabe
	addq.w	#4,sp

	CALL	Forbid

	lea	_PortName,A1
	CALL	FindPort
	move.l	d0,a5
	tst.l	d0
	beq	.install	

	tst.b	_ActiveFlag-PatchBlockStart(a5)
	beq	.shortinstall

	move.l	GfxBase,a1
	move.l	#_LVODraw,a0
	move.l	_DrawSystem-PatchBlockStart(a5),d0
	CALL	SetFunction

	lea	_DrawNew-PatchBlockStart(a5),a0
	cmp.l	a0,d0
	beq	.remove_success

; was not our patch, so reinstall it and simply disable ours
	move.l	GfxBase,a1
	move.l	#_LVODraw,a0
	CALL	SetFunction

	bra	.not_removable

.remove_success:
	move.l	a5,a1
	CALL	RemPort

	move.l	a5,a1
	move.l	_PatchSize-PatchBlockStart(a5),d0
	CALL	FreeMem

	bra	.removed

.not_removable:

	sf	_ActiveFlag-PatchBlockStart(a5)

.removed:
.MemError:
	move.l	SysBase,a6
	CALL	Permit

	pea	RemovedTEXT(pc)
	jsr	TextAusgabe
	addq.w	#4,SP
	bra	Exit

.install:
	lea	LibList(a6),a0
	lea	rtgName(pc),a1
	CALL	FindName
	bne	.found_p96

	pea	p96TEXT(pc)
	jsr	TextAusgabe
	addq.w	#4,SP
	bra	Exit

.found_p96:
	move.l	#PB_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	CALL	AllocMem
	tst.l	d0
	beq	.MemError
	move.l	d0,a1
	move.l	d0,a5
	lea	PatchBlockStart,a0
	move.l	#PB_SIZE,d0
	move.l	d2,_PatchSize-PatchBlockStart(a5)
	CALL	CopyMem
	
	lea	_PubPort-PatchBlockStart(a5),a1
	lea	_PortName-PatchBlockStart(a5),a0
	move.l	a0,LN_NAME(a1)
	move.b	#$80,LN_PRI(a1)
	move.b	#NT_MSGPORT,LN_TYPE(a1)
	move.b	#PA_IGNORE,MP_FLAGS(a1)
	CALL	AddPort
	
	lea	_DrawNew-PatchBlockStart(a5),a0
	move.l	a0,d0
	move.l	GfxBase,a1
	move.l	#_LVODraw,a0
	CALL	SetFunction
	move.l	d0,_DrawSystem-PatchBlockStart(a5)

.shortinstall
	st	_ActiveFlag-PatchBlockStart(a5)

	CALL	Permit

	pea	InstalledTEXT(pc)
	jsr	TextAusgabe
	addq.w	#4,sp

Exit
	move.l	SysBase,a6
	move.l	GfxBase(pc),a1
	CALL	CloseLibrary
no_GfxBase:
	move.l	DosBase(pc),a1
	CALL	CloseLibrary
no_DosBase:

	move.l	Message,d0
	beq	.ThatsIt
	CALL	Forbid
	move.l	Message,A1
	CALL	ReplyMsg
.ThatsIt:
	moveq	#0,d0
	rts				; that's it...

TextAusgabe:
	movem.l	a0/a1/a6/d0-d3,-(sp)
	move.l	Message,d0
	bne	.KeineAusgabe
	move.l	DosBase(pc),a6
	CALL	Output
	move.l	d0,d1
	move.l	32(sp),a0
	move.l	a0,d2
.loop:
	tst.b	(a0)+
	bne	.loop
	move.l	a0,d3
	sub.l	d2,d3
	CALL	Write
.KeineAusgabe:
	movem.l	(sp)+,a0/a1/a6/d0-d3
	rts

GfxName:	dc.b	'graphics.library',0
DosName:	dc.b	'dos.library',0
rtgName:	dc.b	'rtg.library',0

		dc.b	'$VER: '
StartupTEXT:	dc.b	$1B,'[1;33mSaveDrawA1 V1.1',$1B
		dc.b	'[0m Copyright ',169,' 1999 by A.Kneer & T.Abt',10,0
p96TEXT:	dc.b	'Sorry, this needs to be run after Picasso96.',10,0
RemovedTEXT:	dc.b	'A1 save hook removed.',10,0
InstalledTEXT:	dc.b	'A1 save hook installed.',10,0
	even

Message:	dc.l	0
SysBase:	dc.l	0
GfxBase:	dc.l	0
DosBase:	dc.l	0

*-------------------------------------------------------------------------------

PatchBlockStart:
_PubPort:
		dcb.b	MP_SIZE,0
_PortName:	dc.b	'SaveDrawA1',0
_ActiveFlag:	dc.b	0
	even
_PatchSize:	dc.l	PB_SIZE	

*--------------------------
_DrawSystem:		dc.l	0
*--------------------------
_DrawNew:
; Draw() does not need a0 as parameter, but trashes it, so noone will depend on its value
	lea	_ActiveFlag(pc),a0
	tst.b	(a0)
	beq	.inactive
; ok, save a1
	move.l	a1,-(sp)
; push return address for the jsr fake
	pea	.cont
; now fake the jump of the jsr
.inactive:
	move.l	_DrawSystem(pc),-(sp)
	rts
.cont:
; fine, restore a1 and return
	move.l	(sp)+,a1
	rts

PB_SIZE	equ	*-PatchBlockStart
*-------------------------------------------------------------------------------

	END
