
format elfobj64

include "../include/prog.h" "../include/prog.oh"

#multithread unsafe
char dtbool#1
dataxu func#1 #like ui32 swf size
char frame_index={_0,_0,_0,_0,0,0,0,0,0} #id

importx "sprintf" sprintf
importx "memcpy" memcpy

^import "word_arg_to_stack" word_arg_to_stack 1

#here iterates swf tags in the sprite, enters DoAction tags, first tag is the pool with the format from dtitles_frame, goes at last string, overwrites 0000 with the final id
function dtitles_conclude(sd finalid,sd mem)
	if globale.dtbool=(TRUE)
		#finalid is u16
		char store#5
		call sprintf(#store,"%04X",finalid)

		add mem (WORD+WORD) #id field, frame count
		whiletrue
			sd RECORDHEADER
			set RECORDHEADER word_arg_to_stack(mem)
			add mem (WORD)
			sd length=recordheader_long_mark
			and length RECORDHEADER
			divu RECORDHEADER (recordheader_upperbits)
			if RECORDHEADER=(End)
				break
			end
			if length=(recordheader_long_mark)
				set length mem#
				add mem (DWORD)
			end
			if RECORDHEADER=(DoAction)
				call dtitles_conclude_record(mem,#store)
#			elseif RECORDHEADER=(DoInitAction) is not here anymore, see at part_done
			end
			add mem length
		end
	end
end
function dtitles_conclude_record(sd mem,sd finalidstr)
	ss cursor
	set cursor mem
	inc cursor           ##ACTIONRECORDHEADER ActionCode
	sd ?Length
	set Length word_arg_to_stack(cursor)
	add cursor (WORD)
	sd ?Count
	set Count word_arg_to_stack(cursor)
	add cursor (WORD)
	if Count!=1
		add cursor Length     ##now is +2 in offset [Count is u16]
		sub cursor (1+   4+   1+   1+   2)
		#           here 0000 X    null
		#  previous str0       XXX
		while cursor#!=(Nullchar)  #there are 3 more possible digits
			dec cursor
		end
		inc cursor
	end
	call memcpy(cursor,finalidstr,4)
end

function dtitles_init(sd flags)
	and flags (flag_titles)
	if flags!=0
		set globale.dtbool (TRUE)
		set globale.func 0
		ret
	end
	set globale.dtbool (FALSE)
end

import "swf_actionblock_change" swf_actionblock_change #is a good id because is called at done, there are id verif until that, and at button is internal not from user
import "swf_actionblock_change_back" swf_actionblock_change_back

#aftercallimport ebool

import "actionpool_value_ex" actionpool_value_ex

#here adds 0000X[X[X[X]]], if is root 0000 will be final, else is dummy now and will be sprite final id at conclude function
function dtitles_frame(swu ?index) #index is uword here, is checked
	if globale.dtbool=(TRUE)
		ss p=4
		add p #globale.frame_index
		call sprintf(p,"%X",index)
		call actionpool_value_ex(#globale.frame_index,(TRUE))  #force the write because debugger will look at the last pool constant there
	end
end
function dtitles_frame_one(sd preid,sd finalid) #is called at swf_sprite_done and swf_button
	if globale.dtbool=(TRUE)
		#finalid is u16
		char store#5
		call sprintf(#store,"%X",finalid)

		call swf_actionblock_change(preid)
		call actionpool_value_ex(#store,(TRUE))
		call swf_actionblock_change_back()
	end
end

import "action_push" action_push
import "actionpush" actionpush
import "action_one" action_one

function dtitles_func()
	if globale.dtbool=(TRUE)
		#this will not be sync'ed with prev log @ & if on xx_flags
		call action_push((ap_Integer),globale.func)
		call actionpush()
		inc globale.func
		callg action_one((ActionPop))
	end
end

entryraw globale()
