
format elfobj64

override include_sec 1
include "common.h" "common.oh"

#multithread unsafe
char logpad=xlog_pad_default
#

#functions(except oadata_free,oadata_logpad) are returning 0(long) on success and != on failure (EXIT_FAILURE or another != (from extern calls))

importx "puts" puts
importx "printf" printf
importx "free" free
import "sprite_free" sprite_free
import "print" print
import "print_inline_done" print_inline_done
#
importx "strlen" strlen
importx "malloc" malloc
importx "memcpy" memcpy
^import "fcontent" fcontent 2
import "loop" loop
import "inter" inter
import "oadata_init" oadata_init
import "preoadata_init" preoadata_init
import "preoadata_free" preoadata_free
import "presprite_done" presprite_done
import "tree_init" tree_init
import "tree_exit" tree_exit
#
importx "unlink" unlink
importx "rename" rename
^import "inter_extension" inter_extension 1
^import "inter_last_extension" inter_last_extension 1

functionx oadata_free(sv data,charx log) #2
	if data!=(NULL)
		sv sprites;set sprites data#:oadata.sprites
		if sprites!=(NULL)
			sv s;set s sprites
			sv end;set end data#:oadata.sprites_size
			add end s
			if log=(TRUE)
				call puts("sprites:")
			end
			while s!=end
				if log!=(TRUE)
					call sprite_free(s,(NULL))
				else
					call sprite_free(s,sprites)
				end
				incst s
			end
			call free(sprites)

			sv functions;set functions data#:oadata.functions
			if functions!=(NULL)
				if log=(TRUE)
					set s data#:oadata.functions_size
					div s :
					call print("functions: %u",s)
				end
				call free(functions)

				sv marks;set marks data#:oadata.marks
				if marks!=(NULL)
					if log=(TRUE)
						valueu sz#1;set sz data#:oadata.size
						div sz \mark\
						call printf("marks: %u",sz)
						if sz>^\mark\
							#log first and last
							valueu tolog#1;set tolog marks
							add tolog data#:oadata.size
							sub tolog \mark\
							call printf(": %d %u ... %u %d",marks#:mark.prev,marks#:mark.next,tolog#:mark.prev,tolog#:mark.next)
						end
						call print_inline_done()
					end
					call free(marks)
				end
			end
		end
		call free(data)
	end
end

functionx oadata_make(ss swf_fullpath,svu poadata,charx log) #3
	set poadata# (NULL)
	sd size;set size strlen(swf_fullpath)
	char logext=".log";vstr slogext^logext
	sd allsize=\.logext;add allsize size
	sd mem;set mem malloc(allsize)
	if mem!=(NULL)
		sd exit
		call memcpy(mem,swf_fullpath,size)
		add size mem
		call memcpy(size,slogext,\.logext)
		sd logsize
		sd logcontent;set logcontent fcontent(mem,#logsize)
		if logcontent!=(NULL)
			char xext=".x";vstr sxext^xext
			call memcpy(size,sxext,\.xext)
			sd dummy
			sd xcontent;set xcontent fcontent(mem,#dummy)
			if xcontent!=(NULL)
				char xxext=".xx";vstr sxxext^xxext
				call memcpy(size,sxxext,\.xxext)
				sd xxcontent;set xxcontent fcontent(mem,#dummy)
				if xxcontent!=(NULL)
					set exit oadata_init(poadata)
					if exit=(EXIT_SUCCESS)
						sv presprites
						sv sprites   ;#these will be filled before sprite done
						set exit preoadata_init(#presprites,#sprites)
						if exit=(EXIT_SUCCESS)
							if log=(TRUE)
								call tree_init()
							end
							set exit loop(logcontent,xcontent,xxcontent,logsize,#presprites,#sprites,poadata#,log)
							if exit=(EXIT_SUCCESS)
								if log=(TRUE)
									set exit tree_exit()
								end
								if exit=(EXIT_SUCCESS)
									set exit presprite_done(sprites,0,0,poadata#,presprites) #root has not a done(0 to 0) in .log
									if exit=(EXIT_SUCCESS)
										set exit inter(presprites,swf_fullpath,poadata#)
									end
								end
							end
						end
						if exit=(EXIT_SUCCESS)
							call preoadata_free(presprites,sprites,log,xcontent)
						else
							call preoadata_free(presprites,sprites,(FALSE))
						end
					end
					if exit!=(EXIT_SUCCESS)
						call oadata_free(poadata#,(FALSE)) #there are multiple mallocs in outinit
					end
					call free(xxcontent)
				else
					set exit (EXIT_FAILURE)
				end
				call free(xcontent)
			else
				set exit (EXIT_FAILURE)
			end
			call free(logcontent)
		else
			set exit (EXIT_FAILURE)
		end
		call free(mem)
		return exit
	end
	return (EXIT_FAILURE)
end

functionx oadata_ok(ss swf_fullpath)
# move ...last.data ...data
	sd size;set size strlen(swf_fullpath)
	ss slastinter;sd size1
	set slastinter inter_last_extension(#size1)
	add size1 size
	sd from;set from malloc(size1)
	if from!=(NULL)
		ss sinterfinal;sd size2
		set sinterfinal inter_extension(#size2)
		add size2 size
		sd to;set to malloc(size2)
		if to!=(NULL)
			call memcpy(from,swf_fullpath,size)
			call memcpy(to,swf_fullpath,size)
			sub size1 size
			add size from
			call memcpy(size,slastinter,size1)
			sub size from
			sub size2 size
			add size to
			call memcpy(size,sinterfinal,size2)

			datax r#1 ;#same 0/-1, here is int
			call unlink(to) #same 0/-1, on windows will not rename if "File exists"
			set r rename(from,to)

			call free(from)
			call free(to)
			return r
		end
		call free(from)
	end
	return (EXIT_FAILURE)
end

functionx oadata_logpad_get()
	return global.logpad
end
functionx oadata_logpad_set(charx a)
	if a!=0
		set global.logpad a
	else
		set global.logpad 1
	end
end

entryraw global()
