Exemple #1
0
        //needs to assign the text asset as part of its creation
        static void ProcessAsset(string origLocation, TextAsset txt, DeadSimpleCSV csv)
        {
            //if matching file exists, then get it
            ExternalVarsSO extvar = ExternalVarsSOManager.Instance().FindByExternalFile(txt);

            var  fileName     = origLocation.Substring(0, origLocation.Length - (".csv".Length)) + ".asset";
            bool createdAsset = false;

            //if not create it an
            if (extvar == null)
            {
                extvar          = ExternalVarsSO.CreateInstance <ExternalVarsSO>();
                extvar.localCSV = txt;
                AssetDatabase.CreateAsset(extvar, fileName);
                createdAsset = true;
            }

            try
            {
                extvar.GenerateVars(csv.ConvertRowsToObjects <ExternalVarsSO.VarSpec>());
            }
            catch (Exception)
            {
                if (createdAsset)
                {
                    AssetDatabase.DeleteAsset(fileName);
                    UnityEngine.Object.DestroyImmediate(extvar);
                }
            }
        }
        public override void OnInspectorGUI()
        {
            CSVWranglerStartUp my = (CSVWranglerStartUp)target;

            //EditorGUIUtility.LookLikeControls();
            DrawDefaultInspector();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Download index file"))
            {
                FileDownloadWindow.Init();

                FileDownloadWindow.instance.AddToDlQueue(my.settings.liveURL, "CSVs/csvWrangler.csv", FileDownloadWindow.DownloadLocation.ResourceFolder);
            }

            if (File.Exists(Application.dataPath + "/Resources/CSVs/csvWrangler.csv") && GUILayout.Button("Download referenced csvs"))
            {
                //open the csvWrangler.csv into deadsimple csv
                DeadSimpleCSV indexTable = new DeadSimpleCSV(File.ReadAllText(Application.dataPath + "/Resources/CSVs/csvWrangler.csv"), true);

                //dump into classlist
                List<CSVWrangler.IndexRow> rows = indexTable.ConvertRowsToObjects<CSVWrangler.IndexRow>();

                FileDownloadWindow.Init();

                foreach (CSVWrangler.IndexRow row in rows)
                {
                    FileDownloadWindow.instance.AddToDlQueue(row.url, "CSVs/" + row.name + ".csv", FileDownloadWindow.DownloadLocation.ResourceFolder);
                }

            }

            EditorGUILayout.EndHorizontal();
        }
Exemple #3
0
        //load local tables from persist storage as described in the curtable, which is loaded from the csvwrangler.csv
        //	currently synchronous, in the future may become async
        public void LoadTables()
        {
            State = CSVWranglerState.Loading;

            //reset so we could reload
            tables = new Dictionary <string, DeadSimpleCSV>();

            //grab the row and load each file of name into the tables dict
            string[] tableNames = indexTable.GetColumn("name");

            foreach (string name in tableNames)
            {
                string tableFullLoc = Application.persistentDataPath + "/" + name + ".csv";

                //TODO could change this logic so the file contents is returned by the util function so we don't double load?
                //make sure it exists
                UTIL.EnsureTextResourceIsHere(tableFullLoc, "csvs/" + name);

                //attempt to load
                string fileContents = UTIL.ReadAllText(tableFullLoc);

                //notify of failure
                if (string.IsNullOrEmpty(fileContents))
                {
                    Debug.LogWarning("Tried to load the " + name + " csv but something went wrong");
                }
                else//success, maybe
                {
                    DeadSimpleCSV table = new DeadSimpleCSV(fileContents, true);
                    tables.Add(name, table);
                }
            }

            State = CSVWranglerState.Ready;
        }
Exemple #4
0
        public void WriteVarsToLocal()
        {
            var rows    = GenerateVarSpecs();
            var csvFile = DeadSimpleCSV.CreateFromList(rows).GetAsCSVString(true);

#if UNITY_EDITOR
            System.IO.File.WriteAllText(AssetDatabase.GetAssetPath(localCSV), csvFile);
            AssetDatabase.Refresh();
#endif
        }
Exemple #5
0
        private void _addStringMoveForward(List <string> addto, string s)
        {
            int len = s.Length;

            var r = DeadSimpleCSV.DecodeFromCSV(s);

            addto.Add(trimWhiteSpace ? r.Trim() : r);

            curIndex += len;
        }
Exemple #6
0
    public void SetupTests()
    {
        csvFromFile = new AID.DeadSimpleCSV(fileContents, true);

        listFromCSV = csvFromFile.ConvertRowsToObjects <ExampleCSVSerialiseClass>();

        csvFromList = AID.DeadSimpleCSV.CreateFromList(listFromCSV);

        listFromCSVFromList = csvFromList.ConvertRowsToObjects <ExampleCSVSerialiseClass>();

        csvFromListStr = csvFromList.GetAsCSVString(true);
    }
Exemple #7
0
        //create an instance from a list of objects
        static public DeadSimpleCSV CreateFromList <T>(List <T> l) where T : class, new()
        {
            DeadSimpleCSV retval = new DeadSimpleCSV();
            Type          t      = typeof(T);

            //create header
            FieldInfo[]    fields = t.GetFields();
            PropertyInfo[] props  = t.GetProperties();


            retval.headers = new string[fields.Length + props.Length];

            int i = 0;

            for (; i < l.Count; i++)
            {
                retval.rows.Add(new string[fields.Length + props.Length]);
            }


            i = 0;

            foreach (FieldInfo f in fields)
            {
                //filling in headers
                retval.headers[i] = f.Name;

                for (int j = 0; j < l.Count; j++)
                {
                    retval.rows[j][i] = (string)Convert.ChangeType(f.GetValue(l[j]), typeof(string));
                }

                i++;
            }

            foreach (PropertyInfo p in props)
            {
                //filling in headers
                retval.headers[i] = p.Name;

                for (int j = 0; j < l.Count; j++)
                {
                    retval.rows[j][i] = (string)Convert.ChangeType(p.GetValue(l[j], null), typeof(string));
                }

                i++;
            }


            retval.GenerateColumnLookup();

            return(retval);
        }
Exemple #8
0
        public void AppendCols(int skipCols, DeadSimpleCSV appendFrom)
        {
            var appendThis = appendFrom.SkipCols(skipCols);

            for (int i = 0; i < rows.Count && i < appendThis.rows.Count; i++)
            {
                List <string> tmp = new List <string>();
                tmp.AddRange(rows[i]);
                tmp.AddRange(appendThis.rows[i]);

                rows[i] = tmp.ToArray();
            }
        }
Exemple #9
0
        //find table of given name. Should be called sparingly, if you need the table a lot then cache it
        //	DeadSimpleCSV myTable = CSVWrangler.Instance().GetTable("Example");
        public DeadSimpleCSV GetTable(string name)
        {
            DeadSimpleCSV retval = null;

            tables.TryGetValue(name, out retval);

            if (retval == null)
            {
                Debug.LogWarning("No table with name " + name);
            }

            return(retval);
        }
Exemple #10
0
        private void _processAllNewRows(List <string> colnames, DeadSimpleCSV newTable)
        {
            //for all
            foreach (string targetName in colnames)
            {
                string[] curRow = indexTable.GetRowWithData("name", targetName);
                string[] newRow = newTable.GetRowWithData("name", targetName);

                if (curRow == null)
                {
                    //we don't have it add the version and download
                    if (newRow != null)
                    {
                        UpdateTable(targetName,
                                    newTable.GetCell("url", newRow),
                                    newTable.GetCell("ver", newRow));

                        Debug.Log(targetName + " did not exist, creating from live.");
                    }
                }
                else if (newRow == null)
                {
                    Debug.Log(targetName + " no longer exists in live, removing.");
                    //this table doesn't exist anymore, drop it
                    indexTable.RemoveRow(curRow);

                    //we only try to load files that are in the latest csvwrangler index so we can delete this old one now
                    File.Delete(Application.persistentDataPath + "/" + targetName + ".csv");
                }
                else
                {
                    //if the version in ours is lower download
                    int curVer = int.Parse(indexTable.GetCell("ver", curRow));// curRow[curTable.ColumnNames["ver"]]);
                    int newVer = int.Parse(newTable.GetCell("ver", newRow));

                    if (newVer > curVer || settings.alwaysDownloadTables)
                    {
                        Debug.Log(targetName + (newVer > curVer ? " was old, updating" : " was forced to update"));
                        //download new ver
                        UpdateTable(targetName,
                                    newTable.GetCell("url", newRow),
                                    newTable.GetCell("ver", newRow));
                    }
                }
            }
        }
Exemple #11
0
        private void ProcessIndex(DeadSimpleCSV newTable)
        {
            State = CSVWranglerState.ProcessingIndex;

            //make a list of all name col names
            string[] curNames = indexTable.GetColumn("name"), newNames = newTable.GetColumn("name");

            List <string> colnames = new List <string>();

            colnames.AddRange(curNames.ToList());
            colnames.AddRange(newNames.ToList());

            colnames = colnames.Distinct().ToList();

            //this is all a bit too table-ey and memory trashy but it is a table and it is a one off init process

            //handles all new rows from the given index, updates, creates and destroys as required
            _processAllNewRows(colnames, newTable);

            //flush the index file back to storage
            StartCoroutine(_flushUpdatedTable(true));
        }
Exemple #12
0
        static bool ProcessIfValidExtVarAsset(string item)
        {
            //if it is a csv and it loads and it has the required cols
            if (item.EndsWith(".csv"))
            {
                var txt = AssetDatabase.LoadAssetAtPath <TextAsset>(item);
                if (txt == null)
                {
                    return(false);
                }

                DeadSimpleCSV csv = null;
                try
                {
                    csv = new DeadSimpleCSV(txt.text, true);
                }
                catch (Exception)
                {
                }
                ProcessAsset(item, txt, csv);
            }
            return(false);
        }
Exemple #13
0
        public void ForceInitIndex()
        {
            string csvIndexLoc = Application.persistentDataPath + "/csvWrangler.csv";

            //ForceCacheLocalFiles();
            if (!ForceCacheIndex())
            {
                Debug.LogError("CSV Wrangler is trying to be used without a local cached csvWrangler.csv in the Resources. Did you remember to cache your csvs?");
                State = CSVWranglerState.ErrorNoIndex;
                return;
            }

            //load previous csvwrangler index csv file
            indexTable = new DeadSimpleCSV(UTIL.ReadAllText(csvIndexLoc), true);

            //something terrible happened fake an empty one
            if (indexTable.headers == null || indexTable.headers.Length == 0)
            {
                indexTable.headers = new string[] { "name", "url", "ver" };
            }

            State = CSVWranglerState.IndexOnly;
        }
    void Start()
    {
        //get the call back for all dls completed

        //handles native types and enums
        // this shows loading a table of data where the col name matches a classes var name
        // the ConvertRowsToObjects uses reflection and convert to out each row as the given object
        // has not been tested with classes that have refs to other classes, this will most likely NOT work

        AID.DeadSimpleCSV csvFromFile = new AID.DeadSimpleCSV(txtFile.text, true);

        List <ExampleCSVSerialiseClass> listFromCSV = csvFromFile.ConvertRowsToObjects <ExampleCSVSerialiseClass>();

        foreach (ExampleCSVSerialiseClass o in listFromCSV)
        {
            print(o);
        }

        AID.DeadSimpleCSV csvFromList = AID.DeadSimpleCSV.CreateFromList <ExampleCSVSerialiseClass>(listFromCSV);
        print(csvFromList.GetAsCSVString(true));

        AID.CSVWrangler.Instance().CSVWranglerChange += CSVWranglerStateHasChanged;
        AID.CSVWrangler.Instance().Init();
    }
Exemple #15
0
 internal void AppendRows(int skipRows, DeadSimpleCSV t)
 {
     rows.AddRange(t.rows.GetRange(skipRows, t.rows.Count - skipRows));
 }
Exemple #16
0
        public static List <T> ConvertStringCSVToObjects <T>(string csvStr, bool generateColumnLookup)  where T : class, new()
        {
            var csv = new DeadSimpleCSV(csvStr, generateColumnLookup);

            return(csv.ConvertRowsToObjects <T>());
        }
Exemple #17
0
        public void GenerateVarsLocalCSV()
        {
            var items = DeadSimpleCSV.ConvertStringCSVToObjects <VarSpec>(localCSV.text, true);

            GenerateVars(items);
        }