Skip to content

Commit

Permalink
Merge pull request #2 from Lime-blur/1.1
Browse files Browse the repository at this point in the history
Release 1.1
  • Loading branch information
Lime-blur authored Nov 24, 2023
2 parents a8aabfb + b66bd6f commit 551d84a
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "rwparser/src/main/cpp/dff_converter"]
path = rwparser/src/main/cpp/dff_converter
url = https://github.com/SimoSbara/dff_converter.git
branch = cross-library
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}

ext {
lifecycle_runtime_ktx_version = "2.6.2"
}
8 changes: 6 additions & 2 deletions rwparser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.github.Lime-blur'
version = '1.0.5'
version = '1.1'

android {
namespace 'ru.limedev.rwparser'
Expand Down Expand Up @@ -43,14 +43,18 @@ android {
}
}

dependencies {
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_runtime_ktx_version"
}

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = 'com.github.Lime-blur'
artifactId = 'rwparser'
version = '1.0.5'
version = '1.1'
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion rwparser/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ cmake_minimum_required(VERSION 3.22.1)

project("rwparser")

add_subdirectory(dff_converter)

set(DFF_CONVERTER_HEADERS_DIR
dff_converter/src/converter
dff_converter/src/rwtools
dff_converter/externals/tinygltf)

include_directories(${DFF_CONVERTER_HEADERS_DIR})

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
Expand Down Expand Up @@ -56,4 +65,6 @@ target_link_libraries( # Specifies the target library.

# Links the target library to the log library
# included in the NDK.
${log-lib})
${log-lib})

target_link_libraries(rwparser dff_converter)
1 change: 1 addition & 0 deletions rwparser/src/main/cpp/dff_converter
Submodule dff_converter added at 092bea
44 changes: 44 additions & 0 deletions rwparser/src/main/cpp/modelparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "renderware/renderware.h"
#include "utils/utils.cpp"
#include "utils/jlogs.h"
#include <ConverterGLTF.h>

using namespace std;
using namespace rw;
Expand Down Expand Up @@ -67,3 +68,46 @@ extern "C" jint Java_ru_limedev_rwparser_ModelParser_putTxdDumpIntoFileNative(
out.close();
return 0;
}

extern "C" jint Java_ru_limedev_rwparser_ModelParser_convertDffWithTxdToGltfNative(
JNIEnv* env,
jobject,
jstring jInDffFilePath,
jstring jOutFilePath,
jstring jInTxdFilePath
) {
ConverterGLTF converter;
char *inDffFile = jniutils::to_char_ptr(env, jInDffFilePath);
char *inTxdFile = jniutils::to_char_ptr(env, jInTxdFilePath);
char *outFile = jniutils::to_char_ptr(env, jOutFilePath);
ifstream inDff(inDffFile, ios::binary);
if (!utils::isStreamFailed(env, inDff, jInDffFilePath)) return -1;
ifstream inTxd(inTxdFile, ios::binary);
if (!utils::isStreamFailed(env, inTxd, jInTxdFilePath)) return -1;
rw::Clump clump;
rw::TextureDictionary textureDictionary;
clump.read(inDff);
textureDictionary.read(inTxd);
converter.convert(outFile, clump, textureDictionary);
inDff.close();
inTxd.close();
return 0;
}

extern "C" jint Java_ru_limedev_rwparser_ModelParser_convertDffToGltfNative(
JNIEnv* env,
jobject,
jstring jInFilePath,
jstring jOutFilePath
) {
ConverterGLTF converter;
char *inFile = jniutils::to_char_ptr(env, jInFilePath);
char *outFile = jniutils::to_char_ptr(env, jOutFilePath);
ifstream in(inFile, ios::binary);
if (!utils::isStreamFailed(env, in, jInFilePath)) return -1;
rw::Clump clump;
clump.read(in);
converter.convert(outFile, clump);
in.close();
return 0;
}
1 change: 1 addition & 0 deletions rwparser/src/main/cpp/renderware/renderware.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdlib>

#include "renderware.h"

using namespace std;

namespace rw {
Expand Down
3 changes: 2 additions & 1 deletion rwparser/src/main/cpp/renderware/txdread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstdlib>
#include <fstream>
#include "renderware.h"
#include "../utils/jlogs.h"

using namespace std;

Expand Down Expand Up @@ -286,7 +287,7 @@ namespace rw {
}
paletteSize = (rasterFormat & RASTER_PAL8) ? 0x100 : 0x10;
palette = new uint8[paletteSize * 4];
rw.read(reinterpret_cast <char *> (palette),paletteSize * 4 * sizeof(uint8));
rw.read(reinterpret_cast <char *> (palette), paletteSize * 4 * sizeof(uint8));
if (unkh2 == 8 && unkh3 == 3 && unkh4 == 6) rw.seekg(0x20, ios::cur);
}
rasterFormat &= 0xff00;
Expand Down
2 changes: 1 addition & 1 deletion rwparser/src/main/cpp/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class utils {

public: static bool isStreamFailed(JNIEnv *env, std::ios &ios, jstring path) {
if (ios.fail()) {
jexception::throwIOException(env, jniutils::to_string(env, path));
jexception::throwIOException(env, "Failed load: " + jniutils::to_string(env, path));
return false;
}
return true;
Expand Down
164 changes: 153 additions & 11 deletions rwparser/src/main/java/ru/limedev/rwparser/ModelParser.kt
Original file line number Diff line number Diff line change
@@ -1,44 +1,174 @@
package ru.limedev.rwparser

import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ModelParser {

private var parseResourcesJob = CoroutineScope(Job() + Dispatchers.IO)

/**
* Asynchronously parses the dff file and places the dump in the specified file.
* @param inFilePath path to dff file
* @param outFilePath path to the file to place dff's dump
* @param isDetailedDump true - for a detailed dump, false - for a non-detailed dump. Default is false
* @param callback is triggered after parsing with the corresponding [ParseResult].
*/
fun putDffDumpIntoFileAsync(
inFilePath: String,
outFilePath: String,
isDetailedDump: Boolean = false,
callback: (ParseResult) -> Unit
) {
parseResourcesJob.launch {
try {
val parseResult = putDffDumpIntoFileNative(inFilePath, outFilePath, isDetailedDump)
withContext(Dispatchers.Main) {
callback.invoke(if (parseResult == 0) ParseResult.SUCCESS else ParseResult.ERROR)
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Log.e(LOCAL_LOG_TAG, e.toString())
callback.invoke(ParseResult.ERROR)
}
}
}
}

/**
* Asynchronously parses the txd file and places the dump in the specified file.
* @param inFilePath path to txd file
* @param outFilePath path to the file to place txd's dump
* @param callback is triggered after parsing with the corresponding [ParseResult].
*/
fun putTxdDumpIntoFileAsync(
inFilePath: String,
outFilePath: String,
callback: (ParseResult) -> Unit
) {
parseResourcesJob.launch {
try {
val parseResult = putTxdDumpIntoFileNative(inFilePath, outFilePath)
withContext(Dispatchers.Main) {
callback.invoke(if (parseResult == 0) ParseResult.SUCCESS else ParseResult.ERROR)
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Log.e(LOCAL_LOG_TAG, e.toString())
callback.invoke(ParseResult.ERROR)
}
}
}
}

/**
* Asynchronously converts the dff file to gltf file.
* @param inDffFilePath path to dff file
* @param outFilePath path to output gltf file
* @param inTxdFilePath path to txd file (optional)
* @param callback is triggered after converting with the corresponding [ParseResult].
*/
fun convertDffToGltfAsync(
inDffFilePath: String,
outFilePath: String,
inTxdFilePath: String? = null,
callback: (ParseResult) -> Unit
) {
parseResourcesJob.launch {
try {
val parseResult = if (inTxdFilePath != null) {
convertDffWithTxdToGltfNative(inDffFilePath, outFilePath, inTxdFilePath)
} else {
convertDffToGltfNative(inDffFilePath, outFilePath)
}
withContext(Dispatchers.Main) {
callback.invoke(if (parseResult == 0) ParseResult.SUCCESS else ParseResult.ERROR)
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Log.e(LOCAL_LOG_TAG, e.toString())
callback.invoke(ParseResult.ERROR)
}
}
}
}

/**
* Parses the dff file and places the dump in the specified file.
* @param jInFilePath dff file
* @param jOutFilePath the file to place dff's dump
* @param jIsDetailedDump true - for a detailed dump, false - for a non-detailed dump. Default is false
* @param inFilePath path to dff file
* @param outFilePath path to the file to place dff's dump
* @param isDetailedDump true - for a detailed dump, false - for a non-detailed dump. Default is false
* @return [ParseResult.SUCCESS] - if the operation was successful,
* [ParseResult.ERROR] - if a failure occurred.
*/
fun putDffDumpIntoFile(
jInFilePath: String,
jOutFilePath: String,
jIsDetailedDump: Boolean = false
inFilePath: String,
outFilePath: String,
isDetailedDump: Boolean = false
): ParseResult {
return try {
val parseResult = putDffDumpIntoFileNative(jInFilePath, jOutFilePath, jIsDetailedDump)
val parseResult = putDffDumpIntoFileNative(inFilePath, outFilePath, isDetailedDump)
if (parseResult == 0) ParseResult.SUCCESS else ParseResult.ERROR
} catch (e: Exception) {
Log.e(LOCAL_LOG_TAG, e.toString())
ParseResult.ERROR
}
}

/**
* Parses the txd file and places the dump in the specified file.
* @param jInFilePath txd file
* @param jOutFilePath the file to place txd's dump
* @param inFilePath path to txd file
* @param outFilePath path to the file to place txd's dump
* @return [ParseResult.SUCCESS] - if the operation was successful,
* [ParseResult.ERROR] - if a failure occurred.
*/
fun putTxdDumpIntoFile(jInFilePath: String, jOutFilePath: String): ParseResult {
fun putTxdDumpIntoFile(inFilePath: String, outFilePath: String): ParseResult {
return try {
val parseResult = putTxdDumpIntoFileNative(jInFilePath, jOutFilePath)
val parseResult = putTxdDumpIntoFileNative(inFilePath, outFilePath)
if (parseResult == 0) ParseResult.SUCCESS else ParseResult.ERROR
} catch (e: Exception) {
Log.e(LOCAL_LOG_TAG, e.toString())
ParseResult.ERROR
}
}

/**
* Converts the dff file to gltf file.
* @param inDffFilePath path to dff file
* @param outFilePath path to output gltf file
* @param inTxdFilePath path to txd file (optional)
* @return [ParseResult.SUCCESS] - if the operation was successful,
* [ParseResult.ERROR] - if a failure occurred.
*/
fun convertDffToGltf(
inDffFilePath: String,
outFilePath: String,
inTxdFilePath: String? = null
): ParseResult {
return try {
val parseResult = if (inTxdFilePath != null) {
convertDffWithTxdToGltfNative(inDffFilePath, outFilePath, inTxdFilePath)
} else {
convertDffToGltfNative(inDffFilePath, outFilePath)
}
if (parseResult == 0) ParseResult.SUCCESS else ParseResult.ERROR
} catch (e: Exception) {
Log.e(LOCAL_LOG_TAG, e.toString())
ParseResult.ERROR
}
}

/**
* Cancels all asynchronous operations. Tie this method to the life cycle of the component in
* which parsing / conversion operations occur.
*/
fun destroy() { parseResourcesJob.cancel() }

private external fun putDffDumpIntoFileNative(
jInFilePath: String,
jOutFilePath: String,
Expand All @@ -47,7 +177,19 @@ class ModelParser {

private external fun putTxdDumpIntoFileNative(jInFilePath: String, jOutFilePath: String): Int

private external fun convertDffWithTxdToGltfNative(
jInFilePath: String,
jOutFilePath: String,
jInTxdFilePath: String
): Int

private external fun convertDffToGltfNative(
jInFilePath: String,
jOutFilePath: String
): Int

companion object {
init { System.loadLibrary("rwparser") }
private const val LOCAL_LOG_TAG = "ModelParser"
}
}
7 changes: 7 additions & 0 deletions sample/src/main/java/ru/limedev/sample/FileConstants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.limedev.sample

const val RESULT_FILE_NAME = "result"
const val DFF_EXTENSION = ".dff"
const val TXD_EXTENSION = ".txd"
const val DUMP_EXTENSION = ".dump"
const val GLTF_EXTENSION = ".gltf"
Loading

0 comments on commit 551d84a

Please sign in to comment.