Refactoring Code(Last)

This commit is contained in:
backryun
2025-11-05 01:01:37 +09:00
parent e0018909dc
commit bd1dcf6f64
6 changed files with 56 additions and 33 deletions
+18 -18
View File
@@ -108,26 +108,26 @@ namespace APKToolGUI.Utils
switch (line.Split(':')[0])
{
case "package":
PackageName = StringExt.Regex(@"(?<=package: name=\')(.*?)(?=\')", line);
VersionName = StringExt.Regex(@"(?<=versionName=\')(.*?)(?=\')", line);
VersionCode = StringExt.Regex(@"(?<=versionCode=\')(.*?)(?=\')", line);
PackageName = StringExt.RegexExtract(@"(?<=package: name=\')(.*?)(?=\')", line);
VersionName = StringExt.RegexExtract(@"(?<=versionName=\')(.*?)(?=\')", line);
VersionCode = StringExt.RegexExtract(@"(?<=versionCode=\')(.*?)(?=\')", line);
break;
case "uses-permission":
permissionsBuilder.AppendLine(StringExt.Regex(@"(?<=name=\')(.*?)(?=\')", line));
permissionsBuilder.AppendLine(StringExt.RegexExtract(@"(?<=name=\')(.*?)(?=\')", line));
break;
case "sdkVersion":
MinSdkVersionDetailed = SdkToAndroidVer(StringExt.Regex(@"(?<=sdkVersion:\')(.*?)(?=\')", line));
MinSdkVersion = StringExt.Regex(@"(?<=sdkVersion:\')(.*?)(?=\')", line);
MinSdkVersionDetailed = SdkToAndroidVer(StringExt.RegexExtract(@"(?<=sdkVersion:\')(.*?)(?=\')", line));
MinSdkVersion = StringExt.RegexExtract(@"(?<=sdkVersion:\')(.*?)(?=\')", line);
break;
case "targetSdkVersion":
TargetSdkVersionDetailed = SdkToAndroidVer(StringExt.Regex(@"(?<=targetSdkVersion:\')(.*?)(?=\')", line));
TargetSdkVersion = StringExt.Regex(@"(?<=targetSdkVersion:\')(.*?)(?=\')", line);
TargetSdkVersionDetailed = SdkToAndroidVer(StringExt.RegexExtract(@"(?<=targetSdkVersion:\')(.*?)(?=\')", line));
TargetSdkVersion = StringExt.RegexExtract(@"(?<=targetSdkVersion:\')(.*?)(?=\')", line);
break;
case "application-label":
AppName = StringExt.Regex(@"(?<=application-label:\')(.*?)(?=\')", line);
AppName = StringExt.RegexExtract(@"(?<=application-label:\')(.*?)(?=\')", line);
break;
case "launchable-activity":
LaunchableActivity = StringExt.Regex(@"(?<=name=\')(.*?)(?=\')", line);
LaunchableActivity = StringExt.RegexExtract(@"(?<=name=\')(.*?)(?=\')", line);
break;
case "supports-screens":
var screens = Regex.Matches(line.Split(':')[1], @"(?<= \')(.*?)(?=\')").Cast<Match>().Select(m => m.Value).ToList();
@@ -161,13 +161,13 @@ namespace APKToolGUI.Utils
ApkMirrorLink = "https://www.apkmirror.com/?post_type=app_release&searchtype=apk&s=" + PackageName;
ApkGkLink = "https://apkgk.com/" + PackageName + "/download";
AppIcon120 = StringExt.Regex(@"(?<=application-icon-120:\')(.*?)(?=\')", FullInfo);
AppIcon160 = StringExt.Regex(@"(?<=application-icon-160:\')(.*?)(?=\')", FullInfo);
AppIcon240 = StringExt.Regex(@"(?<=application-icon-240:\')(.*?)(?=\')", FullInfo);
AppIcon320 = StringExt.Regex(@"(?<=application-icon-320:\')(.*?)(?=\')", FullInfo);
AppIcon480 = StringExt.Regex(@"(?<=application-icon-480:\')(.*?)(?=\')", FullInfo);
AppIcon640 = StringExt.Regex(@"(?<=application-icon-640:\')(.*?)(?=\')", FullInfo);
AppIcon65534 = StringExt.Regex(@"(?<=application-icon-65534:\')(.*?)(?=\')", FullInfo);
AppIcon120 = StringExt.RegexExtract(@"(?<=application-icon-120:\')(.*?)(?=\')", FullInfo);
AppIcon160 = StringExt.RegexExtract(@"(?<=application-icon-160:\')(.*?)(?=\')", FullInfo);
AppIcon240 = StringExt.RegexExtract(@"(?<=application-icon-240:\')(.*?)(?=\')", FullInfo);
AppIcon320 = StringExt.RegexExtract(@"(?<=application-icon-320:\')(.*?)(?=\')", FullInfo);
AppIcon480 = StringExt.RegexExtract(@"(?<=application-icon-480:\')(.*?)(?=\')", FullInfo);
AppIcon640 = StringExt.RegexExtract(@"(?<=application-icon-640:\')(.*?)(?=\')", FullInfo);
AppIcon65534 = StringExt.RegexExtract(@"(?<=application-icon-65534:\')(.*?)(?=\')", FullInfo);
result = true;
}
@@ -256,7 +256,7 @@ namespace APKToolGUI.Utils
//File.WriteAllText("R:\\t.txt", ps);
string icondl = Path.Combine(cacheDir, "icon.png");
Directory.CreateDirectory(cacheDir);
w.DownloadFile(StringExt.Regex(@"(?<=\""image\"":\"")(.*?)(?=\"",\"")", ps), icondl);
w.DownloadFile(StringExt.RegexExtract(@"(?<=\""image\"":\"")(.*?)(?=\"",\"")", ps), icondl);
iconLocation = icondl;
}
catch (System.Net.WebException ex)
+4 -1
View File
@@ -59,6 +59,8 @@ namespace APKToolGUI
Text += " - v" + ProductVersion;
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
aapt = new AaptParser();
if (!File.Exists(Settings.Default.Decode_InputAppPath))
Settings.Default.Decode_InputAppPath = "";
if (!Directory.Exists(Settings.Default.Build_InputDir))
@@ -203,7 +205,7 @@ namespace APKToolGUI
ToLog(ApktoolEventType.Success, Language.Done);
ToStatus(Language.Done, Resources.done);
}
catch (Exception)
catch (Exception ex)
{
#if DEBUG
ToLog(ApktoolEventType.Warning, Language.ErrorGettingApkInfo + "\n" + ex.ToString());
@@ -419,6 +421,7 @@ namespace APKToolGUI
color = Color.DarkOrange;
else
color = Color.Orange;
color = Color.White;
break;
case ApktoolEventType.Unknown:
if (Program.IsDarkTheme())
+1 -1
View File
@@ -139,7 +139,7 @@ namespace APKToolGUI
if (Language.SystemLanguage.Equals(comboBox1.SelectedItem.ToString()))
Settings.Default.Culture = "Auto";
else
Settings.Default.Culture = StringExt.Regex(@"(?<=\[)(.*?)(?=\])", comboBox1.SelectedItem.ToString());
Settings.Default.Culture = StringExt.RegexExtract(@"(?<=\[)(.*?)(?=\])", comboBox1.SelectedItem.ToString());
Settings.Default.Theme = themeComboBox.SelectedIndex;
Settings.Default.Save();
+5 -3
View File
@@ -10,6 +10,7 @@ using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace APKToolGUI
@@ -165,12 +166,13 @@ namespace APKToolGUI
List<String> missigFiles = MissingFilesCheck();
if (missigFiles.Count > 0)
{
String files = Environment.NewLine;
StringBuilder filesBuilder = new StringBuilder();
filesBuilder.AppendLine();
foreach (String file in missigFiles)
{
files += file + Environment.NewLine;
filesBuilder.AppendLine(file);
}
MessageBox.Show(Language.RequiredFilesMissing + files, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(Language.RequiredFilesMissing + filesBuilder.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
//Application.Exit();
return false;
}
+3 -3
View File
@@ -17,7 +17,7 @@ namespace APKToolGUI.Utils
{
if (mf.Contains("<application"))
{
return StringExt.Regex(@"(?<=android:name=\"")(.*?)(?=\"")", mf);
return StringExt.RegexExtract(@"(?<=android:name=\"")(.*?)(?=\"")", mf);
}
}
return "";
@@ -31,11 +31,11 @@ namespace APKToolGUI.Utils
foreach (string mf in Manifest)
{
if (String.IsNullOrEmpty(packageName))
packageName = StringExt.Regex(@"(?<=package=\"")(.*?)(?=\"")", mf);
packageName = StringExt.RegexExtract(@"(?<=package=\"")(.*?)(?=\"")", mf);
if (mf.Contains("<activity"))
{
mainActivity = StringExt.Regex(@"(?<=android:name=\"")(.*?)(?=\"")", mf);
mainActivity = StringExt.RegexExtract(@"(?<=android:name=\"")(.*?)(?=\"")", mf);
}
if (mf.Contains("android.intent.action.MAIN"))
{
+25 -7
View File
@@ -5,18 +5,36 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace APKToolGUI.Utils
{
public static class StringExt
{
static readonly Random random = new Random();
public static string Regex(string text, string match)
[ThreadStatic]
private static Random threadRandom;
private static Random ThreadRandom
{
Regex myRegex = new Regex(text);
Match matched = myRegex.Match(match);
get
{
if (threadRandom == null)
threadRandom = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId));
return threadRandom;
}
}
/// <summary>
/// Extracts a string from the input using the provided regex pattern.
/// </summary>
/// <param name="pattern">The regex pattern to match.</param>
/// <param name="input">The input string to search.</param>
/// <returns>The matched string or empty string if no match found.</returns>
public static string RegexExtract(string pattern, string input)
{
Regex regex = new Regex(pattern);
Match matched = regex.Match(input);
return matched.ToString();
}
@@ -24,14 +42,14 @@ namespace APKToolGUI.Utils
{
const string chars = "abcdefghijklmnopqrstuvwxyz";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
.Select(s => s[ThreadRandom.Next(s.Length)]).ToArray());
}
public static string RandStrWithCaps(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
.Select(s => s[ThreadRandom.Next(s.Length)]).ToArray());
}
/// <summary>