예제 #1
0
파일: Compiler.cs 프로젝트: rds1983/Jord
        private void FirstRun(IEnumerable <string> sources)
        {
            if (CompilerParams.Verbose)
            {
                TJ.LogInfo("{0} source files found", sources.Count());
            }

            // First run - parse json and build object maps
            foreach (var s in sources)
            {
                if (CompilerParams.Verbose)
                {
                    TJ.LogInfo("Processing {0}", s);
                }

                JObject json;

                try
                {
                    json = JObject.Parse(File.ReadAllText(s));
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("JSON parsing error. Source '{0}'. Error '{1}'",
                                                      s, ex.ToString()));
                }

                foreach (var pair in json)
                {
                    var key = pair.Key;

                    if (key == TileSetName)
                    {
                        if (json.Count > 1)
                        {
                            throw new Exception(string.Format("Tileset file can have only one tileset entry. Source: '{0}'", s));
                        }

                        var    obj = (JObject)pair.Value;
                        JToken idToken;
                        if (!obj.TryGetValue(IdName, out idToken))
                        {
                            throw new Exception(string.Format("Tileset object lacks id. Source: '{0}'", s));
                        }

                        var id = idToken.ToString();

                        _loaders[typeof(TileSet)].SafelyAddObject(id, s, (JObject)pair.Value);

                        continue;
                    }
                    else if (key == MapName)
                    {
                        if (json.Count > 1)
                        {
                            throw new Exception(string.Format("Map file can have only one map entry. Source: '{0}'", s));
                        }

                        var    obj = (JObject)pair.Value;
                        JToken idToken;
                        if (!obj.TryGetValue(IdName, out idToken))
                        {
                            throw new Exception(string.Format("Map object lacks id. Source: '{0}'", s));
                        }

                        var id = idToken.ToString();

                        _loaders[typeof(Map)].SafelyAddObject(id, s, (JObject)pair.Value);

                        continue;
                    }
                    else if (key == ModuleInfoName)
                    {
                        var obj = (JObject)pair.Value;
                        _moduleInfo = new ObjectData
                        {
                            Source = s,
                            Data   = obj
                        };

                        continue;
                    }
                    else if (key == LevelsName)
                    {
                        var arr = (JArray)pair.Value;
                        foreach (JObject levelObject in arr)
                        {
                            var levelCost = new LevelCost(
                                levelObject.EnsureInt("Level"),
                                levelObject.EnsureInt("Experience"),
                                levelObject.EnsureInt("Gold")
                                );

                            _module.LevelCosts[levelCost.Level] = levelCost;
                        }

                        continue;
                    }

                    BaseLoader loader = null;
                    foreach (var pair2 in _loaders)
                    {
                        if (pair2.Value.JsonArrayName == key)
                        {
                            loader = pair2.Value;
                            break;
                        }
                    }

                    if (loader == null)
                    {
                        throw new Exception(string.Format("Unknown object type '{0}', source: '{1}", key, s));
                    }

                    var properties = new Dictionary <string, string>();

                    JToken token;
                    if (((JObject)pair.Value).TryGetValue(PropertiesName, out token))
                    {
                        foreach (var pair2 in (JObject)token)
                        {
                            properties[pair2.Key] = pair2.Value.ToString();
                        }
                    }

                    foreach (var pair2 in (JObject)pair.Value)
                    {
                        if (pair2.Key == PropertiesName)
                        {
                            continue;
                        }

                        loader.SafelyAddObject(pair2.Key, s, (JObject)pair2.Value, properties);
                    }
                }
            }
        }