示例#1
0
 public static void RepairWorldIfNeeded(string directoryName)
 {
     try
     {
         string text = Storage.CombinePaths(directoryName, "Project.xml");
         if (!TestXmlFile(text, "Project"))
         {
             Log.Warning($"Project file at \"{text}\" is corrupt or nonexistent. Will try copying data from the backup file. If that fails, will try making a recovery project file.");
             string text2 = Storage.CombinePaths(directoryName, "Project.bak");
             if (TestXmlFile(text2, "Project"))
             {
                 Storage.CopyFile(text2, text);
             }
             else
             {
                 string path = Storage.CombinePaths(directoryName, "Chunks.dat");
                 if (!Storage.FileExists(path))
                 {
                     throw new InvalidOperationException("Recovery project file could not be generated because chunks file does not exist.");
                 }
                 XElement xElement = ContentManager.Get <XElement>("RecoveryProject");
                 using (Stream stream = Storage.OpenFile(path, OpenFileMode.Read))
                 {
                     TerrainSerializer14.ReadTOCEntry(stream, out int cx, out int cz, out int _);
                     Vector3 vector = new Vector3(16 * cx, 255f, 16 * cz);
                     xElement.Element("Subsystems").Element("Values").Element("Value")
                     .Attribute("Value")
                     .SetValue(HumanReadableConverter.ConvertToString(vector));
                 }
                 using (Stream stream2 = Storage.OpenFile(text, OpenFileMode.Create))
                 {
                     XmlUtils.SaveXmlToStream(xElement, stream2, null, throwOnError: true);
                 }
             }
         }
     }
     catch (Exception)
     {
         throw new InvalidOperationException("The world files are corrupt and could not be repaired.");
     }
 }
示例#2
0
        public void ConvertChunks(string directoryName)
        {
            string path  = Storage.CombinePaths(directoryName, "Chunks.dat");
            string path2 = Storage.CombinePaths(directoryName, "Chunks32.dat");

            using (Stream stream2 = Storage.OpenFile(path, OpenFileMode.Read))
            {
                using (Stream stream = Storage.OpenFile(path2, OpenFileMode.Create))
                {
                    byte[] array  = new byte[65536];
                    byte[] array2 = new byte[131072];
                    for (int i = 0; i < 65537; i++)
                    {
                        TerrainSerializer129.WriteTOCEntry(stream, 0, 0, -1);
                    }
                    int num = 0;
                    while (true)
                    {
                        stream2.Position = 12 * num;
                        TerrainSerializer14.ReadTOCEntry(stream2, out int cx, out int cz, out int offset);
                        if (offset == 0)
                        {
                            break;
                        }
                        stream.Position = 12 * num;
                        TerrainSerializer129.WriteTOCEntry(stream, cx, cz, num);
                        stream2.Position = offset;
                        stream.Position  = stream.Length;
                        TerrainSerializer14.ReadChunkHeader(stream2);
                        TerrainSerializer129.WriteChunkHeader(stream, cx, cz);
                        stream2.Read(array, 0, 65536);
                        int num2 = 0;
                        for (int j = 0; j < 16; j++)
                        {
                            for (int k = 0; k < 16; k++)
                            {
                                for (int l = 0; l < 128; l++)
                                {
                                    int num3 = array[2 * num2] | (array[2 * num2 + 1] << 8);
                                    int num4 = ConvertValue(num3);
                                    if (l < 127)
                                    {
                                        int num5 = num3 & 0xFF;
                                        if (num5 == 18 || num5 == 92)
                                        {
                                            int num6 = (array[2 * num2 + 2] | (array[2 * num2 + 3] << 8)) & 0xFF;
                                            if (num5 != num6)
                                            {
                                                num4 |= 0x40000;
                                            }
                                        }
                                    }
                                    array2[4 * num2]     = (byte)num4;
                                    array2[4 * num2 + 1] = (byte)(num4 >> 8);
                                    array2[4 * num2 + 2] = (byte)(num4 >> 16);
                                    array2[4 * num2 + 3] = (byte)(num4 >> 24);
                                    num2++;
                                }
                            }
                        }
                        stream.Write(array2, 0, 131072);
                        stream2.Read(array, 0, 1024);
                        stream.Write(array, 0, 1024);
                        num++;
                    }
                }
            }
            Storage.DeleteFile(path);
        }
        public override void ConvertWorld(string directoryName)
        {
            string[] array = Storage.ListFileNames(Storage.CombinePaths(directoryName, "Chunks")).ToArray();
            string[] array2;
            using (Stream stream = Storage.OpenFile(Storage.CombinePaths(directoryName, "Chunks.dat"), OpenFileMode.Create))
            {
                for (int i = 0; i < 65537; i++)
                {
                    TerrainSerializer14.WriteTOCEntry(stream, 0, 0, 0);
                }
                int num = 0;
                array2 = array;
                foreach (string text in array2)
                {
                    try
                    {
                        if (num >= 65536)
                        {
                            throw new InvalidOperationException("Too many chunks.");
                        }
                        string[] array3 = Storage.GetFileNameWithoutExtension(text).Split('_', StringSplitOptions.None);
                        int      cx     = int.Parse(array3[1], CultureInfo.InvariantCulture);
                        int      cz     = int.Parse(array3[2], CultureInfo.InvariantCulture);
                        using (Stream stream2 = Storage.OpenFile(Storage.CombinePaths(directoryName, Storage.CombinePaths("Chunks", text)), OpenFileMode.Read))
                        {
                            byte[] array4 = new byte[stream2.Length];
                            stream2.Read(array4, 0, array4.Length);
                            int num2 = (int)stream.Length;
                            stream.Position = num2;
                            TerrainSerializer14.WriteChunkHeader(stream, cx, cz);
                            stream.Write(array4, 0, array4.Length);
                            stream.Position = num * 4 * 3;
                            TerrainSerializer14.WriteTOCEntry(stream, cx, cz, num2);
                            num++;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error($"Error converting chunk file \"{text}\". Skipping chunk. Reason: {ex.Message}");
                    }
                }
                stream.Flush();
                Log.Information($"Converted {num} chunk(s).");
            }
            string   path = Storage.CombinePaths(directoryName, "Project.xml");
            XElement xElement;

            using (Stream stream3 = Storage.OpenFile(path, OpenFileMode.Read))
            {
                xElement = XmlUtils.LoadXmlFromStream(stream3, null, throwOnError: true);
            }
            ConvertProjectXml(xElement);
            using (Stream stream4 = Storage.OpenFile(path, OpenFileMode.Create))
            {
                XmlUtils.SaveXmlToStream(xElement, stream4, null, throwOnError: true);
            }
            array2 = array;
            foreach (string text2 in array2)
            {
                Storage.DeleteFile(Storage.CombinePaths(directoryName, Storage.CombinePaths("Chunks", text2)));
            }
            Storage.DeleteDirectory(Storage.CombinePaths(directoryName, "Chunks"));
        }