Exemple #1
0
        private LoadedMod CompileAssets()
        {
            LoadedMod mod = new LoadedMod();

            mod.toc          = toc;
            mod.assets       = new Dictionary <string, object>();
            mod.xmlObjects   = new Dictionary <string, object>();
            mod.luaFunctions = new Dictionary <string, Closure>();
            foreach (var cached in assetCache)
            {
                string      assetName = cached.Key;
                AssetLoader loader    = cached.Value;
                if (loader.CurrentState == AssetLoader.State.Success)
                {
                    mod.assets[assetName] = loader.Asset;
                }
                else
                {
                    if (loader.Error == AssetLoader.ErrorType.BundleError)
                    {
                        if (loader.BundleLoader.Error == AssetBundleLoader.ErrorType.WWWError)
                        {
                            throw new ModLoaderException(string.Format("Unable to load asset \"{0}\" due to error loading asset bundle: {1}", assetName, loader.BundleLoader.WWWError));
                        }
                        else
                        {
                            throw new ModLoaderException(string.Format("Unable to load asset \"{0}\" due to error loading asset bundle: {1}", assetName, loader.BundleLoader.Error));
                        }
                    }
                    else
                    {
                        throw new ModLoaderException(string.Format("Unable to load asset \"{0}\": {1}", assetName, loader.Error));
                    }
                }
            }
            foreach (var cached in xmlCache)
            {
                string    assetName = cached.Key;
                XMLLoader loader    = cached.Value;
                if (loader.CurrentState == XMLLoader.State.Success)
                {
                    mod.xmlObjects[assetName] = loader.LoadedObject;
                }
                else
                {
                    throw new ModLoaderException(string.Format("Unable to load XML file \"{0}\": {1}", assetName, loader.Error));
                }
            }
            foreach (var cached in luaCache)
            {
                string    scriptName = cached.Key;
                LuaLoader loader     = cached.Value;
                if (loader.CurrentState == LuaLoader.State.Success)
                {
                    foreach (var functionName in loader.Functions)
                    {
                        DynValue dvalue;
                        string   cachedName;
                        if (functionName == ".")
                        {
                            cachedName = scriptName;
                            dvalue     = loader.LoadedObject;
                        }
                        else
                        {
                            cachedName = string.Format("{0}.{1}", scriptName, functionName);
                            dvalue     = scripts.GetGlobal(functionName);
                        }
                        if (dvalue.Type == DataType.Function)
                        {
                            Debug.Log("Set function " + cachedName);
                            mod.luaFunctions[cachedName] = dvalue.Function;
                        }
                        else
                        {
                            throw new ModLoaderException(string.Format("Expected Lua function given {0}", cachedName));
                        }
                    }
                }
                else
                {
                    switch (loader.Error)
                    {
                    case LuaLoader.ErrorType.RuntimeException:
                        throw new ModLoaderException(string.Format("Runtime exception in Lua file \"{0}\": {1}", scriptName, loader.LuaError));

                    case LuaLoader.ErrorType.SyntaxException:
                        throw new ModLoaderException(string.Format("Syntax exception in Lua file \"{0}\": {1}", scriptName, loader.LuaError));

                    default:
                        throw new ModLoaderException(string.Format("Unable to load Lua file \"{0}\": {1}", scriptName, loader.Error));
                    }
                }
            }

            return(mod);
        }