[130] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
| 3 | using System.Threading;
|
---|
| 4 | using UnityEngine;
|
---|
| 5 |
|
---|
| 6 | namespace AllocsFixes.MapRendering
|
---|
| 7 | {
|
---|
| 8 | public class MapRenderBlockBuffer
|
---|
| 9 | {
|
---|
| 10 | private int zoomLevel;
|
---|
| 11 | private string currentBlockMap = string.Empty;
|
---|
| 12 | private Texture2D blockMap = new Texture2D (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE);
|
---|
| 13 | private Texture2D zoomBuffer = new Texture2D (1, 1);
|
---|
| 14 | private Color nullColor = new Color (0, 0, 0, 0);
|
---|
| 15 |
|
---|
| 16 | public MapRenderBlockBuffer (int level)
|
---|
| 17 | {
|
---|
| 18 | zoomLevel = level;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[168] | 21 | public void ResetBlock ()
|
---|
| 22 | {
|
---|
| 23 | currentBlockMap = string.Empty;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[130] | 26 | public void SaveBlock ()
|
---|
| 27 | {
|
---|
| 28 | try {
|
---|
| 29 | if (currentBlockMap.Length > 0)
|
---|
| 30 | saveTextureToFile (currentBlockMap);
|
---|
| 31 | } catch (Exception e) {
|
---|
| 32 | Log.Out ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[143] | 36 | public bool LoadBlock (Vector2i block)
|
---|
[130] | 37 | {
|
---|
[143] | 38 | bool res = false;
|
---|
[130] | 39 | Monitor.Enter (blockMap);
|
---|
| 40 | try {
|
---|
| 41 | string folder = Constants.MAP_DIRECTORY + "/" + (zoomLevel) + "/" + block.x;
|
---|
| 42 | string fileName = folder + "/" + block.y + ".png";
|
---|
| 43 | Directory.CreateDirectory (folder);
|
---|
| 44 | if (!fileName.Equals (currentBlockMap)) {
|
---|
[143] | 45 | res = true;
|
---|
[168] | 46 | SaveBlock ();
|
---|
[130] | 47 | loadTextureFromFile (fileName);
|
---|
| 48 | }
|
---|
| 49 | currentBlockMap = fileName;
|
---|
| 50 | } finally {
|
---|
| 51 | Monitor.Exit (blockMap);
|
---|
| 52 | }
|
---|
[143] | 53 | return res;
|
---|
[130] | 54 | }
|
---|
| 55 |
|
---|
| 56 | public void SetPart (Vector2i offset, int partSize, Color[] pixels)
|
---|
| 57 | {
|
---|
| 58 | blockMap.SetPixels (offset.x, offset.y, partSize, partSize, pixels);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public Color[] GetHalfScaled ()
|
---|
| 62 | {
|
---|
| 63 | zoomBuffer.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE);
|
---|
| 64 | zoomBuffer.SetPixels (blockMap.GetPixels ());
|
---|
| 65 |
|
---|
| 66 | TextureScale.Point (zoomBuffer, Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2);
|
---|
| 67 |
|
---|
| 68 | return zoomBuffer.GetPixels ();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private void loadTextureFromFile (string _fileName)
|
---|
| 72 | {
|
---|
| 73 | try {
|
---|
| 74 | byte[] array = File.ReadAllBytes (_fileName);
|
---|
| 75 | blockMap.LoadImage (array);
|
---|
| 76 | } catch (Exception) {
|
---|
| 77 | for (int x = 0; x < Constants.MAP_BLOCK_SIZE; x++) {
|
---|
| 78 | for (int y = 0; y < Constants.MAP_BLOCK_SIZE; y++) {
|
---|
| 79 | blockMap.SetPixel (x, y, nullColor);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private void saveTextureToFile (string _fileName)
|
---|
| 86 | {
|
---|
| 87 | try {
|
---|
| 88 | byte[] array = blockMap.EncodeToPNG ();
|
---|
| 89 | File.WriteAllBytes (_fileName, array);
|
---|
| 90 | } catch (Exception e) {
|
---|
| 91 | Log.Out ("Exception in MapRenderBlockBuffer.saveTextureToFile(): " + e);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 | }
|
---|