UTY.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.14159274f * (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(RenderTexture f_rtSrc, string f_strFileName = null, TextureFormat f_TexFormat = TextureFormat.ARGB32)
  372. {
  373. Texture2D texture2D = new Texture2D(f_rtSrc.width, f_rtSrc.height, f_TexFormat, false);
  374. RenderTexture active = RenderTexture.active;
  375. RenderTexture.active = f_rtSrc;
  376. texture2D.ReadPixels(new Rect(0f, 0f, (float)f_rtSrc.width, (float)f_rtSrc.height), 0, 0);
  377. RenderTexture.active = active;
  378. UTY.SaveImage(texture2D, f_strFileName);
  379. }
  380. public static string SaveImage(Texture2D f_texSrc, string f_strFileName = null)
  381. {
  382. string text = f_strFileName;
  383. if (text == null || text == string.Empty)
  384. {
  385. text = "img" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + ".png";
  386. }
  387. byte[] bytes = f_texSrc.EncodeToPNG();
  388. File.WriteAllBytes(text, bytes);
  389. return text;
  390. }
  391. public static byte[] LoadImage(string f_strFileName)
  392. {
  393. FileStream fileStream = new FileStream(f_strFileName, FileMode.Open);
  394. if (fileStream == null)
  395. {
  396. NDebug.Assert("イメージファイルがみつかりません。" + f_strFileName, false);
  397. }
  398. byte[] array = new byte[fileStream.Length];
  399. fileStream.Read(array, 0, (int)fileStream.Length);
  400. fileStream.Close();
  401. fileStream.Dispose();
  402. return array;
  403. }
  404. public static Texture2D LoadTexture(string f_strFileName)
  405. {
  406. Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  407. texture2D.LoadImage(UTY.LoadImage(f_strFileName));
  408. return texture2D;
  409. }
  410. public static void ClearRT(RenderTexture target, Color col)
  411. {
  412. RenderTexture active = RenderTexture.active;
  413. RenderTexture.active = target;
  414. GL.Clear(true, true, col);
  415. RenderTexture.active = active;
  416. }
  417. public static bool IsLowercaseAlphanumeric(string target)
  418. {
  419. return new Regex("^[!-~]*$()").IsMatch(target);
  420. }
  421. public static T DeepCopy<T>(T src)
  422. {
  423. BinaryFormatter binaryFormatter = new BinaryFormatter();
  424. MemoryStream memoryStream = new MemoryStream();
  425. T result;
  426. try
  427. {
  428. binaryFormatter.Serialize(memoryStream, src);
  429. memoryStream.Position = 0L;
  430. result = (T)((object)binaryFormatter.Deserialize(memoryStream));
  431. }
  432. finally
  433. {
  434. memoryStream.Close();
  435. }
  436. return result;
  437. }
  438. public static string GetExePath()
  439. {
  440. string dataPath = Application.dataPath;
  441. int num = 0;
  442. for (int i = dataPath.Length - 1; i >= 0; i--)
  443. {
  444. if (dataPath[i] == '/')
  445. {
  446. num = i + 1;
  447. break;
  448. }
  449. }
  450. string str = dataPath.Substring(num, dataPath.Length - num).ToLower().Replace("_data", ".exe");
  451. string str2 = dataPath.Substring(0, num);
  452. return str2 + str;
  453. }
  454. private static System.Random rnd;
  455. private static string m_strEscapePattern = "(\\\\|/|:|\\*|\\?|\"|\\<|\\>|\\||\\.|\\,)";
  456. private static ColorPalette.Status m_colStatus = new ColorPalette.Status();
  457. private class Crypt
  458. {
  459. public static string Encrypt(string text)
  460. {
  461. ICryptoTransform transform = new RijndaelManaged
  462. {
  463. BlockSize = 128,
  464. KeySize = 128,
  465. Padding = PaddingMode.Zeros,
  466. Mode = CipherMode.CBC,
  467. Key = Encoding.UTF8.GetBytes("feaeaeantydhgs"),
  468. IV = Encoding.UTF8.GetBytes("ujuuiaefharsbs")
  469. }.CreateEncryptor();
  470. MemoryStream memoryStream = new MemoryStream();
  471. CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
  472. byte[] bytes = Encoding.UTF8.GetBytes(text);
  473. cryptoStream.Write(bytes, 0, bytes.Length);
  474. cryptoStream.FlushFinalBlock();
  475. byte[] inArray = memoryStream.ToArray();
  476. return Convert.ToBase64String(inArray);
  477. }
  478. public static string Decrypt(string cryptText)
  479. {
  480. ICryptoTransform transform = new RijndaelManaged
  481. {
  482. BlockSize = 128,
  483. KeySize = 128,
  484. Padding = PaddingMode.Zeros,
  485. Mode = CipherMode.CBC,
  486. Key = Encoding.UTF8.GetBytes("feaeaeantydhgs"),
  487. IV = Encoding.UTF8.GetBytes("ujuuiaefharsbs")
  488. }.CreateDecryptor();
  489. byte[] array = Convert.FromBase64String(cryptText);
  490. byte[] array2 = new byte[array.Length];
  491. MemoryStream stream = new MemoryStream(array);
  492. CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read);
  493. cryptoStream.Read(array2, 0, array2.Length);
  494. return Encoding.UTF8.GetString(array2);
  495. }
  496. private const string AesIV = "ujuuiaefharsbs";
  497. private const string AesKey = "feaeaeantydhgs";
  498. }
  499. }