private void ImportRows(DatablockDetectionInfo datablockDetectionInfo) { using (TextReader reader = File.OpenText(csvFilePath)) { var parser = new CsvParser2(); parser.TrimTrailingEmptyLines = true; string[][] parsed = parser.Parse(reader); for (int i = 1; i < parsed.GetLength(0); i++) { string[] row = parsed[i]; string datablockName = row[nameIndex]; Datablock datablock = GetNamedDatablock(datablockDetectionInfo, datablockName); for (int index = 0; index < parsed[i].Length; index++) { if (index == headers.Count) { break; } string fieldValue = parsed[i][index]; string fieldName = headers[index]; ProcessRawField(datablockDetectionInfo, fieldName, datablock, fieldValue); } } Debug.Log("Imported " + (parsed.GetLength(0) - 1) + " datablocks."); } }
private void ImportRows(DatablockDetectionInfo datablockDetectionInfo) { using (TextReader reader = File.OpenText(csvFilePath)) { var parser = new CsvParser2(); parser.TrimTrailingEmptyLines = true; string[][] parsed = parser.Parse(reader); for (int i = 1; i < parsed.GetLength(0); i++) { string[] row = parsed[i]; string datablockName = row[nameIndex]; Datablock datablock = GetNamedDatablock(datablockDetectionInfo, datablockName); for (int index = 0; index < parsed[i].Length; index++) { if (index == headers.Count) break; string fieldValue = parsed[i][index]; string fieldName = headers[index]; ProcessRawField(datablockDetectionInfo, fieldName, datablock, fieldValue); } } Debug.Log("Imported " + (parsed.GetLength(0) - 1) + " datablocks."); } }
/// <summary> /// Attempt to determine the type of datablock based on the spreadsheets headers /// </summary> /// <param name="headers">1st row headers</param> /// <returns></returns> protected List<DatablockDetectionInfo> AutoDetectDatablockType(List<string> headers) { List<Type> types = DatablockList.GetInstances(typeof (Datablock)); if (types.Count == 0) { Debug.LogError("No datablocks defined!"); return null; } var typeMatches = new List<DatablockDetectionInfo>(); foreach (Type datablockType in types) { var detectionInfo = new DatablockDetectionInfo { datablockType = datablockType }; typeMatches.Add(detectionInfo); foreach (string header in headers) { FieldInfo field = datablockType.GetField(header, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (field != null) detectionInfo.fields.Add(field); } } // Not a single field matched if (typeMatches.Sum(t => t.fields.Count) == 0) return null; return typeMatches; }
protected void ProcessRawField(DatablockDetectionInfo datablockDetectionInfo, string fieldName, Datablock datablock, string fieldValue) { if (fieldName.Equals("parent", StringComparison.OrdinalIgnoreCase)) { datablock.Parent = DatablockManager.Instance.GetDatablock(fieldValue, datablockDetectionInfo.datablockType); return; } FieldInfo field = datablockDetectionInfo.fields.FirstOrDefault(f => f.Name.Equals(fieldName, StringComparison.OrdinalIgnoreCase)); if (field == null) { return; } if (string.IsNullOrEmpty(fieldValue)) { datablock.SetOverridesParent(field, false); return; } SetField(datablock, field, fieldValue); }
private void ImportRows(DatablockDetectionInfo datablockDetectionInfo) { var myFile = new StreamReader(jsonFilePath); var jsonStr = myFile.ReadToEnd(); myFile.Close(); var ct = 0; var datablockValues = new Dictionary <string, string>(); var reader = new JsonTextReader(new StringReader(jsonStr)); while (reader.Read()) { if (reader.TokenType == JsonToken.StartObject) { // New datablock datablockValues = new Dictionary <string, string>(); } else if (reader.TokenType == JsonToken.PropertyName) { var fieldName = reader.Value as string; reader.Read(); datablockValues[fieldName] = reader.Value as string; } else if (reader.TokenType == JsonToken.EndObject) { if (!datablockValues.ContainsKey("Name")) { Debug.LogWarning("Datalock missing Name field"); continue; } var datablockName = datablockValues["Name"]; Datablock datablock = GetNamedDatablock(datablockDetectionInfo, datablockName); foreach (var datablockValue in datablockValues) { var fieldName = datablockValue.Key; var fieldValue = datablockValue.Value; if (fieldName == "Name") { continue; } ProcessRawField(datablockDetectionInfo, fieldName, datablock, fieldValue); } ct++; } } Debug.Log("Imported " + ct + " datablocks."); }
protected Datablock GetNamedDatablock(DatablockDetectionInfo datablockDetectionInfo, string datablockName) { Datablock datablock = DatablockManager.Instance.GetDatablock(datablockName, datablockDetectionInfo.datablockType, true); if (datablock == null) { datablock = CreateInstance(datablockDetectionInfo.datablockType) as Datablock; string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(newDatablockDir + "/" + datablockName + ".asset"); AssetDatabase.CreateAsset(datablock, assetPathAndName); } return datablock; }
protected Datablock GetNamedDatablock(DatablockDetectionInfo datablockDetectionInfo, string datablockName) { Datablock datablock = DatablockManager.Instance.GetDatablock(datablockName, datablockDetectionInfo.datablockType, true); if (datablock == null) { datablock = CreateInstance(datablockDetectionInfo.datablockType) as Datablock; string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(newDatablockDir + "/" + datablockName + ".asset"); AssetDatabase.CreateAsset(datablock, assetPathAndName); } return(datablock); }
/// <summary> /// Attempt to determine the type of datablock based on the spreadsheets headers /// </summary> /// <param name="headers">1st row headers</param> /// <returns></returns> protected List <DatablockDetectionInfo> AutoDetectDatablockType(List <string> headers) { List <Type> types = DatablockList.GetInstances(typeof(Datablock)); if (types.Count == 0) { Debug.LogError("No datablocks defined!"); return(null); } var typeMatches = new List <DatablockDetectionInfo>(); foreach (Type datablockType in types) { var detectionInfo = new DatablockDetectionInfo { datablockType = datablockType }; typeMatches.Add(detectionInfo); foreach (string header in headers) { FieldInfo field = datablockType.GetField(header, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (field != null) { detectionInfo.fields.Add(field); } } } // Not a single field matched if (typeMatches.Sum(t => t.fields.Count) == 0) { return(null); } return(typeMatches); }
protected void ProcessRawField(DatablockDetectionInfo datablockDetectionInfo, string fieldName, Datablock datablock, string fieldValue) { if (fieldName.Equals("parent", StringComparison.OrdinalIgnoreCase)) { datablock.Parent = DatablockManager.Instance.GetDatablock(fieldValue, datablockDetectionInfo.datablockType); return; } FieldInfo field = datablockDetectionInfo.fields.FirstOrDefault(f => f.Name.Equals(fieldName, StringComparison.OrdinalIgnoreCase)); if (field == null) return; if (string.IsNullOrEmpty(fieldValue)) { datablock.SetOverridesParent(field, false); return; } SetField(datablock, field, fieldValue); }
private void ImportRows(DatablockDetectionInfo datablockDetectionInfo) { foreach (ListEntry spreadsheetRow in spreadsheetRows) { string name = spreadsheetRow.Elements[nameColumnIndex].Value; Datablock datablock = GetNamedDatablock(datablockDetectionInfo, name); foreach (ListEntry.Custom element in spreadsheetRow.Elements) { string fieldName = element.LocalName; string fieldValue = element.Value; ProcessRawField(datablockDetectionInfo, fieldName, datablock, fieldValue); } EditorUtility.SetDirty(datablock); } AssetDatabase.SaveAssets(); Debug.Log(spreadsheetRows.Count + " datablocks imported"); }
private void OnGUI() { EditorGUILayout.HelpBox("Import data in CSV format.", MessageType.None); GUI.backgroundColor = new Color(0, 0.8f, .8f); if (GUILayout.Button("Choose CSV file")) { csvFilePath = EditorUtility.OpenFilePanel("Load CSV file", "", "csv"); if (!string.IsNullOrEmpty(csvFilePath)) { ParseCSVFile(); if (detectedTypes != null) { importDatablockTypeIndex = detectedTypes.IndexOf(detectedTypes.OrderByDescending(d => d.fields.Count).First()); } } } GUI.backgroundColor = Color.white; if (detectedTypes != null) { importDatablockTypeIndex = EditorGUILayout.Popup("Choose datablock type", importDatablockTypeIndex, detectedTypes.Select(d => d.datablockType.ToString()).ToArray()); DatablockDetectionInfo datablockDetectionInfo = detectedTypes[importDatablockTypeIndex]; fieldsFoldout = EditorGUILayout.Foldout(fieldsFoldout, datablockDetectionInfo.fields.Count + " detected fields"); if (fieldsFoldout) { foreach (FieldInfo field in datablockDetectionInfo.fields) { EditorGUILayout.LabelField(field.Name); } } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("New datablock path: \"" + newDatablockDir + "\""); if (GUILayout.Button("Change")) { string path = EditorUtility.OpenFolderPanel("Select path to save new datablock", "Assets", ""); if (String.IsNullOrEmpty(path)) { return; } int assetsIndex = path.IndexOf("/Assets", StringComparison.Ordinal); if (assetsIndex == -1) { Debug.LogError("Path must be in the Assets folder"); return; } newDatablockDir = path.Substring(assetsIndex + 1); EditorPrefs.SetString("newDatablockDir", newDatablockDir); } EditorGUILayout.EndHorizontal(); GUI.backgroundColor = new Color(0, 0.8f, 0); if (GUILayout.Button("Import!")) { ImportRows(datablockDetectionInfo); DatablockManager.Instance.RefreshAssets(); } GUI.backgroundColor = Color.white; } EditorGUILayout.Separator(); showImportOptions = EditorGUILayout.Foldout(showImportOptions, "Import options"); if (showImportOptions) { EditorGUI.indentLevel = 1; fieldDelimiter = EditorGUILayout.TextField("Field delimiter", fieldDelimiter); } }
private void ShowSpreadsheetList() { EditorGUILayout.HelpBox("Refresh spreadsheets to retrieve a list of sheets associated with the connected Google account. Only the first" + " sheet in a workbook will be loaded and the first row should contain the field names.", MessageType.None); EditorGUILayout.BeginHorizontal(); GUI.backgroundColor = new Color(0, 0.8f, .8f); if (GUILayout.Button("Refresh Spreesheets")) { spreadsheetEntries.Clear(); var query = new SpreadsheetQuery(); SpreadsheetFeed feed = null; try { feed = sheetsAPI.Service.Query(query); } catch (Exception) { Debug.LogError("OAuth error"); sheetsAPI.ClearOAuthToken(); throw; } // Iterate through all of the spreadsheets returned foreach (SpreadsheetEntry entry in feed.Entries) { spreadsheetEntries.Add(entry); } } GUI.backgroundColor = new Color(1f, 0.2f, .4f); if (GUILayout.Button("Clear OAuth token")) { sheetsAPI.ClearOAuthToken(); } GUI.backgroundColor = Color.white; EditorGUILayout.EndHorizontal(); var options = new List <string>(spreadsheetEntries.Select(s => s.Title.Text)); if (spreadsheetEntries.Count == 0) { options.Insert(0, "No spreadsheets found"); } else { options.Insert(0, "Select"); } if (spreadsheetIndex >= options.Count) { spreadsheetIndex = 0; } int tempSpreadsheetIndex = EditorGUILayout.Popup("Select spreedsheet", spreadsheetIndex, options.ToArray()); if (tempSpreadsheetIndex != spreadsheetIndex) { canBeImported = false; spreadsheetIndex = tempSpreadsheetIndex; if (spreadsheetIndex != 0) { DatablockManager.Instance.RefreshAssets(); SpreadsheetEntry spreadsheet = spreadsheetEntries.FirstOrDefault(s => s.Title.Text == options[spreadsheetIndex]); detectedTypes = ParseSpreadsheet(spreadsheet); if (detectedTypes == null) { Debug.LogError("No data detected in sheet"); canBeImported = false; return; } if (nameColumnIndex == -1) { Debug.LogError("Sheet must have a header field named 'Name'"); canBeImported = false; return; } importDatablockTypeIndex = detectedTypes.IndexOf(detectedTypes.OrderByDescending(d => d.fields.Count).First()); canBeImported = true; } } if (spreadsheetIndex != 0 && canBeImported) { importDatablockTypeIndex = EditorGUILayout.Popup("Choose datablock type", importDatablockTypeIndex, detectedTypes.Select(d => d.datablockType.ToString()).ToArray()); DatablockDetectionInfo datablockDetectionInfo = detectedTypes[importDatablockTypeIndex]; fieldsFoldout = EditorGUILayout.Foldout(fieldsFoldout, datablockDetectionInfo.fields.Count + " detected fields"); if (fieldsFoldout) { foreach (FieldInfo field in datablockDetectionInfo.fields) { EditorGUILayout.LabelField(field.Name); } } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("New datablock path: \"" + newDatablockDir + "\""); if (GUILayout.Button("Change")) { string path = EditorUtility.OpenFolderPanel("Select path to save new datablock", "Assets", ""); if (String.IsNullOrEmpty(path)) { return; } int assetsIndex = path.IndexOf("/Assets", StringComparison.Ordinal); if (assetsIndex == -1) { Debug.LogError("Path must be in the Assets folder"); return; } newDatablockDir = path.Substring(assetsIndex + 1); EditorPrefs.SetString("newDatablockDir", newDatablockDir); } EditorGUILayout.EndHorizontal(); int existingCount = 0; int newCount = 0; foreach (ListEntry spreadsheetRow in spreadsheetRows) { string name = spreadsheetRow.Elements[nameColumnIndex].Value; if (DatablockManager.Instance.GetDatablock(name, datablockDetectionInfo.datablockType, true) != null) { existingCount++; } else { newCount++; } } EditorGUILayout.Separator(); EditorGUILayout.HelpBox("Ready to import " + newCount + " new and " + existingCount + " existing datablocks.", MessageType.Info); EditorGUILayout.Separator(); GUI.backgroundColor = new Color(0, 0.8f, 0); if (GUILayout.Button("Import!")) { ImportRows(datablockDetectionInfo); DatablockManager.Instance.RefreshAssets(); } } }