# coding:utf8

__author__ = 'dong'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob
from xml.etree.ElementTree import ElementTree, Element
import distutils.dir_util
import gw_file_system
import gw_data_center

change_map = {
    "invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onCreate()V": "invoke-virtual {v0}, Landroid/app/Application;->onCreate()V",
    "invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onLowMemory()V": "invoke-virtual {v0}, Landroid/app/Application;->onLowMemory()V",
    "invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onTerminate()V": "invoke-virtual {v0}, Landroid/app/Application;->onTerminate()V",
    "invoke-virtual {v0, p1}, Lcom/kf/framework/KFApplication;->onConfigurationChanged(Landroid/content/res/Configuration;)V": "invoke-virtual {v0, p1}, Landroid/app/Application;->onConfigurationChanged(Landroid/content/res/Configuration;)V",
    "invoke-virtual {v0, p1}, Lcom/kf/framework/KFApplication;->onTrimMemory(I)V": "invoke-virtual {v0, p1}, Landroid/app/Application;->onTrimMemory(I)V", }


def replace_super_application(decompileDir):
    smali_files = glob.glob(
        os.path.join(decompileDir, 'smali*/ourpalm/android/channels/Info/Ourpalm_Channels_Application.smali'))
    if len(smali_files) == 1:
        game_application_file = smali_files[0]
        print game_application_file
        if os.path.isfile(game_application_file):
            with open(game_application_file, "r+") as f:
                file_str = f.read()
                for k, v in change_map.items():
                    file_str = file_str.replace(k, v)
            with open(game_application_file, "w+") as f:
                f.write(file_str)


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['SDKName'] == 'reverseforkupai':
        replace_super_application(decompileDir)

    replace_application_verify_rose_bug(decompileDir)

def isNeedReplaceApplicationLine(line):
    return '.super Landroid/app/Application;' in line or 'invoke-direct {p0}, Landroid/app/Application;-><init>()V' in line or 'invoke-super {p0, p1}, Landroid/app/Application;->attachBaseContext(Landroid/content/Context;)V' in line or 'invoke-super {p0, p1}, Landroid/app/Application;->onConfigurationChanged(Landroid/content/res/Configuration;)V' in line or 'invoke-super {p0}, Landroid/app/Application;->onLowMemory()V' in line or 'invoke-super {p0}, Landroid/app/Application;->onTerminate()V' in line or 'invoke-super {p0, p1}, Landroid/app/Application;->onTrimMemory(I)V' in line or 'invoke-super {p0}, Landroid/app/Application;->onCreate()V' in line

def is_rose_replace_application(line):
    return '.super Lcom/coolyun/framework/CoolYunApplication;' in line or 'invoke-direct {p0}, Lcom/coolyun/framework/CoolYunApplication;-><init>()V' in line or 'invoke-super {p0, p1}, Lcom/coolyun/framework/CoolYunApplication;->attachBaseContext(Landroid/content/Context;)V' in line or 'invoke-super {p0, p1}, Lcom/coolyun/framework/CoolYunApplication;->onConfigurationChanged(Landroid/content/res/Configuration;)V' in line or 'invoke-super {p0}, Lcom/coolyun/framework/CoolYunApplication;->onLowMemory()V' in line or 'invoke-super {p0}, Lcom/coolyun/framework/CoolYunApplication;->onTerminate()V' in line or 'invoke-super {p0, p1}, Lcom/coolyun/framework/CoolYunApplication;->onTrimMemory(I)V' in line or 'invoke-super {p0}, Lcom/coolyun/framework/CoolYunApplication;->onCreate()V' in line

def replace_application_verify_rose_bug(decompile_dir):
    for dirpath, dirnames, filenames in os.walk(decompile_dir):
        if r'ourpalm\android\channels\Info' in dirpath and 'Ourpalm_Channels_Application.smali' in filenames:
            application_path = os.path.join(dirpath, "Ourpalm_Channels_Application.smali")
            print(application_path)
            f = open(application_path, "r")
            smali_str_arr = f.readlines()
            f.close()
            for line in smali_str_arr:
                if not is_rose_replace_application(line) and 'Lcom/coolyun/framework/CoolYunApplication' in line:
                    smali_str_arr[smali_str_arr.index(line)] = line.replace('Lcom/coolyun/framework/CoolYunApplication', 'Landroid/app/Application')
                if isNeedReplaceApplicationLine(line):
                    smali_str_arr[smali_str_arr.index(line)] = line.replace('Landroid/app/Application', 'Lcom/coolyun/framework/CoolYunApplication')
            f = open(application_path, "w")
            f.write(''.join(smali_str_arr))
            f.close()


if __name__ == '__main__':
    replace_super_application("D:\\work\\yqcr")
