Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
FurniturePacksManager.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
4
5namespace Game {
6 public static class FurniturePacksManager {
7 public static List<string> m_furniturePackNames;
8
9 public static ReadOnlyList<string> FurniturePackNames => new(m_furniturePackNames);
10
12
13 public static event Action<string> FurniturePackDeleted;
14
16
20
21 public static string GetFileName(string name) => Storage.CombinePaths(FurniturePacksDirectoryName, name);
22
23 public static string GetDisplayName(string name) => Storage.GetFileNameWithoutExtension(name);
24
25 public static DateTime GetCreationDate(string name) {
26 try {
28 }
29 catch {
30 return new DateTime(2000, 1, 1);
31 }
32 }
33
34 public static string ImportFurniturePack(string name, Stream stream) {
36 throw new InvalidOperationException("Cannot import furniture packs in trial mode.");
37 }
39 stream.Position = 0L;
40 string fileNameWithoutExtension = Storage.GetFileNameWithoutExtension(name);
41 name = $"{fileNameWithoutExtension}.scfpack";
42 string fileName = GetFileName(name);
43 int num = 0;
44 while (Storage.FileExists(fileName)) {
45 num++;
46 if (num > 9) {
47 throw new InvalidOperationException("Duplicate name. Delete existing content with conflicting names.");
48 }
49 name = $"{fileNameWithoutExtension} ({num}).scfpack";
50 fileName = GetFileName(name);
51 }
52 using (Stream destination = Storage.OpenFile(fileName, OpenFileMode.Create)) {
53 stream.CopyTo(destination);
54 return name;
55 }
56 }
57
58 public static void ExportFurniturePack(string name, Stream stream) {
59 using (Stream stream2 = Storage.OpenFile(GetFileName(name), OpenFileMode.Read)) {
60 stream2.CopyTo(stream);
61 }
62 }
63
64 public static string CreateFurniturePack(string name, ICollection<FurnitureDesign> designs) {
65 MemoryStream memoryStream = new();
66 using (ZipArchive zipArchive = ZipArchive.Create(memoryStream, true)) {
67 ValuesDictionary valuesDictionary = new();
69 XElement xElement = new("FurnitureDesigns");
70 valuesDictionary.Save(xElement);
71 MemoryStream memoryStream2 = new();
72 xElement.Save(memoryStream2);
73 memoryStream2.Position = 0L;
74 zipArchive.AddStream("FurnitureDesigns.xml", memoryStream2);
75 }
76 memoryStream.Position = 0L;
77 return ImportFurniturePack(name, memoryStream);
78 }
79
80 public static void DeleteFurniturePack(string name) {
81 try {
83 FurniturePackDeleted?.Invoke(name);
84 }
85 catch (Exception e) {
86 ExceptionManager.ReportExceptionToUser($"Unable to delete furniture pack \"{name}\"", e);
87 }
88 }
89
90 public static void UpdateFurniturePacksList() {
92 foreach (string item in Storage.ListFileNames(FurniturePacksDirectoryName)) {
93 if (Storage.GetExtension(item).ToLower() == ".scfpack") {
94 m_furniturePackNames.Add(item);
95 }
96 }
97 }
98
99 public static List<FurnitureDesign> LoadFurniturePack(SubsystemTerrain subsystemTerrain, string name) {
100 using (Stream stream = Storage.OpenFile(GetFileName(name), OpenFileMode.Read)) {
101 return LoadFurniturePack(subsystemTerrain, stream);
102 }
103 }
104
105 public static void ValidateFurniturePack(Stream stream) {
106 LoadFurniturePack(null, stream);
107 }
108
109 public static List<FurnitureDesign> LoadFurniturePack(SubsystemTerrain subsystemTerrain, Stream stream) {
110 using (ZipArchive zipArchive = ZipArchive.Open(stream, true)) {
111 List<ZipArchiveEntry> list = zipArchive.ReadCentralDir();
112 if (list.Count != 1
113 || list[0].FilenameInZip != "FurnitureDesigns.xml") {
114 throw new InvalidOperationException("Invalid furniture pack.");
115 }
116 MemoryStream memoryStream = new();
117 zipArchive.ExtractFile(list[0], memoryStream);
118 memoryStream.Position = 0L;
119 XElement overridesNode = XElement.Load(memoryStream);
120 ValuesDictionary valuesDictionary = new();
121 valuesDictionary.ApplyOverrides(overridesNode);
122 return SubsystemFurnitureBlockBehavior.LoadFurnitureDesigns(subsystemTerrain, valuesDictionary);
123 }
124 }
125 }
126}
static void CreateDirectory(string path)
static IEnumerable< string > ListFileNames(string path)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static string GetExtension(string path)
static DateTime GetFileLastWriteTime(string path)
static bool FileExists(string path)
static string GetFileNameWithoutExtension(string path)
static void DeleteFile(string path)
static string CombinePaths(params string[] paths)
static void ReportExceptionToUser(string additionalMessage, Exception e)
static DateTime GetCreationDate(string name)
static void ExportFurniturePack(string name, Stream stream)
static Action< string > FurniturePackDeleted
static ReadOnlyList< string > FurniturePackNames
static void ValidateFurniturePack(Stream stream)
static string GetDisplayName(string name)
static string GetFileName(string name)
static void DeleteFurniturePack(string name)
static string CreateFurniturePack(string name, ICollection< FurnitureDesign > designs)
static List< string > m_furniturePackNames
static string ImportFurniturePack(string name, Stream stream)
static List< FurnitureDesign > LoadFurniturePack(SubsystemTerrain subsystemTerrain, string name)
static List< FurnitureDesign > LoadFurniturePack(SubsystemTerrain subsystemTerrain, Stream stream)
static List< FurnitureDesign > LoadFurnitureDesigns(SubsystemTerrain subsystemTerrain, ValuesDictionary valuesDictionary)
static void SaveFurnitureDesigns(ValuesDictionary valuesDictionary, ICollection< FurnitureDesign > designs)
static ZipArchive Create(Stream stream, bool keepStreamOpen=false)
void ExtractFile(ZipArchiveEntry zfe, Stream stream)
void AddStream(string filenameInZip, Stream source)
List< ZipArchiveEntry > ReadCentralDir()
static ZipArchive Open(Stream stream, bool keepStreamOpen=false)
static string FurniturePacksDirectoryName
void ApplyOverrides(ValuesDictionary overridesValuesDictionary)