예제 #1
0
    /// <summary>
    /// ����һ������  Ĭ����ĩβ��
    /// </summary>
    /// <param name="function"></param>
    public static void InsertFunction(EditorQueueData functionData, InsertPosType insertPosType = InsertPosType.End)
    {
        if (!File.Exists(taskFull))
        {
            var fc = File.Create(taskFull);
            fc.Close();
            fc.Dispose();
        }
        using (FileStream fs = new FileStream(taskFull, FileMode.Open, FileAccess.ReadWrite))
        {
            string allString = ReadFileStream(fs);
            List <EditorQueueData> tempData = null;
            if (!string.IsNullOrEmpty(allString))
            {
                tempData = LitJson.JsonMapper.ToObject <List <EditorQueueData> >(allString);
            }
            if (tempData == null)
            {
                tempData = new List <EditorQueueData>();
            }
            int insertIndex = 0;
            if (insertPosType == InsertPosType.End)
            {
                insertIndex = tempData.Count;
            }
            tempData.Insert(insertIndex, functionData);

            allString = LitJson.JsonMapper.ToJson(tempData);
            WriteFileStream(fs, allString);

            fs.Close();
        }
    }
예제 #2
0
        /// <summary>
        /// 插入smali语句
        /// </summary>
        /// <param name="smali_content">被插入的smali文件内容</param>
        /// <param name="insert_type">插入函数名</param>
        /// <param name="insert_find">插入的位置</param>
        /// <param name="insert_smali">植入的smali语句</param>
        /// <param name="pos_type">插入在目标位置之前还是之后</param>
        /// <returns>是否植入smali代码成功</returns>
        public static bool insert_smali_code(ref string smali_content, SmaliInsertFunctionType insert_type, string insert_find,string insert_smali, InsertPosType pos_type)
        {
            List<string> smali_lines = smali_content.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
            do
            {
                if (smali_lines.Count > 0)
                {
                    if (shell_env.insert_smali_function_title_dict.ContainsKey(insert_type))
                    {
                        List<string> find_functions = new List<string>();
                        find_functions.Add(shell_env.insert_smali_function_title_dict[insert_type]);
                        find_functions.Add(shell_env.insert_smali_function_title_dict[insert_type].Replace("public", "protected"));
                        find_functions.Add(shell_env.insert_smali_function_title_dict[insert_type].Replace("public", "private"));
                        int func_start_pos = -1;
                        int func_end_pos = -1;
                        int insert_pos = -1;
                        foreach (string find_func in find_functions)
                        {
                            if ((func_start_pos = get_smali_code_index(smali_lines, find_func)) != -1)
                                break;
                        }
                        if (func_start_pos == -1) break;
                        for (int i = func_start_pos; i < smali_lines.Count; i++)
                        {
                            if (smali_lines[i].IndexOf(shell_env.insert_smali_find_end_function) != -1)
                            {
                                if (func_end_pos == -1)
                                    func_end_pos = i;
                            }
                            if (smali_lines[i].IndexOf(insert_find) != -1)
                            {
                                if (insert_pos == -1)
                                    insert_pos = i;
                            }
                            if (func_end_pos != -1 && insert_pos != -1) break;

                        }
                        if (func_end_pos == -1 || insert_pos == -1) break;

                        if (insert_pos < func_end_pos && insert_pos > func_start_pos)
                        {
                            bool flag = false;
                            for (int i = func_start_pos; i < func_end_pos; i++)
                            {
                                if (smali_lines[i].IndexOf(insert_smali) != -1)
                                {
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                switch (pos_type) {
                                    case InsertPosType.insert_before:
                                        break;
                                    case InsertPosType.insert_after:
                                        insert_pos++;
                                        break;
                                    default:
                                        break;
                                }
                                smali_lines.Insert(insert_pos, insert_smali);

                                smali_content = string.Empty;
                                foreach (string line in smali_lines)
                                {
                                    smali_content += line + "\r\n";
                                }
                                return true;
                            }
                        }
                    }
                }
            } while (false);
            return false;
        }