Exemple #1
0
class CreateAndroidMk:

    platfom_pxiel = ''

    mainmk = ""

    mainmkPath = ""
    storeMkPath = ""

    rootpath = ""

    platformCode = 0

    push_platform = ""

    log_platform = ""

    copy = ""

    changeMK = False

    def setPushPlatform(self, pn):
        self.push_platform = pn

    def setLogPlatform(self, pn):
        self.log_platform = pn

    def __init__(self, _rootpath, platform, platformCode):
        self.platfom_pxiel = platform
        self.rootpath = _rootpath
        self.platformCode = platformCode

        self.mainmkPath = _rootpath + "base/mk/Android.mk"
        self.storeMkPath = _rootpath + "jni/Android.mk"
        self.copy = Copy()

    def openFileByWrite(self, path, info):
        path = self.rootpath + path
        f = open(path, 'w')
        f.write(info)
        f.close()

    #读取文件内容
    def getFileData(self, path):
        f = open(path, 'r')
        str = f.read()
        f.close()
        return str

    def saveFile(self, path, info):
        f = open(path, 'w')
        f.write(info)
        f.close()

    #替换文件内容
    #fromStr 源内容
    #flagStr 替换标记
    #replaceStr 替换后内容
    def replaceData(self, fromStr, flagStr, replaceStr):
        #print "flagStr == ",flagStr ,"  replaceStr == ",replaceStr
        return fromStr.replace(flagStr, replaceStr)

    def start(self):
        self.mainmk = self.getFileData(self.mainmkPath)

        platform_mk = self.rootpath + "platform/" + self.platfom_pxiel + "/mk/Android.mk"

        if os.path.exists(platform_mk):
            platform_mk_str = self.getFileData(platform_mk)
            self.mainmk = self.replaceData(self.mainmk, "#platform_so",
                                           platform_mk_str)
            #print "current mk info = \n",self.mainmk
#------------------------- push platform ------------------------------------------------------------

        if self.push_platform.strip():
            pushmkPath = self.rootpath + "push/" + self.push_platform + "/mk/Android.mk"
            #print "pushmkPath = ",pushmkPath
            if os.path.exists(pushmkPath):
                pushmk_str = self.getFileData(pushmkPath)
                self.mainmk = self.replaceData(self.mainmk, "#push_so",
                                               pushmk_str)

# print "current mk info = \n",self.mainmk
#-------------------------- end push platform ---------------------------------------------------------

#-------------------------- start Log platform --------------------------------------------------------
        if self.log_platform.strip():
            logmkPath = self.rootpath + "log/" + self.log_platform + "/mk/Android.mk"
            if os.path.exists(logmkPath):
                loghmk_str = self.getFileData(logmkPath)
                self.mainmk = self.replaceData(self.mainmk, "#log_so",
                                               loghmk_str)

#-------------------------- end log platform ----------------------------------------------------------
        oldMkInfo = self.getFileData(self.storeMkPath)
        if not cmp(oldMkInfo, self.mainmk) == 0:
            tkMessageBox.showinfo("警告", "mk  文件修改了 这下编译so文件的时候又得重新编了")
            self.saveFile(self.storeMkPath, self.mainmk)
        else:
            print "mk文件没变"
            self.changeMK = True

    def __copySoFiles__(self, fromDir, toDir):
        if os.path.exists(fromDir):
            self.copy.copyFiles(fromDir, toDir)

    def copySoFilesToCocos(self):
        toDir = self.rootpath + "jni/prebuild"
        if not self.changeMK:
            return
        #print "copySoFilesToCocos(self):"

        platfor_prebuild_path = self.rootpath + "platform/" + self.platfom_pxiel + "/prebuild"
        self.__copySoFiles__(platfor_prebuild_path, toDir)

        push_prebuild_path = self.rootpath + "push/" + self.push_platform + "/prebuild"
        self.__copySoFiles__(push_prebuild_path, toDir)

        log_prebuild_path = self.rootpath + "log/" + self.push_platform + "/prebuild"
        self.__copySoFiles__(log_prebuild_path, toDir)

    #这个是不编译cpp的处理方式
    def copySoFilesToAndroid(self):
        toDir = self.rootpath + "libs/armeabi"
        self.copy.removeFileByFilter(
            toDir, ['libcocos2djs.so', 'gdb.setup', 'gdbserver'])

        platfor_prebuild_path = self.rootpath + "platform/" + self.platfom_pxiel + "/prebuild"
        self.__copySoFiles__(platfor_prebuild_path, toDir)

        push_prebuild_path = self.rootpath + "push/" + self.push_platform + "/prebuild"
        self.__copySoFiles__(push_prebuild_path, toDir)

        log_prebuild_path = self.rootpath + "log/" + self.log_platform + "/prebuild"
        self.__copySoFiles__(log_prebuild_path, toDir)
Exemple #2
0
class Plugin:

    _rootPath = ""
    copy = ""

    platform_assets = "platform_assets"
    currentPlatform = ""

    def __init__(self, rootPath):
        self._rootPath = rootPath
        self.copy = Copy()

    """
    将临时资源拷贝到项目中
    """

    def tempAssetsToAndroidProject(self):
        self.copy.copyFiles(self.platform_assets, self.fromDir + "assets")

    def registerGamePlatform(self,
                             fromBaseDir,
                             toBaseDir,
                             platformCode,
                             operationCode,
                             platformname="default"):
        if os.path.exists(self.platform_assets):
            shutil.rmtree(self.platform_assets)

        os.mkdir(self.platform_assets)

        self.fromDir = fromBaseDir
        self.toDir = toBaseDir
        """
        平台的assets
        """
        if operationCode == 1:
            self.copy.copyFiles(fromBaseDir + "assets", self.platform_assets)
        elif operationCode == 2:
            self.copy.copyFiles(fromBaseDir + "assets", toBaseDir + "assets")

        self.copy.copyFiles(toBaseDir + "base/assets", toBaseDir + "assets")
        """ 复制 android res """
        Assets().removeAssetsByFilter(toBaseDir + "res", [])
        self.copy.copyFiles(toBaseDir + "base/res", toBaseDir + "res")
        print "copy ", fromBaseDir, " res"
        self.copy.copyFiles(fromBaseDir + "res", toBaseDir + "res")

        mainJsPath = "main.js"
        #if os.path.exists("frameworks/runtime-src/proj.android/platform/main.jsc"):
        #mainJsPath = "main.jsc"

        #if platformCode == 0:
        #self.copy.copyFile(fromBaseDir + mainJsPath, mainJsPath)
        #else:
        # self.copy.copyFile("frameworks/runtime-src/proj.android/platform/" + mainJsPath, mainJsPath)

        # self.copy.removeFileByFilter(toBaseDir + "libs",[])
        """  复制libs """
        # libsList = ["armeabi","armeabi-v7a","mips","x86"]
        self.copy.removeDir(toBaseDir + "libs")

        self.copy.copyFiles(toBaseDir + "base/libs", toBaseDir + "libs")
        self.copy.copyFiles(fromBaseDir + "libs", toBaseDir + "libs")

        # for name in libsList:
        #     ppath = fromBaseDir + 'libs/' + name
        #     if not os.path.exists(ppath):
        #         print "remove unused"
        #         self.copy.removeDir(toBaseDir + 'libs/' + name)

        appPath = 'src/org/cocos2dx/javascript/MyApplication.java'
        self.copy.copyFile(toBaseDir + 'base/' + appPath, toBaseDir + appPath)

        myActivity = 'src/org/cocos2dx/javascript/AppActivity.java'
        self.copy.copyFile(toBaseDir + 'base/' + myActivity,
                           toBaseDir + myActivity)
        """  复制平台代码 """
        Assets().removeAssetsByFilter(
            toBaseDir + "src/org/cocos2dx/javascript/platform",
            ['base', 'PlatformManager.java'])
        # Assets().removeAssetsByFilter(toBaseDir + "src/com/maple/hope/",[])
        Assets().removeAssetsByFilter(toBaseDir + "src/com/", ["android"])
        # self.copy.removeDir(toBaseDir + "src")
        # self.copy.copyFiles(toBaseDir + "base/src",toBaseDir + "src")
        self.copy.copyFiles(toBaseDir + "base/src", toBaseDir + "src")
        self.copy.copyFiles(fromBaseDir + "src", toBaseDir + "src")

        self.copy.copyFile(fromBaseDir + "AndroidManifest.xml",
                           toBaseDir + "AndroidManifest.xml")
        """ 复制平台配置 """
        platformJson = "platform.json"
        self.copy.copyFile(fromBaseDir + platformJson, platformJson)
        self.copy.copyFile(fromBaseDir + platformJson,
                           toBaseDir + 'assets/' + platformJson)

        # #-------------------------------   clear platform code ---------------------------------------
        # #baidu
        # self.copy.removeFileByFilter(toBaseDir + "src/org/cocos2dx/javascript/platform/baidu",[])
        # #oppo
        # self.copy.removeDir(toBaseDir + "src/com/nearme/gamecenter/open/api")

        # #-------------------------------   clear platform code ---------------------------------------

        # #-------------------------------   add platform code -----------------------------------------
        # # baidu_platform
        # if platformCode == 4:
        #     self.refisterBaidu(fromBaseDir,toBaseDir)
        # elif platformCode == 5:
        #     self.registerOppo(fromBaseDir,toBaseDir)
        #-------------------------------   add platform code ----------------------------------------

    def backFileToPlatfrom(self, f, t, t2=""):
        if os.path.exists(t):
            self.copy.copyFile(f, t)
        elif os.path.exists(t2):
            self.copy.copyFile(f, t2)

    """  将在项目中修改的平台代码保存到平台中去 """

    def backToPlatform(self):
        """  平台相关 src  """
        tailPath = 'src/org/cocos2dx/javascript/platform'
        info = self.copy.getFileData('python/log.txt')
        if info == "":
            return
        recoderList = info.split('_')
        platformname = recoderList[0]

        print "将当前项目中修改数据回退到平台%s" % platformname

        # curPlatJsonPath = "frameworks/runtime-src/proj.android/assets/platform.json"
        # jsonData = json.loads(self.copy.getFileData(curPlatJsonPath))
        # if not jsonData['platform_name'] == platformname:
        #     tkMessageBox.showinfo("通知","平台不当 保存的是%s,而当前是%s"%(platformname,jsonData['platform_name']))
        #     return

        headPath = 'frameworks/runtime-src/proj.android/'
        fromDir = headPath + tailPath
        toDir = headPath + 'platform/' + platformname + '/' + tailPath

        print "fromDir = ", fromDir, "  toDir = ", toDir
        for item in os.walk(fromDir):
            # print 'javascript/platform/base    ',item[0] ,'javascript/platform/base' in item[0]
            if not item[2] or 'javascript/platform/base' in item[0]:
                print "filter file%s" % item[0]
                continue

            filterList = ['.DS_Store', 'PlatformManager.java']
            for file in item[2]:
                if file in filterList:
                    continue
                fromPath = os.path.join(item[0], file)
                toPath = fromPath.replace(fromDir, toDir)
                # print fromPath , " ======> ",toPath
                self.copy.copyFile(fromPath, toPath)
        """ handler MyApplication """

        backFiles = [
            "src/org/cocos2dx/javascript/MyApplication.java",
            "src/org/cocos2dx/javascript/AppActivity.java",
            "src/org/cocos2dx/javascript/LoadingActivity.java"
        ]
        for file in backFiles:
            self.backFileToPlatfrom(
                headPath + file,
                headPath + 'platform/' + platformname + '/' + file,
                headPath + 'base/' + file)
        """      back to push   """
        pushname = recoderList[1]
        pushPath = headPath + 'src/org/cocos2dx/javascript/push'
        self.copy.copyFiles(pushPath, headPath + 'push/' + pushname + '/src')
        """  项目配置 """
        self.copy.copyFile(
            headPath + 'assets/platform.json',
            headPath + 'platform/' + platformname + '/platform.json')
        self.copy.copyFile(headPath + 'assets/main.js', 'main.js')
        """  about com dir"""
        for item in os.walk(headPath + "src/com"):
            if not item[2]:
                continue

            for file in item[2]:
                fromFile = os.path.join(item[0], file)
                toFile = fromFile.replace(
                    headPath + "src/com",
                    headPath + "platform/" + platformname + "/src/com", 1)
                if os.path.exists(toFile):
                    self.copy.copyFile(fromFile, toFile)

    def registerPushPlatform(self, fromBaseDir, toBaseDir, pushCode, p=None):

        javaDir = toBaseDir + "src/org/cocos2dx/javascript/push"
        self.copy.removeDir(javaDir)
        self.copy.copyFiles(fromBaseDir + 'src', javaDir)
        """
             tt平台需要用自己的android-support-v4.jar 而这个版本比push里的高,只能特殊处理下了  
        """
        # if not p == "tencent":
        #     self.copy.copyFiles(fromBaseDir + "assets",toBaseDir + "assets")
        #     if not p =="tt":
        #         self.copy.copyFiles(fromBaseDir + "libs",toBaseDir + "libs")
        #     else:
        #         self.copy.copyFiles(fromBaseDir + "libs",toBaseDir + "libs",filterFiles=['android-support-v4.jar'])
        filterList = ["tt", "yeshen"]
        self.copy.copyFiles(fromBaseDir + "assets", toBaseDir + "assets")
        if not p in filterList:
            self.copy.copyFiles(fromBaseDir + "libs", toBaseDir + "libs")
        else:
            self.copy.copyFiles(fromBaseDir + "libs",
                                toBaseDir + "libs",
                                filterFiles=['android-support-v4.jar'])
        self.currentPlatform = p

    def registerLogPlatform(self, fromBaseDir, toBaseDir):
        self.copy.copyFiles(fromBaseDir + "assets", self.platform_assets)
        self.copy.copyFiles(fromBaseDir + "libs", toBaseDir + "libs")
        self.copy.copyFiles(fromBaseDir + "res", toBaseDir + "res")

#---------------------------------------- platform --------------------------------------------------------------------

    def refisterBaidu(self, fromBaseDir, toBaseDir):
        self.copy.copyFiles(fromBaseDir + "src",
                            toBaseDir + "src/org/cocos2dx/javascript/platform")

    def registerOppo(self, fromBaseDir, toBaseDir):
        dirPath = "src/com/nearme/gamecenter/open/api"
        self.copy.copyFiles(fromBaseDir + dirPath, toBaseDir + dirPath)

#---------------------------------------- push -------------------------------------------------------------------------

    def registerXgPush(self, fromBaseDir, toBaseDir):
        javaPath = toBaseDir + "src/com/qq/xgdemo/receiver/MessageReceiver.java"
        #self.copy.removeFile(javaPath)
        self.copy.copyFile(fromBaseDir + "MessageReceiver.java", javaPath)

        javaPath = toBaseDir + "src/com/qq/xgdemo/receiver/XGNotification.java"
        #self.copy.removeFile(javaPath)
        self.copy.copyFile(fromBaseDir + "XGNotification.java", javaPath)

        javaPath = toBaseDir + "src/com/qq/xgdemo/receiver/NotificationService.java"
        #self.copy.removeFile(javaPath)
        self.copy.copyFile(fromBaseDir + "NotificationService.java", javaPath)

        javaPath = toBaseDir + "src/com/qq/xgdemo/receiver/DBOpenHelper.java"
        #self.copy.removeFile(javaPath)
        self.copy.copyFile(fromBaseDir + "DBOpenHelper.java", javaPath)

#------------------------- unit function -------------------------------

    def copyFile(self, fromPath, toPath):
        self.copyFile(fromPath, toPath)