示例#1
0
 public void AddColumn(CastleColumn col)
 {
     if (col.TypeID == CastleType.UniqueIdentifier)
     {
         IDColumn = col;
     }
     Columns.Add(col);
 }
示例#2
0
        public CastleColumn Clone()
        {
            CastleColumn clone = new CastleColumn {
                Name = this.Name, Key = this.Key, TypeID = this.TypeID
            };

            return(clone);
        }
示例#3
0
 void FillSheetData(CastleSheet targetSheet, JArray linesArray)
 {
     foreach (JObject line in linesArray)
     {
         CastleLine newLine = new CastleLine();
         foreach (JProperty property in line.Properties())
         {
             string       propName = property.Name;
             CastleColumn col      = targetSheet.Columns.FirstOrDefault(c => c.Name.Equals(propName));
             if (col == null)
             {
                 continue;
             }
             if (col.TypeID == CastleType.List)
             {
                 string      newSheetName = string.Format("{0}@{1}", targetSheet.Name, col.Name);
                 CastleSheet newTarget    = Sheets.FirstOrDefault(s => s.Name.Equals(newSheetName)).Clone();
                 if (newTarget != null)
                 {
                     FillSheetData(newTarget, property.Value as JArray);
                 }
                 newLine.Values.Add(newTarget);
             }
             else if (col.TypeID == CastleType.Custom)
             {
                 CastleCustom customType = CustomTypes.FirstOrDefault(c => c.Name.Equals(col.Key));
                 if (customType != null)
                 {
                     JArray           valArray = property.Value as JArray;
                     CastleCustomInst inst     = new CastleCustomInst();
                     for (int i = 0; i < valArray.Count; ++i)
                     {
                         JToken token     = valArray[i];
                         string tokenText = token.ToString();
                         if (i == 0)
                         {
                             inst.ConstructorIndex = int.Parse(tokenText);
                         }
                         else
                         {
                             inst.Parameters.Add(tokenText);
                         }
                     }
                 }
                 else
                 {
                     throw new Exception("Unable to find custom type: " + col.Key);
                 }
             }
             else if (col.TypeID == CastleType.Image)
             {
                 string strValue = property.Value.ToString();
                 if (strValue.Contains(":"))
                 {
                     string[] words = strValue.Split(':');
                     newLine.Values.Add(new CastleImage {
                         MD5 = words[0], Path = words[1]
                     });
                 }
                 else
                 {
                     newLine.Values.Add(new CastleImage {
                         MD5 = strValue, Path = ""
                     });
                 }
             }
             else if (col.TypeID == CastleType.TilePos)
             {
                 JObject tileObj = property.Value as JObject;
                 if (tileObj != null)
                 {
                     newLine.Values.Add(new CastleTilePos {
                         File   = tileObj.Property("file").Value.ToString(),
                         Size   = int.Parse(tileObj.Property("size").Value.ToString()),
                         X      = int.Parse(tileObj.Property("x").Value.ToString()),
                         Y      = int.Parse(tileObj.Property("y").Value.ToString()),
                         Width  = tileObj.Property("width") != null ? int.Parse(tileObj.Property("width").Value.ToString()) : 0,
                         Height = tileObj.Property("height") != null ? int.Parse(tileObj.Property("height").Value.ToString()) : 0
                     });
                 }
             }
             else if (col.TypeID == CastleType.Layer)
             {
             }
             else if (col.TypeID == CastleType.TileLayer)
             {
             }
             else if (col.TypeID == CastleType.Dynamic)
             {
                 // Just straight add the JToken to it
                 newLine.Values.Add(property.Value);
             }
             else if (col.TypeID == CastleType.Ref)
             {
                 newLine.Values.Add(property.Value.ToString());
             }
             else
             {
                 newLine.Values.Add(property.Value.ToString());
             }
         }
     }
 }
示例#4
0
        void LoadSheets(JArray sheetsArray)
        {
            if (sheetsArray == null)
            {
                throw new Exception("Unexpected data for sheets");
            }
            foreach (JObject sheet in sheetsArray)
            {
                string    sheetName = sheet.Property("name").Value.ToString();
                JProperty cols      = sheet.Property("columns");
                //TODO create CastleSheet and CastleColumns
                CastleSheet newSheet = new CastleSheet {
                    Name = sheetName
                };
                if (cols.Value is JArray)
                {
                    JArray colsArray = cols.Value as JArray;
                    foreach (JObject column in colsArray)
                    {
                        string typeID   = column.Property("typeStr").Value.ToString();
                        string key      = "";
                        string colName  = column.Property("name").Value.ToString();
                        string enumText = "";
                        if (typeID.Contains(':'))
                        {
                            string[] words = typeID.Split(CastleColumn.STRSPLIT, StringSplitOptions.RemoveEmptyEntries);
                            typeID = words[0];
                            if (words[1].Contains(','))
                            {
                                enumText = words[1];
                            }
                            else
                            {
                                key = words[1];
                            }
                        }
                        CastleColumn newColumn = new CastleColumn {
                            Name = colName, TypeID = (CastleType)Enum.Parse(typeof(CastleType), typeID), Key = key
                        };
                        if (enumText.Length > 0)
                        {
                            newColumn.Enumerations.AddRange(enumText.Split(','));
                        }
                        newSheet.AddColumn(newColumn);
                    }
                }
                Sheets.Add(newSheet);
            }

            // Iterate again to fill data
            foreach (JObject sheet in sheetsArray)
            {
                string      sheetName   = sheet.Property("name").Value.ToString();
                CastleSheet targetSheet = Sheets.FirstOrDefault(p => p.Name.Equals(sheetName));
                if (targetSheet == null)
                {
                    continue;
                }
                JProperty lines = sheet.Property("lines");
                if (lines.Value is JArray)
                {
                    JArray linesArray = lines.Value as JArray;
                    if (linesArray != null)
                    {
                        FillSheetData(targetSheet, linesArray);
                    }
                }
            }

            // Now that all "base" data is loaded, resolve the references
            foreach (CastleSheet sheet in Sheets)
            {
                foreach (CastleLine line in sheet.Lines)
                {
                    for (int i = 0; i < sheet.Columns.Count; ++i)
                    {
                        CastleColumn col = sheet.Columns[i];
                        if (col.TypeID == CastleType.Ref)
                        {
                            string      text        = line.Values[i].ToString();
                            CastleSheet lookupSheet = Sheets.FirstOrDefault(s => s.Name.Equals(col.Key));
                            if (lookupSheet != null)
                            {
                                line.Values[i] = new CastleRef {
                                    Referencedstring = text, ReferenceLine =
                                        lookupSheet.Lines.FirstOrDefault(l => l.Values[lookupSheet.IndexOfID()].Equals(text))
                                };
                            }
                            else
                            {
                                line.Values[i] = null;
                            }
                        }
                    }
                }
            }
        }