示例#1
0
 internal void LoadPlugin(string file)
 {
     LogUtils.Debug("Loading plugin: " + Path.GetFileName(file));
     Sandbox.Instance.LoadAssembly(file, ((success, assembly, appdomain) =>
     {
         if (!success)
         {
             LogUtils.Debug("Couldn't load plugin: " + file);
             return;
         }
         string failedInstruction;
         string failReason;
         if (!SafeCodeHandler.IsSafeAssembly(assembly, out failedInstruction, out failReason))
         {
             LogUtils.LogWarning("WARNING: Plugin " + file + " accesses restricted code! Check failed: " + failedInstruction + (string.IsNullOrEmpty(failReason) ? "" : " (" + failReason + ")"));
             return;
         }
         _loadedAssemblies.Add(file, assembly);
         LoadPluginFromAssembly(assembly, appdomain, file);
     }));
 }
示例#2
0
        public Component[] LoadScripts(string assetName, Type t = null)
        {
            TextAsset asset = null;

            try
            {
                asset = _assetBundle.LoadAsset <TextAsset>(assetName);
            }
            catch (Exception)
            {
                // ignored
            }

            if (asset == null)
            {
                LogUtils.Debug("No scripts found in asset bundle: " + _assetBundle);
                return(new Component[0]);
            }

            if (_bundleScripts == null)
            {
                _bundleScripts = new GameObject(Name + "-BundleScripts");
            }
            List <Component> components = new List <Component>();
            var    assembly             = Assembly.Load(asset.bytes);
            string failedAt;
            string failedInstruction;

            if (!SafeCodeHandler.IsSafeAssembly(assembly, out failedInstruction, out failedAt))
            {
                _assetBundle.Unload(true);
                throw new Exception("Illegal asset script detected: [" + failedInstruction + "] at " + failedAt + " is not allowed (file: " + _assetFile + ")");
            }

            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                if (!type.IsSubclassOf(typeof(Component)) && type != typeof(Component))
                {
                    continue;
                }
                if (t != null && (!type.IsSubclassOf(t) && type != t))
                {
                    continue;
                }
                AttachToGameObjectAttribute[] attrs = (AttachToGameObjectAttribute[])type.GetCustomAttributes(typeof(AttachToGameObjectAttribute),
                                                                                                              true);

                bool wasAttached = false;
                foreach (AttachToGameObjectAttribute attr in attrs)
                {
                    GameObject obj = LoadAsset <Object>(attr.GameObject) as GameObject;
                    if (obj == null)
                    {
                        LogUtils.LogError("AttachToGameObjectAttribute: Couldn't find GameObject \"" + attr.GameObject + "\" in component " + type.FullName + " in bundle " + _assetBundle + "!");
                        continue;
                    }

                    components.Add(_bundleScripts.AddComponent(type));
                    wasAttached = true;
                }

                if (!wasAttached)
                {
                    components.Add(_bundleScripts.AddComponent(type));
                }
            }

            return(components.ToArray());
        }