;TOSAAAABCBOAAAABLHCPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPNCHH

	incdir	"include:"
	include	"exec/types.i"
	include	"exec/ports.i"
	include	"exec/execbase.i"
	include	"exec/memory.i"
	include	"workbench/workbench.i"
	include	"libs/dos.i"
	include	"libs/exec.i"
	include	"dos/dos.i"
	incdir	"ups:"
	include	"ups.i"



Start	moveq	#0,d0
	rts
.end	blk.b	32-(.end-Start)
	dc.l	pfsControlLong
	dc.l	0			; pointer to next patcher
	dc.l	0			; pointer to prev patcher
	dc.l	UPSMainVersion		; minimum version of UPS to start
	dc.l	UPSMainVersion		; version of UPS which was used
					; while patcher was developed
	dc.l	PatcherName		; pointer to name of patcher
	dc.w	0,0			; version
	dc.l	CoderName		; pointer to name of programmer
	dc.l	VerString		; pointer to $VER: string
	dc.l	PatcherIDString		; listview entry
	dc.l	pfsDisableable		; flags
	dc.l	0			; status of flags
execbase
	dc.l	0			; exec base
intbase	dc.l	0			; intuition base
gadtbase
	dc.l	0			; gadtools base
dosbase	dc.l	0			; dos base
gfxbase	dc.l	0			; graphics base
cxbase	dc.l	0			; commodities base
laybase	dc.l	0			; layers base
upsbase	dc.l	0			; UPS lib base
	dc.l	init			; pointer to init routine
	dc.l	0			; pointer to exit routine
	dc.l	0			; pointer to prefs routine
	dc.l	0			; pointer to cx keys to call prefs rout
	dc.l	Resources		; pointer to structure of requested
    					; opened resources
	dc.l	Patch1			; pointer to structure for patches
	dc.l	pls1			; pointer to pls structure
	dc.l	0			; pointer to main UPS msg port
	dc.l	0			; pointer to rexx port
	dc.l	0			; pointer to pxs structure
	dc.l	0,0,0
	dc.l	pfsControlLong
	dc.l	Start			; pointer to the beginning of struct

PatcherName
PatcherIDString
	dc.b	'Icon Changer',0
	even
CoderName
	dc.b	'Goran Mitrovic',0
	even
VerString
	dc.b	'$VER: Icon Changer 1.0',0
	cnop	0,4



Resources
	dc.l	0			; pointer to next entry
	dc.l	0			; pointer to previous entry
	dc.l	rt_Library		; type of resource
	dc.l	100			; resource id
	dc.l	0			; flags
	dc.l	res1name		; pointer to name of resource
dosbase2
	dc.l	0			; base of opened resource
	dc.l	0			; version, if needed
	blk.l	8			; ...to put in data regs
	dc.l	0			; user open rout for user type
	dc.l	0			; user close rout for user type
	dc.l	0			; pointer to name for user type
	dc.l	0,0

res1name
	dc.b	'dos.library',0
	cnop	0,4



Patch1
	dc.l	Patch2			; pointer to next patch
	dc.l	0			; pointer to prev patch
	dc.l	0			; priority, less runs first
	dc.l	Start			; pointer to parent Patcher
	dc.l	Patch1ID		; listview entry
	dc.l	Patch1purpose		; pointer to purpose string
	dc.l	100			; id of resource to be patched
	dc.l	mpsDisableable+mpsFullPatch	; flags
	dc.l	0			; status of flags
	dc.l	Open			; offset to change
	dc.l	Patch1rout		; pointer to new routine
	dc.l	0			; pointer to old routine
	dc.l	0			; pointer to pns structure
	dc.l	0			; notified when enabled/disabled
	dc.l	0			; install rout for user type
	dc.l	0			; uninstall rout for user type
	dc.l	0			; pointer to init routine
	dc.l	0			; pointer to exit routine
	dc.l	pls1			; pointer to pls structure
	dc.l	0,0,0,0


Patch1ID
pls1ID	dc.b	'Open()',0			; 27
	even
Patch1purpose
	dc.b	'improving',0			; 16
	cnop	0,4


Patch2
	dc.l	0			; pointer to next patch
	dc.l	Patch1			; pointer to prev patch
	dc.l	0			; priority, less runs first
	dc.l	Start			; pointer to parent Patcher
	dc.l	Patch2ID		; listview entry
	dc.l	Patch1purpose		; pointer to purpose string
	dc.l	100			; id of resource to be patched
	dc.l	mpsDisableable+mpsFullPatch	; flags
	dc.l	0			; status of flags
	dc.l	Close			; offset to change
	dc.l	Patch2rout		; pointer to new routine
	dc.l	0			; pointer to old routine
	dc.l	0			; pointer to pns structure
	dc.l	0			; notified when enabled/disabled
	dc.l	0			; install rout for user type
	dc.l	0			; uninstall rout for user type
	dc.l	0			; pointer to init routine
	dc.l	0			; pointer to exit routine
	dc.l	pls2			; pointer to pls structure
	dc.l	0,0,0,0

Patch2ID
pls2ID	dc.b	'Close()',0			; 27
	cnop	0,4


pls1	dc.l	pls2			; pointer to next link
	dc.l	0			; pointer to prev link
	dc.l	pls1ID			; listview string
	dc.l	0			; pointer to prefs routine
	dc.l	plsDisableable		; flags
	dc.l	0			; status
	dc.l	Patch1
	dc.l	0


pls2	dc.l	0			; pointer to next link
	dc.l	pls1			; pointer to prev link
	dc.l	pls2ID			; listview string
	dc.l	0			; pointer to prefs routine
	dc.l	plsDisableable		; flags
	dc.l	0			; status
	dc.l	Patch2
	dc.l	0

	cnop	0,4


init	movem.l	d0-a6,-(a7)
	move.l	dosbase,a6
	move.l	#icon,d1
	move.l	#parsed,d2
	move.l	#1024,d3
	jsr	ParsePatternNoCase(a6)
	move.l	#temp,d1
	move.l	#parsed2,d2
	move.l	#1024,d3
	jsr	ParsePatternNoCase(a6)
	move.l	4,a6
	lea	wb,a1
	jsr	FindTask(a6)
	move.l	d0,wbtask
	movem.l	(a7)+,d0-a6
	rts


Patch1rout
	movem.l	d1-a6,-(a7)
	movem.l	d1/d2,-(a7)
	move.l	4,a6
	move.l	wbtask,d0
	cmp.l	ThisTask(a6),d0
	bne.w	old2

	move.l	#parsed,d1
	move.l	d1,d2
	move.l	dosbase,a6
	jsr	MatchPatternNoCase(a6)
	tst.b	d0
	beq.s	old2

	movem.l	(a7)+,d1/d2
	move.l	#next,-(a7)
	lea	Patch1,a1
	move.l	mps_pns(a1),a1
	move.l	pns_Next(a1),-(a7)
	rts
next	tst.l	d0
	bne.s	change
exit	movem.l	(a7)+,d1-a6
	rts

old2	movem.l	(a7)+,d0/d1
old	movem.l	(a7)+,d1-a6
	move.l	a1,d0
	lea	Patch1,a1
	move.l	mps_pns(a1),a1
	move.l	pns_Next(a1),-(a7)
	move.l	d0,a1
	rts


change	move.l	d0,-(a7)			; fh
	move.l	4,a6
	move.l	#fib_SIZEOF,d0
	move.l	#MEMF_PUBLIC,d1
	jsr	AllocVec(a6)
	tst.l	d0
	beq.w	.exit
	move.l	(a7)+,d1			; -
	move.l	d1,-(a7)
	move.l	d0,-(a7)			; fh, fib
	move.l	d0,d2
	move.l	dosbase,a6
	jsr	ExamineFH(a6)
	tst.w	d0
	beq.w	.exit2
	move.l	(a7)+,a1			; fh
	move.l	fib_Size(a1),-(a7)		; fh, len
	move.l	4,a6
	jsr	FreeVec(a6)
	move.l	(a7)+,d0
	move.l	d0,-(a7)
	move.l	#MEMF_PUBLIC,d1
	jsr	AllocVec(a6)
	tst.l	d0
	beq.w	.exit
	move.l	(a7)+,d3
	move.l	(a7)+,d1
	move.l	d0,d2
	move.l	d1,-(a7)
	move.l	d2,-(a7)
	move.l	d3,-(a7)			; fh, buffer, len, fh
	move.l	dosbase,a6
	jsr	Read(a6)
	cmp.l	#-1,d0
	beq.w	.exit3



.wait	tst.l	semaphore
	bne.s	.wait
	move.l	#1,semaphore
	lea	count,a2
	lea	name2,a3
	moveq	#1,d1
	moveq	#7,d7
.loop	move.b	(a2),d0
	add.b	d1,d0
	cmp.b	#16,d0
	blt.s	.noover
	move.b	#0,d0
	add.b	#1,1(a2)
.noover	move.b	d0,(a2)
	add.b	#'a',d0
	move.b	d0,(a3)+
	addq.l	#1,a2
	moveq	#0,d1
	dbra	d7,.loop
	move.l	#name,d1
	move.l	#MODE_NEWFILE,d2

	move.l	#.next,-(a7)
	lea	Patch1,a1
	move.l	mps_pns(a1),a1
	move.l	pns_Next(a1),-(a7)
	rts
.next
	move.l	#0,semaphore
	tst.l	d0
	beq.s	.exit3

	move.l	d0,d1
	move.l	(a7)+,d3			; fh, buffer
	move.l	(a7)+,d2			; fh
	move.l	(a7)+,d4
	move.l	d0,-(a7)			; newfh
	move.l	d2,-(a7)			; newfh, buffer
	move.l	d4,-(a7)			; newfh, buffer, oldfh
	jsr	Write(a6)
	cmp.l	#-1,d0
	beq.s	.exit4

	move.l	(a7)+,d1			; newfh, buffer
	jsr	Close(a6)
	move.l	4,a6
	move.l	(a7)+,a1
	jsr	FreeVec(a6)
	move.l	(a7)+,d1
	move.l	d1,-(a7)
	moveq	#0,d2
	move.l	#OFFSET_BEGINNING,d3
	move.l	dosbase,a6
	jsr	Seek(a6)
	bra.s	.exit


.exit4	move.l	(a7)+,d0
	move.l	(a7)+,d1
	move.l	(a7)+,d2
	bra.s	.exit1
.exit3	move.l	4,a6
	move.l	(a7)+,d0
	move.l	(a7)+,a1
	jsr	FreeVec(a6)
	bra.s	.exit
.exit2	move.l	4,a6
	move.l	(a7)+,a1
	jsr	FreeVec(a6)
.exit	move.l	(a7)+,d0
.exit1	bra.w	exit



Patch2rout
	movem.l	d1-a6,-(a7)
	move.l	d1,-(a7)
	move.l	4,a6
	move.l	wbtask,d0
	cmp.l	ThisTask(a6),d0
	bne.w	.old4

	move.l	#name4,d2
	move.l	#15,d3
	move.l	dosbase,a6
	jsr	NameFromFH(a6)
	tst.w	d0
	beq.s	.old4

	move.l	#parsed2,d1
	move.l	#name4,d2
	jsr	MatchPatternNoCase(a6)
	tst.b	d0
	beq.s	.old4

	move.l	(a7)+,d1
	move.l	#.next,-(a7)
	lea	Patch2,a1
	move.l	mps_pns(a1),a1
	move.l	pns_Next(a1),-(a7)
	rts
.next	tst.l	d0
	bne.s	.change
.exit	movem.l	(a7)+,d1-a6
	rts

.old4	move.l	(a7)+,d1
.old3	movem.l	(a7)+,d1-a6
	move.l	a1,d0
	lea	Patch2,a1
	move.l	mps_pns(a1),a1
	move.l	pns_Next(a1),-(a7)
	move.l	d0,a1
	rts


.change	move.w	#$ffff,d7
.aa	move.w	#$fff,$dff180
	dbra	d7,.aa
	rts




icon	dc.b	'#?.icon',0
wb	dc.b	'Workbench',0
name	dc.b	'ram:tempicon'
name2	dc.l	0,0
	dc.b	0
temp	dc.b	'tempicon#?',0
name3	dc.b	'ram:'
name4	blk.b	15
	dc.b	0
	even

	SECTION	aa,BSS_p
wbtask	ds.l	1
semaphore
	ds.l	1
count	ds.l	2
	ds.l	1
parsed	ds.b	1024
parsed2	ds.b	1024
