CsvLoader_CardEntity.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using Unity.EditorCoroutines.Editor;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using System.Linq;
  10. using System;
  11. [CustomEditor(typeof(LoadCSV_CardEntity))]
  12. public class CsvLoader_CardEntity : Editor
  13. {
  14. public override void OnInspectorGUI()
  15. {
  16. var LoadCSV = target as LoadCSV_CardEntity;
  17. DrawDefaultInspector();
  18. if (GUILayout.Button("Create Scriptable Objects"))
  19. {
  20. EditorCoroutineUtility.StartCoroutine(SetCsvDataToScriptableObject(LoadCSV), this);
  21. }
  22. }
  23. IEnumerator SetCsvDataToScriptableObject(LoadCSV_CardEntity loadCSV)
  24. {
  25. List<TextAsset> CardListTexts = GetAsset.LoadAll<TextAsset>("Assets/TextAsset");
  26. int value = 0;
  27. // Check if CSV file is null
  28. if (loadCSV.csvFile == null)
  29. {
  30. Debug.LogWarning("CSV File is null: " + loadCSV.name);
  31. yield break;
  32. }
  33. Debug.Log("CSV File Found: " + loadCSV.name);
  34. // CSV‚ Text
  35. string csvText = loadCSV.csvFile.text;
  36. //Split by new line
  37. string[] afterParse = csvText.Split('\n');
  38. //Skip the first line, parse by ","
  39. for (int i = 1; i < afterParse.Length; i++)
  40. {
  41. string[] parseByComma = afterParse[i].Split(',');
  42. // �skip if first column is empty
  43. if (string.IsNullOrEmpty(parseByComma[0]))
  44. {
  45. continue;
  46. }
  47. int column = 0;
  48. string cardImageName = parseByComma[column].CleanTrimString().Replace(".jpg", "").Replace(".png", "").Trim();
  49. column++;
  50. yield return EditorCoroutineUtility.StartCoroutine(CreateCardData(cardImageName), this);
  51. IEnumerator CreateCardData(string CardImageName)
  52. {
  53. // CardEntity‚ instance created
  54. CEntity_Base cardEntity = CreateInstance<CEntity_Base>();
  55. cardEntity.CardSpriteName = CardImageName;
  56. //Set card effect class name
  57. cardEntity.CardEffectClassName = parseByComma[column];
  58. column++;
  59. //Set card index number
  60. if (int.TryParse(parseByComma[column], out value))
  61. {
  62. cardEntity.CardIndex = value;
  63. }
  64. column++;
  65. //Parse max count (most csv this is empty)
  66. if (!string.IsNullOrEmpty(parseByComma[column]))
  67. {
  68. if (int.TryParse(parseByComma[column], out value))
  69. {
  70. cardEntity.MaxCountInDeck = value;
  71. }
  72. }
  73. column++;
  74. //Get Card Image Name
  75. string[] parseByUnderBar = CEntity_Base.GetParseByUnderBar(CardImageName);
  76. cardEntity.CardID = $"{parseByUnderBar[0]}";
  77. string customCardID = "";
  78. if (parseByComma.Length >= column + 1)
  79. {
  80. if (!string.IsNullOrEmpty(parseByComma[column]))
  81. {
  82. if (parseByComma[column].Contains("-"))
  83. {
  84. customCardID = parseByComma[column];
  85. }
  86. }
  87. }
  88. column++;
  89. //Get Japanese and English card ID
  90. List<string> CardListIDs_JPN = DataBase.CardListIDs(CEntity_Base.GetParseByHyphen(CardImageName)[0], false);
  91. List<string> CardListIDs_ENG = DataBase.CardListIDs(CEntity_Base.GetParseByHyphen(CardImageName)[0], true);
  92. List<TextAsset> CardListTexts_JPN = new List<TextAsset>();
  93. foreach (TextAsset textAsset in CardListTexts)
  94. {
  95. if (CardListIDs_JPN.Contains(textAsset.name))
  96. {
  97. CardListTexts_JPN.Add(textAsset);
  98. }
  99. }
  100. List<TextAsset> CardListTexts_ENG = new List<TextAsset>();
  101. foreach (TextAsset textAsset in CardListTexts)
  102. {
  103. if (CardListIDs_ENG.Contains(textAsset.name))
  104. {
  105. CardListTexts_ENG.Add(textAsset);
  106. }
  107. }
  108. List<string[]> CardDatas_JPN = OfficialCardListUtility.GetCardDatas(CardListTexts_JPN);
  109. List<string[]> CardDatas_ENG = OfficialCardListUtility.GetCardDatas(CardListTexts_ENG);
  110. List<int> evoCosts_level = new List<int>();
  111. List<int> evoCosts_memory = new List<int>();
  112. OfficialCardListUtility.AttachCardData(cardEntity, CardDatas_JPN, CardDatas_ENG, ref evoCosts_level, ref evoCosts_memory);
  113. if (cardEntity.cardKind.Contains(CardKind.Digimon))
  114. {
  115. if (evoCosts_level.Count >= 1)
  116. {
  117. List<CardColor> evoCosts_color = new List<CardColor>();
  118. //wiki‚grab text from site
  119. yield return new EditorWaitForSeconds(0.5f);
  120. WebClient wc = new WebClient();
  121. wc.Encoding = Encoding.UTF8;
  122. string resultText = wc.DownloadString($"https://wikimon.net/{cardEntity.CardID}_(DCG)");
  123. string[] parseByEnter = resultText.Split('\n');
  124. //�i‰»ƒRƒXƒg‚Ì�F
  125. for (int j = 0; j < parseByEnter.Length; j++)
  126. {
  127. string text = parseByEnter[j];
  128. if (!string.IsNullOrEmpty(text))
  129. {
  130. text = text.Replace("\n", "").Replace("\t", "").Replace(" ", "").Trim();
  131. if (text.Contains(">fromLv."))
  132. {
  133. if (text.Contains("linear-gradient"))
  134. {
  135. evoCosts_color.Add(CardColor.Red);
  136. evoCosts_color.Add(CardColor.Blue);
  137. evoCosts_color.Add(CardColor.Yellow);
  138. evoCosts_color.Add(CardColor.Green);
  139. evoCosts_color.Add(CardColor.Black);
  140. evoCosts_color.Add(CardColor.Purple);
  141. evoCosts_color.Add(CardColor.White);
  142. }
  143. else
  144. {
  145. int startIndex = text.IndexOf('#');
  146. int length = 7;
  147. text = text.Substring(startIndex, length);
  148. switch (text)
  149. {
  150. case "#cb2e32":
  151. evoCosts_color.Add(CardColor.Red);
  152. break;
  153. case "#0099dc":
  154. evoCosts_color.Add(CardColor.Blue);
  155. break;
  156. case "#f0ac09":
  157. evoCosts_color.Add(CardColor.Yellow);
  158. break;
  159. case "#009f6d":
  160. evoCosts_color.Add(CardColor.Green);
  161. break;
  162. case "#4e4e4e":
  163. evoCosts_color.Add(CardColor.Black);
  164. break;
  165. case "#7e67aa":
  166. evoCosts_color.Add(CardColor.Purple);
  167. break;
  168. case "#ceddef":
  169. evoCosts_color.Add(CardColor.White);
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. if (evoCosts_color.Count < evoCosts_level.Count)
  177. {
  178. if (cardEntity.cardColors.Count >= 1)
  179. {
  180. evoCosts_color.Add(cardEntity.cardColors[0]);
  181. }
  182. }
  183. for (int j = 0; j < evoCosts_color.Count; j++)
  184. {
  185. EvoCost evoCost = new EvoCost();
  186. evoCost.CardColor = evoCosts_color[j];
  187. int level = evoCosts_level[evoCosts_level.Count - 1];
  188. if (j < evoCosts_level.Count)
  189. {
  190. level = evoCosts_level[j];
  191. }
  192. evoCost.Level = level;
  193. int memoryCost = evoCosts_memory[evoCosts_memory.Count - 1];
  194. if (j < evoCosts_memory.Count)
  195. {
  196. memoryCost = evoCosts_memory[j];
  197. }
  198. evoCost.MemoryCost = memoryCost;
  199. cardEntity.EvoCosts.Add(evoCost);
  200. }
  201. }
  202. #region BT9-059(ƒoƒNƒ‚ƒ“)
  203. if (cardEntity.CardID == "BT9-059")
  204. {
  205. if (cardEntity.EvoCosts.Count((evoCost) => evoCost.CardColor == CardColor.Black && evoCost.MemoryCost == 0 && evoCost.Level == 2) == 0)
  206. {
  207. EvoCost evoCost = new EvoCost();
  208. evoCost.CardColor = CardColor.Black;
  209. evoCost.MemoryCost = 0;
  210. evoCost.Level = 2;
  211. cardEntity.EvoCosts.Add(evoCost);
  212. }
  213. }
  214. #endregion
  215. #region BT9-041(ƒ‰ƒCƒYƒOƒŒƒCƒ‚ƒ“X�R‘Ì)
  216. if (cardEntity.CardID == "BT9-041")
  217. {
  218. if (cardEntity.EvoCosts.Count((evoCost) => evoCost.CardColor == CardColor.Red && evoCost.MemoryCost == 4 && evoCost.Level == 4) == 0)
  219. {
  220. EvoCost evoCost = new EvoCost();
  221. evoCost.CardColor = CardColor.Red;
  222. evoCost.MemoryCost = 4;
  223. evoCost.Level = 4;
  224. cardEntity.EvoCosts.Add(evoCost);
  225. }
  226. }
  227. #endregion
  228. #region BT14-018(ƒSƒbƒhƒhƒ‰ƒ‚ƒ“)
  229. if (cardEntity.CardID == "BT14-018")
  230. {
  231. if (cardEntity.EvoCosts.Count((evoCost) => evoCost.CardColor == CardColor.Yellow && evoCost.MemoryCost == 4 && evoCost.Level == 5) == 0)
  232. {
  233. EvoCost evoCost = new EvoCost();
  234. evoCost.CardColor = CardColor.Yellow;
  235. evoCost.MemoryCost = 4;
  236. evoCost.Level = 5;
  237. cardEntity.EvoCosts.Add(evoCost);
  238. }
  239. }
  240. #endregion
  241. #region ST12-09(ƒ{ƒ‹ƒP�[ƒ‚ƒ“)
  242. if (cardEntity.CardID == "ST12-09")
  243. {
  244. if (cardEntity.EvoCosts.Count((evoCost) => evoCost.CardColor == CardColor.Black && evoCost.MemoryCost == 4 && evoCost.Level == 4) == 0)
  245. {
  246. EvoCost evoCost = new EvoCost();
  247. evoCost.CardColor = CardColor.Black;
  248. evoCost.MemoryCost = 4;
  249. evoCost.Level = 4;
  250. cardEntity.EvoCosts.Add(evoCost);
  251. }
  252. }
  253. #endregion
  254. cardEntity.EvoCosts = cardEntity.EvoCosts.Distinct().ToList();
  255. }
  256. #region ƒtƒ@ƒCƒ‹–¼‚ƕۑ¶�æ
  257. //ƒtƒ@ƒCƒ‹–¼
  258. string fileName = $"{cardEntity.CardName_ENG}-{CardImageName}.asset";
  259. fileName = fileName.Replace("?", "�H").Replace(":", "�F");
  260. cardEntity.name = fileName.Replace(".asset", "");
  261. //•Û‘¶�æ
  262. string folderName_SetID = $"{cardEntity.SetID}";
  263. string folderName_CardColor = $"{DataBase.CardColorNameDictionary[cardEntity.cardColors[0]]}";
  264. folderName_CardColor = char.ToUpper(folderName_CardColor[0]) + folderName_CardColor.Substring(1);
  265. string folderName_CardKind = $"{DataBase.CardKindENNameDictionary[cardEntity.cardKind[0]]}";
  266. string folderPath = $"Assets/CardBaseEntity/{folderName_SetID}/{folderName_CardColor}/{folderName_CardKind}";
  267. string filePath = $"{folderPath}/{fileName}".Trim().Replace("\t", "").Replace("\n", "").Replace("\r", "").Replace(" ", "");
  268. //ƒtƒHƒ‹ƒ_‚ª–³‚¯‚ê‚Î�ì�¬
  269. if (!Directory.Exists(folderPath))
  270. {
  271. Directory.CreateDirectory(folderPath);
  272. }
  273. #endregion
  274. if (!string.IsNullOrEmpty(customCardID))
  275. {
  276. cardEntity.CardID = customCardID;
  277. }
  278. // ƒAƒZƒbƒg‚Æ‚µ‚ĕۑ¶
  279. var asset = (CEntity_Base)AssetDatabase.LoadAssetAtPath(filePath, typeof(CEntity_Base));
  280. if (asset == null)
  281. {
  282. // Attempt to create Asset
  283. AssetDatabase.CreateAsset(cardEntity, filePath);
  284. }
  285. else
  286. {
  287. // •Û‘¶�æ‚̃pƒX‚Ƀtƒ@ƒCƒ‹‚ª‚ ‚ê‚Î�ã�‘‚«
  288. EditorUtility.CopySerialized(cardEntity, asset);
  289. AssetDatabase.SaveAssets();
  290. }
  291. AssetDatabase.Refresh();
  292. Debug.Log($"{i}/{afterParse.Length - 1}:{cardImageName}‚Scriptable Object Created");
  293. }
  294. }
  295. Debug.Log("Card Data Created for: " + loadCSV.name);
  296. }
  297. string GetCardDataURL(string CardImageName)
  298. {
  299. string URL = "";
  300. string[] parseByUnderBar = CEntity_Base.GetParseByUnderBar(CardImageName);
  301. URL = $"https://www.unionarena-tcg.com/jp/cardlist/detail_iframe.php?card_no={parseByUnderBar[3]}/{parseByUnderBar[0]}-{parseByUnderBar[1]}-{parseByUnderBar[2]}";
  302. return URL;
  303. }
  304. }
  305. /// <summary>
  306. /// string extension methods
  307. /// </summary>
  308. public static partial class StringExtensions
  309. {
  310. /// <summary>
  311. /// Žw’肵‚½•¶Žš—ñ‚ª‚¢‚­‚‚ ‚é‚©
  312. /// </summary>
  313. public static int CountOf(this string self, params string[] strArray)
  314. {
  315. int count = 0;
  316. foreach (string str in strArray)
  317. {
  318. int index = self.IndexOf(str, 0);
  319. while (index != -1)
  320. {
  321. count++;
  322. index = self.IndexOf(str, index + str.Length);
  323. }
  324. }
  325. return count;
  326. }
  327. /// <summary>
  328. /// Removes all instances of the specified string from the current string.
  329. /// </summary>
  330. public static string CleanTrimString(this string self)
  331. {
  332. string cleanString = self;
  333. if (!string.IsNullOrEmpty(self))
  334. {
  335. foreach (string str in new string[] { " ", "\n", "\r", "\t" })
  336. {
  337. cleanString = cleanString.Replace(str, "");
  338. }
  339. }
  340. return cleanString.Trim();
  341. }
  342. }