2016-04-06 00:01:54 -07:00
using System ;
2016-04-18 16:32:17 -07:00
using System.Collections.Generic ;
2016-04-06 00:01:54 -07:00
using System.IO ;
using System.Xml ;
using SabreTools.Helper ;
namespace SabreTools
{
public class SingleGame
{
2016-04-06 13:03:25 -07:00
private static string _filename = "" ;
private static string _path = "" ;
private static bool _rename = true ;
2016-04-06 00:01:54 -07:00
public static void Main ( string [ ] args )
{
2016-04-18 20:04:38 -07:00
Console . Clear ( ) ;
2016-04-06 00:35:39 -07:00
2016-04-18 16:32:17 -07:00
// Credits take precidence over all
if ( ( new List < string > ( args ) ) . Contains ( "--credits" ) )
{
Build . Credits ( ) ;
return ;
}
2016-04-06 13:03:25 -07:00
if ( args . Length = = 0 )
2016-04-06 00:01:54 -07:00
{
2016-04-06 14:19:01 -07:00
Build . Help ( ) ;
2016-04-06 00:01:54 -07:00
return ;
}
2016-04-18 20:04:38 -07:00
// Output the title
Build . Start ( "SingleGame" ) ;
2016-04-06 00:47:28 -07:00
_filename = args [ 0 ] ;
2016-04-06 00:01:54 -07:00
2016-04-06 13:03:25 -07:00
if ( args . Length > 1 )
{
for ( int i = 1 ; i < args . Length ; i + + )
{
_path = ( args [ i ] . StartsWith ( "-r" ) ? args [ i ] . Split ( '=' ) [ 1 ] : _path ) ;
_rename = ( args [ i ] = = "-n" ? false : _rename ) ;
}
}
_path = ( _path = = "" ? Environment . CurrentDirectory : _path ) ;
2016-04-06 00:01:54 -07:00
// Take the filename, and load it as an XML document
XmlDocument doc = new XmlDocument ( ) ;
try
{
2016-04-06 00:47:28 -07:00
doc . LoadXml ( File . ReadAllText ( _filename ) ) ;
2016-04-06 00:01:54 -07:00
}
catch ( XmlException )
{
2016-04-06 00:47:28 -07:00
doc . LoadXml ( Converters . RomVaultToXML ( File . ReadAllLines ( _filename ) ) . ToString ( ) ) ;
2016-04-06 00:01:54 -07:00
}
// We all start the same
XmlNode node = doc . FirstChild ;
if ( node ! = null & & node . Name = = "xml" )
{
// Skip over everything that's not an element
while ( node . NodeType ! = XmlNodeType . Element )
{
node = node . NextSibling ;
}
}
XmlDocument tempDoc = new XmlDocument ( ) ;
XmlNode outNode = tempDoc . CreateNode ( XmlNodeType . Element , node . Name , "" ) ;
// Once we find the main body, enter it
if ( node ! = null & & node . Name = = "datafile" )
{
node = node . FirstChild ;
}
// Now here's where it differs from import
bool inGame = false ;
while ( node ! = null )
{
// If we're at a game node, add the parent node but not all the internals
2016-04-06 13:03:25 -07:00
if ( _rename & & node . NodeType = = XmlNodeType . Element & & ( node . Name = = "machine" | | node . Name = = "game" ) )
2016-04-06 00:01:54 -07:00
{
if ( ! inGame )
{
XmlElement tempNode = tempDoc . CreateElement ( node . Name ) ;
tempNode . SetAttribute ( "name" , "!" ) ;
outNode . AppendChild ( tempNode ) ;
outNode = outNode . LastChild ;
inGame = true ;
}
// Get the roms from the machine
if ( node . HasChildNodes )
{
// If this node has children, traverse the children
foreach ( XmlNode child in node . ChildNodes )
{
// If we find a rom or disk, add it
if ( child . NodeType = = XmlNodeType . Element & & ( child . Name = = "rom" | | child . Name = = "disk" ) )
{
// Take care of hex-sized files
long size = - 1 ;
if ( child . Attributes [ "size" ] ! = null & & child . Attributes [ "size" ] . Value . Contains ( "0x" ) )
{
size = Convert . ToInt64 ( child . Attributes [ "size" ] . Value , 16 ) ;
}
else if ( child . Attributes [ "size" ] ! = null )
{
size = Int64 . Parse ( child . Attributes [ "size" ] . Value ) ;
}
XmlElement tempNode = ( XmlElement ) tempDoc . ImportNode ( child , true ) ;
2016-04-06 13:03:25 -07:00
// Windows max name length is 260
string tempname = child . Attributes [ "name" ] . Value ;
int usableLength = 259 - _path . Length ;
if ( tempname . Length > usableLength )
2016-04-06 00:53:00 -07:00
{
string ext = Path . GetExtension ( tempname ) ;
2016-04-06 13:03:25 -07:00
tempname = tempname . Substring ( 0 , usableLength - ext . Length ) ;
2016-04-06 00:53:00 -07:00
tempname + = ext ;
}
tempNode . SetAttribute ( "name" , tempname ) ;
2016-04-06 00:01:54 -07:00
outNode . AppendChild ( tempNode ) ;
}
}
}
}
else
{
XmlNode tempNode = tempDoc . ImportNode ( node , true ) ;
2016-04-06 13:03:25 -07:00
if ( tempNode . Name = = "header" )
{
if ( tempNode . SelectSingleNode ( "clrmamepro" ) = = null )
{
XmlElement tempChild = tempDoc . CreateElement ( "clrmamepro" ) ;
tempChild . SetAttribute ( "forcepacking" , "unzip" ) ;
tempNode . AppendChild ( tempChild ) ;
}
else
{
( tempNode . SelectSingleNode ( "clrmamepro" ) as XmlElement ) . SetAttribute ( "forcepacking" , "unzip" ) ;
}
}
2016-04-06 00:01:54 -07:00
outNode . AppendChild ( tempNode ) ;
}
node = node . NextSibling ;
}
2016-04-06 13:03:25 -07:00
if ( inGame )
{
outNode = outNode . ParentNode ;
}
2016-04-06 00:01:54 -07:00
tempDoc . AppendChild ( tempDoc . CreateDocumentType ( "datafile" , "-//Logiqx//DTD ROM Management Datafile//EN" , "http://www.logiqx.com/Dats/datafile.dtd" , null ) ) ;
tempDoc . AppendChild ( outNode ) ;
2016-04-06 00:47:28 -07:00
string outPath = Path . GetFileNameWithoutExtension ( _filename ) + ".new" + Path . GetExtension ( _filename ) ;
2016-04-06 00:43:02 -07:00
File . WriteAllText ( outPath , Style . Beautify ( tempDoc ) ) ;
2016-04-06 00:01:54 -07:00
}
}
}