Exemple #1
0
    def create_project(self,
                       project_name,
                       crypt_key,
                       xcode_project_path,
                       origin_xcode_project_name,
                       xcode_project_name,
                       target_name=None,
                       display_name=None,
                       xxtea_key=None,
                       xxtea_sign=None):

        project_data = {}

        project_data["name"] = project_name
        project_data["resource_path"] = project_name
        project_data["project_path"] = project_name
        project_data["crypt"] = ConfigCreator.create_crypt_info(crypt_key)
        project_data["xcode_project_path"] = xcode_project_path
        project_data["origin_xcode_project_name"] = origin_xcode_project_name
        project_data["xcode_project_name"] = xcode_project_name

        if target_name:
            project_data["target_name"] = target_name
        if display_name:
            project_data["display_name"] = display_name
        if xxtea_key:
            project_data["xxtea_key"] = xxtea_key
        if xxtea_sign:
            project_data["xxtea_sign"] = xxtea_sign

        project_data["gen_cpp_dir"] = RandomGenerater.generate_string(6, 12)
        project_data["gen_objc_dir"] = RandomGenerater.generate_string(6, 12)

        return project_data
Exemple #2
0
    def generate_class(tpl_folder_path,
                       field_count,
                       method_count,
                       max_parameter,
                       method_return_probability,
                       class_name=None):
        # gen fields
        fields = None
        if field_count > 0:
            fields = []
            for i in range(field_count):
                fields.append(ObjcGenerator.generate_field(tpl_folder_path))

        if not class_name:
            class_name = RandomGenerater.generate_string()
            class_name = class_name[0].upper() + class_name[1:]

        objc_class = ObjcClass(class_name, fields, None, tpl_folder_path)

        # gen methods
        methods = None
        if method_count > 0:
            methods = []

            for i in range(method_count):
                method = ObjcGenerator.generate_function(
                    tpl_folder_path, max_parameter, method_return_probability)
                method.objc_class = objc_class
                methods.append(method)

        objc_class.methods = methods
        return objc_class
Exemple #3
0
    def prepare_config(self, out_folder_path):

        if "namespace" in self.generate_config:
            if not self.generate_config["namespace"]:
                self.generate_config[
                    "namespace"] = RandomGenerater.generate_string(5,
                                                                   8).lower()

        return super(CppGarbageCode, self).prepare_config(out_folder_path)
Exemple #4
0
    def _gen_inject_code(self):
        """
        获取注入代码。
        目前实现二种代码,后面可以加入多种。
        :return:
        """
        p = random.randrange(0, 10)
        # now use two code type
        if p > 4:
            num = []
            for _ in range(0, 3):
                num.append(RandomGenerater.generate_int())

            tpl_data = {
                "num": num,
                "var_name": RandomGenerater.generate_string()
            }
            code_declare = TemplateManager.get_data(
                os.path.join(self.tpl_folder_path, "one_int_declare.cpp"),
                [tpl_data])

            code = TemplateManager.get_data(
                os.path.join(self.tpl_folder_path, "one_int.cpp"), [tpl_data])

            return code_declare, code
        else:
            num = []
            for _ in range(0, 6):
                num.append(RandomGenerater.generate_float())

            num.sort()
            tpl_data = {
                "num": num,
                "var_name": RandomGenerater.generate_string()
            }
            code_declare = TemplateManager.get_data(
                os.path.join(self.tpl_folder_path, "one_float_declare.cpp"),
                [tpl_data])

            code = TemplateManager.get_data(
                os.path.join(self.tpl_folder_path, "one_float.cpp"),
                [tpl_data])

            return code_declare, code
Exemple #5
0
    def prepare_config(self, out_folder_path):

        gen_file_count = gc_utils.get_range_count("generate_file_count",
                                                  self.generate_config, 6)

        if "group_name" not in self.generate_config:
            self.generate_config[
                "group_name"] = RandomGenerater.generate_string(6, 10)

        if "call_others" not in self.generate_config:
            self.generate_config["call_others"] = True

        if "search_path" not in self.generate_config:
            self.generate_config["search_path"] = out_folder_path

        if "namespace" in self.generate_config:
            if not self.generate_config["namespace"]:
                self.generate_config[
                    "namespace"] = RandomGenerater.generate_string(5,
                                                                   8).lower()

        return gen_file_count
Exemple #6
0
    def prepare(self):
        # check class name
        if not self.class_name:
            self.class_name = RandomGenerater.generate_string(8, 32)
            self.class_name = self.class_name[0].upper() + self.class_name[1:]

        cpp_class = CppGenerator.generate_class(
            self.tpl_folder_path, self.config["field_count"],
            self.config["method_count"], self.config["parameter_count"],
            self.config["return_probability"], self.class_name)

        if "call_others" in self.config and self.config["call_others"]:
            for i in range(len(cpp_class.methods) - 1):
                cpp_class.methods[i].call(cpp_class.methods[i + 1])

        self.c_class = cpp_class
Exemple #7
0
 def inject(self, functions, cpp_class, percent=1, code_times=6):
     """
     把调用类的方法和属性的代码插入源函数的代码之间
     :param functions:
     :param cpp_class:
     :return:
     """
     inst_name = RandomGenerater.generate_string()
     var_declare = cpp_class.get_stack_instance_def(inst_name)
     for function_info in functions:
         if function_info.root_statement:
             code_count = int(
                 len(
                     gc_utils.get_children_array_from_cursor(
                         function_info.root_statement)) * percent *
                 code_times)
             if code_count < 1:
                 code_count = 1
             codes = cpp_class.get_call_codes(inst_name, code_count)
             self._inject_function_more(function_info, var_declare, codes,
                                        percent)
Exemple #8
0
def main():
    work_path = os.getcwd()
    repack_project_path = os.path.dirname(os.path.realpath(__file__))

    parser = ArgumentParser()

    parser.add_argument('-p', '--project', dest='project',
                        help="xcode project file path")

    parser.add_argument('-s', '--scheme', dest='scheme',
                        help="xcode scheme")

    parser.add_argument('-m', '--mode', dest='mode',
                        help="build mod archive or ipa")

    parser.add_argument('-o', '--out', dest='out',
                        help="out put path")

    parser.add_argument('steps', nargs='*',
                        help="steps to run")

    args = parser.parse_args()

    xcode_project_file_path = args.project

    if not os.path.isabs(xcode_project_file_path):
        xcode_project_file_path = os.path.join(work_path, xcode_project_file_path)



    # 分析cocos路径
    pos = xcode_project_file_path.find("frameworks")

    if pos > -1:
        src_project = xcode_project_file_path[:pos - 1]
        xcode_project_path = os.path.dirname(xcode_project_file_path[pos:])
    else:
        raise Exception("not a cocos project")

    print("matrix:%s,xcode root:%s" % (src_project, xcode_project_path))

    origin_xcode_project_name = os.path.basename(xcode_project_file_path)
    project_name = os.path.splitext(origin_xcode_project_name)[0]
    xcode_project_name = project_name + ".xcodeproj"

    # 生成配置
    crypt_key = RandomGenerater.generate_string(6, 10)

    config_creator = ConfigCreator()
    project_data = config_creator.create_project(project_name, crypt_key, xcode_project_path,
                                                 origin_xcode_project_name, xcode_project_name)

    project_data["build_scheme"] = args.scheme

    if "out" in args and args.out:
        out_path = args.out
        if not os.path.isabs(out_path):
            out_path = os.path.join(work_path, out_path)
        project_data["archive_file_path_name"] = os.path.join(out_path, project_name)
        project_data["app_file_path_name"] = os.path.join(out_path, project_name + ".ipa")
    else:
        project_data["archive_file_path_name"] = project_name
        project_data["app_file_path_name"] =  project_name + ".ipa"

    # 打包的其它目录
    resource_dir = os.path.join(repack_project_path, "../resources")
    out_dir = os.path.join(repack_project_path, "../tempprojects")
    data_dir = os.path.join(repack_project_path, "data")

    # 配置保存成json
    project_config_path = os.path.join(resource_dir, project_name)
    if not os.path.exists(project_config_path):
        os.makedirs(project_config_path)
    config_file = os.path.join(resource_dir, project_name, "project.json")
    config_creator.save_project(project_data, config_file)

    # 读取操作步骤
    step_config_file = os.path.join(repack_project_path, "../steps.json")
    fp = open(step_config_file)
    step_config = json.load(fp)
    fp.close()

    # 进行打包
    repack.repack_project(src_project, out_dir, resource_dir, data_dir, project_data,
                          step_config["steps"], None, args.steps, None, 1)
Exemple #9
0
    def generate_call_file_for_files(self, out_folder_path,
                                     call_generate_codes):
        """
        使用一个文件引用生成的文件
        :param out_folder_path:
        :param generate_config:
        :param call_generate_codes:
        :return:
        """
        # generate call generated code prevent delete by link optimization
        exec_once_tpl = Template(file=os.path.join(self.tpl_folder_path,
                                                   "exec_code_once.tpl"),
                                 searchList=[{
                                     "code":
                                     "".join(call_generate_codes),
                                     "prefix":
                                     RandomGenerater.generate_string()
                                 }])
        exec_once = str(exec_once_tpl)

        if "generate_executor" in self.generate_config and self.generate_config[
                "generate_executor"]:
            print("generate a executor")
        else:
            print("insert into execute file")
            include_heads = "\n"
            for head_file in self.generated_head_files:
                include_heads += "#include \"%s\"\n" % head_file

        auto_all_name = RandomGenerater.generate_string(20, 30)
        auto_all_function = RandomGenerater.generate_string(20, 30)
        auto_all_head_file = os.path.join(out_folder_path,
                                          auto_all_name + ".h")
        auto_all_source_file = os.path.join(
            out_folder_path, auto_all_name + self.source_file_ext)
        self.generated_files.append(auto_all_head_file)
        self.generated_files.append(auto_all_source_file)

        auto_all_head_tpl = Template(file=os.path.join(self.tpl_folder_path,
                                                       "auto_all_head.tpl"),
                                     searchList=[{
                                         "name":
                                         auto_all_name,
                                         "headers":
                                         include_heads,
                                         "auto_all_function":
                                         auto_all_function
                                     }])

        auto_all_source_tpl = Template(file=os.path.join(
            self.tpl_folder_path, "auto_all_source.tpl"),
                                       searchList=[{
                                           "name":
                                           auto_all_name,
                                           "code":
                                           exec_once,
                                           "auto_all_function":
                                           auto_all_function
                                       }])

        fp = open(auto_all_head_file, "w+")
        fp.write(str(auto_all_head_tpl))
        fp.close()

        fp = open(auto_all_source_file, "w+")
        fp.write(str(auto_all_source_tpl))
        fp.close()

        return auto_all_name, auto_all_function
Exemple #10
0
 def generate_ext_name(use_rule_ext_probability=70):
     if random.randint(1,100)<use_rule_ext_probability:
         return file_exts[random.randint(0,file_exts_length-1)]
     else:
         return RandomGenerater.generate_string(2,3).lower()