public async Task RunAsync() { var directory = new DirectoryInfo("root"); var archive = new FileSystemArchive(directory); bool skipToken = false; archive.OnEntryChanged += (args) => { skipToken = true; }; while (true) { Console.Clear(); // Console.WriteLine(); // Console.WriteLine(); DrawTree(archive); while (!skipToken) { Thread.Sleep(10); } skipToken = false; } }
public void ExportFoldersToDirectory(BuildPipeline pipeline, string path) { var directoryInfo = new DirectoryInfo(path); directoryInfo.Create(); var buildProcess = new ProjectBuildProcess(pipeline, this, directoryInfo.FullName); string folderPath = Path.Combine(directoryInfo.FullName, $"{Definition.Properties.Name}"); var archive = new FileSystemArchive(new DirectoryInfo(folderPath)); buildProcess.PerformBuild(archive.RootDirectory); }
public static void Install() { // get gamepath and Mod folder string gamepath = Path.GetFullPath(".\\"); string ModFolder = Path.Combine(gamepath, "Mod"); ArcFileSystem fs = new ArcFileSystem(); // Iterate through each file in Mod folder and add it to the proxy ARC file foreach (string file in Directory.GetFiles(ModFolder, "*.ks", SearchOption.AllDirectories)) { string name = Path.GetFileName(file); ArcFileEntry arcFile = !fs.FileExists(file) ? fs.CreateFile(name) : fs.GetFile(file); arcFile.Pointer = new WindowsFilePointer(file); } foreach (string file in Directory.GetFiles(ModFolder, "*.ogg", SearchOption.AllDirectories)) { string name = Path.GetFileName(file); ArcFileEntry arcFile = !fs.FileExists(file) ? fs.CreateFile(name) : fs.GetFile(file); arcFile.Pointer = new WindowsFilePointer(file); } // Save the ARC file on disk because the game does not support loading ARC from meory easily // the temp ARC is saved in ML_temp folder in game's folder, so as to not get in the way of general mods if (!Directory.Exists(Path.Combine(gamepath, "ML_temp"))) { Directory.CreateDirectory(Path.Combine(gamepath, "ML_temp")); } using (FileStream fStream = File.Create(Path.Combine(gamepath, "ML_temp\\ML_temp.arc"))) { fs.Save(fStream); } // Get the game's file system and add our custom ARC file FileSystemArchive gameFileSystem = GameUty.FileSystem as FileSystemArchive; gameFileSystem.AddArchive(Path.Combine(gamepath, "ML_temp\\ML_temp.arc")); // load custom arcs, cuz yolo foreach (string arc in Directory.GetFiles(ModFolder, "*.arc", SearchOption.AllDirectories)) { gameFileSystem.AddArchive(arc); } }
public static async Task <ProjectExplorer> LoadAsync(string projectPath, ImportPipeline importPipeline) { string bprojPath = null; if (projectPath.EndsWith(".bproj")) { bprojPath = projectPath; projectPath = new DirectoryInfo(projectPath).Parent.FullName; } else { foreach (string rootFile in Directory.EnumerateFiles(projectPath, "*.bproj")) { bprojPath = rootFile; break; } } var archive = new FileSystemArchive(new DirectoryInfo(projectPath)); var resources = new Dictionary <string, ProjectResource>(); var rootDirectiory = new ProjectDirectory("", "", null); var projectExplorer = new ProjectExplorer { Archive = archive, Definition = ProjectDefinition.Load(bprojPath), Resources = new ProjectResourceCollection(resources), RootDirectory = rootDirectiory }; void ImportDirectory(IArchiveDirectory directory, ProjectDirectory projectDirectory) { foreach (var childDirectory in directory.Directories) { ImportDirectory(childDirectory, new ProjectDirectory(childDirectory.Name, childDirectory.FullName, projectDirectory)); } foreach (var file in directory.Files) { if (file.FullName.StartsWith("bin/") || file.FullName.EndsWith(".bproj") || !importPipeline.IsResource(file)) { continue; } var resource = importPipeline.ImportResource(projectExplorer, projectDirectory, file, file.FullName); projectExplorer.Resources.Add(resource.FullName, resource); projectDirectory.Resources.Add(resource.Name, resource); } } ImportDirectory(archive.RootDirectory, rootDirectiory); foreach (var resource in projectExplorer.Resources) { foreach (var dependency in resource.Dependencies) { var dependencyResource = dependency.Resource; if (dependencyResource == null) { Console.WriteLine($"ERROR: Unable to resolve dependency for \"{dependency.Key}\""); } else { dependencyResource.Dependants.dependencies.Add(new ProjectResourceDependency(projectExplorer, resource.FullName, dependency.metdadata)); } } } // Size Calculation long uncompressedSize = 0; foreach (var resource in projectExplorer.Resources) { uncompressedSize += resource.UncompressedSize; } projectExplorer.UncompressedSize = uncompressedSize; // Tag Indexing var tags = new Dictionary <string, IResourceCollection>(); foreach (var resource in projectExplorer.Resources) { foreach (string tag in resource.Tags) { if (!tags.TryGetValue(tag, out var taggedResourcesCollection)) { taggedResourcesCollection = new ProjectResourceCollection(); tags[tag] = taggedResourcesCollection; } var taggedResources = (ProjectResourceCollection)taggedResourcesCollection; taggedResources.Add(resource.FullName, resource); } } projectExplorer.Tags = new PackageTagsCollection(tags); return(projectExplorer); }
public static async Task <ProjectExplorer> LoadAsync(string projectPath, ImportPipeline importPipeline) { string bprojPath = null; if (projectPath.EndsWith(".bproj")) { bprojPath = projectPath; projectPath = new DirectoryInfo(projectPath).Parent.FullName; } else { foreach (string rootFile in Directory.EnumerateFiles(projectPath, "*.bproj")) { bprojPath = rootFile; break; } } var archive = new FileSystemArchive(new DirectoryInfo(projectPath)); var resources = new Dictionary <string, ProjectResource>(); var rootDirectiory = new ProjectDirectory("", "", null); ProjectDirectory ForPath(string path) { int currentIndex = 0; var currentDirectory = rootDirectiory; while (true) { int nextIndex = path.IndexOf('/', currentIndex); if (nextIndex == -1) { break; } string segment = path.Substring(currentIndex, nextIndex - currentIndex); bool found = false; foreach (var directory in currentDirectory.Directories) { if (directory.Name == segment) { currentDirectory = directory; found = true; break; } } if (!found) { var newDirectory = new ProjectDirectory(segment, path, currentDirectory); currentDirectory.Directories.Add(newDirectory); currentDirectory = newDirectory; } currentIndex = nextIndex + 1; } return(currentDirectory); } var projectExplorer = new ProjectExplorer { Archive = archive, Definition = ProjectDefinition.Load(bprojPath), Resources = new ProjectResourceCollection(resources), RootDirectory = rootDirectiory }; // Resources foreach (var projectEntry in archive.Files) { if (projectEntry.FullName.StartsWith("bin/") || projectEntry.FullName.EndsWith(".bproj")) { continue; } if (!importPipeline.IsResource(projectEntry)) { continue; } var forPath = ForPath(projectEntry.FullName); var resource = importPipeline.ImportResource(projectExplorer, forPath, projectEntry, projectEntry.FullName); projectExplorer.Resources.Add(resource.FullName, resource); forPath.Resources.Add(resource.Name, resource); } foreach (var resource in projectExplorer.Resources) { foreach (var dependency in resource.Dependencies) { var dependencyResource = dependency.Resource; if (dependencyResource == null) { Console.WriteLine($"ERROR: Unable to resolve dependency for \"{dependency.Key}\""); } else { dependencyResource.Dependants.dependencies.Add(new ProjectResourceDependency(projectExplorer, resource.FullName, dependency.metdadata)); } } } // Size Calculation long uncompressedSize = 0; foreach (var resource in projectExplorer.Resources) { uncompressedSize += resource.UncompressedSize; } projectExplorer.UncompressedSize = uncompressedSize; // Tag Indexing var tags = new Dictionary <string, IResourceCollection>(); foreach (var resource in projectExplorer.Resources) { foreach (string tag in resource.Tags) { if (!tags.TryGetValue(tag, out var taggedResourcesCollection)) { taggedResourcesCollection = new ProjectResourceCollection(); tags[tag] = taggedResourcesCollection; } var taggedResources = (ProjectResourceCollection)taggedResourcesCollection; taggedResources.Add(resource.FullName, resource); } } projectExplorer.Tags = new PackageTagsCollection(tags); return(projectExplorer); }
public static Task <PackageExplorer> LoadFromDirectoryAsync(string directory) { var archive = new FileSystemArchive(new DirectoryInfo(directory)); return(LoadAsync(archive.RootDirectory)); }
public static async Task <ProjectExplorer> LoadAsync(string projectPath, ImportPipeline importPipeline) { string bprojPath = null; if (projectPath.EndsWith(".bproj")) { bprojPath = projectPath; projectPath = new DirectoryInfo(projectPath).Parent.FullName; } else { foreach (string rootFile in Directory.EnumerateFiles(projectPath, "*.bproj")) { bprojPath = rootFile; break; } } var sourceArchive = new FileSystemArchive(new DirectoryInfo(projectPath)); var resources = new Dictionary <string, ProjectResource>(); var rootDirectiory = new ProjectDirectory(null, null); var projectExplorer = new ProjectExplorer { Archive = sourceArchive, Definition = ProjectDefinition.Load(bprojPath), Resources = new ProjectResourceCollection(resources), RootDirectory = rootDirectiory, ImportPipeline = importPipeline }; // Organise temporary directory var tempDirectory = new DirectoryInfo(Path.Combine(projectPath, "../temp")); tempDirectory.Create(); var tempArchive = new FileSystemArchive(tempDirectory, false); void ApplyUpdate(ProjectResourceUpdate update) { string[] elements = update.ProjectKey.Split(new char[] { '/' }); var placementDirectory = rootDirectiory; for (int i = 0; i < elements.Length - 1; i++) { string element = elements[i]; var parent = placementDirectory; placementDirectory = placementDirectory.Directories[element]; if (placementDirectory == null) { placementDirectory = new ProjectDirectory(placementDirectory, element); parent.Directories.Add(placementDirectory); } } string fileName = elements[elements.Length - 1]; // Locate or create resource to apply updates to. if (!placementDirectory.Resources.TryGetResource(fileName, out var projectResource)) { projectResource = new ProjectResource(projectExplorer, placementDirectory, update.ProjectKey); projectExplorer.Resources.Add(projectResource.FullName, projectResource); placementDirectory.Resources.Add(projectResource.Name, projectResource); } // Apply updates to resource projectResource.Tags.importerTags.AddRange(update.ImporterTags); foreach (var importerDependency in update.Dependencies) { projectResource.Dependencies.dependencies.Add( new ProjectResourceDependency(projectExplorer, importerDependency.Resource, importerDependency.Metadata)); } if (update.FileContent != null) { projectResource.Content = new ProjectResourceContent(update.FileContent); } if (update.DeferredContent != null) { string tempId = Guid.NewGuid().ToString(); var tempFile = tempArchive.RootDirectory.Files.GetOrCreateFile(tempId); using (var tempOutput = tempFile.OpenWrite()) { update.DeferredContent.WriteContentAsync(tempOutput).Wait(); } projectResource.Content = new ProjectResourceContent(tempFile); } } // Import resources foreach (var update in importPipeline.ImportDirectory(projectExplorer, sourceArchive.RootDirectory)) { ApplyUpdate(update); } // Attach dependencies foreach (var resource in projectExplorer.Resources) { foreach (var dependency in resource.Dependencies) { var dependencyResource = dependency.Resource; if (dependencyResource == null) { Console.WriteLine($"ERROR: Unable to resolve dependency for \"{dependency.Key}\""); } else { dependencyResource.Dependants.dependencies.Add(new ProjectResourceDependency(projectExplorer, resource.FullName, dependency.metdadata)); } } } // Process Resoures var importProcessorContext = new ImportProcessorContext() { Explorer = projectExplorer }; foreach (var processor in importPipeline.importProcessors) { var preExistingResources = new List <ProjectResource>(); foreach (var resource in projectExplorer.Resources) { preExistingResources.Add(resource); } foreach (var resource in preExistingResources) { var maxThread = new SemaphoreSlim(32); var tasks = new List <Task>(); if (processor.CanProcess(resource)) { maxThread.Wait(); tasks.Add(Task.Factory.StartNew(() => { var updates = processor.ProcessImport(importProcessorContext, resource); if (updates != null) { foreach (var update in updates) { ApplyUpdate(update); } } }, TaskCreationOptions.LongRunning) .ContinueWith((task) => { tasks.Remove(task); return(maxThread.Release()); })); } Task.WaitAll(tasks.ToArray()); } } // Size Calculation long uncompressedSize = 0; foreach (var resource in projectExplorer.Resources) { uncompressedSize += resource.Content.UncompressedSize; } projectExplorer.UncompressedSize = uncompressedSize; // Tag Indexing var tags = new Dictionary <string, IResourceCollection>(); foreach (var resource in projectExplorer.Resources) { foreach (string tag in resource.Tags) { if (!tags.TryGetValue(tag, out var taggedResourcesCollection)) { taggedResourcesCollection = new ProjectResourceCollection(); tags[tag] = taggedResourcesCollection; } var taggedResources = (ProjectResourceCollection)taggedResourcesCollection; taggedResources.Add(resource.FullName, resource); } } projectExplorer.Tags = new ProjectTagsCollection(tags); return(projectExplorer); }
public async Task StartAsync() { // Import the project var importPipeline = ImportPipeline.Create() .UseImporter(new BoardGameResourceImporter()) .UseJsonMetaFiles(options => { options.IsMetaFilesOptional = true; }) .Build(); var projectExplorer = ProjectExplorer.Load("Content/BoardGame", importPipeline); // Build the project to a package. var consoleRenderer = new BuildConsoleRenderer(); var buildPipeline = new BuildPipeline(); buildPipeline.BuildActions.Add(consoleRenderer); consoleRenderer.DrawProgressBar(32); projectExplorer.ExportZippedToDirectory(buildPipeline, "BoardGame/Temp"); var packageExplorer = PackageExplorer.LoadFromFileAsync("BoardGame/Temp/BoardGame.bpkg").Result; var dest = new FileSystemArchive(new DirectoryInfo("BoardGame/Temp")); await packageExplorer.Source.CopyIntoAsync(dest.RootDirectory, "Fast"); var cottage = packageExplorer.Resources["buildings/cottage.json"]; foreach (var dep in cottage.Dependencies) { Console.WriteLine($"{dep.Key}: {dep.Resource?.Name}"); } Step(); var gameServer = new GameServer(); gameServer.StartHosting(projectExplorer); var playerA = LocalId.NewShortId(); var playerB = LocalId.NewShortId(); gameServer.OnClientConnected(playerA, "Player A"); gameServer.OnClientConnected(playerB, "Player B"); DrawGameState(gameServer.Lobby); Step(); gameServer.AcceptInput(playerA, new StartGameCommand() { }); DrawGameState(gameServer.Lobby); Step(); gameServer.AcceptInput(playerA, new DeclareResourceCommand() { ResourceIdentifier = "x" }); DrawGameState(gameServer.Lobby); Step(); gameServer.AcceptInput(playerA, new PlaceResourceCommand() { ResourceIdentifier = "x", ResourcePosition = new Integer2(2, 2) }); DrawGameState(gameServer.Lobby); Step(); gameServer.AcceptInput(playerB, new PlaceResourceCommand() { ResourceIdentifier = "x", ResourcePosition = new Integer2(3, 1) }); DrawGameState(gameServer.Lobby); Step(); gameServer.AcceptInput(playerA, new BuildBuildingCommand() { BuildingIdentifier = "cottage", BuildingPosition = new Integer2(1, 1), Offset = new Integer2(1, 1), }); DrawGameState(gameServer.Lobby); Step(); gameServer.AcceptInput(playerB, new EndTurnCommand()); DrawGameState(gameServer.Lobby); }