// Built with delph 6
// https://winworldpc.com/product/delphi/60
// key: xawp-twcmd-tdegr  j9v-77c
// Inside windows XP VM: https://archive.org/details/net-yaroze-dev-winxp-vm-2022

// Copied from MQOExporter, MC Dev docs:
// https://web.archive.org/web/20160612052732/http://homepage3.nifty.com/escargot/DownLoadFrm.html (MarbleSDK beta5.6)
// Built in
// C:\MarbleSDK\Plugin\RSDExporter\

// I'm not a very good pascal programmer, sorry :/
// If you improve this plugin, please share!



unit RSD_Exporter;

interface
uses
  Windows, SysUtils, Classes, Dialogs,MarbleSDK,MarbleSDKType;

function  MCP_PluginEntry (AppHandle : THandle; DllHandle : THandle; Ver : integer; AppPath : PChar; size : integer): integer; cdecl;
function  MCP_GetPluginInfo(sender : TCW_ID; index : integer) : TCW_BOOLEAN; cdecl;
procedure MCP_ReleasePlugin(moduleid : TCW_ID) ; cdecl;
function  MCP_GetExporterInfo(sender : TCW_ID; moduleid : TCW_ID) : TCW_BOOLEAN; cdecl;
function  MCP_ExecuteExport(sender : TCW_ID; moduleid : TCW_ID; doc : PCW_Document; info : PCW_TransporterInfo) : TCW_BOOLEAN; cdecl;

exports MCP_PluginEntry, MCP_GetPluginInfo, MCP_ReleasePlugin, MCP_GetExporterInfo,MCP_ExecuteExport;

implementation
uses RSDXcode;
type
  TRSDExportOption = record
    FlipVValue : TCW_GUI_CheckBox;
  end;

var
 RSDExportOption : TRSDExportOption;

// ---------------------------------------------------------------------------
// plugin 初期化関数群
// ---------------------------------------------------------------------------


function  MCP_PluginEntry (AppHandle : THandle; DllHandle : THandle; Ver : integer; AppPath : PChar; size : integer): integer; cdecl;
begin
  if Ver >= $00050600 then
  begin
    result := 1; // このdllに含まれるモジュール数。このサンプルは1つのモジュールを持つので1
  end else begin
    result := 0; // 古いバージョンのMarbleCLAYで動作しないプラグインの場合は0を返す。
                 // モジュール個別で動作の可・不可を切り替えたい場合は、MCP_GetPluginInfoでfalseを返す
  end;
end;


function MCP_GetPluginInfo(sender : TCW_ID; index : integer) : TCW_BOOLEAN; cdecl;
var
  info : TCW_PluginInfo;
begin
  result := CW_TRUE;

  // 各モジュール情報をMarbleCLAYに渡す
  case index of
  0:
    begin
      ZeroMemory(@info, sizeof(info)); // ADD b5.5 sample
      info.Name := PChar('RSD Exporter');
      info.Info := PChar('PlayStation RSD exporter');
      info.AuthorID := 97; // 開発者ID 0=T.Maeda  1以上=各開発者固有の番号
      info.TypeID := CW_EXPORTER;
     // info.ModuleID := 1997; // モジュールのID。モジュールを識別できるならばどんな値でも良い。(例えばモジュールをクラス化して、インスタンスポインタをidにする)
	 // info.ModuleID := TCW_ID(@RSDExportOption); // モジュールの識別ID。ここでは各モジュール用の情報格納用構造体のポインタを渡す
      info.ModuleID := 97; // ID of the module. Any value that identifies the module will do. (e.g. class the module and make the instance pointer the id)
 
	end;
  else
    result := CW_FALSE;
  end;

  if result then CWAPIs.SendData( 0 , sender, CW_PLUGIN_INFO, @info, sizeof(info) );
end;

procedure MCP_ReleasePlugin(moduleid : TCW_ID) ; cdecl;
begin
  case moduleid of
  1 :
    begin
    end;
  end;
end;

function MCP_GetExporterInfo(sender : TCW_ID; moduleid : TCW_ID) : Boolean; cdecl;
var
  info : TCW_TransporterInfo;
 // temp : string;
begin
  result := true;
  case moduleid of
  97 :
    begin
      ZeroMemory(@info, sizeof(info)); // ADD b5.5 sample
      info.Ext := PChar('rsd');
      info.Desc := PChar('PlayStation (filename must be <=7 chars!)');
      info.Upaxis := CW_Zaxis;
      info.Leftaxis := CW_Yaxis;
      info.Rightaxis := CW_Xaxis;
      info.AxisLock := true;
      info.Flip := false;
      info.Scale := 1;

      ZeroMemory(@RSDExportOption.FlipVValue, sizeof(RSDExportOption.FlipVValue) );
      RSDExportOption.FlipVValue.Base.size := sizeof(RSDExportOption.FlipVValue); // b5.6 構造体にsizeプロパティを導入
      RSDExportOption.FlipVValue.Base.RegistScript := 'FlipV|restrFlipVValue=1';
      // not needed
	  CWAPIs.SendData( moduleid, sender, CW_REGISTER_GUI_CHECKBOX, @RSDExportOption.FlipVValue, sizeof(RSDExportOption.FlipVValue));
    end;
  else
    result := false;
  end;

  if result then CWAPIs.SendData( moduleid, sender, CW_MODULE_INFO, @info, sizeof(info) );
end;

// ---------------------------------------------------------------------------
// GUIイベント
// ---------------------------------------------------------------------------

procedure MCP_GUIEvent(sender : TCW_ID; moduleid : TCW_ID; msg : PCW_GUI_MESSAGE);
//var
// name : string;
begin
  if moduleid = TCW_ID(@RSDExportOption) then
  begin
  end;
end;

// ---------------------------------------------------------------------------
// plugin Export呼び出し 入り口
// ---------------------------------------------------------------------------

function MCP_ExecuteExport(sender : TCW_ID; moduleid : TCW_ID; doc : PCW_Document; info : PCW_TransporterInfo) : TCW_BOOLEAN; cdecl;
var
  i, objcount, tempi, tempi2, objselected : integer;
    obj : PCW_MeshObject;
	temp, temp1 , objname: string;
	
begin
  result := false;

  if moduleid = 97 then
  begin
  
  objcount := CWAPIs.GetMeshObjectCount(doc);
			
			
			for i := 0 to objcount-1 do
			begin
			  obj := CWAPIs.GetMeshObject(doc,i); // オブジェクトを取得
				
				str (i,temp);
				str (objcount,temp1);
				temp := 'Object = ' + temp + ' of ' + temp1 ;			
				  // ShowMessage(temp);
				  
			  // オブジェクトの選択状態を取得
			  objselected := ObjectAttrInteger( obj, 'Activated' , 0); //2004.8.14 Selected->Activatedに変更
			  objname := ObjectAttrString( obj, 'Name', '(noname)');

			  // 選択されているオブジェクトのみをエクスポート
			  // if AnsiCompareText(objname, 'NewObject') =0 then
			  if (objselected<>0) and (result = false) then
			  begin
				result := true; // do onces
				RSDExporter( doc, i, info.FileName, info.Scale , 2); //ask TMD, Launch NoPSX, cancel
				
				end;
				
			//for
			end;
			
		if result = true then
		begin
		
		end else begin
		ShowMessage('ERROR! No model selected or no model data is present! Please select the root model to export.');
							
		
		
		end;
end;
  

end;



end.
