/// <summary> /// Factory Method. /// Creates a project in the specified empty directory and initializes it. /// </summary> /// <param name="dir">Directory, where to create the new project.</param> /// <returns></returns> public static void Create(String dir, String name) { // Create directory name from project name String dirName = name; foreach (var c in Path.GetInvalidPathChars()) dirName = dirName.Replace(c, '_'); foreach (var c in Path.GetInvalidFileNameChars()) dirName = dirName.Replace(c, '_'); dirName = dirName.Replace(' ', '-'); // Combine both names dir = Path.Combine(dir, dirName); // Create dir if it does not exist if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // Check if it is empty if (Directory.GetFiles(dir).Length != 0 || Directory.GetDirectories(dir).Length != 0) throw new Exception("New projects can only be created in an empty directory!"); // Initialize Subdirectories Directory.CreateDirectory(Path.Combine(dir, RESOURCE_DIR)); // Create Resources Folder Directory.CreateDirectory(Path.Combine(dir, FRAMES_DIR)); // Create Frames Folder Directory.CreateDirectory(Path.Combine(dir, SEQUENCES_DIR)); // Create Sequences Folder Directory.CreateDirectory(Path.Combine(dir, EVENTS_DIR)); // Create Events Folder // Create Project Descriptor File // TODO Serialize instead of writing //File.WriteAllText(Path.Combine(dir, PROJECT_FILE), String.Format(Resources.DefaultProjectDescriptor, name)); var model = new ProjectModel() { FPS = 10, Name = name, Resources = { }, FrameFiles = {"\\frames\\default.bxframe"}, SequenceFiles = {"\\sequences\\default.bxseq"}, EventFiles = {"\\events\\default.bxevent"} }; ProjectModel.Serialize(Path.Combine(dir, PROJECT_FILE), model); // Create Default Files File.WriteAllText(Path.Combine(dir, FRAMES_DIR, "default.bxframe"), "{ }"); File.WriteAllText(Path.Combine(dir, SEQUENCES_DIR, "default.bxseq"), "{ }"); File.WriteAllText(Path.Combine(dir, EVENTS_DIR, "default.bxevent"), "{ }"); // Load Default Project instance = new AnimatProject(Path.Combine(dir, PROJECT_FILE)); }
/// <summary> /// Serializes the Project Model to a file. /// </summary> /// <param name="path">Path of the file.</param> public static void Serialize(String path, ProjectModel model) { // Make sure that the parent directory exists if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path)); // Serialize using (var stream = new StreamEx(path, FileMode.Create)) { var serializer = new DataContractJsonSerializer(typeof(ProjectModel)); serializer.WriteObject(stream, model); } }