SDK_UnityMoney/Assets/Script/Common/FileParse/FileParse.cs

314 lines
12 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Google.MiniJSON;
using UnityEngine;
namespace WZ
{
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; }
}
}
}