117 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C#
		
	
	
using System.Linq;
 | 
						|
 | 
						|
namespace Guru.Editor
 | 
						|
{
 | 
						|
    using UnityEditor;
 | 
						|
    using UnityEngine;
 | 
						|
    using System;
 | 
						|
    using System.IO;
 | 
						|
    public class AndroidProjectMod
 | 
						|
    {
 | 
						|
        private static readonly int TargetSDKVersion = 33;
 | 
						|
        
 | 
						|
        private static readonly string LauncherName = "launcherTemplate";
 | 
						|
        private static string LauncherFullPath = Path.Combine(Application.dataPath, $"Plugins/Android/{LauncherName}.gradle");
 | 
						|
        
 | 
						|
        private static readonly string MainName = "mainTemplate";
 | 
						|
        private static string MainFullPath = Path.Combine(Application.dataPath,  $"Plugins/Android/{MainName}.gradle");
 | 
						|
        
 | 
						|
        private static readonly string PropertiesName = "gradleTemplate";
 | 
						|
        private static string PropertiesFullPath = Path.Combine(Application.dataPath,  $"Plugins/Android/{PropertiesName}.properties");
 | 
						|
        
 | 
						|
        public static void Apply()
 | 
						|
        {
 | 
						|
            FixLauncher();
 | 
						|
            FixMain();
 | 
						|
            FixProperties();
 | 
						|
            CheckTargetSDKVersion();
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        private static void FixLauncher()
 | 
						|
        {
 | 
						|
            if (!File.Exists(LauncherFullPath))
 | 
						|
            {
 | 
						|
                CopyFile($"{LauncherName}.txt", LauncherFullPath);
 | 
						|
                Debug.Log($"[MOD] --- Copy file to: {LauncherFullPath}");
 | 
						|
                return;
 | 
						|
            }
 | 
						|
 | 
						|
            var ptn1 = "**PACKAGING_OPTIONS**";
 | 
						|
            var ptn2 = "abortOnError false";
 | 
						|
            var lines = File.ReadAllLines(LauncherFullPath);
 | 
						|
 | 
						|
            string line = "";
 | 
						|
            for (int i = 0; i < lines.Length; i++)
 | 
						|
            {
 | 
						|
                line = lines[i];
 | 
						|
                if (line.Contains(ptn1))
 | 
						|
                {
 | 
						|
                    lines[i] = line.Replace(ptn1, "\n\n\tpackagingOptions {\n\t\texclude(\"META-INF/*.kotlin_module\")\n\t}\n\n");
 | 
						|
                }
 | 
						|
 | 
						|
                if (line.Contains(ptn2))
 | 
						|
                {
 | 
						|
                    if (lines[i + 1].Contains("}"))
 | 
						|
                    {
 | 
						|
                        lines[i + 1] = lines[i + 1].Replace("}", "\tcheckReleaseBuilds false\n\t}");
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
            Debug.Log($"[MOD] --- Fix file at: {LauncherFullPath}");
 | 
						|
            File.WriteAllLines(LauncherFullPath, lines);
 | 
						|
 | 
						|
        }
 | 
						|
        private static void FixMain()
 | 
						|
        {
 | 
						|
            if (!File.Exists(MainFullPath))
 | 
						|
            {
 | 
						|
                Debug.Log($"[MOD] --- Copy file to: {MainFullPath}");
 | 
						|
                CopyFile($"{MainName}.txt", MainFullPath);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        private static void FixProperties()
 | 
						|
        {
 | 
						|
            if (!File.Exists(PropertiesFullPath))
 | 
						|
            {
 | 
						|
                Debug.Log($"[MOD] --- Copy file to: {PropertiesFullPath}");
 | 
						|
                CopyFile($"{PropertiesName}.txt", PropertiesFullPath);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        private static void CheckTargetSDKVersion()
 | 
						|
        {
 | 
						|
            var ver = (int) PlayerSettings.Android.targetSdkVersion;
 | 
						|
            if (ver < TargetSDKVersion)
 | 
						|
            {
 | 
						|
                Debug.Log($"[MOD] --- Fix target sdk version -> {TargetSDKVersion}");
 | 
						|
                PlayerSettings.Android.targetSdkVersion = (AndroidSdkVersions)TargetSDKVersion;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #region File IO
 | 
						|
 | 
						|
        private static string GetMoveFilePath(string fileName)
 | 
						|
        {
 | 
						|
            var path = GuruEditorHelper.GetFilePath($"{nameof(AndroidProjectMod)}.cs t:Script");
 | 
						|
            var files = Path.GetFullPath($"{path}/../Files");
 | 
						|
            return $"{files}/{fileName}";
 | 
						|
        }
 | 
						|
        private static void CopyFile(string fileName, string toPath)
 | 
						|
        {
 | 
						|
            var from = GetMoveFilePath(fileName);
 | 
						|
            if (!string.IsNullOrEmpty(from))
 | 
						|
            {
 | 
						|
                if (File.Exists(from))
 | 
						|
                {
 | 
						|
                    File.Copy(from, toPath);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
 | 
						|
        
 | 
						|
    }
 | 
						|
} |