-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
1337 lines (1214 loc) · 49.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using HiGames;
using System.Net;
using System.Diagnostics;
using System.Reflection;
using HiToText.Utils;
using HiToText.HXML;
using HiToText.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.Collections.Specialized;
using System.Xml.XPath;
namespace HiToText
{
class Program
{
public static List<string> supportedGames = new List<string>();
public static HXMLReader r = new HXMLReader();
#if DEBUG
public static const string HTT_XML = @"E:\Stuff\Nick Test\SVN\hitotext\HiToText.xml";
#else
public static string HTT_XML = "/etc/sdlmame/HiToText.xml";
#endif
#region SERVER PATHS
private static string updatePath = "http://www.hitotext.com/HiToText/update.txt";
private static string versionPath = "http://www.hitotext.com/HiToText/version.txt";
#endregion
#region LEGACY DRIVERS
private static Hiscore[] m_games =
{
new _1942(),
//new galaga(),
new msword(),
//new dkong(),
new puckman(),
new mrdo(),
new mappy(),
//new asterix(),
//new digdug2(),
new jackal(),
new pulstar(),
new ddonpach(),
//new contra(),
new suprmrio(),
//new galaxian(),
new zaxxon(),
new mhavoc(),
//new ckong(),
//new dkongjr(),
new raiden(),
new marble(),
new wb3(),
//new digdug(),
//new btime(),
new gigawing(),
//new dkong3(),
new sf2(),
//new frogger(),
new gng(),
new mario(),
//new altbeast(),
new simpsons(),
new roadrunn(),
//new daioh(),
new xmen(),
//new bublbobl(),
new captaven(),
new knights(), //Added in version 11/26/08
new rtype(),
new spdodgeb(),
//new bombjack(), //Added in version 12/1/2008
new hyprduel(),
new metrocrs(),
new hyperpac(),
//new bnzabros(), //Added in version 20081212
new gijoe(),
new nitd(),
new defender(), //Added in version 20081218
new jjsquawk(),
new ccastles(),
new gunsmoke(),
//new elevator(),
new junglek(),
new tempest(), //Added in version 20081222
new sci(),
new upndown(),
//new dowild(),
//new alibaba(),
//new _1943(), //Added in version 20090106
/*new invaders(),*/ new searthin(), //new darthvdr(),
new slapfigh(),
new phoenix(),
//new dino(),
//new columns(),
//new djboy(), //Added in version 2009.1.17
new tmnt2(),
new dragnblz(),
new mrdrillr(),
//new asteroid(),
new tmnt(),
new pacmania(),
//new dorunrun(),
//new commando(),
//new dariusg(), //Added in version 2009.2.5
new outzone(),
new trackfld_090410(),
//new centiped(),
new xmvsf(),
//new cclimber(),
new milliped(),
new mooncrst(),
new mpatrol(),
//new arkanoid(),
new tetris(), //Added in version 2009.2.11
new zookeep(),
//new _005(), //Added in version 2009.2.20
//new _8ballact(),
//new aliensyn(),
new spf2t(),
new gauntlet(),
new paperboy(),
new junofrst(), //Added in version 2009.3.25
new pbaction(),
new rtype2(),
new s1945ii(),
new aquajack(),
//new balonfgt(),
//new _4dwarrio(),
//new extrmatn(),
new hvysmsh(),
new imgfight(),
//new _10yard(),
//new bullfgt(),
//new explorer(),
//new eyes(),
new kchamp(),
new scramble(),
new viofight(),
//new amidar(), //Added in version 2009.4.15
//new arknoid2(),
//new arkretrn(),
//new astdelux(),
//new baddudes(),
//new bagman(),
//new bzone(),
//new carnival(),
//new ddribble(),
//new frogs(),
//new gberet(),
new robotron(),
new sdtennis(),
//new seawolf(),
new seawolf2(),
new kungfum(), //Added in version 2009.5.29
//new circusc(),
//new ddragon(),
new pacland(),
new matmania(),
//new bankp(),
new punchout(),
new spnchout(), new spnchotj(),
new kamikcab(),
new rastan(),
new robocop(),
//new blktiger(),
new wrally(),
new vigilant(),
new srumbler(),
new wboy(),
new yiear(),
new vendetta(),
new twincobr(),
new popeye(), //Added in version 2009.6.19
new silkworm(),
new esprade(),
new gyruss(),
new timeplt(),
new jungler(),
new ladybug(),
//new brubber(),
new shadoww(),
new trackfld(), //Added in version 2009.6.29
new rygar(),
//new bloodbro(),
//new blueprnt(),
//new crush(),
//new bjtwin(),
new steelwkr(),
//new _1941(),
new klax(),
new terracre(),
new rthunder(),
new astyanax(),
new turfmast(),
new kangaroo(), //Added in version 2009.7.1
new swimmer(),
//new _1944(),
new troangel(),
//new exprraid(), new exprrada(),
new pooyan(), //Added in version 2009.7.14
new rpatrol(),
new joust(),
new joust2(),
new headon(), new headon2(), new headoni(),
//new crzrally(),
//new cabal(),
//new dotron(), new dotrone(),
new toki(),
new tron(),
new narc(), //Added in version 2009.8.4
new unsquad(),
//new ajax(),
new qbert(),
new mk(), new mkla1(),
new armora(),
new boxingb(),
new ripoff(),
new speedfrk(),
new starcas(),
new tailg(),
new barrier(), //Added in version 2009.8.5
//new batsugun(),
//new battlcry(),
//new chasehq(),
new demon(),
new pengo(),
new qb3(),
new solarq(),
new sundance(),
new wotw(),
new woodpeck(), //Added in version 2009.9.1
new nmouse(),
new journey(),
new tapper(),
new timber(),
new domino(),
new mk2(),
new berzerk(),
new _19xx(),
//new blockout(), //Added in version 2009.9.16
//new brkthru(),
//new cleopatr(),
new dazzler(),
//new deadeye(),
//new dynagear(),
new lrescue(),
new madalien(),
new megadon(),
new mimonkey(),
new missile(), new missile2(),
new mslug(),
new mslug2(),
new mslug3(), new mslug4(), new mslug5(),
new mslugx(),
new mtlchamp(),
new mwalk(),
new naughtyb(),
new nemesis(),
new nova2001(),
new olibochu(),
new omegaf(), new omegafs(),
new qbertqub(),
new starwars(),
new streakng(),
new subroc3d(),
//new boomrang(),
//new evilston(),
new pirates(),
new redbaron(),
new sprint1(),
new hyperspt() //Added in version 2010.1.26
};
#endregion
private static string GetFileNameWithoutExtension(string fileName){
int initNameIndex = fileName.LastIndexOf(System.IO.Path.DirectorySeparatorChar)+1;
int termDotIndex = fileName.LastIndexOf(".");
return fileName.Substring(initNameIndex, termDotIndex - initNameIndex);
}
private static void Initialize(ConsoleFlags flag, string romName)
{
XPathDocument docNav = new XPathDocument(HTT_XML);
XPathNavigator nav = docNav.CreateNavigator();
string strExpression =
string.Format("/HiToText/Entry[Header/Games/Name=\"{0}\"]", romName);
XPathNodeIterator NodeIter = nav.Select(strExpression);
NodeIter.MoveNext();
if(NodeIter.Current != null)
{
r = new HXMLReader(NodeIter.Current);
}
else
{
//Load up XML file
#if DEBUG
r = new HXMLReader(HTT_XML);
#else
if (File.Exists(HTT_XML))
r = new HXMLReader(HTT_XML);
#endif
}
supportedGames = r.GetSupportedGames();
}
static void Main(string[] args)
{
#if DEBUG_TIME
//TimingTest();
TimingTestCompare();
return;
#endif
#if DEBUG
DateTime tStart = DateTime.Now;
string dRom = "4dwarrio";
#endif
bool isLegacyStart = false;
string cmdLine = string.Empty;
CsvParser parser = new CsvParser();
parser.separator = ' ';
#if DEBUG_READ
cmdLine = string.Format(@"-ra ""E:\Stuff\Nick Test\hi\{0}.hi""", dRom);
#elif DEBUG_LIST
cmdLine = string.Format(@"-lp ""E:\Stuff\Nick Test\mame.exe""");
#elif DEBUG_WRITE
string dToWrite = "1 62000 NL";
cmdLine = string.Format(@"-w ""E:\Stuff\Nick Test\hi\{0}.hi"" {1}", dRom, dToWrite);
#endif
if (args.Length > 0)
isLegacyStart = true;
if (isLegacyStart)
{
#if !DEBUG
cmdLine = "\"" + string.Join("\" \"", args) + "\"";
#endif
StringCollection commands = parser.ParseCsv(cmdLine);
//Put anything that would only need to be run once in the initialize function.
#if DEBUG_READ
Initialize(ConsoleFlags.ReadAll, dRom);
#else
if (commands.Count > 1)
Initialize(GetFlagFromString(commands[0].ToLower()), GetFileNameWithoutExtension(commands[1]));
else
Initialize(ConsoleFlags.None, string.Empty);
PerformCommand(commands);
return;
#endif
}
else
Initialize(ConsoleFlags.None, string.Empty);
#if DEBUG
DateTime tAIStart = DateTime.Now;
#endif
Console.WriteLine("HiToText initialized, please enter command. (-h for help)");
Console.Write(">");
#if DEBUG
if (true)
{
#else
while (!(cmdLine = Console.ReadLine()).Equals("-q"))
{
#endif
PerformCommand(parser.ParseCsv(cmdLine));
#if DEBUG
DateTime tAIEnd = DateTime.Now;
double totalAITimems = tAIEnd.Subtract(tAIStart).TotalMilliseconds;
Console.WriteLine(string.Format("Inner operation took {0}ms to complete.", totalAITimems.ToString("#.00#")));
#else
Console.Write(">");
#endif
}
Console.WriteLine("Exitting HiToText.");
#if DEBUG
DateTime tEnd = DateTime.Now;
double totalTimems = tEnd.Subtract(tStart).TotalMilliseconds;
Console.WriteLine(string.Format("Operation took {0}ms to complete.", totalTimems.ToString("#.00#")));
#endif
}
private static void PerformCommand(StringCollection cmdLineArgs)
{
ConsoleFlags flag = ConsoleFlags.None;
string fileName = null;
string romName = null;
string mameFolder = String.Empty;
List<string> scoreData = new List<string>();
Hiscore game = null;
if (cmdLineArgs.Count == 0)
{
WriteUsage();
return;
}
flag = GetFlagFromString(cmdLineArgs[0].ToLower());
if (cmdLineArgs.Count >= 2 &&
!flag.Equals(ConsoleFlags.Update) &&
!flag.Equals(ConsoleFlags.ListParents) &&
!flag.Equals(ConsoleFlags.Version))
{
fileName = Path.GetFullPath(
Path.GetExtension(cmdLineArgs[1]) == String.Empty ? cmdLineArgs[1] + ".hi" : cmdLineArgs[1])
.Replace('/', System.IO.Path.DirectorySeparatorChar)
.Replace('\\', System.IO.Path.DirectorySeparatorChar);
try
{
mameFolder = GetMAMEFolderFromFileName(fileName);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.WriteLine(String.Format("Error: MAME directory could not be inferred from '{0}'", fileName));
}
//romName = Path.GetFileNameWithoutExtension(fileName);
//Removed due to use of Windows-specific path assumption
romName = GetFileNameWithoutExtension(fileName);
List<string> possibleFileNames = GetPossibleFileNames(mameFolder, romName);
//foreach (string i in possibleFileNames){Console.WriteLine(i);}
bool isVersionSpecified = false;
DateTime dVersion = DateTime.Now;
int argCounter = 0;
foreach (string argument in cmdLineArgs)
{
if (argument.Equals("-v"))
{
if (cmdLineArgs.Count >= argCounter + 1)
{
Console.WriteLine("Error: Version flag found without version date.");
return;
}
try
{
dVersion = Convert.ToDateTime(cmdLineArgs[argCounter + 1]);
}
#if DEBUG
catch (Exception ex)
#else
catch
#endif
{
Console.WriteLine("Error: Could not format version date, please use MMDDYYYY format.");
#if DEBUG
Console.WriteLine(Environment.NewLine + ex.Message);
#endif
return;
}
isVersionSpecified = true;
}
argCounter++;
}
if (!isVersionSpecified && IsVersionRequired(romName))
dVersion = GetMAMEVersionAsDate(mameFolder);
if (DoesAFileNameExist(possibleFileNames) && !File.Exists(fileName))
{
Console.WriteLine(String.Format("Error: File Not Found '{0}'", fileName));
return;
}
if (supportedGames.Contains(romName))
{
game = new General(r.GetEntry(romName));
game.FileNames = GetFileNamesFromGame(mameFolder, romName, game);
}
else
{
if (!TryGetGame(romName, mameFolder, dVersion, out game))
{
Console.WriteLine(String.Format("Error: ROM Not Supported '{0}'", romName));
return;
}
}
}
switch (flag)
{
#region READ
case ConsoleFlags.Read:
if (game != null)
{
try
{
Console.WriteLine(RemoveAlternates(game.HiToString()));
}
catch (Exception ex)
{
Console.WriteLine("Read Error: " + ex.Message);
}
}
else
Console.WriteLine("Error: No ROM Specified");
break;
#endregion
#region READ ALL
case ConsoleFlags.ReadAll:
if (game != null)
try
{
Console.WriteLine(game.HiToString());
}
catch (Exception ex)
{
Console.WriteLine("ReadAll Error: " + ex.Message);
}
else
Console.WriteLine("Error: No ROM Specified");
break;
#endregion
#region WRITE
case ConsoleFlags.Write:
if (game != null)
{
for (int i = 2; i < cmdLineArgs.Count; i++)
scoreData.Add(cmdLineArgs[i]);
#if DEBUG
if (true)
#else
if (scoreData.Count == game.FieldCount)
#endif
{
try
{
game.SetHiScore(scoreData.ToArray());
game.SaveData();
}
catch (Exception ex)
{
Console.WriteLine("Write Error: " + ex.Message);
Console.WriteLine("In: " + ex.StackTrace);
}
}
#if DEBUG
#else
else
Console.WriteLine(String.Format("Error: Expecting {0} Entries", game.FieldCount));
#endif
}
else
Console.WriteLine("Error: No ROM Specified");
break;
#endregion
#region WRITE ALTERNATE
case ConsoleFlags.WriteAlternate:
if (game != null)
{
if (game.NumAltScores == 0)
Console.WriteLine(String.Format("Error: {0} Does Not Contain Any Alternate Scores", romName));
else
{
int gameLoc = -1;
for (int i = 2; i < cmdLineArgs.Count; i++)
scoreData.Add(cmdLineArgs[i]);
for (int i = 0; i < game.NumAltScores; i++)
{
if (cmdLineArgs[2].ToUpper().Equals(game.AltFormat[i].Substring(0, game.AltFormat[i].IndexOf(Environment.NewLine))))
{
gameLoc = i;
break;
}
}
if (gameLoc == -1)
Console.WriteLine(String.Format("Error: \"{0}\" Does Not Match Any Known Alternate Score Names", cmdLineArgs[2]));
else
{
if ((scoreData.Count - 1) == game.AltFieldCount[gameLoc])
{
try
{
game.SetAlternateScore(scoreData.ToArray());
game.SaveData();
}
catch (Exception ex)
{
Console.WriteLine("Write Alt Error: " + ex.Message);
}
}
else
Console.WriteLine(String.Format("Error: Expecting {0} Entries For Alternate Score \"{1}\"", game.AltFieldCount[gameLoc], game.AltScoreName[gameLoc]));
}
}
}
else
Console.WriteLine("Error: No ROM Specified");
break;
#endregion
#region FORMAT
case ConsoleFlags.Format:
if (game != null)
Console.WriteLine(game.Format);
else
Console.WriteLine("Error: No ROM Specified");
break;
#endregion
#region FORMAT ALTERNATE
case ConsoleFlags.FormatAlternate:
if (game != null)
{
if (game.NumAltScores == 0)
Console.WriteLine(String.Format("Error: {0} Does Not Contain Any Alternate Scores", romName));
else
{
for (int i = 0; i < game.AltFormat.Length; i++)
Console.WriteLine(game.AltFormat[i].Replace(Environment.NewLine, "|"));
}
}
else
Console.WriteLine("Format Alt Error: No ROM Specified");
break;
#endregion
#region LIST
case ConsoleFlags.List:
try
{
ListGamesSupported();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("List Error: {0}", ex.Message));
}
break;
#endregion
#region LIST PARENTS
case ConsoleFlags.ListParents:
try
{
ListParentGamesSupported(cmdLineArgs[1]);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("List Parents Error: {0}", ex.Message));
}
break;
#endregion
#region UPDATE
case ConsoleFlags.Update:
try
{
Version latestVersion = new Version(ReadTxtFrom(versionPath, cmdLineArgs[1]));
if (GetVersion().CompareTo(latestVersion) < 0)
Console.WriteLine(ReadTxtFrom(updatePath, cmdLineArgs[1]));
else
Console.WriteLine("No newer version available.");
}
catch (Exception ex)
{
Console.WriteLine("Update Error: " + ex.Message);
}
break;
#endregion
#region VERSION
case ConsoleFlags.Version:
if (cmdLineArgs.Count < 2)
{
Console.WriteLine("Error: Not enough command line arguments. (-h for help)");
return;
}
try
{
Console.WriteLine(GetMAMEVersionAsDate(cmdLineArgs[1]).ToString("MMddyyyy"));
}
catch (Exception ex)
{
Console.WriteLine("Version Error: " + ex.Message);
}
break;
#endregion
#region ERASE
case ConsoleFlags.Erase:
try
{
if (game != null)
{
game.EmptyScores();
//SaveData is now done in the general hiscoreData.
//game.SaveData();
}
else
Console.WriteLine("Error: No ROM Specified");
}
catch (Exception ex)
{
Console.WriteLine("Erase Error: " + ex.Message);
}
break;
#endregion
#region MODIFY
case ConsoleFlags.Modify:
try
{
if (game != null)
{
game.ModifyName(Convert.ToInt32(cmdLineArgs[3]), cmdLineArgs[4]);
game.SaveData();
}
else
Console.WriteLine("Error: No ROM Specified");
}
catch (Exception ex)
{
Console.WriteLine("Modify Error: " + ex.Message);
}
break;
#endregion
#region HELP
case ConsoleFlags.Help:
WriteUsage();
break;
#endregion
}
}
private static bool DoesAFileNameExist(List<string> possibleFileNames)
{
foreach (string fileName in possibleFileNames)
{
if (File.Exists(fileName))
return true;
}
return false;
}
//TODO: Find a smarter way to contain this information rather than hardcoding.
//TODO: Add save state file names, and locations.
private static List<string> GetPossibleFileNames(string mameFolder, string romName)
{
List<string> toReturn = new List<string>();
//toReturn.Add(Path.Combine(Path.Combine(mameFolder, "hi"), romName + ".hi"));
//toReturn.Add(Path.Combine(Path.Combine(mameFolder, "nvram"), romName + ".nv"));
//Removed due to use of Windows-specific path format assumptions
toReturn.Add(mameFolder + System.IO.Path.DirectorySeparatorChar + "hi" + System.IO.Path.DirectorySeparatorChar + romName + ".hi");
toReturn.Add(mameFolder + System.IO.Path.DirectorySeparatorChar + "nvram" + System.IO.Path.DirectorySeparatorChar + romName + ".nv");
return toReturn;
}
private static bool IsVersionRequired(string romName)
{
foreach (Hiscore h in m_games)
{
foreach (string gName in h.GamesSupported)
{
if (romName.Equals(gName))
{
if (h.GetVersionDate.Equals(DateTime.MaxValue))
return false;
else
return true;
}
}
}
return false;
}
private static string[] GetFileNamesFromGame(string mameFolder, string romName, Hiscore game)
{
List<string> possibleFileNames = GetPossibleFileNames(mameFolder, romName);
string[] extReq = game.ExtensionsRequired;
string[] fileNameArray = new String[extReq.Length];
for (int x = 0; x < extReq.Length; x++)
{
foreach (string pfn in possibleFileNames)
{
if (extReq[x].Equals(Path.GetExtension(pfn)))
{
if (File.Exists(pfn))
fileNameArray[x] = pfn;
else
fileNameArray[x] = romName + extReq[x];
break;
}
}
}
return fileNameArray;
}
//TODO: Throw save states in here.
//UPDATED: Now uses Path.IO.DirSepChar to be cross-platform safe
private static string GetMAMEFolderFromFileName(string fileName)
{
string mameFolder = string.Empty;
string hiSubStr = System.IO.Path.DirectorySeparatorChar + "hi";
string nvSubStr = System.IO.Path.DirectorySeparatorChar + "nvram";
if (fileName.IndexOf(hiSubStr) == -1)
{
//No longer throw an exception due to not finding the MAME directory.
if (fileName.IndexOf(nvSubStr) == -1)
//throw new Exception(String.Format("Error: MAME directory could not be inferred from '{0}'", fileName));
return mameFolder;
else
mameFolder = Path.GetDirectoryName(fileName).Substring(0, fileName.IndexOf(nvSubStr));
}
else{
mameFolder = fileName.Substring(0, fileName.IndexOf(hiSubStr));}
return mameFolder;
}
private static ConsoleFlags GetFlagFromString(string flag)
{
switch (flag.ToLower())
{
case "-r":
return ConsoleFlags.Read;
case "-ra":
return ConsoleFlags.ReadAll;
case "-w":
return ConsoleFlags.Write;
case "-wa":
return ConsoleFlags.WriteAlternate;
case "-f":
return ConsoleFlags.Format;
case "-fa":
return ConsoleFlags.FormatAlternate;
case "-l":
return ConsoleFlags.List;
case "-lp":
return ConsoleFlags.ListParents;
case "-u":
return ConsoleFlags.Update;
case "-v":
return ConsoleFlags.Version;
case "-e":
return ConsoleFlags.Erase;
case "-m":
return ConsoleFlags.Modify;
case "-h":
default:
return ConsoleFlags.Help;
}
}
private static bool TryGetGame(string romName, string mameFolder, DateTime versionAllowed, out Hiscore game)
{
game = null;
List<Hiscore> validGames = new List<Hiscore>();
for (int i = 0; i < m_games.Length; i++)
{
string[] gamesSupported = m_games[i].GamesSupported;
for (int j = 0; j < gamesSupported.Length; j++)
{
if (gamesSupported[j] == romName)
validGames.Add(m_games[i]);
}
}
DateTime oldestValid = DateTime.Now;
foreach (Hiscore gm in validGames)
{
if ((gm.GetVersionDate.CompareTo(oldestValid) <= 0 &&
gm.GetVersionDate.CompareTo(versionAllowed) >= 0) ||
gm.GetVersionDate.Equals(DateTime.MaxValue))
{
game = gm;
oldestValid = gm.GetVersionDate;
}
}
if (game != null)
{
game.FileNames = GetFileNamesFromGame(mameFolder, romName, game);
return true;
}
return false;
}
private static List<string> GetSupportedGames()
{
List<string> gameArray = new List<string>();
for (int i = 0; i < m_games.Length; i++)
gameArray.AddRange(m_games[i].GamesSupported);
gameArray.AddRange(supportedGames);
gameArray.Sort();
return gameArray;
}
private static void ListGamesSupported()
{
List<string> gameArray = GetSupportedGames();
for (int i = 0; i < gameArray.Count; i++)
Console.WriteLine(gameArray[i]);
}
private static void ListParentGamesSupported(string mameExe)
{
List<string> gameArray = GetSupportedGames();
mame xMAME = new mame();
XmlSerializer xSerial = new XmlSerializer(typeof(mame));
#if DEBUG_CHILLIN
Console.WriteLine("DEBUG: MAME serializer initialized.");
#endif
byte[] bytes = Encoding.ASCII.GetBytes(String.Join(Environment.NewLine, GetMAMEFullXML(mameExe).ToArray()));
MemoryStream ms = new MemoryStream(bytes);
#if DEBUG_CHILLIN
Console.WriteLine(string.Format("DEBUG: {0} bytes long.", bytes.Length));
#endif
if (ms == null)
{
Console.WriteLine(string.Format("{0} is not a valid mame.exe file. HiToText cannot list parents supported without a valid mame.exe.", mameExe));
return;
}
using (XmlReader reader = XmlReader.Create(ms))
{
xMAME = (mame)xSerial.Deserialize(reader);
reader.Close();
bytes = new byte[0];
ms.Close();
}
#if DEBUG_CHILLIN
Console.WriteLine("DEBUG: MAME serializer utilized successfully.");
#endif
List<string> supportedParents = new List<string>();
foreach (string game in gameArray)
{
foreach (mameGame mg in xMAME.game)
{
if (mg.name.Equals(game))
{
if (mg.cloneof == null)
supportedParents.Add(game);
break;
}
}
}
supportedParents.Sort();
for (int i = 0; i < supportedParents.Count; i++)
Console.WriteLine(supportedParents[i]);
}
private static List<string> GetMAMEFullXML(string mameExe)
{
string info = ShellMAME(mameExe, "-listxml");
string[] lines = info.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);