Exemplo n.º 1
0
        public void SaveExporterFiles(Dictionary <string, byte[]> files)
        {
            // TODO the dictionary string should be converted into a Workspace path

            // Save all the files to the disk
            foreach (var file in files)
            {
                try
                {
                    // Anything that is not in the "/Workspace/" root is routed to save into the tmp directory
                    var path = WorkspacePath.Parse(file.Key);

//                    Console.WriteLine("Save Exported file " + file.Key + " to " + path);

                    Stream stream;


                    if (Exists(path))
                    {
                        stream = OpenFile(path, FileAccess.ReadWrite);
                    }
                    else
                    {
                        // Make sure we have the correct directory structure
                        if (path.IsFile)
                        {
                            // Get the parent path
                            var parent = path.ParentPath;

                            if (!Exists(parent))
                            {
                                this.CreateDirectoryRecursive(parent);
                            }
                        }

                        // Create a new file
                        // Create a new file
                        stream = CreateFile(path);
                    }

                    // TODO need to write to the file
                    if (file.Value != null)
                    {
                        // Clear the file contents before writing to it
                        stream.SetLength(0);

                        // Write the byte data to it
                        stream.Write(file.Value);

                        stream.Close();
                        // TODO make sure we dispose of the stream?
                        stream.Dispose();
                    }
                }
                catch
                {
//                    Console.WriteLine("Couldn't save " + file.Key + "\n" + e.Message);
                }
            }
        }
Exemplo n.º 2
0
        public string FindValidSavePath(string gamePath)
        {
            string savePath;

            var filePath = WorkspacePath.Parse(gamePath);


            var parentFilePath = filePath.ParentPath;

            var writeAccess = WriteAccess(parentFilePath);

            if (writeAccess)
            {
                savePath = "/Game/";

                if (filePath.IsFile)
                {
                    savePath += filePath.EntityName;
                }
            }
            else
            {
                savePath = "/Tmp" + gamePath;
            }

//            Console.WriteLine("Save Path " + savePath);

            return(savePath);
        }
Exemplo n.º 3
0
        public Dictionary <string, object> CreateZipFile(WorkspacePath path, Dictionary <WorkspacePath, WorkspacePath> files)
        {
            var fileHelper  = new WorkspaceFileLoadHelper(this);
            var zipExporter = new ZipExporter(path.Path, fileHelper, files);

            zipExporter.CalculateSteps();

            while (zipExporter.completed == false)
            {
                zipExporter.NextStep();
            }

            try
            {
                if ((bool)zipExporter.Response["success"])
                {
                    var zipPath = WorkspacePath.Parse(zipExporter.fileName);

                    SaveExporterFiles(new Dictionary <string, byte[]>()
                    {
                        { zipExporter.fileName, zipExporter.bytes }
                    });
                }
            }
            catch (Exception e)
            {
                // Change the success to false
                zipExporter.Response["success"] = false;
                zipExporter.Response["message"] = e.Message;
            }


            return(zipExporter.Response);
        }
Exemplo n.º 4
0
        public string[] LoadGame(string path)
        {
            var filePath = WorkspacePath.Parse(path);
            var exits    = Exists(filePath);

            string[] files = null;

            if (exits)
            {
                // Found disk to load
                if (filePath.IsDirectory)
                {
                    currentDisk = new SubFileSystem(this, filePath);
                }
                else if (filePath.IsFile)
                {
                    // TODO need to figure out how to do this from a disk now without the currentDisk drive?
                    if (archiveExtensions.IndexOf(filePath.Path.Split('.').Last()) > -1)
                    {
                        using (var stream = OpenFile(filePath, FileAccess.ReadWrite))
                        {
                            if (stream is FileStream)
                            {
                                currentDisk = ZipFileSystem.Open((FileStream)stream);
                            }
                            else
                            {
                                currentDisk = ZipFileSystem.Open(stream, path);
                            }

                            stream.Close();
                        }
                    }
                }

                // We need to get a list of the current mounts

                if (Mounts is SortedList <WorkspacePath, IFileSystem> mounts)
                {
                    // Create a new mount point for the current game
                    var rootPath = WorkspacePath.Root.AppendDirectory("Game");

                    // Make sure we don't have a disk with the same name
                    if (mounts.ContainsKey(rootPath))
                    {
                        mounts.Remove(rootPath);
                    }

                    mounts.Add(rootPath, currentDisk);

                    // Filter out only the files we can use and convert this into a dictionary with the file name as the key and the path as the value
                    files = GetGameEntities(rootPath);
                }
            }

            return(files);
        }
Exemplo n.º 5
0
        public int GenerateSprites(string path, PixelVisionEngine targetGame)
        {
            var count = 0;

            var filePath = WorkspacePath.Parse(path);

            var srcPath = filePath.AppendDirectory("SpriteBuilder");

            var fileData = new Dictionary <string, byte[]>();

            if (Exists(srcPath))
            {
                // Get all the files in the folder
                var files = from file in GetEntities(srcPath)
                            where file.GetExtension() == ".png"
                            select file;

                foreach (var file in files)
                {
                    var name = file.EntityName.Substring(0, file.EntityName.Length - file.GetExtension().Length);

                    var bytes = OpenFile(file, FileAccess.Read).ReadAllBytes();

                    if (fileData.ContainsKey(name))
                    {
                        fileData[name] = bytes;
                    }
                    else
                    {
                        fileData.Add(name, bytes);
                    }

                    count++;
//                    Console.WriteLine("Parse File " + name);
                }

                try
                {
                    // TODO exporting sprites doesn't work
                    if (locator.GetService(typeof(ExportService).FullName) is ExportService exportService)
                    {
                        exportService.ExportSpriteBuilder(path + "sb-sprites.lua", targetGame, fileData);
                        //
                        exportService.StartExport();
                    }
                }
                catch (Exception e)
                {
                    // TODO this needs to go through the error system?
                    Console.WriteLine(e);
                    throw;
                }
            }

            return(count);
        }
        public void EjectDisk(string path)
        {
            ejectingDisk = true;

            workspaceServicePlus.EjectDisk(WorkspacePath.Parse(path));

            UpdateDiskInBios();

            AutoLoadDefaultGame();

            ejectingDisk = false;
        }
Exemplo n.º 7
0
        public byte[] ReadAllBytes(string file)
        {
            var path = WorkspacePath.Parse(file);

            using (var memoryStream = new MemoryStream())
            {
                using (var fileStream = WorkspaceService.OpenFile(path, FileAccess.Read))
                {
                    fileStream.CopyTo(memoryStream);
                    fileStream.Close();
                }

                return(memoryStream.ToArray());
            }
        }
Exemplo n.º 8
0
        // Exports the active song in the music chip
        public void ExportSong(string path, MusicChip musicChip, SoundChip soundChip, int id)
        {
            var currentSong = musicChip.songs[id];

            var selectedPatterns = new int[currentSong.end];

            Array.Copy(currentSong.patterns, selectedPatterns, selectedPatterns.Length);

            var filePath = WorkspacePath.Parse(path);

            if (Exists(filePath))
            {
                filePath = filePath.AppendDirectory("Wavs").AppendDirectory("Songs");

                if (!Exists(filePath))
                {
                    CreateDirectory(filePath);
                }

                try
                {
                    filePath = UniqueFilePath(filePath.AppendFile("song " + id + " - " + currentSong.name + ".wav"));

                    Console.WriteLine("Export song to " + filePath);


                    // TODO exporting sprites doesn't work
                    if (locator.GetService(typeof(ExportService).FullName) is ExportService exportService)
                    {
                        exportService.ExportSong(filePath.Path, musicChip, soundChip, selectedPatterns);
                        //
                        exportService.StartExport();
                    }
                }
                catch (Exception e)
                {
                    // TODO this needs to go through the error system?
                    Console.WriteLine(e);
                    throw;
                }

                // TODO saving song doesn't work
//                runner.exportService.ExportSong(filePath.Path, musicChip, soundChip);
//
//                runner.StartExport();
            }
        }
Exemplo n.º 9
0
        public string AutoRunGameFromDisk(string diskName)
        {
            var diskPath = WorkspacePath.Root.AppendDirectory("Disks")
                           .AppendDirectory(diskName);

            var autoRunPath = diskPath.AppendFile("info.json");

            // Try to read the disk's info file and see if there is an auto run path
            try
            {
                // Only run a disk if there is an auto run file in there
                if (Exists(autoRunPath))
                {
                    var json = ReadTextFromFile(autoRunPath);

                    var autoRunData = Json.Deserialize(json) as Dictionary <string, object>;

                    var tmpPath = autoRunData["AutoRun"] as string;

                    // Get the auto run from the json file
                    var newDiskPath = WorkspacePath.Parse($"/Disks/{diskName}{tmpPath}");

                    // Change the disk path to the one in the auto-run file
                    if (Exists(newDiskPath))
                    {
                        diskPath = newDiskPath;
                    }
                }
            }
            catch
            {
                // ignored
            }

            // Always validate that the disk is a valid game before trying to load it.
            if (ValidateGameInDir(diskPath))
            {
                // TODO need to make sure the auto run disk is at the top of the list

                // Move the new disk to the top of the list
                var diskPaths = new List <string>();

                // Add the remaining disks
                foreach (var disk in Disks)
                {
                    diskPaths.Add(DiskPhysicalRoot(disk));
                }

                // Remove the old disks
                EjectAll();

                // Mount all the disks
                foreach (var oldPath in diskPaths)
                {
                    MountDisk(oldPath);
                }

                return(diskPath.Path);
            }

            return(null);
        }
Exemplo n.º 10
0
 public ZipDiskExporter(string fileName, WorkspaceService workspaceService) : base(fileName)
 {
     diskPath = WorkspacePath.Parse(fileName);
     this.workspaceService = workspaceService;
     this.FileLoadHelper   = new WorkspaceFileLoadHelper(this.workspaceService);
 }
 public void OS_FileMove(string src, string dst)
 {
     workspace.Move(WorkspacePath.Parse(src), WorkspacePath.Parse(dst));
 }
 public bool OS_FileExists(string file)
 {
     return(workspace.Exists(WorkspacePath.Parse(file)));
 }
        public Stream IO_OpenFile(Script script, string filename, Encoding encoding, string mode)
        {
            // TODO need to convert mode to file system mode

            return(workspace.OpenFile(WorkspacePath.Parse(filename), FileAccess.Read));
        }
Exemplo n.º 14
0
 public string GetFileName(string path)
 {
     return(WorkspacePath.Parse(path).EntityName);
 }
Exemplo n.º 15
0
        public Dictionary <string, byte[]> LoadGame(string path)
        {
            var filePath = WorkspacePath.Parse(path); //FileSystemPath.Root.AppendPath(fullPath);
            var exits    = Exists(filePath);

            Dictionary <string, byte[]> files = null;

            if (exits)
            {
                try
                {
                    // Found disk to load
                    if (filePath.IsDirectory)
                    {
                        currentDisk = new SubFileSystem(this, filePath);
                    }
                    else if (filePath.IsFile)
                    {
                        if (archiveExtensions.IndexOf(filePath.Path.Split('.').Last()) > -1)
                        {
                            using (var stream = OpenFile(filePath, FileAccess.ReadWrite))
                            {
                                if (stream is FileStream)
                                {
                                    currentDisk = ZipFileSystem.Open((FileStream)stream);
                                }
                                else
                                {
                                    currentDisk = ZipFileSystem.Open(stream, path);
                                }

                                stream.Close();
                            }
                        }
                    }

                    // We need to get a list of the current mounts
                    var mounts = Mounts as SortedList <WorkspacePath, IFileSystem>;

                    // Create a new mount point for the current game
                    var rootPath = WorkspacePath.Root.AppendDirectory("Game");

                    // Make sure we don't have a disk with the same name
                    if (mounts.ContainsKey(rootPath))
                    {
                        mounts.Remove(rootPath);
                    }

                    mounts.Add(rootPath, currentDisk);

                    files = ConvertDiskFilesToBytes(currentDisk);

                    IncludeLibDirectoryFiles(files);


                    try
                    {
                        // Convert the path to a system path
                        var tmpFilePath = WorkspacePath.Parse(path);


                        // TODO should we still try to load the saves file from a zip?

                        // If the path is a directory we are going to look for a save file in it
                        if (tmpFilePath.IsDirectory)
                        {
                            tmpFilePath = tmpFilePath.AppendFile("saves.json");

//                        if (WriteAccess(tmpFilePath) == false)
//                        {
                            // Check if save file is in tmp directory
                            var saveFile = WorkspacePath.Parse(FindValidSavePath(tmpFilePath.Path));

                            if (saveFile.Path != "/" && Exists(saveFile))
                            {
                                using (var memoryStream = new MemoryStream())
                                {
                                    using (var file = OpenFile(saveFile, FileAccess.Read))
                                    {
                                        file.CopyTo(memoryStream);
                                        file.Close();
                                    }

                                    var fileName = saveFile.EntityName;
                                    var data     = memoryStream.ToArray();

                                    if (files.ContainsKey(fileName))
                                    {
                                        files[fileName] = data;
                                    }
                                    else
                                    {
                                        files.Add(fileName, data);
                                    }

                                    memoryStream.Close();
                                }
                            }
                        }


//                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }


//                    return true;
                }
                catch
                {
                    //		        // TODO need to have a clearer messgae, like not a mount point or can't load from X because of Y
//                    Console.WriteLine("System Error: Could not load from path " + filePath.Path);
                }
            }

            return(files);
        }
Exemplo n.º 16
0
        /// <summary>
        ///     This will read a text file from a valid workspace path and return it as a string. This can read .txt, .json and
        ///     .lua files.
        /// </summary>
        /// <param name="path">A valid workspace path.</param>
        /// <returns>Returns the contents of the file as a string.</returns>
        public string ReadTextFile(string path)
        {
            var filePath = WorkspacePath.Parse(path);

            return(workspace.ReadTextFromFile(filePath));
        }
Exemplo n.º 17
0
        public bool SaveTextToFile(string filePath, string text, bool autoCreate = false)
        {
            var path = WorkspacePath.Parse(filePath);

            return(workspace.SaveTextToFile(path, text, autoCreate));
        }
 public void OS_FileDelete(string file)
 {
     workspace.Delete(WorkspacePath.Parse(file));
 }