UTY.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using UnityEngine;
  9. public class UTY : MonoBehaviour
  10. {
  11. public static string gameProjectPath
  12. {
  13. get
  14. {
  15. return Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/"));
  16. }
  17. }
  18. public static string gameDataPath
  19. {
  20. get
  21. {
  22. return Path.Combine(UTY.gameProjectPath, "GameData");
  23. }
  24. }
  25. public static string[] GetStringList(string sss)
  26. {
  27. List<string> list = new List<string>();
  28. if (sss == null)
  29. {
  30. sss = string.Empty;
  31. }
  32. int length = sss.Length;
  33. int i = 0;
  34. string text = string.Empty;
  35. while (i < length)
  36. {
  37. while (i < length && (sss[i] == ' ' || sss[i] == '\t' || sss[i] == '\u3000' || sss[i] == '\r'))
  38. {
  39. i++;
  40. }
  41. if (i == length)
  42. {
  43. break;
  44. }
  45. if (i < length && sss[i] == '"')
  46. {
  47. i++;
  48. while (i < length && sss[i] != '"')
  49. {
  50. text += sss[i];
  51. i++;
  52. }
  53. i++;
  54. }
  55. else
  56. {
  57. while (i < length && sss[i] != ' ' && sss[i] != '\t' && sss[i] != '\u3000' && sss[i] != '\r')
  58. {
  59. text += sss[i];
  60. i++;
  61. }
  62. }
  63. list.Add(text);
  64. text = string.Empty;
  65. }
  66. return list.ToArray();
  67. }
  68. public static float DistLineToPointPrim3(Vector3 p0, Vector3 p1, Vector3 c)
  69. {
  70. Vector3 vector = p1 - p0;
  71. if (vector == Vector3.zero)
  72. {
  73. return 0f;
  74. }
  75. Vector3 rhs = c - p0;
  76. float magnitude = vector.magnitude;
  77. float d = Vector3.Dot(vector, rhs) / magnitude;
  78. return (c - (p0 + vector * d)).magnitude;
  79. }
  80. public static Vector3 LineToPointPrim3(Vector3 p0, Vector3 p1, Vector3 c)
  81. {
  82. Vector3 vector = p1 - p0;
  83. if (vector == Vector3.zero)
  84. {
  85. return Vector3.zero;
  86. }
  87. Vector3 rhs = c - p0;
  88. float sqrMagnitude = vector.sqrMagnitude;
  89. float d = Vector3.Dot(vector, rhs) / sqrMagnitude;
  90. return p0 + vector * d;
  91. }
  92. public static string GetStringCom(string sss)
  93. {
  94. int length = sss.Length;
  95. int num = 0;
  96. string text = string.Empty;
  97. while (num < length && (sss[num] == ' ' || sss[num] == '\t' || sss[num] == '\u3000'))
  98. {
  99. num++;
  100. }
  101. if (num < length && sss[num] == '/')
  102. {
  103. return string.Empty;
  104. }
  105. if (num < length && sss[num] == '"')
  106. {
  107. num++;
  108. while (num < length && sss[num] != '"')
  109. {
  110. text += sss[num];
  111. num++;
  112. }
  113. num++;
  114. }
  115. else
  116. {
  117. while (num < length && sss[num] != ' ' && sss[num] != '\t' && sss[num] != '\u3000')
  118. {
  119. text += sss[num];
  120. num++;
  121. }
  122. }
  123. return text.ToLower();
  124. }
  125. public static string[] GetStringListSS(string sss)
  126. {
  127. List<string> list = new List<string>();
  128. int length = sss.Length;
  129. int i = 0;
  130. string text = string.Empty;
  131. while (i < length)
  132. {
  133. while (i < length && (sss[i] == ' ' || sss[i] == '\t' || sss[i] == '\u3000'))
  134. {
  135. i++;
  136. }
  137. if (i == length)
  138. {
  139. break;
  140. }
  141. if (i < length && sss[i] == '"')
  142. {
  143. i++;
  144. while (i < length && sss[i] != '"')
  145. {
  146. text += sss[i];
  147. i++;
  148. }
  149. i++;
  150. }
  151. else if (i < length && sss[i] == '『')
  152. {
  153. i++;
  154. while (i < length && sss[i] != '』')
  155. {
  156. text += sss[i];
  157. i++;
  158. }
  159. i++;
  160. }
  161. else
  162. {
  163. while (i < length && sss[i] != ' ' && sss[i] != '\t' && sss[i] != '\u3000')
  164. {
  165. text += sss[i];
  166. i++;
  167. }
  168. }
  169. list.Add(text);
  170. text = string.Empty;
  171. }
  172. return list.ToArray();
  173. }
  174. public static string[] GetStringListSS_2(string sss)
  175. {
  176. List<string> list = new List<string>();
  177. int length = sss.Length;
  178. int i = 0;
  179. string text = string.Empty;
  180. while (i < length)
  181. {
  182. while (i < length && (sss[i] == ' ' || sss[i] == '\t' || sss[i] == '\u3000'))
  183. {
  184. i++;
  185. }
  186. if (i == length)
  187. {
  188. break;
  189. }
  190. if (i < length && sss[i] == '"')
  191. {
  192. i++;
  193. text += "\"";
  194. while (i < length && sss[i] != '"')
  195. {
  196. text += sss[i];
  197. i++;
  198. }
  199. i++;
  200. text += "\"";
  201. }
  202. else if (i < length && sss[i] == '『')
  203. {
  204. i++;
  205. text += "『";
  206. while (i < length && sss[i] != '』')
  207. {
  208. text += sss[i];
  209. i++;
  210. }
  211. i++;
  212. text += "』";
  213. }
  214. else
  215. {
  216. while (i < length && sss[i] != ' ' && sss[i] != '\t' && sss[i] != '\u3000')
  217. {
  218. text += sss[i];
  219. i++;
  220. }
  221. }
  222. list.Add(text);
  223. text = string.Empty;
  224. }
  225. return list.ToArray();
  226. }
  227. public static int Clamp(int val, int min, int max)
  228. {
  229. if (val > max)
  230. {
  231. return max;
  232. }
  233. if (val < min)
  234. {
  235. return min;
  236. }
  237. return val;
  238. }
  239. public static int GetRnd(int n)
  240. {
  241. if (UTY.rnd == null)
  242. {
  243. UTY.rnd = new System.Random();
  244. }
  245. return UTY.rnd.Next(n);
  246. }
  247. public static float COSS2(float v, float pw = 4f)
  248. {
  249. if (v < 0.5f)
  250. {
  251. v *= 2f;
  252. float num = Mathf.Pow(v, pw);
  253. return num * 0.5f;
  254. }
  255. float num2 = (1f - v) * 2f;
  256. num2 = 1f - Mathf.Pow(num2, pw);
  257. return num2 * 0.5f + 0.5f;
  258. }
  259. public static float COSS(float v)
  260. {
  261. return Mathf.Cos(3.1415927f * (1f - v)) * 0.5f + 0.5f;
  262. }
  263. private bool Texture2Image(RenderTexture f_rtSrc, string f_strSaveFileName)
  264. {
  265. return true;
  266. }
  267. public static string FileNameEscape(string f_strSrc)
  268. {
  269. return Regex.Replace(f_strSrc, UTY.m_strEscapePattern, "_");
  270. }
  271. public static void InitDll()
  272. {
  273. }
  274. public static void FreeDll()
  275. {
  276. }
  277. public static GameObject GetChildObject(GameObject f_goParent, string f_strObjName, bool f_bNoError = false)
  278. {
  279. Transform transform = f_goParent.transform.Find(f_strObjName);
  280. if (f_bNoError)
  281. {
  282. if (transform == null)
  283. {
  284. return null;
  285. }
  286. }
  287. else if (transform == null)
  288. {
  289. NDebug.Assert(f_goParent.name + " の子から " + f_strObjName + " が見つかりませんでした。相対パス指定が必要です。", false);
  290. }
  291. return transform.gameObject;
  292. }
  293. public static GameObject GetChildObjectNoError(GameObject f_goParent, string f_strObjName, bool f_bNoError = false)
  294. {
  295. Transform transform = f_goParent.transform.Find(f_strObjName);
  296. if (f_bNoError)
  297. {
  298. if (transform == null)
  299. {
  300. return null;
  301. }
  302. }
  303. else if (transform == null)
  304. {
  305. return null;
  306. }
  307. return transform.gameObject;
  308. }
  309. public static string GetObjectTreePath(GameObject obj)
  310. {
  311. string text = string.Empty;
  312. if (obj == null)
  313. {
  314. return text;
  315. }
  316. text += obj.name;
  317. while (obj.transform.parent != null)
  318. {
  319. obj = obj.transform.parent.gameObject;
  320. text = obj.name + "/" + text;
  321. }
  322. return text;
  323. }
  324. public static byte[] GetPixelArray(Texture2D f_texSrc)
  325. {
  326. return ColorPalette.GetPixelArrayNativeFromRGBA8(f_texSrc);
  327. }
  328. public static void ConvertColor(MaidParts.PartsColor f_cColor, byte[] f_bySrc, ref Texture2D f_texDest)
  329. {
  330. UTY.m_colStatus.base_color.hue = f_cColor.m_nMainHue;
  331. UTY.m_colStatus.base_color.sat = f_cColor.m_nMainChroma;
  332. UTY.m_colStatus.base_color.brightness = f_cColor.m_nMainBrightness;
  333. UTY.m_colStatus.base_color.contrast = f_cColor.m_nMainContrast;
  334. UTY.m_colStatus.shadow_color.hue = f_cColor.m_nShadowHue;
  335. UTY.m_colStatus.shadow_color.sat = f_cColor.m_nShadowChroma;
  336. UTY.m_colStatus.shadow_color.brightness = f_cColor.m_nShadowBrightness;
  337. UTY.m_colStatus.shadow_color.contrast = f_cColor.m_nShadowContrast;
  338. UTY.m_colStatus.shadow_threshold = f_cColor.m_nShadowRate;
  339. ColorPalette.ConvertColor(UTY.m_colStatus, f_bySrc, ref f_texDest);
  340. }
  341. public static void ConvertColorHue(int f_nHue, byte[] f_bySrc, ref Texture2D f_texDest)
  342. {
  343. UTY.m_colStatus.base_color.hue = f_nHue;
  344. UTY.m_colStatus.base_color.sat = 128;
  345. UTY.m_colStatus.base_color.brightness = 255;
  346. UTY.m_colStatus.base_color.contrast = 0;
  347. UTY.m_colStatus.shadow_color.hue = 0;
  348. UTY.m_colStatus.shadow_color.sat = 0;
  349. UTY.m_colStatus.shadow_color.brightness = 0;
  350. UTY.m_colStatus.shadow_color.contrast = 0;
  351. UTY.m_colStatus.shadow_threshold = 0;
  352. ColorPalette.ConvertColor(UTY.m_colStatus, f_bySrc, ref f_texDest);
  353. }
  354. public static void ConvertColor(ColorPalette.Status f_cColor, byte[] f_bySrc, ref Texture2D f_texDest)
  355. {
  356. ColorPalette.ConvertColor(f_cColor, f_bySrc, ref f_texDest);
  357. }
  358. public static void UpdateColorTableTexture(MaidParts.PartsColor f_cColor, ref Texture2D f_texDest)
  359. {
  360. UTY.m_colStatus.base_color.hue = f_cColor.m_nMainHue;
  361. UTY.m_colStatus.base_color.sat = f_cColor.m_nMainChroma;
  362. UTY.m_colStatus.base_color.brightness = f_cColor.m_nMainBrightness;
  363. UTY.m_colStatus.base_color.contrast = f_cColor.m_nMainContrast;
  364. UTY.m_colStatus.shadow_color.hue = f_cColor.m_nShadowHue;
  365. UTY.m_colStatus.shadow_color.sat = f_cColor.m_nShadowChroma;
  366. UTY.m_colStatus.shadow_color.brightness = f_cColor.m_nShadowBrightness;
  367. UTY.m_colStatus.shadow_color.contrast = f_cColor.m_nShadowContrast;
  368. UTY.m_colStatus.shadow_threshold = f_cColor.m_nShadowRate;
  369. ColorPalette.WriteColorTableTexture(UTY.m_colStatus, ref f_texDest);
  370. }
  371. public static void SaveImage(Texture f_texSrc, string f_strFileName = null)
  372. {
  373. if (f_texSrc is Texture2D)
  374. {
  375. UTY.SaveImage(f_texSrc as Texture2D, f_strFileName);
  376. }
  377. else if (f_texSrc is RenderTexture)
  378. {
  379. UTY.SaveImage(f_texSrc as RenderTexture, f_strFileName, TextureFormat.ARGB32);
  380. }
  381. }
  382. public static void SaveImage(RenderTexture f_rtSrc, string f_strFileName = null, TextureFormat f_TexFormat = TextureFormat.ARGB32)
  383. {
  384. Texture2D texture2D = new Texture2D(f_rtSrc.width, f_rtSrc.height, f_TexFormat, false);
  385. RenderTexture active = RenderTexture.active;
  386. RenderTexture.active = f_rtSrc;
  387. texture2D.ReadPixels(new Rect(0f, 0f, (float)f_rtSrc.width, (float)f_rtSrc.height), 0, 0);
  388. RenderTexture.active = active;
  389. UTY.SaveImage(texture2D, f_strFileName);
  390. }
  391. public static string SaveImage(Texture2D f_texSrc, string f_strFileName = null)
  392. {
  393. string text = f_strFileName;
  394. if (text == null || text == string.Empty)
  395. {
  396. text = "img" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + ".png";
  397. }
  398. byte[] bytes = f_texSrc.EncodeToPNG();
  399. File.WriteAllBytes(text, bytes);
  400. return text;
  401. }
  402. public static byte[] LoadImage(string f_strFileName)
  403. {
  404. FileStream fileStream = new FileStream(f_strFileName, FileMode.Open);
  405. if (fileStream == null)
  406. {
  407. NDebug.Assert("イメージファイルがみつかりません。" + f_strFileName, false);
  408. }
  409. byte[] array = new byte[fileStream.Length];
  410. fileStream.Read(array, 0, (int)fileStream.Length);
  411. fileStream.Close();
  412. fileStream.Dispose();
  413. return array;
  414. }
  415. public static Texture2D LoadTexture(string f_strFileName)
  416. {
  417. Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  418. texture2D.LoadImage(UTY.LoadImage(f_strFileName));
  419. return texture2D;
  420. }
  421. public static void ClearRT(RenderTexture target, Color col)
  422. {
  423. RenderTexture active = RenderTexture.active;
  424. RenderTexture.active = target;
  425. GL.Clear(true, true, col);
  426. RenderTexture.active = active;
  427. }
  428. public static bool IsLowercaseAlphanumeric(string target)
  429. {
  430. return new Regex("^[!-~]*$()").IsMatch(target);
  431. }
  432. public static T DeepCopy<T>(T src)
  433. {
  434. BinaryFormatter binaryFormatter = new BinaryFormatter();
  435. MemoryStream memoryStream = new MemoryStream();
  436. T result;
  437. try
  438. {
  439. binaryFormatter.Serialize(memoryStream, src);
  440. memoryStream.Position = 0L;
  441. result = (T)((object)binaryFormatter.Deserialize(memoryStream));
  442. }
  443. finally
  444. {
  445. memoryStream.Close();
  446. }
  447. return result;
  448. }
  449. public static string GetExePath()
  450. {
  451. string dataPath = Application.dataPath;
  452. int num = 0;
  453. for (int i = dataPath.Length - 1; i >= 0; i--)
  454. {
  455. if (dataPath[i] == '/')
  456. {
  457. num = i + 1;
  458. break;
  459. }
  460. }
  461. string str = dataPath.Substring(num, dataPath.Length - num).ToLower().Replace("_data", ".exe");
  462. string str2 = dataPath.Substring(0, num);
  463. return str2 + str;
  464. }
  465. public static void DirectorySafeDelete(string path, string[] reqExt, SearchOption opt)
  466. {
  467. if (!Directory.Exists(path))
  468. {
  469. Debug.LogWarning("del directory does not exist. -> " + path);
  470. return;
  471. }
  472. List<string> list = new List<string>();
  473. foreach (string str in reqExt)
  474. {
  475. string[] files = Directory.GetFiles(path, "*." + str, opt);
  476. list.AddRange(files);
  477. }
  478. foreach (string path2 in list)
  479. {
  480. File.Delete(path2);
  481. }
  482. string[] fileSystemEntries = Directory.GetFileSystemEntries(path);
  483. if (fileSystemEntries == null || fileSystemEntries.Length == 0)
  484. {
  485. Directory.Delete(path);
  486. return;
  487. }
  488. Debug.LogWarning("del directory does not empty. -> " + path);
  489. }
  490. private static System.Random rnd;
  491. private static string m_strEscapePattern = "(\\\\|/|:|\\*|\\?|\"|\\<|\\>|\\||\\.|\\,)";
  492. private static ColorPalette.Status m_colStatus = new ColorPalette.Status();
  493. private class Crypt
  494. {
  495. public static string Encrypt(string text)
  496. {
  497. ICryptoTransform transform = new RijndaelManaged
  498. {
  499. BlockSize = 128,
  500. KeySize = 128,
  501. Padding = PaddingMode.Zeros,
  502. Mode = CipherMode.CBC,
  503. Key = Encoding.UTF8.GetBytes("feaeaeantydhgs"),
  504. IV = Encoding.UTF8.GetBytes("ujuuiaefharsbs")
  505. }.CreateEncryptor();
  506. MemoryStream memoryStream = new MemoryStream();
  507. CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
  508. byte[] bytes = Encoding.UTF8.GetBytes(text);
  509. cryptoStream.Write(bytes, 0, bytes.Length);
  510. cryptoStream.FlushFinalBlock();
  511. byte[] inArray = memoryStream.ToArray();
  512. return Convert.ToBase64String(inArray);
  513. }
  514. public static string Decrypt(string cryptText)
  515. {
  516. ICryptoTransform transform = new RijndaelManaged
  517. {
  518. BlockSize = 128,
  519. KeySize = 128,
  520. Padding = PaddingMode.Zeros,
  521. Mode = CipherMode.CBC,
  522. Key = Encoding.UTF8.GetBytes("feaeaeantydhgs"),
  523. IV = Encoding.UTF8.GetBytes("ujuuiaefharsbs")
  524. }.CreateDecryptor();
  525. byte[] array = Convert.FromBase64String(cryptText);
  526. byte[] array2 = new byte[array.Length];
  527. MemoryStream stream = new MemoryStream(array);
  528. CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read);
  529. cryptoStream.Read(array2, 0, array2.Length);
  530. return Encoding.UTF8.GetString(array2);
  531. }
  532. private const string AesIV = "ujuuiaefharsbs";
  533. private const string AesKey = "feaeaeantydhgs";
  534. }
  535. }