 

 --fonctions générique, pas de namespace à priori
 function PrintUnitsState()
	local s= printLine('UnitsState = {')


	for className, classArray in pairs(SaveManager.ClassesToSave) do
		local nbAttributes = table.getn(classArray)
		local u, b = OPUnitFactory.GetFirstUnit(className)
		s = s .. printLine(className .. ' = {')


		while u~= nil do
			s = s .. printLine('["' .. u.Name .. '"] = {')

			for attrIdx, attrName in pairs(classArray) do
				s = s .. printLine(attrName .. " = " .. attrToString(u[attrName]) .. ',')
			end

			s = s .. printLine('},')
			u = OPUnitFactory.GetNextUnit(b)
		end


		s = s .. printLine('},')
	end

	s = s .. printLine('},')
	return s
end

 
 function doesFileExist(_fileName)
	local foundFiles = OPFileManager.FindFiles(_fileName, SORT_FIND_FILES_BY_DATE)
	local nbFiles = table.getn(foundFiles)
	if (nbFiles == 0) then
		return false
	end
	return true
end

function PrintTable(t,deep)
	local i = next(t)
	local s = '\r' .. '{'

	while i ~= nil do
		if type(i) == "number" then
			s = s..'\r' .. MyToString(t[i],deep+1) .. ","
		elseif type(i) == 'string' then
			index = i
			s = s..'\r' .. '["' .. index .. '"] = '
			s = s .. MyToString(t[i],deep+1) .. ","
		else
			index = i
			s = s..'\r' .. '["' .. index .. '"] = '
			s = s .. MyToString(t[i],deep+1) .. ","
		end
		i = next(t, i)
	end

	s = s.."\r" .."}"
	
	return s
end

function MyToString(_value,deep)
	if deep == nil then deep = 0 end 
	local value
	if type(_value) == "boolean"
		or type(_value) == "number"
	then
		value = tostring(_value)
	elseif type(_value) == "table" then
		value = PrintTable(_value,deep)
	elseif type(_value) ~= "string" then
		value = "nil"
	else
		value = '"' .. _value .. '"'
end

return value
end


function printLine(_string)
	return  _string .. '\r'
end


function PrintDataArrays(DataArrays)
	local s = printLine('DataArrays = {')


	for i, dataArrayName in pairs(DataArrays) do
		local da = Load(dataArrayName, 'DataArray')
		s = s..PrintDataArray(da).."\r"
	end

	s = s .. printLine('},')

	return s
end

function PrintDataArray(t)
	local s = printLine('["' .. t.Name .. '"] = {')
	local i = t.Next()
while i ~= nil do
local value = MyToString(t.Get(i), 2)

if type(i) == "number" then
value = string.gsub(value, "\\", "\\".."\\")
			s = s .. value .. ",\r"
else
value = string.gsub(value, "\\", "\\".."\\")
s = s .. '["' .. i .. '"] = ' .. value .. ',\r'
end
		i = t.Next(i)
end
s = s .. "},"

return s
end

function attrToString(_attr)
	local value
	
	if (_attr == nil) then
		return "'nil'"
	end
	
	if type(_attr) == 'boolean' then
		value = tostring(_attr)
	elseif type(_attr) == 'number' then
		value = '' .. _attr
	elseif type(_attr) == 'string' then
		value = "'" .. _attr .. "'"
	else
		value = "''"
	end
	return value
end