-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileManager.cs
More file actions
158 lines (131 loc) · 4.92 KB
/
Copy pathFileManager.cs
File metadata and controls
158 lines (131 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnityEngine;
namespace MapImporter
{
internal class FileManager
{
// Returns a list of all files in the given directory
public static string[] getFiles(string path)
{
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path);
return files;
}
return null;
}
// Reads a PNG file and returns a Texture2D object
public static Texture2D readPng(string filePath)
{
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath))
{
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); // Auto-resizes the texture dimensions.
}
else
{
Melon<Main>.Logger.Error("Could not find file");
return null;
}
return tex;
}
// Returns a list of all map folders with their subfiles in the given path
public static Dictionary<string, (string rawFile, string pngFile)> GetMapFiles(string path)
{
Dictionary<string, (string, string)> mapFolders = new Dictionary<string, (string, string)>();
if (Directory.Exists(path))
{
string[] directories = Directory.GetDirectories(path); // Get all folders inside Maps/
foreach (string dir in directories)
{
string rawFile = Directory.GetFiles(dir, "*.raw").FirstOrDefault();
string pngFile = Directory.GetFiles(dir, "*.png").FirstOrDefault();
if (!string.IsNullOrEmpty(rawFile))
{
mapFolders[dir] = (rawFile, pngFile);
}
}
}
else
{
Melon<Main>.Logger.Msg("Maps folder does not exist");
return null;
}
return mapFolders;
}
// Reads a raw file as a heightmap
public static float[,] ReadRawHeightmap(string filePath, uint resolution, bool is16Bit)
{
if (!File.Exists(filePath))
{
Melon<Main>.Logger.Msg($"RAW file not found: {filePath}");
return null;
}
byte[] rawData = File.ReadAllBytes(filePath);
int expectedSize = (int)resolution * (int)resolution * (is16Bit ? 2 : 1);
if (rawData.Length != expectedSize)
{
Melon<Main>.Logger.Msg($"Invalid RAW file size: Expected {expectedSize} bytes, got {rawData.Length} bytes.");
return null;
}
float[,] heights = new float[resolution, resolution];
int index = 0;
for (int y = 0; y < resolution; y++)
{
for (int x = 0; x < resolution; x++)
{
if (is16Bit)
{
// Read 16-bit value (Little Endian)
ushort value = (ushort)(rawData[index] | (rawData[index + 1] << 8));
heights[y, x] = value / 65535f; // Normalize to 0-1 range
index += 2;
}
else
{
// Read 8-bit value
heights[y, x] = rawData[index] / 255f;
index++;
}
}
}
Melon<Main>.Logger.Msg($"Successfully loaded heightmap from {filePath}");
return heights;
}
// Reads a text file and returns a heightmap
public static float[,] ReadTxtFile(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
StreamReader sr = fileInfo.OpenText();
string fl = sr.ReadLine();
if (fl != "HeightMap")
{
Melon<Main>.Logger.Msg("file not correct type");
return null;
}
int heightMapWidth = int.Parse(sr.ReadLine().Substring(16));
int heightMapHeight = int.Parse(sr.ReadLine().Substring(17));
int width = int.Parse(sr.ReadLine().Substring(14));
int length = int.Parse(sr.ReadLine().Substring(14));
int height = int.Parse(sr.ReadLine().Substring(14));
float[,] heights = new float[heightMapWidth, heightMapHeight];
for (int x = 0; x < heightMapWidth; x++)
{
for (int y = 0; y < heightMapHeight; y++)
{
heights[x, y] = float.Parse(sr.ReadLine()) / 100000;
}
}
return heights;
}
}
}