Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylancyclone committed Apr 28, 2021
2 parents e303d62 + ce81fc9 commit 9fa5eb0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 104 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ local.properties
# Annotation Processing
.apt_generated

.sts4-cache/
.sts4-cache/
.vscode/
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
4/27/2021 1.1.3:
* Improve transparent material compatibility
* Fix handling materials with keyless commands/values
* Move all temporary files to system temp folder

7/08/2020 1.1.2:
* Massively improve conversion speed (up to 670% on large maps)
* Fix Crowbar `DecompileFolderForEachModelIsChecked` setting causing issues
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.lathrum.VMF2OBJ</groupId>
<artifactId>VMF2OBJ</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
<packaging>jar</packaging>

<name>VMF2OBJ</name>
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
182 changes: 83 additions & 99 deletions src/main/java/com/lathrum/VMF2OBJ/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public static void printProgressBar(String text) {
* @throws Exception Unhandled exception
*/
public static void main(String[] args) throws Exception {
// The General outline of the program is a follows:
// The General outline of the program is as follows:

// Read Geometry
// Collapse Vertices
Expand Down Expand Up @@ -382,6 +382,7 @@ public static void main(String[] args) throws Exception {
String outPath = "";
String objName = "";
String matLibName = "";
final File tempDir;

// Prepare Arguments
try {
Expand Down Expand Up @@ -427,15 +428,23 @@ public static void main(String[] args) throws Exception {

// Clean working directory
try {
deleteRecursiveByExtension(new File(Paths.get(outPath).getParent().resolve("materials").toString()), "vtf");
deleteRecursive(new File(Paths.get(outPath).getParent().resolve("models").toString()));
} catch (Exception e) {
// System.err.println("Exception: "+e);
}
deleteRecursiveByExtension(new File(Paths.get(outPath).getParent().resolve("materials").toString()), "vtf"); // Delete unconverted textures
} catch (Exception ignored) {}

tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "vmf2objtemp");
tempDir.mkdirs();
// When the program shuts down, delete temporary directory
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
deleteRecursive(tempDir);
} catch (Exception ignored) { ignored.printStackTrace();}
}
});

// Extract Libraries
try {
extractLibraries(Paths.get(outPath).getParent().resolve("temp").toString());
extractLibraries(tempDir.getAbsolutePath());
} catch (Exception e) {
System.err.println("Failed to extract tools, do you have permissions?");
System.err.println(e.toString());
Expand Down Expand Up @@ -642,35 +651,27 @@ public static void main(String[] args) throws Exception {
VTFLibPath,
"-folder", formatPath(materialOutPath.toString()),
"-output", formatPath(materialOutPath.getParent()),
"-exportformat", "jpg"};

"-exportformat", "tga",
"-format", "BGR888"};

if (vmt.translucent == 1 || vmt.alphatest == 1) {
command[6] = "tga"; // If the texture is translucent, use the targa format
command[8] = "BGRA8888"; // Only include alpha channel if it's a transparent texture
}

proc = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((reader.readLine()) != null) {}
proc.waitFor();
// materialOutPath.delete();
if (vmt.translucent == 1 || vmt.alphatest == 1) {
materialOutPath = new File(
materialOutPath.toString().substring(0, materialOutPath.toString().lastIndexOf('.')) + ".tga");
} else {
materialOutPath = new File(
materialOutPath.toString().substring(0, materialOutPath.toString().lastIndexOf('.')) + ".jpg");
}
materialOutPath = new File(
materialOutPath.toString().substring(0, materialOutPath.toString().lastIndexOf('.')) + ".tga");

int width = 1;
int height = 1;
BufferedImage bimg;
try {
if (vmt.translucent == 1 || vmt.alphatest == 1) {
byte[] fileContent = Files.readAllBytes(materialOutPath.toPath());
bimg = TargaReader.decode(fileContent);
} else {
bimg = ImageIO.read(materialOutPath);
}
width = bimg.getWidth();
height = bimg.getHeight();
byte[] fileContent = Files.readAllBytes(materialOutPath.toPath());
width = TargaReader.getWidth(fileContent);
height = TargaReader.getHeight(fileContent);
} catch (Exception e) {
System.out.println("Cant read Material: " + materialOutPath);
// System.out.println(e);
Expand Down Expand Up @@ -709,8 +710,11 @@ public static void main(String[] args) throws Exception {
VTFLibPath,
"-folder", formatPath(bumpMapOutPath.toString()),
"-output", formatPath(bumpMapOutPath.getParent()),
"-exportformat", "jpg"};
"-exportformat", "tga",
"-format", "BGR888"};
proc = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((reader.readLine()) != null) {}
proc.waitFor();
} catch (Exception e) {
System.err.println("Failed to extract bump material: " + vmt.bumpmap);
Expand All @@ -725,20 +729,16 @@ public static void main(String[] args) throws Exception {
"Ka 1.000 1.000 1.000\n" +
"Kd 1.000 1.000 1.000\n" +
"Ks 0.000 0.000 0.000\n" +
"d 1.0\n" +
"illum 2");
if (vmt.translucent == 1 || vmt.alphatest == 1) {
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".tga" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
} else {
"map_Ka " + "materials/" + vmt.basetexture + ".tga\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
if (vmt.bumpmap != null) { //If the material has a bump map associated with it
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".jpg" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".jpg");
"map_bump " + "materials/" + vmt.bumpmap + ".tga");
}
if (vmt.bumpmap != null) { //If the material has a bump map associated with it
if (vmt.translucent == 1 || vmt.alphatest == 1) { // If the material has transparency
materialFile.println(
"map_bump " + "materials/" + vmt.bumpmap + ".jpg");
// "map_d -imfchan m" + "materials/" + vmt.basetexture + ".tga\n" + // Many programs fail to import the alpha channel of the tga file and instead import the color channel
"illum 4");
}
materialFile.println();
} else { // File has already been extracted
Expand All @@ -754,20 +754,16 @@ public static void main(String[] args) throws Exception {
"Ka 1.000 1.000 1.000\n" +
"Kd 1.000 1.000 1.000\n" +
"Ks 0.000 0.000 0.000\n" +
"d 1.0\n" +
"illum 2");
if (vmt.translucent == 1 || vmt.alphatest == 1) {
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".tga" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
} else {
"map_Ka " + "materials/" + vmt.basetexture + ".tga\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
if (vmt.bumpmap != null) { //If the material has a bump map associated with it
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".jpg" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".jpg");
"map_bump " + "materials/" + vmt.bumpmap + ".tga");
}
if (vmt.bumpmap != null) { //If the material has a bump map associated with it
if (vmt.translucent == 1 || vmt.alphatest == 1) { // If the material has transparency
materialFile.println(
"map_bump " + "materials/" + vmt.bumpmap + ".jpg");
// "map_d -imfchan m" + "materials/" + vmt.basetexture + ".tga\n" + // Many programs fail to import the alpha channel of the tga file and instead import the color channel
"illum 4");
}
materialFile.println();
}
Expand Down Expand Up @@ -938,7 +934,7 @@ public static void main(String[] args) throws Exception {

// Crowbar has a setting that puts all decompiled models in a subfolder
// called `DecompileFolderForEachModelIsChecked`. This is false by default,
// but must be handled otherwise all model will fail to convert
// but must be handled otherwise all models will fail to convert
Boolean crowbarSubfolderSetting = false;

String modelWithoutExtension = entity.model.substring(0, entity.model.lastIndexOf('.'));
Expand All @@ -952,9 +948,9 @@ public static void main(String[] args) throws Exception {
for (int index : indicies) {

if (index != -1) {
File fileOutPath = new File(outPath);
File fileOutPath = tempDir;
fileOutPath = new File(
formatPath(fileOutPath.getParent() + File.separator + vpkEntries.get(index).getFullPath()));
formatPath(fileOutPath + File.separator + vpkEntries.get(index).getFullPath()));
if (!fileOutPath.exists()) {
try {
File directory = new File(fileOutPath.getParent());
Expand All @@ -977,7 +973,7 @@ public static void main(String[] args) throws Exception {

String[] command = new String[] {
CrowbarLibPath,
"-p", formatPath(new File(outPath).getParent() + File.separator + entity.model)};
"-p", formatPath(tempDir + File.separator + entity.model)};

proc = Runtime.getRuntime().exec(command);
// BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
Expand All @@ -992,7 +988,7 @@ public static void main(String[] args) throws Exception {
String qcText = "";
try {
// This line may cause errors if the qc file does not have the same name as the mdl file
qcText = readFile(new File(outPath).getParent() + File.separator + modelWithoutExtension + ".qc");
qcText = readFile(tempDir + File.separator + modelWithoutExtension + ".qc");
} catch (IOException e) {
//This will be caught by detecting a blank string
// System.out.println("Exception Occured: " + e.toString());
Expand All @@ -1001,7 +997,7 @@ public static void main(String[] args) throws Exception {
try {
crowbarSubfolderSetting = true;
// This line may cause errors if the qc file does not have the same name as the mdl file
qcText = readFile(new File(outPath).getParent() + File.separator + modelWithoutExtension + File.separator + entity.modelName + ".qc");
qcText = readFile(tempDir + File.separator + modelWithoutExtension + File.separator + entity.modelName + ".qc");
} catch (IOException e) {
//This will be caught by detecting a blank string
// System.out.println("Exception Occured: " + e.toString());
Expand All @@ -1025,7 +1021,7 @@ public static void main(String[] args) throws Exception {
path += File.separator + entity.modelName;
}
String smdText = readFile(formatPath(
new File(outPath).getParent() + File.separator + "models" + path + File.separator + bodyGroup));
tempDir + File.separator + "models" + path + File.separator + bodyGroup));

SMDTriangles.addAll(Arrays.asList(SMDTriangle.parseSMD(smdText)));
}
Expand Down Expand Up @@ -1149,35 +1145,27 @@ public static void main(String[] args) throws Exception {
VTFLibPath,
"-folder", formatPath(materialOutPath.toString()),
"-output", formatPath(materialOutPath.getParent()),
"-exportformat", "jpg"};

"-exportformat", "tga",
"-format", "BGR888"};

if (vmt.translucent == 1 || vmt.alphatest == 1) {
convertCommand[6] = "tga"; // If the texture is translucent, use the targa format
convertCommand[8] = "BGRA8888"; // Only include alpha channel if it's a transparent texture
}

proc = Runtime.getRuntime().exec(convertCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((reader.readLine()) != null) {}
proc.waitFor();
// materialOutPath.delete();
if (vmt.translucent == 1 || vmt.alphatest == 1) {
materialOutPath = new File(
materialOutPath.toString().substring(0, materialOutPath.toString().lastIndexOf('.')) + ".tga");
} else {
materialOutPath = new File(
materialOutPath.toString().substring(0, materialOutPath.toString().lastIndexOf('.')) + ".jpg");
}
materialOutPath = new File(
materialOutPath.toString().substring(0, materialOutPath.toString().lastIndexOf('.')) + ".tga");

int width = 1;
int height = 1;
BufferedImage bimg;
try {
if (vmt.translucent == 1 || vmt.alphatest == 1) {
byte[] fileContent = Files.readAllBytes(materialOutPath.toPath());
bimg = TargaReader.decode(fileContent);
} else {
bimg = ImageIO.read(materialOutPath);
}
width = bimg.getWidth();
height = bimg.getHeight();
byte[] fileContent = Files.readAllBytes(materialOutPath.toPath());
width = TargaReader.getWidth(fileContent);
height = TargaReader.getHeight(fileContent);
} catch (Exception e) {
System.out.println("Cant read Material: " + materialOutPath);
// System.out.println(e);
Expand Down Expand Up @@ -1216,8 +1204,11 @@ public static void main(String[] args) throws Exception {
VTFLibPath,
"-folder", formatPath(bumpMapOutPath.toString()),
"-output", formatPath(bumpMapOutPath.getParent()),
"-exportformat", "jpg"};
"-exportformat", "tga",
"-format", "BGR888"};
proc = Runtime.getRuntime().exec(convertCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((reader.readLine()) != null) {}
proc.waitFor();
} catch (Exception e) {
System.err.println("Failed to extract bump material: " + vmt.bumpmap);
Expand All @@ -1232,18 +1223,15 @@ public static void main(String[] args) throws Exception {
"Ka 1.000 1.000 1.000\n" +
"Kd 1.000 1.000 1.000\n" +
"Ks 0.000 0.000 0.000\n" +
"d 1.0\n" +
"illum 2");
if (vmt.translucent == 1 || vmt.alphatest == 1) {
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".tga" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
} else {
materialFile.println("map_Ka " + "materials/" + vmt.basetexture + ".jpg" + "\n" + "map_Kd "
+ "materials/" + vmt.basetexture + ".jpg");
}
"map_Ka " + "materials/" + vmt.basetexture + ".tga\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
if (vmt.bumpmap != null) { // If the material has a bump map associated with it
materialFile.println("map_bump " + "materials/" + vmt.bumpmap + ".jpg");
materialFile.println("map_bump " + "materials/" + vmt.bumpmap + ".tga");
}
if (vmt.translucent == 1 || vmt.alphatest == 1) { // If the material has transparency
materialFile.println(
// "map_d -imfchan m" + "materials/" + vmt.basetexture + ".tga\n" + // Many programs fail to import the alpha channel of the tga file and instead import the color channel
"illum 4");
}
materialFile.println();
} else { // File has already been extracted
Expand All @@ -1259,19 +1247,15 @@ public static void main(String[] args) throws Exception {
"Ka 1.000 1.000 1.000\n" +
"Kd 1.000 1.000 1.000\n" +
"Ks 0.000 0.000 0.000\n" +
"d 1.0\n" +
"illum 2");
if (vmt.translucent == 1 || vmt.alphatest == 1) {
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".tga" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
} else {
materialFile.println(
"map_Ka " + "materials/" + vmt.basetexture + ".jpg" + "\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".jpg");
}
"map_Ka " + "materials/" + vmt.basetexture + ".tga\n" +
"map_Kd " + "materials/" + vmt.basetexture + ".tga");
if (vmt.bumpmap != null) { // If the material has a bump map associated with it
materialFile.println("map_bump " + "materials/" + vmt.bumpmap + ".jpg");
materialFile.println("map_bump " + "materials/" + vmt.bumpmap + ".tga");
}
if (vmt.translucent == 1 || vmt.alphatest == 1) { // If the material has transparency
materialFile.println(
// "map_d -imfchan m" + "materials/" + vmt.basetexture + ".tga\n" + // Many programs fail to import the alpha channel of the tga file and instead import the color channel
"illum 4");
}
materialFile.println();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static QC parseQC(String text) {

text = text.replaceAll("\\\\", "/"); // Replace backslashs with forwardslashs
text = text.replaceAll("//(.*)", ""); // Remove all commented lines
text = text.replaceAll("\\$staticprop", ""); // Remove all weirdos
text = text.replaceAll("\\$[^\" \\t]+\\n", ""); // Remove all keyless values
text = text.replaceAll("[^\"](\\$[^\" \\t]+)", "\"$1\""); // fix unquoted keys
text = text.replaceAll("(\".+\"[ \\t]+)([^\" \\t\\s].*)", "$1\"$2\""); // fix unquoted values
text = text.replaceAll("\\$", ""); // Remove all key prefixes
Expand Down
Loading

0 comments on commit 9fa5eb0

Please sign in to comment.