Skip to content

Commit ef88450

Browse files
committed
Use Xb2FileSystem for tasks
1 parent 49e3dd3 commit ef88450

File tree

9 files changed

+63
-29
lines changed

9 files changed

+63
-29
lines changed

XbTool/XbTool/Bdat/BdatTables.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using System.Text.RegularExpressions;
77
using LibHac;
8+
using LibHac.IO;
89
using XbTool.CodeGen;
910
using XbTool.Common;
1011

@@ -22,7 +23,7 @@ public class BdatTables
2223
public BdatTableDesc[] TableDesc { get; set; }
2324
public byte[] FileData { get; }
2425

25-
public BdatTables(IFileReader fs, bool readMetadata, IProgressReport progress = null, string lang = "gb")
26+
public BdatTables(IFileSystem fs, bool readMetadata, IProgressReport progress = null, string lang = "gb")
2627
{
2728
Game = Game.XB2;
2829
progress?.LogMessage("Reading BDAT files");
@@ -82,15 +83,15 @@ private void ReadMetadata()
8283
MarkFlagMembers();
8384
}
8485

85-
public static BdatTable[] ReadAllBdats(IFileReader fs, IProgressReport progress = null, string lang = "gb")
86+
public static BdatTable[] ReadAllBdats(IFileSystem fs, IProgressReport progress = null, string lang = "gb")
8687
{
8788
var tables = new List<BdatTable>();
8889

8990
tables.AddRange(ReadBdatFile(new DataBuffer(fs.ReadFile("/bdat/common.bdat"), Game.XB2, 0), "/bdat/common.bdat"));
9091
tables.AddRange(ReadBdatFile(new DataBuffer(fs.ReadFile("/bdat/common_gmk.bdat"), Game.XB2, 0), "/bdat/common_gmk.bdat"));
9192
tables.AddRange(ReadBdatFile(new DataBuffer(fs.ReadFile("/bdat/lookat.bdat"), Game.XB2, 0), "/bdat/lookat.bdat"));
9293

93-
string[] files = fs.FindFiles($"/bdat/{lang}/*").ToArray();
94+
string[] files = fs.OpenDirectory($"/bdat/{lang}", OpenDirectoryMode.Files).Read().Select(x => x.FullPath).ToArray();
9495
progress?.SetTotal(files.Length);
9596

9697
foreach (string filename in files)

XbTool/XbTool/CliArguments.cs

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ public static Options Parse(string[] args)
117117
options.Xb2Dir = args[i + 1];
118118
i++;
119119
continue;
120+
case "-SD":
121+
if (i + 1 >= args.Length)
122+
{
123+
PrintWithUsage("No argument after -sd option.");
124+
return null;
125+
}
126+
127+
options.SdPath = args[i + 1];
128+
i++;
129+
continue;
120130
}
121131
}
122132

XbTool/XbTool/Common/Helpers.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using LibHac.IO;
34

45
namespace XbTool.Common
56
{
@@ -72,5 +73,14 @@ public static int GetNextMultiple(int value, int multiple)
7273

7374
return value + multiple - value % multiple;
7475
}
76+
77+
public static byte[] ReadFile(this IFileSystem fs, string path)
78+
{
79+
IFile file = fs.OpenFile(path, OpenMode.Read);
80+
var fileArr = new byte[file.GetSize()];
81+
file.Read(fileArr, 0);
82+
83+
return fileArr;
84+
}
7585
}
7686
}

XbTool/XbTool/Gimmick/ExportMap.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing.Imaging;
55
using System.IO;
66
using System.Text;
7+
using LibHac.IO;
78
using XbTool.Common;
89
using XbTool.Common.Textures;
910
using XbTool.Xb2.Textures;
@@ -12,7 +13,7 @@ namespace XbTool.Gimmick
1213
{
1314
public static class ExportMap
1415
{
15-
public static void Export(IFileReader fs, MapInfo[] gimmicks, string outDir)
16+
public static void Export(IFileSystem fs, MapInfo[] gimmicks, string outDir)
1617
{
1718
Directory.CreateDirectory(Path.Combine(outDir, "png"));
1819

XbTool/XbTool/Gimmick/MapInfo.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Linq;
5+
using LibHac.IO;
46
using XbTool.Common;
57

68
namespace XbTool.Gimmick
@@ -61,10 +63,11 @@ public MapAreaInfo GetContainingArea(Point3 point)
6163
return containingArea;
6264
}
6365

64-
public static Dictionary<string, MapInfo> ReadAll(IFileReader fs)
66+
public static Dictionary<string, MapInfo> ReadAll(IFileSystem fs)
6567
{
6668
var infos = new Dictionary<string, MapInfo>();
67-
IEnumerable<string> filenames = fs.FindFiles("/menu/minimap/*.mi");
69+
IEnumerable<string> filenames = fs.OpenDirectory("/menu/minimap", OpenDirectoryMode.Files)
70+
.EnumerateEntries("*.mi", SearchOptions.Default).Select(x => x.FullPath);
6871

6972
foreach (string filename in filenames)
7073
{

XbTool/XbTool/Gimmick/ReadGmk.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Reflection;
44
using LibHac;
5+
using LibHac.IO;
56
using XbTool.Bdat;
67
using XbTool.Common;
78
using XbTool.Types;
@@ -10,7 +11,7 @@ namespace XbTool.Gimmick
1011
{
1112
public static class ReadGmk
1213
{
13-
public static MapInfo[] ReadAll(IFileReader fs, BdatCollection tables, IProgressReport progress = null)
14+
public static MapInfo[] ReadAll(IFileSystem fs, BdatCollection tables, IProgressReport progress = null)
1415
{
1516
progress?.LogMessage("Reading map info and gimmick sets");
1617
Dictionary<string, MapInfo> maps = MapInfo.ReadAll(fs);
@@ -65,7 +66,7 @@ public static MapInfo[] ReadAll(IFileReader fs, BdatCollection tables, IProgress
6566
return maps.Values.ToArray();
6667
}
6768

68-
public static Dictionary<string, Lvb> ReadGimmickSet(IFileReader fs, BdatCollection tables, int mapId)
69+
public static Dictionary<string, Lvb> ReadGimmickSet(IFileSystem fs, BdatCollection tables, int mapId)
6970
{
7071
RSC_GmkSetList setBdat = tables.RSC_GmkSetList.First(x => x.mapId == mapId);
7172
Dictionary<string, FieldInfo> fieldsDict = setBdat.GetType().GetFields().ToDictionary(x => x.Name, x => x);
@@ -77,7 +78,7 @@ public static Dictionary<string, Lvb> ReadGimmickSet(IFileReader fs, BdatCollect
7778
string value = (string)field.GetValue(setBdat);
7879
if (value == null) continue;
7980
string filename = $"/gmk/{value}.lvb";
80-
if (!fs.Exists(filename)) continue;
81+
if (!fs.FileExists(filename)) continue;
8182

8283
byte[] file = fs.ReadFile(filename);
8384
var lvb = new Lvb(new DataBuffer(file, Game.XB2, 0)) { Filename = field.Name };

XbTool/XbTool/Options.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Options
1414
public string Output { get; set; }
1515
public string Filter { get; set; }
1616
public string Xb2Dir { get; set; }
17+
public string SdPath { get; set; }
1718
public IProgressReport Progress { get; set; }
1819
}
1920

XbTool/XbTool/Tasks.cs

+24-17
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,20 @@ private static BdatTables ReadBdatTables(Options options, bool readMetadata)
163163
{
164164
if (options.Game == Game.XB2 && options.ArdFilename != null)
165165
{
166-
using (var archive = new FileArchive(options.ArhFilename, options.ArdFilename))
166+
using (var headerFile = new LocalFile(options.ArhFilename, OpenMode.Read))
167+
using (var dataFile = new LocalFile(options.ArdFilename, OpenMode.Read))
167168
{
169+
var archive = new ArchiveFileSystem(headerFile, dataFile);
168170
return new BdatTables(archive, readMetadata);
169171
}
170172
}
171173

174+
if (options.Game == Game.XB2 && options.SdPath != null)
175+
{
176+
var archive = new Xb2FileSystem(options.SdPath);
177+
return new BdatTables(archive, readMetadata);
178+
}
179+
172180
string pattern = options.Filter ?? "*";
173181
string[] filenames = Directory.GetFiles(options.BdatDir, pattern);
174182
return new BdatTables(filenames, options.Game, readMetadata);
@@ -189,7 +197,7 @@ public static BdatCollection GetBdatCollection(Options options)
189197
return tables;
190198
}
191199

192-
public static BdatCollection GetBdatCollection(IFileReader fs, bool readMetadata)
200+
public static BdatCollection GetBdatCollection(IFileSystem fs, bool readMetadata)
193201
{
194202
var bdats = new BdatTables(fs, readMetadata);
195203
BdatCollection tables = Deserialize.DeserializeTables(bdats);
@@ -353,19 +361,18 @@ private static void CombineBdat(Options options)
353361

354362
private static void ReadGimmick(Options options)
355363
{
356-
using (var xb2Fs = new Xb2Fs(options.Xb2Dir))
357-
{
358-
if (options.Xb2Dir == null) throw new NullReferenceException("Must specify XB2 Directory.");
359-
if (options.Output == null) throw new NullReferenceException("No output path was specified.");
360-
if (!Directory.Exists(options.Xb2Dir)) throw new DirectoryNotFoundException($"{options.Xb2Dir} is not a valid directory.");
364+
if (options.SdPath == null) throw new NullReferenceException("Must specify SD card path.");
365+
if (options.Output == null) throw new NullReferenceException("No output path was specified.");
366+
if (!Directory.Exists(options.SdPath)) throw new DirectoryNotFoundException($"{options.SdPath} is not a valid directory.");
361367

362-
BdatCollection tables = GetBdatCollection(xb2Fs, false);
368+
var xb2Fs = new Xb2FileSystem(options.SdPath);
363369

364-
MapInfo[] gimmicks = ReadGmk.ReadAll(xb2Fs, tables);
365-
ExportMap.ExportCsv(gimmicks, options.Output);
370+
BdatCollection tables = GetBdatCollection(xb2Fs, false);
366371

367-
ExportMap.Export(xb2Fs, gimmicks, options.Output);
368-
}
372+
MapInfo[] gimmicks = ReadGmk.ReadAll(xb2Fs, tables);
373+
ExportMap.ExportCsv(gimmicks, options.Output);
374+
375+
ExportMap.Export(xb2Fs, gimmicks, options.Output);
369376
}
370377

371378
private static void ReadScript(Options options)
@@ -421,15 +428,15 @@ private static void ExtractMinimap(Options options)
421428

422429
private static void GenerateSite(Options options)
423430
{
424-
if (options.Xb2Dir == null) throw new NullReferenceException("Must specify XB2 Directory.");
431+
if (options.SdPath == null) throw new NullReferenceException("Must specify SD card path.");
425432
if (options.Output == null) throw new NullReferenceException("No output path was specified.");
426433
if (!Directory.Exists(options.Xb2Dir)) throw new DirectoryNotFoundException($"{options.Xb2Dir} is not a valid directory.");
427434

428435
options.Progress.LogMessage("Reading XB2 directories");
429-
using (var xb2Fs = new Xb2Fs(options.Xb2Dir))
430-
{
431-
Website.Generate.GenerateSite(xb2Fs, options.Output, options.Progress);
432-
}
436+
var xb2Fs = new Xb2FileSystem(options.SdPath);
437+
438+
Website.Generate.GenerateSite(xb2Fs, options.Output, options.Progress);
439+
433440
}
434441

435442
private static void ExportQuests(Options options)

XbTool/XbTool/Website/Generate.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.IO;
22
using LibHac;
3+
using LibHac.IO;
34
using XbTool.Bdat;
45
using XbTool.BdatString;
5-
using XbTool.Common;
66
using XbTool.Gimmick;
77
using XbTool.Salvaging;
88
using XbTool.Serialization;
@@ -18,13 +18,13 @@ public static class Generate
1818
private const string DataDir = "data";
1919
private const string GmkDir = "gimmick";
2020

21-
public static void GenerateSite(IFileReader fs, string outDir, IProgressReport progress)
21+
public static void GenerateSite(IFileSystem fs, string outDir, IProgressReport progress)
2222
{
2323
Directory.CreateDirectory(outDir);
2424
GenerateBdatHtml(fs, outDir, progress);
2525
}
2626

27-
public static void GenerateBdatHtml(IFileReader fs, string outDir, IProgressReport progress)
27+
public static void GenerateBdatHtml(IFileSystem fs, string outDir, IProgressReport progress)
2828
{
2929
var bdats = new BdatTables(fs, true, progress);
3030
BdatStringCollection tablesStr = DeserializeStrings.DeserializeTables(bdats, progress);

0 commit comments

Comments
 (0)