private static Dictionary <string, string> ToCSharpArray(string excelPath) { var lst = new Dictionary <string, string>(); var book = EEWorkbook.Load(excelPath); if (book == null) { return(lst); } foreach (var sheet in book.sheets) { if (sheet == null) { continue; } if (!IsValidSheet(sheet)) { EELog.Log(string.Format("Skipped sheet {0} in file {1}.", sheet.name, excelPath)); continue; } var sheetData = ToSheetData(sheet); var csTxt = ToCSharp(sheetData, sheet.name); lst.Add(sheet.name, csTxt); } return(lst); }
private static Dictionary <string, string> ToGolangArray(string excelPath) { var lst = new Dictionary <string, string>(); var book = EEWorkbook.Load(excelPath); if (book == null) { return(lst); } string fileName = Path.GetFileName(excelPath); foreach (var sheet in book.sheets) { if (sheet == null) { continue; } if (!IsValidSheet(sheet)) { EELog.Log(string.Format("Skipped sheet [{0}] in file <{1}>.", sheet.name, fileName)); continue; } var sheetData = ToSheetData(sheet); var csTxt = ToGolang(sheetData, sheet.name, fileName); lst.Add(sheet.name, csTxt); } return(lst); }
public static void GenerateScriptableObjects(string xlsxPath, string assetPath) { try { xlsxPath = xlsxPath.Replace("\\", "/"); assetPath = assetPath.Replace("\\", "/"); if (!Directory.Exists(xlsxPath)) { EditorUtility.DisplayDialog("EasyExcel", "Xls/xlsx path doesn't exist.", "OK"); return; } xlsxPath = xlsxPath.Replace("\\", "/"); assetPath = assetPath.Replace("\\", "/"); if (!assetPath.EndsWith("/")) { assetPath += "/"; } if (Directory.Exists(assetPath)) { Directory.Delete(assetPath, true); } Directory.CreateDirectory(assetPath); AssetDatabase.Refresh(); var filePaths = Directory.GetFiles(xlsxPath); var count = 0; for (var i = 0; i < filePaths.Length; ++i) { var filePath = filePaths[i].Replace("\\", "/"); if (!IsExcelFile(filePath)) { continue; } UpdateProgressBar(i, filePaths.Length, ""); ToScriptableObject(filePath, assetPath); count++; } EELog.Log("Assets are generated successfully."); ClearProgressBar(); AssetDatabase.Refresh(); EELog.Log(string.Format("Import done. {0} sheets were imported.", count)); } catch (Exception e) { EELog.LogError(e.ToString()); ClearProgressBar(); AssetDatabase.Refresh(); } }
private static void OnScriptsReloaded() { if (!EditorPrefs.GetBool(EEConverter.csChangedKey, false)) { return; } EditorPrefs.SetBool(EEConverter.csChangedKey, false); var historyExcelPath = EditorPrefs.GetString(EEConverter.excelPathKey); if (string.IsNullOrEmpty(historyExcelPath)) { return; } EELog.Log("Scripts are reloaded, start generating assets..."); EEConverter.GenerateScriptableObjects(historyExcelPath, Environment.CurrentDirectory + "/" + EESettings.Current.GeneratedAssetPath); }
public static void OpenSettingsWindow() { try { if (EditorApplication.isCompiling) { EELog.Log("Waiting for Compiling completed."); return; } var window = GetWindowWithRect <EESettingsEditor>(new Rect(0, 0, 480, 640), true, "Settings", true); window.Show(); } catch (Exception e) { EELog.LogError(e.ToString()); } }
//[MenuItem("Tools/EasyExcel/Workbook Editor", false, 9)] private static void MenuOpenWindow() { try { if (EditorApplication.isCompiling) { EELog.Log("Waiting for Compiling completed."); return; } var window = GetWindow <EEWorkbookEditor>(WindowName); window.Show(); } catch (Exception e) { EELog.LogError(e.ToString()); } }
private static void OpenAboutWindow() { try { if (EditorApplication.isCompiling) { EELog.Log("Waiting for Compiling completed."); return; } var window = GetWindowWithRect <EEAboutWindow>(new Rect(0, 0, 480, 320), true, "About EasyExcel", true); window.Show(); } catch (Exception e) { EELog.LogError(e.ToString()); } }
public void Load() { #if UNITY_EDITOR if (!EESettings.Current.GeneratedAssetPath.Contains("/Resources/")) { EditorUtility.DisplayDialog("EasyExcel", string.Format( "AssetPath of EasyExcel Settings MUST be in Resources folder.\nCurrent is {0}.", EESettings.Current.GeneratedAssetPath), "OK"); return; } #endif tableDataDic.Clear(); var baseDataTableType = typeof(RowDataCollection); foreach (var dataTableType in baseDataTableType.Assembly.GetTypes().Where(t => t.IsSubclassOf(baseDataTableType))) { ParseOneDataTable(dataTableType); } EELog.Log(string.Format("{0} tables loaded.", tableDataDic.Count)); }
public static void GenerateCSharpFiles(string excelPath, string csPath) { try { excelPath = excelPath.Replace("\\", "/"); csPath = csPath.Replace("\\", "/"); if (!Directory.Exists(excelPath)) { EditorUtility.DisplayDialog("EasyExcel", "Excel files path doesn't exist.", "OK"); return; } if (!Directory.Exists(csPath)) { var opt = EditorUtility.DisplayDialogComplex("EasyExcel", string.Format( "EasyExcelSettings CSharpPath doesn't exist. Would you like to create it?\n{0}", csPath), "Create", "Cancel", "Quit"); switch (opt) { case 0: Directory.CreateDirectory(csPath); break; case 1: case 2: return; } } var tmpPath = Environment.CurrentDirectory + "/EasyExcelTmp/"; if (Directory.Exists(tmpPath)) { Directory.Delete(tmpPath, true); } Directory.CreateDirectory(tmpPath); excelPath = excelPath.Replace("\\", "/"); csPath = csPath.Replace("\\", "/"); if (!csPath.EndsWith("/")) { csPath += "/"; } var csChanged = false; var filePaths = Directory.GetFiles(excelPath); for (var i = 0; i < filePaths.Length; ++i) { var excelFilePath = filePaths[i].Replace("\\", "/"); if (i + 1 < filePaths.Length) { UpdateProgressBar(i + 1, filePaths.Length, ""); } else { ClearProgressBar(); } if (!IsExcelFile(excelFilePath)) { continue; } var newCsDict = ToCSharpArray(excelFilePath); foreach (var newCs in newCsDict) { var cSharpFileName = EESettings.Current.GetCSharpFileName(newCs.Key); var tmpCsFilePath = tmpPath + cSharpFileName; var csFilePath = csPath + cSharpFileName; var shouldWrite = true; if (File.Exists(csFilePath)) { var oldCs = File.ReadAllText(csFilePath); shouldWrite = oldCs != newCs.Value; } if (!shouldWrite) { continue; } csChanged = true; var streamwriter = new StreamWriter(tmpCsFilePath, false); streamwriter.Write(newCs.Value); streamwriter.Flush(); streamwriter.Close(); } } if (csChanged) { EditorPrefs.SetBool(csChangedKey, true); var files = Directory.GetFiles(tmpPath); foreach (var s in files) { var p = s.Replace("\\", "/"); File.Copy(s, csPath + p.Substring(p.LastIndexOf("/", StringComparison.Ordinal)), true); } Directory.Delete(tmpPath, true); AssetDatabase.Refresh(); EELog.Log("Scripts are generated, wait for generating assets..."); } else { EELog.Log("No CSharp files changed, begin generating assets..."); ClearProgressBar(); var historyExcelPath = EditorPrefs.GetString(excelPathKey); if (!string.IsNullOrEmpty(historyExcelPath)) { GenerateScriptableObjects(historyExcelPath, Environment.CurrentDirectory + "/" + EESettings.Current.GeneratedAssetPath); } } } catch (Exception e) { EELog.LogError(e.ToString()); EditorPrefs.SetBool(csChangedKey, false); ClearProgressBar(); AssetDatabase.Refresh(); } }
public static void GenerateGolangFiles(string excelPath, string goPath) { try { excelPath = excelPath.Replace("\\", "/"); goPath = goPath.Replace("\\", "/"); if (!Directory.Exists(excelPath)) { EditorUtility.DisplayDialog("EasyExcel", "Excel files path doesn't exist.", "OK"); return; } if (!Directory.Exists(goPath)) { Directory.CreateDirectory(goPath); } var tmpPath = Environment.CurrentDirectory + "/EasyExcelTmp/"; if (Directory.Exists(tmpPath)) { Directory.Delete(tmpPath, true); } Directory.CreateDirectory(tmpPath); excelPath = excelPath.Replace("\\", "/"); goPath = goPath.Replace("\\", "/"); if (!goPath.EndsWith("/")) { goPath += "/"; } var goChanged = false; var filePaths = Directory.GetFiles(excelPath); var loadGoFileBuilder = new StringBuilder().AppendLine("package gamedata").AppendLine().AppendLine("func LoadAllTables() {"); for (var i = 0; i < filePaths.Length; ++i) { var excelFilePath = filePaths[i].Replace("\\", "/"); if (i + 1 < filePaths.Length) { UpdateProgressBar(i + 1, filePaths.Length, ""); } else { ClearProgressBar(); } if (!IsExcelFile(excelFilePath)) { continue; } string fileName = Path.GetFileName(excelFilePath); var newGoTxtDic = ToGolangArray(excelFilePath); foreach (var newGoInfo in newGoTxtDic) { var cSharpFileName = EESettings.Current.GetGolangFileName(fileName, newGoInfo.Key); var tmpCsFilePath = tmpPath + cSharpFileName; var csFilePath = goPath + cSharpFileName; loadGoFileBuilder.AppendLine($" Load_{EESettings.Current.GetSheetClassName(fileName, newGoInfo.Key)}_Map()"); var shouldWrite = true; if (File.Exists(csFilePath)) { var oldCs = File.ReadAllText(csFilePath); shouldWrite = oldCs != newGoInfo.Value; } if (!shouldWrite) { continue; } goChanged = true; File.WriteAllText(tmpCsFilePath, newGoInfo.Value, Encoding.UTF8); } } // LoadAllTables.go loadGoFileBuilder.AppendLine("}"); var loadGoFileBuilderStr = loadGoFileBuilder.ToString(); var tempLoadAllTablesFilePath = tmpPath + "LoadAllTables.go"; var targetLoadAllTablesFilePath = goPath + "LoadAllTables.go"; if (File.Exists(targetLoadAllTablesFilePath)) { var oldCs = File.ReadAllText(targetLoadAllTablesFilePath); goChanged = goChanged || oldCs != loadGoFileBuilderStr; } else { goChanged = true; } File.WriteAllText(tempLoadAllTablesFilePath, loadGoFileBuilderStr, Encoding.UTF8); // if (goChanged) { EditorPrefs.SetBool(csChangedKey, true); var files = Directory.GetFiles(tmpPath); foreach (var s in files) { var p = s.Replace("\\", "/"); File.Copy(s, goPath + p.Substring(p.LastIndexOf("/", StringComparison.Ordinal)), true); } Directory.Delete(tmpPath, true); EELog.Log("Scripts are generated, wait for generating assets..."); } else { EELog.Log("No GoLang files changed, begin generating assets..."); ClearProgressBar(); var historyExcelPath = EditorPrefs.GetString(excelPathKey); if (!string.IsNullOrEmpty(historyExcelPath)) { // 生成go资源 if (!string.IsNullOrWhiteSpace(EESettings.Current.GeneratedGoAssetPath)) { GenerateGoAssets(historyExcelPath, Environment.CurrentDirectory + "/" + EESettings.Current.GeneratedGoAssetPath); } } } } catch (Exception e) { EELog.LogError(e.ToString()); EditorPrefs.SetBool(csChangedKey, false); ClearProgressBar(); AssetDatabase.Refresh(); } }