// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Sidecar.cs
// Version : 1.0
// Author(s) : Natalia Portillo
//
// Component : Component
//
// Revision : $Revision$
// Last change by : $Author$
// Date : $Date$
//
// --[ Description ] ----------------------------------------------------------
//
// Description
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// ----------------------------------------------------------------------------
// Copyright (C) 2011-2015 Claunia.com
// ****************************************************************************/
// //$Id$
using System.Collections.Generic;
using System.IO;
using DiscImageChef.CommonTypes;
using DiscImageChef.Console;
using DiscImageChef.Decoders.PCMCIA;
using DiscImageChef.Filesystems;
using DiscImageChef.ImagePlugins;
using DiscImageChef.PartPlugins;
using Schemas;
namespace DiscImageChef.Core
{
public static class Sidecar
{
public static event InitProgressHandler InitProgressEvent;
public static event UpdateProgressHandler UpdateProgressEvent;
public static event EndProgressHandler EndProgressEvent;
public static event InitProgressHandler2 InitProgressEvent2;
public static event UpdateProgressHandler2 UpdateProgressEvent2;
public static event EndProgressHandler2 EndProgressEvent2;
public static event UpdateStatusHandler UpdateStatusEvent;
public static void InitProgress()
{
if(InitProgressEvent != null)
InitProgressEvent();
}
public static void UpdateProgress(string text, long current, long maximum)
{
if(UpdateProgressEvent != null)
UpdateProgressEvent(string.Format(text, current, maximum), current, maximum);
}
public static void EndProgress()
{
if(EndProgressEvent != null)
EndProgressEvent();
}
public static void InitProgress2()
{
if(InitProgressEvent2 != null)
InitProgressEvent2();
}
public static void UpdateProgress2(string text, long current, long maximum)
{
if(UpdateProgressEvent2 != null)
UpdateProgressEvent2(string.Format(text, current, maximum), current, maximum);
}
public static void EndProgress2()
{
if(EndProgressEvent2 != null)
EndProgressEvent2();
}
public static void UpdateStatus(string text, params object[] args)
{
if(UpdateStatusEvent != null)
UpdateStatusEvent(string.Format(text, args));
}
public static CICMMetadataType Create(ImagePlugin image, string imagePath)
{
CICMMetadataType sidecar = new CICMMetadataType();
PluginBase plugins = new PluginBase();
plugins.RegisterAllPlugins();
FileInfo fi = new FileInfo(imagePath);
FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
Checksum imgChkWorker = new Checksum();
// For fast debugging, skip checksum
//goto skipImageChecksum;
byte[] data;
long position = 0;
InitProgress();
while(position < (fi.Length - 1048576))
{
data = new byte[1048576];
fs.Read(data, 0, 1048576);
UpdateProgress("Hashing image file byte {0} of {1}", position, fi.Length);
imgChkWorker.Update(data);
position += 1048576;
}
data = new byte[fi.Length - position];
fs.Read(data, 0, (int)(fi.Length - position));
UpdateProgress("Hashing image file byte {0} of {1}", position, fi.Length);
imgChkWorker.Update(data);
// For fast debugging, skip checksum
//skipImageChecksum:
EndProgress();
fs.Close();
List imgChecksums = imgChkWorker.End();
switch(image.ImageInfo.xmlMediaType)
{
case XmlMediaType.OpticalDisc:
{
sidecar.OpticalDisc = new OpticalDiscType[1];
sidecar.OpticalDisc[0] = new OpticalDiscType();
sidecar.OpticalDisc[0].Checksums = imgChecksums.ToArray();
sidecar.OpticalDisc[0].Image = new ImageType();
sidecar.OpticalDisc[0].Image.format = image.GetImageFormat();
sidecar.OpticalDisc[0].Image.offset = 0;
sidecar.OpticalDisc[0].Image.offsetSpecified = true;
sidecar.OpticalDisc[0].Image.Value = Path.GetFileName(imagePath);
sidecar.OpticalDisc[0].Size = fi.Length;
sidecar.OpticalDisc[0].Sequence = new SequenceType();
if(image.GetMediaSequence() != 0 && image.GetLastDiskSequence() != 0)
{
sidecar.OpticalDisc[0].Sequence.MediaSequence = image.GetMediaSequence();
sidecar.OpticalDisc[0].Sequence.TotalMedia = image.GetMediaSequence();
}
else
{
sidecar.OpticalDisc[0].Sequence.MediaSequence = 1;
sidecar.OpticalDisc[0].Sequence.TotalMedia = 1;
}
sidecar.OpticalDisc[0].Sequence.MediaTitle = image.GetImageName();
MediaType dskType = image.ImageInfo.mediaType;
foreach(MediaTagType tagType in image.ImageInfo.readableMediaTags)
{
switch(tagType)
{
case MediaTagType.CD_ATIP:
sidecar.OpticalDisc[0].ATIP = new DumpType();
sidecar.OpticalDisc[0].ATIP.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.CD_ATIP)).ToArray();
sidecar.OpticalDisc[0].ATIP.Size = image.ReadDiskTag(MediaTagType.CD_ATIP).Length;
Decoders.CD.ATIP.CDATIP? atip = Decoders.CD.ATIP.Decode(image.ReadDiskTag(MediaTagType.CD_ATIP));
if(atip.HasValue)
{
if(atip.Value.DDCD)
dskType = atip.Value.DiscType ? MediaType.DDCDRW : MediaType.DDCDR;
else
dskType = atip.Value.DiscType ? MediaType.CDRW : MediaType.CDR;
}
break;
case MediaTagType.DVD_BCA:
sidecar.OpticalDisc[0].BCA = new DumpType();
sidecar.OpticalDisc[0].BCA.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_BCA)).ToArray();
sidecar.OpticalDisc[0].BCA.Size = image.ReadDiskTag(MediaTagType.DVD_BCA).Length;
break;
case MediaTagType.BD_BCA:
sidecar.OpticalDisc[0].BCA = new DumpType();
sidecar.OpticalDisc[0].BCA.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.BD_BCA)).ToArray();
sidecar.OpticalDisc[0].BCA.Size = image.ReadDiskTag(MediaTagType.BD_BCA).Length;
break;
case MediaTagType.DVD_CMI:
sidecar.OpticalDisc[0].CMI = new DumpType();
Decoders.DVD.CSS_CPRM.LeadInCopyright? cmi = Decoders.DVD.CSS_CPRM.DecodeLeadInCopyright(image.ReadDiskTag(MediaTagType.DVD_CMI));
if(cmi.HasValue)
{
switch(cmi.Value.CopyrightType)
{
case Decoders.DVD.CopyrightType.AACS:
sidecar.OpticalDisc[0].CopyProtection = "AACS";
break;
case Decoders.DVD.CopyrightType.CSS:
sidecar.OpticalDisc[0].CopyProtection = "CSS";
break;
case Decoders.DVD.CopyrightType.CPRM:
sidecar.OpticalDisc[0].CopyProtection = "CPRM";
break;
}
}
sidecar.OpticalDisc[0].CMI.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_CMI)).ToArray();
sidecar.OpticalDisc[0].CMI.Size = image.ReadDiskTag(MediaTagType.DVD_CMI).Length;
break;
case MediaTagType.DVD_DMI:
sidecar.OpticalDisc[0].DMI = new DumpType();
sidecar.OpticalDisc[0].DMI.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_DMI)).ToArray();
sidecar.OpticalDisc[0].DMI.Size = image.ReadDiskTag(MediaTagType.DVD_DMI).Length;
if(Decoders.Xbox.DMI.IsXbox(image.ReadDiskTag(MediaTagType.DVD_DMI)))
{
dskType = MediaType.XGD;
sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
sidecar.OpticalDisc[0].Dimensions.Diameter = 120;
}
else if(Decoders.Xbox.DMI.IsXbox360(image.ReadDiskTag(MediaTagType.DVD_DMI)))
{
dskType = MediaType.XGD2;
sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
sidecar.OpticalDisc[0].Dimensions.Diameter = 120;
}
break;
case MediaTagType.DVD_PFI:
sidecar.OpticalDisc[0].PFI = new DumpType();
sidecar.OpticalDisc[0].PFI.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_PFI)).ToArray();
sidecar.OpticalDisc[0].PFI.Size = image.ReadDiskTag(MediaTagType.DVD_PFI).Length;
Decoders.DVD.PFI.PhysicalFormatInformation? pfi = Decoders.DVD.PFI.Decode(image.ReadDiskTag(MediaTagType.DVD_PFI));
if(pfi.HasValue)
{
if(dskType != MediaType.XGD &&
dskType != MediaType.XGD2 &&
dskType != MediaType.XGD3)
{
switch(pfi.Value.DiskCategory)
{
case Decoders.DVD.DiskCategory.DVDPR:
dskType = MediaType.DVDPR;
break;
case Decoders.DVD.DiskCategory.DVDPRDL:
dskType = MediaType.DVDPRDL;
break;
case Decoders.DVD.DiskCategory.DVDPRW:
dskType = MediaType.DVDPRW;
break;
case Decoders.DVD.DiskCategory.DVDPRWDL:
dskType = MediaType.DVDPRWDL;
break;
case Decoders.DVD.DiskCategory.DVDR:
dskType = MediaType.DVDR;
break;
case Decoders.DVD.DiskCategory.DVDRAM:
dskType = MediaType.DVDRAM;
break;
case Decoders.DVD.DiskCategory.DVDROM:
dskType = MediaType.DVDROM;
break;
case Decoders.DVD.DiskCategory.DVDRW:
dskType = MediaType.DVDRW;
break;
case Decoders.DVD.DiskCategory.HDDVDR:
dskType = MediaType.HDDVDR;
break;
case Decoders.DVD.DiskCategory.HDDVDRAM:
dskType = MediaType.HDDVDRAM;
break;
case Decoders.DVD.DiskCategory.HDDVDROM:
dskType = MediaType.HDDVDROM;
break;
case Decoders.DVD.DiskCategory.HDDVDRW:
dskType = MediaType.HDDVDRW;
break;
case Decoders.DVD.DiskCategory.Nintendo:
dskType = MediaType.GOD;
break;
case Decoders.DVD.DiskCategory.UMD:
dskType = MediaType.UMD;
break;
}
if(dskType == MediaType.DVDR && pfi.Value.PartVersion == 6)
dskType = MediaType.DVDRDL;
if(dskType == MediaType.DVDRW && pfi.Value.PartVersion == 3)
dskType = MediaType.DVDRWDL;
if(dskType == MediaType.GOD && pfi.Value.DiscSize == Decoders.DVD.DVDSize.OneTwenty)
dskType = MediaType.WOD;
sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
if(dskType == MediaType.UMD)
sidecar.OpticalDisc[0].Dimensions.Diameter = 60;
else if(pfi.Value.DiscSize == Decoders.DVD.DVDSize.Eighty)
sidecar.OpticalDisc[0].Dimensions.Diameter = 80;
else if(pfi.Value.DiscSize == Decoders.DVD.DVDSize.OneTwenty)
sidecar.OpticalDisc[0].Dimensions.Diameter = 120;
}
}
break;
case MediaTagType.CD_PMA:
sidecar.OpticalDisc[0].PMA = new DumpType();
sidecar.OpticalDisc[0].PMA.Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.CD_PMA)).ToArray();
sidecar.OpticalDisc[0].PMA.Size = image.ReadDiskTag(MediaTagType.CD_PMA).Length;
break;
}
}
try
{
List sessions = image.GetSessions();
sidecar.OpticalDisc[0].Sessions = sessions != null ? sessions.Count : 1;
}
catch
{
sidecar.OpticalDisc[0].Sessions = 1;
}
List