コード例 #1
0
        /// <summary>
        /// Obtain a particular type from the MEF composition container (if it's in there)
        /// <remarks>This method doesn't report exceptions from types that don't exist.</remarks>
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <returns>Type from the MEF composition container or null</returns>
        public static T TryGet <T>() where T : class
        {
            try
            {
                var container = SledServiceReferenceCompositionContainer.Get;
                if (container == null)
                {
                    throw new NullReferenceException("container is null");
                }

                var lazy = container.GetExport <T>();
                if (lazy == null)
                {
                    throw new NullReferenceException("lazy is null");
                }

                return(lazy.Value);
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(
                    SledMessageType.Warning,
                    "{0}: Exception in TryGet<{1}>(): {2}",
                    typeof(SledServiceInstance), typeof(T), ex.Message);

                return(null);
            }
        }
コード例 #2
0
        public static SledLuaVarLookUpType FromCustomValues(SledLuaVarScopeType scope, SledLuaVarLookUpContextType context, IList <SledLuaVarNameTypePairType> namesAndTypes)
        {
            try
            {
                var lookUp =
                    new DomNode(SledLuaSchema.SledLuaVarLookUpType.Type)
                    .As <SledLuaVarLookUpType>();

                lookUp.Scope   = scope;
                lookUp.Context = context;

                foreach (var nameAndType in namesAndTypes)
                {
                    lookUp.NamesAndTypes.Add((SledLuaVarNameTypePairType)nameAndType.Clone());
                }

                lookUp.Index      = 0;
                lookUp.StackLevel = 0;

                return(lookUp);
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(
                    SledMessageType.Error,
                    "{0}: Exception in FromCustomValues: {1}",
                    typeof(SledLuaVarLookUpType), ex.Message);

                return(null);
            }
        }
コード例 #3
0
        /// <summary>
        /// Create lookup from a Lua variable
        /// </summary>
        /// <param name="luaVar"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static SledLuaVarLookUpType FromLuaVar(ISledLuaVarBaseType luaVar, SledLuaVarLookUpContextType context)
        {
            try
            {
                var lookUp =
                    new DomNode(SledLuaSchema.SledLuaVarLookUpType.Type)
                    .As <SledLuaVarLookUpType>();

                lookUp.Scope   = luaVar.Scope;
                lookUp.Context = context;

                // Go through and generate a list of key/value pairs of the hierarchy so we can
                // look up and find the variable on the target no matter where it may be
                foreach (var nameAndType in luaVar.TargetHierarchy)
                {
                    lookUp.NamesAndTypes.Add((SledLuaVarNameTypePairType)nameAndType.Clone());
                }

                lookUp.NamesAndTypes.Add(SledLuaVarNameTypePairType.Create(luaVar.DisplayName, luaVar.KeyType));

                var index      = 0;
                var stackLevel = 0;

                // Some special processing if a local or upvalue variable
                if (luaVar.DomNode.Is <SledLuaVarLocalUpvalueBaseType>())
                {
                    var varTmp = luaVar.DomNode.As <SledLuaVarLocalUpvalueBaseType>();

                    stackLevel = varTmp.Level;
                    index      = varTmp.Index;
                }

                // Some special processing if an environment variable
                if (luaVar.DomNode.Is <SledLuaVarEnvType>())
                {
                    var varTmp = luaVar.DomNode.As <SledLuaVarEnvType>();

                    stackLevel = varTmp.Level;
                }

                lookUp.StackLevel = stackLevel;
                lookUp.Index      = index;

                return(lookUp);
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(
                    SledMessageType.Error,
                    "{0}: Exception in FromLuaVar: {1}",
                    typeof(SledLuaVarLookUpType), ex.Message);

                return(null);
            }
        }
コード例 #4
0
        public bool Load(out ISledTarget target, XmlElement elem)
        {
            target = null;

            if (elem == null)
            {
                return(false);
            }

            var bSuccessful = false;

            try
            {
                var name = elem.GetAttribute("name");
                if (string.IsNullOrEmpty(name))
                {
                    return(false);
                }

                var ipaddress = elem.GetAttribute("ipaddress");
                if (string.IsNullOrEmpty(ipaddress))
                {
                    return(false);
                }

                IPAddress ipAddr;
                if (!IPAddress.TryParse(ipaddress, out ipAddr))
                {
                    return(false);
                }

                int port;
                if (!int.TryParse(elem.GetAttribute("port"), out port))
                {
                    return(false);
                }

                target      = CreateAndSetup(name, new IPEndPoint(ipAddr, port), null);
                bSuccessful = target != null;
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(SledMessageType.Error, "{0}: Exception loading settings: {1}", this, ex.Message);
                target = null;
            }

            return(bSuccessful);
        }
コード例 #5
0
        public void PhoneHome()
        {
            if (!m_canConnect)
            {
                return;
            }

            try
            {
                AtfUsageLogger.SendAtfUsageInfo();
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(SledMessageType.Error, "{0}: Exception in PhoneHome: {1}", this, ex.Message);
            }
        }
コード例 #6
0
ファイル: SledUtil.cs プロジェクト: victorzhangl/SLED
        /// <summary>
        /// Check if a file has a byte order mark
        /// </summary>
        /// <param name="filePath">Absolute path to file</param>
        /// <returns>True iff the file has a byte order mark</returns>
        public static bool FileHasBom(string filePath)
        {
            FileStream file = null;

            try
            {
                file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                if (file == Stream.Null)
                {
                    return(false);
                }

                if (!file.CanRead)
                {
                    return(false);
                }

                var bom = new byte[4];

                var iRead = file.Read(bom, 0, 4);
                if (iRead != 4)
                {
                    return(false);
                }

                if ((bom[0] == 0xFF) && (bom[1] == 0xFE)) // UTF-16
                {
                    return(true);
                }
                else if ((bom[0] == 0xFE) && (bom[1] == 0xFF)) // UTF-16 Big-Endian
                {
                    return(true);
                }
                else if ((bom[0] == 0xFF) && (bom[1] == 0xFE) && (bom[2] == 0x00) && (bom[3] == 0x00)) // UTF-32
                {
                    return(true);
                }
                else if ((bom[0] == 0x00) && (bom[1] == 0x00) && (bom[2] == 0xFE) && (bom[3] == 0xFF)) // UTF-32 Big-Endian
                {
                    return(true);
                }
                else if ((bom[0] == 0xEF) && (bom[1] == 0xBB) && (bom[2] == 0xBF)) // UTF-8
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(SledMessageType.Error, "{0}: Exception in FileHasBom: {1}", typeof(SledUtil), ex.Message);
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                    file.Dispose();
                }
            }

            return(false);
        }
コード例 #7
0
        protected virtual void DomNodeAttributeChanged(object sender, AttributeEventArgs e)
        {
            if (UndoingAttribute)
            {
                return;
            }

            if ((e.AttributeInfo != SledLuaSchema.SledLuaVarGlobalType.valueAttribute) &&
                (e.AttributeInfo != SledLuaSchema.SledLuaVarLocalType.valueAttribute) &&
                (e.AttributeInfo != SledLuaSchema.SledLuaVarUpvalueType.valueAttribute) &&
                (e.AttributeInfo != SledLuaSchema.SledLuaVarEnvType.valueAttribute))
            {
                return;
            }

            var bUndo = true;

            try
            {
                var bCanModifyVariables =
                    DebugService.IsConnected &&
                    !DebugService.IsDebugging;

                if (!bCanModifyVariables)
                {
                    SledOutDevice.OutLine(
                        SledMessageType.Error,
                        Localization.SledLuaModifyVarInvalidTime);

                    return;
                }

                var luaVar = e.DomNode.As <ISledLuaVarBaseType>();
                if (luaVar == null)
                {
                    return;
                }

                // Check if variable type is modifiable by SLED
                if (!SledLuaUtil.IsEditableLuaType(luaVar))
                {
                    SledOutDevice.OutLine(
                        SledMessageType.Error,
                        Localization.SledLuaModifyVarInvalidType);

                    return;
                }

                string szNewValue = null;

                // Check if what the user entered is valid for that particular Lua type
                switch (luaVar.LuaType)
                {
                case LuaType.LUA_TNUMBER:
                {
                    // Make sure no letters
                    double dNumber;
                    var    szValue = e.NewValue.ToString();
                    if (!double.TryParse(szValue, out dNumber))
                    {
                        SledOutDevice.OutLine(
                            SledMessageType.Error,
                            SledUtil.TransSub(Localization.SledLuaModifyVarInvalidValue1, szValue));

                        return;
                    }

                    szNewValue = szValue;
                }
                break;

                case LuaType.LUA_TSTRING:
                {
                    szNewValue = e.NewValue.ToString();
                }
                break;

                case LuaType.LUA_TBOOLEAN:
                {
                    // Make sure true/false/1/0
                    var szValue = e.NewValue.ToString().ToLower();
                    if (string.Compare(szValue, Resources.Resource.True, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        szNewValue = Resources.Resource.One;
                    }
                    else if (string.Compare(szValue, Resources.Resource.False, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        szNewValue = Resources.Resource.Zero;
                    }
                    else
                    {
                        if ((string.Compare(szValue, Resources.Resource.One, StringComparison.Ordinal) != 0) &&
                            (string.Compare(szValue, Resources.Resource.Zero, StringComparison.Ordinal) != 0))
                        {
                            SledOutDevice.OutLine(
                                SledMessageType.Error,
                                SledUtil.TransSub(Localization.SledLuaModifyVarInvalidValue2, e.NewValue));

                            return;
                        }

                        szNewValue = szValue;
                    }
                }
                break;
                }

                // Somehow the value the user wanted didn't make it through
                if (string.IsNullOrEmpty(szNewValue))
                {
                    return;
                }

                // Cheat and use this to get all the key value pairs.
                // This takes into account watched variables as well!
                var lookUp = SledLuaVarLookUpType.FromLuaVar(luaVar, SledLuaVarLookUpContextType.Normal);
                if (lookUp == null)
                {
                    return;
                }

                SledOutDevice.OutLine(SledMessageType.Info, Localization.SledLuaModifyVarSent);

                // Send message off
                DebugService.SendScmp(new Scmp.LuaVarUpdate(LuaLanguagePlugin.LanguageId, lookUp, szNewValue, (int)luaVar.LuaType));

                bUndo = false;
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(
                    SledMessageType.Error,
                    "{0}: Exception in DomNodeAttributeChanged: {1}",
                    this, ex.Message);

                bUndo = true;
            }
            finally
            {
                if (bUndo)
                {
                    UndoAttributeChange(e);
                }
            }
        }
コード例 #8
0
        protected virtual void OnDebugServiceLookupBegin()
        {
            LookingUp = true;

            // If not during an update it's a
            // manual lookup and we are good to go
            if (!DebugService.IsUpdateInProgress)
            {
                return;
            }

            // Uh-oh
            if (ListNameInsert.Count <= 0)
            {
                return;
            }

            // Find where to place the incoming variable
            try
            {
                var pieces = new List <string>();
                {
                    Pair <string, IList <SledLuaVarNameTypePairType> > temp;
                    if (!ListNameInsertDict.TryGetValue(ListNameInsert[0], out temp))
                    {
                        throw new InvalidOperationException("Unknown variable");
                    }

                    pieces.AddRange(temp.Second.Select(kv => kv.Name)); // to navigate to the right table
                    pieces.Add(temp.First);                             // actual variable name
                }

                TType insert = null;

                // Try and find where the variable should be inserted
                for (var i = 0; i < pieces.Count; i++)
                {
                    IList <TType> lstVariables;

                    if (i == 0)
                    {
                        lstVariables = Collection[0].Variables;
                    }
                    else
                    {
                        if (insert == null)
                        {
                            throw new InvalidOperationException("Variable not found");
                        }

                        lstVariables =
                            new List <TType>(
                                insert.Variables.Select(v => v.As <TType>()));
                    }

                    var iPos = -1;

                    for (var j = 0; j < lstVariables.Count; j++)
                    {
                        if (string.Compare(lstVariables[j].DisplayName, pieces[i], StringComparison.Ordinal) != 0)
                        {
                            continue;
                        }

                        iPos = j;
                        break;
                    }

                    if (iPos == -1)
                    {
                        throw new InvalidOperationException("Variable not found");
                    }

                    insert = lstVariables[iPos];
                }

                if (insert == null)
                {
                    throw new InvalidOperationException("Variable not found");
                }

                // Finally found where to place lookups
                ListInsert.Add(insert);
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLineDebug(SledMessageType.Error, "{0}: Exception in OnDebugServiceLookupBegin: {1}", this, ex.Message);
            }
            finally
            {
                // Remove the item we just used
                if (ListNameInsert.Count > 0)
                {
                    ListNameInsertDict.Remove(ListNameInsert[0]);
                    ListNameInsert.RemoveAt(0);
                }
            }
        }