UTY.cs 12 KB

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