解析配置文件

This commit is contained in:
juncong lee 2025-09-01 12:14:42 +08:00
parent b46582b837
commit 51d6bbfb04
15 changed files with 593 additions and 2 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5d5d8d4959271412c9836c232fa3acfa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,314 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Google.MiniJSON;
using Script.Utils;
using UnityEngine;
namespace Script.Common.FileParse
{
public static class FileParse
{
static string resourcePath = Application.streamingAssetsPath +"/";
public static void Parse()
{
string bundleId = GetBundleId();
LoggerUtils.Debug("[FileParse] get bundleId: " + bundleId);
ParseConfig(bundleId);
ParseSdkConfig(bundleId);
}
// 获取key-value配置信息
public static string GetConfigByKey(string _key)
{
PlayerPrefPair[] configs = new PlayerPrefPair[0];
configs = ParseConfig(GetBundleId());
for (int i = 0; i < configs.Length; i++)
{
if (configs[i].Key == _key)
{
return configs[i].Value.ToString();
}
}
return "";
}
private static PlayerPrefPair[] ParseConfig(string bundleId)
{
string fileName = GetMD5Hash("Config" + bundleId);
PlayerPrefPair[] configs = GetConfig(fileName, false);
ParseConfigsInner(configs);
return configs;
}
private static PlayerPrefPair[] ParseSdkConfig(string bundleId)
{
string fileName = GetMD5Hash("SdkConfig" + bundleId);
PlayerPrefPair[] configs = GetConfig(fileName, true);
ParseConfigsInner(configs);
return configs;
}
private static string GetMD5Hash(string input)
{
using (var md5 = MD5.Create())
{
var inputBytes = Encoding.ASCII.GetBytes(input);
var hashBytes = md5.ComputeHash(inputBytes);
var builder = new StringBuilder();
foreach (var t in hashBytes)
{
builder.Append(t.ToString("x2"));
}
return builder.ToString();
}
}
private static PlayerPrefPair[] GetConfig(string _configFile, bool _sdkConfigFile)
{
return GetAllFromProperties(resourcePath + _configFile, _sdkConfigFile);
}
private static PlayerPrefPair[] GetAllFromProperties(string _configPath, bool _sdkConfigFile)
{
#if !UNITY_EDITOR && UNITY_ANDROID
WWW reader = new WWW(_configPath);
while (!reader.isDone) { }
string m_content = reader.text;
#else
string m_content = System.IO.File.ReadAllText(_configPath);
#endif
string m_ecryptFileContent = DecryptFileBody(m_content, _sdkConfigFile);
Hashtable ht = LoadHashtable(m_ecryptFileContent);
if (ht.Count > 0)
{
PlayerPrefPair[] tempPlayerPrefs = new PlayerPrefPair[ht.Count];
int i = 0;
foreach (string key in ht.Keys)
{
tempPlayerPrefs[i] = new PlayerPrefPair() { Key = key, Value = ht[key] };
i++;
}
return tempPlayerPrefs;
}
else
{
return new PlayerPrefPair[0];
}
}
private static void ParseConfigsInner(PlayerPrefPair[] _configs)
{
if (_configs.Length <= 0) return;
for (int i = 0; i < _configs.Length; i++)
{
string valueTemp = _configs[i].Value.ToString();
LoggerUtils.Debug("[FileParse] ParseConfigsInner key" + _configs[i].Key + "value:" + valueTemp);
if (_configs[i].Key.ToLower() == KEY_Admob_CollapsibleBannerId.ToLower())
{
StaticValue.AdmobCollapsibleBannerId = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Admob_NormalBannerId.ToLower())
{
StaticValue.AdmobNormalBannerId = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Admob_SplashId.ToLower())
{
StaticValue.AdmobSplashId = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Admob_NativeId.ToLower())
{
StaticValue.AdmobNativeId = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Admob_RewardId.ToLower())
{
StaticValue.AdmobRewardId = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Admob_InterId.ToLower())
{
StaticValue.AdmobInterId = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_PRIVACY_URL.ToLower())
{
StaticValue.PrivacyUrl = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_ADJUST_ID.ToLower())
{
StaticValue.AdjustToken = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Max_APPKEY.ToLower())
{
StaticValue.ApplovinKey = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_THINKDATA_ID.ToLower())
{
StaticValue.TDAppID = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Max_Inter.ToLower())
{
StaticValue.InterAdUnitID = valueTemp;
}else if (_configs[i].Key.ToLower() == KEY_Max_Reward.ToLower())
{
StaticValue.RewardAdUnitID = valueTemp;
}
else if (_configs[i].Key.ToLower() == KEY_TopOn_AppId.ToLower())
{
string[] condition = { "@" };
string[] parts = valueTemp.Split(condition, StringSplitOptions.None);
if (parts.Length == 2)
{
StaticValue.TopOnAppID = parts[0];
StaticValue.TopOnAppKey = parts[1];
}
}
else if (_configs[i].Key.ToLower() == KEY_TopOn_Inter.ToLower())
{
StaticValue.TopOnInterAdUnitID = valueTemp;
}
else if (_configs[i].Key.ToLower() == KEY_TopOn_Reward.ToLower())
{
StaticValue.TopOnRewardAdUnitID = valueTemp;
}
}
}
private static Hashtable LoadHashtable(string file)
{
Hashtable ht = new Hashtable(16);
string content = null;
try
{
content = file;
}
catch (Exception e)
{
return null;
}
string[] rows = content.Split('\n');
string[] kv = null;
foreach (string c in rows)
{
if (c.Trim().Length == 0)
continue;
kv = c.Split('=');
if (kv.Length == 1)
{
ht[kv[0].Trim()] = "";
}
else if (kv.Length == 2)
{
ht[kv[0].Trim()] = kv[1].Trim();
}
}
return ht;
}
private static string DecryptFileBody(string fileBody, bool sdkConfigFile)
{
string padded = fileBody.PadRight(fileBody.Length + (4 - fileBody.Length % 4) % 4, '=');
var decryptResponseBodyBytes = XXTEA.Decrypt(System.Convert.FromBase64String(padded), Encoding.UTF8.GetBytes(GetMD5Hash((sdkConfigFile ? "SdkConfigKey#$!" : "ConfigKey#$!") + GetBundleId())));
var decryptResponseBody = Encoding.UTF8.GetString(decryptResponseBodyBytes);
return decryptResponseBody;
}
private static string GetBundleId()
{
#if UNITY_EDITOR
string packageName = "";
var files = Directory.GetFiles(Application.dataPath, "*.json", SearchOption.AllDirectories);
var googleFilePath = "";
foreach (var item in files)
{
if (item.Contains("google-services.json"))
{
googleFilePath = item;
}
}
if (!File.Exists(googleFilePath))
{
// 显示错误提示框
throw new Exception("google-services.json not exists.");
}
var googleServicesDictionary = (Dictionary<string, object>)Json.Deserialize(File.ReadAllText(googleFilePath));
googleServicesDictionary.TryGetValue("client", out var tempClientList);
if (!(tempClientList is List<object> clientList) || clientList == null || clientList.Count == 0)
{
// 显示错误提示框
throw new Exception("Illegal google-services.json");
}
foreach (var tempClientItem in clientList)
{
if (!(tempClientItem is Dictionary<string, object> clientItem))
{
continue;
}
clientItem.TryGetValue("client_info", out var tempClientInfo);
if (!(tempClientInfo is Dictionary<string, object> clientInfo) || clientInfo == null || clientInfo.Count == 0)
{
// 显示错误提示框
throw new Exception("Illegal google-services.json");
}
clientInfo.TryGetValue("android_client_info", out var tempAndroidClientInfo);
if (!(tempAndroidClientInfo is Dictionary<string, object> androidClientInfo) || androidClientInfo == null || androidClientInfo.Count == 0)
{
// 显示错误提示框
throw new Exception("Illegal google-services.json");
}
androidClientInfo.TryGetValue("package_name", out var googlePackageName);
packageName = (string)googlePackageName;
if (!string.IsNullOrEmpty(packageName))
{
break;
}
}
return packageName;
#else
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");
string packageName = currentActivity.Call<string>("getPackageName");
return packageName;
#endif
}
public const string KEY_Admob_CollapsibleBannerId = "Admob_Banner_ID1";
public const string KEY_Admob_NormalBannerId = "Admob_Banner_ID2";
public const string KEY_Admob_SplashId = "Admob_APPOPEN_ID";
public const string KEY_Admob_NativeId = "Admob_NATIVE_ID";
public const string KEY_Admob_RewardId = "Admob_RV_ID";
public const string KEY_Admob_InterId = "Admob_IV_ID";
public const string KEY_PRIVACY_URL = "tka_url_privacy";
public const string KEY_THINKDATA_ID = "ss_app_id";
public const string KEY_ADJUST_ID = "adjust_id";
public const string KEY_TopOn_AppId = "ad_appid";
public const string KEY_TopOn_Reward = "AD_Reward";
public const string KEY_TopOn_Inter = "AD_Inter";
public const string KEY_Admob_AppID = "admob_id";
public const string KEY_Max_APPKEY = "ad_appkey";
public const string KEY_Max_Inter = "ad_interstital_id";
public const string KEY_Max_Reward = "ad_reward_id";
public struct PlayerPrefPair
{
public string Key { get; set; }
public object Value { get; set; }
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bd74719ba6cd94424a20539116ebabff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 96f1e9850f3d444ab85bcb5ed5ec8f00
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,193 @@
/**********************************************************\
| |
| XXTEA.cs |
| |
| XXTEA encryption algorithm library for .NET. |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Mar 10, 2015 |
| |
\**********************************************************/
using System;
using System.Text;
public sealed class XXTEA {
private static readonly UTF8Encoding utf8 = new UTF8Encoding();
private const UInt32 delta = 0x9E3779B9;
private static UInt32 MX(UInt32 sum, UInt32 y, UInt32 z, Int32 p, UInt32 e, UInt32[] k) {
return (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
}
private XXTEA() {
}
public static Byte[] Encrypt(Byte[] data, Byte[] key) {
if (data.Length == 0) {
return data;
}
return ToByteArray(Encrypt(ToUInt32Array(data, true), ToUInt32Array(FixKey(key), false)), false);
}
public static Byte[] Encrypt(String data, Byte[] key) {
return Encrypt(utf8.GetBytes(data), key);
}
public static Byte[] Encrypt(Byte[] data, String key) {
return Encrypt(data, utf8.GetBytes(key));
}
public static Byte[] Encrypt(String data, String key) {
return Encrypt(utf8.GetBytes(data), utf8.GetBytes(key));
}
public static String EncryptToBase64String(Byte[] data, Byte[] key) {
return Convert.ToBase64String(Encrypt(data, key));
}
public static String EncryptToBase64String(String data, Byte[] key) {
return Convert.ToBase64String(Encrypt(data, key));
}
public static String EncryptToBase64String(Byte[] data, String key) {
return Convert.ToBase64String(Encrypt(data, key));
}
public static String EncryptToBase64String(String data, String key) {
return Convert.ToBase64String(Encrypt(data, key));
}
public static Byte[] Decrypt(Byte[] data, Byte[] key) {
if (data.Length == 0) {
return data;
}
return ToByteArray(Decrypt(ToUInt32Array(data, false), ToUInt32Array(FixKey(key), false)), true);
}
public static Byte[] Decrypt(Byte[] data, String key) {
return Decrypt(data, utf8.GetBytes(key));
}
public static Byte[] DecryptBase64String(String data, Byte[] key) {
return Decrypt(Convert.FromBase64String(data), key);
}
public static Byte[] DecryptBase64String(String data, String key) {
return Decrypt(Convert.FromBase64String(data), key);
}
public static String DecryptToString(Byte[] data, Byte[] key) {
return utf8.GetString(Decrypt(data, key));
}
public static String DecryptToString(Byte[] data, String key) {
return utf8.GetString(Decrypt(data, key));
}
public static String DecryptBase64StringToString(String data, Byte[] key) {
return utf8.GetString(DecryptBase64String(data, key));
}
public static String DecryptBase64StringToString(String data, String key) {
return utf8.GetString(DecryptBase64String(data, key));
}
private static UInt32[] Encrypt(UInt32[] v, UInt32[] k) {
Int32 n = v.Length - 1;
if (n < 1) {
return v;
}
UInt32 z = v[n], y, sum = 0, e;
Int32 p, q = 6 + 52 / (n + 1);
unchecked {
while (0 < q--) {
sum += delta;
e = sum >> 2 & 3;
for (p = 0; p < n; p++) {
y = v[p + 1];
z = v[p] += MX(sum, y, z, p, e, k);
}
y = v[0];
z = v[n] += MX(sum, y, z, p, e, k);
}
}
return v;
}
private static UInt32[] Decrypt(UInt32[] v, UInt32[] k) {
Int32 n = v.Length - 1;
if (n < 1) {
return v;
}
UInt32 z, y = v[0], sum, e;
Int32 p, q = 6 + 52 / (n + 1);
unchecked {
sum = (UInt32)(q * delta);
while (sum != 0) {
e = sum >> 2 & 3;
for (p = n; p > 0; p--) {
z = v[p - 1];
y = v[p] -= MX(sum, y, z, p, e, k);
}
z = v[n];
y = v[0] -= MX(sum, y, z, p, e, k);
sum -= delta;
}
}
return v;
}
private static Byte[] FixKey(Byte[] key) {
if (key.Length == 16) return key;
Byte[] fixedkey = new Byte[16];
if (key.Length < 16) {
key.CopyTo(fixedkey, 0);
}
else {
Array.Copy(key, 0, fixedkey, 0, 16);
}
return fixedkey;
}
private static UInt32[] ToUInt32Array(Byte[] data, Boolean includeLength) {
Int32 length = data.Length;
Int32 n = (((length & 3) == 0) ? (length >> 2) : ((length >> 2) + 1));
UInt32[] result;
if (includeLength) {
result = new UInt32[n + 1];
result[n] = (UInt32)length;
}
else {
result = new UInt32[n];
}
for (Int32 i = 0; i < length; i++) {
result[i >> 2] |= (UInt32)data[i] << ((i & 3) << 3);
}
return result;
}
private static Byte[] ToByteArray(UInt32[] data, Boolean includeLength) {
Int32 n = data.Length << 2;
if (includeLength) {
Int32 m = (Int32)data[data.Length - 1];
n -= 4;
if ((m < n - 3) || (m > n)) {
return null;
}
n = m;
}
Byte[] result = new Byte[n];
for (Int32 i = 0; i < n; i++) {
result[i] = (Byte)(data[i >> 2] >> ((i & 3) << 3));
}
return result;
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1db01a1175e3f451bbf15da2925e759a

View File

@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Script.Common
{
public static class StaticValue
{
public static string AdmobCollapsibleBannerId = "";
public static string AdmobNormalBannerId = "";
public static string AdmobSplashId = "";
public static string AdmobNativeId = "";
public static string AdmobRewardId = "";
public static string AdmobInterId = "";
public static string PrivacyUrl = "";
public static string TopOnAppID = "";
public static string TopOnAppKey = "";
public static string TopOnRewardAdUnitID = "";
public static string TopOnInterAdUnitID = "";
public static string ApplovinKey = "";
public static string AdjustToken = "";
public static string TDAppID = "";
public static string TDServerURL = "";
// max
public static string InterAdUnitID = "";
public static string RewardAdUnitID = "";
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 34aa20abb977944f29d68bb385e84b01

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c6f50e7adcea1478db7f7c75a59a0186
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c995cf1c9b1ce43f08dc884ec8430308
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1d355797e637f624b9148b20da4b2a10
guid: b218769dd2f8e41f3a8821896e5a13ef
TextScriptImporter:
externalObjects: {}
userData:

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 7b27930e2fb6bc84e96b66bdceb0c41e
guid: 5e91905a8b73e41d2adb698f4793e279
TextScriptImporter:
externalObjects: {}
userData: