Skip to content

Commit

Permalink
Fixed material names with ".001" ending being incorrectly renamed, ad…
Browse files Browse the repository at this point in the history
…ded more crash paths
  • Loading branch information
Kinerius committed Mar 21, 2023
1 parent 6e29853 commit 50b5375
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH

# Change this value ONLY if we have done breaking changes for every material, doing so is VERY costly
ENV AB_VERSION v2
ENV AB_VERSION v3

# NODE_ENV is used to configure some runtime options, like JSON logger
ENV NODE_ENV production
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ namespace DCL.ABConverter
{
public class AssetBundleConverter
{
public struct GltfImportSettings
private struct GltfImportSettings
{
public string url;
public GltfImport import;
public AssetPath AssetPath;
}

// For consistency, never remove any of these enum values as it will break old error codes
public enum ErrorCodes
{
SUCCESS,
Expand All @@ -37,6 +38,9 @@ public enum ErrorCodes
VISUAL_TEST_FAILED,
UNEXPECTED_ERROR,
GLTFAST_CRITICAL_ERROR,
GLTF_IMPORTER_NOT_FOUND,
EMBED_MATERIAL_FAILURE,
DOWNLOAD_FAILED
}

public class State
Expand Down Expand Up @@ -254,12 +258,11 @@ private async Task<bool> ImportAllGltf()

if (textures.Count > 0)
{
var directory = Path.GetDirectoryName(relativePath);
textures = CreateTextureAssets(textures, directory);
log.Verbose($"gltf creating dummy images completed: {gltfUrl}");
string directory = Path.GetDirectoryName(relativePath);
textures = ExtractEmbedTexturesFromGltf(textures, directory);
}

CreateMaterialsForThisGltf(textures, gltf, gltfImport, gltfUrl);
ExtractEmbedMaterialsFromGltf(textures, gltf, gltfImport, gltfUrl);

log.Verbose($"gltf load success {gltfUrl}");

Expand Down Expand Up @@ -297,7 +300,7 @@ private async Task<bool> ImportAllGltf()
var message = $"Failed to get the gltf importer for {gltfUrl} \nPath: {relativePath}";
log.Error(message);
errorReporter.ReportError(message, settings);
EditorUtility.ClearProgressBar();
Utils.Exit((int)ErrorCodes.GLTF_IMPORTER_NOT_FOUND);
continue;
}

Expand All @@ -311,13 +314,15 @@ private async Task<bool> ImportAllGltf()
var renderers = clone.GetComponentsInChildren<Renderer>(true);

foreach (Renderer renderer in renderers)
if (renderer.name.ToLower().Contains("_collider")) { renderer.enabled = false; }
if (renderer.name.ToLower().Contains("_collider"))
renderer.enabled = false;
}
catch (Exception e)
{
log.Exception(e.ToString());
errorReporter.ReportException(new ConversionException(ConversionStep.Import, settings, e));
continue;

// we dont crash here since we dont do this on batch mode
}
}

Expand All @@ -340,7 +345,7 @@ private async Task<bool> ImportAllGltf()
return false;
}

private void CreateMaterialsForThisGltf(List<Texture2D> textures, GltfImportSettings gltf, GltfImport gltfImport, string gltfUrl)
private void ExtractEmbedMaterialsFromGltf(List<Texture2D> textures, GltfImportSettings gltf, GltfImport gltfImport, string gltfUrl)
{
Dictionary<string, Texture2D> texNameMap = new Dictionary<string, Texture2D>();

Expand Down Expand Up @@ -389,17 +394,20 @@ private void CreateMaterialsForThisGltf(List<Texture2D> textures, GltfImportSett

// we reassign the texture reference
string texName = Utils.NicifyName(tex.name);
texName = Path.GetFileNameWithoutExtension(texName);

var prevTexture = newMaterial.GetTexture(propertyName);

if (texNameMap.ContainsKey(texName)) { newMaterial.SetTexture(propertyName, texNameMap[texName]); }
if (texNameMap.ContainsKey(texName))
newMaterial.SetTexture(propertyName, texNameMap[texName]);
else
{
if (prevTexture == null)
{
var message = $"Failed to set texture \"{texName}\" to material \"{matName}\". This will cause white materials";
log.Error(message);
errorReporter.ReportError(message, settings);
Utils.Exit((int)ErrorCodes.GLTFAST_CRITICAL_ERROR);
Utils.Exit((int)ErrorCodes.EMBED_MATERIAL_FAILURE);
return;
}
}
Expand All @@ -422,7 +430,7 @@ private static void RefreshAssetsWithNoLogs()
Debug.unityLogger.logEnabled = true;
}

private List<Texture2D> CreateTextureAssets(List<Texture2D> textures, string folderName)
private List<Texture2D> ExtractEmbedTexturesFromGltf(List<Texture2D> textures, string folderName)
{
var newTextures = new List<Texture2D>();

Expand All @@ -435,6 +443,7 @@ private List<Texture2D> CreateTextureAssets(List<Texture2D> textures, string fol
var tex = textures[i];
string texName = tex.name;
texName = Utils.NicifyName(texName);
texName = Path.GetFileNameWithoutExtension(texName);

var texPath = string.Concat(texturesRoot, texName);

Expand Down Expand Up @@ -608,8 +617,10 @@ public virtual bool BuildAssetBundles(out AssetBundleManifest manifest)

if (manifest == null)
{
log.Error("Error generating asset bundle!");

var message = "Error generating asset bundle!";
log.Error(message);
errorReporter.ReportError(message, settings);
Utils.Exit((int)ErrorCodes.ASSET_BUNDLE_BUILD_FAIL);
return false;
}

Expand Down Expand Up @@ -939,6 +950,8 @@ void FinallyImportAsset()
{
var message = $"Download Failed {finalUrl} -- {e.Message}";
log.Error(message);
errorReporter.ReportError(message, settings);
Utils.Exit((int)ErrorCodes.DOWNLOAD_FAILED);
return null;
}

Expand All @@ -963,45 +976,44 @@ void FinallyImportAsset()
return outputPath;
}

private GltfImport CreateGltfImport(AssetPath filePath)
{
return new GltfImport(
private GltfImport CreateGltfImport(AssetPath filePath) =>
new (
new GltFastFileProvider(filePath.fileRootPath, filePath.hash, contentTable),
new UninterruptedDeferAgent(),
GetNewMaterialGenerator(),
new ConsoleLogger());
}

private bool ReduceTextureSizeIfNeeded(string texturePath, float maxSize)
private void ReduceTextureSizeIfNeeded(string texturePath, float maxSize)
{
byte[] image = File.ReadAllBytes(texturePath);

var tmpTex = new Texture2D(1, 1);

if (!tmpTex.LoadImage(image))
return false;
return;

float factor = 1.0f;
float factor;
int width = tmpTex.width;
int height = tmpTex.height;

float maxTextureSize = maxSize;

if (width < maxTextureSize && height < maxTextureSize)
return false;
return;

if (width >= height) { factor = (float)maxTextureSize / width; }
else { factor = (float)maxTextureSize / height; }
if (width >= height)
factor = maxTextureSize / width;
else
factor = maxTextureSize / height;

Texture2D dstTex = Utils.ResizeTexture(tmpTex, (int)(width * factor), (int)(height * factor));
byte[] endTex = dstTex.EncodeToPNG();
UnityEngine.Object.DestroyImmediate(tmpTex);
Object.DestroyImmediate(tmpTex);

File.WriteAllBytes(texturePath, endTex);

AssetDatabase.ImportAsset(PathUtils.GetRelativePathTo(Application.dataPath, texturePath), ImportAssetOptions.ForceUpdate);
AssetDatabase.SaveAssets();
return true;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ private void SetupSettings()
shaderType = shader,
stripShaders = stripShaders,
importGltf = importGltf,
placeOnScene = placeOnScene
placeOnScene = placeOnScene,
verbose = verbose
};

Debug.ClearDeveloperConsole();
Expand Down
5 changes: 3 additions & 2 deletions asset-bundle-converter/Assets/AssetBundleConverter/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,11 @@ public static Texture2D ResizeTexture(Texture2D source, int newWidth, int newHei

public static string NicifyName(string name)
{
name = Path.GetFileNameWithoutExtension(name);
name = name.Replace(":", ".");
foreach (char c in Path.GetInvalidFileNameChars()) { name = name.Replace(c, '_'); }
name = name.Replace(":", "_");
name = name.Replace(" ", "_");
name = name.Replace("*", "_");
name = name.Replace("|", "_");

if (string.IsNullOrEmpty(name))
name = "unnamed";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public static async Task TestConvertedAssetsAsync(Environment env, ClientSetting
skippedAssets++;

string message = "Visual test failed on " + go.name + $" with {result}% affinity";
Debug.Log(message);
errorReporter.ReportError(message, clientSettings);
Debug.LogError(message, go);
//errorReporter.ReportError(message, clientSettings);
}

go.SetActive(false);
Expand Down

0 comments on commit 50b5375

Please sign in to comment.