Beispiel #1
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static XBufferAsset          LoadBytecodeAsset(string fileName)
    {
        XBufferAsset bufferAsset = null;

#if UNITY_ANDROID && !UNITY_EDITOR
        string            name = fileName.ToLower();
        XSheet.XAssetInfo info = XSheet.Instance.Find(name);
        if (info != null && info.locationType == XSheet.XLocationType.Resource)
        {
            if (asset != null)
            {
                Debug.LogError(fileName);
                byte[] bytes = GetBytecodeByFileName(fileName);
                if (bytes != null)
                {
                    bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                    bufferAsset.init(bytes);
                }
            }
            else
            {
                bufferAsset = XRes.Load <XBufferAsset>(fileName);
            }
        }
        else
        {
            bufferAsset = XRes.Load <XBufferAsset>(fileName);
        }
#else
        bufferAsset = XRes.Load <XBufferAsset>(fileName);
#endif

        return(bufferAsset);
    }
Beispiel #2
0
    /// <summary>
    /// Loads the script.
    /// </summary>
    /// <returns>The script.</returns>
    /// <param name="fn">Fn.</param>
    public static byte[]    LoadScript(string fn, ref string str)
    {
        fn = fn.Replace(".", "/");

#if UNITY_STANDALONE || UNITY_EDITOR
        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();

        // find config source code
        string filePath = System.IO.Path.Combine(Application.dataPath, string.Format("Config/{0}.lua", fn));
        if (!System.IO.File.Exists(filePath))
        {
            // find logic script
            filePath = System.IO.Path.Combine(Application.dataPath, string.Format("Code/Script/{0}.lua", fn));
            if (!System.IO.File.Exists(filePath))
            {
                string bytefn = fn.Replace("/", "@").ToLower();
                // find lua byte code
                filePath = System.IO.Path.Combine(Application.dataPath,
                                                  string.Format("Bytecode/{0}/{1}.bytes", platformPath, bytefn));
            }
        }

#if !RELEASE
        GLog.Log(string.Format("{0}", filePath));
#endif

        if (System.IO.File.Exists(filePath))
        {
            System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
            asset.init((int)fs.Length);
            fs.Read(asset.bytes, 0, (int)fs.Length);
            fs.Close();

            return(asset.bytes);
        }
        else
        {
            Debug.LogError("can't find file : " + filePath);
        }
#else
        string bytefn = fn.Replace("/", "@").ToLower();
        string hashfn = XUtility.Md5Sum(string.Format("{0}/{1}",
                                                      platformPath, bytefn));

        XBufferAsset asset = XBytecodeFilePicker.LoadBytecodeAsset(string.Format("bytecode/{0}", hashfn));
        if (asset != null)
        {
            return(asset.bytes);
        }
        else
        {
            Debug.LogError("Can't load bytecode " + platformPath + " " + bytefn);
        }
#endif
        return(null);
    }
Beispiel #3
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="info"></param>
    /// <param name="type"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public IEnumerator DoLoadAssetAsync(XSheet.XAssetInfo info, System.Type type, System.Action <Object> callback)
    {
        XAssetBundleInfo bundleInfo = GetAssetBundleInfo(info.bundleName);

        if (!bundleInfo.isDone)
        {
            yield return(XCoroutine.Run(LoadAssetBundleAsync(bundleInfo)));
        }

        if (bundleInfo.isDone)
        {
            bundleInfo.IncRef(Time.time);

            AssetBundleRequest request = null;
            Object             obj     = null;
            if (type == typeof(XBufferAsset))
            {
                request = bundleInfo.bundle.LoadAssetAsync <TextAsset>(System.IO.Path.Combine(assetBasePath, info.fullName));
                yield return(request);

                XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                asset.init((TextAsset)request.asset);
                obj = asset;
            }
            else
            {
                request = bundleInfo.bundle.LoadAssetAsync(System.IO.Path.Combine(assetBasePath, info.fullName), type);
                yield return(request);

                obj = request.asset;
            }

            bundleInfo.DecRef();

            callback(obj);
        }
    }
Beispiel #4
0
    public static Object                Load(string path, System.Type type)
    {
        if (string.IsNullOrEmpty(path))
        {
            GLog.LogError("[XRes.Load] sent empty/null path!");
            return(null);
        }

        string name = path.ToLower();

        Object asset = Find(name);

        if (!asset)
        {
            XSheet.XAssetInfo info = XSheet.Instance.Find(name);
            if (info != null)
            {
                switch (info.locationType)
                {
                case XSheet.XLocationType.Resource:
                    if (type == typeof(XBufferAsset))
                    {
                        XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                        if (bufferAsset)
                        {
                            bufferAsset.init(
                                Resources.Load <TextAsset>(name)
                                );
                            asset = bufferAsset;
                        }
                    }
                    else
                    {
                        asset = Resources.Load(name, type);
                    }
                    break;

                case XSheet.XLocationType.Bundle:
                    GLog.LogError("[XRes.Load] can't load bundle in sync load {0}", name);
                    break;

                case XSheet.XLocationType.Data:
                    string filePath = System.IO.Path.Combine(Application.persistentDataPath, info.fullName);
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
                        if (fs.CanRead)
                        {
                            XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                            if (bufferAsset)
                            {
                                bufferAsset.init((int)fs.Length);
                                fs.Read(
                                    bufferAsset.bytes, 0, (int)fs.Length
                                    );
                                fs.Close();

                                if (type == typeof(Texture2D))
                                {
                                    Texture2D buffTexture = new Texture2D(1, 1);
                                    buffTexture.LoadImage(bufferAsset.bytes);

                                    asset = buffTexture;
                                }
                                else
                                {
                                    asset = bufferAsset;
                                }
                            }
                        }
                    }
                    break;

                default:
                    GLog.LogError("[XRes] no imp resource type {0}", info.locationType.ToString());
                    break;
                }
            }
        }
        else
        {
            if (type == typeof(XBufferAsset))
            {
                XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                bufferAsset.init(
                    Resources.Load <TextAsset>(name)
                    );

                asset = bufferAsset;
            }
            else
            {
                asset = Resources.Load(name, type);
            }
        }


        return(asset);
    }
Beispiel #5
0
    /// <summary>
    /// Dos the load async.
    /// </summary>
    /// <returns>The load async.</returns>
    /// <param name="path">Path.</param>
    /// <param name="type">Type.</param>
    /// <param name="callback">Callback.</param>
    private static IEnumerator  DoLoadAsync(string path, System.Type type, System.Action <Object> callback)
    {
        if (string.IsNullOrEmpty(path))
        {
            GLog.LogError("[XRes::Load] sent empty/null path!");
            yield break;
        }

        string name = path.ToLower();

        UnityEngine.Object obj = Find(name);
        if (!obj)
        {
            XSheet.XAssetInfo info = XSheet.Instance.Find(name);
            if (info != null)
            {
                switch (info.locationType)
                {
                case XSheet.XLocationType.Resource:
                    ResourceRequest request = null;
                    if (type == typeof(XBufferAsset))
                    {
                        request = Resources.LoadAsync <TextAsset>(name);
                        yield return(request);

                        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                        asset.init((TextAsset)request.asset);
                        obj = asset;
                    }
                    else
                    {
                        request = Resources.LoadAsync(name, type);
                        yield return(request);

                        obj = request.asset;
                    }
                    break;

                case XSheet.XLocationType.Bundle:
                    yield return(XBundleManager.Instance.LoadAssetAsync(info,
                                                                        type, delegate(Object result) { obj = result; }));

                    break;

                case XSheet.XLocationType.Data:
                    string fileUrl    = XSheet.GetLocalFileUrl(System.IO.Path.Combine(Application.persistentDataPath, info.fullName));
                    WWW    fileLoader = new WWW(fileUrl);
                    yield return(fileLoader);

                    if (!string.IsNullOrEmpty(fileLoader.error))
                    {
                        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                        asset.init(
                            fileLoader.bytes
                            );

                        if (type == typeof(Texture2D))
                        {
                            Texture2D texture = new Texture2D(1, 1);
                            texture.LoadImage(asset.bytes);

                            obj = texture;
                        }
                        else
                        {
                            obj = asset;
                        }
                    }
                    break;

                case XSheet.XLocationType.Streaming:
                    string streamUrl = XSheet.GetLocalFileUrl(System.IO.Path.Combine(Application.streamingAssetsPath, info.fullName));
                    if (Application.platform != RuntimePlatform.Android)
                    {
                        streamUrl = XSheet.GetLocalFileUrl(streamUrl);
                    }

                    WWW streamLoader = new WWW(streamUrl);
                    yield return(streamLoader);

                    if (!string.IsNullOrEmpty(streamLoader.error))
                    {
                        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                        asset.init(
                            streamLoader.bytes
                            );

                        if (type == typeof(Texture2D))
                        {
                            Texture2D texture = new Texture2D(1, 1);
                            texture.LoadImage(asset.bytes);
                            obj = texture;
                        }
                        else
                        {
                            obj = asset;
                        }
                    }
                    break;
                }
            }
            else
            {
                ResourceRequest request = Resources.LoadAsync(name, type);
                yield return(request);

                obj = request.asset;
            }

            if (!obj)
            {
                if (info != null)
                {
                    GLog.LogError("[XRes.Load] Can't find {0} in Location({1})", name, info.locationType);
                }
                else
                {
                    GLog.LogError("[XRes.Load] Can't find {0} in Resources", name);
                }
            }
        }

        callback(obj);
    }