private void GetRulesets(string document, string file, bool isRoot, ImportedFile cache) { string absolute = new Uri(file).AbsolutePath; // If this file was already loaded, don't load it again. if (Imported.Contains(absolute)) { return; } Imported.Add(absolute); Diagnostics.AddFile(file); // Get the ruleset. RulesetNode ruleset; if (cache == null) { ruleset = GetRuleset(file, document); } else if (cache.Update() || cache.Cache == null) { ruleset = GetRuleset(file, cache.Content); cache.Cache = ruleset; } else { ruleset = (RulesetNode)cache.Cache; } Rulesets.Add(file, ruleset); // Get the imported files. if (ruleset != null && !Diagnostics.ContainsErrors()) { ReservedGlobalIDs.AddRange(ruleset.ReservedGlobalIDs); ReservedPlayerIDs.AddRange(ruleset.ReservedPlayerIDs); if (ruleset.ReservedGlobal != null) { ReservedGlobalIDs.AddRange(ruleset.ReservedGlobal.ReservedIDs); ReservedGlobalNames.AddRange(ruleset.ReservedGlobal.ReservedNames); } if (ruleset.ReservedPlayer != null) { ReservedPlayerIDs.AddRange(ruleset.ReservedPlayer.ReservedIDs); ReservedPlayerNames.AddRange(ruleset.ReservedPlayer.ReservedNames); } List <string> importedFiles = new List <string>(); foreach (ImportNode importNode in ruleset.Imports) { try { Importer importer = new Importer(Diagnostics, importedFiles, importNode.File, file, importNode.Location); if (!importer.AlreadyImported) { GetRulesets(null, importer.ResultingPath, false, importer.FileData); importedFiles.Add(importer.ResultingPath); } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } } }
private void GetObjects(string document, string file, TranslateRule globalTranslate, TranslateRule playerTranslate, bool isRoot) { // If this file was already loaded, don't load it again. if (Imported.Contains(file)) { return; } Imported.Add(file); Diagnostics.AddFile(file); // Get the ruleset. RulesetNode ruleset = GetRuleset(file, document); Rulesets.Add(file, ruleset); if (ruleset != null && !Diagnostics.ContainsErrors()) { if (isRoot) { VarCollection = new VarCollection(ruleset.UseGlobalVar, ruleset.UsePlayerVar, ruleset.UseBuilderVar); Root = new ScopeGroup(VarCollection); } // Get the defined types foreach (var definedType in ruleset.DefinedTypes) { try { if (DefinedTypes.Any(type => type.Name == definedType.Name)) { throw SyntaxErrorException.NameAlreadyDefined(definedType.Location); } DefinedTypes.Add(DefinedType.GetDefinedType(definedType)); } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the user methods. for (int i = 0; i < ruleset.UserMethods.Length; i++) { try { UserMethods.Add(new UserMethod(Root, ruleset.UserMethods[i])); } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the rules RuleNodes.AddRange(ruleset.Rules); List <string> importedFiles = new List <string>(); foreach (ImportObjectNode importObject in ruleset.ObjectImports) { try { Importer importer = new Importer(Diagnostics, importedFiles, importObject.File, file, importObject.Location); if (!importer.AlreadyImported) { importedFiles.Add(importer.ResultingPath); string content = importer.GetFile(); switch (importer.FileType) { case ".obj": Model newModel = Model.ImportObj(content); new ModelVar(importObject.Name, Root, importObject, newModel); break; } } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Check the imported files. foreach (ImportNode importNode in ruleset.Imports) { try { Importer importer = new Importer(Diagnostics, importedFiles, importNode.File, file, importNode.Location); if (!importer.AlreadyImported) { string content = File.ReadAllText(importer.ResultingPath); GetObjects(content, importer.ResultingPath, globalTranslate, playerTranslate, false); importedFiles.Add(importer.ResultingPath); } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the variables foreach (var definedVar in ruleset.DefinedVars) { try { IndexedVar var; if (definedVar.UseVar == null) { var = VarCollection.AssignVar(Root, definedVar.VariableName, definedVar.IsGlobal, definedVar); } else { var = VarCollection.AssignVar( Root, definedVar.VariableName, definedVar.IsGlobal, definedVar.UseVar.Variable, definedVar.UseVar.Index, definedVar ); } if (definedVar.Type != null) { var.Type = GetDefinedType(definedVar.Type, definedVar.Location); } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } } }