-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from teogor/feature/add-build-fingerprint-reader
Add BuildFingerprint Data Class and File Reader for Unity Build Information
- Loading branch information
Showing
7 changed files
with
305 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
gradle-plugin/src/main/kotlin/dev/teogor/drifter/plugin/unity/BuildFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2024 teogor (Teodor Grigor) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.teogor.drifter.plugin.unity | ||
|
||
import java.io.File | ||
import java.io.IOException | ||
|
||
/** | ||
* Data class to represent the build fingerprint information. | ||
* | ||
* @param unityVersion The Unity version used. | ||
* @param scriptingBackend The scripting backend used (e.g., IL2CPP). | ||
* @param buildType The build configuration (e.g., Release). | ||
* @param stripEngineCode Indicates if engine code is stripped. | ||
* @param optimizedFramePacing Indicates if frame pacing is optimized. | ||
*/ | ||
data class BuildFingerprint( | ||
val unityVersion: UnityVersion, | ||
val scriptingBackend: String, | ||
val buildType: String, | ||
val stripEngineCode: Int, | ||
val optimizedFramePacing: Int, | ||
) | ||
|
||
/** | ||
* Reads and parses the build fingerprint information from a file. | ||
* | ||
* @param dirPath The directory path where the file is located. | ||
* @param filePath The path within the directory where the file is located. | ||
* @param fileName The name of the file to read. | ||
* @return A [BuildFingerprint] object containing the parsed data, or null if an error occurs. | ||
*/ | ||
fun readBuildFingerprint( | ||
dirPath: String, | ||
filePath: String = "src/main/resources/META-INF", | ||
fileName: String = "com.android.games.engine.build_fingerprint", | ||
): BuildFingerprint? { | ||
// Construct the full file path | ||
val fullFilePath = "$dirPath/$filePath/$fileName" | ||
|
||
// Attempt to access the file | ||
val file = runCatching { File(fullFilePath) } | ||
.getOrElse { | ||
return null | ||
} | ||
|
||
return try { | ||
// Read the file content | ||
val content = file.readText().trim() | ||
|
||
// Split the content by semicolons | ||
val parts = content.split(';') | ||
.dropLastWhile { it.isEmpty() } | ||
|
||
// Ensure we have the expected number of parts | ||
if (parts.size != 5) { | ||
println("Unexpected file format. Expected 5 parts but found ${parts.size}.") | ||
println(parts) | ||
return null | ||
} | ||
|
||
// Extract and validate each part | ||
val unityVersion = parts[0].takeIf { it.isNotBlank() }?.toUnityVersion() ?: run { | ||
println("Invalid Unity Version.") | ||
return null | ||
} | ||
|
||
val scriptingBackend = parts[1].takeIf { it.isNotBlank() } ?: run { | ||
println("Invalid Scripting Backend.") | ||
return null | ||
} | ||
|
||
val buildType = parts[2].takeIf { it.isNotBlank() } ?: run { | ||
println("Invalid Build Type.") | ||
return null | ||
} | ||
|
||
val stripEngineCode = parts[3].removePrefix("StripEngineCode:").toIntOrNull() ?: run { | ||
println("Invalid Strip Engine Code.") | ||
return null | ||
} | ||
|
||
val optimizedFramePacing = parts[4].removePrefix("OptimizedFramePacing:").toIntOrNull() ?: run { | ||
println("Invalid Optimized Frame Pacing.") | ||
return null | ||
} | ||
|
||
// Create and return the BuildFingerprint object | ||
BuildFingerprint( | ||
unityVersion = unityVersion, | ||
scriptingBackend = scriptingBackend, | ||
buildType = buildType, | ||
stripEngineCode = stripEngineCode, | ||
optimizedFramePacing = optimizedFramePacing, | ||
) | ||
} catch (e: IOException) { | ||
println("Error reading the build fingerprint file: ${e.message}") | ||
null | ||
} catch (e: NumberFormatException) { | ||
println("Error parsing the build fingerprint values: ${e.message}") | ||
null | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
gradle-plugin/src/main/kotlin/dev/teogor/drifter/plugin/unity/UnityVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2024 teogor (Teodor Grigor) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.teogor.drifter.plugin.unity | ||
|
||
/** | ||
* Data class to represent the Unity version. | ||
* | ||
* This class encapsulates the components of a Unity version, which typically includes | ||
* the year, major version, minor version, and an optional patch version suffix. | ||
* | ||
* @param year The year of the Unity version, represented as a 4-digit integer. | ||
* @param major The major version number, represented as an integer. | ||
* @param minor The minor version number, represented as an integer. | ||
* @param patch The patch version with optional suffix, represented as a string. | ||
*/ | ||
data class UnityVersion( | ||
val year: Int, | ||
val major: Int, | ||
val minor: Int, | ||
val patch: String, | ||
) { | ||
|
||
/** | ||
* Converts the [UnityVersion] object to a string in the format "YYYY.M.mfX". | ||
* | ||
* @return The Unity version string representation. | ||
*/ | ||
override fun toString(): String { | ||
return "v$year.$major.$minor$patch" | ||
} | ||
|
||
companion object { | ||
/** | ||
* Parses a Unity version string into a [UnityVersion] object. | ||
* | ||
* This method takes a Unity version string in the format "YYYY.M.mX", where: | ||
* - `YYYY` is the 4-digit year, | ||
* - `M` is the major version number, | ||
* - `m` is the minor version number, | ||
* - `X` is an optional patch suffix (can be letters and/or numbers). | ||
* | ||
* @param versionString The Unity version string to be parsed, e.g., "2022.3.7f1". | ||
* @return A [UnityVersion] object if the format is valid, or null if parsing fails. | ||
*/ | ||
fun parseString(versionString: String): UnityVersion? { | ||
// Define a regular expression to match the Unity version format | ||
val regex = """(\d{4})\.(\d+)\.(\d+)([a-zA-Z0-9]*)""".toRegex() | ||
|
||
return regex.matchEntire(versionString)?.let { matchResult -> | ||
// Extract components from the match | ||
val (yearStr, majorStr, minorStr, patchStr) = matchResult.destructured | ||
|
||
// Parse components into integers and strings | ||
val year = yearStr.toIntOrNull() ?: return null | ||
val major = majorStr.toIntOrNull() ?: return null | ||
val minor = minorStr.toIntOrNull() ?: return null | ||
|
||
// Return the UnityVersion object | ||
UnityVersion( | ||
year = year, | ||
major = major, | ||
minor = minor, | ||
patch = patchStr, | ||
) | ||
} ?: run { | ||
// Return null if the version string does not match the expected format | ||
null | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Extension function to convert a string into a [UnityVersion] object. | ||
* | ||
* This function is a convenient wrapper around the [UnityVersion.parseString] method | ||
* and allows for a more fluent API when converting strings to UnityVersion instances. | ||
* | ||
* @return A [UnityVersion] object if the string is a valid Unity version format, or null if parsing fails. | ||
*/ | ||
fun String.toUnityVersion(): UnityVersion? { | ||
return UnityVersion.parseString(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters