forked from WolvenKit/WolvenKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundleManager.cs
168 lines (145 loc) · 6.31 KB
/
BundleManager.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using WolvenKit.Common;
using WolvenKit.Common.Model;
namespace WolvenKit.Bundles
{
public class BundleManager : WitcherArchiveManager
{
public BundleManager()
{
Items = new Dictionary<string, List<IGameFile>>();
Bundles = new Dictionary<string, Bundle>();
FileList = new List<IGameFile>();
Extensions = new List<string>();
AutocompleteSource = new List<string>();
}
public Dictionary<string, Bundle> Bundles { get; }
public override EArchiveType TypeName => EArchiveType.Bundle;
public static string SerializationVersion => "1.0";
/// <summary>
/// Load a single mod bundle
/// </summary>
/// <param name="filename">
/// file to process
/// </param>
public override void LoadModArchive(string filename)
{
if (Bundles.ContainsKey(filename))
return;
var bundle = new Bundle(filename);
foreach (var item in bundle.Items)
{
if (!Items.ContainsKey(GetModFolder(filename) + "\\" + item.Key))
Items.Add(GetModFolder(filename) + "\\" + item.Key, new List<IGameFile>());
Items[GetModFolder(filename) + "\\" +item.Key].Add(item.Value);
}
Bundles.Add(filename, bundle);
}
/// <summary>
/// Load a single bundle
/// </summary>
/// <param name="filename"></param>
/// <param name="ispatch"></param>
public override void LoadArchive(string filename, bool ispatch = false)
{
if (Bundles.ContainsKey(filename))
return;
var bundle = new Bundle(filename);
foreach (KeyValuePair<string, BundleItem> item in bundle.Items)
{
// add new key if the file isn't already in another bundle
if (!Items.ContainsKey(item.Key))
Items.TryAdd(item.Key, new List<IGameFile>());
// if file is already in another bundle
if (ispatch && Items[item.Key].Count > 0)
{
// check if file is already in contentN directory (content0, content1 etc)
List<IGameFile> filesInBundles = Items[item.Key];
var splits = filesInBundles.First().Archive.ArchiveAbsolutePath.Split(Path.DirectorySeparatorChar);
var contentdir = splits[splits.Length - 3];
if (contentdir.Contains("content"))
{
// then remove all other existing files
for (var i = 0; i < filesInBundles.Count; i++)
{
bundle.Patchedfiles.Add(filesInBundles[i]);
filesInBundles.RemoveAt(0);
}
}
}
Items[item.Key].Add(item.Value);
}
Bundles.Add(filename, bundle);
}
/// <summary>
/// Load every non-mod bundle it can find in ..\..\content and ..\..\DLC, also calls RebuildRootNode()
/// </summary>
/// <param name="exedir">Path to executable directory</param>
public override void LoadAll(string exedir)
{
var di = new DirectoryInfo(exedir);
if (!di.Exists)
return;
var dlc = Path.Combine(di.Parent.Parent.FullName, "DLC");
var content = Path.Combine(di.Parent.Parent.FullName, "content");
var contentdirs = new List<string>(Directory.GetDirectories(content, "content*"));
contentdirs.Sort(new AlphanumComparator<string>());
foreach (var file in contentdirs.SelectMany(dir => Directory.GetFiles(dir, "*.bundle", SearchOption.AllDirectories)))
{
LoadArchive(file);
}
var patchdirs = new List<string>(Directory.GetDirectories(content, "patch*"));
patchdirs.Sort(new AlphanumComparator<string>());
foreach (var file in patchdirs.SelectMany(dir =>
Directory.GetFiles(dir, "*.bundle", SearchOption.AllDirectories)))
{
LoadArchive(file, true);
}
if (Directory.Exists(dlc))
{
var dlcdirs = new List<string>(Directory.GetDirectories(dlc));
dlcdirs.Sort(new AlphanumComparator<string>());
foreach (var file in dlcdirs
.Where(_ => VanillaDlClist.Contains(new DirectoryInfo(_).Name))
.SelectMany(dir => Directory.GetFiles(dir ?? "", "*.bundle", SearchOption.AllDirectories)
.OrderBy(k => k)))
{
LoadArchive(file);
}
}
RebuildRootNode();
}
/// <summary>
/// Loads bundles from specified mods and dlc folder
/// </summary>
/// <param name="mods"></param>
/// <param name="dlc"></param>
public override void LoadModsArchives(string mods, string dlc)
{
if (!Directory.Exists(mods))
Directory.CreateDirectory(mods);
var modsdirs = new List<string>(Directory.GetDirectories(mods));
modsdirs.Sort(new AlphanumComparator<string>());
var modbundles = modsdirs.SelectMany(dir => Directory.GetFiles(dir, "*.bundle", SearchOption.AllDirectories)).ToList();
foreach (var file in modbundles)
{
LoadModArchive(file);
}
if (Directory.Exists(dlc))
{
var dlcdirs = new List<string>(Directory.GetDirectories(dlc));
dlcdirs.Sort(new AlphanumComparator<string>());
var tmp = dlcdirs.Where(_ => !VanillaDlClist.Contains(new DirectoryInfo(_).Name)).ToList();
foreach (var file in tmp.SelectMany(dir => Directory.GetFiles(dir ?? "", "*.bundle", SearchOption.AllDirectories).OrderBy(k => k)))
{
LoadModArchive(file);
}
}
RebuildRootNode();
}
}
}