CsvLoader_CardEntity.cs 16 KB

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