#include "stdafx.h"
#include "effectdata.hpp"

CEffectData::CEffectData (CString path)
{
	Init ();
	m_Path = path;
	m_Dirty = false;
}

CEffectData::~CEffectData (void)
{
}


void CEffectData::Init (void)
{
	m_VideoFileName.Empty ();
	m_AudioFileName.Empty ();
	m_DataFileName.Empty ();
	m_Path.Empty ();

	m_Class.Empty ();
	m_StartRunning.Empty ();
	m_Translation.Empty ();
	m_RotationType.Empty ();
	InitVideo ();
	InitAudio ();
}


void CEffectData::InitVideo (void)
{
	m_FX.Empty ();
	m_VideoType.Empty ();
	m_Fog.Empty ();
	m_SimMode.Empty ();
	m_SimExecute.Empty ();
	m_VideoStatus.Empty ();
	m_Locator.Empty ();
}

void CEffectData::InitAudio (void)
{
	m_AudioType.Empty ();
	m_Sample.Empty ();
	m_Loop.Empty ();
	m_MinRange.Empty ();
	m_MaxRange.Empty ();
	m_AudioStatus.Empty ();
	m_AudioShouldExecute.Empty ();
	m_AudioStartRunning.Empty ();
}

void CEffectData::InitialData (CString datafilename)
{
	m_VideoFileName.Empty ();
	m_AudioFileName.Empty ();
	m_DataFileName = datafilename;

	m_Class =	"Adept::Effect";
	m_StartRunning = "yes";
	m_Translation = "0 0 0";
	m_RotationType = "IntoMotionRotation";

	InitialAudio ();
	InitialVideo ();
}
void CEffectData::InitialVideo (void)
{
	m_FX.Empty ();
	m_VideoType = "gosFXComponent";
	m_Fog = "no";
	m_SimMode = "DynamicWorldSpace";
	m_SimExecute = "1";
	m_VideoStatus = "VideoStatus";
	m_Locator = "Effect";
}
void CEffectData::InitialAudio (void)
{
	m_AudioType = "AudioFXComponent";
	m_Sample.Empty ();
	m_Loop = "0";
	m_MinRange = "0";
	m_MaxRange = "0";
	m_AudioStatus = "AudioStatus";
	m_AudioShouldExecute = "1";
	m_AudioStartRunning = "true";
}

void CEffectData::ReadData (CString datafilename)
{
	ASSERT (datafilename != "");
	m_DataFileName = datafilename;

	CString filename;
	char *buf;

	filename = m_Path;
	filename += "\\";
	filename += m_DataFileName;

	buf = m_VideoFileName.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Renderers","VideoRenderer","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_VideoFileName.ReleaseBuffer ();
	
	buf = m_AudioFileName.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Renderers","AudioRenderer","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_AudioFileName.ReleaseBuffer ();

	buf = m_Class.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("GameData","Class","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_Class.ReleaseBuffer ();
	
	buf = m_StartRunning.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("GameData","StartsRunning","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_StartRunning.ReleaseBuffer ();
	
	buf = m_RotationType.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("GameData","RotationType","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_RotationType.ReleaseBuffer ();
	
	buf = m_Translation.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("GameData","translation","fred",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_Translation.ReleaseBuffer ();

	ReadAudioData ();
	ReadVideoData ();
}

void CEffectData::ReadAudioData (void)
{
	CString filename;
	char *buf;

	if (m_AudioFileName.IsEmpty ())
	{
		m_AudioType.Empty ();
		m_Sample.Empty ();
		m_Loop.Empty ();
		m_MinRange.Empty ();
		m_MaxRange.Empty ();
		m_AudioStatus.Empty ();
		m_AudioShouldExecute.Empty ();
		m_AudioStartRunning.Empty ();
		return;
	}
	filename = m_Path;
	filename += "\\";
	filename += m_AudioFileName;


	buf = m_AudioType.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","Type","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_AudioType.ReleaseBuffer ();

	buf = m_Sample.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","Sample","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_Sample.ReleaseBuffer ();

	buf = m_Loop.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","Loop","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_Loop.ReleaseBuffer ();

	buf = m_MinRange.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","MinRange","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_MinRange.ReleaseBuffer ();

	buf = m_MaxRange.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","MaxRange","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_MaxRange.ReleaseBuffer ();

	buf = m_AudioStatus.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","Status","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_AudioStatus.ReleaseBuffer ();

	buf = m_AudioShouldExecute.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","RendererShouldExecute","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_AudioShouldExecute.ReleaseBuffer ();

	buf = m_AudioStartRunning.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("SoundEffect","StartsRunning","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_AudioStartRunning.ReleaseBuffer ();
}

void CEffectData::ReadVideoData (void)
{
	CString filename;
	char *buf;

	if (m_VideoFileName.IsEmpty ())
	{
		m_FX.Empty ();
		m_VideoType.Empty ();
		m_Fog.Empty ();
		m_SimMode.Empty ();
		m_SimExecute.Empty ();
		m_VideoStatus.Empty ();
		m_Locator.Empty ();
		return;
	}
	filename = m_Path;
	filename += "\\";
	filename += m_VideoFileName;

	buf = m_FX.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Effect","gosFX","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_FX.ReleaseBuffer ();

	buf = m_VideoType.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Effect","Type","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_VideoType.ReleaseBuffer ();

	buf = m_Fog.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Effect","fog","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_Fog.ReleaseBuffer ();

	buf = m_SimMode.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Effect","SimulationMode","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_SimMode.ReleaseBuffer ();

	buf = m_SimExecute.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Effect","SimulationShouldExecute","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_SimExecute.ReleaseBuffer ();

	buf = m_VideoStatus.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Effect","Status","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_VideoStatus.ReleaseBuffer ();

	buf = m_Locator.GetBufferSetLength (MAX_PATH);
	GetPrivateProfileString ("Locator","Child","",buf,MAX_PATH-1,(LPCTSTR) filename);
	m_Locator.ReleaseBuffer ();

}

void CEffectData::WriteData (void)
{

	if (!m_Dirty)
		return;

	m_Dirty = false;
	CString filename;
	BOOL error;
	DWORD errcode;
	char *buf;
	CString curdir;

	buf = curdir.GetBufferSetLength (MAX_PATH);
	GetCurrentDirectory (MAX_PATH-1,buf);
	curdir.ReleaseBuffer ();

	filename = curdir;
	filename += "\\";
	filename += m_Path;
	filename += "\\";
	filename += m_DataFileName;

	if (!m_VideoFileName.IsEmpty ())
	{
		buf = m_VideoFileName.GetBufferSetLength (MAX_PATH);
		error = WritePrivateProfileString ("Renderers","VideoRenderer",buf,(LPCTSTR) filename);
		if (!error)
			errcode = GetLastError ();
		m_VideoFileName.ReleaseBuffer ();
	}
	
	if (!m_AudioFileName.IsEmpty ())
	{
		buf = m_AudioFileName.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Renderers","AudioRenderer",buf,(LPCTSTR) filename);
		m_AudioFileName.ReleaseBuffer ();
	}

	if (!m_Class.IsEmpty ())
	{
		buf = m_Class.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("GameData","Class",buf,(LPCTSTR) filename);
		m_Class.ReleaseBuffer ();
	}
	
	if (!m_StartRunning.IsEmpty ())
	{
		buf = m_StartRunning.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("GameData","StartsRunning",buf,(LPCTSTR) filename);
		m_StartRunning.ReleaseBuffer ();
	}
	
	if (!m_RotationType.IsEmpty ())
	{
		buf = m_RotationType.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("GameData","RotationType",buf,(LPCTSTR) filename);
		m_RotationType.ReleaseBuffer ();
	}
	
	if (m_Translation != "fred")
	{
		buf = m_Translation.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("GameData","translation",buf,(LPCTSTR) filename);
		m_Translation.ReleaseBuffer ();
	}


	WriteAudioData ();
	WriteVideoData ();
}

void CEffectData::WriteAudioData (void)
{
	CString filename;
	char *buf;

	if (m_AudioFileName.IsEmpty ())
	{
		return;
	}
	CString curdir;

	buf = curdir.GetBufferSetLength (MAX_PATH);
	GetCurrentDirectory (MAX_PATH-1,buf);
	curdir.ReleaseBuffer ();

	filename = curdir;
	filename += "\\";
	filename += m_Path;
	filename += "\\";
	filename += m_AudioFileName;

	BOOL error;
	DWORD errcode;

	if (!m_AudioType.IsEmpty ())
	{
		buf = m_AudioType.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","Type",buf,(LPCTSTR) filename);
		m_AudioType.ReleaseBuffer ();
	}

	if (!m_Sample.IsEmpty ())
	{
		buf = m_Sample.GetBufferSetLength (MAX_PATH);
		error = WritePrivateProfileString ("SoundEffect","Sample",buf,(LPCTSTR) filename);
		if (!error)
			errcode = GetLastError ();
		m_Sample.ReleaseBuffer ();
	}

	if (!m_Loop.IsEmpty ())
	{
		buf = m_Loop.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","Loop",buf,(LPCTSTR) filename);
		m_Loop.ReleaseBuffer ();
	}

	if (!m_MinRange.IsEmpty ())
	{
		buf = m_MinRange.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","MinRange",buf,(LPCTSTR) filename);
		m_MinRange.ReleaseBuffer ();
	}

	if (!m_MaxRange.IsEmpty ())
	{
		buf = m_MaxRange.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","MaxRange",buf,(LPCTSTR) filename);
		m_MaxRange.ReleaseBuffer ();
	}

	if (!m_AudioStatus.IsEmpty ())
	{
		buf = m_AudioStatus.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","Status",buf,(LPCTSTR) filename);
		m_AudioStatus.ReleaseBuffer ();
	}

	if (!m_AudioShouldExecute.IsEmpty ())
	{
		buf = m_AudioShouldExecute.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","RendererShouldExecute",buf,(LPCTSTR) filename);
		m_AudioShouldExecute.ReleaseBuffer ();
	}

	if (!m_AudioStartRunning.IsEmpty ())
	{
		buf = m_AudioStartRunning.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("SoundEffect","StartsRunning",buf,(LPCTSTR) filename);
		m_AudioStartRunning.ReleaseBuffer ();
	}
}

void CEffectData::WriteVideoData (void)
{
	CString filename;
	char *buf;

	if (m_VideoFileName.IsEmpty ())
	{
		return;
	}
	CString curdir;

	buf = curdir.GetBufferSetLength (MAX_PATH);
	GetCurrentDirectory (MAX_PATH-1,buf);
	curdir.ReleaseBuffer ();

	filename = curdir;
	filename += "\\";
	filename += m_Path;
	filename += "\\";
	filename += m_VideoFileName;

	if (!m_FX.IsEmpty ())
	{
		buf = m_FX.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Effect","gosFX",buf,(LPCTSTR) filename);
		m_FX.ReleaseBuffer ();
	}

	if (!m_VideoType.IsEmpty ())
	{
		buf = m_VideoType.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Effect","Type",buf,(LPCTSTR) filename);
		m_VideoType.ReleaseBuffer ();
	}

	if (!m_Fog.IsEmpty ())
	{
		buf = m_Fog.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Effect","fog",buf,(LPCTSTR) filename);
		m_Fog.ReleaseBuffer ();
	}

	if (!m_SimMode.IsEmpty ())
	{
		buf = m_SimMode.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Effect","SimulationMode",buf,(LPCTSTR) filename);
		m_SimMode.ReleaseBuffer ();
	}

	if (!m_SimExecute.IsEmpty ())
	{
		buf = m_SimExecute.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Effect","SimulationShouldExecute",buf,(LPCTSTR) filename);
		m_SimExecute.ReleaseBuffer ();
	}

	if (!m_VideoStatus.IsEmpty ())
	{
		buf = m_VideoStatus.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Effect","Status",buf,(LPCTSTR) filename);
		m_VideoStatus.ReleaseBuffer ();
	}

	if (!m_Locator.IsEmpty ())
	{
		buf = m_Locator.GetBufferSetLength (MAX_PATH);
		WritePrivateProfileString ("Locator","Child",buf,(LPCTSTR) filename);
		m_Locator.ReleaseBuffer ();
	}

}

void CEffectData::VideoFileName (const char *p1)
{
	m_Dirty = true;
	m_VideoFileName = p1;
}
void CEffectData::AudioFileName (const char *p1)
{
	m_Dirty = true;
	m_AudioFileName = p1;
}
void CEffectData::DataFileName (const char *p1)
{
	m_Dirty = true;
	m_DataFileName = p1;
}
void CEffectData::Path (const char *p1)
{
	m_Dirty = true;
	m_Path = p1;
}

void CEffectData::Class (const char *p1)
{
	m_Dirty = true;
	m_Class = p1;
}
void CEffectData::StartRunning (const char *p1)
{
	m_Dirty = true;
	m_StartRunning = p1;
}
void CEffectData::Translation (const char *p1)
{
	m_Dirty = true;
	m_Translation = p1;
}
void CEffectData::RotationType (const char *p1)
{
	m_Dirty = true;
	m_RotationType = p1;
}

// video data
void CEffectData::FX (const char *p1)
{
	m_Dirty = true;
	m_FX = p1;
}
void CEffectData::VideoType (const char *p1)
{
	m_Dirty = true;
	m_VideoType = p1;
}
void CEffectData::Fog (const char *p1)
{
	m_Dirty = true;
	m_Fog = p1;
}
void CEffectData::SimMode (const char *p1)
{
	m_Dirty = true;
	m_SimMode = p1;
}
void CEffectData::SimExecute (const char *p1)
{
	m_Dirty = true;
	m_SimExecute = p1;
}
void CEffectData::VideoStatus (const char *p1)
{
	m_Dirty = true;
	m_VideoStatus = p1;
}
void CEffectData::Locator (const char *p1)
{
	m_Dirty = true;
	m_Locator = p1;
}

// audio data
void CEffectData::AudioType (const char *p1)
{
	m_Dirty = true;
	m_AudioType = p1;
}
void CEffectData::Sample (const char *p1)
{
	m_Dirty = true;
	if (m_Sample.IsEmpty () && strcmp (p1,""))
	{
		InitialAudio ();
		m_AudioFileName = m_DataFileName;
		int index;
		index = m_AudioFileName.ReverseFind ('.');
		m_AudioFileName = m_AudioFileName.Left (index);
		m_AudioFileName += ".audio";
	}
	m_Sample = p1;
}
void CEffectData::Loop (const char *p1)
{
	m_Dirty = true;
	m_Loop = p1;
}
void CEffectData::MinRange (const char *p1)
{
	m_Dirty = true;
	m_MinRange = p1;
}
void CEffectData::MaxRange (const char *p1)
{
	m_Dirty = true;
	m_MaxRange = p1;
}
void CEffectData::AudioStatus (const char *p1)
{
	m_Dirty = true;
	m_AudioStatus = p1;
}
void CEffectData::AudioShouldExecute (const char *p1)
{
	m_Dirty = true;
	m_AudioShouldExecute = p1;
}
void CEffectData::AudioStartRunning (const char *p1)
{
	m_Dirty = true;
	m_AudioStartRunning = p1;
}

