Beispiel #1
0
        //---------------------------------


        public void ConvertMap()
        {
            Log("Loading map into memory.", true);
            tileMatrix.PreLoadMap(task_mapstatics, ProgressSetMax, ProgressSetVal, Log);
            Log("\nMap loaded!\n", true);

            int totBlocks = (width / 64) * (height / 64);

            ProgressSetMax(totBlocks);


            Log("Conversion started.", true);
            try
            {
                //WriteMapResolution();
                //-From my experience, we don't need this file, even if we create a facet UOP without this it will work fine.
                //-Moreover, we don't have the real name of this file, so we can't repack it. When i'll find out this name i'll uncomment the line (and fix the output name).

                //for (int blocksX = 0; blocksX < (tileMatrix.Width / 64); blocksX++)
                Parallel.For(0, (tileMatrix.Width / 64), blocksX =>
                {
                    for (int blocksY = 0; blocksY < (tileMatrix.Height / 64); blocksY++)
                    {
                        WriteMapBlock(blocksX, blocksY, ((blocksX * 64) + blocksY));
                        //tileMatrix.DisposeMapBlock(blocksX, blocksY);
                        //   I can't dispose the map block because i may need a different block from the current when writing delimiters
                    }
                });
            }
            catch (Exception e) // (ThreadAbortException e)
            {
                Log(String.Format("\nConversion ERROR: ABORTING. Message: {0}", e.Message), true);
                SetItemsState(true);
                ProgressSetVal(0);
                return;
            }
            finally
            {
                Log("\nConversion ended successifully!", true);
            }
            //Saving memory
            if (task_dds)
            {
                radarcol = null;
            }
            tileMatrix.Dispose();
            GC.Collect();       //needed, otherwise it won't readily free the memory
            GC.WaitForPendingFinalizers();


            if (task_uop)
            {
                ProgressSetVal(0);
                ProgressSetMax(totBlocks);
                MythicPackage package = new MythicPackage(5);

                Log("\nStarting to pack the files into the .uop package... ", true);
                try
                {
                    for (int i = 0; i < totBlocks; i++)
                    {
                        string binFileName = binPath + string.Format("{0:D8}.bin", i);
                        package.AddFile(binFileName, string.Format("build/sectors/facet_0{0}/", mapIndex), CompressionFlag.Zlib);
                        ProgressIncrease();
                    }
                    package.Save(outPath + string.Format("facet{0}.uop", mapIndex));
                }
                catch (System.IO.IOException e)
                {
                    Log(String.Format("\nUop packing ERROR: skipping. Message: {0}", e.Message), true);
                    File.Delete(outPath + string.Format("facet{0}.uop", mapIndex));
                }
                finally
                {
                    Log("Done!", true);
                }
                //Saving memory
                for (int i = 0; i < package.Blocks.Count; i++)
                {
                    package.RemoveBlock(i);
                }
                package = null;
                //GC.Collect();
                //GC.WaitForPendingFinalizers();

                Log("\nRemoving .bin temporary files... ", true);
                try
                {
                    Directory.Delete(outPath + "build", true);
                }
                catch (System.IO.IOException e)
                {
                    Log(String.Format("\nRemoving .bin temporary files ERROR: skipping. Message: {0}", e.Message), true);
                }
                finally
                {
                    Log("Done!", true);
                }
            }


            if (task_dds)
            {
                Log("\nStarting to render the dds image... ", true);
                ProgressSetMarquee(true);
                try
                {
                    // Unlock the bits.
                    unsafe { bmpPtr = null; }
                    bmp.UnlockBits(bmpData);

                    /*
                     * MemoryStream bmpStream = new MemoryStream();
                     * bmp.Save(bmpStream, ImageFormat.Bmp);
                     * byte[] bmpArray = bmpStream.ToArray();
                     */

                    ImageConverter converter = new ImageConverter();
                    byte[]         bmpArray  = (byte[])converter.ConvertTo(bmp, typeof(byte[]));
                    bmp.Dispose();
                    using (MagickImage bmpMagick = new MagickImage(bmpArray))
                    {
                        bmpMagick.Format = MagickFormat.Dds;
                        bmpMagick.Settings.SetDefine(MagickFormat.Dds, "compression", "dxt1");  // convert to a DXT1 DDS
                        using (FileStream fileS = File.Create(outPath + string.Format("facet0{0}.dds", mapIndex)))
                        {
                            bmpMagick.Write(fileS);
                        }
                    }

                    Log("Done!", true);

                    //Saving memory
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                catch (Exception e) //(System.IO.IOException e)
                {
                    Log(String.Format("\nDDS rendering ERROR: skipping. Message: {0}", e.Message), true);
                    File.Delete(outPath + string.Format("facet0{0}.dds", mapIndex));
                }
                ProgressSetMarquee(false);
            }


            if (!task_uop)
            {
                Log(string.Format("\nSuccess!\n" +
                                  "Generated files are raw .bin binary files, " +
                                  "you need to repack them using Mythic Package Editor.\n" +
                                  "Remember, you should use zlib compression and add the files " +
                                  "with the directory tree (\"build/sectors/facet_0{0}/\".\n" +
                                  "Then, you can pack it all into facet{0}.uop\n\n", mapIndex), true);
            }
            else
            {
                Log("\nSuccess!\n", true);
            }
            Success();
        }