# 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
from xml.etree import ElementTree as ET

ANDROID_NS = 'http://schemas.android.com/apk/res/android'


def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    handle_smali(extract_dir)
    # fix_coolyun_application_getinstance_null_bug(extract_dir)
    return


def handle_smali(decompile_dir_path):
    for dirpath, dirnames, filenames in os.walk(decompile_dir_path):
        if 'com\\youzu\\gamexy' in dirpath and 'Game.smali' in filenames:
            smali_path = os.path.join(dirpath, 'Game.smali')
            print(smali_path)
            f = open(smali_path, 'r')
            smali_str_arr = f.readlines()
            f.close()
            for line in smali_str_arr:
                if 'invoke-static {p0}, Lcom/youzu/gamexy/Game;->Init(Landroid/content/Context;)V' in line:
                    logging.info("script脚本执行成功gamexy")
                    smali_str_arr[smali_str_arr.index(
                        line)] = '\t#invoke-static {p0}, Lcom/youzu/gamexy/Game;->Init(Landroid/content/Context;)V\n'
            f = open(smali_path, 'w')
            f.write(''.join(smali_str_arr))
            f.close()

def fix_coolyun_application_getinstance_null_bug(decompile_dir):
    insert_str_list = [
        '\n\t.locals 1\n\n',
        '\tsget-object v0, Lcom/coolyun/framework/CoolYunApplication;->application:Landroid/content/Context;\n\n',
        '\tcheck-cast v0, Lcom/coolyun/framework/CoolYunApplication;\n\n',
        '\treturn-object v0\n\n']
    for dirpath, dirnames, filenames in os.walk(decompile_dir):
        if r'com\coolyun\framework' in dirpath and 'CoolYunApplication.smali' in filenames:
            smali_path = os.path.join(dirpath, 'CoolYunApplication.smali')
            with open(smali_path, 'r') as f:
                smali_str_arr = f.readlines()
                index_str = '.method public static getInstance()Lcom/coolyun/framework/CoolYunApplication;'
                end_method_str = '.end method'
                insert_str = ''.join(insert_str_list)
                on_create_index = 0
                insert_index = 0
                for line in smali_str_arr:
                    if index_str in line:
                        on_create_index = smali_str_arr.index(line)
                        print('on_create_index', on_create_index, line)
                        break
                for index, value in enumerate(smali_str_arr):
                    if index > on_create_index and end_method_str in value:
                        insert_index = index
                        print('insert_index', index)
                        break
                print(on_create_index, insert_index)
                del smali_str_arr[on_create_index + 1:insert_index]
                smali_str_arr.insert(on_create_index + 1, insert_str)
            with open(smali_path, 'w') as f:
                f.write(''.join(smali_str_arr))
                break

if __name__ == '__main__':
    d="D:/work/autopack3/tool/workspace/quanminpaopaochaorenkp/120/extract"
    handle_smali(d)
    # replace_super_application("D:\work\wzdq")
    # modify_manifest("E:\\apk\\youhua\\youhua.zip.out", "requestLegacyExternalStorage")
