Skip to content

Commit

Permalink
Copy gameInfo.db on build for windows builds - #46
Browse files Browse the repository at this point in the history
Do a case-insensitive search for resource files -  #48
Return a non-zero error when exiting after an error occurs - #49
  • Loading branch information
RupertAvery committed Jul 9, 2023
1 parent d686207 commit 9a7f2d7
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 76 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ build-osx:

build-gui-win-x64:
dotnet publish ./PSXPackagerGUI/PSXPackagerGUI.csproj -c Release -r win-x64 -o ./build/PsxPackagerGUI $(SELF_CONTAINED_PROPERTIES) $(WINDOWS_PROPERTIES)
cp ./Popstation.Database/Resources/gameInfo.db ./build/PsxPackagerGUI/Resources/gameInfo.db

build-win-x64:
$(eval RID := $(subst build-,,$(@)))
dotnet publish ./PSXPackager/PSXPackager-windows.csproj -c Release -r ${RID} -o ./build/${RID} $(SELF_CONTAINED_PROPERTIES) $(WINDOWS_PROPERTIES)
cp ./Popstation.Database/Resources/gameInfo.db ./build/${RID}/Resources/gameInfo.db
cp README.md ./build/${RID}

build-linux-x64 build-linux-arm build-linux-arm64 build-osx-x64 build-osx-arm64:
Expand Down
2 changes: 2 additions & 0 deletions PSXPackager/ConsoleNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public void Notify(PopstationEventEnum @event, object value)
break;

case PopstationEventEnum.ConvertComplete:
break;

case PopstationEventEnum.ExtractComplete:
case PopstationEventEnum.DiscComplete:
case PopstationEventEnum.DecompressComplete:
Expand Down
33 changes: 26 additions & 7 deletions PSXPackager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

namespace PSXPackager
{
public static class Results
{
public const int OK = 0;
public const int INVALID_INPUT = -1;
public const int CANCELLED = -2;
public const int ERROR = -3;
}

class Program
{
private static CancellationTokenSource _cancellationTokenSource;
Expand All @@ -26,8 +34,10 @@ static void CancelEventHandler(object sender, ConsoleCancelEventArgs args)
args.Cancel = true;
}

static void Main(string[] args)
static int Main(string[] args)
{
var result = 0;

_cancellationTokenSource = new CancellationTokenSource();

Console.CancelKeyPress += CancelEventHandler;
Expand Down Expand Up @@ -164,8 +174,10 @@ static void Main(string[] args)
ResourceRoot = o.ResourceRoot,
};

ProcessFiles(options);
result = ProcessFiles(options);
});

return result;
}

private static IEnumerable<string> GetFilesFromDirectory(string path, string filterExpression, bool recursive)
Expand Down Expand Up @@ -229,8 +241,10 @@ private static bool PathIsDirectory(string path)
return (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory;
}

private static void ProcessFiles(ProcessOptions options)
private static int ProcessFiles(ProcessOptions options)
{
var result = 0;

var eventHandler = new EventHandler();
var notifier = new AggregateNotifier();

Expand All @@ -252,6 +266,7 @@ private static void ProcessFiles(ProcessOptions options)
if (options.Files.Count == 0)
{
notifier.Notify(PopstationEventEnum.Error, "No files matched!");
result = Results.ERROR;
}
else if (options.Files.Count > 1)
{
Expand All @@ -273,16 +288,17 @@ private static void ProcessFiles(ProcessOptions options)
notifier.Notify(PopstationEventEnum.Info, $"Processing {i} of {options.Files.Count}");
notifier.Notify(PopstationEventEnum.FileName, $"Processing {file}");

var result = processing.ProcessFile(file,
var processResult = processing.ProcessFile(file,
options,
_cancellationTokenSource.Token);

if (_cancellationTokenSource.Token.IsCancellationRequested || eventHandler.Cancelled)
{
result = Results.CANCELLED;
break;
}

processed += result ? 1 : 0;
processed += processResult ? 1 : 0;

i++;
}
Expand All @@ -301,16 +317,19 @@ private static void ProcessFiles(ProcessOptions options)
if (!File.Exists(file))
{
notifier.Notify(PopstationEventEnum.Error, $"Could not find file '{file}'");
return;
return Results.INVALID_INPUT;
}

notifier.Notify(PopstationEventEnum.FileName, $"Processing {file}");

processing.ProcessFile(file, options, _cancellationTokenSource.Token);
var processResult = processing.ProcessFile(file, options, _cancellationTokenSource.Token);

result = processResult ? 0 : (_cancellationTokenSource.Token.IsCancellationRequested ? Results.CANCELLED : Results.ERROR);
}

notifier.Notify(PopstationEventEnum.ProcessingComplete, null);

return result;
}


Expand Down
35 changes: 24 additions & 11 deletions PSXPackagerGUI/Pages/SinglePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,21 +355,34 @@ public void Save(bool pspMode = false)

Task.Run(() =>
{
Model.IsBusy = true;

using (var stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
try
{
writer.Write(stream, _cancellationTokenSource.Token);
Model.IsBusy = true;

using (var stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
writer.Write(stream, _cancellationTokenSource.Token);
}

Dispatcher.Invoke(() =>
{
MessageBox.Show(Window, $"EBOOT has been saved to \"{filename}\"",
"PSXPackager",
MessageBoxButton.OK, MessageBoxImage.Information);
});

Model.IsBusy = false;
}

Dispatcher.Invoke(() =>
catch (Exception e)
{
MessageBox.Show(Window, $"EBOOT has been saved to \"{filename}\"",
"PSXPackager",
MessageBoxButton.OK, MessageBoxImage.Information);
});

Model.IsBusy = false;
Dispatcher.Invoke(() =>
{
MessageBox.Show(Window, e.Message,
"PSXPackager",
MessageBoxButton.OK, MessageBoxImage.Error);
});
}
});
}
}
Expand Down
99 changes: 46 additions & 53 deletions Popstation/Pbp/PbpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,81 +35,74 @@ public PbpWriter(ConvertOptions convertInfo)

public void Write(Stream outputStream, CancellationToken cancellationToken)
{
try
{
EnsureRequiredResourcesExist();
EnsureRequiredResourcesExist();

ProcessTOCs();
ProcessTOCs();

var sfo = BuildSFO();
var sfo = BuildSFO();

var header = BuildHeader(sfo);
var header = BuildHeader(sfo);

var psarOffset = header[9];
var psarOffset = header[9];

outputStream.Write(header, 0, 0x28);
outputStream.Write(header, 0, 0x28);

Notify?.Invoke(PopstationEventEnum.WriteSfo, null);
outputStream.WriteSFO(sfo);
Notify?.Invoke(PopstationEventEnum.WriteSfo, null);
outputStream.WriteSFO(sfo);

Notify?.Invoke(PopstationEventEnum.WriteIcon0Png, null);
convertInfo.Icon0.Write(outputStream);
Notify?.Invoke(PopstationEventEnum.WriteIcon0Png, null);
convertInfo.Icon0.Write(outputStream);

if (convertInfo.Icon1.Exists)
{
Notify?.Invoke(PopstationEventEnum.WriteIcon1Pmf, null);
convertInfo.Icon1.Write(outputStream);
}
if (convertInfo.Icon1.Exists)
{
Notify?.Invoke(PopstationEventEnum.WriteIcon1Pmf, null);
convertInfo.Icon1.Write(outputStream);
}

Notify?.Invoke(PopstationEventEnum.WritePic0Png, null);
convertInfo.Pic0.Write(outputStream);
Notify?.Invoke(PopstationEventEnum.WritePic0Png, null);
convertInfo.Pic0.Write(outputStream);

Notify?.Invoke(PopstationEventEnum.WritePic1Png, null);
convertInfo.Pic1.Write(outputStream);
Notify?.Invoke(PopstationEventEnum.WritePic1Png, null);
convertInfo.Pic1.Write(outputStream);

if (convertInfo.Snd0.Exists)
{
Notify?.Invoke(PopstationEventEnum.WriteSnd0At3, null);
convertInfo.Snd0.Write(outputStream);
}
if (convertInfo.Snd0.Exists)
{
Notify?.Invoke(PopstationEventEnum.WriteSnd0At3, null);
convertInfo.Snd0.Write(outputStream);
}

Notify?.Invoke(PopstationEventEnum.WriteDataPsp, null);
convertInfo.DataPsp.Write(outputStream);
Notify?.Invoke(PopstationEventEnum.WriteDataPsp, null);
convertInfo.DataPsp.Write(outputStream);

var offset = (uint)outputStream.Position;
var offset = (uint)outputStream.Position;

// fill with NULL
for (var i = 0; i < psarOffset - offset; i++)
{
outputStream.WriteByte(0);
}
// fill with NULL
for (var i = 0; i < psarOffset - offset; i++)
{
outputStream.WriteByte(0);
}

uint totSize = 0;
uint totSize = 0;

foreach (var disc in convertInfo.DiscInfos)
foreach (var disc in convertInfo.DiscInfos)
{
if (File.Exists(disc.SourceIso))
{
if (File.Exists(disc.SourceIso))
{
var t = new FileInfo(disc.SourceIso);
var isosize = (uint)t.Length;
disc.IsoSize = isosize;
totSize += isosize;
}
var t = new FileInfo(disc.SourceIso);
var isosize = (uint)t.Length;
disc.IsoSize = isosize;
totSize += isosize;
}
}

WritePSAR(outputStream, psarOffset, cancellationToken);

if (cancellationToken.IsCancellationRequested)
{
return;
}
WritePSAR(outputStream, psarOffset, cancellationToken);

WriteSTARTDAT(outputStream);
}
catch (Exception e)
if (cancellationToken.IsCancellationRequested)
{
Notify?.Invoke(PopstationEventEnum.Error, e.Message);
return;
}

WriteSTARTDAT(outputStream);
}

public abstract void WritePSAR(Stream outputStream, uint psarOffset, CancellationToken cancellationToken);
Expand Down
43 changes: 41 additions & 2 deletions Popstation/Processing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public bool ProcessFile(
return false;
}

_notifier?.Notify(PopstationEventEnum.ConvertComplete, null);

}
catch (CancellationException ex)
{
Expand All @@ -189,7 +191,6 @@ public bool ProcessFile(
}
finally
{
_notifier?.Notify(PopstationEventEnum.ConvertComplete, null);
if (tempFiles != null)
{
foreach (var tempFile in tempFiles)
Expand Down Expand Up @@ -507,7 +508,7 @@ Resource GetResourceOrDefault(ResourceType type, string ext)
resourcePath = Path.Combine(defaultPath, $"{type}.{ext}");
}

return new Resource(type, resourcePath);
return new Resource(type, GetActualFileName(resourcePath));
}

options.Icon0 = GetResourceOrDefault(ResourceType.ICON0, "png");
Expand All @@ -517,6 +518,44 @@ Resource GetResourceOrDefault(ResourceType type, string ext)
options.Snd0 = GetResourceOrDefault(ResourceType.SND0, "at3");
}

/// <summary>
/// For case-sensitve file systems, this will return the actual filename of a path
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
/// <exception cref="FileNotFoundException"></exception>
private static string GetActualFileName(string path)
{
var directory = Path.GetDirectoryName(path);
var pattern = Path.GetFileName(path);
string resultFileName;

// Enumerate all files in the directory, using the file name as a pattern
// This will list all case variants of the filename even on file systems that
// are case sensitive
var foundFiles = Directory.EnumerateFiles(directory, pattern).ToList();

if (foundFiles.Any())
{
if (foundFiles.Count() > 1)
{
// More than two files with the same name but different case spelling found
throw new Exception("Ambiguous File reference for " + path);
}
else
{
resultFileName = foundFiles.First();
}
}
else
{
resultFileName = path;
}

return resultFileName;
}

private void GenerateResourceFolders(ProcessOptions processOptions, ConvertOptions options, GameEntry entry)
{
var path = Popstation.GetResourceFilename(processOptions.ResourceFormat, options.OriginalFilename, entry.GameID, entry.SaveFolderName, entry.GameName, entry.SaveDescription, entry.Format, ResourceType.ICON0, "png");
Expand Down
6 changes: 3 additions & 3 deletions psxpackager.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<Version>1.6.1</Version>
<FileVersion>1.6.1.0</FileVersion>
<AssemblyVersion>1.6.1.0</AssemblyVersion>
<Version>1.6.2</Version>
<FileVersion>1.6.2.0</FileVersion>
<AssemblyVersion>1.6.2.0</AssemblyVersion>
<Authors>RupertAvery</Authors>
</PropertyGroup>
</Project>

0 comments on commit 9a7f2d7

Please sign in to comment.