Skip to content

Commit

Permalink
Fix batch replacement in SteamMode and BundleMode
Browse files Browse the repository at this point in the history
Update definition of CraftingBenchOptions.dat and CraftingBenchSortCategories.dat
  • Loading branch information
aianlinb committed Sep 10, 2021
1 parent edc128c commit 7506888
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 30 deletions.
5 changes: 3 additions & 2 deletions LibBundle/IndexContainer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using LibBundle.Records;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LibBundle
Expand Down Expand Up @@ -169,7 +168,9 @@ public virtual byte[] Save()
public virtual BundleRecord GetSmallestBundle(IList<BundleRecord> Bundles = null)
{
Bundles ??= this.Bundles;
var result = Bundles.ElementAt(0);
if (Bundles.Count == 0)
return null;
var result = Bundles[0];
var l = Bundles[0].UncompressedSize;
foreach (var b in Bundles)
if (b.UncompressedSize < l)
Expand Down
2 changes: 1 addition & 1 deletion LibBundle/LibBundle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>aianlinb</Authors>
<Copyright>Copyright © 2020-2021 aianlinb.</Copyright>
<Platforms>x64</Platforms>
<Version>2.4.9</Version>
<Version>2.5.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down
14 changes: 11 additions & 3 deletions LibDat2/DatDefinitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1367,20 +1367,28 @@
"IsAreaOption": "bool",
"RecipeIds": "array|i32",
"Tier": "i32",
"ModFamily": "string",
//"ModFamily": "string",
"CraftingItemClassCategoriesKeys": "array|foreignrow",
"MaximumMapTier": "i32",
"CraftingBenchUnlockCategoriesKey": "foreignrow",
"UnveilsRequired": "i32",
"UnveilsRequired2": "i32",
"AffixType": "string",
//"AffixType": "string",
"Unknown1": "array|foreignrow",
"Unknown2": "array|foreignrow",
"AchievementItemsKeys": "array|foreignrow",
"Unknown3": "i32",
"Unknown4": "i32",
"Unknown5": "bool",
"Unknown6": "foreignrow"
"Unknown6": "foreignrow",
"Unknown7": "foreignrow",
"Unknown8": "foreignrow",
"Unknown9": "foreignrow"
},
"CraftingBenchSortCategories": {
"Id": "string",
"Key0": "foreignrow",
"Flag0": "bool"
},
"CraftingBenchUnlockCategories": {
"Id": "string",
Expand Down
55 changes: 38 additions & 17 deletions LibGGPK2/GGPKContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,38 +241,59 @@ public BundleMissingException(int failBundles, int failFiles) : base($"{failBund
/// </summary>
/// <param name="list">File list to replace (generate by <see cref="RecursiveFileList"/>)</param>
/// <param name="ProgressStep">It will be executed every time a file is replaced</param>
public virtual void Replace(IEnumerable<KeyValuePair<IFileRecord, string>> list, Action ProgressStep = null)
{
var bundles = new List<LibBundle.Records.BundleRecord>(Index.Bundles);
public virtual void Replace(IEnumerable<KeyValuePair<IFileRecord, string>> list, Action ProgressStep = null) {
var bundles = Index == null ? new() : new List<LibBundle.Records.BundleRecord>(Index.Bundles);
var changed = false;
var BundleToSave = Index?.GetSmallestBundle(bundles);
var BundleToSave = Index.GetSmallestBundle(bundles);
var fr = RecordOfBundle(BundleToSave);
while (fr == null) {
bundles.Remove(BundleToSave);
BundleToSave = Index?.GetSmallestBundle(bundles);
fr = RecordOfBundle(BundleToSave);
}

if (Index != null) // else BundleMode
if (fileStream == null) // SteamMode
while (!File.Exists(BundleToSave.Name)) {
bundles.Remove(BundleToSave);
if (bundles.Count == 0)
throw new("Couldn't find a bundle to save");
BundleToSave = Index.GetSmallestBundle(bundles);
}
else
while (fr == null) {
bundles.Remove(BundleToSave);
if (bundles.Count == 0)
throw new("Couldn't find a bundle to save");
BundleToSave = Index.GetSmallestBundle(bundles);
fr = RecordOfBundle(BundleToSave);
}

var SavedSize = 0;
foreach (var (record, path) in list) {
if (SavedSize > 50000000) // 50MB per bundle
{
if (SavedSize > 200000000 && bundles.Count > 1) { // 200MB per bundle
changed = true;
if (fileStream == null) {
if (fileStream == null) // SteamMode
BundleToSave.Save();
} else {
else {
fr.ReplaceContent(BundleToSave.Save(Reader, fr.DataBegin));
BundleToSave.Bundle.offset = fr.DataBegin;
BundleFileNode.LastFileToUpdate.RemoveOldCache(BundleToSave);
}
BundleToSave = Index.GetSmallestBundle();
fr = RecordOfBundle(BundleToSave);
while (fr == null) {
bundles.Remove(BundleToSave);
BundleToSave = Index?.GetSmallestBundle(bundles);

if (Index != null) { // else BundleMode
fr = RecordOfBundle(BundleToSave);
if (fileStream == null) // SteamMode
while (!File.Exists(BundleToSave.Name)) {
bundles.Remove(BundleToSave);
BundleToSave = Index.GetSmallestBundle(bundles);
}
else
while (fr == null) {
bundles.Remove(BundleToSave);
BundleToSave = Index.GetSmallestBundle(bundles);
fr = RecordOfBundle(BundleToSave);
}
}
SavedSize = 0;
}

if (record is BundleFileNode bfn) // In Bundle
SavedSize += bfn.BatchReplaceContent(File.ReadAllBytes(path), BundleToSave);
else // In GGPK
Expand Down
2 changes: 1 addition & 1 deletion LibGGPK2/LibGGPK2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>aianlinb</Authors>
<Copyright>Copyright © 2020-2021 aianlinb.</Copyright>
<Platforms>x64</Platforms>
<Version>0.12.11</Version>
<Version>0.12.12</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down
2 changes: 1 addition & 1 deletion LibGGPK2/Records/BundleFileNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public virtual byte[] ReadFileContent(Stream ggpkStream = null)
cached = BundleFileRecord.bundleRecord.Bundle.Read(br);
CachedBundleData.AddLast(new KeyValuePair<BundleRecord, MemoryStream>(BundleFileRecord.bundleRecord, cached));
CachedSize += cached.Length;
while (CachedSize > 300000000 && CachedBundleData.Count > 1) {
while (CachedSize > 500000000 && CachedBundleData.Count > 1) { // 500MB
var ms = CachedBundleData.First.Value.Value;
CachedSize -= ms.Length;
ms.Close();
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A visual program to view/edit ggpk file.
![image](https://github.com/aianlinb/LibGGPK2/blob/master/.github/example.png)
## What's New?
- Directly access the files in bundles.
- No longer read all Records at the beginning.
- No longer read all Records of GGPK at the beginning.
- The new file added will replace existing FreeRecord instead of being appended to the end of the GGPK.
- Correctly handle all NextFreeRecordOffset of FreeRecord.
- No longer allow other programs to modify GGPK file when opening it.
Expand All @@ -18,9 +18,11 @@ A visual program to view/edit ggpk file.
- Recovering files from patch server.
- Vista style folder selector.
- Export/Replace in background.
- ProgressBar to view the current work progress.
- Fix DDS viewer.
- Directly edit and save in TextViewer.
- Custom exception window instead of crashing.
- Port from .NET Framework to .NET Core.
- Remove unnecessary code.
- Allow editing the whole dat file.
- Allow editing the whole dat file.
- Automatically check for updates.
2 changes: 1 addition & 1 deletion VisualGGPK2/ErrorWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Window x:Class="VisualGGPK2.ErrorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ErrorWindow (v0.12.11)" Width="560" Height="500" WindowStartupLocation="CenterScreen" Closing="OnClosing">
Title="ErrorWindow (v0.12.12)" Width="560" Height="500" WindowStartupLocation="CenterScreen" Closing="OnClosing">
<Grid>
<TextBox x:Name="ErrorBox" Text="Loading Exception . . ." Padding="2" IsReadOnly="True" TextWrapping="Wrap" AcceptsReturn="True" Margin="0,0,0,31" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
<TextBlock FontSize="14"
Expand Down
2 changes: 1 addition & 1 deletion VisualGGPK2/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Window x:Class="VisualGGPK2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VisualGGPK2 (v0.12.11)" Width="960" Height="560" AllowDrop="True" Loaded="OnLoaded" DragEnter="OnDragEnter" Drop="OnDragDrop">
Title="VisualGGPK2 (v0.12.12)" Width="960" Height="560" AllowDrop="True" Loaded="OnLoaded" DragEnter="OnDragEnter" Drop="OnDragDrop">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="280" MinWidth="10"/>
Expand Down
2 changes: 1 addition & 1 deletion VisualGGPK2/VisualGGPK2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0-windows</TargetFramework>
<Authors>aianlinb</Authors>
<Copyright>Copyright © 2020-2021 aianlinb.</Copyright>
<Version>0.12.11</Version>
<Version>0.12.12</Version>
<Platforms>x64</Platforms>
<OutputType>WinExe</OutputType>
<UseWPF>true</UseWPF>
Expand Down

0 comments on commit 7506888

Please sign in to comment.