# coding:utf8

__author__ = '极无双'

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

lines = ['invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onCreate()V',
         'invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onLowMemory()V',
         'invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onTerminate()V',
         'invoke-virtual {v0, p1}, Lcom/kf/framework/KFApplication;->onTrimMemory(I)V',
         'invoke-virtual {v0, p1}, Lcom/kf/framework/KFApplication;->onConfigurationChanged(Landroid/content/res/Configuration;)V']


# def script_init(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
#     if channelSdkInfo["id"] == "549":
#         gw_data_center.pack_small = False
#     return


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    # logging.info('------------SDK:'+SDK)
    # logging.info('------------decompileDir:'+decompileDir)
    # logging.info('------------channelSdkInfo:'+str(channelSdkInfo))
    # logging.info('------------new_game_channel_info:'+str(new_game_channel_info))
    # logging.info('------------gameInfo:'+str(gameInfo))
    # logging.info("------rm baidu smali------")
    # restdir = decompileDir+"/"+"smali_classes2/com/baidu/gamesdk"
    # if os.path.exists(restdir):
    # shutil.rmtree(restdir)

    # s3 = decompileDir + "/smali_classes3"

    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['SDKName'] == 'reverseforkupai':
        replace_kf_application(decompileDir)


def replace_kf_application(extract_dir):
    goal_file = os.path.join(extract_dir, 'smali/com/kunlun/platform/android/KunlunApplication.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()
            for line in lines:
                change_line = line.replace('Lcom/kf/framework/KFApplication', 'Landroid/app/Application')
                cont = cont.replace(line, change_line)
            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


if __name__ == '__main__':
    replace_kf_application("C:\\Users\\Administrator\\Desktop\\xianjian")
