// EdenAutoMorpher_ProfileSaver_Script, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.profile.ProfileUtils_VertexUtil
using System;
using System.IO;
using System.Text;
using UnityEngine;

public class ProfileUtils_VertexUtil
{
    public BaseKey3[] LoadTable(string path, out int version, out int countInFile)
    {
        version = 0;
        countInFile = 0;
        if (string.IsNullOrEmpty(path))
        {
            Debug.LogError("[ProfileUtil_VertexUtil] LoadTable - path is null or empty");
            return null;
        }
        path = Path.Combine(Application.dataPath, path);
        if (!File.Exists(path))
        {
            Debug.LogError("[ProfileUtil_VertexUtil] LoadTable - file not found");
            return null;
        }
        string text;
        try
        {
            text = File.ReadAllText(path, Encoding.UTF8);
        }
        catch (Exception ex)
        {
            Debug.LogError("[ProfileUtil_VertexUtil] LoadTable - read failed: " + ex.Message);
            return null;
        }
        int num = 0;
        string baseMagic = new ProfileUtils().GetBaseMagic();
        if (text.Length < 4 || text.Substring(0, 4) != baseMagic)
        {
            Debug.LogError("[ProfileUtil_VertexUtil] LoadTable - MAGIC mismatch");
            return null;
        }
        num += 4;
        version = this.ReadHexIntSafe(text, ref num);
        countInFile = this.ReadHexIntSafe(text, ref num);
        if (countInFile <= 0)
        {
            Debug.LogError("[ProfileUtil_VertexUtil] LoadTable - invalid count");
            return null;
        }
        BaseKey3[] array = new BaseKey3[countInFile];
        for (int i = 0; i < countInFile; i++)
        {
            array[i] = this.ReadHexKey3Safe(text, ref num);
        }
        return array;
    }

    private int ReadHexIntSafe(string s, ref int p)
    {
        if (p + 8 > s.Length)
        {
            Debug.LogError("[ProfileUtil_VertexUtil] ReadHexIntSafe - out of range");
            return 0;
        }
        string value = s.Substring(p, 8);
        p += 8;
        return (int)Convert.ToUInt32(value, 16);
    }

    private BaseKey3 ReadHexKey3Safe(string s, ref int p)
    {
        int x = this.ReadHexIntSafe(s, ref p);
        uint y = (uint)this.ReadHexIntSafe(s, ref p);
        uint z = (uint)this.ReadHexIntSafe(s, ref p);
        return new BaseKey3((uint)x, y, z);
    }
}
