Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
VersionConverter21To22.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
4using XmlUtilities;
5
6namespace Game {
8 public override string SourceVersion => "2.1";
9
10 public override string TargetVersion => "2.2";
11
12 public override void ConvertProjectXml(XElement projectNode) {
13 XmlUtils.SetAttributeValue(projectNode, "Version", TargetVersion);
14 _ = string.Empty;
15 foreach (XElement item in from e in projectNode.Element("Subsystems").Elements()
16 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "GameInfo"
17 select e) {
18 foreach (XElement item2 in from e in item.Elements("Value")
19 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "TerrainGenerationMode"
20 select e) {
21 if (XmlUtils.GetAttributeValue(item2, "Value", "") == "Flat") {
22 XmlUtils.SetAttributeValue(item2, "Value", "FlatContinent");
23 }
24 }
25 }
26 foreach (XElement item3 in from e in projectNode.Element("Subsystems").Elements()
27 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Pickables"
28 select e) {
29 foreach (XElement item4 in from e in item3.Elements("Values")
30 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Pickables"
31 select e) {
32 foreach (XElement item5 in item4.Elements("Values")) {
33 foreach (XElement item6 in from e in item5.Elements("Value")
34 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Value"
35 select e) {
36 int num = ConvertValue(XmlUtils.GetAttributeValue<int>(item6, "Value"));
37 XmlUtils.SetAttributeValue(item6, "Value", num);
38 }
39 }
40 }
41 }
42 foreach (XElement item7 in from e in projectNode.Element("Subsystems").Elements()
43 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Projectiles"
44 select e) {
45 foreach (XElement item8 in from e in item7.Elements("Values")
46 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Projectiles"
47 select e) {
48 foreach (XElement item9 in item8.Elements("Values")) {
49 foreach (XElement item10 in from e in item9.Elements("Value")
50 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Value"
51 select e) {
52 int num2 = ConvertValue(XmlUtils.GetAttributeValue<int>(item10, "Value"));
53 XmlUtils.SetAttributeValue(item10, "Value", num2);
54 }
55 }
56 }
57 }
58 foreach (XElement item11 in from e in projectNode.Element("Subsystems").Elements()
59 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "CollapsingBlockBehavior"
60 select e) {
61 foreach (XElement item12 in from e in item11.Elements("Values")
62 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "CollapsingBlocks"
63 select e) {
64 foreach (XElement item13 in item12.Elements("Values")) {
65 foreach (XElement item14 in from e in item13.Elements("Value")
66 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Value"
67 select e) {
68 int num3 = ConvertValue(XmlUtils.GetAttributeValue<int>(item14, "Value"));
69 XmlUtils.SetAttributeValue(item14, "Value", num3);
70 }
71 }
72 }
73 }
74 foreach (XElement item15 in projectNode.Element("Entities").Elements()) {
75 foreach (XElement item16 in from e in item15.Elements("Values")
76 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Clothing"
77 select e) {
78 foreach (XElement item17 in from e in item16.Elements("Values")
79 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Clothes"
80 select e) {
81 foreach (XElement item18 in item17.Elements()) {
82 string attributeValue = XmlUtils.GetAttributeValue<string>(item18, "Value");
83 int[] array = HumanReadableConverter.ValuesListFromString<int>(';', attributeValue);
84 for (int i = 0; i < array.Length; i++) {
85 array[i] = ConvertValue(array[i]);
86 }
87 string value = HumanReadableConverter.ValuesListToString(';', array);
88 XmlUtils.SetAttributeValue(item18, "Value", value);
89 }
90 }
91 }
92 }
93 string[] inventoryNames = ["Inventory", "CreativeInventory", "CraftingTable", "Chest", "Furnace", "Dispenser"];
94 foreach (XElement item19 in projectNode.Element("Entities").Elements()) {
95 foreach (XElement item20 in from e in item19.Elements("Values")
96 where inventoryNames.Contains(XmlUtils.GetAttributeValue(e, "Name", string.Empty))
97 select e) {
98 foreach (XElement item21 in from e in item20.Elements("Values")
99 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Slots"
100 select e) {
101 foreach (XElement item22 in item21.Elements()) {
102 foreach (XElement item23 in from e in item22.Elements("Value")
103 where XmlUtils.GetAttributeValue(e, "Name", string.Empty) == "Contents"
104 select e) {
105 int num4 = ConvertValue(XmlUtils.GetAttributeValue<int>(item23, "Value"));
106 XmlUtils.SetAttributeValue(item23, "Value", num4);
107 }
108 }
109 }
110 }
111 }
112 }
113
114 public override void ConvertWorld(string directoryName) {
115 try {
116 ConvertChunks(directoryName);
117 ConvertProject(directoryName);
118 foreach (string item in from f in Storage.ListFileNames(directoryName) where Storage.GetExtension(f) == ".new" select f) {
119 string sourcePath = Storage.CombinePaths(directoryName, item);
120 string destinationPath = Storage.CombinePaths(directoryName, Storage.GetFileNameWithoutExtension(item));
121 Storage.MoveFile(sourcePath, destinationPath);
122 }
123 foreach (string item2 in from f in Storage.ListFileNames(directoryName) where Storage.GetExtension(f) == ".old" select f) {
124 Storage.DeleteFile(Storage.CombinePaths(directoryName, item2));
125 }
126 }
127 catch (Exception) {
128 foreach (string item3 in from f in Storage.ListFileNames(directoryName) where Storage.GetExtension(f) == ".old" select f) {
129 string sourcePath2 = Storage.CombinePaths(directoryName, item3);
130 string destinationPath2 = Storage.CombinePaths(directoryName, Storage.GetFileNameWithoutExtension(item3));
131 Storage.MoveFile(sourcePath2, destinationPath2);
132 }
133 foreach (string item4 in from f in Storage.ListFileNames(directoryName) where Storage.GetExtension(f) == ".new" select f) {
134 Storage.DeleteFile(Storage.CombinePaths(directoryName, item4));
135 }
136 throw;
137 }
138 }
139
140 public void ConvertProject(string directoryName) {
141 string path = Storage.CombinePaths(directoryName, "Project.xml");
142 string path2 = Storage.CombinePaths(directoryName, "Project.xml.new");
143 XElement xElement;
144 using (Stream stream = Storage.OpenFile(path, OpenFileMode.Read)) {
145 xElement = XmlUtils.LoadXmlFromStream(stream, null, true);
146 }
147 ConvertProjectXml(xElement);
148 using (Stream stream2 = Storage.OpenFile(path2, OpenFileMode.Create)) {
149 XmlUtils.SaveXmlToStream(xElement, stream2, null, true);
150 }
151 }
152
153 public void ConvertChunks(string directoryName) {
154 string path = Storage.CombinePaths(directoryName, "Chunks32.dat");
155 string path2 = Storage.CombinePaths(directoryName, "Chunks32h.dat.new");
156 long num = 2 * Storage.GetFileSize(path) + 52428800;
157 if (Storage.FreeSpace < num) {
158 throw new InvalidOperationException($"Not enough free space to convert world. {num / 1024 / 1024}MB required.");
159 }
160 using (Stream stream2 = Storage.OpenFile(path, OpenFileMode.Read)) {
161 using (Stream stream = Storage.OpenFile(path2, OpenFileMode.Create)) {
162 byte[] array = new byte[131072];
163 byte[] array2 = new byte[262144];
164 for (int i = 0; i < 65537; i++) {
165 TerrainSerializer22.WriteTOCEntry(stream, 0, 0, -1);
166 }
167 int num2 = 0;
168 while (true) {
169 stream2.Position = 12 * num2;
170 TerrainSerializer129.ReadTOCEntry(stream2, out int cx, out int cz, out int index);
171 if (index < 0) {
172 break;
173 }
174 stream.Position = 12 * num2;
175 TerrainSerializer22.WriteTOCEntry(stream, cx, cz, num2);
176 stream2.Position = 786444 + 132112L * index;
177 stream.Position = stream.Length;
180 stream2.ReadExactly(array, 0, 131072);
181 int num3 = 0;
182 int num4 = 0;
183 for (int j = 0; j < 16; j++) {
184 for (int k = 0; k < 16; k++) {
185 for (int l = 0; l < 256; l++) {
186 int num5;
187 if (l <= 127) {
188 num5 = ConvertValue(
189 array[4 * num3] | (array[4 * num3 + 1] << 8) | (array[4 * num3 + 2] << 16) | (array[4 * num3 + 3] << 24)
190 );
191 num3++;
192 }
193 else {
194 num5 = 0;
195 }
196 array2[4 * num4] = (byte)num5;
197 array2[4 * num4 + 1] = (byte)(num5 >> 8);
198 array2[4 * num4 + 2] = (byte)(num5 >> 16);
199 array2[4 * num4 + 3] = (byte)(num5 >> 24);
200 num4++;
201 }
202 }
203 }
204 stream.Write(array2, 0, 262144);
205 stream2.ReadExactly(array, 0, 1024);
206 stream.Write(array, 0, 1024);
207 num2++;
208 }
209 }
210 }
211 Storage.MoveFile(Storage.CombinePaths(directoryName, "Chunks32.dat"), Storage.CombinePaths(directoryName, "Chunks32.dat.old"));
212 }
213
214 public static int ConvertValue(int value) {
215 int contents = value & 0x3FF;
216 int light = (value >> 10) & 0xF;
217 int data = value >> 14;
218 ConvertContentsLightData(ref contents, ref light, ref data);
219 return contents | (light << 10) | (data << 14);
220 }
221
222 public static void ConvertContentsLightData(ref int contents, ref int light, ref int data) {
223 if (contents == 30) {
224 contents = 29;
225 }
226 if (contents == 34) {
227 contents = 29;
228 }
229 if (contents == 32) {
230 contents = 29;
231 }
232 if (contents == 35) {
233 contents = 29;
234 }
235 if (contents == 33) {
236 contents = 29;
237 }
238 if (contents == 170) {
239 contents = 169;
240 }
241 if (contents == 122) {
242 contents = 122;
243 }
244 if (contents == 123) {
245 contents = 123;
246 }
247 }
248 }
249}
static void MoveFile(string sourcePath, string destinationPath)
static IEnumerable< string > ListFileNames(string path)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static string GetExtension(string path)
static string GetFileNameWithoutExtension(string path)
static long FreeSpace
static long GetFileSize(string path)
static void DeleteFile(string path)
static string CombinePaths(params string[] paths)
static void ReadChunkHeader(Stream stream)
static void ReadTOCEntry(Stream stream, out int cx, out int cz, out int index)
static void WriteChunkHeader(Stream stream, int cx, int cz)
static void WriteTOCEntry(Stream stream, int cx, int cz, int index)
static void ConvertContentsLightData(ref int contents, ref int light, ref int data)
void ConvertChunks(string directoryName)
override void ConvertWorld(string directoryName)
void ConvertProject(string directoryName)
override void ConvertProjectXml(XElement projectNode)
static void SetAttributeValue(XElement node, string attributeName, object value)
static object GetAttributeValue(XElement node, string attributeName, Type type)
static XElement LoadXmlFromStream(Stream stream, Encoding encoding, bool throwOnError)
static void SaveXmlToStream(XElement node, Stream stream, Encoding encoding, bool throwOnError)