<commit_before>﻿using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
using System.Globalization;

namespace AsImpL
{
    /// <summary>
    /// Class for loading OBJ files into Unity scene at run-time and in editor mode.
    /// </summary>
    /// <remarks>
    /// Partially derived from "Runtime OBJ Loader"
    /// (http://forum.unity3d.com/threads/free-runtime-obj-loader.365884/)
    /// and from "runtime .OBJ file loader for Unity3D"
    /// (https://github.com/hammmm/unity-obj-loader) and 
    /// (https://github.com/cmdr2/unity-remote-obj-loader)
    /// 
    /// New features:
    /// <list type="bullet">
    /// <item><description>meshes with more than 65K vertices/indices are splitted and loaded</description></item>
    /// <item><description>groups are loaded into game (sub) objects</description></item>
    /// <item><description>extended material support</description></item>
    /// <item><description>computation of normal maps and tangents</description></item>
    /// <item><description>computation of albedo texture from diffuse and opacity textures</description></item>
    /// <item><description>progressive loading</description></item>
    /// <item><description>reusing data for multiple objects</description></item>
    /// <item><description>create a loader for each model for parallel loading</description></item>
    /// <item><description>support for asset import</description></item>
    /// </list>
    /// <seealso cref="DataSet"/>
    /// <seealso cref="MaterialData"/>
    /// <seealso cref="ObjectBuilder"/>
    /// </remarks>
    public class LoaderObj : Loader
    {
        private string mtlLib;
        private string loadedText;


        /// <summary>
        /// Parse dependencies of the given OBJ file.
        /// </summary>
        /// <param name="absolutePath">absolute file path</param>
        /// <returns>The list of dependencies (textures files, if any).</returns>
        public override string[] ParseTexturePaths(string absolutePath)
        {
            List<string> mtlTexPathList = new List<string>();
            string basePath = GetDirName(absolutePath);

            string mtlLibName = ParseMaterialLibName(absolutePath);

            if (!string.IsNullOrEmpty(mtlLibName))
            {
                //mtlDepPathList.Add(mtlLibName);
                string mtlPath = basePath + mtlLibName;
                string[] lines = File.ReadAllLines(mtlPath);
                List<MaterialData> mtlData = new List<MaterialData>();
                ParseMaterialData(lines, mtlData);
                foreach (MaterialData mtl in mtlData)
                {
                    if (!string.IsNullOrEmpty(mtl.diffuseTexPath))
                    {
                        mtlTexPathList.Add(mtl.diffuseTexPath);
                    }
                    if (!string.IsNullOrEmpty(mtl.specularTexPath))
                    {
                        mtlTexPathList.Add(mtl.specularTexPath);
                    }
                    if (!string.IsNullOrEmpty(mtl.bumpTexPath))
                    {
                        mtlTexPathList.Add(mtl.bumpTexPath);
                    }
                    if (!string.IsNullOrEmpty(mtl.opacityTexPath))
                    {
                        mtlTexPathList.Add(mtl.opacityTexPath);
                    }
                }
            }

            return mtlTexPathList.ToArray();
        }


        protected override IEnumerator LoadModelFile(string absolutePath)
        {
            string url = absolutePath.Contains("//") ? absolutePath : "file:///" + absolutePath;
            yield return LoadOrDownloadText(url);

            if (string.IsNullOrEmpty(loadedText))
            {
                Debug.LogError("Failed to load: empty path.");
                // remove this progress to let complete the total loading process
                totalProgress.singleProgress.Remove(objLoadingProgress);
                yield break;
            }
            //Debug.LogFormat("Parsing geometry data in {0}...", www.url);

            yield return ParseGeometryData(loadedText);
        }


        protected override IEnumerator LoadMaterialLibrary(string absolutePath)
        {
            string mtlPath;
            string basePath = GetDirName(absolutePath);
            if (absolutePath.Contains("//"))
            {
                int pos;
                // handle the special case of a PHP URL containing "...?...=model.obj"
                if (absolutePath.Contains("?"))
                {
                    // in this case try to get the library path reading until last "=".
                    pos = absolutePath.LastIndexOf('=');
                }
                else<commit_msg>fix(LoaderObj): wrong error message modified<commit_after>﻿using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
using System.Globalization;

namespace AsImpL
{
    /// <summary>
    /// Class for loading OBJ files into Unity scene at run-time and in editor mode.
    /// </summary>
    /// <remarks>
    /// Partially derived from "Runtime OBJ Loader"
    /// (http://forum.unity3d.com/threads/free-runtime-obj-loader.365884/)
    /// and from "runtime .OBJ file loader for Unity3D"
    /// (https://github.com/hammmm/unity-obj-loader) and 
    /// (https://github.com/cmdr2/unity-remote-obj-loader)
    /// 
    /// New features:
    /// <list type="bullet">
    /// <item><description>meshes with more than 65K vertices/indices are splitted and loaded</description></item>
    /// <item><description>groups are loaded into game (sub) objects</description></item>
    /// <item><description>extended material support</description></item>
    /// <item><description>computation of normal maps and tangents</description></item>
    /// <item><description>computation of albedo texture from diffuse and opacity textures</description></item>
    /// <item><description>progressive loading</description></item>
    /// <item><description>reusing data for multiple objects</description></item>
    /// <item><description>create a loader for each model for parallel loading</description></item>
    /// <item><description>support for asset import</description></item>
    /// </list>
    /// <seealso cref="DataSet"/>
    /// <seealso cref="MaterialData"/>
    /// <seealso cref="ObjectBuilder"/>
    /// </remarks>
    public class LoaderObj : Loader
    {
        private string mtlLib;
        private string loadedText;


        /// <summary>
        /// Parse dependencies of the given OBJ file.
        /// </summary>
        /// <param name="absolutePath">absolute file path</param>
        /// <returns>The list of dependencies (textures files, if any).</returns>
        public override string[] ParseTexturePaths(string absolutePath)
        {
            List<string> mtlTexPathList = new List<string>();
            string basePath = GetDirName(absolutePath);

            string mtlLibName = ParseMaterialLibName(absolutePath);

            if (!string.IsNullOrEmpty(mtlLibName))
            {
                //mtlDepPathList.Add(mtlLibName);
                string mtlPath = basePath + mtlLibName;
                string[] lines = File.ReadAllLines(mtlPath);
                List<MaterialData> mtlData = new List<MaterialData>();
                ParseMaterialData(lines, mtlData);
                foreach (MaterialData mtl in mtlData)
                {
                    if (!string.IsNullOrEmpty(mtl.diffuseTexPath))
                    {
                        mtlTexPathList.Add(mtl.diffuseTexPath);
                    }
                    if (!string.IsNullOrEmpty(mtl.specularTexPath))
                    {
                        mtlTexPathList.Add(mtl.specularTexPath);
                    }
                    if (!string.IsNullOrEmpty(mtl.bumpTexPath))
                    {
                        mtlTexPathList.Add(mtl.bumpTexPath);
                    }
                    if (!string.IsNullOrEmpty(mtl.opacityTexPath))
                    {
                        mtlTexPathList.Add(mtl.opacityTexPath);
                    }
                }
            }

            return mtlTexPathList.ToArray();
        }


        protected override IEnumerator LoadModelFile(string absolutePath)
        {
            string url = absolutePath.Contains("//") ? absolutePath : "file:///" + absolutePath;
            yield return LoadOrDownloadText(url);

            if (string.IsNullOrEmpty(loadedText))
            {
                // loading errors are already notified by LoadOrDownloadText()
                if (loadedText == "")
                {
                    Debug.LogError("Empty model file.");
                }
                // remove this progress to let complete the total loading process
                totalProgress.singleProgress.Remove(objLoadingProgress);
                yield break;
            }
            //Debug.LogFormat("Parsing geometry data in {0}...", www.url);

            yield return ParseGeometryData(loadedText);
        }


        protected override IEnumerator LoadMaterialLibrary(string absolutePath)
        {
            string mtlPath;
            string basePath = GetDirName(absolutePath);
            if (absolutePath.Contains("//"))
            {
                int pos;
                // handle the special case of a PHP URL containing "...?...=model.obj"
                if (absolutePath.Contains("?"))
                {
                    // in this case try to get the library path reading until last "=".
                    pos = absolutePath.LastIndexOf('=');
                }
                else
{'fraction_non_alphanumeric': 0.11447128069819362, 'fraction_numerical': 0.0024355591637913536, 'mean_word_length': 1.822973360068748, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 113, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5924957', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2429, 'n_words': 580}