Esempio n. 1
0
        internal HeaderRecord ReadHeader()
        {
            // Change state to Reading if state is Ready
            ChangeState(States.Reading, s => s == States.Ready);

            // Go to the start of the stream
            if (BaseStream.Position > 0)
            {
                BaseStream.Position = 0;
            }

            // Read header meta data
            RecordMetadata recordMetaData = context.CreateRecordMetaData();

            NextSegment(recordMetaData);

            // Create header, set flags and read the header
            var pluginHeader = context.CreateHeader();

            pluginHeader.RawFlags = recordMetaData.Flags;

            CurrentRecord = pluginHeader;
            pluginHeader.ReadRecord(this);
            CurrentRecord = null;

            // Keep plugin flags for internal use by the reader
            PluginFlags = (PluginFlags)recordMetaData.Flags;

            EndSegment();

            ChangeState(States.AfterHeader);

            return(pluginHeader);
        }
Esempio n. 2
0
        ///////////////////////////////////////////////////////////////////////

        public static bool HasFlags(
            PluginFlags flags,
            PluginFlags hasFlags,
            bool all
            )
        {
            if (all)
            {
                return((flags & hasFlags) == hasFlags);
            }
            else
            {
                return((flags & hasFlags) != PluginFlags.None);
            }
        }
Esempio n. 3
0
 public PluginData(
     string name,
     string group,
     string description,
     IClientData clientData,
     PluginFlags flags,
     Version version,
     Uri uri,
     AppDomain appDomain,
     Assembly assembly,
     AssemblyName assemblyName,
     string fileName,
     string typeName,
     CommandDataList commands,
     PolicyDataList policies,
     LongList commandTokens,
     LongList functionTokens,
     LongList policyTokens,
     LongList traceTokens,
     ResourceManager resourceManager,
     ObjectDictionary auxiliaryData,
     long token
     )
 {
     this.kind            = IdentifierKind.PluginData;
     this.id              = AttributeOps.GetObjectId(this);
     this.name            = name;
     this.group           = group;
     this.description     = description;
     this.clientData      = clientData;
     this.flags           = flags;
     this.version         = version;
     this.uri             = uri;
     this.appDomain       = appDomain;
     this.assembly        = assembly;
     this.assemblyName    = assemblyName;
     this.fileName        = fileName;
     this.typeName        = typeName;
     this.commands        = commands;
     this.policies        = policies;
     this.commandTokens   = commandTokens;
     this.functionTokens  = functionTokens;
     this.policyTokens    = policyTokens;
     this.traceTokens     = traceTokens;
     this.resourceManager = resourceManager;
     this.auxiliaryData   = auxiliaryData;
     this.token           = token;
 }
Esempio n. 4
0
 public PluginFlagsAttribute(PluginFlags flags)
 {
     this.flags = flags;
 }
Esempio n. 5
0
 private object ReadPrimitiveFieldValue(MemberInfo memberInfo)
 {
     if (!memberInfo.IsPrimitiveType)
     {
         Field property = Context.CreateField(memberInfo.FieldType);
         property.ReadField(this);
         return(property);
     }
     else if (memberInfo.FieldType == typeof(string))
     {
         if (memberInfo.LocalizedStringGroup != LocalizedStringGroups.None && PluginFlags.HasFlag(PluginFlags.Localized))
         {
             uint index = ReadUInt32();
             return(GetLocalizedString(memberInfo.LocalizedStringGroup, (index)));
         }
         else
         {
             return(ReadPrimitiveFieldValue <string>());
         }
     }
     else if (memberInfo.FieldType == typeof(int))
     {
         return(ReadInt32());
     }
     else if (memberInfo.FieldType == typeof(uint))
     {
         if (memberInfo.IsReference)
         {
             return(ReadReference(memberInfo.ReferencedFormKinds));
         }
         else
         {
             return(ReadUInt32());
         }
     }
     else if (memberInfo.FieldType == typeof(long))
     {
         return(ReadInt64());
     }
     else if (memberInfo.FieldType == typeof(ulong))
     {
         return(ReadUInt64());
     }
     else if (memberInfo.FieldType == typeof(short))
     {
         return(ReadInt16());
     }
     else if (memberInfo.FieldType == typeof(ushort))
     {
         return(ReadUInt16());
     }
     else if (memberInfo.FieldType == typeof(char))
     {
         return((char)ReadByte());
     }
     else if (memberInfo.FieldType == typeof(byte))
     {
         return(ReadByte());
     }
     else if (memberInfo.FieldType == typeof(float))
     {
         if (memberInfo.IsFakeFloat)
         {
             return((float)ReadInt32() / 100);
         }
         else
         {
             return(ReadSingle());
         }
     }
     else
     {
         throw new InvalidProgramException("Unsupported primitive field type: " + memberInfo.FieldType.FullName);
     }
 }
Esempio n. 6
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-nocommands", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-nofunctions", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-nopolicies", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-notraces", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-noprovide", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-noresources", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-verifiedonly", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-trustedonly", null),
#if ISOLATED_PLUGINS
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-noisolated", null),
#else
                            new Option(null, OptionFlags.Unsafe | OptionFlags.Unsupported, Index.Invalid, Index.Invalid,
                                       "-noisolated", null),
#endif
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-clientdata", null),
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-data", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            //
                            // NOTE: There should be a minimum of one and a maximum
                            //       of three arguments after the final option.
                            //
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 3) >= arguments.Count))
                            {
                                string path = ((argumentIndex + 2) < arguments.Count) ?
                                              (string)arguments[argumentIndex + 2] : String.Empty;

                                Interpreter slaveInterpreter = null;

                                code = interpreter.GetNestedSlaveInterpreter(
                                    path, LookupFlags.Interpreter, false,
                                    ref slaveInterpreter, ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    Variant     value           = null;
                                    IClientData localClientData = clientData;

                                    if (options.IsPresent("-clientdata", ref value))
                                    {
                                        IObject @object = (IObject)value.Value;

                                        if ((@object.Value == null) ||
                                            (@object.Value is IClientData))
                                        {
                                            localClientData = (IClientData)@object.Value;
                                        }
                                        else
                                        {
                                            result = "option value has invalid clientData";
                                            code   = ReturnCode.Error;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (options.IsPresent("-data", ref value))
                                        {
                                            IObject @object = (IObject)value.Value;

                                            if (@object != null)
                                            {
                                                localClientData = _Public.ClientData.WrapOrReplace(
                                                    localClientData, @object.Value);
                                            }
                                            else
                                            {
                                                result = "option value has invalid data";
                                                code   = ReturnCode.Error;
                                            }
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            //
                                            // NOTE: All plugins loaded by this command are considered
                                            //       as having been loaded "on demand".
                                            //
                                            PluginFlags pluginFlags = PluginFlags.Demand;

                                            //
                                            // NOTE: Add the plugin flags for the target interpreter.
                                            //
                                            pluginFlags |= slaveInterpreter.PluginFlags;

#if ISOLATED_PLUGINS
                                            //
                                            // NOTE: Disable loading this plugin into an isolated
                                            //       application domain (i.e. load it into the default
                                            //       application domain for the target interpreter).
                                            //
                                            if (options.IsPresent("-noisolated"))
                                            {
                                                pluginFlags &= ~PluginFlags.Isolated;
                                            }
#endif

                                            if (options.IsPresent("-nocommands"))
                                            {
                                                pluginFlags |= PluginFlags.NoCommands;
                                            }

                                            if (options.IsPresent("-nofunctions"))
                                            {
                                                pluginFlags |= PluginFlags.NoFunctions;
                                            }

                                            if (options.IsPresent("-nopolicies"))
                                            {
                                                pluginFlags |= PluginFlags.NoPolicies;
                                            }

                                            if (options.IsPresent("-notraces"))
                                            {
                                                pluginFlags |= PluginFlags.NoTraces;
                                            }

                                            if (options.IsPresent("-noprovide"))
                                            {
                                                pluginFlags |= PluginFlags.NoProvide;
                                            }

                                            if (options.IsPresent("-noresources"))
                                            {
                                                pluginFlags |= PluginFlags.NoResources;
                                            }

                                            if (options.IsPresent("-verifiedonly"))
                                            {
                                                pluginFlags |= PluginFlags.VerifiedOnly;
                                            }

                                            if (options.IsPresent("-trustedonly"))
                                            {
                                                pluginFlags |= PluginFlags.TrustedOnly;
                                            }

                                            string fileName = PathOps.ResolveFullPath(
                                                interpreter, arguments[argumentIndex]);

                                            if (!String.IsNullOrEmpty(fileName))
                                            {
                                                string typeName = null;

                                                if ((argumentIndex + 1) < arguments.Count)
                                                {
                                                    typeName = arguments[argumentIndex + 1];
                                                }

                                                IPlugin plugin = null;
                                                long    token  = 0;

                                                try
                                                {
                                                    code = slaveInterpreter.LoadPlugin(
                                                        fileName,
#if CAS_POLICY
                                                        null, null, AssemblyHashAlgorithm.None,
#endif
                                                        typeName, localClientData, pluginFlags,
                                                        ref plugin, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        code = slaveInterpreter.AddPlugin(
                                                            plugin, localClientData, ref token,
                                                            ref result);
                                                    }
                                                }
                                                finally
                                                {
                                                    if (code != ReturnCode.Ok)
                                                    {
                                                        if (token != 0)
                                                        {
                                                            //
                                                            // NOTE: Terminate and remove the plugin now.
                                                            //       This does not unload the associated
                                                            //       AppDomain, if any.
                                                            //
                                                            ReturnCode removeCode;
                                                            Result     removeResult = null;

                                                            removeCode = slaveInterpreter.RemovePlugin(
                                                                token, localClientData, ref removeResult);

                                                            if (removeCode != ReturnCode.Ok)
                                                            {
                                                                DebugOps.Complain(
                                                                    slaveInterpreter, removeCode,
                                                                    removeResult);
                                                            }
                                                        }

                                                        if (plugin != null)
                                                        {
                                                            //
                                                            // NOTE: Unload the plugin.  This basically does
                                                            //       "nothing" unless the plugin was isolated.
                                                            //       In that case, it unloads the associated
                                                            //       AppDomain.
                                                            //
                                                            ReturnCode unloadCode;
                                                            Result     unloadResult = null;

                                                            unloadCode = slaveInterpreter.UnloadPlugin(
                                                                plugin, localClientData, pluginFlags |
                                                                PluginFlags.SkipTerminate, ref unloadResult);

                                                            if (unloadCode != ReturnCode.Ok)
                                                            {
                                                                DebugOps.Complain(
                                                                    slaveInterpreter, unloadCode,
                                                                    unloadResult);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                result = "invalid file name";
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"load ?options? fileName ?packageName? ?interp?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"load ?options? fileName ?packageName? ?interp?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
Esempio n. 7
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-clientdata", null),
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-data", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-keeplibrary", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocomplain", null),
                            new Option(null, OptionFlags.MustHaveMatchModeValue, Index.Invalid, Index.Invalid, "-match",
                                       new Variant(StringOps.DefaultUnloadMatchMode)),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            //
                            // NOTE: There should be a minimum of one and a maximum
                            //       of three arguments after the final option.
                            //
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 3) >= arguments.Count))
                            {
                                string path = ((argumentIndex + 2) < arguments.Count) ?
                                              (string)arguments[argumentIndex + 2] : String.Empty;

                                Interpreter slaveInterpreter = null;

                                code = interpreter.GetNestedSlaveInterpreter(
                                    path, LookupFlags.Interpreter, false,
                                    ref slaveInterpreter, ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    Variant     value           = null;
                                    IClientData localClientData = clientData;

                                    if (options.IsPresent("-clientdata", ref value))
                                    {
                                        IObject @object = (IObject)value.Value;

                                        if ((@object.Value == null) ||
                                            (@object.Value is IClientData))
                                        {
                                            localClientData = (IClientData)@object.Value;
                                        }
                                        else
                                        {
                                            result = "option value has invalid clientData";
                                            code   = ReturnCode.Error;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (options.IsPresent("-data", ref value))
                                        {
                                            IObject @object = (IObject)value.Value;

                                            localClientData = _Public.ClientData.WrapOrReplace(
                                                localClientData, @object.Value);
                                        }

                                        MatchMode mode = StringOps.DefaultUnloadMatchMode;

                                        if (options.IsPresent("-match", ref value))
                                        {
                                            mode = (MatchMode)value.Value;
                                        }

                                        bool noCase = false;

                                        if (options.IsPresent("-nocase"))
                                        {
                                            noCase = true;
                                        }

                                        if (slaveInterpreter.HasPlugins(ref result))
                                        {
                                            string fileName = PathOps.ResolveFullPath(
                                                interpreter, arguments[argumentIndex]);

                                            if (!String.IsNullOrEmpty(fileName))
                                            {
                                                string typeName = null;

                                                if ((argumentIndex + 1) < arguments.Count)
                                                {
                                                    typeName = arguments[argumentIndex + 1];
                                                }

                                                //
                                                // NOTE: Grab the plugin flags to match from the target
                                                //       interpreter and add the Demand flag to them.
                                                //
                                                PluginFlags pluginFlags =
                                                    slaveInterpreter.PluginFlags | PluginFlags.Demand;

                                                //
                                                // FIXME: PRI 4: Threading.
                                                //
                                                bool       unload = false;
                                                StringList list   = slaveInterpreter.CopyPluginKeys();

                                                foreach (string name in list)
                                                {
                                                    IPluginData pluginData = slaveInterpreter.GetPluginData(name);

                                                    //
                                                    // NOTE: Check that this plugin represents a loaded
                                                    //       assembly.
                                                    //
                                                    if (pluginData != null)
                                                    {
                                                        if ((pluginData.FileName != null) &&
                                                            PathOps.IsSameFile(interpreter,
                                                                               pluginData.FileName, fileName))
                                                        {
                                                            if (String.IsNullOrEmpty(typeName) ||
                                                                StringOps.Match(interpreter, mode,
                                                                                pluginData.TypeName, typeName, noCase) ||
                                                                StringOps.Match(interpreter, mode,
                                                                                pluginData.Name, typeName, noCase))
                                                            {
                                                                code = slaveInterpreter.UnloadPlugin(
                                                                    name, localClientData, pluginFlags,
                                                                    ref result);

                                                                if (code == ReturnCode.Ok)
                                                                {
                                                                    unload = true;
                                                                }

                                                                //
                                                                // NOTE: Stop as soon as we match and
                                                                //       attempt to unload a plugin,
                                                                //       whether or not we actually
                                                                //       unloaded it.  We always halt
                                                                //       on errors and since we only
                                                                //       support unloading a single
                                                                //       plugin at a time (even if
                                                                //       there are multiple plugins
                                                                //       contained in a particular
                                                                //       assembly file), we know it
                                                                //       is safe to stop now.
                                                                //
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }

                                                if ((code == ReturnCode.Ok) && !unload)
                                                {
                                                    if (typeName != null)
                                                    {
                                                        result = String.Format(
                                                            "type \"{0}\" and file \"{1}\" have never been loaded",
                                                            typeName, fileName);
                                                    }
                                                    else
                                                    {
                                                        result = String.Format(
                                                            "file \"{0}\" has never been loaded",
                                                            fileName);
                                                    }

                                                    code = ReturnCode.Error;
                                                }
                                            }
                                            else
                                            {
                                                result = "invalid file name";
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"unload ?options? fileName ?packageName? ?interp?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"unload ?options? fileName ?packageName? ?interp?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }