forked from ParanormalVibe/Vox2Cub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVox2CubApp.cs
129 lines (117 loc) · 4.75 KB
/
Vox2CubApp.cs
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
using System;
using System.IO;
using Voxels;
namespace Vox2Cub
{
class Vox2CubApp
{
static void Main(string[] args)
{
string inputDirPath = GetInputDirectory();
DirectoryInfo inputDirectory = new DirectoryInfo(inputDirPath);
string outputDir = Environment.CurrentDirectory +
@"\converted files\";
StageOutputDirectory(outputDir);
var inputFiles = inputDirectory.GetFiles();
int progress = 1;
int fileCount = inputFiles.Length;
foreach (var file in inputFiles)
{
var importedData = VoxelImport.Import(file.FullName);
var origFileName = Path.GetFileNameWithoutExtension(file.FullName);
string outputFilePath = outputDir + origFileName + ".cub";
Console.WriteLine(progress + "/" + fileCount
+ " - " + origFileName);
// Voxels library only works with .vox and .qb files.
if (file.Extension == ".vox" || file.Extension == ".qb")
CreateCubFile(importedData, outputFilePath);
progress++;
}
Console.WriteLine("Success! All converted files are in \""
+ outputDir + "\"");
Console.ReadLine();
}
static string GetInputDirectory()
{
Console.Write("vox/cb file folder: ");
string targetDir = Console.ReadLine();
try
{
VerifyDirectoryPath(targetDir);
}
catch (ArgumentException)
{
Console.WriteLine("The path \"" + targetDir + "\" " +
"is invalid. Please enter a valid folder path.");
targetDir = GetInputDirectory();
}
catch (System.Security.SecurityException)
{
Console.WriteLine("Insufficient permissions to access \""
+ targetDir + "\".");
Console.WriteLine("Select another folder or run this " +
"application as an administrator and try again.");
targetDir = GetInputDirectory();
}
catch (NotSupportedException)
{
Console.WriteLine("The path \"" + targetDir + "\" " +
"contains a colon ':' outside of the volume identifier." +
" Please try again.");
targetDir = GetInputDirectory();
}
catch (PathTooLongException)
{
Console.WriteLine("The path \" " + targetDir + "\" " +
"is too long. Please enter a valid folder path.");
targetDir = GetInputDirectory();
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("The path \" " + targetDir + "\" " +
"could not be found. Please enter a valid folder path.");
targetDir = GetInputDirectory();
}
return targetDir;
}
static void StageOutputDirectory(string outputDir)
{
if (!Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);
foreach (FileInfo file in new DirectoryInfo(outputDir).GetFiles())
file.Delete();
}
static void CreateCubFile(VoxelData origData, string outputPath)
{
using (BinaryWriter writeBinary = new BinaryWriter(File.Open
(outputPath, FileMode.Create)))
{
writeBinary.Write(origData.size.X);
writeBinary.Write(origData.size.Y);
writeBinary.Write(origData.size.Z);
for (int x = 0; x < origData.size.X; x++)
for (int y = 0; y < origData.size.Y; y++)
for (int z = 0; z < origData.size.Z; z++)
{
var currentPos = new XYZ(x, y, z);
if (origData.IsValid(currentPos))
{
var voxelColor = origData[currentPos].Color;
writeBinary.Write(voxelColor.R);
writeBinary.Write(voxelColor.G);
writeBinary.Write(voxelColor.B);
}
else
writeBinary.Write(000);
}
writeBinary.Close();
}
}
static void VerifyDirectoryPath(string path)
{
Path.GetFullPath(path);
if (!Directory.Exists(path))
throw new DirectoryNotFoundException();
}
}
}