Skip to content

Commit

Permalink
add some design data helpers, rework p4kfilesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Jan 13, 2025
1 parent 28fefa9 commit b9e846f
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CliFx.Infrastructure;
using StarBreaker.Cli.Utils;
using StarBreaker.DataCore;
using StarBreaker.P4k;

namespace StarBreaker.Cli;

Expand Down Expand Up @@ -43,7 +44,7 @@ public ValueTask ExecuteAsync(IConsole console)
}
else if (!string.IsNullOrEmpty(P4kFile))
{
var p4k = P4k.P4kFile.FromFile(P4kFile);
var p4k = new P4kFileSystem(P4k.P4kFile.FromFile(P4kFile));
console.Output.WriteLine("P4k loaded.");
foreach (var file in DataCoreUtils.KnownPaths)
{
Expand Down
3 changes: 2 additions & 1 deletion src/StarBreaker.Cli/DiffCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using StarBreaker.DataCore;
using StarBreaker.P4k;

namespace StarBreaker.Cli;

Expand Down Expand Up @@ -90,7 +91,7 @@ public async ValueTask ExecuteAsync(IConsole console)

private static async Task ExtractDataCoreIntoZip(string p4kFile, string zipPath)
{
var p4k = P4k.P4kFile.FromFile(p4kFile);
var p4k = new P4kFileSystem(P4kFile.FromFile(p4kFile));
Stream? dcbStream = null;
string? dcbFile = null;
foreach (var file in DataCoreUtils.KnownPaths)
Expand Down
19 changes: 19 additions & 0 deletions src/StarBreaker.P4k/FakeP4kFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace StarBreaker.P4k;

public class FakeP4kFile : IP4kFile
{
public string P4KPath { get; }
public ZipEntry[] Entries { get; }
public ZipNode Root { get; }

public FakeP4kFile(string path, ZipEntry[] entries, ZipNode root)
{
P4KPath = path;
Root = root;
Entries = entries;
}

public Stream OpenStream(ZipEntry entry) => new MemoryStream(new byte[entry.UncompressedSize]);

public byte[] OpenInMemory(ZipEntry entry) => new byte[entry.UncompressedSize];
}
10 changes: 10 additions & 0 deletions src/StarBreaker.P4k/IP4kFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace StarBreaker.P4k;

public interface IP4kFile
{
string P4KPath { get; }
ZipEntry[] Entries { get; }
ZipNode Root { get; }
Stream OpenStream(ZipEntry entry);
byte[] OpenInMemory(ZipEntry entry);
}
4 changes: 2 additions & 2 deletions src/StarBreaker.P4k/P4kFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace StarBreaker.P4k;

public sealed partial class P4kFile
public sealed class P4kFile : IP4kFile
{
[ThreadStatic] private static Decompressor? decompressor;
private readonly Aes _aes;

public ZipEntry[] Entries { get; }
public string P4KPath { get; }
public ZipEntry[] Entries { get; }
public ZipNode Root { get; }

private P4kFile(string path, ZipEntry[] entries, ZipNode root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace StarBreaker.P4k;

public sealed partial class P4kFile : IFileSystem
public class P4kFileSystem : IFileSystem
{
public IP4kFile P4kFile { get; }
public ZipNode Root { get; }

public P4kFileSystem(IP4kFile p4kFile)
{
P4kFile = p4kFile;
Root = p4kFile.Root;
}

public IEnumerable<string> GetDirectories(string path)
{
Span<Range> ranges = stackalloc Range[20];
Expand All @@ -25,7 +34,7 @@ public IEnumerable<string> GetDirectories(string path)
yield return child.Name;
}
}

public IEnumerable<string> GetFiles(string path)
{
Span<Range> ranges = stackalloc Range[20];
Expand Down Expand Up @@ -88,6 +97,6 @@ public Stream OpenRead(string path)
throw new FileNotFoundException();

//Is this a bad idea? Most things that use this rely on the stream being seekable.
return new MemoryStream(OpenInMemory(current.ZipEntry));
return new MemoryStream(P4kFile.OpenInMemory(current.ZipEntry));
}
}
9 changes: 9 additions & 0 deletions src/StarBreaker.P4k/ZipNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ public sealed class ZipNode
public ZipEntry? ZipEntry { get; }
public Dictionary<string, ZipNode> Children { get; }

public ZipNode(string name, IEnumerable<ZipEntry> entries)
{
Name = name;
ZipEntry = null;
Children = [];
foreach (var entry in entries)
Insert(entry);
}

public ZipNode(string name)
{
Name = name;
Expand Down
4 changes: 2 additions & 2 deletions src/StarBreaker.Sandbox/Crc32CSandbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void Run()
{
var uintsToTest = ReadKeys("keys.txt");

var p4k = P4kFile.FromFile(@"C:\Program Files\Roberts Space Industries\StarCitizen\4.0_PREVIEW\Data.p4k");
var p4k = new P4kFileSystem(P4kFile.FromFile(@"C:\Program Files\Roberts Space Industries\StarCitizen\4.0_PREVIEW\Data.p4k"));
var dcbStream = p4k.OpenRead(@"Data\Game2.dcb");

var dcb = new DataForge(dcbStream);
Expand All @@ -30,7 +30,7 @@ public static void Run()
.Concat(StreamLines("mats.txt"))
.Concat(EnumeratePaths(StreamLines("mats.txt"), '/'))
//.Concat(StreamLines("working.txt"))
.Concat(EnumeratePaths(p4k.Entries.Select(x => x.Name), '\\'));
.Concat(EnumeratePaths(p4k.P4kFile.Entries.Select(x => x.Name), '\\'));

//TODO: charactercustomizer_pu.socpak

Expand Down
2 changes: 1 addition & 1 deletion src/StarBreaker.Sandbox/DataCoreSandbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class DataCoreSandbox
{
public static void Run()
{
var p4k = P4kFile.FromFile(@"C:\Program Files\Roberts Space Industries\StarCitizen\PTU\Data.p4k");
var p4k = new P4kFileSystem(P4kFile.FromFile(@"C:\Program Files\Roberts Space Industries\StarCitizen\PTU\Data.p4k"));
var dcbStream = p4k.OpenRead(@"Data\Game2.dcb");
var dcb = new DataForge(dcbStream);

Expand Down
2 changes: 1 addition & 1 deletion src/StarBreaker/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void OnFrameworkInitializationCompleted()

var collection = new ServiceCollection();

collection.RegisterServices();
collection.RegisterServices(Design.IsDesignMode);
ViewLocator.RegisterViews();

Services = collection.BuildServiceProvider();
Expand Down
18 changes: 15 additions & 3 deletions src/StarBreaker/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ namespace StarBreaker.Extensions;

public static class ServiceCollectionExtensions
{
public static void RegisterServices(this ServiceCollection services)
public static void RegisterServices(this ServiceCollection services, bool isDesignMode)
{
services.AddLogging(b => { b.AddSimpleConsole(options => { options.SingleLine = true; }); });
services.AddLogging(b =>
{
b.AddDebug();
b.AddConsole();
});
services.AddTransient<MainWindowViewModel>();
services.AddTransient<SplashWindowViewModel>();
services.AddTransient<P4kTabViewModel>();
services.AddTransient<DataCoreTabViewModel>();
services.AddSingleton<IP4kService, P4kService>();

if (isDesignMode)
{
services.AddSingleton<IP4kService, DesignP4kService>();
}
else
{
services.AddSingleton<IP4kService, P4kService>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public DataCoreTabViewModel(IP4kService p4kService)

private void Initialize()
{
var entry = _p4KService.P4kFile.OpenRead(dataCorePath);
var entry = _p4KService.P4KFileSystem.OpenRead(dataCorePath);
var dcb = new DataCoreBinary(entry);
entry.Dispose();

Expand Down
4 changes: 2 additions & 2 deletions src/StarBreaker/Screens/Tabs/P4kTabView/P4kTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public P4kTabViewModel(IP4kService p4kService, ILogger<P4kTabViewModel> logger)

Source.RowSelection!.SingleSelect = true;
Source.RowSelection.SelectionChanged += SelectionChanged;
Source.Items = _p4KService.P4kFile.Root.Children.Values;
Source.Items = _p4KService.P4KFileSystem.Root.Children.Values;
}

private void SelectionChanged(object? sender, TreeSelectionModelSelectionChangedEventArgs<ZipNode> e)
Expand Down Expand Up @@ -98,7 +98,7 @@ private void SelectionChanged(object? sender, TreeSelectionModelSelectionChanged
try
{
//TODO: move this to a service?
var buffer = _p4KService.P4kFile.OpenInMemory(selectedEntry.ZipEntry);
var buffer = _p4KService.P4KFileSystem.P4kFile.OpenInMemory(selectedEntry.ZipEntry);

FilePreviewViewModel preview;

Expand Down
35 changes: 35 additions & 0 deletions src/StarBreaker/Services/P4kService/DesignP4kService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using StarBreaker.P4k;

namespace StarBreaker.Services;

public class DesignP4kService : IP4kService
{
public P4kFileSystem P4KFileSystem { get; }

public DesignP4kService()
{
var entries = GetFakeEntries();
var root = new ZipNode("Root", entries);

P4KFileSystem = new P4kFileSystem(new FakeP4kFile(@"C:\This\Is\A\Path", entries, root));
}

public void OpenP4k(string path, IProgress<double> progress)
{
progress.Report(0);
Thread.Sleep(500);
progress.Report(0.5);
Thread.Sleep(500);
progress.Report(1);
}

private static ZipEntry[] GetFakeEntries() =>
[
new(@"Data\entry1", 69, 69, 0, false, 123, 0xffff, 0xdeadbeef),
new(@"Data\entry2", 69, 69, 0, false, 123, 0xffff, 0xdeadbeef),
new(@"Data\ObjectContainers\entry2", 69, 69, 0, false, 123, 0xffff, 0xdeadbeef),
new(@"Data\Textures\entry2", 69, 69, 0, false, 123, 0xffff, 0xdeadbeef),
new(@"Engine\entry3", 69, 69, 0, false, 123, 0xffff, 0xdeadbeef),
new(@"Engine\entry3", 69, 69, 0, false, 123, 0xffff, 0xdeadbeef)
];
}
9 changes: 9 additions & 0 deletions src/StarBreaker/Services/P4kService/IP4kService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using StarBreaker.P4k;

namespace StarBreaker.Services;

public interface IP4kService
{
P4kFileSystem P4KFileSystem { get; }
void OpenP4k(string path, IProgress<double> progress);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using Microsoft.Extensions.Logging;
using StarBreaker.FileSystem;
using StarBreaker.P4k;

namespace StarBreaker.Services;

public interface IP4kService
{
P4kFile P4kFile { get; }
void OpenP4k(string path, IProgress<double> progress);
}

public class P4kService : IP4kService
{
private readonly ILogger<P4kService> _logger;
private P4kFile? _p4KFile;
private P4kFileSystem? _p4KFile;

public P4kFile P4kFile => _p4KFile ?? throw new InvalidOperationException("P4k file not open");
public P4kFileSystem P4KFileSystem => _p4KFile ?? throw new InvalidOperationException("P4k file not open");

public P4kService(ILogger<P4kService> logger)
{
Expand All @@ -28,6 +23,7 @@ public void OpenP4k(string path, IProgress<double> progress)
_logger.LogWarning("P4k file already open");
return;
}
_p4KFile = P4kFile.FromFile(path, progress);

_p4KFile = new P4kFileSystem(P4kFile.FromFile(path, progress));
}
}

0 comments on commit b9e846f

Please sign in to comment.