-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathProjectExplorer.cs
More file actions
332 lines (275 loc) · 9.41 KB
/
Copy pathProjectExplorer.cs
File metadata and controls
332 lines (275 loc) · 9.41 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using RPGCore.FileTree;
using RPGCore.FileTree.FileSystem;
using RPGCore.FileTree.Packed;
using RPGCore.Packages;
using RPGCore.Projects.Pipeline;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
namespace RPGCore.Projects;
/// <summary>
/// <para>Used for loading the content of an uncompiled package.</para>
/// </summary>
/// <remarks>
/// <para>Can be used to analyse, but not modify the content, of a package.</para>
/// </remarks>
public sealed class ProjectExplorer : IExplorer
{
public ImportPipeline ImportPipeline { get; private set; }
/// <summary>
/// <para>The source for this project explorer.</para>
/// </summary>
public IArchive Archive { get; private set; }
/// <summary>
/// <para>The size of all of the projects resources on disk.</para>
/// </summary>
public long UncompressedSize { get; private set; }
/// <summary>
/// <para>The project definition for this project.</para>
/// </summary>
public ProjectDefinition Definition { get; private set; }
/// <summary>
/// <para>A collection of all of the resources contained in this project.</para>
/// </summary>
public ProjectResourceCollection Resources { get; private set; }
/// <summary>
/// <para>An index of the tags contained within this project for performing asset queries.</para>
/// </summary>
public ITagsCollection Tags { get; private set; }
/// <summary>
/// <para>A directory representing the root of the project.</para>
/// </summary>
public ProjectDirectory RootDirectory { get; private set; }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] IDefinition IExplorer.Definition => Definition;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] IResourceCollection IExplorer.Resources => Resources;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] ITagsCollection IExplorer.Tags => Tags;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] IDirectory IExplorer.RootDirectory => RootDirectory;
internal ProjectExplorer()
{
}
public static ProjectExplorer CreateProject(string projectDirectoryPath, ImportPipeline importPipeline)
{
var directoryInfo = new DirectoryInfo(projectDirectoryPath);
// Ensure the directory exists.
if (!directoryInfo.Exists)
{
Directory.CreateDirectory(directoryInfo.FullName);
}
string bprojPath = Path.Combine(directoryInfo.FullName, $"{directoryInfo.Name}.bproj");
File.WriteAllText(bprojPath, "<Project>\r\n\t\r\n</Project>");
return Load(projectDirectoryPath, importPipeline);
}
public static ProjectExplorer Load(string projectPath, ImportPipeline importPipeline)
{
return LoadAsync(projectPath, importPipeline).Result;
}
public static async Task<ProjectExplorer> LoadAsync(string projectPath, ImportPipeline importPipeline)
{
string bprojPath = null;
if (projectPath.EndsWith(".bproj"))
{
bprojPath = projectPath;
projectPath = new DirectoryInfo(projectPath).Parent.FullName;
}
else
{
foreach (string rootFile in Directory.EnumerateFiles(projectPath, "*.bproj"))
{
bprojPath = rootFile;
break;
}
}
var sourceArchive = new FileSystemArchive(new DirectoryInfo(projectPath));
var resources = new Dictionary<string, ProjectResource>();
var rootDirectiory = new ProjectDirectory(null, null);
var projectExplorer = new ProjectExplorer
{
Archive = sourceArchive,
Definition = ProjectDefinition.Load(bprojPath),
Resources = new ProjectResourceCollection(resources),
RootDirectory = rootDirectiory,
ImportPipeline = importPipeline
};
// Organise temporary directory
var tempDirectory = new DirectoryInfo(Path.Combine(projectPath, "../temp"));
tempDirectory.Create();
var tempArchive = new FileSystemArchive(tempDirectory, false);
void ApplyUpdate(ProjectResourceUpdate update)
{
string[] elements = update.ProjectKey.Split(new char[] { '/' });
var placementDirectory = rootDirectiory;
for (int i = 0; i < elements.Length - 1; i++)
{
string element = elements[i];
var parent = placementDirectory;
placementDirectory = placementDirectory.Directories[element];
if (placementDirectory == null)
{
placementDirectory = new ProjectDirectory(placementDirectory, element);
parent.Directories.Add(placementDirectory);
}
}
string fileName = elements[elements.Length - 1];
// Locate or create resource to apply updates to.
if (!placementDirectory.Resources.TryGetResource(fileName, out var projectResource))
{
projectResource = new ProjectResource(projectExplorer, placementDirectory, update.ProjectKey);
projectExplorer.Resources.Add(projectResource.FullName, projectResource);
placementDirectory.Resources.Add(projectResource.Name, projectResource);
}
// Apply updates to resource
projectResource.Tags.importerTags.AddRange(update.ImporterTags);
foreach (var importerDependency in update.Dependencies)
{
projectResource.Dependencies.dependencies.Add(
new ProjectResourceDependency(projectExplorer, importerDependency.Resource, importerDependency.Metadata));
}
if (update.FileContent != null)
{
projectResource.Content = new ProjectResourceContent(update.FileContent);
}
if (update.DeferredContent != null)
{
string tempId = Guid.NewGuid().ToString();
var tempFile = tempArchive.RootDirectory.Files.GetOrCreateFile(tempId);
using (var tempOutput = tempFile.OpenWrite())
{
update.DeferredContent.WriteContentAsync(tempOutput).Wait();
}
projectResource.Content = new ProjectResourceContent(tempFile);
}
}
// Import resources
foreach (var update in importPipeline.ImportDirectory(projectExplorer, sourceArchive.RootDirectory))
{
ApplyUpdate(update);
}
// Attach dependencies
foreach (var resource in projectExplorer.Resources)
{
foreach (var dependency in resource.Dependencies)
{
var dependencyResource = dependency.Resource;
if (dependencyResource == null)
{
Console.WriteLine($"ERROR: Unable to resolve dependency for '{dependency.Key}'");
}
else
{
dependencyResource.Dependants.dependencies.Add(new ProjectResourceDependency(projectExplorer, resource.FullName, dependency.metdadata));
}
}
}
// Process Resoures
var importProcessorContext = new ImportProcessorContext()
{
Explorer = projectExplorer
};
foreach (var processor in importPipeline.importProcessors)
{
var preExistingResources = new List<ProjectResource>();
foreach (var resource in projectExplorer.Resources)
{
preExistingResources.Add(resource);
}
int maxThreads = 32;
var semaphore = new SemaphoreSlim(maxThreads);
foreach (var resource in preExistingResources)
{
if (processor.CanProcess(resource))
{
semaphore.Wait();
_ = Task.Factory.StartNew(() =>
{
try
{
var updates = processor.ProcessImport(importProcessorContext, resource);
if (updates != null)
{
foreach (var update in updates)
{
ApplyUpdate(update);
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
finally
{
semaphore.Release();
}
}, TaskCreationOptions.LongRunning);
}
}
for (int i = 0; i < maxThreads; i++)
{
semaphore.Wait();
}
}
// Size Calculation
long uncompressedSize = 0;
foreach (var resource in projectExplorer.Resources)
{
uncompressedSize += resource.Content.UncompressedSize;
}
projectExplorer.UncompressedSize = uncompressedSize;
// Tag Indexing
var tags = new Dictionary<string, IResourceCollection>();
foreach (var resource in projectExplorer.Resources)
{
foreach (string tag in resource.Tags)
{
if (!tags.TryGetValue(tag, out var taggedResourcesCollection))
{
taggedResourcesCollection = new ProjectResourceCollection();
tags[tag] = taggedResourcesCollection;
}
var taggedResources = (ProjectResourceCollection)taggedResourcesCollection;
taggedResources.Add(resource.FullName, resource);
}
}
projectExplorer.Tags = new ProjectTagsCollection(tags);
return projectExplorer;
}
public void Dispose()
{
}
public FileInfo ExportZippedToDirectory(BuildPipeline pipeline, string path)
{
var directoryInfo = new DirectoryInfo(path);
if (directoryInfo.Exists)
{
directoryInfo.Delete(true);
}
directoryInfo.Create();
var buildProcess = new ProjectBuildProcess(pipeline, this, directoryInfo.FullName);
string packageFile = Path.Combine(directoryInfo.FullName, $"{Definition.Properties.Name}.bpkg");
var packageFileInfo = new FileInfo(packageFile);
using var fileStream = new FileStream(packageFile, FileMode.Create, FileAccess.Write);
using var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, false);
var archive = new PackedArchive(zipArchive);
buildProcess.PerformBuild(archive.RootDirectory);
return packageFileInfo;
}
public DirectoryInfo ExportFoldersToDirectory(BuildPipeline pipeline, string path)
{
var directoryInfo = new DirectoryInfo(path);
if (directoryInfo.Exists)
{
directoryInfo.Delete(true);
}
directoryInfo.Create();
var buildProcess = new ProjectBuildProcess(pipeline, this, directoryInfo.FullName);
string folderPath = Path.Combine(directoryInfo.FullName, $"{Definition.Properties.Name}");
var folderInfo = new DirectoryInfo(folderPath);
var archive = new FileSystemArchive(folderInfo, false);
buildProcess.PerformBuild(archive.RootDirectory);
return folderInfo;
}
}