
format elfobj64

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

#multithread unsafe
value folder=NULL
vstrx concat_point#1
datax flag_dup#1
#
datax flag_splitter#1
#charx markers#max_structures
charx splitdelim#1+3+1

importx "strrchr" strrchr
importx "strcpy" strcpy
importx "memcpy" memcpy
importx "sprintf" sprintf
importx "strstr" strstr
importx "fclose" fclose

^import "swf_sprite_frames_pointer" swf_sprite_frames_pointer 1 #full return
^import "word_arg_to_stack" word_arg_to_stack 1
import "mem_free" mem_free
^import "struct_ids_action" struct_ids_action 2 #full
import "block_get_size" block_get_size

function alt_join()
	add globall.concat_point globall.folder
	call strcpy(globall.concat_point,".dbg")
	add globall.concat_point 4;set globall.concat_point# (path_separator)
	inc globall.concat_point
end
function alt_free()
	if globall.folder!=(NULL)
		#set globall.concat_point# (Nullchar)
		#call rmdir(globall.folder)
		call mem_free(#globall.folder)
	end
end

function flag_noDup_get()
	return globall.flag_dup
end

import "ainit" ainit

importx "strlen" strlen

#aftercallimport ebool

^import "memalloc" memalloc 1
import "error" error
^import "len_of" len_of 2
^import "f_open" f_open 2
import "f_write" f_write

function alt_init(sd flags,sd path)
	if globall.folder!=(NULL)
		call error("swf started, alt not closed")
	end

	set globall.flag_splitter flags
	and globall.flag_splitter (flag_splitters)

	set globall.flag_dup flags
	and globall.flag_dup (flag_noDuplicate) #needed at ffdec reproducible

	and flags (flag_alt)
	if flags=(flag_alt)
		sd size;setcall size strlen(path)
		sd r;set r strrchr(path,(Period))
		if r!=(NULL)
			sub r path
			set size r
		end
		sd finalsize;set finalsize len_of(size,4+ext_str_len) #ext+...

		setcall globall.folder memalloc(finalsize)
		call memcpy(globall.folder,path,size)
		set globall.concat_point size
		call alt_join()

		#ss p;set p #globall.markers
		#sd end=max_structures
		#add end p
		#while p!=end
		#	set p# 0
		#	inc p
		#end
		if globall.flag_splitter!=0
			ss s;set s #globall.splitdelim
			inc s
			char a="-1;";ss b^a
			call memcpy(s,b,\.a)
			dec s
			if globall.flag_splitter=(flag_splitter_r)
				set s# (CarriageReturn)
			else
				set s# (LineFeed)
			end
		end
	end
end

^import "file_get_content" file_get_content 2

function alt_action(ss orig_action,sd ?preid)
	callret alt_action_base(orig_action,preid,(TRUE))
end
function alt_action_base(ss orig_action,sd ?preid,ss ?has_frames)
	if globall.folder!=(NULL)
		if orig_action!=(NULL) #this to filter a button with no swf actions for example
			#ss mark;set mark preid
			#add mark #globall.markers
			#if mark#=0
			whiletrue
				if globall.flag_splitter=0
					sd block;sd ?size
					setcall block struct_ids_action((ids_get),preid)
					setcall size block_get_size(block)
					if size!=0 #at alt, first action from the frame is with all
						break
					end
				end
				whiletrue
					if has_frames=(TRUE)
						ss ?ai;set ai ainit()
						if ai=(xlog_ainit_false)
							sw p
							#if preid!=0
							set p swf_sprite_frames_pointer(preid)
							#else #set p swf_frames_pointer()#end
							sd ?f;set f word_arg_to_stack(p)
							inc f  ##index 0 is frame 1 at ffdec
							call sprintf(globall.concat_point,"%u_%u",preid,f)
							break
						end
					end
					call sprintf(globall.concat_point,"%u",preid)
					break
				end
				#datax int#1;set int access(globall.folder,(R_OK))
				#if int=0
				ss mem
				set mem file_get_content(globall.folder,(NULL))
				#call unlink(globall.folder) #if there are two action() at one frame, the second one must not see this
				if globall.flag_splitter!=0
					ss junction;set junction strstr(mem,#globall.splitdelim)
					if junction!=(NULL)
						inc junction ##prexx and alt can go together, must keep system line endings
						set junction# (Nullchar)
						add junction \globall.splitdelim-2
						svu ?sz;set sz strlen(junction)

						sd file;set file f_open(globall.folder,"wb")
						call f_write(file,junction,sz)
						call fclose(file)
					end
				end
				#set mark# ~0
				#break

				return mem
			end
		end
		return (NULL)
	end
	return orig_action
end

import "swf_actionblock_add" swf_actionblock_add

function alt_splitter()
	if globall.flag_splitter!=0
		char vals={0x96, 0x05, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0x17} # push -1 pop, titles have push pop only at last phase?
		callg swf_actionblock_add(#vals,\.vals)
	end
end

entryraw globall()
