# coding:utf8

__author__ = '哥哥捕鱼酷派'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob
import gw_data_center


def script_init(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    gw_data_center.pack_small = False


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == '549':
        add_super_invoke(decompileDir)
        replace_life_method(decompileDir)
        replace_permission_tips(decompileDir)


def add_super_invoke(decompileDir):
    goal_file = os.path.join(decompileDir, 'smali_classes2/com/bohaoo/dlby/coolpad/MyApplication.smali')
    print(os.path.isfile(goal_file))
    if os.path.isfile(goal_file):
        logging.info('method_proceed: ' + 'goal_file')
        print(goal_file)
        with open(goal_file, 'r+') as f:
            cont = f.read()
            change_str = '.method public onCreate()V'
            str = '.method public onCreate()V\n' \
                  'invoke-super {p0}, Lcom/kf/framework/KFApplication;->onCreate()V'
            cont = cont.replace(change_str, str)

            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


def replace_permission_tips(decompileDir):
    goal_file = os.path.join(decompileDir, 'smali_classes2/com/bohaoo/dlby/coolpad/AppActivity.smali')
    print(os.path.isfile(goal_file))
    if os.path.isfile(goal_file):
        logging.info('method_proceed: ' + 'goal_file')
        print(goal_file)
        with open(goal_file, 'r+') as f:
            cont = f.read()
            print(cont)
            change_str = "\u672a\u80fd\u83b7\u53d6\u5230\u5bf9\u5e94\u6743\u9650"
            str = ""
            cont = cont.replace(change_str, str)

        with open(goal_file, 'w+') as tf:
            tf.write(cont)


def replace_life_method(extract_dir):
    goal_file = os.path.join(extract_dir, 'smali_classes2/org/cocos2dx/javascript/AppActivity.smali')
    print(os.path.isfile(goal_file))
    if os.path.isfile(goal_file):
        logging.info('method_proceed: ' + 'goal_file')
        print(goal_file)
        with open(goal_file, 'r+') as f:
            cont = f.read()
            change_str = '.method protected onResume()V'
            str = '.method public onResume()V\n' \
                  'invoke-static {}, Lcom/kf/framework/SDKPluginWrapper;->onResume()V'
            cont = cont.replace(change_str, str)

            change_str_1 = '.method protected onPause()V'
            str_1 = '.method public onPause()V\n' \
                    'invoke-static {}, Lcom/kf/framework/SDKPluginWrapper;->onPause()V'
            cont = cont.replace(change_str_1, str_1)
            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


if __name__ == '__main__':
    replace_permission_tips("C:\\Users\\Administrator\\Desktop\\gege")
