public override Stream BeginRead(CarbonFile file) { var stream = this.database.FileStorage.OpenRead(file.GetPathUsingAlternativeSeparator()); this.openStreams.Add(stream); return(stream); }
// ------------------------------------------------------------------- // Protected // ------------------------------------------------------------------- protected override void DoProcess(CarbonFile source) { string content = source.ReadAsString(); string[] segments = content.Split(StripFromTemplates, StringSplitOptions.RemoveEmptyEntries); content = string.Join(" ", segments); this.templateSegments.Add($"{source.FileNameWithoutExtension}: '{content}'"); }
// ------------------------------------------------------------------- // Public // ------------------------------------------------------------------- public ProjectFile(CarbonFile template) { this.project = new XmlDocument(); this.project.Load(template.GetPath()); this.PrepareProject(); }
protected override bool RegisterCommandLineArguments() { ICommandLineSwitchDefinition definition = this.Arguments.Define("s", "script", x => this.scriptFileName = new CarbonFile(x)); definition.RequireArgument = true; definition.Description = "The script to process"; return true; }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public AddonContent(CarbonFile file, CarbonDirectory rootDirectory) { this.File = file; this.RootDirectory = rootDirectory; this.SubContent = new List<AddonContent>(); }
public virtual void Write(CarbonFile file, byte[] data) { using (var stream = this.BeginWrite(file)) { stream.Write(data, 0, data.Length); stream.Flush(); } }
// ------------------------------------------------------------------- // Protected // ------------------------------------------------------------------- protected override void DoProcess(CarbonFile source) { string relativeRootPath = source.GetDirectory().GetPath().Replace(this.GetContext<JavaScriptBuildingContext>().Root.GetPath(), string.Empty); string fileName = source.FileName; string fileNameId = this.BuildPathId(relativeRootPath + source.FileNameWithoutExtension); string rootPathId = this.BuildPathId(relativeRootPath, true); this.GetContext<JavaScriptBuildingContext>().Cache.RegisterImage(fileNameId, $@"staticData.imageRoot{rootPathId} + ""{fileName}"""); }
public static XmlDocument ReadAddonXml(CarbonFile file) { var document = new XmlDocument(); XmlParserContext context = new XmlParserContext(null, XmlNamespace, string.Empty, XmlSpace.Default); XmlReader reader = XmlReader.Create(file.GetPath(), XmlSettings, context); document.Load(reader); return document; }
public override CarbonFile[] Find(string pattern) { CarbonFileResult[] results = this.Root.GetFiles(pattern, SearchOption.AllDirectories); CarbonFile[] result = new CarbonFile[results.Length]; for (var i = 0; i < results.Length; i++) { result[i] = results[i].Relative; } return(result); }
public override Stream BeginWrite(CarbonFile file) { CarbonFile fullFile = this.Root.ToFile(file); fullFile.GetDirectory().Create(); var stream = fullFile.OpenCreate(); this.openStreams.Add(stream); return(stream); }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public DBFileProvider(CarbonFile file) { if (!file.Exists) { // Ensure the directory exists file.GetDirectory().Create(); } this.database = new LiteDatabase(file.GetPath()); this.openStreams = new List <LiteFileStream>(); }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public void AddInclude(CarbonFile file) { System.Diagnostics.Trace.Assert(this.project.DocumentElement != null); XmlNode node = this.project.CreateElement("Compile", this.project.DocumentElement.NamespaceURI); System.Diagnostics.Trace.Assert(node.Attributes != null); XmlAttribute attribute = this.project.CreateAttribute("Include"); attribute.Value = file.GetPath(); node.Attributes.Append(attribute); this.projectReferenceParent.AppendChild(node); }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public Main(IFactory factory) : base(factory) { this.factory = factory; this.config = factory.Resolve<IConfig>(); this.existingMetaFiles = new List<CarbonFile>(); this.configFile = new CarbonFile(Constants.ConfigFileName); this.mode = MetaUtilityMode.Check; }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public MCMod(CarbonFile file) { this.File = file; try { this.ReloadModInfo(); } catch (Exception e) { Diagnostic.Error("Failed to read Mod info for {0}: {1}", file, e); } }
public override Stream BeginRead(CarbonFile file) { CarbonFile fullFile = this.Root.ToFile(file); if (fullFile.Exists) { var stream = fullFile.OpenRead(); this.openStreams.Add(stream); return(stream); } return(null); }
protected override bool RegisterCommandLineArguments() { ICommandLineSwitchDefinition definition = this.arguments.Define("s", "sourceFile", x => this.sourceFile = new CarbonFile(x)); definition.Required = true; definition.RequireArgument = true; definition.Description = "The source file to process"; definition = this.arguments.Define("m", "mode", x => this.mode = (ProcessingMode)Enum.Parse(typeof(ProcessingMode), x)); definition.RequireArgument = true; definition.Description = "The processing mode"; return true; }
public virtual void Read(CarbonFile file, out byte[] data) { using (var stream = this.BeginRead(file)) { if (stream == null) { data = null; return; } data = new byte[stream.Length]; stream.Read(data, 0, data.Length); } }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public AddonEntry(CarbonFile tocFile) { this.Name = tocFile.FileNameWithoutExtension; this.TocFile = tocFile; this.RootDirectory = tocFile.GetDirectory(); this.Meta = new Dictionary<string, string>(); this.Contents = new Dictionary<CarbonFile, AddonContent>(); this.Resources = new List<CarbonFile>(); this.Dependencies = new List<AddonEntry>(); }
// ------------------------------------------------------------------- // Protected // ------------------------------------------------------------------- protected override void DoProcess(CarbonFile source) { if (!this.formatterTargetLookup.ContainsKey(this.GetContext<JavaScriptBuildingContext>().TargetPlatform)) { Diagnostic.Warning("No Excel Formatter for target Platform {0}", this.GetContext<JavaScriptBuildingContext>().TargetPlatform); return; } ExcelData sourceData = ExcelProcessor.Process(source); if (sourceData != null) { this.data.Add(sourceData); } }
protected override void StartFinished() { this.configFile = new CarbonFile(ConfigFileName); this.config.Load(this.configFile); if (this.config.Current.Root == null || !this.config.Current.Root.Exists) { Diagnostic.Error("Root directory is invalid or does not exist"); return; } string source = this.config.Current.SourcePath; string target = this.config.Current.TargetPath; bool force = this.config.Current.Force; bool server = this.config.Current.Server; if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) { Diagnostic.Error("Invalid source or target path"); return; } CarbonFile syncExecutable = this.config.Current.Root.ToFile(MCSyncExecutable); if (!syncExecutable.Exists) { Diagnostic.Error("Could not locate MCSync executable"); return; } StringBuilder arguments = new StringBuilder(); arguments.AppendFormat("-s \"{0}\" -t \"{1}\"", source, target); if (force) { arguments.Append(" -force"); } if (server) { arguments.Append(" -server"); } using (var syncProcess = new Process()) { syncProcess.StartInfo = new ProcessStartInfo(syncExecutable.GetPath(), arguments.ToString()) { UseShellExecute = false }; syncProcess.Start(); syncProcess.WaitForExit(); } }
protected override bool RegisterCommandLineArguments() { ICommandLineSwitchDefinition definition = this.Arguments.Define("p", "projectFile", x => this.configFileName = new CarbonFile(x)); definition.RequireArgument = true; definition.Description = "The project file to compile"; definition = this.Arguments.Define("d", "debug", x => this.useDebug = true); definition.Description = "Build with debug info enabled"; definition = this.Arguments.Define("c", "closure", x => this.useClosure = true); definition.Description = "Run closure on the target script file"; definition = this.Arguments.Define("l", "language", x => this.targetLanguage = x); definition.RequireArgument = true; definition.Description = "Set the language to build (en, fr, de ...)"; return true; }
private static AddonEntry ReadToc(CompileContext context, CarbonFile file) { var entry = new AddonEntry(file); context.CurrentScanEntry = entry; using (var stream = file.OpenRead()) { using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); ParseTocLine(context, line); } } } context.FinalizeScannedEntry(); return entry; }
protected override bool RegisterCommandLineArguments() { ICommandLineSwitchDefinition definition = this.Arguments.Define("d", "directory", x => this.projectDirectory = new CarbonDirectory(x)); definition.RequireArgument = true; definition.Required = true; definition.Description = "The directory to run on"; definition = this.Arguments.Define("c", "configFile", x => this.configFile = new CarbonFile(x)); definition.RequireArgument = true; definition.Description = "Specify a custom MetaFileUtility config"; definition = this.Arguments.Define("m", "mode", x => CarbonCore.Utils.Edge.EnumExtensions.TryParseInvariant(x, out this.mode)); definition.RequireArgument = true; definition.Description = "Show info about the folder and the environment"; definition = this.Arguments.Define("p4", x => this.enableP4Check = true); definition.Description = "Enable check of meta files against p4"; return true; }
// ------------------------------------------------------------------- // Protected // ------------------------------------------------------------------- protected override void DoProcess(CarbonFile source) { // Skip template scripts if (source.FileNameWithoutExtension.EndsWith("template", StringComparison.OrdinalIgnoreCase)) { return; } var localContext = new JavaScriptProcessingContext(source); string content = source.ReadAsString(); this.ProcessSource(localContext, ref content); // In debug mode append the file name of the source if (this.GetContext<JavaScriptBuildingContext>().IsDebug) { this.AppendFormatLine("// {0}", source.FileNameWithoutExtension); } this.AppendLine(content); }
protected override bool RegisterCommandLineArguments() { // -I Partials -I Overrides -o ..\..\..\..\Source -p ..\..\..\..\SharpMC.jtlproj ICommandLineSwitchDefinition definition = this.Arguments.Define("p", "projectFile", x => this.projectFileName = new CarbonFile(x)); definition.RequireArgument = true; definition.Required = true; definition.Description = "The project file to compile"; definition = this.Arguments.Define("o", "output", x => this.targetDirectory = new CarbonDirectory(x)); definition.RequireArgument = true; definition.Required = true; definition.Description = "Target directory for the compilation"; definition = this.Arguments.Define("I", "include", x => this.includes.Add(new CarbonDirectory(x))); definition.RequireArgument = true; definition.AllowMultiple = true; definition.Description = "Include directories for files to be included in the project file"; definition = this.Arguments.Define("s", "skipProjectUpdate", x => this.skipProjectUpdate = true); definition.Description = "Skip project file re-generation"; definition.RequireArgument = false; return true; }
private static bool WriteXmlContentToTarget(CarbonFile source, CarbonFile target) { bool isEmpty = true; XmlDocument document = AddonXmlUtils.ReadAddonXml(source); XmlNode root = document.DocumentElement; IList<XmlNode> deleteList = new List<XmlNode>(); foreach (XmlNode childNode in root.ChildNodes) { if (childNode.Name.Equals(Constants.XmlNodeScript, StringComparison.OrdinalIgnoreCase) || childNode.Name.Equals(Constants.XmlNodeInclude, StringComparison.OrdinalIgnoreCase) || childNode.NodeType == XmlNodeType.Comment) { deleteList.Add(childNode); continue; } isEmpty = false; } if (isEmpty) { return false; } foreach (XmlNode node in deleteList) { root.RemoveChild(node); } target.GetDirectory().Create(); document.Save(target.GetPath()); return true; }
// ------------------------------------------------------------------- // Public // ------------------------------------------------------------------- public void BuildProjectFile(IList<CarbonFileResult> sources, CarbonFile target, IProcessingContext context) { Diagnostic.Info("Building {0} Sources into {2}", sources.Count, target); }
private IList<Token> TokenizeFile(CarbonFile file) { IList<Token> tokens; var tokenizer = new Tokenizer(); using (new ProfileRegion("Tokenize")) { using (var stream = file.OpenRead()) { using (var reader = new StreamReader(stream, Encoding.UTF8, false, 4096, true)) { tokens = tokenizer.Tokenize(this.grammar, reader); } } } return tokens; }
// ------------------------------------------------------------------- // Private // ------------------------------------------------------------------- private void TranslateFile(TempProject project, CarbonFile sourceFile, CarbonFile targetFile) { Diagnostic.RegisterThread("Translate." + sourceFile.FileName); CarbonDirectory targetRelativeSubDir = targetFile.GetDirectory() ?? new CarbonDirectory(string.Empty); CarbonDirectory fullTargetPath = this.targetDirectory.ToDirectory(this.config.Current.IntermediateSubDirectory, targetRelativeSubDir); CarbonFile fullTargetFile = fullTargetPath.ToFile(targetFile.FileName + TempExtension); CarbonFile relativeTargetFile = this.config.Current.IntermediateSubDirectory.ToDirectory(targetRelativeSubDir).ToFile(targetFile.FileName + TempExtension); if (this.config.Current.Verbose) { System.Diagnostics.Trace.TraceInformation("Translating {0} -> {1}", sourceFile, fullTargetFile); } IList<Token> tokens = this.TokenizeFile(sourceFile); if (tokens == null) { throw new InvalidDataException(); } project.AddStat(TempProjectStat.Files); project.AddStat(TempProjectStat.Tokens, tokens.Count); var fileEntry = new TempProjectFileEntry { IsCompressed = this.config.Current.CompressIntermediate, RelativeFile = new CarbonFile(targetFile.FileName), RelativePath = targetFile.GetDirectory(), File = relativeTargetFile, }; var data = new TranslationData(tokens) { ProjectData = project, SourceFile = sourceFile, TargetFile = fullTargetFile, FileEntry = fileEntry }; // Register the file entry into the project project.AddFile(fileEntry); try { TempFileFull file = this.TranslateToTempFile(data); this.SaveTempFile(data, file); } catch (Exception e) { System.Diagnostics.Trace.TraceError("Failed to translate {0}: {1}", fileEntry.File, e); this.SaveTempFile(data, new TempFileFull() { Name = data.TargetFile.FileName }); } Diagnostic.UnregisterThread(); }
public void AddResource(CarbonFile file) { if (this.Resources.Contains(file)) { Diagnostic.Warning("File {0} was already in contents of addon {1}", file, this.Name); return; } this.Resources.Add(file); }
private static AddonContent ReadXMLContent(CarbonFile file, CarbonDirectory rootDirectory) { CarbonFile absoluteFile = rootDirectory.ToFile(file); if (!absoluteFile.Exists) { Diagnostic.Warning("Could not find content file {0}", absoluteFile); return null; } try { var result = new AddonContent(file, rootDirectory); XmlDocument document = AddonXmlUtils.ReadAddonXml(absoluteFile); XmlNode root = document.DocumentElement; foreach (XmlNode node in root.ChildNodes) { if (node.Name.Equals(Constants.XmlNodeScript, StringComparison.OrdinalIgnoreCase)) { string nestedContent = node.Attributes[Constants.XmlScriptFileAttribute].Value; CarbonDirectory nestedRoot = file.GetDirectory() == null ? rootDirectory : rootDirectory.ToDirectory(file.GetDirectory()); result.SubContent.Add(new AddonContent(new CarbonFile(nestedContent), nestedRoot)); continue; } if (node.Name.Equals(Constants.XmlNodeInclude, StringComparison.OrdinalIgnoreCase)) { string nestedContent = node.Attributes[Constants.XmlIncludeFileAttribute].Value; CarbonFile nestedContentFile = new CarbonFile(nestedContent.Trim()); if (nestedContentFile.Extension == Constants.ExtensionLua) { CarbonDirectory nestedRoot = file.GetDirectory() == null ? rootDirectory : rootDirectory.ToDirectory(file.GetDirectory()); result.SubContent.Add(new AddonContent(nestedContentFile, nestedRoot)); continue; } AddonContent nestedEntry = ReadXMLContent( new CarbonFile(nestedContent), absoluteFile.GetDirectory()); if (nestedEntry != null) { result.SubContent.Add(nestedEntry); } } } return result; } catch (Exception e) { Diagnostic.Error("Could not read content XML {0}\n{1}", file, e); return null; } }
private bool CheckDirectoryWithoutMeta() { bool result = true; using (new ProfileRegion("CheckDirectoryWithoutMeta")) { CarbonDirectoryResult[] directoryResults = this.assetDirectory.GetDirectories(options: SearchOption.AllDirectories); Diagnostic.Info("Checking Meta files for {0} Directories...", directoryResults.Length); foreach (CarbonDirectoryResult directory in directoryResults) { CarbonFile directoryMeta; CarbonDirectory parentDirectory = directory.Absolute.GetParent(); if (parentDirectory != null) { directoryMeta = parentDirectory.ToFile( directory.Absolute.DirectoryNameWithoutPath + Constants.ExtensionMeta); } else { directoryMeta = new CarbonFile(directory.Relative.DirectoryNameWithoutPath + Constants.ExtensionMeta); } if (!directoryMeta.Exists) { Diagnostic.Error("Missing Meta file for Directory {0}", directory.Relative); result = false; } } } return result; }
// ------------------------------------------------------------------- // Private // ------------------------------------------------------------------- private void DoTranslate() { CarbonDirectory sourceDirectory = this.config.Current.SourceDirectory; if (this.config.Current.SourceDirectory.IsRelative) { sourceDirectory = this.config.Current.ProjectRoot.ToDirectory(sourceDirectory); } CarbonFileResult[] sources = sourceDirectory.GetFiles(this.config.Current.Filter, SearchOption.AllDirectories); System.Diagnostics.Trace.TraceInformation("Found {0} files to translate", sources.Length); IDictionary<CarbonFileResult, CarbonFile> files = new Dictionary<CarbonFileResult, CarbonFile>(); for (int i = 0; i < sources.Length; i++) { CarbonFileResult source = sources[i]; #if DEBUG /*if (!source.Contains(@"\BaseMetadataSectionSerializer.")) { continue; }*/ /*if (!source.Contains(@"\Render.") && !source.Contains(@"\RenderEnderman.") && !source.Contains(@"\RendererLivingEntity.") && !source.Contains(@"\RenderLiving.")) { continue; }*/ #endif bool ignore = false; foreach (string ignorePattern in MCData.IgnoreList) { if (source.Relative.Contains(ignorePattern)) { ignore = true; break; } } if (ignore) { continue; } CarbonFile targetFile = new CarbonFile(source.Relative.FileName).ChangeExtension(CarbonCore.Utils.Constants.ExtensionCSharp); var subDirectory = new CarbonDirectory(string.Empty); foreach (string pattern in MCData.StructureMapping.Keys) { if (source.Relative.StartsWith(pattern)) { subDirectory = new CarbonDirectory(MCData.StructureMapping[pattern]); break; } } targetFile = subDirectory.ToFile(targetFile); files.Add(source, targetFile); } var translation = this.factory.Resolve<ITranslation>(); translation.Translate(files); System.Diagnostics.Trace.TraceInformation(" Report: "); System.Diagnostics.Trace.TraceInformation(" Tokens: {0}", translation.TokenCount); System.Diagnostics.Trace.TraceInformation(" after Translation: {0}", translation.LinesTranslated); }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public JavaScriptProcessingContext(CarbonFile source) { this.DirectiveStack = new Stack<ProcessingInstructions>(); this.UsingVars = new Dictionary<string, int>(); this.SourceName = source.FileNameWithoutExtension; }
// ------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------- public CompileContent(AddonEntry addon, CarbonFile absolute, CarbonFile relative) { this.Addon = addon; this.AbsoluteFile = absolute; this.RelativeFile = relative; }
public abstract Stream BeginRead(CarbonFile file);
public abstract Stream BeginWrite(CarbonFile file);
private static bool WriteScriptContentToTarget(CompileContent content, CarbonFile target) { IList<string> contents = new List<string>(); using (var stream = content.AbsoluteFile.OpenRead()) { using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); contents.Add(line); } } } for (int i = 0; i < contents.Count; i++) { string line = contents[i]; string trimmedLine = line.Trim().TrimStart('\t'); if (trimmedLine.StartsWith("--")) { continue; } Match match = AddonInitParamRegex.Match(line); if (match.Success) { string replacementLine = string.Concat(match.Groups[1].Value, $"GetAddonGlobalArgs(\"{content.Addon.Name}\")", match.Groups[3].Value); contents[i] = replacementLine; Diagnostic.Warning("Adjusting lua parameters: {0}, line {1}\n{2}\n{3}", content.AbsoluteFile, i, line, replacementLine); } } target.WriteAsString(string.Join(Environment.NewLine, contents)); return true; }
private void WriteSchematic(CarbonFile targetFile) { var sizeVector = this.outputBoundingBox.Maximum - this.outputBoundingBox.Minimum + new Vector3(1); int maxAddress = (int)(sizeVector.Y * sizeVector.Z * sizeVector.X); using (var file = File.Open(targetFile.GetPath(), FileMode.Create, FileAccess.Write, FileShare.Read)) { var writer = new BinaryTagWriter(file); var compound = new TagCompound("Schematic"); compound.Value.Add(new TagShort("Height", (short)sizeVector.Y)); compound.Value.Add(new TagShort("Length", (short)sizeVector.Z)); compound.Value.Add(new TagShort("Width", (short)sizeVector.X)); compound.Value.Add(new TagList("Entities")); compound.Value.Add(new TagList("TileEntities")); compound.Value.Add(new TagString("Materials", "Alpha")); var blockArray = new byte[maxAddress]; var biomeArray = new byte[(int)(sizeVector.Z * sizeVector.X)]; var dataArray = new byte[maxAddress]; foreach (Block block in this.blocks.Keys) { // (Y×length + Z)×width + X int address = this.GetInternalAddress(sizeVector, new Vector3(block.X, block.Y, block.Z)); blockArray[address] = 41; // Gold block for testing dataArray[address] = 0; } /*int count = 0; for (var x = 0; x < sizeVector.X; x++) { for (var y = 0; y < sizeVector.Y; y++) { for (var z = 0; z < sizeVector.Z; z++) { int test = this.GetInternalAddress(sizeVector, new Vector3(x, y, z)); blockArray[test] = 23; count++; } } }*/ /*for (int i = 0; i < sizeVector.Y; i++) { //blockArray[i] = 1; int test = this.GetInternalAddress(sizeVector, new Vector3(i, i, i)); blockArray[test] = 1; }*/ compound.Value.Add(new TagByteArray("Data", dataArray)); compound.Value.Add(new TagByteArray("Biomes", biomeArray)); compound.Value.Add(new TagByteArray("Blocks", blockArray)); writer.Write(compound); } }