-
-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathBundleFile.cs
More file actions
76 lines (63 loc) · 2.82 KB
/
BundleFile.cs
File metadata and controls
76 lines (63 loc) · 2.82 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
using System;
using System.IO;
using System.Text;
namespace StardewModdingAPI.ModBuildConfig.Framework;
/// <summary>A file that should be deployed or zipped as part of a mod.</summary>
internal class BundleFile
{
/*********
** Accessors
*********/
/// <summary>The name of the manifest file.</summary>
public const string ManifestFileName = "manifest.json";
/// <summary>The file's relative path within the mod.</summary>
public string RelativePath { get; }
/// <summary>The file to copy from.</summary>
public FileInfo File { get; }
/// <summary>If set, deploy this content instead of copying the original file.</summary>
public string OverrideContent { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="relativePath">The file's relative path within the mod.</param>
/// <param name="file">The file to copy from.</param>
/// <param name="overrideContent">If set, deploy this content instead of copying the original file.</param>
public BundleFile(string relativePath, FileInfo file, string overrideContent = null)
{
this.RelativePath = relativePath;
this.File = file;
this.OverrideContent = overrideContent;
}
/// <summary>Get whether this entry is for the mod's <samp>manifest.json</samp> file.</summary>
public bool IsModManifest()
{
return BundleFile.IsModManifest(this.RelativePath);
}
/// <summary>Copy the file into a destination folder.</summary>
/// <param name="folderPath">The folder path to use as the base for the <see cref="RelativePath"/>.</param>
public void CopyToFolder(string folderPath)
{
string toPath = Path.Combine(folderPath, this.RelativePath);
Directory.CreateDirectory(Path.GetDirectoryName(toPath)!);
if (this.OverrideContent != null)
System.IO.File.WriteAllText(toPath, this.OverrideContent, Encoding.UTF8);
else
this.File.CopyTo(toPath, overwrite: true);
}
/// <summary>Copy the file's contents into a stream.</summary>
/// <param name="stream">The stream into which to write the file's contents.</param>
public void CopyToStream(Stream stream)
{
using Stream fromStream = this.OverrideContent != null
? new MemoryStream(Encoding.UTF8.GetBytes(this.OverrideContent))
: this.File.OpenRead();
fromStream.CopyTo(stream);
}
/// <summary>Get whether a relative path is for the mod's <samp>manifest.json</samp> file.</summary>
/// <param name="relativePath">The relative path within the mod folder within the mod's folder.</param>
public static bool IsModManifest(string relativePath)
{
return string.Equals(relativePath, BundleFile.ManifestFileName, StringComparison.OrdinalIgnoreCase);
}
}